aboutsummaryrefslogtreecommitdiff
path: root/src/templates.c
blob: 632e5a9fa669ed493d42684c2f1dfcfd7ef61617 (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
/*
 * templates.c
 *
 * Handle templates
 *
 * (c) 2006-2009 Thomas White <thomas.white@desy.de>
 *
 * template_index - Indexing diffraction patterns by template matching
 *
 */


#define _GNU_SOURCE 1
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <stdio.h>

#include "templates.h"
#include "cell.h"
#include "image.h"
#include "utils.h"
#include "relrod.h"


struct _templatelist
{
	int		n_templates;
	struct template	*templates;
};


struct template_feature
{
	float		x;
	float		y;
	struct template_feature *next;
};


struct template
{
	float		omega;
	float		tilt;
	struct template_feature *features;
};


static int template_add(TemplateList *list, struct template *template)
{
	if ( list->templates ) {
		list->templates = realloc(list->templates,
		                 (list->n_templates+1)*sizeof(struct template));
	} else {
		assert(list->n_templates == 0);
		list->templates = malloc(sizeof(struct template));
	}

	/* Copy the data */
	list->templates[list->n_templates] = *template;

	list->n_templates++;

	return list->n_templates - 1;
}


static TemplateList *template_list_new()
{
	TemplateList *list;

	list = malloc(sizeof(TemplateList));

	list->n_templates = 0;
	list->templates = NULL;

	return list;
}


TemplateList *generate_templates(UnitCell *cell, struct image params)
{
	TemplateList *list;
	double omega, tilt;

	list = template_list_new();

	omega = deg2rad(40.0);

	//for ( omega=deg2rad(-180); omega<deg2rad(180); omega+=deg2rad(1) ) {

		params.omega = omega;

		for ( tilt=0; tilt<deg2rad(180); tilt+=deg2rad(1) ) {

			struct template t;
			struct template_feature *tfc;
			int nrefl, i;

			t.omega = omega;
			t.tilt = tilt;
			t.features = malloc(sizeof(struct template_feature));
			t.features->next = NULL;
			tfc = t.features;

			params.tilt = tilt;
			get_reflections(&params, cell, 0.01e9);

			nrefl = image_feature_count(params.rflist);
			for ( i=0; i<nrefl; i++ ) {

				struct imagefeature *f;

				f = image_get_feature(params.rflist, i);

				tfc->x = f->x;
				tfc->y = f->y;
				tfc->next = malloc(sizeof(struct template_feature));
				tfc = tfc->next;

			}

			template_add(list, &t);

			image_feature_list_free(params.rflist);

			printf("Generating templates... %+5.2f %+5.2f\r",
			       rad2deg(omega), rad2deg(tilt));

		}
	//}

	printf("Generating templates... done                      \n");

	return list;
}


static int try_template(struct image *image, struct template template)
{
	int fit = 0;
	struct template_feature *f;

	f = template.features;
	while ( f != NULL ) {

		int x, y;

		x = f->x;
		y = f->y;	/* Discards digits after the decimal point */

		fit += image->data[y*image->width+x];

		f = f->next;

	}

	return fit;
}


int try_templates(struct image *image, TemplateList *list)
{
	int i;
	int fit_max = 0;
	int i_max = 0;

	for ( i=0; i<list->n_templates; i++ ) {

		int fit;

		fit = try_template(image, list->templates[i]);
		if ( fit > fit_max ) {
			fit_max = fit;
			i_max = i;
		}

	}
	image->omega = list->templates[i_max].omega;
	image->tilt = list->templates[i_max].tilt;

	return 0;
}