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-2010 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>
23 #include <tap/basic.h>
25 static void add_holder(void)
27 HklGeometry
*g
= NULL
;
28 HklHolder
*holder
= NULL
;
30 g
= hkl_geometry_new();
31 is_int(0, g
->holders_len
, __func__
);
33 holder
= hkl_geometry_add_holder(g
);
34 hkl_holder_add_rotation_axis(holder
, "A", 1., 0., 0.);
35 hkl_holder_add_rotation_axis(holder
, "B", 1., 0., 0.);
36 is_int(1, g
->holders_len
, __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
, "C", 1., 0., 0.);
41 is_int(2, g
->holders_len
, __func__
);
43 ok(holder
== &g
->holders
[1], __func__
);
48 static void get_axis(void)
50 HklGeometry
*g
= NULL
;
51 HklHolder
*holder
= NULL
;
52 HklAxis
*axis0
, *axis1
, *axis2
;
54 g
= hkl_geometry_new();
56 holder
= hkl_geometry_add_holder(g
);
57 hkl_holder_add_rotation_axis(holder
, "A", 1., 0., 0.);
58 hkl_holder_add_rotation_axis(holder
, "B", 1., 0., 0.);
60 holder
= hkl_geometry_add_holder(g
);
61 hkl_holder_add_rotation_axis(holder
, "A", 1., 0., 0.);
62 hkl_holder_add_rotation_axis(holder
, "C", 1., 0., 0.);
64 ok(0 == !hkl_geometry_get_axis_by_name(g
, "A"), __func__
);
65 ok(0 == !hkl_geometry_get_axis_by_name(g
, "B"), __func__
);
66 ok(0 == !hkl_geometry_get_axis_by_name(g
, "C"), __func__
);
67 ok(1 == !hkl_geometry_get_axis_by_name(g
, "D"), __func__
);
72 static void update(void)
74 HklGeometry
*g
= NULL
;
75 HklHolder
*holder
= NULL
;
76 HklAxis
*axis0
, *axis1
, *axis2
;
78 g
= hkl_geometry_new();
80 holder
= hkl_geometry_add_holder(g
);
81 hkl_holder_add_rotation_axis(holder
, "A", 1., 0., 0.);
82 hkl_holder_add_rotation_axis(holder
, "B", 1., 0., 0.);
84 holder
= hkl_geometry_add_holder(g
);
85 hkl_holder_add_rotation_axis(holder
, "A", 1., 0., 0.);
86 hkl_holder_add_rotation_axis(holder
, "C", 1., 0., 0.);
88 axis1
= hkl_geometry_get_axis_by_name(g
, "B");
89 hkl_axis_set_value(axis1
, M_PI_2
);
90 /* now axis1 is dirty */
91 ok(HKL_TRUE
== axis1
->parameter
.changed
, __func__
);
93 hkl_geometry_update(g
);
94 is_double(1./sqrt(2), g
->holders
[0].q
.data
[0], HKL_EPSILON
, __func__
);
95 is_double(1./sqrt(2), g
->holders
[0].q
.data
[1], HKL_EPSILON
, __func__
);
96 is_double(.0, g
->holders
[0].q
.data
[2], HKL_EPSILON
, __func__
);
97 is_double(.0, g
->holders
[0].q
.data
[3], HKL_EPSILON
, __func__
);
98 /* now axis1 is clean */
99 ok(HKL_FALSE
== axis1
->parameter
.changed
, __func__
);
101 hkl_geometry_free(g
);
104 static void set_values(void)
109 g
= hkl_geometry_new();
110 holder
= hkl_geometry_add_holder(g
);
111 hkl_holder_add_rotation_axis(holder
, "A", 1., 0., 0.);
112 hkl_holder_add_rotation_axis(holder
, "B", 1., 0., 0.);
113 hkl_holder_add_rotation_axis(holder
, "C", 1., 0., 0.);
115 hkl_geometry_set_values_v(g
, 3, 1., 1., 1.);
116 is_double(1., hkl_parameter_get_value(&g
->axes
[0].parameter
), HKL_EPSILON
, __func__
);
117 is_double(1., hkl_parameter_get_value(&g
->axes
[1].parameter
), HKL_EPSILON
, __func__
);
118 is_double(1., hkl_parameter_get_value(&g
->axes
[2].parameter
), HKL_EPSILON
, __func__
);
120 hkl_geometry_free(g
);
123 static void set_values_unit(void)
128 g
= hkl_geometry_new();
129 holder
= hkl_geometry_add_holder(g
);
130 hkl_holder_add_rotation_axis(holder
, "A", 1., 0., 0.);
131 hkl_holder_add_rotation_axis(holder
, "B", 1., 0., 0.);
132 hkl_holder_add_rotation_axis(holder
, "C", 1., 0., 0.);
134 hkl_geometry_set_values_unit_v(g
, 10., 10., 10.);
135 is_double(10. * HKL_DEGTORAD
, hkl_parameter_get_value(&g
->axes
[0].parameter
), HKL_EPSILON
, __func__
);
136 is_double(10. * HKL_DEGTORAD
, hkl_parameter_get_value(&g
->axes
[1].parameter
), HKL_EPSILON
, __func__
);
137 is_double(10. * HKL_DEGTORAD
, hkl_parameter_get_value(&g
->axes
[2].parameter
), HKL_EPSILON
, __func__
);
139 hkl_geometry_free(g
);
142 static void distance(void)
144 HklGeometry
*g1
= NULL
;
145 HklGeometry
*g2
= NULL
;
146 HklHolder
*holder
= NULL
;
148 g1
= hkl_geometry_new();
149 holder
= hkl_geometry_add_holder(g1
);
150 hkl_holder_add_rotation_axis(holder
, "A", 1., 0., 0.);
151 hkl_holder_add_rotation_axis(holder
, "B", 1., 0., 0.);
152 hkl_holder_add_rotation_axis(holder
, "C", 1., 0., 0.);
154 g2
= hkl_geometry_new_copy(g1
);
156 hkl_geometry_set_values_v(g1
, 3, 0., 0., 0.);
157 hkl_geometry_set_values_v(g2
, 3, 1., 1., 1.);
158 is_double(3., hkl_geometry_distance(g1
, g2
), HKL_EPSILON
, __func__
);
160 hkl_geometry_free(g1
);
161 hkl_geometry_free(g2
);
164 static void is_valid(void)
166 HklGeometry
*geom
= NULL
;
167 HklHolder
*holder
= NULL
;
169 geom
= hkl_geometry_new();
170 holder
= hkl_geometry_add_holder(geom
);
171 hkl_holder_add_rotation_axis(holder
, "A", 1., 0., 0.);
172 hkl_holder_add_rotation_axis(holder
, "B", 1., 0., 0.);
173 hkl_holder_add_rotation_axis(holder
, "C", 1., 0., 0.);
175 hkl_geometry_set_values_v(geom
, 3, 0., 0., 0.);
176 ok(HKL_TRUE
== hkl_geometry_is_valid(geom
), __func__
);
178 hkl_geometry_set_values_v(geom
, 3, -180., 0., 0.);
179 ok(HKL_FALSE
== hkl_geometry_is_valid(geom
), __func__
);
181 hkl_geometry_free(geom
);
184 static void list(void)
188 HklGeometryList
*list
;
189 HklGeometryListItem
*item
;
191 static double values
[] = {0. * HKL_DEGTORAD
, 10 * HKL_DEGTORAD
, 30 * HKL_DEGTORAD
};
193 g
= hkl_geometry_new();
194 holder
= hkl_geometry_add_holder(g
);
195 hkl_holder_add_rotation_axis(holder
, "A", 1., 0., 0.);
196 hkl_holder_add_rotation_axis(holder
, "B", 1., 0., 0.);
197 hkl_holder_add_rotation_axis(holder
, "C", 1., 0., 0.);
199 list
= hkl_geometry_list_new();
201 hkl_geometry_set_values_v(g
, 3, values
[0], 0., 0.);
202 hkl_geometry_list_add(list
, g
);
203 is_int(1, list
->len
, __func__
);
205 /* can not add two times the same geometry */
206 hkl_geometry_list_add(list
, g
);
207 is_int(1, list
->len
, __func__
);
209 hkl_geometry_set_values_v(g
, 3, values
[2], 0., 0.);
210 hkl_geometry_list_add(list
, g
);
211 hkl_geometry_set_values_v(g
, 3, values
[1], 0., 0.);
212 hkl_geometry_list_add(list
, g
);
213 is_int(3, list
->len
, __func__
);
215 hkl_geometry_set_values_v(g
, 3, values
[0], 0., 0.);
216 hkl_geometry_list_sort(list
, g
);
218 list_for_each(&list
->items
, item
, node
){
219 is_double(values
[i
++],
220 hkl_parameter_get_value(&item
->geometry
->axes
[0].parameter
),
221 HKL_EPSILON
, __func__
);
224 hkl_geometry_free(g
);
225 hkl_geometry_list_free(list
);
228 static void list_multiply_from_range(void)
231 HklGeometryList
*list
;
233 HklAxis
*axisA
, *axisB
, *axisC
;
235 g
= hkl_geometry_new();
236 holder
= hkl_geometry_add_holder(g
);
237 hkl_holder_add_rotation_axis(holder
, "A", 1., 0., 0.);
238 hkl_holder_add_rotation_axis(holder
, "B", 1., 0., 0.);
239 hkl_holder_add_rotation_axis(holder
, "C", 1., 0., 0.);
241 axisA
= hkl_geometry_get_axis_by_name(g
, "A");
242 axisB
= hkl_geometry_get_axis_by_name(g
, "B");
243 axisC
= hkl_geometry_get_axis_by_name(g
, "C");
245 hkl_axis_set_range_unit(axisA
, -190, 190);
246 hkl_axis_set_range_unit(axisB
, -190, 190);
247 hkl_axis_set_range_unit(axisC
, -190, 190);
249 list
= hkl_geometry_list_new();
251 hkl_geometry_set_values_v(g
, 3, 185. * HKL_DEGTORAD
, -185. * HKL_DEGTORAD
, 190. * HKL_DEGTORAD
);
252 hkl_geometry_list_add(list
, g
);
254 hkl_geometry_list_multiply_from_range(list
);
256 hkl_geometry_free(g
);
257 hkl_geometry_list_free(list
);
260 static void list_remove_invalid(void)
263 HklGeometryList
*list
;
265 HklAxis
*axisA
, *axisB
, *axisC
;
267 g
= hkl_geometry_new();
268 holder
= hkl_geometry_add_holder(g
);
269 hkl_holder_add_rotation_axis(holder
, "A", 1., 0., 0.);
270 hkl_holder_add_rotation_axis(holder
, "B", 1., 0., 0.);
271 hkl_holder_add_rotation_axis(holder
, "C", 1., 0., 0.);
273 axisA
= hkl_geometry_get_axis_by_name(g
, "A");
274 axisB
= hkl_geometry_get_axis_by_name(g
, "B");
275 axisC
= hkl_geometry_get_axis_by_name(g
, "C");
277 hkl_axis_set_range_unit(axisA
, -190., 180.);
278 hkl_axis_set_range_unit(axisB
, -190., 180.);
279 hkl_axis_set_range_unit(axisC
, -190., 180.);
281 list
= hkl_geometry_list_new();
283 hkl_geometry_set_values_v(g
, 3,
286 185. * HKL_DEGTORAD
);
287 hkl_geometry_list_add(list
, g
);
289 hkl_geometry_set_values_v(g
, 3,
290 -190. * HKL_DEGTORAD
,
292 -190.* HKL_DEGTORAD
);
293 hkl_geometry_list_add(list
, g
);
295 hkl_geometry_set_values_v(g
, 3,
299 hkl_geometry_list_add(list
, g
);
301 is_int(3, list
->len
, __func__
);
302 hkl_geometry_list_remove_invalid(list
);
303 is_int(2, list
->len
, __func__
);
305 hkl_geometry_free(g
);
306 hkl_geometry_list_free(list
);
309 int main(int argc
, char** argv
)
322 list_multiply_from_range();
323 list_remove_invalid();