aboutsummaryrefslogtreecommitdiff
path: root/src/reflections.c
blob: ab626fa4281f932c3000499c88af9cbd0874ee20 (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * reflections.c
 *
 * Data structures in 3D space
 *
 * (c) 2007 Thomas White <taw27@cam.ac.uk>
 *
 *  dtr - Diffraction Tomography Reconstruction
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

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

#include "reflections.h"

static void reflection_addfirst(ReflectionContext *reflectionctx) {
	/* Create first items on lists - saves faffing later.  Corresponds to a central marker.
		Reflections are only stored if they have non-zero value. */
	reflectionctx->reflections = malloc(sizeof(Reflection));
	reflectionctx->reflections->next = NULL;
	reflectionctx->reflections->x = 0;
	reflectionctx->reflections->y = 0;
	reflectionctx->reflections->z = 0;
	reflectionctx->reflections->type = REFLECTION_CENTRAL;
	reflectionctx->last_reflection = reflectionctx->reflections;
	reflectionctx->n_reflections = 1;
	reflectionctx->list_capped = 0;
}

ReflectionContext *reflection_init() {

	ReflectionContext *reflectionctx = malloc(sizeof(ReflectionContext));
	reflection_addfirst(reflectionctx);
	reflectionctx->n_reflections = 0;
	reflectionctx->list_capped = 0;
	
	return reflectionctx;

}

void reflection_clear_markers(ReflectionContext *reflectionctx) {

	Reflection *reflection = reflectionctx->reflections;
	Reflection *prev = NULL;
	int del = 0;
	
	do {
		Reflection *next = reflection->next;
		
		if ( (reflection->type == REFLECTION_MARKER) || (reflection->type == REFLECTION_GENERATED)
		 || (reflection->type == REFLECTION_VECTOR_MARKER_1) || (reflection->type == REFLECTION_VECTOR_MARKER_2)
		 || (reflection->type == REFLECTION_VECTOR_MARKER_3) ) {
			free(reflection);
			del++;
			if ( prev ) {
				prev->next = next;
			} else {
				reflectionctx->reflections = next;
			}
		} else {
			prev = reflection;
		}
		
		reflection = next;
	
	} while ( reflection );
	
	reflectionctx->n_reflections -= del;
	reflectionctx->last_reflection = prev;
	
}
void reflection_clear(ReflectionContext *reflectionctx) {

	Reflection *reflection = reflectionctx->reflections;
	do {
		Reflection *next = reflection->next;
		free(reflection);
		reflection = next;
	} while ( reflection );
	
	reflectionctx->n_reflections = 0;
	reflectionctx->list_capped = 0;
	reflection_addfirst(reflectionctx);

}

void reflection_free(ReflectionContext *reflectionctx) {
	reflection_clear(reflectionctx);
	free(reflectionctx->reflections);
	free(reflectionctx);
}

Reflection *reflection_add(ReflectionContext *reflectionctx, double x, double y, double z, double intensity, ReflectionType type) {

	Reflection *new_reflection;
	
	if ( reflectionctx->list_capped ) return NULL;
	
	if ( reflectionctx->n_reflections > 1e7 ) {
		fprintf(stderr, "More than 10 million reflections on list.  I think this is silly.\n");
		fprintf(stderr, "No further reflections will be stored.  Go and fix the peak detection.\n");
		reflectionctx->list_capped = 1;
	}
	reflectionctx->n_reflections++;
	
	new_reflection = malloc(sizeof(Reflection));
	new_reflection->next = NULL;
	new_reflection->x = x;
	new_reflection->y = y;
	new_reflection->z = z;
	new_reflection->intensity = intensity;
	new_reflection->type = type;
	
	reflectionctx->last_reflection->next = new_reflection;
	reflectionctx->last_reflection = new_reflection;
	
	return new_reflection;
	
}

/* x and y in pixels, measured from centre of image */
void reflection_add_from_dp(ControlContext *ctx, double x, double y, ImageRecord imagerecord, double intensity) {

	/* "Input" space */
	double d;
	
	/* Angular description of reflection */
	double theta, psi, k;
	
	/* Reciprocal space */
	double tilt;
	double omega;
	
	double x_temp, y_temp, z_temp;
	double nx, ny, nz;
	
	tilt = 2*M_PI*(imagerecord.tilt/360);	/* Convert to Radians */
	omega = 2*M_PI*(imagerecord.omega/360);	/* Likewise */
	k = 1/imagerecord.lambda;
	
	/* Calculate an angular description of the reflection */
	if ( ctx->fmode == FORMULATION_CLEN ) {
		x /= imagerecord.resolution;
		y /= imagerecord.resolution;	/* Convert pixels to metres */
		d = sqrt((x*x) + (y*y));
		theta = atan2(d, imagerecord.camera_len);
	} else if ( ctx->fmode == FORMULATION_PIXELSIZE ) {
		x *= imagerecord.pixel_size;
		y *= imagerecord.pixel_size;	/* Convert pixels to metres^-1 */
		d = sqrt((x*x) + (y*y));
		theta = atan2(d, k);
	} else {
		fprintf(stderr, "Unrecognised formulation mode in reflection_add_from_dp\n");
		return;
	}
	psi = atan2(y, x);
	
	x_temp = k*sin(theta)*cos(psi);
	y_temp = k*sin(theta)*sin(psi);
	z_temp = k - k*cos(theta);
	
	/* Apply the rotations...
		First: rotate image clockwise until tilt axis is aligned horizontally. */
	nx = x_temp*cos(omega) + y_temp*sin(omega);
	ny = -x_temp*sin(omega) + y_temp*cos(omega);
	nz = z_temp;
	
	/* 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;
	
	reflection_add(ctx->reflectionctx, nx, ny, nz, intensity, REFLECTION_NORMAL);

}

void reflection_add_from_reflection(ReflectionContext *rctx, Reflection *r) {
	r->next = NULL;
	rctx->last_reflection->next = r;
	rctx->last_reflection = r;
}