[hkl] add hkl_geometry_[sample/detector]_rotation_get
[hkl.git] / tests / hkl-vector-t.c
blob64b04ded8908db535c9629e55a82f9ab0eb55414
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-2016 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-vector-private.h" /* use to test also the private API */
28 static void init(void)
30 HklVector v;
32 hkl_vector_init(&v, 1, 2, 3);
34 is_double(1., v.data[0], HKL_EPSILON, __func__);
35 is_double(2., v.data[1], HKL_EPSILON, __func__);
36 is_double(3., v.data[2], HKL_EPSILON, __func__);
39 static void cmp(void)
41 HklVector v1 = {{0.0, 1.0, 2.0}};
42 HklVector v2 = {{1.0, 2.0, 3.0}};
44 ok(0 == hkl_vector_cmp(&v1, &v1), __func__);
45 ok(1 == hkl_vector_cmp(&v1, &v2), __func__);
48 static void is_opposite(void)
50 HklVector v_ref = {{0, 1, 2}};
51 HklVector v1 = {{1, 2, 3}};
52 HklVector v2 = {{0, -1, -2}};
54 ok(FALSE == hkl_vector_is_opposite(&v_ref, &v1), __func__);
55 ok(TRUE == hkl_vector_is_opposite(&v_ref, &v2), __func__);
58 static void norm2(void)
60 HklVector v1 = {{0.0, 1.0, 2.0}};
61 HklVector v2 = {{-1.0, 1.0, 2.0}};
63 is_double(sqrt(5.0), hkl_vector_norm2(&v1), HKL_EPSILON, __func__);
64 is_double(sqrt(6.0), hkl_vector_norm2(&v2), HKL_EPSILON, __func__);
67 static void normalize(void)
69 HklVector v_ref = {{1. /sqrt(2.), 1. / sqrt(2.), 0.}};
70 HklVector v = {{1., 1., 0.}};
72 hkl_vector_normalize(&v);
73 ok(0 == hkl_vector_cmp(&v_ref, &v), __func__);
76 static void scalar_product(void)
78 HklVector v = {{0.0, 1.0, 2.0}};
80 double scalar = hkl_vector_scalar_product(&v, &v);
81 is_double( 5.0, scalar, HKL_EPSILON, __func__ );
84 static void vectorial_product(void)
86 HklVector v = {{0.0, 1.0, 2.0}};
87 HklVector v1 = {{1.0, 2.0, 3.0}};
88 HklVector v_ref = {{-1.0, 2.0, -1.0}};
90 hkl_vector_vectorial_product(&v, &v1);
91 ok(0 == hkl_vector_cmp(&v_ref, &v), __func__);
94 static void angle(void)
96 double angle;
97 HklVector v = {{1., 0., 0.}};
98 HklVector v1 = {{1., 1., 0.}};
99 HklVector v2 = {{1., 1., .5}};
100 HklVector v3 = {{1., .5, -1}};
101 HklVector v4 = {{0., 1., 0.}};
102 HklVector v5 = {{0., -1., 0.}};
104 angle = hkl_vector_angle(&v, &v);
105 is_double(0., angle, HKL_EPSILON, __func__);
107 angle = hkl_vector_angle(&v, &v1);
108 is_double(acos(1./sqrt(2.)), angle, HKL_EPSILON, __func__);
110 angle = hkl_vector_angle(&v2, &v3);
111 is_double(acos(1./2.25), angle, HKL_EPSILON, __func__);
113 angle = hkl_vector_angle(&v, &v4);
114 is_double(90 * HKL_DEGTORAD, angle, HKL_EPSILON, __func__);
116 angle = hkl_vector_angle(&v, &v5);
117 is_double(90 * HKL_DEGTORAD, angle, HKL_EPSILON, __func__);
120 static void oriented_angle(void)
122 double angle;
123 HklVector v = {{1., 0., 0.}};
124 HklVector v1 = {{1., 1., 0.}};
125 HklVector v2 = {{0., 1., 0.}};
126 HklVector v3 = {{0., -1., 0.}};
127 HklVector ref = {{0, 0, 1}};
129 angle = hkl_vector_oriented_angle(&v, &v, &ref);
130 is_double(0., angle, HKL_EPSILON, __func__);
132 angle = hkl_vector_oriented_angle(&v, &v1, &ref);
133 is_double(acos(1./sqrt(2.)), angle, HKL_EPSILON, __func__);
135 angle = hkl_vector_oriented_angle(&v, &v2, &ref);
136 is_double(90 * HKL_DEGTORAD, angle, HKL_EPSILON, __func__);
138 angle = hkl_vector_oriented_angle(&v, &v3, &ref);
139 is_double(-90 * HKL_DEGTORAD, angle, HKL_EPSILON, __func__);
142 static void oriented_angle_points(void)
144 double angle;
145 HklVector v = {{1., 0., 1.}};
146 HklVector v1 = {{1., 1., 1.}};
147 HklVector v2 = {{0., 1., 1.}};
148 HklVector v3 = {{0., -1., 1.}};
149 HklVector ref = {{0, 0, 1}};
151 angle = hkl_vector_oriented_angle_points(&v, &ref, &v, &ref);
152 is_double(0., angle, HKL_EPSILON, __func__);
154 angle = hkl_vector_oriented_angle_points(&v, &ref, &v1, &ref);
155 is_double(acos(1./sqrt(2.)), angle, HKL_EPSILON, __func__);
157 angle = hkl_vector_oriented_angle_points(&v, &ref, &v2, &ref);
158 is_double(90 * HKL_DEGTORAD, angle, HKL_EPSILON, __func__);
160 angle = hkl_vector_oriented_angle_points(&v, &ref, &v3, &ref);
161 is_double(-90 * HKL_DEGTORAD, angle, HKL_EPSILON, __func__);
164 static void rotated_around_vector(void)
166 HklVector x = {{1, 0, 0}};
167 HklVector z = {{0, 0, 1}};
168 HklVector y_ref = {{0, 1, 0}};
170 hkl_vector_rotated_around_vector(&x, &z, 90*HKL_DEGTORAD);
171 ok(0 == hkl_vector_cmp(&y_ref, &x), __func__);
174 static void rotated_around_line(void)
176 HklVector x = {{1, 0, 0}};
177 HklVector c1 = {{0, 0, 0}};
178 HklVector c2 = {{0, 0, 1}};
179 HklVector x_ref = {{1, 0, 0}};
180 HklVector y_ref = {{0, 1, 0}};
182 hkl_vector_rotated_around_line(&x, 0*HKL_DEGTORAD, &c1, &c2);
183 ok(0 == hkl_vector_cmp(&x_ref, &x), __func__);
185 hkl_vector_rotated_around_line(&x, 90*HKL_DEGTORAD, &c1, &c2);
186 ok(0 == hkl_vector_cmp(&y_ref, &x), __func__);
189 static void times_matrix(void)
191 HklMatrix *m = hkl_matrix_new_full(1.0, 3.0,-2.0,
192 10.0, 5.0, 5.0,
193 -3.0, 2.0, 0.0);
194 HklVector v = {{1.0, 2.0, 3.0}};
195 HklVector v_ref = {{12., 19., 8.}};
197 hkl_vector_times_matrix(&v, m);
198 ok(0 == hkl_vector_cmp(&v_ref, &v), __func__);
199 hkl_matrix_free(m);
202 static void project_on_plan(void)
204 HklVector v;
205 HklVector v_ref = {{1, 0, 0}};
206 HklVector v1_ref = {{1, 0, 1}};
207 HklVector v2_ref = {{1, 0, -2}};
208 HklVector v1 = {{1, 0, 2}};
209 HklVector plan = {{0, 0, 1}};
210 HklVector point1 = {{0, 0, 1}};
211 HklVector point2 = {{0, 0, -2}};
213 v = v1;
214 hkl_vector_project_on_plan(&v, &plan);
215 ok(0 == hkl_vector_cmp(&v_ref, &v), __func__);
217 v = v1;
218 hkl_vector_project_on_plan_with_point(&v, &plan, &point1);
219 ok(0 == hkl_vector_cmp(&v1_ref, &v), __func__);
221 v = v1;
222 hkl_vector_project_on_plan_with_point(&v, &plan, &point2);
223 ok(0 == hkl_vector_cmp(&v2_ref, &v), __func__);
226 int main(void)
228 plan(32);
230 init();
231 cmp();
232 is_opposite();
233 norm2();
234 normalize();
235 scalar_product();
236 vectorial_product();
237 angle();
238 oriented_angle();
239 oriented_angle_points();
240 rotated_around_vector();
241 rotated_around_line();
242 times_matrix();
243 project_on_plan();
245 return 0;