aboutsummaryrefslogtreecommitdiff
path: root/src/reproject.c
blob: 4f0dd3c8341bc1e509eaf0e9c1721901727e4760 (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
/*
 * reproject.c
 *
 * Synthesize diffraction patterns
 *
 * (c) 2007 Thomas White <taw27@cam.ac.uk>
 *
 *  dtr - Diffraction Tomography Reconstruction
 *
 */

#include <stdlib.h>
#include <math.h>

#include "control.h"
#include "reflections.h"
#include "utils.h"

#define MAX_IMAGE_REFLECTIONS 8*1024

ImageReflection *reproject_get_reflections(ImageRecord image, size_t *n, ReflectionContext *rctx, ControlContext *ctx) {

	ImageReflection *refl;
	Reflection *reflection;
	int i;
	double smax = 0.5e9;
	double tilt, omega;
	double xt, yt, zt;
	double nx, ny, nz;
	double kx, ky, kz;
			
	tilt = deg2rad(image.tilt);
	omega = deg2rad(image.omega);
	
	refl = malloc(MAX_IMAGE_REFLECTIONS*sizeof(ImageReflection));
	
	i = 0;
	
	reflection = rctx->reflections;
	
	/* Calculate the (normalised) incident electron wavevector */
	xt = 0;  yt = sin(tilt);  zt = cos(tilt);
	nx = xt*cos(omega) + yt*-sin(omega);
	ny = xt*sin(omega) + yt*cos(omega);
	nz = zt;
	kx = nx / image.lambda;
	ky = ny / image.lambda;
	kz = nz / image.lambda;	/* This is the centre of the Ewald sphere */
	
	reflection_add(ctx->reflectionctx, kx, ky, kz, 1, REFLECTION_VECTOR_MARKER_1);
	
	do {
	
		double xl, yl, zl;
		double a, b, c;
		double A1, A2, s1, s2, s;
		
		/* Get the coordinates of the reciprocal lattice point */
		xl = reflection->x;
		yl = reflection->y;
		zl = reflection->z;
		
		/* Next, solve the relrod equation to calculate the excitation error */
		a = 1.0;
		b = -2.0*(xl*nx + yl*ny + zl*nz);
		c = xl*xl + yl*yl + zl*zl - 1.0/(image.lambda*image.lambda);
		A1 = (-b + sqrt(b*b-4.0*a*c))/(2.0*a);
		A2 = (-b - sqrt(b*b-4.0*a*c))/(2.0*a);
		s1 = 1.0/image.lambda - A1;
		s2 = 1.0/image.lambda - A2;
		
		if ( fabs(s1) < fabs(s2) ) s = s1; else s = s2;
		
		/* Skip this reflection if s is large */
		if ( fabs(s) <= smax ) {
		
			double xi, yi, zi;
			double gx, gy, gz;
			double cx, cy, cz;
			double ux, uy, uz;
			double uyt, uzt;
			double theta;
			double x, y;
			double rx, ry, rz;
			
			/* Determine the intersection point */
			xi = xl + s*nx;  yi = yl + s*ny;  zi = zl + s*nz;
			
			/* Calculate Bragg angle */
			gx = xi - kx;
			gy = yi - ky;
			gz = zi - kz;	/* This is the vector from the centre of the sphere to the intersection */
			theta = angle_between(-kx, -ky, -kz, gx, gy, gz);
			
			if ( theta > 0.0 ) {
			
				double psi, disc;
			
				reflection_add(ctx->reflectionctx, xl, yl, zl, 1, REFLECTION_GENERATED);
				reflection_add(ctx->reflectionctx, xi, yi, zi, 1, REFLECTION_MARKER);
				
				/* Determine where "up" is */
				uyt = cos(tilt);	uzt = -sin(tilt);  /* tilt it (uxt not needed) */
				ux = uyt*-sin(omega);	uy = uyt*cos(omega);	uz = uzt;  /* rotate it */
				
				if ( i == 0 ) reflection_add(ctx->reflectionctx, ux*50, uy*50, uz*50, 1, REFLECTION_VECTOR_MARKER_2);
				
				/* Calculate azimuth of point in image (clockwise from "up", will be changed later) */
				cx = nz*yi - ny*zi;  cy = nx*zi - nz*xi;  cz = ny*xi - nx*yi;  /* c = (-n) x i */
				psi = angle_between(cx, cy, cz, ux, uy, uz);
				
				/* Finally, resolve the +/- Pi ambiguity from the previous step */
				rx = nz*cy - ny*cz;  ry = nx*cz - nz*cx;  rz = ny*cx - nx*cy;  /* r = (-n) x [(-n) x i] */
				disc = angle_between(rx, ry, rz, ux, uy, uz);
				if ( (psi >= M_PI_2) && (disc >= M_PI_2) ) psi -= M_PI_2;		/* Case 1 */
				else if ( (psi >= M_PI_2) && (disc < M_PI_2) ) psi = 3*M_PI_2 - psi;	/* Case 2 */
				else if ( (psi < M_PI_2) && (disc < M_PI_2) ) psi = 3*M_PI_2 - psi;	/* Case 3 */
				else if ( (psi < M_PI_2) && (disc >= M_PI_2) ) psi = 3*M_PI_2 + psi;	/* Case 4 */
				psi = M_PI_2 - psi;	/* Anticlockwise from "+x" instead of clockwise from "up" */
				
				/* Calculate image coordinates from polar representation */
				if ( image.fmode == FORMULATION_CLEN ) {
					x = image.camera_len*sin(theta)*cos(psi);
					y = image.camera_len*sin(theta)*sin(psi);
					x *= image.resolution;
					y *= image.resolution;
				} else if ( image.fmode == FORMULATION_PIXELSIZE ) {
					x = sin(theta)*cos(psi) / image.lambda;
					y = sin(theta)*sin(psi) / image.lambda;
					x /= image.pixel_size;
					y /= image.pixel_size;
				} else {
					fprintf(stderr, "Unrecognised formulation mode in reproject_get_reflections()\n");
					return NULL;
				}
				
				/* Adjust centre */
				x += image.x_centre;
				y += image.y_centre;
				
				/* Sanity check */
				if ( (x>=0) && (x<image.width) && (y>=0) && (y<image.height) ) {
				
					/* Record the reflection */
					refl[i].x = x;
					refl[i].y = y;
					i++;
					
					if ( i > MAX_IMAGE_REFLECTIONS ) {
						fprintf(stderr, "Too many reflections\n");
						break;
					}
					
					//printf("Reflection %i at %i,%i\n", i, refl[i-1].x, refl[i-1].y);
				
				} else {
					//fprintf(stderr, "Reflection failed sanity test (x=%f, y=%f)\n", x, y);
				}
			
			} /* else this is the central beam so don't worry about it */
		
		}
		
		reflection = reflection->next;

	} while ( reflection );
	
	//printf("Found %i reflections in image\n", i);
	*n = i;
	return refl;

}