aboutsummaryrefslogtreecommitdiff
path: root/src/utils.c
blob: e33defd4352503c59ba42f02aca608f468730f15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 * utils.c
 *
 * Utility stuff
 *
 * (c) 2006-2009 Thomas White <thomas.white@desy.de>
 *
 * pattern_sim - Simulate diffraction patterns from small crystals
 *
 */

#include <math.h>
#include <string.h>
#include <stdio.h>

#include "utils.h"


/* Return the MOST POSITIVE of two numbers */
unsigned int biggest(signed int a, signed int b)
{
	if ( a>b ) {
		return a;
	}
	return b;
}


/* Return the LEAST POSITIVE of two numbers */
unsigned int smallest(signed int a, signed int b)
{
	if ( a<b ) {
		return a;
	}
	return b;
}


double distance(double x1, double y1, double x2, double y2)
{
	return sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
}


double modulus(double x, double y, double z)
{
	return sqrt(x*x + y*y + z*z);
}


double modulus_squared(double x, double y, double z) {
	return x*x + y*y + z*z;
}


double distance3d(double x1, double y1, double z1,
                  double x2, double y2, double z2)
{
	return modulus(x1-x2, y1-y2, z1-z2);
}


/* Angle between two vectors.  Answer in radians */
double angle_between(double x1, double y1, double z1,
                     double x2, double y2, double z2)
{
	double mod1 = modulus(x1, y1, z1);
	double mod2 = modulus(x2, y2, z2);
	return acos( (x1*x2 + y1*y2 + z1*z2) / (mod1*mod2) );
}


/* As above, answer in degrees */
double angle_between_d(double x1, double y1, double z1,
                       double x2, double y2, double z2)
{
	return rad2deg(angle_between(x1, y1, z1, x2, y2, z2));
}


/* Wavelength of an electron (in m) given accelerating potential (in V) */
double lambda(double V)
{
	double m = 9.110E-31;
	double h = 6.625E-34;
	double e = 1.60E-19;
	double c = 2.998E8;

	return h / sqrt(2*m*e*V*(1+((e*V) / (2*m*c*c))));
}


size_t skipspace(const char *s)
{
	size_t i;

	for ( i=0; i<strlen(s); i++ ) {
		if ( (s[i] != ' ') && (s[i] != '\t') ) return i;
	}

	return strlen(s);
}


void chomp(char *s)
{
	size_t i;

	if ( !s ) return;

	for ( i=0; i<strlen(s); i++ ) {
		if ( (s[i] == '\n') || (s[i] == '\r') ) {
			s[i] = '\0';
			return;
		}
	}
}


int sign(double a)
{
	if ( a < 0 ) return -1;
	if ( a > 0 ) return +1;
	return 0;
}


void mapping_rotate(double x, double y, double z,
                    double *ddx, double *ddy, double *ddz,
                    double omega, double tilt)
{
	double nx, ny, nz;
	double x_temp, y_temp, z_temp;

	/* First: rotate image clockwise until tilt axis is aligned
	 * horizontally. */
	nx = x*cos(omega) + y*sin(omega);
	ny = -x*sin(omega) + y*cos(omega);
	nz = z;

	/* Now, tilt about the x-axis ANTICLOCKWISE around +x, i.e. the
	 * "wrong" way. This is because the crystal is rotated in the
	 * experiment, not the Ewald sphere. */
	x_temp = nx; y_temp = ny; z_temp = nz;
	nx = x_temp;
	ny = cos(tilt)*y_temp + sin(tilt)*z_temp;
	nz = -sin(tilt)*y_temp + cos(tilt)*z_temp;

	/* Finally, reverse the omega rotation to restore the location of the
	 * image in 3D space */
	x_temp = nx; y_temp = ny; z_temp = nz;
	nx = x_temp*cos(-omega) + y_temp*sin(-omega);
	ny = -x_temp*sin(-omega) + y_temp*cos(-omega);
	nz = z_temp;

	*ddx = nx;
	*ddy = ny;
	*ddz = nz;
}


void progress_bar(int val, int total)
{
	double frac;
	int n, i;
	char s[1024];

	frac = (double)val/total;
	n = (int)(frac*80);

	for ( i=0; i<n; i++ ) s[i] = '=';
	for ( i=n; i<80; i++ ) s[i] = '.';
	s[80] = '\0';
	printf("\r|%s|", s);

	if ( val == total ) printf("\n");
	
	fflush(stdout);
}