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-2019 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>
24 #include <tap/float.h>
25 #include <tap/hkl-tap.h>
27 #include <hkl-axis-private.h>
32 static HklVector v
= {{1, 0, 0}};
35 axis
= hkl_parameter_new_rotation("rotation", &v
, &hkl_unit_angle_deg
);
36 is_string("rotation", hkl_parameter_name_get(axis
), __func__
);
37 hkl_parameter_min_max_get(axis
, &min
, &max
, HKL_UNIT_DEFAULT
);
38 is_double(-M_PI
, min
, HKL_EPSILON
, __func__
);
39 is_double(M_PI
, max
, HKL_EPSILON
, __func__
);
40 is_double(0., hkl_parameter_value_get(axis
, HKL_UNIT_DEFAULT
), HKL_EPSILON
, __func__
);
41 ok(TRUE
== hkl_parameter_fit_get(axis
), __func__
);
42 hkl_parameter_free(axis
);
44 axis
= hkl_parameter_new_translation("translation", &v
, &hkl_unit_length_mm
);
45 is_string("translation", hkl_parameter_name_get(axis
), __func__
);
46 hkl_parameter_min_max_get(axis
, &min
, &max
, HKL_UNIT_DEFAULT
);
47 is_double(-DBL_MAX
, min
, HKL_EPSILON
, __func__
);
48 is_double(DBL_MAX
, max
, HKL_EPSILON
, __func__
);
49 is_double(0., hkl_parameter_value_get(axis
, HKL_UNIT_DEFAULT
), HKL_EPSILON
, __func__
);
50 ok(TRUE
== hkl_parameter_fit_get(axis
), __func__
);
51 hkl_parameter_free(axis
);
54 static void get_quaternions(void)
56 static HklVector v_ref
= {{1, 0, 0}};
57 static HklQuaternion q1_ref
= {{1, 0, 0, 0}};
58 static HklQuaternion q2_ref
= {{M_SQRT1_2
, -M_SQRT1_2
, 0, 0}};
61 axis
= hkl_parameter_new_rotation("rotation", &v_ref
, &hkl_unit_angle_deg
);
62 is_quaternion(&q1_ref
, hkl_parameter_quaternion_get(axis
), __func__
);
63 ok(TRUE
== hkl_parameter_value_set(axis
, -M_PI_2
, HKL_UNIT_DEFAULT
, NULL
), __func__
);
64 is_quaternion(&q2_ref
, hkl_parameter_quaternion_get(axis
), __func__
);
65 hkl_parameter_free(axis
);
67 axis
= hkl_parameter_new_translation("translation", &v_ref
, &hkl_unit_length_mm
);
68 ok(NULL
== hkl_parameter_quaternion_get(axis
), __func__
);
69 hkl_parameter_free(axis
);
72 static void copy(void)
74 static HklVector v
= {{1, 0, 0}};
75 static HklQuaternion q_ref
= {{M_SQRT1_2
, -M_SQRT1_2
, 0, 0}};
81 axis
= hkl_parameter_new_rotation("rotation", &v
, &hkl_unit_angle_deg
);
82 ok(TRUE
== hkl_parameter_value_set(axis
, -M_PI_2
, HKL_UNIT_DEFAULT
, NULL
), __func__
);
83 copy
= hkl_parameter_new_copy(axis
);
84 is_string("rotation", hkl_parameter_name_get(copy
), __func__
);
85 hkl_parameter_min_max_get(copy
, &min
, &max
, HKL_UNIT_DEFAULT
);
86 is_double(-M_PI
, min
, HKL_EPSILON
, __func__
);
87 is_double(M_PI
, max
, HKL_EPSILON
, __func__
);
88 is_double(-M_PI_2
, hkl_parameter_value_get(copy
, HKL_UNIT_DEFAULT
), HKL_EPSILON
, __func__
);
89 ok(TRUE
== hkl_parameter_fit_get(copy
), __func__
);
90 is_quaternion(&q_ref
, hkl_parameter_quaternion_get(copy
), __func__
);
91 hkl_parameter_free(axis
);
92 hkl_parameter_free(copy
);
94 axis
= hkl_parameter_new_translation("translation", &v
, &hkl_unit_length_mm
);
95 ok(TRUE
== hkl_parameter_value_set(axis
, 100, HKL_UNIT_DEFAULT
, NULL
), __func__
);
96 copy
= hkl_parameter_new_copy(axis
);
97 is_string("translation", hkl_parameter_name_get(copy
), __func__
);
98 hkl_parameter_min_max_get(copy
, &min
, &max
, HKL_UNIT_DEFAULT
);
99 is_double(-DBL_MAX
, min
, HKL_EPSILON
, __func__
);
100 is_double(DBL_MAX
, max
, HKL_EPSILON
, __func__
);
101 is_double(100, hkl_parameter_value_get(copy
, HKL_UNIT_DEFAULT
), HKL_EPSILON
, __func__
);
102 ok(TRUE
== hkl_parameter_fit_get(copy
), __func__
);
103 ok(NULL
== hkl_parameter_quaternion_get(copy
), __func__
);
104 hkl_parameter_free(axis
);
105 hkl_parameter_free(copy
);
108 static void is_valid(void)
110 static HklVector v
= {{1, 0, 0}};
114 axis
= hkl_parameter_new_rotation("rotation", &v
, &hkl_unit_angle_deg
);
116 res
&= DIAG(TRUE
== hkl_parameter_value_set(axis
, 45, HKL_UNIT_USER
, NULL
));
117 res
&= DIAG(TRUE
== hkl_parameter_is_valid(axis
));
118 res
&= DIAG(TRUE
== hkl_parameter_min_max_set(axis
, -270, 0.0, HKL_UNIT_USER
, NULL
));
119 res
&= DIAG(FALSE
== hkl_parameter_is_valid(axis
));
120 res
&= DIAG(TRUE
== hkl_parameter_value_set(axis
, -45, HKL_UNIT_USER
, NULL
));
121 res
&= DIAG(TRUE
== hkl_parameter_is_valid(axis
));
122 res
&= DIAG(TRUE
== hkl_parameter_min_max_set(axis
, 350, 450, HKL_UNIT_USER
, NULL
));
123 res
&= DIAG(TRUE
== hkl_parameter_value_set(axis
, 45, HKL_UNIT_USER
, NULL
));
124 res
&= DIAG(TRUE
== hkl_parameter_is_valid(axis
));
125 res
&= DIAG(TRUE
== hkl_parameter_value_set(axis
, -45, HKL_UNIT_USER
, NULL
));
126 res
&= DIAG(FALSE
== hkl_parameter_is_valid(axis
));
127 res
&= DIAG(TRUE
== hkl_parameter_min_max_set(axis
, -10, 90, HKL_UNIT_USER
, NULL
));
128 res
&= DIAG(TRUE
== hkl_parameter_value_set(axis
, 405, HKL_UNIT_USER
, NULL
));
129 res
&= DIAG(TRUE
== hkl_parameter_is_valid(axis
));
130 res
&= DIAG(TRUE
== hkl_parameter_value_set(axis
, -405, HKL_UNIT_USER
, NULL
));
131 res
&= DIAG(FALSE
== hkl_parameter_is_valid(axis
));
132 hkl_parameter_free(axis
);
134 axis
= hkl_parameter_new_translation("translation", &v
, &hkl_unit_length_mm
);
135 res
&= DIAG(TRUE
== hkl_parameter_value_set(axis
, 45, HKL_UNIT_USER
, NULL
));
136 res
&= DIAG(TRUE
== hkl_parameter_is_valid(axis
));
137 res
&= DIAG(TRUE
== hkl_parameter_min_max_set(axis
, -270, 0, HKL_UNIT_USER
, NULL
));
138 res
&= DIAG(FALSE
== hkl_parameter_is_valid(axis
));
139 res
&= DIAG(TRUE
== hkl_parameter_value_set(axis
, -45, HKL_UNIT_USER
, NULL
));
140 res
&= DIAG(TRUE
== hkl_parameter_is_valid(axis
));
141 hkl_parameter_free(axis
);
143 ok(res
== TRUE
, __func__
);
146 static void set_value_smallest_in_range(void)
149 static HklVector v
= {{1, 0, 0}};
152 axis
= hkl_parameter_new_rotation("rotation", &v
, &hkl_unit_angle_deg
);
154 /* can not set a parameter value with a NaN */
155 res
&= DIAG(FALSE
== hkl_parameter_value_set(axis
, NAN
, HKL_UNIT_USER
, NULL
));
156 res
&= DIAG(FALSE
== hkl_parameter_value_set(axis
, NAN
, HKL_UNIT_DEFAULT
, NULL
));
158 res
&= DIAG(TRUE
== hkl_parameter_min_max_set(axis
, -190, 190, HKL_UNIT_USER
, NULL
));
160 res
&= DIAG(TRUE
== hkl_parameter_value_set(axis
, 185, HKL_UNIT_USER
, NULL
));
161 hkl_parameter_value_set_smallest_in_range(axis
);
162 is_double(-175., hkl_parameter_value_get(axis
, HKL_UNIT_USER
), HKL_EPSILON
, __func__
);
164 res
&= DIAG(TRUE
== hkl_parameter_value_set(axis
, 545, HKL_UNIT_USER
, NULL
));
165 hkl_parameter_value_set_smallest_in_range(axis
);
166 is_double(-175., hkl_parameter_value_get(axis
, HKL_UNIT_USER
), HKL_EPSILON
, __func__
);
168 res
&= DIAG(TRUE
== hkl_parameter_value_set(axis
, -185, HKL_UNIT_USER
, NULL
));
169 hkl_parameter_value_set_smallest_in_range(axis
);
170 is_double(-185., hkl_parameter_value_get(axis
, HKL_UNIT_USER
), HKL_EPSILON
, __func__
);
172 res
&= DIAG(TRUE
== hkl_parameter_value_set(axis
, 175, HKL_UNIT_USER
, NULL
));
173 hkl_parameter_value_set_smallest_in_range(axis
);
174 is_double(-185., hkl_parameter_value_get(axis
, HKL_UNIT_USER
), HKL_EPSILON
, __func__
);
176 res
&= DIAG(TRUE
== hkl_parameter_value_set(axis
, 190, HKL_UNIT_USER
, NULL
));
177 hkl_parameter_value_set_smallest_in_range(axis
);
178 is_double(-170., hkl_parameter_value_get(axis
, HKL_UNIT_USER
), HKL_EPSILON
, __func__
);
180 res
&= DIAG(TRUE
== hkl_parameter_value_set(axis
, -190, HKL_UNIT_USER
, NULL
));
181 hkl_parameter_value_set_smallest_in_range(axis
);
182 is_double(-190., hkl_parameter_value_get(axis
, HKL_UNIT_USER
), HKL_EPSILON
, __func__
);
184 hkl_parameter_free(axis
);
186 ok(res
== TRUE
, __func__
);
189 static void get_value_closest(void)
191 HklParameter
*axis1
, *axis2
;
192 static HklVector v
= {{1, 0, 0}};
194 axis1
= hkl_parameter_new_rotation("rotation", &v
, &hkl_unit_angle_deg
);
195 axis2
= hkl_parameter_new_rotation("rotation", &v
, &hkl_unit_angle_deg
);
197 ok(TRUE
== hkl_parameter_value_set(axis1
, 0, HKL_UNIT_USER
, NULL
), __func__
);
198 ok(TRUE
== hkl_parameter_value_set(axis2
, 0, HKL_UNIT_USER
, NULL
), __func__
);
199 is_double(0., hkl_parameter_value_get_closest(axis1
,
201 HKL_EPSILON
, __func__
);
203 /* change the range of axis1 */
204 ok(TRUE
== hkl_parameter_min_max_set(axis1
, -270, 180, HKL_UNIT_USER
, NULL
), __func__
);
205 ok(TRUE
== hkl_parameter_value_set(axis1
, 100, HKL_UNIT_USER
, NULL
), __func__
);
207 ok(TRUE
== hkl_parameter_value_set(axis2
, -75, HKL_UNIT_USER
, NULL
), __func__
);
208 is_double(100 * HKL_DEGTORAD
, hkl_parameter_value_get_closest(axis1
,
210 HKL_EPSILON
, __func__
);
211 ok(TRUE
== hkl_parameter_value_set(axis2
, -85, HKL_UNIT_USER
, NULL
), __func__
);
212 is_double(-260 * HKL_DEGTORAD
, hkl_parameter_value_get_closest(axis1
,
214 HKL_EPSILON
, __func__
);
216 hkl_parameter_free(axis2
);
217 hkl_parameter_free(axis1
);
220 static void transformation_cmp(void)
223 HklParameter
*axis1
, *axis2
, *translation1
, *translation2
;
224 static HklVector v1
= {{1, 0, 0}};
225 static HklVector v2
= {{0, 1, 0}};
227 axis1
= hkl_parameter_new_rotation("rotation", &v1
, &hkl_unit_angle_deg
);
228 axis2
= hkl_parameter_new_rotation("rotation", &v2
, &hkl_unit_angle_deg
);
229 translation1
= hkl_parameter_new_translation("translation", &v1
, &hkl_unit_length_mm
);
230 translation2
= hkl_parameter_new_translation("translation", &v2
, &hkl_unit_length_mm
);
232 res
&= DIAG(0 == hkl_parameter_transformation_cmp(axis1
, axis1
));
233 res
&= DIAG(0 != hkl_parameter_transformation_cmp(axis1
, axis2
));
234 res
&= DIAG(0 != hkl_parameter_transformation_cmp(axis1
, translation1
));
235 res
&= DIAG(0 != hkl_parameter_transformation_cmp(axis1
, translation2
));
236 res
&= DIAG(0 != hkl_parameter_transformation_cmp(axis2
, translation1
));
237 res
&= DIAG(0 != hkl_parameter_transformation_cmp(axis2
, translation2
));
238 res
&= DIAG(0 == hkl_parameter_transformation_cmp(translation1
, translation1
));
239 res
&= DIAG(0 != hkl_parameter_transformation_cmp(translation1
, translation2
));
240 res
&= DIAG(0 != hkl_parameter_transformation_cmp(translation1
, axis1
));
241 res
&= DIAG(0 != hkl_parameter_transformation_cmp(translation1
, axis2
));
242 res
&= DIAG(0 != hkl_parameter_transformation_cmp(translation2
, axis1
));
243 res
&= DIAG(0 != hkl_parameter_transformation_cmp(translation2
, axis2
));
245 ok(res
== TRUE
, __func__
);
247 hkl_parameter_free(translation2
);
248 hkl_parameter_free(translation1
);
249 hkl_parameter_free(axis2
);
250 hkl_parameter_free(axis1
);
261 set_value_smallest_in_range();
263 transformation_cmp();