[doc] corrections
[hkl.git] / tests / hkl-quaternion-t.c
blob5e172761dda115a4df70ffbb104791c85d378001
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-2015 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>
25 #include <tap/hkl-tap.h>
27 #include "hkl-quaternion-private.h"
29 static void assignment(void)
31 HklQuaternion q = {{1, 0, 0, 0}};
32 HklQuaternion copy = q;
34 is_double(1., copy.data[0], HKL_EPSILON, __func__);
35 is_double(0., copy.data[1], HKL_EPSILON, __func__);
36 is_double(0., copy.data[2], HKL_EPSILON, __func__);
37 is_double(0., copy.data[3], HKL_EPSILON, __func__);
40 static void cmp(void)
42 HklQuaternion q_ref = {{1., 2., 3., 4.}};
43 HklQuaternion q = {{1., 2., 3., 4.}};
44 HklQuaternion q1 = {{1., 1., 3., 4.}};
46 ok(TRUE == hkl_quaternion_cmp(&q_ref, &q), __func__);
47 ok(FALSE == hkl_quaternion_cmp(&q_ref, &q1), __func__);
49 /* test the assignation */
50 q1 = q_ref;
51 ok(TRUE == hkl_quaternion_cmp(&q_ref, &q1), __func__);
54 static void init_from_vector(void)
56 HklQuaternion q_ref = {{0, 1, -1, .5}};
57 HklVector v = {{1., -1., .5}};
58 HklQuaternion q;
60 hkl_quaternion_init_from_vector(&q, &v);
61 ok(TRUE == hkl_quaternion_cmp(&q_ref, &q), __func__);
64 static void init_from_angle_and_axe(void)
66 HklQuaternion q_ref1 = {{1, 0, 0, 0}};
67 HklQuaternion q_ref2 = {{sqrt(2.)/2., sqrt(2./9.), -sqrt(2./9.), sqrt(1./18.)}};
68 HklVector v_ref2 = {{1., -1., .5}};
69 HklQuaternion q;
71 hkl_quaternion_init_from_angle_and_axe(&q, 0, &v_ref2);
72 ok(TRUE == hkl_quaternion_cmp(&q_ref1, &q), __func__);
74 hkl_quaternion_init_from_angle_and_axe(&q, 90. * HKL_DEGTORAD, &v_ref2);
75 ok(TRUE == hkl_quaternion_cmp(&q_ref2, &q), __func__);
78 static void times_quaternion(void)
80 HklQuaternion q_ref = {{-28., 4., 6., 8.}};
81 HklQuaternion q = {{1., 2., 3., 4.}};
83 hkl_quaternion_times_quaternion(&q, &q);
84 ok(TRUE == hkl_quaternion_cmp(&q_ref, &q), __func__);
87 static void norm2(void)
89 HklQuaternion q = {{1., 2., 3., 4.}};
91 is_double(sqrt(30.), hkl_quaternion_norm2(&q), HKL_EPSILON, __func__);
94 static void conjugate(void)
96 HklQuaternion q_ref = {{1., -2., -3., -4.}};
97 HklQuaternion q = {{1., 2., 3., 4.}};
99 hkl_quaternion_conjugate(&q);
100 ok(TRUE == hkl_quaternion_cmp(&q_ref, &q), __func__);
103 static void to_matrix(void)
105 HklQuaternion q_ref = {{1./sqrt(2), 0, 0, 1./sqrt(2)}};
106 HklMatrix *m_ref = hkl_matrix_new_full(0,-1, 0,
107 1, 0, 0,
108 0, 0, 1);
109 HklMatrix *m = hkl_matrix_new();
111 hkl_quaternion_to_matrix(&q_ref, m);
112 is_matrix(m_ref, m, __func__);
114 hkl_matrix_free(m_ref);
115 hkl_matrix_free(m);
118 static void to_angle_and_axe(void)
120 HklVector v_ref = {{0 ,0, 1}};
121 HklVector v_null = {{0 ,0, 0}};
122 HklQuaternion q_I = {{1, 0, 0, 0}};
124 int i;
125 double angle_ref;
126 double angle;
127 HklVector v;
128 HklQuaternion q;
131 /* test the q = (1, 0, 0, 0) solution axe == (0, 0, 0) and angle = 0. */
132 hkl_quaternion_to_angle_and_axe(&q_I, &angle, &v);
133 ok(0 == hkl_vector_cmp(&v_null, &v), __func__);
134 is_double(0., angle, HKL_EPSILON, __func__);
136 /* test other cases */
137 for(i=-180; i<180; i++) {
138 angle_ref = i * HKL_DEGTORAD;
139 hkl_quaternion_init_from_angle_and_axe(&q, angle_ref, &v_ref);
140 hkl_quaternion_to_angle_and_axe(&q, &angle, &v);
142 if (!hkl_vector_cmp(&v_ref, &v))
143 is_double(angle_ref, angle, HKL_EPSILON, __func__);
144 else if (hkl_vector_is_opposite(&v, &v_ref))
145 is_double(angle_ref, -angle, HKL_EPSILON, __func__);
149 int main(int argc, char** argv)
151 plan(375);
153 assignment();
154 cmp();
155 init_from_vector();
156 init_from_angle_and_axe();
157 times_quaternion();
158 norm2();
159 conjugate();
160 to_matrix();
161 to_angle_and_axe();
163 return 0;