Orientation estimation
axang2rotm(axang)
Convert axis-angle representation to rotation matrix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
axang
|
ndarray
|
Input array of axis-angle representations with shape (..., 4), where the first three elements are the axis of rotation and the last element is the angle of rotation in radians. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: Rotation matrix corresponding to the input axis-angle representations. |
The function computes the rotation matrix using Rodrigues' rotation formula.
Source code in kielmat/utils/quaternion.py
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 |
|
quat2axang(q)
Convert a quaternion to axis-angle representation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
q
|
ndarray
|
Input quaternion array of shape (..., 4). |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: Axis-angle representation array of shape (..., 4), where the first three elements are the axis of rotation and the last element is the angle of rotation in radians. |
The function normalizes the input quaternion, calculates the angle of rotation, and computes the axis of rotation in the axis-angle representation.
Note: The input quaternion array is expected to have the last dimension of size 4.
Source code in kielmat/utils/quaternion.py
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 |
|
quat2rotm(q, scalar_first=True, channels_last=True)
Convert quaternion(s) to rotation matrix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
q
|
ndarray
|
Input quaternion(s) as a NumPy array. The last dimension must have size 4. |
required |
scalar_first
|
bool
|
If True, the quaternion is assumed to be in scalar-first order (default is True). |
True
|
channels_last
|
bool
|
If True, the last dimension represents the quaternion channels (default is True). If False, the quaternion channels are assumed to be in the first dimension. |
True
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: Rotation matrix corresponding to the input quaternion(s). |
Raises:
Type | Description |
---|---|
AssertionError
|
If the last dimension of the input array |
Notes
The conversion is based on the formula: R = | 1 - 2q2^2 - 2q3^2 2(q1q2 - q3q0) 2(q1q3 + q2q0) | | 2(q1q2 + q3q0) 1 - 2q1^2 - 2q3^2 2(q2q3 - q1q0) | | 2(q1q3 - q2q0) 2(q1q0 + q2q3) 1 - 2q1^2 - 2q2^2 |
References
Wikipedia: https://en.wikipedia.org/wiki/Rotation_matrix#Quaternion
Examples:
>>> quaternion = np.array([1.0, 0.0, 0.0, 0.0])
>>> rotation_matrix = quat2rotm(quaternion)
Source code in kielmat/utils/quaternion.py
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
|
quatconj(q, scalar_first=True, channels_last=True)
Compute the quaternion conjugate.
This function calculates the conjugate of a quaternion, which is obtained by negating the imaginary (vector) parts while keeping the real (scalar) part unchanged.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
q
|
ndarray
|
Input array of quaternions with shape (..., 4). |
required |
scalar_first
|
bool
|
If True, assumes the scalar part is the first element. If False, assumes the scalar part is the last element. Default is True. |
True
|
channels_last
|
bool
|
If True, assumes the channels are the last dimension. If False, assumes the channels are the second-to-last dimension. Default is True. |
True
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: Quaternion conjugate with the same shape as the input array. |
Notes
- The input array is cast to float before the computation.
- If channels_last is False, the input array is transposed to switch channels and time axis.
- If scalar_first is False, the scalar part is moved to the last element.
Quaternion Conjugate Formula
q_conj = [w, -x, -y, -z]
Source code in kielmat/utils/quaternion.py
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 |
|
quatinv(q, scalar_first=True, channels_last=True)
Compute the inverse of quaternions.
This function calculates the inverse of quaternions by first computing the conjugate and then normalizing the conjugate.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
q
|
ndarray
|
Input array of quaternions with shape (..., 4). |
required |
scalar_first
|
bool
|
If True, assumes the scalar part is the first element. If False, assumes the scalar part is the last element. Default is True. |
True
|
channels_last
|
bool
|
If True, assumes the channels are the last dimension. If False, assumes the channels are the second-to-last dimension. Default is True. |
True
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: Inverse of quaternions with the same shape as the input array. |
Notes
- The input array is cast to float before the computation.
- If channels_last is False, the input array is transposed to switch channels and time axis.
Quaternion Inverse Calculation
The inverse of a quaternion q is obtained by first calculating its conjugate and then normalizing it: q_inv = normalize(conjugate(q))
Source code in kielmat/utils/quaternion.py
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 |
|
quatmultiply(q1, q2=None, scalar_first=True, channels_last=True)
Multiply two sets of quaternions.
This function performs quaternion multiplication on two sets of quaternions. Quaternions are 4-dimensional vectors of the form [w, x, y, z], where 'w' is the scalar (real) part, and 'x', 'y', and 'z' are the vector (imaginary) parts.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
q1
|
ndarray
|
Input array of quaternions with shape (..., 4). |
required |
q2
|
ndarray
|
Input array of quaternions with shape (..., 4). If None, q2 is set to q1, making it a self-multiplication. Default is None. |
None
|
scalar_first
|
bool
|
If True, assumes the scalar part is the first element. If False, assumes the scalar part is the last element. Default is True. |
True
|
channels_last
|
bool
|
If True, assumes the channels are the last dimension. If False, assumes the channels are the second-to-last dimension. Default is True. |
True
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: Result of quaternion multiplication with the same shape as the input arrays. |
Raises:
Type | Description |
---|---|
AssertionError
|
If the last dimension of q1 and q2 is not 4. |
Notes
- If q2 is None, this function performs self-multiplication (q1 * q1).
- The input arrays are cast to float before the computation.
- If channels_last is False, the input arrays are transposed to switch channels and time axis.
Quaternion Conjugate Formula
q3 = [w1w2 - x1x2 - y1y2 - z1z2, w1x2 + x1w2 + y1z2 - z1y2, w1y2 - x1z2 + y1w2 + z1x2, w1z2 + x1y2 - y1x2 + z1w2]
Source code in kielmat/utils/quaternion.py
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 |
|
quatnorm(q, channels_last=True)
Calculate the norm (magnitude) of quaternions.
This function computes the norm (magnitude) of quaternions along the specified axis, which represents the length of the quaternion vector.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
q
|
ndarray
|
Input array of quaternions with shape (..., 4). |
required |
channels_last
|
bool
|
If True, assumes the channels are the last dimension. If False, assumes the channels are the first dimension. Default is True. |
True
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: Norm of quaternions along the specified axis with the same shape as the input array. |
Notes
- The input array is cast to float before the computation.
- If channels_last is False, the input array is transposed to switch channels and time axis.
Quaternion Norm Calculation
The norm of a quaternion q is calculated as follows: norm(q) = sqrt(w^2 + x^2 + y^2 + z^2)
Source code in kielmat/utils/quaternion.py
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 |
|
quatnormalize(q, channels_last=True)
Normalize quaternions.
This function normalizes quaternions by dividing each quaternion by its magnitude (norm). The result is a unit quaternion with the same orientation as the original quaternion.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
q
|
ndarray
|
Input array of quaternions with shape (..., 4). |
required |
channels_last
|
bool
|
If True, assumes the channels are the last dimension. If False, assumes the channels are the second-to-last dimension. Default is True. |
True
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: Normalized quaternions with the same shape as the input array. |
Notes
- The input array is cast to float before the computation.
- If channels_last is False, the input array is transposed to switch channels and time axis.
Quaternion Normalization
The normalization of a quaternion q is performed by dividing each element of q by its norm: q_normalized = q / norm(q)
Source code in kielmat/utils/quaternion.py
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 |
|
rotm2quat(R, method='auto')
Convert a 3x3 rotation matrix to a quaternion.
Source: - https://github.com/dlaidig/qmt/blob/0fa8d32eb461e14d78e9ddbd569664ea59bcea19/qmt/functions/quaternion.py#L1004
Parameters:
Name | Type | Description | Default |
---|---|---|---|
R
|
ndarray
|
A rotation matrix with shape (3, 3). |
required |
scalar_first
|
bool
|
If True, sets the first element as the scalar part. If False, sets the last element as the scalar part is the last element. Default is True. |
required |
channels_last
|
bool
|
If True, assumes the channels are the last dimension. If False, assumes the channels are the first dimension. Default is True. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: The quaternion corresponding to the rotation matrix. |
Raises:
Type | Description |
---|---|
AssertionError
|
If the shape of R is not (3, 3). |
Notes
- If q2 is None, this function performs self-multiplication (q1 * q1).
- The input arrays are cast to float before the computation.
- If channels_last is False, the input arrays are transposed to switch channels and time axis.
Source code in kielmat/utils/quaternion.py
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 |
|