aboutsummaryrefslogtreecommitdiff
path: root/src/basis.c
blob: 8a282e3fa98f57690510366bcbb03aed2a968073 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
 * basis.c
 *
 * Find approximate lattices to feed various procedures
 *
 * (c) 2007 Thomas White <taw27@cam.ac.uk>
 *
 *  dtr - Diffraction Tomography Reconstruction
 *
 */

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

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

#include "utils.h"
#include "reflections.h"
#include "basis.h"

static double basis_efom(ReflectionList *reflectionlist, Basis *basis) {

	int n_indexed, n_counted;
	Reflection *cur;
	
	cur = reflectionlist->reflections;
	n_indexed = 0;
	n_counted = 0;
	while ( cur ) {

		if ( cur->type == REFLECTION_NORMAL ) {
			
			/* Can this basis "approximately" account for this reflection? */
			double det;
			double a11, a12, a13, a21, a22, a23, a31, a32, a33;
			double h, k, l;
			
			/* Set up the coordinate transform from hkl to xyz */
			a11 = basis->a.x;  a12 = basis->a.y;  a13 = basis->a.z;
			a21 = basis->b.x;  a22 = basis->b.y;  a23 = basis->b.z;
			a31 = basis->c.x;  a32 = basis->c.y;  a33 = basis->c.z;
			
			/* Invert the matrix to get hkl from xyz */
			det = a11*(a22*a33 - a23*a32) - a12*(a21*a33 - a23*a31) + a13*(a21*a32 - a22*a31);
			h = ((a22*a33-a23*a32)*cur->x + (a23*a31-a21*a33)*cur->y + (a21*a32-a22*a31)*cur->z) / det;
			k = ((a13*a32-a12*a33)*cur->x + (a11*a33-a13*a31)*cur->y + (a12*a31-a11*a32)*cur->z) / det;
			l = ((a12*a23-a13*a22)*cur->x + (a13*a21-a11*a23)*cur->y + (a11*a22-a12*a21)*cur->z) / det;
			
			/* Calculate the deviations in terms of |a|, |b| and |c| */
			h = fabs(h);  k = fabs(k);  l = fabs(l);
			h -= floor(h);  k -= floor(k);  l -= floor(l);
			if ( h == 1.0 ) h = 0.0;
			if ( k == 1.0 ) k = 0.0;
			if ( l == 1.0 ) l = 0.0;
			
			/* Define "approximately" here.  Circle in basis space becomes an ellipsoid in reciprocal space */
			if ( h*h + k*k + l*l <= 0.1*0.1*0.1 ) n_indexed++;
			
			n_counted++;
		
		}
		
		cur = cur->next;

	}
	
	return (double)n_indexed / n_counted;

}

static int basis_lfom(ControlContext *ctx, double vx, double vy, double vz) {
	
	Reflection	*tcentre;
	int		lfom;
	double		tol;
	int		j;
	
	lfom = 0;
	tol = modulus(vx, vy, vz)/10.0;
	tcentre = ctx->reflectionlist->reflections;
	do {
	
		for ( j=-20; j<=20; j++ ) {
		
			Reflection *check;
			
			check = reflectionlist_find_nearest(ctx->reflectionlist, tcentre->x+vx*j, tcentre->y+vy*j, tcentre->z+vz*j);
			if ( check && (distance3d(check->x, check->y, check->z, tcentre->x+vx*j, tcentre->y+vy*j, tcentre->z+vz*j) < tol) ) {
				lfom++;
			}
			
		}
		
		tcentre = tcentre->next;
		
	} while ( tcentre );

	return lfom;

}

/* Select a suitable number of sensible seed vectors */
static ReflectionList *basis_find_seeds(ControlContext *ctx) {

	double		tilt_min;
	double		tilt_max;
	double		tilt_mid;
	ImageRecord	*imagerecord;
	double		x_temp, y_temp, z_temp;
	double		scale;
	double		x, y, z;
	Reflection	*centre;
	int		i;
	ReflectionList *seeds;
	
	seeds = reflectionlist_new();
	
	/* Locate the 'plane' in the middle of the "wedge".
	 *	This whole procedure assumes there is just one tilt axis. */
	tilt_min = control_min_tilt(ctx);
	tilt_max = control_max_tilt(ctx);
	tilt_mid = tilt_min + (tilt_max-tilt_min)/2;
	imagerecord = control_image_nearest_tilt(ctx, tilt_mid);
	
	/* Apply the last two steps of the mapping transform to get the direction from the origin
	 *	towards the middle of the wedge */
	x_temp = 0.0;
	y_temp = cos(deg2rad(imagerecord->tilt));
	z_temp = -sin(deg2rad(imagerecord->tilt));
	x = x_temp*cos(-deg2rad(imagerecord->omega)) + y_temp*sin(-deg2rad(imagerecord->omega));
	y = -x_temp*sin(-deg2rad(imagerecord->omega)) + y_temp*cos(-deg2rad(imagerecord->omega));
	z = z_temp;
	
	/* Find the point in the middle of the "wedge" */
	scale = reflectionlist_largest_g(ctx->reflectionlist)/6;
	x *= scale;
	y *= scale;
	z *= scale;
	reflection_add(ctx->reflectionlist, x, y, z, 1.0, REFLECTION_VECTOR_MARKER_2);
	
	/* Find an "origin" reflection */
	centre = reflectionlist_find_nearest(ctx->reflectionlist, x, y, z);
	if ( !centre ) return NULL;
	centre->found = 1;
	reflection_add(ctx->reflectionlist, centre->x, centre->y, centre->z, 1.0, REFLECTION_GENERATED);
	
	for ( i=1; i<=10; i++ ) {
	
		Reflection *vector;
		Reflection *new_seed;
		int accept, lfom;
		double vx, vy, vz;
		
		do {
			
			Reflection	*check;
			
			accept = 1;
		
			/* Find a "candidate vector" reflection */
			vector = reflectionlist_find_nearest_longer_unknown(ctx->reflectionlist, centre->x, centre->y, centre->z, 1e9);
			if ( !vector ) {
				printf("BS: Only found %i seeds\n", i);
				return seeds;
			}
			vector->found = 1;
			
			/* Get vector components (not the coordinates the vector was calculated from!) */
			vx = vector->x - centre->x;
			vy = vector->y - centre->y;
			vz = vector->z - centre->z;
			
			/* Proximity test: don't duplicate seeds */
			check = reflectionlist_find_nearest(seeds, vx, vy, vz);
			if ( check ) {
				if ( distance3d(vx, vy, vz, check->x, check->y, check->z) < 1e9 ) {
					/* Too close to another seed */
					accept = 0;
					continue;
				}
			}
			
			/* Record lFOM for later analysis */
			lfom = basis_lfom(ctx, vx, vy, vz);
			
		} while ( !accept );

		/* Add to the list of seeds */
		new_seed = reflection_add(seeds, vx, vy, vz, 1.0, REFLECTION_NORMAL);
		new_seed->lfom = lfom;
		
		/* Create a marker in the default list for visualisation */
		reflection_add(ctx->reflectionlist, vx, vy, vz, 1.0, REFLECTION_MARKER);

	}

	return seeds;

}

/* Assemble the most sensible basis from seeds */
static Basis *basis_assemble_seeds(ReflectionList *seeds) {

	Basis *basis;
	Reflection *ref;
	int i, j, b;
	ReflectionList *seeds_sorted;

	seeds_sorted = reflectionlist_sort_lfom(seeds);
	
	basis = malloc(10*sizeof(Basis));
	
	for ( b=0; b<10; b++ ) {
	
		ref = seeds_sorted->reflections;
		j = 0;	/* Number of basis components already found */
		for ( i=1; i<=seeds_sorted->n_reflections; i++ ) {
		
			double vx, vy, vz;
		
			vx = ref->x;
			vy = ref->y;
			vz = ref->z;
			
			printf("Seed %2i: lFOM=%6i", i, ref->lfom);
			
			switch ( j ) {
				case 0 : {
					basis[b].a.x = vx;
					basis[b].a.y = vy;
					basis[b].a.z = vz;
					j++;
					printf(" *");
					break;
				}
				case 1 : {
					if ( (angle_between(vx, vy, vz, basis[b].a.x, basis[b].a.y, basis[b].a.z) > M_PI/6)
					  && (angle_between(vx, vy, vz, basis[b].a.x, basis[b].a.y, basis[b].a.z) < M_PI-M_PI/6) ) {
						basis[b].b.x = vx;
						basis[b].b.y = vy;
						basis[b].b.z = vz;
						j++;
						printf(" * (%4.1f deg)", rad2deg(angle_between(vx, vy, vz, basis[b].a.x, basis[b].a.y, basis[b].a.z)));
						break;
					}
				}
				case 2 : {
					double cx, cy, cz;
					cx = basis[b].a.y*basis[b].b.z - basis[b].a.z*basis[b].b.y;
					cy =  - basis[b].a.x*basis[b].b.z + basis[b].a.z*basis[b].b.x;
					cz = basis[b].a.x*basis[b].b.y - basis[b].a.y*basis[b].b.x;
					if ( (angle_between(vx, vy, vz, basis[b].a.x, basis[b].a.y, basis[b].a.z) > M_PI/6)
					  && (angle_between(vx, vy, vz, basis[b].b.x, basis[b].b.y, basis[b].b.z) > M_PI/6)
					  && (angle_between(vx, vy, vz, cx, cy, cz) < M_PI/2-M_PI/6) ) {
						basis[b].c.x = vx;
						basis[b].c.y = vy;
						basis[b].c.z = vz;
						j++;
						printf(" * (%4.1f deg)", rad2deg(angle_between(vx, vy, vz, cx, cy, cz)));
						break;
					}
				}
			}
			
			printf("\n");
			if ( j >= 3 ) break;
			ref = ref->next;
			
		}
		if ( j < 3 ) {
			break;
		}
	}
	
	return basis;

}

Basis *basis_find(ControlContext *ctx) {

	Basis *basis;
	ReflectionList *seeds;
	
	/* Get the shortlist of seeds */
	seeds = basis_find_seeds(ctx);
	if ( seeds->n_reflections < 3 ) {
		printf("BS: Not enough seeds to form a basis\n");
		return NULL;
	}
	printf("BS: Found %i seeds\n", seeds->n_reflections);

	basis = basis_assemble_seeds(seeds);
	
	printf("BS: eFOM = %7.3f %%\n", basis_efom(ctx->reflectionlist, basis)*100);
	
	return basis;

}