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
|
/*
* templates.c
*
* Indexing by template matching
*
* (c) 2006-2010 Thomas White <taw@physics.org>
*
* Part of CrystFEL - crystallography with a FEL
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "index.h"
#include "index-priv.h"
#include "symmetry.h"
#include "utils.h"
#include "geometry.h"
#include "hdf5-file.h"
#include "peaks.h"
#include <assert.h>
#define INTEGRATION_SQUARE_SIDE (10)
/* Private data for template indexing */
struct _indexingprivate_template
{
struct _indexingprivate base;
int n_templates;
struct template *templates;
};
struct template {
double omega;
double phi;
int n;
struct reflhit *spots;
};
UnitCell *rotate_cell(UnitCell *in, double omega, double phi)
{
UnitCell *out;
double asx, asy, asz;
double bsx, bsy, bsz;
double csx, csy, csz;
double xnew, ynew, znew;
cell_get_reciprocal(in, &asx, &asy, &asz, &bsx, &bsy,
&bsz, &csx, &csy, &csz);
/* Rotate by "omega" about +z (parallel to c* and c unless triclinic) */
xnew = asx*cos(omega) + asy*sin(omega);
ynew = -asx*sin(omega) + asy*cos(omega);
znew = asz;
asx = xnew; asy = ynew; asz = znew;
xnew = bsx*cos(omega) + bsy*sin(omega);
ynew = -bsx*sin(omega) + bsy*cos(omega);
znew = bsz;
bsx = xnew; bsy = ynew; bsz = znew;
xnew = csx*cos(omega) + csy*sin(omega);
ynew = -csx*sin(omega) + csy*cos(omega);
znew = csz;
csx = xnew; csy = ynew; csz = znew;
/* Rotate by "phi" about +x (not parallel to anything specific) */
xnew = asx;
ynew = asy*cos(phi) + asz*sin(phi);
znew = -asy*sin(phi) + asz*cos(phi);
asx = xnew; asy = ynew; asz = znew;
xnew = bsx;
ynew = bsy*cos(phi) + bsz*sin(phi);
znew = -bsy*sin(phi) + bsz*cos(phi);
bsx = xnew; bsy = ynew; bsz = znew;
xnew = csx;
ynew = csy*cos(phi) + csz*sin(phi);
znew = -csy*sin(phi) + csz*cos(phi);
csx = xnew; csy = ynew; csz = znew;
out = cell_new_from_cell(in);
cell_set_reciprocal(out, asx, asy, asz, bsx, bsy, bsz, csx, csy, csz);
return out;
}
/* Generate templates for the given cell using a representative image */
IndexingPrivate *generate_templates(UnitCell *cell, const char *filename,
struct detector *det)
{
struct _indexingprivate_template *priv;
const char *holo;
double omega_max, phi_max;
int n_templates;
const double omega_step = deg2rad(0.5);
const double phi_step = deg2rad(0.5);
double omega, phi;
struct image image;
struct hdfile *hdfile;
int i;
hdfile = hdfile_open(filename);
if ( hdfile == NULL ) {
return NULL;
} else if ( hdfile_set_image(hdfile, "/data/data0") ) {
ERROR("Couldn't select path\n");
return NULL;
}
hdf5_read(hdfile, &image, 0);
hdfile_close(hdfile);
image.det = det;
priv = calloc(1, sizeof(struct _indexingprivate_template));
priv->base.indm = INDEXING_TEMPLATE;
/* We can only distinguish orientations within the holohedral cell */
holo = get_holohedral(cell_get_pointgroup(cell));
/* These define the orientation in space */
if ( is_polyhedral(holo) ) {
ERROR("WARNING: Holohedral point group is polyhedral.\n");
ERROR("This means I can't properly determine the orientation");
ERROR(" ranges for template matching. Expect trouble.\n");
}
omega_max = 2.0*M_PI / rotational_order(holo);
if ( has_bisecting_mirror_or_diad(holo) ) omega_max /= 2.0;
phi_max = M_PI;
if ( has_perpendicular_mirror(holo) ) phi_max /= 2.0;
/* One more axis would define the rotation in the plane of the image */
STATUS("Orientation ranges in %s: %.0f-%.0f, %.0f-%.0f deg.\n",
holo, 0.0, rad2deg(omega_max), 0.0, rad2deg(phi_max));
n_templates = (omega_max * phi_max)/(omega_step * phi_step);
STATUS("%i templates to be calculated.\n", n_templates);
priv->templates = malloc(n_templates * sizeof(struct template));
i = 0;
for ( omega = 0.0; omega < omega_max-omega_step; omega += omega_step ) {
for ( phi = 0.0; phi < phi_max-phi_step; phi += phi_step ) {
int n;
struct reflhit *hits;
UnitCell *cell_rot;
assert(i < n_templates);
cell_rot = rotate_cell(cell, omega, phi);
hits = find_intersections(&image, cell_rot, 5.0e-3,
3.0/100.0, &n, 0);
if ( hits == NULL ) {
ERROR("Template calculation failed.\n");
return NULL;
}
priv->templates[i].omega = omega;
priv->templates[i].phi = phi;
priv->templates[i].n = n;
priv->templates[i].spots = hits;
i++;
free(cell_rot);
}
progress_bar(omega*1000.0, (omega_max-2.0*omega_step)*1000.0,
"Generating templates");
}
priv->n_templates = n_templates;
return (struct _indexingprivate *)priv;
}
static int fast_integrate_peak(struct image *image, int xp, int yp)
{
int x, y;
double total = 0;
int r = INTEGRATION_SQUARE_SIDE;
for ( x=xp-r; x<=xp+r; x++ ) {
for ( y=yp-r; y<=yp+r; y++ ) {
if ( (x>=image->width) || (x<0) ) continue;
if ( (y>=image->height) || (y<0) ) continue;
total += image->data[x+image->width*y];
}
}
return total;
}
static double integrate_all_rot(struct image *image, struct reflhit *hits,
int n, double rot)
{
double itot = 0.0;
int i;
for ( i=0; i<n; i++ ) {
float xp, yp;
xp = cos(rot)*hits[i].x + sin(rot)*hits[i].y;
yp = -sin(rot)*hits[i].x + cos(rot)*hits[i].y;
itot += fast_integrate_peak(image, xp, yp);
}
return itot;
}
void match_templates(struct image *image, IndexingPrivate *ipriv)
{
struct _indexingprivate_template *priv
= (struct _indexingprivate_template *)ipriv;
int i, max_i;
double max, tot;
double rot, rot_max, rot_step;
max = 0.0;
max_i = 0;
rot_max = 2.0*M_PI;
rot_step = 2.0*M_PI / 360.0; /* 1 deg steps */
for ( i=0; i<priv->n_templates; i++ ) {
for ( rot=0.0; rot<rot_max; rot+=rot_step ) {
double val;
val = integrate_all_rot(image, priv->templates[i].spots,
priv->templates[i].n, rot);
if ( val > max ) {
max = val;
max_i = i;
}
}
progress_bar(i, priv->n_templates-1, "Indexing");
}
tot = 0.0;
for ( i=0; i<(image->width*image->height); i++ ) {
tot += image->data[i];
}
STATUS("%i (%.2f, %.2f): %.2f%%\n", max_i, priv->templates[max_i].omega,
priv->templates[max_i].phi,
100.0 * max / tot);
}
|