aboutsummaryrefslogtreecommitdiff
path: root/src/templates.c
blob: 33dc172001696848657139a7337d0f80a776178e (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
/*
 * 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));

		}
	//}

	return list;
}


int try_templates(struct image *image, TemplateList *list)
{
	return 0;
}