aboutsummaryrefslogtreecommitdiff
path: root/src/basis.c
blob: a7ff9783bf5bbaf850cb6101840984e46c99bd24 (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
/*
 * 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(ReflectionContext *rctx, Basis *basis) {

	int n_indexed, n_counted;
	Reflection *cur;
	
	cur = rctx->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->reflectionctx->reflections;
	do {
	
		for ( j=-20; j<=20; j++ ) {
		
			Reflection *check;
			
			check = reflection_find_nearest(ctx->reflectionctx, 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;

}

static ReflectionContext *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;
	ReflectionContext *seeds;
	
	seeds = reflection_init();
	
	/* 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 = reflection_largest_g(ctx->reflectionctx)/4;
	x *= scale;
	y *= scale;
	z *= scale;
	reflection_add(ctx->reflectionctx, x, y, z, 1.0, REFLECTION_VECTOR_MARKER_2);
	
	/* Find an "origin" reflection */
	centre = reflection_find_nearest(ctx->reflectionctx, x, y, z);
	if ( !centre ) return NULL;
	centre->found = 1;
	reflection_add(ctx->reflectionctx, centre->x, centre->y, centre->z, 1.0, REFLECTION_GENERATED);
	
	for ( i=1; i<=10; i++ ) {
	
		Reflection *vector;
		int accept;
		double vx, vy, vz;
		
		do {
			
			Reflection	*check;
			int		lfom;
			
			accept = 1;
		
			/* Find a "candidate vector" reflection */
			vector = reflection_find_nearest_longer(ctx->reflectionctx, centre->x, centre->y, centre->z, 1e9); /* 0.5 A^-1 */
			if ( !vector ) {
				printf("BS: Couldn't find enough seeds\n");
				return NULL;
			}
			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 */
			check = reflection_find_nearest_type(ctx->reflectionctx, vx, vy, vz, REFLECTION_NORMAL);
			if ( check ) {
				if ( distance3d(vx, vy, vz, check->x, check->y, check->z) < 1e9 ) {
					/* Too close to another seed */
					accept = 0;
					continue;
				}
			}
			
			/* lFOM test */
			lfom = basis_lfom(ctx, vx, vy, vz);
			if ( lfom < 1 ) {
				accept = 0;
				continue;
			}
			printf("lfom=%i\n", lfom);
			
		} while ( !accept );

		reflection_add(seeds, vx, vy, vz, 1.0, REFLECTION_NORMAL);
		reflection_add(ctx->reflectionctx, vx, vy, vz, 1.0, REFLECTION_MARKER);

	}

	return seeds;

}

Basis *basis_find(ControlContext *ctx) {

	Basis *basis;
	ReflectionContext *seeds;
	Reflection *ref;
	int i;
	
	/* Get the shortlist of seeds */
	seeds = basis_find_seeds(ctx);

	/* Assemble the seeds into a basis */
	basis = malloc(sizeof(Basis));
	ref = seeds->reflections;
	for ( i=1; i<=3; i++ ) {
	
		double vx, vy, vz;
	
		vx = ref->x;
		vy = ref->y;
		vz = ref->z;
	
		switch ( i ) {
			case 1 : {
				basis->a.x = vx;
				basis->a.y = vy;
				basis->a.z = vz;
			}
			case 2 : {
				basis->b.x = vx;
				basis->b.y = vy;
				basis->b.z = vz;
			}
			case 3 : {
				basis->c.x = vx;
				basis->c.y = vy;
				basis->c.z = vz;
			}
		}
		
		ref = ref->next;
	
	}
	
	printf("BS: eFOM = %7.3f %%\n", basis_efom(ctx->reflectionctx, basis)*100);
	
	return basis;

}