rename hkl_engine_pseudo_axes_get -> hkl_engine_pseudo_axes_names_get
[hkl.git] / tests / hkl-quaternion-t.c
blobe7147c8103c8cd492dedeb883157872f35f0b747
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-quaternion-private.h"
28 static void assignment(void)
30 HklQuaternion q = {{1, 0, 0, 0}};
31 HklQuaternion copy = q;
33 is_double(1., copy.data[0], HKL_EPSILON, __func__);
34 is_double(0., copy.data[1], HKL_EPSILON, __func__);
35 is_double(0., copy.data[2], HKL_EPSILON, __func__);
36 is_double(0., copy.data[3], HKL_EPSILON, __func__);
39 static void cmp(void)
41 HklQuaternion q_ref = {{1., 2., 3., 4.}};
42 HklQuaternion q = {{1., 2., 3., 4.}};
43 HklQuaternion q1 = {{1., 1., 3., 4.}};
45 ok(HKL_TRUE == hkl_quaternion_cmp(&q_ref, &q), __func__);
46 ok(HKL_FALSE == hkl_quaternion_cmp(&q_ref, &q1), __func__);
48 /* test the assignation */
49 q1 = q_ref;
50 ok(HKL_TRUE == hkl_quaternion_cmp(&q_ref, &q1), __func__);
53 static void init_from_vector(void)
55 HklQuaternion q_ref = {{0, 1, -1, .5}};
56 HklVector v = {{1., -1., .5}};
57 HklQuaternion q;
59 hkl_quaternion_init_from_vector(&q, &v);
60 ok(HKL_TRUE == hkl_quaternion_cmp(&q_ref, &q), __func__);
63 static void init_from_angle_and_axe(void)
65 HklQuaternion q_ref1 = {{1, 0, 0, 0}};
66 HklQuaternion q_ref2 = {{sqrt(2.)/2., sqrt(2./9.), -sqrt(2./9.), sqrt(1./18.)}};
67 HklVector v_ref2 = {{1., -1., .5}};
68 HklQuaternion q;
70 hkl_quaternion_init_from_angle_and_axe(&q, 0, &v_ref2);
71 ok(HKL_TRUE == hkl_quaternion_cmp(&q_ref1, &q), __func__);
73 hkl_quaternion_init_from_angle_and_axe(&q, 90. * HKL_DEGTORAD, &v_ref2);
74 ok(HKL_TRUE == hkl_quaternion_cmp(&q_ref2, &q), __func__);
77 static void times_quaternion(void)
79 HklQuaternion q_ref = {{-28., 4., 6., 8.}};
80 HklQuaternion q = {{1., 2., 3., 4.}};
82 hkl_quaternion_times_quaternion(&q, &q);
83 ok(HKL_TRUE == hkl_quaternion_cmp(&q_ref, &q), __func__);
86 static void norm2(void)
88 HklQuaternion q = {{1., 2., 3., 4.}};
90 is_double(sqrt(30.), hkl_quaternion_norm2(&q), HKL_EPSILON, __func__);
93 static void conjugate(void)
95 HklQuaternion q_ref = {{1., -2., -3., -4.}};
96 HklQuaternion q = {{1., 2., 3., 4.}};
98 hkl_quaternion_conjugate(&q);
99 ok(HKL_TRUE == hkl_quaternion_cmp(&q_ref, &q), __func__);
102 static void to_matrix(void)
104 HklQuaternion q_ref = {{1./sqrt(2), 0, 0, 1./sqrt(2)}};
105 HklMatrix *m_ref = hkl_matrix_new_full(0,-1, 0,
106 1, 0, 0,
107 0, 0, 1);
108 HklMatrix *m = hkl_matrix_new();
110 hkl_quaternion_to_matrix(&q_ref, m);
111 ok(HKL_TRUE == hkl_matrix_cmp(m_ref, m), __func__);
113 hkl_matrix_free(m_ref);
114 hkl_matrix_free(m);
117 static void to_angle_and_axe(void)
119 HklVector v_ref = {{0 ,0, 1}};
120 HklVector v_null = {{0 ,0, 0}};
121 HklQuaternion q_I = {{1, 0, 0, 0}};
123 int i;
124 double angle_ref;
125 double angle;
126 HklVector v;
127 HklQuaternion q;
130 /* test the q = (1, 0, 0, 0) solution axe == (0, 0, 0) and angle = 0. */
131 hkl_quaternion_to_angle_and_axe(&q_I, &angle, &v);
132 ok(0 == hkl_vector_cmp(&v_null, &v), __func__);
133 is_double(0., angle, HKL_EPSILON, __func__);
135 /* test other cases */
136 for(i=-180; i<180; i++) {
137 angle_ref = i * HKL_DEGTORAD;
138 hkl_quaternion_init_from_angle_and_axe(&q, angle_ref, &v_ref);
139 hkl_quaternion_to_angle_and_axe(&q, &angle, &v);
141 if (!hkl_vector_cmp(&v_ref, &v))
142 is_double(angle_ref, angle, HKL_EPSILON, __func__);
143 else if (hkl_vector_is_opposite(&v, &v_ref))
144 is_double(angle_ref, -angle, HKL_EPSILON, __func__);
148 int main(int argc, char** argv)
150 plan(375);
152 assignment();
153 cmp();
154 init_from_vector();
155 init_from_angle_and_axe();
156 times_quaternion();
157 norm2();
158 conjugate();
159 to_matrix();
160 to_angle_and_axe();
162 return 0;