fix a bug when selecting a new crystal
[hkl.git] / tests / hkl-geometry-t.c
blob547702441cb8fb2997ebc3f545c59e2732fe6c8c
1 /* This file is part of the hkl library.
3 * The hkl library is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * The hkl library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with the hkl library. If not, see <http://www.gnu.org/licenses/>.
16 * Copyright (C) 2003-2013 Synchrotron SOLEIL
17 * L'Orme des Merisiers Saint-Aubin
18 * BP 48 91192 GIF-sur-YVETTE CEDEX
20 * Authors: Picca Frédéric-Emmanuel <picca@synchrotron-soleil.fr>
22 #include "hkl.h"
23 #include <tap/basic.h>
24 #include <tap/float.h>
26 #include "hkl/ccan/container_of/container_of.h"
27 #include "hkl-axis-private.h" /* temporary */
28 #include "hkl-geometry-private.h"
30 static void add_holder(void)
32 HklGeometry *g = NULL;
33 HklHolder *holder = NULL;
35 g = hkl_geometry_new(NULL);
36 is_int(0, darray_size(g->holders), __func__);
38 holder = hkl_geometry_add_holder(g);
39 hkl_holder_add_rotation_axis(holder, "A", 1., 0., 0.);
40 hkl_holder_add_rotation_axis(holder, "B", 1., 0., 0.);
41 is_int(1, darray_size(g->holders), __func__);
43 holder = hkl_geometry_add_holder(g);
44 hkl_holder_add_rotation_axis(holder, "A", 1., 0., 0.);
45 hkl_holder_add_rotation_axis(holder, "C", 1., 0., 0.);
46 is_int(2, darray_size(g->holders), __func__);
48 ok(holder == darray_item(g->holders, 1), __func__);
50 hkl_geometry_free(g);
53 static void get_axis(void)
55 HklGeometry *g = NULL;
56 HklHolder *holder = NULL;
57 HklAxis *axis0, *axis1, *axis2;
59 g = hkl_geometry_new(NULL);
61 holder = hkl_geometry_add_holder(g);
62 hkl_holder_add_rotation_axis(holder, "A", 1., 0., 0.);
63 hkl_holder_add_rotation_axis(holder, "B", 1., 0., 0.);
65 holder = hkl_geometry_add_holder(g);
66 hkl_holder_add_rotation_axis(holder, "A", 1., 0., 0.);
67 hkl_holder_add_rotation_axis(holder, "C", 1., 0., 0.);
69 ok(0 == !hkl_geometry_get_axis_by_name(g, "A"), __func__);
70 ok(0 == !hkl_geometry_get_axis_by_name(g, "B"), __func__);
71 ok(0 == !hkl_geometry_get_axis_by_name(g, "C"), __func__);
72 ok(1 == !hkl_geometry_get_axis_by_name(g, "D"), __func__);
74 hkl_geometry_free(g);
77 static void update(void)
79 HklGeometry *g = NULL;
80 HklHolder *holder = NULL;
81 HklAxis *axis0, *axis1, *axis2;
83 g = hkl_geometry_new(NULL);
85 holder = hkl_geometry_add_holder(g);
86 hkl_holder_add_rotation_axis(holder, "A", 1., 0., 0.);
87 hkl_holder_add_rotation_axis(holder, "B", 1., 0., 0.);
89 holder = hkl_geometry_add_holder(g);
90 hkl_holder_add_rotation_axis(holder, "A", 1., 0., 0.);
91 hkl_holder_add_rotation_axis(holder, "C", 1., 0., 0.);
93 axis1 = container_of(hkl_geometry_get_axis_by_name(g, "B"), HklAxis, parameter);
94 hkl_parameter_value_set(&axis1->parameter, M_PI_2, NULL);
95 /* now axis1 is dirty */
96 ok(HKL_TRUE == axis1->parameter.changed, __func__);
98 hkl_geometry_update(g);
99 holder = darray_item(g->holders, 0);
100 is_double(1./sqrt(2), holder->q.data[0], HKL_EPSILON, __func__);
101 is_double(1./sqrt(2), holder->q.data[1], HKL_EPSILON, __func__);
102 is_double(.0, holder->q.data[2], HKL_EPSILON, __func__);
103 is_double(.0, holder->q.data[3], HKL_EPSILON, __func__);
104 /* now axis1 is clean */
105 ok(HKL_FALSE == axis1->parameter.changed, __func__);
107 hkl_geometry_free(g);
110 static void set_values(void)
112 HklGeometry *g;
113 HklHolder *holder;
115 g = hkl_geometry_new(NULL);
116 holder = hkl_geometry_add_holder(g);
117 hkl_holder_add_rotation_axis(holder, "A", 1., 0., 0.);
118 hkl_holder_add_rotation_axis(holder, "B", 1., 0., 0.);
119 hkl_holder_add_rotation_axis(holder, "C", 1., 0., 0.);
121 hkl_geometry_set_values_v(g, 3, 1., 1., 1.);
122 is_double(1., hkl_parameter_value_get(darray_item(g->axes, 0)), HKL_EPSILON, __func__);
123 is_double(1., hkl_parameter_value_get(darray_item(g->axes, 1)), HKL_EPSILON, __func__);
124 is_double(1., hkl_parameter_value_get(darray_item(g->axes, 2)), HKL_EPSILON, __func__);
126 hkl_geometry_free(g);
129 static void set_values_unit(void)
131 HklGeometry *g;
132 HklHolder *holder;
134 g = hkl_geometry_new(NULL);
135 holder = hkl_geometry_add_holder(g);
136 hkl_holder_add_rotation_axis(holder, "A", 1., 0., 0.);
137 hkl_holder_add_rotation_axis(holder, "B", 1., 0., 0.);
138 hkl_holder_add_rotation_axis(holder, "C", 1., 0., 0.);
140 hkl_geometry_set_values_unit_v(g, 10., 10., 10.);
141 is_double(10. * HKL_DEGTORAD, hkl_parameter_value_get(darray_item(g->axes, 0)), HKL_EPSILON, __func__);
142 is_double(10. * HKL_DEGTORAD, hkl_parameter_value_get(darray_item(g->axes, 1)), HKL_EPSILON, __func__);
143 is_double(10. * HKL_DEGTORAD, hkl_parameter_value_get(darray_item(g->axes, 2)), HKL_EPSILON, __func__);
145 hkl_geometry_free(g);
148 static void distance(void)
150 HklGeometry *g1 = NULL;
151 HklGeometry *g2 = NULL;
152 HklHolder *holder = NULL;
154 g1 = hkl_geometry_new(NULL);
155 holder = hkl_geometry_add_holder(g1);
156 hkl_holder_add_rotation_axis(holder, "A", 1., 0., 0.);
157 hkl_holder_add_rotation_axis(holder, "B", 1., 0., 0.);
158 hkl_holder_add_rotation_axis(holder, "C", 1., 0., 0.);
160 g2 = hkl_geometry_new_copy(g1);
162 hkl_geometry_set_values_v(g1, 3, 0., 0., 0.);
163 hkl_geometry_set_values_v(g2, 3, 1., 1., 1.);
164 is_double(3., hkl_geometry_distance(g1, g2), HKL_EPSILON, __func__);
166 hkl_geometry_free(g1);
167 hkl_geometry_free(g2);
170 static void is_valid(void)
172 HklGeometry *geom = NULL;
173 HklHolder *holder = NULL;
175 geom = hkl_geometry_new(NULL);
176 holder = hkl_geometry_add_holder(geom);
177 hkl_holder_add_rotation_axis(holder, "A", 1., 0., 0.);
178 hkl_holder_add_rotation_axis(holder, "B", 1., 0., 0.);
179 hkl_holder_add_rotation_axis(holder, "C", 1., 0., 0.);
181 hkl_geometry_set_values_v(geom, 3, 0., 0., 0.);
182 ok(HKL_TRUE == hkl_geometry_is_valid(geom), __func__);
184 hkl_geometry_set_values_v(geom, 3, -181. * HKL_DEGTORAD, 0., 0.);
185 ok(HKL_TRUE == hkl_geometry_is_valid(geom), __func__);
187 hkl_parameter_min_max_set(darray_item(geom->axes, 0),
188 -100 * HKL_DEGTORAD, 100 * HKL_DEGTORAD);
189 ok(HKL_FALSE == hkl_geometry_is_valid(geom), __func__);
191 hkl_geometry_free(geom);
194 static void list(void)
196 int i = 0;
197 HklGeometry *g;
198 HklGeometryList *list;
199 HklGeometryListItem **item;
200 HklHolder *holder;
201 static double values[] = {0. * HKL_DEGTORAD, 10 * HKL_DEGTORAD, 30 * HKL_DEGTORAD};
203 g = hkl_geometry_new(NULL);
204 holder = hkl_geometry_add_holder(g);
205 hkl_holder_add_rotation_axis(holder, "A", 1., 0., 0.);
206 hkl_holder_add_rotation_axis(holder, "B", 1., 0., 0.);
207 hkl_holder_add_rotation_axis(holder, "C", 1., 0., 0.);
209 list = hkl_geometry_list_new();
211 hkl_geometry_set_values_v(g, 3, values[0], 0., 0.);
212 hkl_geometry_fprintf(stderr, g);
213 hkl_geometry_list_add(list, g);
214 is_int(1, darray_size(list->items), __func__);
216 /* can not add two times the same geometry */
217 hkl_geometry_list_add(list, g);
218 is_int(1, darray_size(list->items), __func__);
220 hkl_geometry_set_values_v(g, 3, values[2], 0., 0.);
221 hkl_geometry_list_add(list, g);
222 hkl_geometry_set_values_v(g, 3, values[1], 0., 0.);
223 hkl_geometry_list_add(list, g);
224 is_int(3, darray_size(list->items), __func__);
226 hkl_geometry_set_values_v(g, 3, values[0], 0., 0.);
227 hkl_geometry_list_sort(list, g);
229 hkl_geometry_list_fprintf(stderr, list);
230 darray_foreach(item, list->items){
231 is_double(values[i++],
232 hkl_parameter_value_get(darray_item((*item)->geometry->axes, 0)),
233 HKL_EPSILON, __func__);
236 hkl_geometry_free(g);
237 hkl_geometry_list_free(list);
240 static void list_multiply_from_range(void)
242 HklGeometry *g;
243 HklGeometryList *list;
244 HklHolder *holder;
245 HklParameter *axisA, *axisB, *axisC;
247 g = hkl_geometry_new(NULL);
248 holder = hkl_geometry_add_holder(g);
249 hkl_holder_add_rotation_axis(holder, "A", 1., 0., 0.);
250 hkl_holder_add_rotation_axis(holder, "B", 1., 0., 0.);
251 hkl_holder_add_rotation_axis(holder, "C", 1., 0., 0.);
253 axisA = hkl_geometry_get_axis_by_name(g, "A");
254 axisB = hkl_geometry_get_axis_by_name(g, "B");
255 axisC = hkl_geometry_get_axis_by_name(g, "C");
257 hkl_parameter_min_max_unit_set(axisA, -190, 190);
258 hkl_parameter_min_max_unit_set(axisB, -190, 190);
259 hkl_parameter_min_max_unit_set(axisC, -190, 190);
261 list = hkl_geometry_list_new();
263 hkl_geometry_set_values_v(g, 3, 185. * HKL_DEGTORAD, -185. * HKL_DEGTORAD, 190. * HKL_DEGTORAD);
264 hkl_geometry_list_add(list, g);
266 hkl_geometry_list_multiply_from_range(list);
268 hkl_geometry_free(g);
269 hkl_geometry_list_free(list);
272 static void list_remove_invalid(void)
274 HklGeometry *g;
275 HklGeometryList *list;
276 HklHolder *holder;
277 HklParameter *axisA, *axisB, *axisC;
279 g = hkl_geometry_new(NULL);
280 holder = hkl_geometry_add_holder(g);
281 hkl_holder_add_rotation_axis(holder, "A", 1., 0., 0.);
282 hkl_holder_add_rotation_axis(holder, "B", 1., 0., 0.);
283 hkl_holder_add_rotation_axis(holder, "C", 1., 0., 0.);
285 axisA = hkl_geometry_get_axis_by_name(g, "A");
286 axisB = hkl_geometry_get_axis_by_name(g, "B");
287 axisC = hkl_geometry_get_axis_by_name(g, "C");
289 hkl_parameter_min_max_unit_set(axisA, -100, 180.);
290 hkl_parameter_min_max_unit_set(axisB, -100., 180.);
291 hkl_parameter_min_max_unit_set(axisC, -100., 180.);
293 list = hkl_geometry_list_new();
295 hkl_geometry_set_values_v(g, 3,
296 185. * HKL_DEGTORAD,
297 -185.* HKL_DEGTORAD,
298 185. * HKL_DEGTORAD);
299 hkl_geometry_list_add(list, g);
301 hkl_geometry_set_values_v(g, 3,
302 -190. * HKL_DEGTORAD,
303 -190.* HKL_DEGTORAD,
304 -190.* HKL_DEGTORAD);
305 hkl_geometry_list_add(list, g);
307 hkl_geometry_set_values_v(g, 3,
308 180. * HKL_DEGTORAD,
309 180.* HKL_DEGTORAD,
310 180.* HKL_DEGTORAD);
311 hkl_geometry_list_add(list, g);
313 is_int(3, darray_size(list->items), __func__);
314 hkl_geometry_list_remove_invalid(list);
315 is_int(2, darray_size(list->items), __func__);
317 hkl_geometry_free(g);
318 hkl_geometry_list_free(list);
321 int main(int argc, char** argv)
323 plan(32);
325 add_holder();
326 get_axis();
327 update();
328 set_values();
329 set_values_unit();
330 distance();
331 is_valid();
333 list();
334 list_multiply_from_range();
335 list_remove_invalid();
337 return 0;