33 :
w(static_cast<Scalar>(1.0)),
x(static_cast<Scalar>(0.0)),
y(static_cast<Scalar>(0.0)),
34 z(static_cast<Scalar>(0.0)) {}
49 w * other.
x +
x * other.
w +
y * other.
z -
z * other.
y,
50 w * other.
y -
x * other.
z +
y * other.
w +
z * other.
x,
51 w * other.
z +
x * other.
y -
y * other.
x +
z * other.
w);
118 Scalar one =
static_cast<Scalar
>(1.0);
119 Scalar two =
static_cast<Scalar
>(2.0);
131 R(0, 0) = one - two * (yy + zz);
132 R(0, 1) = two * (xy - wz);
133 R(0, 2) = two * (xz + wy);
135 R(1, 0) = two * (xy + wz);
136 R(1, 1) = one - two * (xx + zz);
137 R(1, 2) = two * (yz - wx);
139 R(2, 0) = two * (xz - wy);
140 R(2, 1) = two * (yz + wx);
141 R(2, 2) = one - two * (xx + yy);
165 Scalar half =
static_cast<Scalar
>(0.5);
173 return Quaternion(cr * cp * cy + sr * sp * sy,
174 sr * cp * cy - cr * sp * sy,
175 cr * sp * cy + sr * cp * sy,
176 cr * cp * sy - sr * sp * cy
187 Scalar half =
static_cast<Scalar
>(0.5);
198 return Quaternion(c, n_axis(0) * s, n_axis(1) * s, n_axis(2) * s);
207 Scalar half =
static_cast<Scalar
>(0.5);
208 Scalar eps =
static_cast<Scalar
>(1e-12);
210 Scalar half_angle = angle * half;
211 Scalar safe_angle = angle + eps;
214 Scalar scale_raw =
janus::sin(half_angle) / safe_angle;
215 Scalar scale =
janus::where(angle > eps, scale_raw, half);
237 Scalar trace = mat.trace();
238 Scalar q_w, q_x, q_y, q_z;
239 Scalar one =
static_cast<Scalar
>(1.0);
240 Scalar half =
static_cast<Scalar
>(0.5);
241 Scalar two =
static_cast<Scalar
>(2.0);
243 if constexpr (std::is_floating_point_v<Scalar>) {
246 Scalar s =
static_cast<Scalar
>(0.5) /
janus::sqrt(trace + 1.0);
248 q_x = (mat(2, 1) - mat(1, 2)) * s;
249 q_y = (mat(0, 2) - mat(2, 0)) * s;
250 q_z = (mat(1, 0) - mat(0, 1)) * s;
252 if (mat(0, 0) > mat(1, 1) && mat(0, 0) > mat(2, 2)) {
253 Scalar s = 2.0 *
janus::sqrt(1.0 + mat(0, 0) - mat(1, 1) - mat(2, 2));
254 q_w = (mat(2, 1) - mat(1, 2)) / s;
256 q_y = (mat(0, 1) + mat(1, 0)) / s;
257 q_z = (mat(0, 2) + mat(2, 0)) / s;
258 }
else if (mat(1, 1) > mat(2, 2)) {
259 Scalar s = 2.0 *
janus::sqrt(1.0 + mat(1, 1) - mat(0, 0) - mat(2, 2));
260 q_w = (mat(0, 2) - mat(2, 0)) / s;
261 q_x = (mat(0, 1) + mat(1, 0)) / s;
263 q_z = (mat(1, 2) + mat(2, 1)) / s;
265 Scalar s = 2.0 *
janus::sqrt(1.0 + mat(2, 2) - mat(0, 0) - mat(1, 1));
266 q_w = (mat(1, 0) - mat(0, 1)) / s;
267 q_x = (mat(0, 2) + mat(2, 0)) / s;
268 q_y = (mat(1, 2) + mat(2, 1)) / s;
277 Scalar eps =
static_cast<Scalar
>(1e-12);
280 Scalar r0 =
janus::where(trace + one > eps, trace + one, eps);
282 Scalar w0 =
static_cast<Scalar
>(0.25) / s0;
283 Scalar x0 = (mat(2, 1) - mat(1, 2)) * s0;
284 Scalar y0 = (mat(0, 2) - mat(2, 0)) * s0;
285 Scalar z0 = (mat(1, 0) - mat(0, 1)) * s0;
288 Scalar r1 = one + mat(0, 0) - mat(1, 1) - mat(2, 2);
291 Scalar w1 = (mat(2, 1) - mat(1, 2)) / s1;
292 Scalar x1 =
static_cast<Scalar
>(0.25) * s1;
293 Scalar y1 = (mat(0, 1) + mat(1, 0)) / s1;
294 Scalar z1 = (mat(0, 2) + mat(2, 0)) / s1;
297 Scalar r2 = one + mat(1, 1) - mat(0, 0) - mat(2, 2);
300 Scalar w2 = (mat(0, 2) - mat(2, 0)) / s2;
301 Scalar x2 = (mat(0, 1) + mat(1, 0)) / s2;
302 Scalar y2 =
static_cast<Scalar
>(0.25) * s2;
303 Scalar z2 = (mat(1, 2) + mat(2, 1)) / s2;
306 Scalar r3 = one + mat(2, 2) - mat(0, 0) - mat(1, 1);
309 Scalar w3 = (mat(1, 0) - mat(0, 1)) / s3;
310 Scalar x3 = (mat(0, 2) + mat(2, 0)) / s3;
311 Scalar y3 = (mat(1, 2) + mat(2, 1)) / s3;
312 Scalar z3 =
static_cast<Scalar
>(0.25) * s3;
315 auto cond_trace = trace >
static_cast<Scalar
>(0.0);
317 auto cond_r11 = mat(1, 1) > mat(2, 2);
344 Scalar sinr_cosp =
static_cast<Scalar
>(2.0) * (
w *
x +
y *
z);
345 Scalar cosr_cosp =
static_cast<Scalar
>(1.0) -
static_cast<Scalar
>(2.0) * (
x *
x +
y *
y);
349 Scalar sinp =
static_cast<Scalar
>(2.0) * (
w *
y -
z *
x);
353 if constexpr (std::is_floating_point_v<Scalar>) {
354 if (std::abs(sinp) >= 1)
355 pitch = std::copysign(std::numbers::pi_v<double> / 2,
358 pitch = std::asin(sinp);
365 Scalar siny_cosp =
static_cast<Scalar
>(2.0) * (
w *
z +
x *
y);
366 Scalar cosy_cosp =
static_cast<Scalar
>(1.0) -
static_cast<Scalar
>(2.0) * (
y *
y +
z *
z);
386template <
typename Scalar>
388 Scalar one =
static_cast<Scalar
>(1.0);
389 Scalar zero =
static_cast<Scalar
>(0.0);
390 Scalar dot_threshold =
static_cast<Scalar
>(0.9995);
393 Scalar
dot = q0.
w * q1.
w + q0.
x * q1.
x + q0.
y * q1.
y + q0.
z * q1.
z;
412 Scalar wa_slerp =
janus::sin((one - t) * theta) / sin_theta;
413 Scalar wb_slerp =
janus::sin(t * theta) / sin_theta;
416 Scalar wa_nlerp = one - t;
420 Scalar use_slerp = dot_eff < dot_threshold;
422 Scalar wa =
janus::where(use_slerp, wa_slerp, wa_nlerp);
423 Scalar wb =
janus::where(use_slerp, wb_slerp, wb_nlerp);
Scalar and element-wise arithmetic functions (abs, sqrt, pow, exp, log, etc.).
C++20 concepts constraining valid Janus scalar types.
Core type aliases for numeric and symbolic Eigen/CasADi interop.
Linear algebra operations (solve, inverse, determinant, eigendecomposition, norms).
Conditional selection, comparison, and logical operations.
Trigonometric and inverse trigonometric functions.
Quaternion class for rotation representation.
Definition Quaternion.hpp:25
Scalar z
Definition Quaternion.hpp:27
Quaternion(const Vec4< Scalar > &v)
Construct from Vec4 [w, x, y, z].
Definition Quaternion.hpp:40
static Quaternion from_axis_angle(const Vec3< Scalar > &axis, Scalar angle)
Create from axis-angle representation.
Definition Quaternion.hpp:186
Scalar y
Definition Quaternion.hpp:27
Vec3< Scalar > to_euler() const
Extract Euler angles (Roll-Pitch-Yaw / XYZ).
Definition Quaternion.hpp:342
static Quaternion from_euler(Scalar roll, Scalar pitch, Scalar yaw)
Create from Euler Angles (Yaw-Pitch-Roll / Z-Y-X sequence).
Definition Quaternion.hpp:164
Quaternion operator*(const Scalar &s) const
Scalar multiplication.
Definition Quaternion.hpp:57
Scalar x
Definition Quaternion.hpp:27
Scalar squared_norm() const
Squared norm (w^2 + x^2 + y^2 + z^2).
Definition Quaternion.hpp:76
Scalar norm() const
Quaternion norm.
Definition Quaternion.hpp:80
Quaternion inverse() const
Inverse (conjugate / norm_sq).
Definition Quaternion.hpp:72
Vec4< Scalar > coeffs() const
Export as vector [w, x, y, z].
Definition Quaternion.hpp:148
Mat3< Scalar > to_rotation_matrix() const
Convert to 3x3 rotation matrix.
Definition Quaternion.hpp:116
Quaternion normalized() const
Return unit quaternion.
Definition Quaternion.hpp:84
Vec3< Scalar > rotate(const Vec3< Scalar > &v) const
Rotate a 3D vector: v_rot = q * v * q_conj.
Definition Quaternion.hpp:95
Scalar w
Definition Quaternion.hpp:27
Quaternion(Scalar w, Scalar x, Scalar y, Scalar z)
Component constructor.
Definition Quaternion.hpp:37
Quaternion conjugate() const
Conjugate (w, -x, -y, -z).
Definition Quaternion.hpp:68
static Quaternion from_rotation_matrix(const Mat3< Scalar > &mat)
Create from 3x3 rotation matrix.
Definition Quaternion.hpp:226
static Quaternion from_rotation_vector(const Vec3< Scalar > &rot_vec)
Create from rotation vector (axis * angle).
Definition Quaternion.hpp:206
Quaternion operator+(const Quaternion &other) const
Quaternion addition.
Definition Quaternion.hpp:62
Quaternion operator*(const Quaternion &other) const
Hamilton product.
Definition Quaternion.hpp:47
Quaternion()
Default constructor: Identity quaternion (1, 0, 0, 0).
Definition Quaternion.hpp:32
Definition Diagnostics.hpp:19
auto where(const Cond &cond, const T1 &if_true, const T2 &if_false)
Select values based on condition (ternary operator) Returns: cond ? if_true : if_false Supports mixed...
Definition Logic.hpp:43
auto dot(const Eigen::MatrixBase< DerivedA > &a, const Eigen::MatrixBase< DerivedB > &b)
Computes dot product of two vectors.
Definition Linalg.hpp:500
T sqrt(const T &x)
Computes the square root of a scalar.
Definition Arithmetic.hpp:46
T acos(const T &x)
Computes arc cosine of x.
Definition Trig.hpp:121
Eigen::Matrix< Scalar, 4, 1 > Vec4
Definition JanusTypes.hpp:58
Quaternion< Scalar > slerp(const Quaternion< Scalar > &q0, const Quaternion< Scalar > &q1, Scalar t)
Spherical Linear Interpolation (full fidelity).
Definition Quaternion.hpp:387
T cos(const T &x)
Computes cosine of x.
Definition Trig.hpp:46
T sign(const T &x)
Computes sign of x.
Definition Arithmetic.hpp:326
Eigen::Matrix< Scalar, 3, 3 > Mat3
Definition JanusTypes.hpp:61
auto norm(const Eigen::MatrixBase< Derived > &x, NormType type=NormType::L2)
Computes vector/matrix norm.
Definition Linalg.hpp:607
T asin(const T &x)
Computes arc sine of x.
Definition Trig.hpp:96
T sin(const T &x)
Computes sine of x.
Definition Trig.hpp:21
auto logical_and(const T1 &x1, const T2 &x2)
Logical AND (x && y).
Definition Logic.hpp:422
Eigen::Matrix< Scalar, 3, 1 > Vec3
Definition JanusTypes.hpp:57
auto cross(const Eigen::MatrixBase< DerivedA > &a, const Eigen::MatrixBase< DerivedB > &b)
Computes 3D cross product.
Definition Linalg.hpp:513
T atan2(const T &y, const T &x)
Computes arc tangent of y/x using signs of both arguments.
Definition Trig.hpp:172