1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
|
from __future__ import absolute_import
try:
import unittest2 as unittest
except:
import unittest
import numpy as np
from pyrr.objects.matrix33 import Matrix33
from pyrr.objects.matrix44 import Matrix44
from pyrr.objects.quaternion import Quaternion
from pyrr.objects.vector3 import Vector3
from pyrr.objects.vector4 import Vector4
from pyrr import matrix33
from pyrr import matrix44
from pyrr import quaternion
class test_object_quaternion(unittest.TestCase):
_shape = (4,)
_size = np.multiply.reduce(_shape)
def test_imports(self):
import pyrr
pyrr.Quaternion()
pyrr.quaternion.Quaternion()
pyrr.objects.quaternion.Quaternion()
from pyrr import Quaternion
from pyrr.objects import Quaternion
from pyrr.objects.quaternion import Quaternion
def test_create(self):
q = Quaternion()
self.assertTrue(np.array_equal(q, [0., 0., 0., 1.]))
self.assertEqual(q.shape, self._shape)
q = Quaternion([1., 2., 3., 4.])
self.assertTrue(np.array_equal(q, [1., 2., 3., 4.]))
self.assertEqual(q.shape, self._shape)
q = Quaternion(Quaternion([1., 2., 3., 4.]))
self.assertTrue(np.array_equal(q, [1., 2., 3., 4.]))
self.assertEqual(q.shape, self._shape)
def test_from_x_rotation(self):
# 180 degree turn around X axis
q = Quaternion.from_x_rotation(np.pi)
self.assertTrue(np.allclose(q, [1., 0., 0., 0.]))
self.assertTrue(np.allclose(q * Vector3([1., 0., 0.]), [1., 0., 0.]))
self.assertTrue(np.allclose(q * Vector3([0., 1., 0.]), [0.,-1., 0.]))
self.assertTrue(np.allclose(q * Vector3([0., 0., 1.]), [0., 0.,-1.]))
# 90 degree rotation around X axis
q = Quaternion.from_x_rotation(np.pi / 2.)
self.assertTrue(np.allclose(q, [np.sqrt(0.5), 0., 0., np.sqrt(0.5)]))
self.assertTrue(np.allclose(q * Vector3([1., 0., 0.]), [1., 0., 0.]))
self.assertTrue(np.allclose(q * Vector3([0., 1., 0.]), [0., 0., 1.]))
self.assertTrue(np.allclose(q * Vector3([0., 0., 1.]), [0.,-1., 0.]))
# -90 degree rotation around X axis
q = Quaternion.from_x_rotation(-np.pi / 2.)
self.assertTrue(np.allclose(q, [-np.sqrt(0.5), 0., 0., np.sqrt(0.5)]))
self.assertTrue(np.allclose(q * Vector3([1., 0., 0.]), [1., 0., 0.]))
self.assertTrue(np.allclose(q * Vector3([0., 1., 0.]), [0., 0.,-1.]))
self.assertTrue(np.allclose(q * Vector3([0., 0., 1.]), [0., 1., 0.]))
def test_from_y_rotation(self):
# 180 degree turn around Y axis
q = Quaternion.from_y_rotation(np.pi)
self.assertTrue(np.allclose(q, [0., 1., 0., 0.]))
self.assertTrue(np.allclose(q * Vector3([1., 0., 0.]), [-1., 0., 0.]))
self.assertTrue(np.allclose(q * Vector3([0., 1., 0.]), [0., 1., 0.]))
self.assertTrue(np.allclose(q * Vector3([0., 0., 1.]), [0., 0.,-1.]))
# 90 degree rotation around Y axis
q = Quaternion.from_y_rotation(np.pi / 2.)
self.assertTrue(np.allclose(q, [0., np.sqrt(0.5), 0., np.sqrt(0.5)]))
self.assertTrue(np.allclose(q * Vector3([1., 0., 0.]), [0., 0.,-1.]))
self.assertTrue(np.allclose(q * Vector3([0., 1., 0.]), [0., 1., 0.]))
self.assertTrue(np.allclose(q * Vector3([0., 0., 1.]), [1., 0., 0.]))
# -90 degree rotation around Y axis
q = Quaternion.from_y_rotation(-np.pi / 2.)
self.assertTrue(np.allclose(q, [0., -np.sqrt(0.5), 0., np.sqrt(0.5)]))
self.assertTrue(np.allclose(q * Vector3([1., 0., 0.]), [0., 0., 1.]))
self.assertTrue(np.allclose(q * Vector3([0., 1., 0.]), [0., 1., 0.]))
self.assertTrue(np.allclose(q * Vector3([0., 0., 1.]), [-1., 0., 0.]))
def test_from_z_rotation(self):
# 180 degree turn around Z axis
q = Quaternion.from_z_rotation(np.pi)
self.assertTrue(np.allclose(q, [0., 0., 1., 0.]))
self.assertTrue(np.allclose(q * Vector3([1., 0., 0.]), [-1., 0., 0.]))
self.assertTrue(np.allclose(q * Vector3([0., 1., 0.]), [0.,-1., 0.]))
self.assertTrue(np.allclose(q * Vector3([0., 0., 1.]), [0., 0., 1.]))
# 90 degree rotation around Z axis
q = Quaternion.from_z_rotation(np.pi / 2.)
self.assertTrue(np.allclose(q, [0., 0., np.sqrt(0.5), np.sqrt(0.5)]))
self.assertTrue(np.allclose(q * Vector3([1., 0., 0.]), [0., 1., 0.]))
self.assertTrue(np.allclose(q * Vector3([0., 1., 0.]), [-1., 0., 0.]))
self.assertTrue(np.allclose(q * Vector3([0., 0., 1.]), [0., 0., 1.]))
# -90 degree rotation around Z axis
q = Quaternion.from_z_rotation(-np.pi / 2.)
self.assertTrue(np.allclose(q, [0., 0., -np.sqrt(0.5), np.sqrt(0.5)]))
self.assertTrue(np.allclose(q * Vector3([1., 0., 0.]), [0.,-1., 0.]))
self.assertTrue(np.allclose(q * Vector3([0., 1., 0.]), [1., 0., 0.]))
self.assertTrue(np.allclose(q * Vector3([0., 0., 1.]), [0., 0., 1.]))
def test_from_axis_rotation(self):
q = Quaternion.from_axis_rotation([1., 0., 0.], np.pi / 2.)
self.assertTrue(np.allclose(q, [np.sqrt(0.5), 0., 0., np.sqrt(0.5)]))
self.assertTrue(np.allclose(q * Vector3([1., 0., 0.]), [1., 0., 0.]))
self.assertTrue(np.allclose(q * Vector3([0., 1., 0.]), [0., 0., 1.]))
self.assertTrue(np.allclose(q * Vector3([0., 0., 1.]), [0.,-1., 0.]))
def test_from_axis(self):
source = np.array([np.pi / 2, 0, 0])
result = Quaternion.from_axis(source)
expected = np.array([np.sqrt(0.5), 0, 0, np.sqrt(0.5)])
self.assertTrue(np.allclose(result, expected))
source = np.array([0, np.pi, 0])
result = Quaternion.from_axis(source)
expected = np.array([0, 1, 0, 0])
self.assertTrue(np.allclose(result, expected))
source = np.array([0, 0, 2 * np.pi])
result = Quaternion.from_axis(source)
expected = np.array([0, 0, 0, -1])
self.assertTrue(np.allclose(result, expected))
@unittest.skip('Not implemented')
def test_from_eulers(self):
pass
@unittest.skip('Not implemented')
def test_from_inverse_of_eulers(self):
pass
def test_length(self):
q = Quaternion.from_x_rotation(np.pi / 2.0)
self.assertTrue(np.allclose(q.length, quaternion.length(q)))
def test_normalize(self):
q = Quaternion([1., 2., 3., 4.])
self.assertFalse(np.allclose(q.length, 1.))
q.normalize()
self.assertTrue(np.allclose(q.length, 1.))
def test_normalized(self):
q1 = Quaternion([1., 2., 3., 4.])
self.assertFalse(np.allclose(q1.length, 1.))
q2 = q1.normalized
self.assertFalse(np.allclose(q1.length, 1.))
self.assertTrue(np.allclose(q2.length, 1.))
def test_angle(self):
q = Quaternion.from_x_rotation(np.pi / 2.0)
self.assertEqual(q.angle, quaternion.rotation_angle(q))
def test_axis(self):
q = Quaternion.from_x_rotation(np.pi / 2.0)
self.assertTrue(np.allclose(q.axis, quaternion.rotation_axis(q)))
def test_cross(self):
q1 = Quaternion.from_x_rotation(np.pi / 2.0)
q2 = Quaternion.from_y_rotation(np.pi / 2.0)
self.assertTrue(np.allclose(q1.cross(q2), quaternion.cross(q1, q2)))
def test_dot(self):
q1 = Quaternion.from_x_rotation(np.pi / 2.0)
q2 = Quaternion.from_y_rotation(np.pi / 2.0)
self.assertTrue(np.allclose(q1.dot(q2), quaternion.dot(q1, q2)))
def test_conjugate(self):
q = Quaternion.from_x_rotation(np.pi / 2.0)
self.assertTrue(np.allclose(q.conjugate, quaternion.conjugate(q)))
def test_inverse(self):
q = Quaternion.from_x_rotation(np.pi / 2.0)
self.assertTrue(np.allclose(q.inverse, quaternion.inverse(q)))
def test_exp(self):
source = Quaternion.from_eulers([0, np.pi / 2, 0])
result = source.exp()
expected = np.array([0, 1.31753841, 0, 1.54186346])
self.assertTrue(np.allclose(result, expected))
def test_power(self):
q1 = Quaternion.from_x_rotation(np.pi / 2.0)
q2 = Quaternion.from_x_rotation(np.pi / 2.0)
self.assertTrue(np.allclose(q1.power(2.0), quaternion.power(q2, 2.0)))
def test_negative(self):
q = Quaternion.from_x_rotation(np.pi / 2.0)
self.assertTrue(np.allclose(q.negative, quaternion.negate(q)))
def test_is_identity(self):
self.assertTrue(quaternion.is_identity(Quaternion()))
self.assertTrue(quaternion.is_identity(Quaternion([0., 0., 0., 1.])))
self.assertFalse(quaternion.is_identity(Quaternion([1., 0., 0., 0.])))
def test_matrix33(self):
q = Quaternion.from_x_rotation(np.pi / 2.0)
self.assertTrue(np.allclose(q.matrix33, matrix33.create_from_quaternion(q)))
def test_matrix44(self):
q = Quaternion.from_x_rotation(np.pi / 2.0)
self.assertTrue(np.allclose(q.matrix44, matrix44.create_from_quaternion(q)))
def test_operators_matrix33(self):
q = Quaternion()
m = Matrix33.from_x_rotation(0.5)
# add
self.assertRaises(ValueError, lambda: q + m)
# subtract
self.assertRaises(ValueError, lambda: q - m)
# multiply
self.assertTrue(np.array_equal(q * m, quaternion.cross(quaternion.create(), quaternion.create_from_matrix(matrix33.create_from_x_rotation(0.5)))))
# divide
self.assertRaises(ValueError, lambda: q / m)
def test_operators_matrix44(self):
q = Quaternion()
m = Matrix44.from_x_rotation(0.5)
# add
self.assertRaises(ValueError, lambda: q + m)
# subtract
self.assertRaises(ValueError, lambda: q - m)
# multiply
self.assertTrue(np.array_equal(q * m, quaternion.cross(quaternion.create(), quaternion.create_from_matrix(matrix44.create_from_x_rotation(0.5)))))
# divide
self.assertRaises(ValueError, lambda: q / m)
def test_operators_quaternion(self):
q1 = Quaternion()
q2 = Quaternion.from_x_rotation(0.5)
# add
self.assertRaises(ValueError, lambda: q1 + q2)
# subtract
# we had to add this to enable np.array_equal to work
# as it uses subtraction
#self.assertRaises(ValueError, lambda: q1 - q2)
# multiply
self.assertTrue(np.array_equal(q1 * q2, quaternion.cross(quaternion.create(), quaternion.create_from_x_rotation(0.5))))
# divide
self.assertRaises(ValueError, lambda: q1 / q2)
# or
self.assertTrue(np.array_equal(q1 | q2, quaternion.dot(quaternion.create(), quaternion.create_from_x_rotation(0.5))))
# inverse
self.assertTrue(np.array_equal(~q2, quaternion.conjugate(quaternion.create_from_x_rotation(0.5))))
# ==
self.assertTrue(Quaternion() == Quaternion())
self.assertFalse(Quaternion() == Quaternion([0., 0., 0., 0.]))
# !=
self.assertTrue(Quaternion() != Quaternion([1., 1., 1., 1.]))
self.assertFalse(Quaternion() != Quaternion())
def test_operators_vector3(self):
q = Quaternion.from_x_rotation(0.5)
v = Vector3([1., 0., 0.])
# add
self.assertRaises(ValueError, lambda: q + v)
# subtract
self.assertRaises(ValueError, lambda: q - v)
# multiply
self.assertTrue(np.array_equal(q * v, quaternion.apply_to_vector(quaternion.create_from_x_rotation(0.5), [1., 0., 0.])))
# divide
self.assertRaises(ValueError, lambda: q / v)
def test_operators_vector4(self):
q = Quaternion.from_x_rotation(0.5)
v = Vector4([1., 0., 0., 1.])
# add
self.assertRaises(ValueError, lambda: q + v)
# subtract
self.assertRaises(ValueError, lambda: q - v)
# multiply
self.assertTrue(np.array_equal(q * v, quaternion.apply_to_vector(quaternion.create_from_x_rotation(0.5), [1., 0., 0., 1.])))
# divide
self.assertRaises(ValueError, lambda: q / v)
def test_apply_to_vector_non_unit(self):
q = Quaternion.from_x_rotation(np.pi)
# zero length
v = Vector3([0., 0., 0.])
self.assertTrue(np.allclose(q * v, quaternion.apply_to_vector(quaternion.create_from_x_rotation(np.pi), [0., 0., 0.])))
# >1 length
v = Vector3([2., 0., 0.])
self.assertTrue(np.allclose(q * v, quaternion.apply_to_vector(quaternion.create_from_x_rotation(np.pi), [2., 0., 0.])))
v = Vector3([0., 2., 0.])
self.assertTrue(np.allclose(q * v, quaternion.apply_to_vector(quaternion.create_from_x_rotation(np.pi), [0., 2., 0.])))
v = Vector3([0., 0., 2.])
self.assertTrue(np.allclose(q * v, quaternion.apply_to_vector(quaternion.create_from_x_rotation(np.pi), [0., 0., 2.])))
def test_accessors(self):
q = Quaternion(np.arange(self._size))
self.assertTrue(np.array_equal(q.xy, [0, 1]))
self.assertTrue(np.array_equal(q.xyz, [0, 1, 2]))
self.assertTrue(np.array_equal(q.xyzw, [0, 1, 2, 3]))
self.assertTrue(np.array_equal(q.xz, [0, 2]))
self.assertTrue(np.array_equal(q.xyz, [0, 1, 2]))
self.assertTrue(np.array_equal(q.xyw, [0, 1, 3]))
self.assertTrue(np.array_equal(q.xw, [0, 3]))
self.assertEqual(q.x, 0)
self.assertEqual(q.y, 1)
self.assertEqual(q.z, 2)
self.assertEqual(q.w, 3)
q.x = 1
self.assertEqual(q.x, 1)
self.assertEqual(q[0], 1)
q.x += 1
self.assertEqual(q.x, 2)
self.assertEqual(q[0], 2)
def test_equality(self):
q1 = Quaternion([0, 0, 0, 1])
q2 = Quaternion([0, 0, 0, 1])
q3 = Quaternion([0, 0, 1, -1])
self.assertEqual(q1, q2)
self.assertNotEqual(q1, q3)
self.assertNotEqual(q2, q3)
def test_equality_negative(self):
q1 = Quaternion([0, 0, 0, 1])
q2 = Quaternion([0, 0, 0, -1])
q3 = Quaternion([0, 0, 1, -1])
self.assertEqual(q1, q2)
self.assertNotEqual(q1, q3)
if __name__ == '__main__':
unittest.main()
|