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>
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__
);
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 */
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}};
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}};
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,
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
);
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}};
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
)
155 init_from_angle_and_axe();