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 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 407 408 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 443 444 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 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
//! Quaternion library written in Rust.
//!
//! This provides Quaternion operations and interconversion with several attitude
//! representations as generic functions (supports `f32` & `f64`).
//!
//! ## Generics
//!
//! Functions implementing the `QuaternionOps` trait can take both `Quaternion<T>`
//! and `Vector3<T>` as arguments. In this case, `Vector3<T>` is treated as a Pure Quaternion.
//!
//! For example:
//! ```
//! use quaternion_core::{Vector3, Quaternion, add};
//!
//! // --- Vector3 --- //
//! let v1: Vector3<f32> = [1.0, 2.0, 3.0];
//! let v2: Vector3<f32> = [0.1, 0.2, 0.3];
//! println!("{:?}", add(v1, v2)); // <--- It's [1.1, 2.2, 3.3]
//!
//! // --- Quaternion --- //
//! let q1: Quaternion<f64> = (1.0, [2.0, 3.0, 4.0]);
//! let q2: Quaternion<f64> = (0.1, [0.2, 0.3, 0.4]);
//! println!("{:?}", add(q1, q2)); // <--- It's (1.1, [2.2, 3.3, 4.4])
//! ```
//!
//! ## Versor
//!
//! Versor refers to a Quaternion representing a rotation, the norm of which is 1.
//!
//! The documentation for this crate basically writes Versor instead of Unit Quaternion,
//! but the difference in usage is not clear.
//! Please think Versor = Unit Quaternion.
#![no_std]
#[cfg(feature = "std")]
extern crate std;
#[cfg(any(feature = "std", feature = "libm"))]
use num_traits::float::{Float, FloatConst};
mod euler;
mod generics;
mod private_functions;
pub use generics::QuaternionOps;
use private_functions::{
IDENTITY, ZERO_VECTOR, cast, mul_add, acos_safe,
sinc, max4, orthogonal_vector, pythag
};
/// Three dimensional vector (Pure Quaternion)
///
/// The type `[q1, q2, q3]` is equivalent to the expression `q1i + q2j + q3k`,
/// where `i`, `j`, `k` are basis of quaternions and satisfy the following equality:
///
/// `i^2 = j^2 = k^2 = ijk = -1`
pub type Vector3<T> = [T; 3];
/// Quaternion
///
/// The type `(q0, [q1, q2, q3])` is equivalent to the expression `q0 + q1i + q2j + q3k`,
/// where `1`, `i`, `j`, `k` are basis of quaternions and satisfy the following equality:
///
/// `i^2 = j^2 = k^2 = ijk = -1`
pub type Quaternion<T> = (T, Vector3<T>);
/// Direction Cosine Matrix
///
/// `mij`: row `i`, column `j`
///
/// `
/// [
/// [m11, m12, m13],
/// [m21, m22, m23],
/// [m31, m32, m33]
/// ]
/// `
pub type DCM<T> = [Vector3<T>; 3];
/// Specifies the rotation type of Euler angles.
///
/// Considering a fixed `Reference frame` and a rotating `Body frame`,
/// `Intrinsic rotation` and `Extrinsic rotation` represent the following rotations:
///
/// * `Intrinsic`: Rotate around the axes of the `Body frame`
/// * `Extrinsic`: Rotate around the axes of the `Reference frame`
#[derive(Debug, Clone, Copy)]
pub enum RotationType {
Intrinsic,
Extrinsic,
}
/// Specifies the rotation sequence of Euler angles.
///
/// Each variant reads from left to right.
/// For example, `RotationSequence::XYZ` represents first a rotation
/// around the X axis, then around the Y axis, and finally around
/// the Z axis (X ---> Y ---> Z).
#[derive(Debug, Clone, Copy)]
pub enum RotationSequence {
// Proper (z-x-z, x-y-x, y-z-y, z-y-z, x-z-x, y-x-y)
ZXZ,
XYX,
YZY,
ZYZ,
XZX,
YXY,
// Tait–Bryan (x-y-z, y-z-x, z-x-y, x-z-y, z-y-x, y-x-z)
XYZ,
YZX,
ZXY,
XZY,
ZYX,
YXZ,
}
/// Generate Versor by specifying rotation `angle`\[rad\] and `axis` vector.
///
/// The `axis` vector does not have to be a unit vector.
///
/// If you enter a zero vector, it returns an identity quaternion.
///
/// # Examples
///
/// ```
/// # use quaternion_core::{from_axis_angle, point_rotation};
/// # let PI = std::f64::consts::PI;
/// // Generates a quaternion representing the
/// // rotation of π/2[rad] around the y-axis.
/// let q = from_axis_angle([0.0, 1.0, 0.0], PI/2.0);
///
/// // Rotate the point.
/// let r = point_rotation(q, [2.0, 2.0, 0.0]);
///
/// assert!( (r[0] - 0.0).abs() < 1e-12 );
/// assert!( (r[1] - 2.0).abs() < 1e-12 );
/// assert!( (r[2] + 2.0).abs() < 1e-12 );
/// ```
#[inline]
pub fn from_axis_angle<T>(axis: Vector3<T>, angle: T) -> Quaternion<T>
where T: Float + FloatConst {
let theta = angle % ( T::PI() + T::PI() ); // limit to (-2π, 2π)
let (sin, cos) = ( theta * cast(0.5) ).sin_cos();
let coef = sin / norm(axis);
if coef.is_infinite() {
IDENTITY()
} else {
( cos, scale(coef, axis) )
}
}
/// Calculate the rotation `axis` (unit vector) and the rotation `angle`\[rad\]
/// around the `axis` from the Versor.
///
/// If identity quaternion is entered, `angle` returns zero and
/// the `axis` returns a zero vector.
///
/// Range of `angle`: `(-π, π]`
///
/// # Examples
///
/// ```
/// # use quaternion_core::{from_axis_angle, to_axis_angle};
/// # let PI = std::f64::consts::PI;
/// let axis_ori = [0.0, 1.0, 2.0];
/// let angle_ori = PI / 2.0;
/// let q = from_axis_angle(axis_ori, angle_ori);
///
/// let (axis, angle) = to_axis_angle(q);
///
/// assert!( (axis_ori[0] - axis[0]).abs() < 1e-12 );
/// assert!( (axis_ori[0] - axis[0]).abs() < 1e-12 );
/// assert!( (axis_ori[0] - axis[0]).abs() < 1e-12 );
/// assert!( (angle_ori - angle).abs() < 1e-12 );
/// ```
#[inline]
pub fn to_axis_angle<T>(q: Quaternion<T>) -> (Vector3<T>, T)
where T: Float {
let norm_v = norm(q.1);
let norm_inv = norm_v.recip();
if norm_inv.is_infinite() {
( ZERO_VECTOR(), T::zero() )
} else {
let mut angle = cast::<T>(2.0) * norm_v.min( T::one() ).asin();
if q.0 < T::zero() {
angle = -angle;
}
( scale(norm_inv, q.1), angle )
}
}
/// Convert a DCM to a Versor representing
/// the `q v q*` rotation (Point Rotation - Frame Fixed).
///
/// When convert to a DCM representing `q* v q` rotation
/// (Frame Rotation - Point Fixed) to a Versor, do the following:
///
/// ```
/// # use quaternion_core::{from_dcm, to_dcm, conj};
/// # let dcm = to_dcm((1.0, [0.0; 3]));
/// let q = conj( from_dcm(dcm) );
/// ```
///
/// # Examples
///
/// ```
/// # use quaternion_core::{
/// # from_axis_angle, dot, conj, negate, to_dcm, from_dcm,
/// # matrix_product, point_rotation, frame_rotation
/// # };
/// # let PI = std::f64::consts::PI;
/// // Make these as you like.
/// let v = [1.0, 0.5, -8.0];
/// let q = from_axis_angle([0.2, 1.0, -2.0], PI/4.0);
///
/// // --- Point rotation --- //
/// {
/// let m = to_dcm(q);
/// let q_check = from_dcm(m);
///
/// assert!( (q.0 - q_check.0).abs() < 1e-12 );
/// assert!( (q.1[0] - q_check.1[0]).abs() < 1e-12 );
/// assert!( (q.1[1] - q_check.1[1]).abs() < 1e-12 );
/// assert!( (q.1[2] - q_check.1[2]).abs() < 1e-12 );
/// }
///
/// // --- Frame rotation --- //
/// {
/// let m = to_dcm( conj(q) );
/// let q_check = conj( from_dcm(m) );
///
/// assert!( (q.0 - q_check.0).abs() < 1e-12 );
/// assert!( (q.1[0] - q_check.1[0]).abs() < 1e-12 );
/// assert!( (q.1[1] - q_check.1[1]).abs() < 1e-12 );
/// assert!( (q.1[2] - q_check.1[2]).abs() < 1e-12 );
/// }
/// ```
#[inline]
pub fn from_dcm<T>(m: DCM<T>) -> Quaternion<T>
where T: Float {
// ゼロ除算を避けるために,4通りの式で求めたうちの最大値を係数として使う.
let m22_p_m33 = m[1][1] + m[2][2];
let m22_m_m33 = m[1][1] - m[2][2];
let (index, max_num) = max4([
m[0][0] + m22_p_m33,
m[0][0] - m22_p_m33,
-m[0][0] + m22_m_m33,
-m[0][0] - m22_m_m33,
]);
let half: T = cast(0.5);
let tmp = ( max_num + T::one() ).sqrt();
let coef = half / tmp;
let (q0, [q1, q2, q3]): Quaternion<T>;
match index {
0 => {
q0 = half * tmp;
q1 = (m[2][1] - m[1][2]) * coef;
q2 = (m[0][2] - m[2][0]) * coef;
q3 = (m[1][0] - m[0][1]) * coef;
},
1 => {
q0 = (m[2][1] - m[1][2]) * coef;
q1 = half * tmp;
q2 = (m[0][1] + m[1][0]) * coef;
q3 = (m[0][2] + m[2][0]) * coef;
},
2 => {
q0 = (m[0][2] - m[2][0]) * coef;
q1 = (m[0][1] + m[1][0]) * coef;
q2 = half * tmp;
q3 = (m[1][2] + m[2][1]) * coef;
},
3 => {
q0 = (m[1][0] - m[0][1]) * coef;
q1 = (m[0][2] + m[2][0]) * coef;
q2 = (m[1][2] + m[2][1]) * coef;
q3 = half * tmp;
},
_ => unreachable!(),
};
(q0, [q1, q2, q3])
}
/// Convert a Versor to a DCM representing
/// the `q v q*` rotation (Point Rotation - Frame Fixed).
///
/// When convert to a DCM representing the
/// `q* v q` rotation (Frame Rotation - Point Fixed), do the following:
///
/// ```
/// # use quaternion_core::{to_dcm, conj};
/// # let q = (1.0, [0.0; 3]);
/// let dcm = to_dcm( conj(q) );
/// ```
///
/// # Examples
///
/// ```
/// # use quaternion_core::{
/// # from_axis_angle, to_dcm, conj,
/// # matrix_product, point_rotation, frame_rotation
/// # };
/// # let PI = std::f64::consts::PI;
/// // Make these as you like.
/// let v = [1.0, 0.5, -8.0];
/// let q = from_axis_angle([0.2, 1.0, -2.0], PI/4.0);
///
/// // --- Point rotation --- //
/// {
/// let m = to_dcm(q);
///
/// let rm = matrix_product(m, v);
/// let rq = point_rotation(q, v);
/// assert!( (rm[0] - rq[0]).abs() < 1e-12 );
/// assert!( (rm[1] - rq[1]).abs() < 1e-12 );
/// assert!( (rm[2] - rq[2]).abs() < 1e-12 );
/// }
///
/// // --- Frame rotation --- //
/// {
/// let m = to_dcm( conj(q) );
///
/// let rm = matrix_product(m, v);
/// let rq = frame_rotation(q, v);
/// assert!( (rm[0] - rq[0]).abs() < 1e-12 );
/// assert!( (rm[1] - rq[1]).abs() < 1e-12 );
/// assert!( (rm[2] - rq[2]).abs() < 1e-12 );
/// }
/// ```
#[inline]
pub fn to_dcm<T>(q: Quaternion<T>) -> DCM<T>
where T: Float {
let neg_one = -T::one();
let two = cast(2.0);
// Compute these value only once.
let (q0_q0, [q0_q1, q0_q2, q0_q3]) = scale(q.0, q);
let q1_q2 = q.1[0] * q.1[1];
let q1_q3 = q.1[0] * q.1[2];
let q2_q3 = q.1[1] * q.1[2];
let m11 = mul_add(mul_add(q.1[0], q.1[0], q0_q0), two, neg_one);
let m12 = (q1_q2 - q0_q3) * two;
let m13 = (q1_q3 + q0_q2) * two;
let m21 = (q1_q2 + q0_q3) * two;
let m22 = mul_add(mul_add(q.1[1], q.1[1], q0_q0), two, neg_one);
let m23 = (q2_q3 - q0_q1) * two;
let m31 = (q1_q3 - q0_q2) * two;
let m32 = (q2_q3 + q0_q1) * two;
let m33 = mul_add(mul_add(q.1[2], q.1[2], q0_q0), two, neg_one);
[
[m11, m12, m13],
[m21, m22, m23],
[m31, m32, m33],
]
}
/// Convert euler angles to versor.
///
/// The type of rotation (Intrinsic or Extrinsic) is specified by `RotationType` enum,
/// and the rotation sequence (XZX, XYZ, ...) is specified by `RotationSequence` enum.
///
/// Each element of `angles` should be specified in the range: `[-2π, 2π]`.
///
/// Sequences: `angles[0]` ---> `angles[1]` ---> `angles[2]`
///
/// # Examples
///
/// ```
/// # use quaternion_core::{from_axis_angle, mul, from_euler_angles, point_rotation};
/// # let PI = std::f64::consts::PI;
/// use quaternion_core::{RotationType::*, RotationSequence::XYZ};
///
/// let angles = [PI/6.0, 1.6*PI, -PI/4.0];
/// let v = [1.0, 0.5, -0.4];
///
/// // Quaternions representing rotation around each axis.
/// let x = from_axis_angle([1.0, 0.0, 0.0], angles[0]);
/// let y = from_axis_angle([0.0, 1.0, 0.0], angles[1]);
/// let z = from_axis_angle([0.0, 0.0, 1.0], angles[2]);
///
/// // ---- Intrinsic (X-Y-Z) ---- //
/// // These represent the same rotation.
/// let q_in = mul( mul(x, y), z );
/// let e2q_in = from_euler_angles(Intrinsic, XYZ, angles);
/// // Confirmation
/// let a_in = point_rotation(q_in, v);
/// let b_in = point_rotation(e2q_in, v);
/// assert!( (a_in[0] - b_in[0]).abs() < 1e-12 );
/// assert!( (a_in[1] - b_in[1]).abs() < 1e-12 );
/// assert!( (a_in[2] - b_in[2]).abs() < 1e-12 );
///
/// // ---- Extrinsic (X-Y-Z) ---- //
/// // These represent the same rotation.
/// let q_ex = mul( mul(z, y), x );
/// let e2q_ex = from_euler_angles(Extrinsic, XYZ, angles);
/// // Confirmation
/// let a_ex = point_rotation(q_ex, v);
/// let b_ex = point_rotation(e2q_ex, v);
/// assert!( (a_ex[0] - b_ex[0]).abs() < 1e-12 );
/// assert!( (a_ex[1] - b_ex[1]).abs() < 1e-12 );
/// assert!( (a_ex[2] - b_ex[2]).abs() < 1e-12 );
/// ```
#[inline]
pub fn from_euler_angles<T>(rt: RotationType, rs: RotationSequence, angles: Vector3<T>) -> Quaternion<T>
where T: Float + FloatConst {
debug_assert!( angles[0].abs() <= T::PI() + T::PI(), "angles[0] is out of range!");
debug_assert!( angles[1].abs() <= T::PI() + T::PI(), "angles[1] is out of range!");
debug_assert!( angles[2].abs() <= T::PI() + T::PI(), "angles[2] is out of range!");
match rt {
RotationType::Intrinsic => euler::from_intrinsic_euler_angles(rs, angles),
RotationType::Extrinsic => euler::from_extrinsic_euler_angles(rs, angles),
}
}
/// Convert versor to euler angles.
///
/// The type of rotation (Intrinsic or Extrinsic) is specified by `RotationType` enum,
/// and the rotation sequence (XZX, XYZ, ...) is specified by `RotationSequence` enum.
///
/// ```
/// # use quaternion_core::{RotationType::Intrinsic, RotationSequence::XYZ, to_euler_angles};
/// # let q = (1.0, [0.0; 3]);
/// let angles = to_euler_angles(Intrinsic, XYZ, q);
/// ```
///
/// Sequences: `angles[0]` ---> `angles[1]` ---> `angles[2]`
///
/// # Singularity
///
/// ## RotationType::Intrinsic
///
/// For Proper Euler angles (ZXZ, XYX, YZY, ZYZ, XZX, YXY), the singularity is reached
/// when the sine of the second rotation angle is 0 (angle = 0, ±π, ...), and for
/// Tait-Bryan angles (XYZ, YZX, ZXY, XZY, ZYX, YXZ), the singularity is reached when
/// the cosine of the second rotation angle is 0 (angle = ±π/2).
///
/// At the singularity, the third rotation angle is set to 0\[rad\].
///
/// ## RotationType::Extrinsic
///
/// As in the case of Intrinsic rotation, for Proper Euler angles, the singularity occurs
/// when the sine of the second rotation angle is 0 (angle = 0, ±π, ...), and for
/// Tait-Bryan angles, the singularity occurs when the cosine of the second rotation angle
/// is 0 (angle = ±π/2).
///
/// At the singularity, the first rotation angle is set to 0\[rad\].
///
/// # Examples
///
/// Depending on the rotation angle of each axis, it may not be possible to recover the
/// same rotation angle as the original. However, they represent the same rotation in 3D space.
///
/// ```
/// # use quaternion_core::{from_euler_angles, to_euler_angles, point_rotation};
/// # let PI = std::f64::consts::PI;
/// use quaternion_core::{RotationType::*, RotationSequence::XYZ};
///
/// let angles = [PI/6.0, PI/4.0, PI/3.0];
///
/// // ---- Intrinsic (X-Y-Z) ---- //
/// let q_in = from_euler_angles(Intrinsic, XYZ, angles);
/// let e_in = to_euler_angles(Intrinsic, XYZ, q_in);
/// assert!( (angles[0] - e_in[0]).abs() < 1e-12 );
/// assert!( (angles[1] - e_in[1]).abs() < 1e-12 );
/// assert!( (angles[2] - e_in[2]).abs() < 1e-12 );
///
/// // ---- Extrinsic (X-Y-Z) ---- //
/// let q_ex = from_euler_angles(Extrinsic, XYZ, angles);
/// let e_ex = to_euler_angles(Extrinsic, XYZ, q_ex);
/// assert!( (angles[0] - e_ex[0]).abs() < 1e-12 );
/// assert!( (angles[1] - e_ex[1]).abs() < 1e-12 );
/// assert!( (angles[2] - e_ex[2]).abs() < 1e-12 );
/// ```
#[inline]
pub fn to_euler_angles<T>(rt: RotationType, rs: RotationSequence, q: Quaternion<T>) -> Vector3<T>
where T: Float + FloatConst {
match rt {
RotationType::Intrinsic => euler::to_intrinsic_euler_angles(rs, q),
RotationType::Extrinsic => euler::to_extrinsic_euler_angles(rs, q),
}
}
/// Convert Rotation vector to Versor.
///
/// The Rotation vector itself represents the axis of rotation,
/// and the norm represents the angle \[rad\] of rotation around the axis.
///
/// Maximum range of the norm of the rotation vector: `[0, 2π]`
///
/// # Examples
///
/// ```
/// # use quaternion_core::{from_rotation_vector, scale, point_rotation};
/// # let PI = std::f64::consts::PI;
/// let angle = PI / 2.0;
/// let axis = [1.0, 0.0, 0.0];
///
/// // This represents a rotation of π/2 around the x-axis.
/// let rot_vec = scale(angle, axis); // Rotation vector
///
/// // Rotation vector ---> Quaternion
/// let q = from_rotation_vector(rot_vec);
///
/// let r = point_rotation(q, [1.0, 1.0, 0.0]);
///
/// assert!( (r[0] - 1.0).abs() < 1e-12 );
/// assert!( (r[1] - 0.0).abs() < 1e-12 );
/// assert!( (r[2] - 1.0).abs() < 1e-12 );
/// ```
#[inline]
pub fn from_rotation_vector<T>(r: Vector3<T>) -> Quaternion<T>
where T: Float {
let half: T = cast(0.5);
let half_theta = half * norm(r);
(half_theta.cos(), scale(half * sinc(half_theta), r))
}
/// Convert Versor to Rotation vector.
///
/// The Rotation vector itself represents the axis of rotation,
/// and the norm represents the angle of rotation around the axis.
///
/// Norm range of the calculation result: `[0, π]`
///
/// # Examples
///
/// ```
/// # use quaternion_core::{from_axis_angle, to_rotation_vector, scale};
/// # let PI = std::f64::consts::PI;
/// let angle = PI / 2.0;
/// let axis = [1.0, 0.0, 0.0];
///
/// // These represent the same rotation.
/// let rv = scale(angle, axis); // Rotation vector
/// let q = from_axis_angle(axis, angle); // Quaternion
///
/// // Quaternion ---> Rotation vector
/// let q2rv = to_rotation_vector(q);
///
/// assert!( (rv[0] - q2rv[0]).abs() < 1e-12 );
/// assert!( (rv[1] - q2rv[1]).abs() < 1e-12 );
/// assert!( (rv[2] - q2rv[2]).abs() < 1e-12 );
/// ```
#[inline]
pub fn to_rotation_vector<T>(q: Quaternion<T>) -> Vector3<T>
where T: Float {
let mut half_theta = norm(q.1).min( T::one() ).asin();
if q.0 < T::zero() {
half_theta = -half_theta;
}
scale(cast::<T>(2.0) / sinc(half_theta), q.1)
}
/// Product of DCM and Vector3
///
/// # Examples
///
/// ```
/// # use quaternion_core::{Vector3, Quaternion, matrix_product};
/// # let PI = std::f64::consts::PI;
/// let theta = PI / 2.0;
/// let rot_x = [
/// [1.0, 0.0, 0.0],
/// [0.0, theta.cos(), -theta.sin()],
/// [0.0, theta.sin(), theta.cos()]
/// ];
/// let v = [0.0, 1.0, 0.0];
///
/// let r = matrix_product(rot_x, v);
/// assert!( (r[0] - 0.0).abs() < 1e-12 );
/// assert!( (r[1] - 0.0).abs() < 1e-12 );
/// assert!( (r[2] - 1.0).abs() < 1e-12 );
/// ```
#[inline]
pub fn matrix_product<T>(m: DCM<T>, v: Vector3<T>) -> Vector3<T>
where T: Float {
let mut r = [T::zero(); 3];
for i in 0..3 {
for j in 0..3 {
r[i] = mul_add(m[i][j], v[j], r[i]);
}
}
r
}
/// Calculate the sum of each element of Quaternion or Vector3.
///
/// # Examples
///
/// ```
/// # use quaternion_core::{Vector3, Quaternion, sum};
/// // --- Vector3 --- //
/// let v: Vector3<f64> = [1.0, 2.0, 3.0];
///
/// assert!( (6.0 - sum(v)).abs() < 1e-12 );
///
/// // --- Quaternion --- //
/// let q: Quaternion<f64> = (1.0, [2.0, 3.0, 4.0]);
///
/// assert!( (10.0 - sum(q)).abs() < 1e-12 );
/// ```
#[inline]
pub fn sum<T, U>(a: U) -> T
where T: Float, U: QuaternionOps<T> {
a.sum()
}
/// `a + b`
///
/// # Examples
///
/// ```
/// # use quaternion_core::{Vector3, Quaternion, add};
/// // --- Vector3 --- //
/// let v1: Vector3<f64> = [1.0, 2.0, 3.0];
/// let v2: Vector3<f64> = [0.1, 0.2, 0.3];
/// let v_result = add(v1, v2);
///
/// assert!( (1.1 - v_result[0]).abs() < 1e-12 );
/// assert!( (2.2 - v_result[1]).abs() < 1e-12 );
/// assert!( (3.3 - v_result[2]).abs() < 1e-12 );
///
/// // --- Quaternion --- //
/// let q1: Quaternion<f64> = (1.0, [2.0, 3.0, 4.0]);
/// let q2: Quaternion<f64> = (0.1, [0.2, 0.3, 0.4]);
/// let q_result = add(q1, q2);
///
/// assert!( (1.1 - q_result.0).abs() < 1e-12 );
/// assert!( (2.2 - q_result.1[0]).abs() < 1e-12 );
/// assert!( (3.3 - q_result.1[1]).abs() < 1e-12 );
/// assert!( (4.4 - q_result.1[2]).abs() < 1e-12 );
/// ```
#[inline]
pub fn add<T, U>(a: U, b: U) -> U
where T: Float, U: QuaternionOps<T> {
a.add(b)
}
/// `a - b`
///
/// # Examples
///
/// ```
/// # use quaternion_core::{Vector3, Quaternion, sub};
/// // --- Vector3 --- //
/// let v1: Vector3<f64> = [1.0, 2.0, 3.0];
/// let v2: Vector3<f64> = [0.1, 0.2, 0.3];
/// let v_result = sub(v1, v2);
///
/// assert!( (0.9 - v_result[0]).abs() < 1e-12 );
/// assert!( (1.8 - v_result[1]).abs() < 1e-12 );
/// assert!( (2.7 - v_result[2]).abs() < 1e-12 );
///
/// // --- Quaternion --- //
/// let q1: Quaternion<f64> = (1.0, [2.0, 3.0, 4.0]);
/// let q2: Quaternion<f64> = (0.1, [0.2, 0.3, 0.4]);
/// let q_result = sub(q1, q2);
///
/// assert!( (0.9 - q_result.0).abs() < 1e-12 );
/// assert!( (1.8 - q_result.1[0]).abs() < 1e-12 );
/// assert!( (2.7 - q_result.1[1]).abs() < 1e-12 );
/// assert!( (3.6 - q_result.1[2]).abs() < 1e-12 );
/// ```
#[inline]
pub fn sub<T, U>(a: U, b: U) -> U
where T: Float, U: QuaternionOps<T> {
a.sub(b)
}
/// `s * a`
///
/// # Examples
///
/// ```
/// # use quaternion_core::{Vector3, Quaternion, scale};
/// // --- Vector3 --- //
/// let v: Vector3<f64> = [1.0, 2.0, 3.0];
/// let v_result = scale(2.0, v);
///
/// assert!( (2.0 - v_result[0]).abs() < 1e-12 );
/// assert!( (4.0 - v_result[1]).abs() < 1e-12 );
/// assert!( (6.0 - v_result[2]).abs() < 1e-12 );
///
/// // --- Quaternion --- //
/// let q: Quaternion<f64> = (1.0, [2.0, 3.0, 4.0]);
/// let q_result = scale(2.0, q);
///
/// assert!( (2.0 - q_result.0).abs() < 1e-12 );
/// assert!( (4.0 - q_result.1[0]).abs() < 1e-12 );
/// assert!( (6.0 - q_result.1[1]).abs() < 1e-12 );
/// assert!( (8.0 - q_result.1[2]).abs() < 1e-12 );
/// ```
#[inline]
pub fn scale<T, U>(s: T, a: U) -> U
where T: Float, U: QuaternionOps<T> {
a.scale(s)
}
/// `s * a + b`
///
/// If the `fma` feature is enabled, the FMA calculation is performed using
/// the `mul_add` method (`s.mul_add(a, b)`).
/// If not enabled, it's computed by unfused multiply-add (`s * a + b`).
///
/// # Examples
///
/// ```
/// # use quaternion_core::{Vector3, Quaternion, scale_add};
/// // --- Vector3 --- //
/// let v1: Vector3<f64> = [1.0, 2.0, 3.0];
/// let v2: Vector3<f64> = [0.1, 0.2, 0.3];
/// let v_result = scale_add(2.0, v1, v2);
///
/// assert!( (2.1 - v_result[0]).abs() < 1e-12 );
/// assert!( (4.2 - v_result[1]).abs() < 1e-12 );
/// assert!( (6.3 - v_result[2]).abs() < 1e-12 );
///
/// // --- Quaternion --- //
/// let q1: Quaternion<f64> = (1.0, [2.0, 3.0, 4.0]);
/// let q2: Quaternion<f64> = (0.1, [0.2, 0.3, 0.4]);
/// let q_result = scale_add(2.0, q1, q2);
///
/// assert!( (2.1 - q_result.0).abs() < 1e-12 );
/// assert!( (4.2 - q_result.1[0]).abs() < 1e-12 );
/// assert!( (6.3 - q_result.1[1]).abs() < 1e-12 );
/// assert!( (8.4 - q_result.1[2]).abs() < 1e-12 );
/// ```
#[inline]
pub fn scale_add<T, U>(s: T, a: U, b: U) -> U
where T: Float, U: QuaternionOps<T> {
a.scale_add(s, b)
}
/// `a ∘ b`
///
/// Hadamard product of Vector3 or Quaternion.
///
/// # Examples
///
/// ```
/// # use quaternion_core::{Vector3, Quaternion, hadamard};
/// // --- Vector3 --- //
/// let v1: Vector3<f64> = [1.0, 2.0, 3.0];
/// let v2: Vector3<f64> = [0.1, 0.2, 0.3];
/// let v_result = hadamard(v1, v2);
///
/// assert!( (0.1 - v_result[0]).abs() < 1e-12 );
/// assert!( (0.4 - v_result[1]).abs() < 1e-12 );
/// assert!( (0.9 - v_result[2]).abs() < 1e-12 );
///
/// // --- Quaternion --- //
/// let q1: Quaternion<f64> = (1.0, [2.0, 3.0, 4.0]);
/// let q2: Quaternion<f64> = (0.1, [0.2, 0.3, 0.4]);
/// let q_result = hadamard(q1, q2);
///
/// assert!( (0.1 - q_result.0).abs() < 1e-12 );
/// assert!( (0.4 - q_result.1[0]).abs() < 1e-12 );
/// assert!( (0.9 - q_result.1[1]).abs() < 1e-12 );
/// assert!( (1.6 - q_result.1[2]).abs() < 1e-12 );
/// ```
#[inline]
pub fn hadamard<T, U>(a: U, b: U) -> U
where T: Float, U: QuaternionOps<T> {
a.hadamard(b)
}
/// `a ∘ b + c`
///
/// Hadamard product and addiction of Quaternion or Vector3.
///
/// If the `fma` feature is enabled, the FMA calculation is performed using
/// the `mul_add` method (`s.mul_add(a, b)`).
/// If not enabled, it's computed by unfused multiply-add (`s * a + b`).
///
/// # Examples
///
/// ```
/// # use quaternion_core::{Vector3, Quaternion, hadamard_add};
/// // --- Vector3 --- //
/// let v1: Vector3<f64> = [1.0, 2.0, 3.0];
/// let v2: Vector3<f64> = [0.1, 0.2, 0.3];
/// let v3: Vector3<f64> = [0.4, 0.5, 0.6];
/// let v_result = hadamard_add(v1, v2, v3);
///
/// assert!( (0.5 - v_result[0]).abs() < 1e-12 );
/// assert!( (0.9 - v_result[1]).abs() < 1e-12 );
/// assert!( (1.5 - v_result[2]).abs() < 1e-12 );
///
/// // --- Quaternion --- //
/// let q1: Quaternion<f64> = (1.0, [2.0, 3.0, 4.0]);
/// let q2: Quaternion<f64> = (0.1, [0.2, 0.3, 0.4]);
/// let q3: Quaternion<f64> = (0.5, [0.6, 0.7, 0.8]);
/// let q_result = hadamard_add(q1, q2, q3);
///
/// assert!( (0.6 - q_result.0).abs() < 1e-12 );
/// assert!( (1.0 - q_result.1[0]).abs() < 1e-12 );
/// assert!( (1.6 - q_result.1[1]).abs() < 1e-12 );
/// assert!( (2.4 - q_result.1[2]).abs() < 1e-12 );
/// ```
#[inline]
pub fn hadamard_add<T, U>(a: U, b: U, c: U) -> U
where T: Float, U: QuaternionOps<T> {
a.hadamard_add(b, c)
}
/// `a · b`
///
/// Dot product of Vector3 or Quaternion.
///
/// # Examples
///
/// ```
/// # use quaternion_core::{Vector3, Quaternion, dot};
/// // --- Vector3 --- //
/// let v1: Vector3<f64> = [1.0, 2.0, 3.0];
/// let v2: Vector3<f64> = [0.1, 0.2, 0.3];
///
/// assert!( (1.4 - dot(v1, v2)).abs() < 1e-12 );
///
/// // --- Quaternion --- //
/// let q1: Quaternion<f64> = (1.0, [2.0, 3.0, 4.0]);
/// let q2: Quaternion<f64> = (0.1, [0.2, 0.3, 0.4]);
///
/// assert!( (3.0 - dot(q1, q2)).abs() < 1e-12 );
/// ```
#[inline]
pub fn dot<T, U>(a: U, b: U) -> T
where T: Float, U: QuaternionOps<T> {
sum( hadamard(a, b) )
}
/// Cross product (vector product): `a × b`
///
/// The product order is `a × b (!= b × a)`
///
/// # Examples
///
/// ```
/// # use quaternion_core::{Vector3, scale, cross};
/// let v1: Vector3<f64> = [0.5, -1.0, 0.8];
/// let v2: Vector3<f64> = scale(2.0, v1);
/// let v_result = cross(v1, v2);
///
/// // The cross product of parallel vectors is a zero vector.
/// assert!( v_result[0].abs() < 1e-12 );
/// assert!( v_result[1].abs() < 1e-12 );
/// assert!( v_result[2].abs() < 1e-12 );
/// ```
#[inline]
pub fn cross<T>(a: Vector3<T>, b: Vector3<T>) -> Vector3<T>
where T: Float {
[
a[1]*b[2] - a[2]*b[1],
a[2]*b[0] - a[0]*b[2],
a[0]*b[1] - a[1]*b[0],
]
}
/// Calculate L2 norm of Vector3 or Quaternion.
///
/// Compared to `dot(a, a).sqrt()`, this function is less likely
/// to cause overflow and underflow.
///
/// When the `norm-sqrt` feature is enabled, the default
/// implementation is compiled with `dot(a, a).sqrt()` instead.
///
/// # Examples
///
/// ```
/// # use quaternion_core::{Vector3, Quaternion, sum, dot, norm};
/// // --- Vector3 --- //
/// let v: Vector3<f64> = [1.0, 2.0, 3.0];
/// assert!( (14.0_f64.sqrt() - norm(v)).abs() < 1e-12 );
///
/// // --- Quaternion --- //
/// let q: Quaternion<f64> = (1.0, [2.0, 3.0, 4.0]);
/// assert!( (30.0_f64.sqrt() - norm(q)).abs() < 1e-12 );
///
/// // --- Check about overflow --- //
/// let v: Vector3<f32> = [1e15, 2e20, -3e15];
/// assert_eq!( dot(v, v).sqrt(), f32::INFINITY ); // Oh...
///
/// #[cfg(not(feature = "norm-sqrt"))]
/// assert_eq!( norm(v), 2e20 ); // Excellent!
/// ```
#[inline]
pub fn norm<T, U>(a: U) -> T
where T: Float, U: QuaternionOps<T> {
a.norm()
}
/// Normalization of Vector3 or Quaternion.
///
/// # Examples
///
/// ```
/// # use quaternion_core::{Vector3, Quaternion, norm, normalize};
/// // --- Vector3 --- //
/// // This norm is not 1.
/// let v: Vector3<f64> = [1.0, 2.0, 3.0];
/// assert!( (1.0 - norm(v)).abs() > 1e-12 );
///
/// // Now that normalized, this norm is 1!
/// let v_n = normalize(v);
/// assert!( (1.0 - norm(v_n)).abs() < 1e-12 );
///
/// // --- Quaternion --- //
/// // This norm is not 1.
/// let q: Quaternion<f64> = (1.0, [2.0, 3.0, 4.0]);
/// assert!( (1.0 - norm(q)).abs() > 1e-12 );
///
/// // Now that normalized, this norm is 1!
/// let q_n = normalize(q);
/// assert!( (1.0 - norm(q_n)).abs() < 1e-12 );
/// ```
#[inline]
pub fn normalize<T, U>(a: U) -> U
where T: Float, U: QuaternionOps<T> {
scale(norm(a).recip(), a)
}
/// Invert the sign of a Vector3 or Quaternion.
///
/// # Examples
///
/// ```
/// # use quaternion_core::{Vector3, Quaternion, negate};
/// // --- Vector3 --- //
/// let v: Vector3<f64> = [1.0, 2.0, 3.0];
/// let v_n = negate(v);
///
/// assert_eq!(-v[0], v_n[0]);
/// assert_eq!(-v[1], v_n[1]);
/// assert_eq!(-v[2], v_n[2]);
///
/// // --- Quaternion --- //
/// let q: Quaternion<f64> = (1.0, [2.0, 3.0, 4.0]);
/// let q_n = negate(q);
///
/// assert_eq!(-q.0, q_n.0);
/// assert_eq!(-q.1[0], q_n.1[0]);
/// assert_eq!(-q.1[1], q_n.1[1]);
/// assert_eq!(-q.1[2], q_n.1[2]);
/// ```
#[inline]
pub fn negate<T, U>(a: U) -> U
where T: Float, U: QuaternionOps<T> {
a.negate()
}
/// Hamilton product (Product of Quaternion or Pure Quaternion)
///
/// The product order is `ab (!= ba)`
///
/// # Examples
///
/// ```
/// # use quaternion_core::{Vector3, Quaternion, inv, mul};
/// // ---- Pure Quaternion (Vector3) ---- //
/// let v: Vector3<f64> = [1.0, 2.0, 3.0];
///
/// // Identity quaternion
/// let id = mul( v, inv(v) ); // = mul( inv(v), v );
///
/// assert!( (1.0 - id.0).abs() < 1e-12 );
/// assert!( id.1[0].abs() < 1e-12 );
/// assert!( id.1[1].abs() < 1e-12 );
/// assert!( id.1[2].abs() < 1e-12 );
///
/// // ---- Quaternion ---- //
/// let q: Quaternion<f64> = (1.0, [2.0, 3.0, 4.0]);
///
/// // Identity quaternion
/// let id = mul( q, inv(q) ); // = mul( inv(q), q );
///
/// assert!( (1.0 - id.0).abs() < 1e-12 );
/// assert!( id.1[0].abs() < 1e-12 );
/// assert!( id.1[1].abs() < 1e-12 );
/// assert!( id.1[2].abs() < 1e-12 );
/// ```
#[inline]
pub fn mul<T, U>(a: U, b: U) -> Quaternion<T>
where T: Float, U: QuaternionOps<T> {
a.mul(b)
}
/// Calculate the conjugate of Quaternion.
///
/// # Examples
///
/// ```
/// # use quaternion_core::{Quaternion, conj};
/// let q: Quaternion<f64> = (1.0, [2.0, 3.0, 4.0]);
/// let q_conj = conj(q);
///
/// assert_eq!( q.0, q_conj.0);
/// assert_eq!(-q.1[0], q_conj.1[0]);
/// assert_eq!(-q.1[1], q_conj.1[1]);
/// assert_eq!(-q.1[2], q_conj.1[2]);
/// ```
#[inline]
pub fn conj<T>(q: Quaternion<T>) -> Quaternion<T>
where T: Float {
( q.0, negate(q.1) )
}
/// Calculate the inverse of Quaternion or Pure Quaternion (Vector3).
///
/// # Examples
///
/// ```
/// # use quaternion_core::{Vector3, Quaternion, inv, mul};
/// // ---- Pure Quaternion (Vector3) ---- //
/// let v: Vector3<f64> = [1.0, 2.0, 3.0];
///
/// // Identity quaternion
/// let id = mul( v, inv(v) ); // = mul( inv(v), v );
///
/// assert!( (id.0 - 1.0).abs() < 1e-12 );
/// assert!( id.1[0].abs() < 1e-12 );
/// assert!( id.1[1].abs() < 1e-12 );
/// assert!( id.1[2].abs() < 1e-12 );
///
/// // ---- Quaternion ---- //
/// let q: Quaternion<f64> = (1.0, [2.0, 3.0, 4.0]);
///
/// // Identity quaternion
/// let id = mul( q, inv(q) ); // = mul( inv(q), q );
///
/// assert!( (id.0 - 1.0).abs() < 1e-12 );
/// assert!( id.1[0].abs() < 1e-12 );
/// assert!( id.1[1].abs() < 1e-12 );
/// assert!( id.1[2].abs() < 1e-12 );
/// ```
#[inline]
pub fn inv<T, U>(a: U) -> U
where T: Float, U: QuaternionOps<T> {
a.inv()
}
// acosは[-π/2, π/2]の範囲でしか値を返さないので、qのとり方によってはlnで完全に復元できない。
// q == ln(exp(q)) が成り立つのはcos(norm(q.1))が[-π/2, π/2]の範囲内にある場合のみ。
/// Exponential function of Quaternion or Pure Quaternion (Vector3).
///
/// # Examples
///
/// ```
/// # use quaternion_core::{Vector3, Quaternion, scale, exp, ln};
/// // ---- Pure Quaternion (Vector3) ---- //
/// let v: Vector3<f64> = [0.1, 0.2, 0.3];
/// let v_r = ln( exp(v) );
///
/// assert!( v_r.0.abs() < 1e-12 );
/// assert!( (v[0] - v_r.1[0]).abs() < 1e-12 );
/// assert!( (v[1] - v_r.1[1]).abs() < 1e-12 );
/// assert!( (v[2] - v_r.1[2]).abs() < 1e-12 );
///
/// // ---- Quaternion ---- //
/// let q: Quaternion<f64> = (0.1, [0.2, 0.3, 0.4]);
/// let q_r = ln( exp(q) );
///
/// assert!( (q.0 - q_r.0).abs() < 1e-12 );
/// assert!( (q.1[0] - q_r.1[0]).abs() < 1e-12 );
/// assert!( (q.1[1] - q_r.1[1]).abs() < 1e-12 );
/// assert!( (q.1[2] - q_r.1[2]).abs() < 1e-12 );
///
/// // The relationship between exp(q) and exp(q.1)
/// let exp_q = exp(q);
/// let exp_check = scale( q.0.exp(), exp(q.1) );
/// assert!( (exp_q.0 - exp_check.0).abs() < 1e-12 );
/// assert!( (exp_q.1[0] - exp_check.1[0]).abs() < 1e-12 );
/// assert!( (exp_q.1[1] - exp_check.1[1]).abs() < 1e-12 );
/// assert!( (exp_q.1[2] - exp_check.1[2]).abs() < 1e-12 );
/// ```
#[inline]
pub fn exp<T, U>(a: U) -> Quaternion<T>
where T: Float, U: QuaternionOps<T> {
a.exp()
}
// acosは[-π/2, π/2]の範囲でしか値を返さないので、qのとり方によってはlnで完全に復元できない。
// q == ln(exp(q)) が成り立つのはcos(norm(q.1))が[-π/2, π/2]の範囲内にある場合のみ。
/// Natural logarithm of Quaternion.
///
/// # Examples
///
/// ```
/// # use quaternion_core::{Vector3, Quaternion, exp, ln};
/// let q: Quaternion<f64> = (0.1, [0.2, 0.3, 0.4]);
/// let q_r = ln( exp(q) );
///
/// assert!( (q.0 - q_r.0).abs() < 1e-12 );
/// assert!( (q.1[0] - q_r.1[0]).abs() < 1e-12 );
/// assert!( (q.1[1] - q_r.1[1]).abs() < 1e-12 );
/// assert!( (q.1[2] - q_r.1[2]).abs() < 1e-12 );
/// ```
#[inline]
pub fn ln<T>(q: Quaternion<T>) -> Quaternion<T>
where T: Float {
let norm_v = norm(q.1);
let norm_q = pythag(q.0, norm_v);
let coef = (q.0 / norm_q).acos() / norm_v;
( norm_q.ln(), scale(coef, q.1) )
}
// exp(q)の結果がVersorとなる条件は,qのスカラー部が0(つまりqが純虚四元数).
//
/// Natural logarithm of Versor.
///
/// If the argument `q` is guaranteed to be a Versor,
/// it is less calculation cost than the `ln(...)` function.
///
/// Only the vector part is returned since the real part is always zero.
///
/// # Examples
///
/// ```
/// # use quaternion_core::{Vector3, exp, ln_versor};
/// let v: Vector3<f64> = [0.1, 0.2, 0.3];
/// let r = ln_versor( exp(v) );
///
/// assert!( (v[0] - r[0]).abs() < 1e-12 );
/// assert!( (v[1] - r[1]).abs() < 1e-12 );
/// assert!( (v[2] - r[2]).abs() < 1e-12 );
/// ```
#[inline]
pub fn ln_versor<T>(q: Quaternion<T>) -> Vector3<T>
where T: Float {
scale( q.0.acos() / norm(q.1), q.1)
}
/// Power function of Quaternion.
///
/// # Examples
///
/// ```
/// # use quaternion_core::{Quaternion, mul, inv, pow, sqrt};
/// let q: Quaternion<f64> = (1.0, [2.0, 3.0, 4.0]);
///
/// let q_q = mul(q, q);
/// let q_pow_2 = pow(q, 2.0);
/// assert!( (q_q.0 - q_pow_2.0).abs() < 1e-12 );
/// assert!( (q_q.1[0] - q_pow_2.1[0]).abs() < 1e-12 );
/// assert!( (q_q.1[1] - q_pow_2.1[1]).abs() < 1e-12 );
/// assert!( (q_q.1[2] - q_pow_2.1[2]).abs() < 1e-12 );
///
/// let q_sqrt = sqrt(q);
/// let q_pow_0p5 = pow(q, 0.5);
/// assert!( (q_sqrt.0 - q_pow_0p5.0).abs() < 1e-12 );
/// assert!( (q_sqrt.1[0] - q_pow_0p5.1[0]).abs() < 1e-12 );
/// assert!( (q_sqrt.1[1] - q_pow_0p5.1[1]).abs() < 1e-12 );
/// assert!( (q_sqrt.1[2] - q_pow_0p5.1[2]).abs() < 1e-12 );
///
/// let q_inv = inv(q);
/// let q_pow_m1 = pow(q, -1.0);
/// assert!( (q_inv.0 - q_pow_m1.0).abs() < 1e-12 );
/// assert!( (q_inv.1[0] - q_pow_m1.1[0]).abs() < 1e-12 );
/// assert!( (q_inv.1[1] - q_pow_m1.1[1]).abs() < 1e-12 );
/// assert!( (q_inv.1[2] - q_pow_m1.1[2]).abs() < 1e-12 );
/// ```
#[inline]
pub fn pow<T>(q: Quaternion<T>, t: T) -> Quaternion<T>
where T: Float {
let norm_v = norm(q.1);
let norm_q = pythag(q.0, norm_v);
let omega = (q.0 / norm_q).acos();
let (sin, cos) = (t * omega).sin_cos();
let coef = norm_q.powf(t);
( coef * cos, scale((coef / norm_v) * sin, q.1) )
}
/// Power function of Versor.
///
/// If the argument `q` is guaranteed to be a Versor,
/// it is less calculation cost than the `pow(...)` function.
///
/// # Examples
///
/// ```
/// # use quaternion_core::{Quaternion, normalize, mul, inv, pow_versor, sqrt};
/// let q: Quaternion<f64> = normalize( (1.0, [2.0, 3.0, 4.0]) );
///
/// let q_q = mul(q, q);
/// let q_pow_2 = pow_versor(q, 2.0);
/// assert!( (q_q.0 - q_pow_2.0).abs() < 1e-12 );
/// assert!( (q_q.1[0] - q_pow_2.1[0]).abs() < 1e-12 );
/// assert!( (q_q.1[1] - q_pow_2.1[1]).abs() < 1e-12 );
/// assert!( (q_q.1[2] - q_pow_2.1[2]).abs() < 1e-12 );
///
/// let q_sqrt = sqrt(q);
/// let q_pow_0p5 = pow_versor(q, 0.5);
/// assert!( (q_sqrt.0 - q_pow_0p5.0).abs() < 1e-12 );
/// assert!( (q_sqrt.1[0] - q_pow_0p5.1[0]).abs() < 1e-12 );
/// assert!( (q_sqrt.1[1] - q_pow_0p5.1[1]).abs() < 1e-12 );
/// assert!( (q_sqrt.1[2] - q_pow_0p5.1[2]).abs() < 1e-12 );
///
/// let q_inv = inv(q);
/// let q_pow_m1 = pow_versor(q, -1.0);
/// assert!( (q_inv.0 - q_pow_m1.0).abs() < 1e-12 );
/// assert!( (q_inv.1[0] - q_pow_m1.1[0]).abs() < 1e-12 );
/// assert!( (q_inv.1[1] - q_pow_m1.1[1]).abs() < 1e-12 );
/// assert!( (q_inv.1[2] - q_pow_m1.1[2]).abs() < 1e-12 );
/// ```
#[inline]
pub fn pow_versor<T>(q: Quaternion<T>, t: T) -> Quaternion<T>
where T: Float {
let (sin, cos) = (t * q.0.acos()).sin_cos();
( cos, scale(sin / norm(q.1), q.1) )
}
/// Square root of Quaternion.
///
/// # Examples
///
/// ```
/// # use quaternion_core::{Quaternion, mul, sqrt};
/// let q: Quaternion<f64> = (1.0, [2.0, 3.0, 4.0]);
/// let q_sqrt = sqrt(q);
///
/// let result = mul(q_sqrt, q_sqrt);
/// assert!( (q.0 - result.0).abs() < 1e-12 );
/// assert!( (q.1[0] - result.1[0]).abs() < 1e-12 );
/// assert!( (q.1[1] - result.1[1]).abs() < 1e-12 );
/// assert!( (q.1[2] - result.1[2]).abs() < 1e-12 );
/// ```
#[inline]
pub fn sqrt<T>(q: Quaternion<T>) -> Quaternion<T>
where T: Float {
let half = cast(0.5);
let norm_v = norm(q.1);
let norm_q = pythag(q.0, norm_v);
let coef = ((norm_q - q.0) * half).sqrt() / norm_v;
( ((norm_q + q.0) * half).sqrt(), scale(coef, q.1) )
}
/// Square root of Versor.
///
/// If the argument `q` is guaranteed to be a Versor,
/// it is less calculation cost than the `sqrt(...)` function.
///
/// # Examples
///
/// ```
/// # use quaternion_core::{Quaternion, normalize, mul, sqrt_versor};
/// let q: Quaternion<f64> = normalize( (1.0, [2.0, 3.0, 4.0]) );
/// let q_sqrt = sqrt_versor(q);
///
/// let result = mul(q_sqrt, q_sqrt);
/// assert!( (q.0 - result.0).abs() < 1e-12 );
/// assert!( (q.1[0] - result.1[0]).abs() < 1e-12 );
/// assert!( (q.1[1] - result.1[1]).abs() < 1e-12 );
/// assert!( (q.1[2] - result.1[2]).abs() < 1e-12 );
/// ```
#[inline]
pub fn sqrt_versor<T>(q: Quaternion<T>) -> Quaternion<T>
where T: Float {
let half = cast(0.5);
let coef = (half - q.0 * half).sqrt() / norm(q.1);
( mul_add(q.0, half, half).sqrt(), scale(coef, q.1) )
}
/// Rotation of point (Point Rotation - Frame Fixed)
///
/// `q v q* (||q|| = 1)`
///
/// Since it is implemented with an optimized formula,
/// it can be calculated with the amount of operations shown in the table below:
///
/// | Operation | Num |
/// |:------------:|:---:|
/// | Multiply | 18 |
/// | Add/Subtract | 12 |
///
/// # Examples
///
/// ```
/// # use quaternion_core::{from_axis_angle, point_rotation, mul, conj};
/// # let PI = std::f64::consts::PI;
/// // Make these as you like.
/// let v = [1.0, 0.5, -8.0];
/// let q = from_axis_angle([0.2, 1.0, -2.0], PI);
///
/// let r = point_rotation(q, v);
///
/// // This makes a lot of wasted calculations.
/// let r_check = mul( mul(q, (0.0, v)), conj(q) ).1;
///
/// assert!( (r[0] - r_check[0]).abs() < 1e-12 );
/// assert!( (r[1] - r_check[1]).abs() < 1e-12 );
/// assert!( (r[2] - r_check[2]).abs() < 1e-12 );
/// ```
#[inline]
pub fn point_rotation<T>(q: Quaternion<T>, v: Vector3<T>) -> Vector3<T>
where T: Float {
let tmp = scale_add(q.0, v, cross(q.1, v));
scale_add(cast(2.0), cross(q.1, tmp), v)
}
/// Rotation of frame (Frame Rotation - Point Fixed)
///
/// `q* v q (||q|| = 1)`
///
/// Since it is implemented with an optimized formula,
/// it can be calculated with the amount of operations shown in the table below:
///
/// | Operation | Num |
/// |:------------:|:---:|
/// | Multiply | 18 |
/// | Add/Subtract | 12 |
///
/// # Examples
///
/// ```
/// # use quaternion_core::{from_axis_angle, point_rotation, mul, conj};
/// # let PI = std::f64::consts::PI;
/// // Make these as you like.
/// let v = [1.0, 0.5, -8.0];
/// let q = from_axis_angle([0.2, 1.0, -2.0], PI);
///
/// let r = point_rotation(q, v);
///
/// // This makes a lot of wasted calculations.
/// let r_check = mul( mul(conj(q), (0.0, v)), q ).1;
///
/// assert!( (r[0] - r_check[0]).abs() < 1e-12 );
/// assert!( (r[1] - r_check[1]).abs() < 1e-12 );
/// assert!( (r[2] - r_check[2]).abs() < 1e-12 );
/// ```
#[inline]
pub fn frame_rotation<T>(q: Quaternion<T>, v: Vector3<T>) -> Vector3<T>
where T: Float {
let tmp = scale_add(q.0, v, cross(v, q.1));
scale_add(cast(2.0), cross(tmp, q.1), v)
}
/// Calculate the versor to rotate from vector `a` to vector `b` (Without singularity!).
///
/// This function calculates `q` satisfying `b = point_rotation(q, a)`
/// when `norm(a) = norm(b)`.
/// If `norm(a) > 0` and `norm(b) > 0`, then `q` can be calculated with good
/// accuracy no matter what the positional relationship between `a` and `b` is.
///
/// If you enter a zero vector either `a` or `b`, it returns `None`.
///
/// # Examples
///
/// ```
/// # use quaternion_core::{Vector3, cross, rotate_a_to_b, point_rotation, normalize};
/// let a: Vector3<f64> = [1.5, -0.5, 0.2];
/// let b: Vector3<f64> = [0.1, 0.6, 1.0];
///
/// let q = rotate_a_to_b(a, b).unwrap();
/// let b_check = point_rotation(q, a);
///
/// let b_u = normalize(b);
/// let b_check_u = normalize(b_check);
/// assert!( (b_u[0] - b_check_u[0]).abs() < 1e-12 );
/// assert!( (b_u[1] - b_check_u[1]).abs() < 1e-12 );
/// assert!( (b_u[2] - b_check_u[2]).abs() < 1e-12 );
/// ```
#[inline]
pub fn rotate_a_to_b<T>(a: Vector3<T>, b: Vector3<T>) -> Option<Quaternion<T>>
where T: Float {
let norm_inv_a = norm(a).recip();
let norm_inv_b = norm(b).recip();
if norm_inv_a.is_infinite() || norm_inv_b.is_infinite() {
return None;
}
let a = scale(norm_inv_a, a);
let b = scale(norm_inv_b, b);
let a_add_b = add(a, b);
let dot_a_add_b = dot(a_add_b, a_add_b);
if dot_a_add_b >= T::one() { // arccos(a・b) = 120degで切り替え
Some( (T::zero(), scale(dot_a_add_b.sqrt().recip(), a_add_b)) )
} else {
let norm_a_sub_b = (cast::<T>(4.0) - dot_a_add_b).sqrt();
let axis_a2mb = scale(norm_a_sub_b.recip(), sub(a, b)); // a ==> -b
let axis_mb2b = orthogonal_vector(b); // -b ==> b
Some( mul(axis_mb2b, axis_a2mb) )
}
}
/// Calculate the versor to rotate from vector `a` to vector `b` by the shortest path.
///
/// The parameter `t` adjusts the amount of movement from `a` to `b`.
/// When `t = 1`, `a` moves completely to position `b`.
///
/// The algorithm used in this function is less accurate when `a` and `b` are parallel.
/// Therefore, it is better to use the `rotate_a_to_b(a, b)` function when `t = 1` and
/// the rotation axis is not important.
///
/// If you enter a zero vector either `a` or `b`, it returns `None`.
#[inline]
pub fn rotate_a_to_b_shortest<T>(a: Vector3<T>, b: Vector3<T>, t: T) -> Option<Quaternion<T>>
where T: Float {
let norm_inv_a = norm(a).recip();
let norm_inv_b = norm(b).recip();
if norm_inv_a.is_infinite() || norm_inv_b.is_infinite() {
return None;
}
let a = scale(norm_inv_a, a);
let b = scale(norm_inv_b, b);
let axis = cross(a, b);
let norm_axis_inv = norm(axis).recip();
if norm_axis_inv.is_finite() {
let (sin, cos) = (t * acos_safe(dot(a, b)) * cast(0.5)).sin_cos();
Some( (cos, scale(sin * norm_axis_inv, axis)) )
} else { // Singularity (a || b)
if dot(a, b) > T::zero() {
Some( IDENTITY() ) // a = b
} else {
Some( (T::zero(), orthogonal_vector(a)) ) // a = -b
}
}
}
/// Lerp (Linear interpolation)
///
/// Generate a Versor that interpolate the shortest path from `a` to `b`.
/// The argument `t (0 <= t <= 1)` is the interpolation parameter.
///
/// The arguments `a` and `b` must be Versor.
///
/// Normalization is not performed internally because
/// it increases the computational complexity.
#[inline]
pub fn lerp<T>(a: Quaternion<T>, b: Quaternion<T>, t: T) -> Quaternion<T>
where T: Float {
// 最短経路で補間する
if dot(a, b).is_sign_negative() {
// bの符号を反転
scale_add(-t, add(a, b), a)
} else {
scale_add( t, sub(b, a), a)
}
}
/// Slerp (Spherical linear interpolation)
///
/// Generate a Versor that interpolate the shortest path from `a` to `b`.
/// The argument `t (0 <= t <= 1)` is the interpolation parameter.
///
/// The arguments `a` and `b` must be Versor.
#[inline]
pub fn slerp<T>(a: Quaternion<T>, mut b: Quaternion<T>, t: T) -> Quaternion<T>
where T: Float {
// 最短経路で補間する
let mut dot = dot(a, b);
if dot.is_sign_negative() {
b = negate(b);
dot = -dot;
}
// If the distance between quaternions is close enough, use lerp.
if dot > cast(0.9995) { // Approximation error < 0.017%
scale_add(t, sub(b, a), a) // lerp
} else {
let omega = dot.acos(); // Angle between the two quaternions.
let tmp = t * omega;
let s1 = (omega - tmp).sin();
let s2 = tmp.sin();
let coef = (T::one() - dot*dot).sqrt().recip();
let term1 = scale(s1 * coef, a);
let term2 = scale(s2 * coef, b);
add(term1, term2)
}
}