aboutsummaryrefslogtreecommitdiff
path: root/src/intensities.c
blob: 1c6bb4aea32ca3410a8486434248d12bf4dd66fa (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
/*
 * intensities.c
 *
 * Extract intensities from patterns
 *
 * (c) 2007-2009 Thomas White <thomas.white@desy.de>
 *
 * pattern_sim - Simulate diffraction patterns from small crystals
 *
 */


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

#include "image.h"
#include "intensities.h"
#include "cell.h"
#include "sfac.h"


#define MAX_HITS (1024)


struct reflhit {
	signed int h;
	signed int k;
	signed int l;
	double min_distance;
	int x;
	int y;
};


static int sum_nearby_points(uint16_t *data, int width, int x, int y)
{
	int dx, dy;
	int intensity = 0;

	for ( dx=-3; dx<=3; dx++ ) {
	for ( dy=-3; dy<=3; dy++ ) {
		intensity += data[(x+dx) + width*(y+dy)];
	}
	}

	return intensity;
}


void output_intensities(struct image *image)
{
	int x, y;
	double ax, ay, az;
	double bx, by, bz;
	double cx, cy, cz;
	struct reflhit hits[MAX_HITS];
	int n_hits = 0;
	int i;

	cell_get_cartesian(image->molecule->cell, &ax, &ay, &az,
		                                  &bx, &by, &bz,
		                                  &cx, &cy, &cz);

	for ( x=0; x<image->width; x++ ) {
	for ( y=0; y<image->height; y++ ) {

		double hd, kd, ld;  /* Indices with decimal places */
		double dh, dk, dl;  /* Distances in h,k,l directions */
		signed int h, k, l;
		struct threevec q;
		double dist;
		int found = 0;
		int j;

		q = image->qvecs[x + image->width*y];

		hd = q.u * ax + q.v * ay + q.w * az;
		kd = q.u * bx + q.v * by + q.w * bz;
		ld = q.u * cx + q.v * cy + q.w * cz;

		h = (signed int)rint(hd);
		k = (signed int)rint(kd);
		l = (signed int)rint(ld);

		dh = hd - h;
		dk = kd - k;
		dl = ld - l;
		dist = sqrt(pow(dh, 2.0) + pow(dk, 2.0) + pow(dl, 2.0));
		if ( dist > 0.1 ) continue;

		for ( j=0; j<n_hits; j++ ) {
			if ( (hits[j].h == h) && (hits[j].k == k)
			                      && (hits[j].l == l) ) {

				if ( dist < hits[j].min_distance ) {
					hits[j].min_distance = dist;
					hits[j].x = x;
					hits[j].y = y;
				}

				found = 1;

			}
		}

		if ( !found ) {
			hits[n_hits].min_distance = dist;
			hits[n_hits].x = x;
			hits[n_hits].y = y;
			hits[n_hits].h = h;
			hits[n_hits].k = k;
			hits[n_hits].l = l;
			n_hits++;
			assert(n_hits < MAX_HITS);
		}

	}
	}

	STATUS("Found %i reflections\n", n_hits);

	/* Explicit printf() used here (not normally allowed) because
	 * we really want to output to stdout */
	printf("New pattern: %7.5f %7.5f %7.5f %7.5f\n",
	       image->orientation.w, image->orientation.x,
	       image->orientation.y, image->orientation.z);
	for ( i=0; i<n_hits; i++ ) {

		int intensity;

		/* Bounds check */
		if ( hits[i].x + 3 >= image->width ) continue;
		if ( hits[i].x - 3 < 0 ) continue;
		if ( hits[i].y + 3 >= image->height ) continue;
		if ( hits[i].y - 3 < 0 ) continue;

		intensity = sum_nearby_points(image->data, image->width,
		                              hits[i].x, hits[i].y);

		printf("%3i %3i %3i %6i (at %i,%i)\n",
		       hits[i].h, hits[i].k, hits[i].l, intensity,
		       hits[i].x, hits[i].y);

	}

	/* Blank line at end */
	printf("\n");
}