* add the Hklquaternnion documentation
[hkl.git] / test / hkl-test-quaternion.c
bloba7cf03e500c3b318a36fcbe3c01a788ecd4ba242
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>
22 #include <hkl.h>
24 #include "hkl-test.h"
26 #ifdef HKL_TEST_SUITE_NAME
27 # undef HKL_TEST_SUITE_NAME
28 #endif
29 #define HKL_TEST_SUITE_NAME quaternion
31 HKL_TEST_SUITE_FUNC(assignment)
33 HklQuaternion q = {{1, 0, 0, 0}};
34 HklQuaternion copy = q;
36 HKL_ASSERT_DOUBLES_EQUAL(1., copy.data[0], HKL_EPSILON);
37 HKL_ASSERT_DOUBLES_EQUAL(0., copy.data[1], HKL_EPSILON);
38 HKL_ASSERT_DOUBLES_EQUAL(0., copy.data[2], HKL_EPSILON);
39 HKL_ASSERT_DOUBLES_EQUAL(0., copy.data[3], HKL_EPSILON);
41 return HKL_TEST_PASS;
44 HKL_TEST_SUITE_FUNC(cmp)
46 HklQuaternion q_ref = {{1., 2., 3., 4.}};
47 HklQuaternion q = {{1., 2., 3., 4.}};
48 HklQuaternion q1 = {{1., 1., 3., 4.}};
50 HKL_ASSERT_EQUAL(HKL_TRUE, hkl_quaternion_cmp(&q_ref, &q));
51 HKL_ASSERT_EQUAL(HKL_FALSE, hkl_quaternion_cmp(&q_ref, &q1));
53 // test the assignation
54 q1 = q_ref;
55 HKL_ASSERT_EQUAL(HKL_TRUE, hkl_quaternion_cmp(&q_ref, &q1));
57 return HKL_TEST_PASS;
60 HKL_TEST_SUITE_FUNC(init_from_vector)
62 HklQuaternion q_ref = {{0, 1, -1, .5}};
63 HklVector v = {{1., -1., .5}};
64 HklQuaternion q;
66 hkl_quaternion_init_from_vector(&q, &v);
67 HKL_ASSERT_EQUAL(HKL_TRUE, hkl_quaternion_cmp(&q_ref, &q));
69 return HKL_TEST_PASS;
72 HKL_TEST_SUITE_FUNC(init_from_angle_and_axe)
74 HklQuaternion q_ref1 = {{1, 0, 0, 0}};
75 HklQuaternion q_ref2 = {{sqrt(2.)/2., sqrt(2./9.), -sqrt(2./9.), sqrt(1./18.)}};
76 HklVector v_ref2 = {{1., -1., .5}};
77 HklQuaternion q;
79 hkl_quaternion_init_from_angle_and_axe(&q, 0, &v_ref2);
80 HKL_ASSERT_EQUAL(HKL_TRUE, hkl_quaternion_cmp(&q_ref1, &q));
82 hkl_quaternion_init_from_angle_and_axe(&q, 90. * HKL_DEGTORAD, &v_ref2);
83 HKL_ASSERT_EQUAL(HKL_TRUE, hkl_quaternion_cmp(&q_ref2, &q));
85 return HKL_TEST_PASS;
88 HKL_TEST_SUITE_FUNC(times_quaternion)
90 HklQuaternion q_ref = {{-28., 4., 6., 8.}};
91 HklQuaternion q = {{1., 2., 3., 4.}};
93 hkl_quaternion_times_quaternion(&q, &q);
94 HKL_ASSERT_EQUAL(HKL_TRUE, hkl_quaternion_cmp(&q_ref, &q));
96 return HKL_TEST_PASS;
99 HKL_TEST_SUITE_FUNC(norm2)
101 HklQuaternion q = {{1., 2., 3., 4.}};
103 HKL_ASSERT_DOUBLES_EQUAL(sqrt(30.), hkl_quaternion_norm2(&q), HKL_EPSILON);
105 return HKL_TEST_PASS;
108 HKL_TEST_SUITE_FUNC(conjugate)
110 HklQuaternion q_ref = {{1., -2., -3., -4.}};
111 HklQuaternion q = {{1., 2., 3., 4.}};
113 hkl_quaternion_conjugate(&q);
114 HKL_ASSERT_EQUAL(HKL_TRUE, hkl_quaternion_cmp(&q_ref, &q));
116 return HKL_TEST_PASS;
119 HKL_TEST_SUITE_FUNC(to_matrix)
121 HklQuaternion q_ref = {{1./sqrt(2), 0, 0, 1./sqrt(2)}};
122 HklMatrix m_ref = {{{0,-1, 0},
123 {1, 0, 0},
124 {0, 0, 1}}};
125 HklMatrix m;
127 hkl_quaternion_to_matrix(&q_ref, &m);
128 HKL_ASSERT_EQUAL(HKL_TRUE, hkl_matrix_cmp(&m_ref, &m));
130 return HKL_TEST_PASS;
133 HKL_TEST_SUITE_FUNC(to_angle_and_axe)
135 HklVector v_ref = {{0 ,0, 1}};
136 HklVector v_null = {{0 ,0, 0}};
137 HklQuaternion q_I = {{1, 0, 0, 0}};
139 int i;
140 double angle_ref;
141 double angle;
142 HklVector v;
143 HklQuaternion q;
146 // test the q = (1, 0, 0, 0) solution axe == (0, 0, 0) and angle = 0.
147 hkl_quaternion_to_angle_and_axe(&q_I, &angle, &v);
148 HKL_ASSERT_EQUAL(0, hkl_vector_cmp(&v_null, &v));
149 HKL_ASSERT_DOUBLES_EQUAL(0., angle, HKL_EPSILON);
151 // test other cases
152 for(i=-180; i<180; i++) {
153 angle_ref = i * HKL_DEGTORAD;
154 hkl_quaternion_init_from_angle_and_axe(&q, angle_ref, &v_ref);
155 hkl_quaternion_to_angle_and_axe(&q, &angle, &v);
157 if (!hkl_vector_cmp(&v_ref, &v))
158 HKL_ASSERT_DOUBLES_EQUAL(angle_ref, angle, HKL_EPSILON);
159 else if (hkl_vector_is_opposite(&v, &v_ref))
160 HKL_ASSERT_DOUBLES_EQUAL(angle_ref, -angle, HKL_EPSILON);
163 return HKL_TEST_PASS;
166 HKL_TEST_SUITE_BEGIN
168 HKL_TEST( assignment );
169 HKL_TEST( cmp );
170 HKL_TEST( init_from_vector );
171 HKL_TEST( init_from_angle_and_axe );
172 HKL_TEST( times_quaternion );
173 HKL_TEST( norm2 );
174 HKL_TEST( conjugate );
175 HKL_TEST( to_matrix );
176 HKL_TEST( to_angle_and_axe );
178 HKL_TEST_SUITE_END