render_hkl.c 5.69 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
 * render_hkl.c
 *
 * Draw pretty renderings of reflection lists
 *
 * (c) 2006-2010 Thomas White <taw@physics.org>
 *
 * Part of CrystFEL - crystallography with a FEL
 *
 */


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

#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
Thomas White's avatar
Thomas White committed
23
24
#include <cairo.h>
#include <cairo-pdf.h>
25
26
27

#include "utils.h"
#include "reflections.h"
28
#include "povray.h"
29
#include "symmetry.h"
30
31


32
33
34
35
static void show_help(const char *s)
{
	printf("Syntax: %s [options] <file.hkl>\n\n", s);
	printf(
36
"Render intensity lists in various ways.\n"
37
"\n"
38
39
40
41
42
43
"  -h, --help       Display this help message.\n"
"      --povray     Render a 3D animation using POV-ray.\n"
"      --zone-axis  Render a 2D zone axis pattern.\n"
"  -j <n>           Run <n> instances of POV-ray in parallel.\n"
"  -p, --pdb=<file> PDB file from which to get the unit cell.\n"
);
44
45
46
}


Thomas White's avatar
Thomas White committed
47
static void render_za(UnitCell *cell, double *ref, unsigned int *c)
48
{
Thomas White's avatar
Thomas White committed
49
50
51
52
53
	cairo_surface_t *surface;
	cairo_t *dctx;
	double max_u, max_v, max_res, max_intensity, scale;
	double sep_u, sep_v, max_r;
	double as, bs, theta;
54
55
56
	double asx, asy, asz;
	double bsx, bsy, bsz;
	double csx, csy, csz;
Thomas White's avatar
Thomas White committed
57
58
	signed int h, k;
	float wh, ht;
59
	const char *sym = "6/mmm";
60

Thomas White's avatar
Thomas White committed
61
62
	wh = 1024;
	ht = 1024;
63

Thomas White's avatar
Thomas White committed
64
	surface = cairo_pdf_surface_create("za.pdf", wh, ht);
65

Thomas White's avatar
Thomas White committed
66
67
68
69
70
	if ( cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS ) {
		fprintf(stderr, "Couldn't create Cairo surface\n");
		cairo_surface_destroy(surface);
		return;
	}
71

Thomas White's avatar
Thomas White committed
72
	dctx = cairo_create(surface);
73

Thomas White's avatar
Thomas White committed
74
75
76
77
	/* Black background */
	cairo_rectangle(dctx, 0.0, 0.0, wh, ht);
	cairo_set_source_rgb(dctx, 0.0, 0.0, 0.0);
	cairo_fill(dctx);
78

Thomas White's avatar
Thomas White committed
79
80
81
82
	max_u = 0.0;  max_v = 0.0;  max_intensity = 0.0;
	max_res = 0.0;

	/* Work out reciprocal lattice spacings and angles for this cut */
83
	if ( cell_get_reciprocal(cell, &asx, &asy, &asz,
Thomas White's avatar
Thomas White committed
84
	                          &bsx, &bsy, &bsz,
85
86
87
88
	                          &csx, &csy, &csz) ) {
		ERROR("Couldn't get reciprocal parameters\n");
		return;
	}
Thomas White's avatar
Thomas White committed
89
90
91
92
93
94
95
96
97
98
	theta = angle_between(asx, asy, asz, bsx, bsy, bsz);
	as = modulus(asx, asy, asz) / 1e9;
	bs = modulus(bsx, bsy, bsz) / 1e9;
	STATUS("theta=%f\n", rad2deg(theta));

	for ( h=-INDMAX; h<INDMAX; h++ ) {
	for ( k=-INDMAX; k<INDMAX; k++ ) {

		double u, v, intensity, res;
		int ct;
99
		int nequiv, p;
Thomas White's avatar
Thomas White committed
100
101
102
103
104
105
106

		ct = lookup_count(c, h, k, 0);
		if ( ct < 1 ) continue;

		intensity = lookup_intensity(ref, h, k, 0) / (float)ct;

		if ( intensity != 0 ) {
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126

			nequiv = num_equivs(h, k, 0, sym);
			for ( p=0; p<nequiv; p++ ) {

				signed int he, ke, le;
				get_equiv(h, k, 0, &he, &ke, &le, sym, p);

				u =  (double)he*as*sin(theta);
				v =  (double)he*as*cos(theta) + ke*bs;
				if ( fabs(u) > fabs(max_u) ) max_u = fabs(u);
				if ( fabs(v) > fabs(max_v) ) max_v = fabs(v);
				if ( fabs(intensity) > fabs(max_intensity) ) {
					max_intensity = fabs(intensity);
				}
				res = resolution(cell, he, ke, 0);
				if ( res > max_res ) max_res = res;



			}
127
128
129
		}

	}
Thomas White's avatar
Thomas White committed
130
	}
131

Thomas White's avatar
Thomas White committed
132
133
134
	max_res /= 1e9;
	max_u /= 0.5;
	max_v /= 0.5;
135
136
	printf("Maximum resolution is 1/d = %5.3f nm^-1, d = %5.3f nm\n",
	       max_res*2.0, 1.0/(max_res*2.0));
Thomas White's avatar
Thomas White committed
137
138
139
140

	if ( max_intensity <= 0.0 ) {
		max_r = 4.0;
		goto out;
141
142
	}

Thomas White's avatar
Thomas White committed
143
144
145
146
147
	/* Choose whichever scaling factor gives the smallest value */
	scale = ((double)wh-50.0) / (2*max_u);
	if ( ((double)ht-50.0) / (2*max_v) < scale ) {
		scale = ((double)ht-50.0) / (2*max_v);
	}
148

Thomas White's avatar
Thomas White committed
149
150
151
152
153
154
155
156
157
	sep_u = as * scale * cos(theta);
	sep_v = bs * scale;
	max_r = ((sep_u < sep_v)?sep_u:sep_v);

	for ( h=-INDMAX; h<INDMAX; h++ ) {
	for ( k=-INDMAX; k<INDMAX; k++ ) {

		double u, v, intensity, val;
		int ct;
158
		int nequiv, p;
Thomas White's avatar
Thomas White committed
159
160
161
162
163

		ct = lookup_count(c, h, k, 0);
		if ( ct < 1 ) continue;

		intensity = lookup_intensity(ref, h, k, 0) / (float)ct;
Thomas White's avatar
Thomas White committed
164
		val = 3.0*intensity/max_intensity;
Thomas White's avatar
Thomas White committed
165

166
167
168
169
170
171
172
173
		nequiv = num_equivs(h, k, 0, sym);
		for ( p=0; p<nequiv; p++ ) {

			signed int he, ke, le;
			get_equiv(h, k, 0, &he, &ke, &le, sym, p);

			u = (double)he*as*sin(theta);
			v = (double)he*as*cos(theta) + ke*bs;
Thomas White's avatar
Thomas White committed
174

175
176
177
			cairo_arc(dctx, ((double)wh/2)+u*scale*2,
					((double)ht/2)+v*scale*2, max_r,
					0, 2*M_PI);
Thomas White's avatar
Thomas White committed
178

179
180
181
182
			cairo_set_source_rgb(dctx, val, val, val);
			cairo_fill(dctx);

		}
Thomas White's avatar
Thomas White committed
183
184

	}
185
186
	}

Thomas White's avatar
Thomas White committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
out:
	/* Centre marker */
	cairo_arc(dctx, (double)wh/2,
			(double)ht/2, max_r, 0, 2*M_PI);
	cairo_set_source_rgb(dctx, 1.0, 0.0, 0.0);
	cairo_fill(dctx);

	cairo_surface_finish(surface);
	cairo_destroy(dctx);
}


int main(int argc, char *argv[])
{
	int c;
	UnitCell *cell;
	char *infile;
	double *ref;
	unsigned int *cts;
	int config_povray = 0;
	int config_zoneaxis = 0;
	unsigned int nproc = 1;
209
	char *pdb = NULL;
210
	int r = 0;
Thomas White's avatar
Thomas White committed
211
212
213
214
215
216

	/* Long options */
	const struct option longopts[] = {
		{"help",               0, NULL,               'h'},
		{"povray",             0, &config_povray,      1},
		{"zone-axis",          0, &config_zoneaxis,    1},
217
		{"pdb",                1, NULL,               'p'},
Thomas White's avatar
Thomas White committed
218
219
220
221
		{0, 0, NULL, 0}
	};

	/* Short options */
222
	while ((c = getopt_long(argc, argv, "hj:p:", longopts, NULL)) != -1) {
Thomas White's avatar
Thomas White committed
223
224

		switch (c) {
Thomas White's avatar
Thomas White committed
225
		case 'h' :
Thomas White's avatar
Thomas White committed
226
227
228
			show_help(argv[0]);
			return 0;

Thomas White's avatar
Thomas White committed
229
		case 'j' :
Thomas White's avatar
Thomas White committed
230
231
232
			nproc = atoi(optarg);
			break;

233
234
235
236
		case 'p' :
			pdb = strdup(optarg);
			break;

Thomas White's avatar
Thomas White committed
237
		case 0 :
Thomas White's avatar
Thomas White committed
238
239
			break;

Thomas White's avatar
Thomas White committed
240
		default :
Thomas White's avatar
Thomas White committed
241
242
243
244
245
			return 1;
		}

	}

246
247
248
249
	if ( pdb == NULL ) {
		pdb = strdup("molecule.pdb");
	}

Thomas White's avatar
Thomas White committed
250
251
	infile = argv[optind];

252
	cell = load_cell_from_pdb(pdb);
253
254
255
256
	if ( cell == NULL ) {
		ERROR("Couldn't load unit cell from %s\n", pdb);
		return 1;
	}
Thomas White's avatar
Thomas White committed
257
258
259
260
261
262
263
264
	cts = new_list_count();
	ref = read_reflections(infile, cts, NULL);
	if ( ref == NULL ) {
		ERROR("Couldn't open file '%s'\n", infile);
		return 1;
	}

	if ( config_povray ) {
265
		r = povray_render_animation(cell, ref, cts, nproc);
Thomas White's avatar
Thomas White committed
266
267
268
269
270
271
	} else if ( config_zoneaxis ) {
		render_za(cell, ref, cts);
	} else {
		ERROR("Try again with either --povray or --zone-axis.\n");
	}

272
	free(pdb);
273

274
	return r;
275
}