Visual Computing Library
Loading...
Searching...
No Matches
quaternion.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_SPACE_CORE_QUATERNION_H
24#define VCL_SPACE_CORE_QUATERNION_H
25
26#include "matrix.h"
27#include "point.h"
28
29namespace vcl {
30
49template<typename Scalar>
50class Quaternion : public Eigen::Quaternion<Scalar>
51{
52 using Base = Eigen::Quaternion<Scalar>;
53
54public:
55 // inherit Base constructors
56 using Base::Base;
57
58 // inherit Base operators
59 using Base::operator*;
60 using Base::operator*=;
61
65 using ScalarType = Scalar;
66
71 Quaternion() : Base(1, 0, 0, 0) {}
72
73 template<typename S, int Options>
74 Quaternion(const Eigen::Quaternion<S, Options>& q) : Base(q)
75 {
76 }
77
78 Quaternion(const Scalar& angle, const Point3<Scalar>& axis) :
79 Base(Eigen::AngleAxis<Scalar>(angle, axis))
80 {
81 }
82
83 Quaternion(const Matrix44<Scalar>& rotMatrix) :
84 Base(Matrix33<Scalar>(rotMatrix.block(0, 0, 3, 3)))
85 {
86 }
87
100 {
101 Base::setFromTwoVectors(a, b);
102 }
103
115 template<typename S>
116 auto cast() const
117 {
118 if constexpr (std::is_same_v<Scalar, S>) {
119 return *this;
120 }
121 else {
122 return Quaternion<S>(Base::template cast<S>());
123 }
124 }
125
126 constexpr uint size() const { return 4; }
127
128 // void setIdentity() { mQ.setIdentity(); }
129
130 void set(const Scalar& w, const Scalar& x, const Scalar& y, const Scalar& z)
131 {
132 Base::w() = w;
133 Base::x() = x;
134 Base::y() = y;
135 Base::z() = z;
136 }
137
138 void setFromAngleAxis(const Scalar& angle, const Point3<Scalar>& axis)
139 {
140 *this = Base(Eigen::AngleAxis<Scalar>(angle, axis));
141 }
142
151 std::size_t hash() const
152 {
153 std::size_t h = 0;
154 for (size_t i = 0; i < 4; ++i)
155 hashCombine(h, Base::operator()(i));
156 return h;
157 }
158
169 {
170 Eigen::Matrix<Scalar, 3, 1> fc = Base::vec().cross(p);
171
172 Eigen::Matrix<Scalar, 3, 1> fd = p * Base::w();
173
174 Eigen::Matrix<Scalar, 3, 1> s = fc + fd;
175
176 Eigen::Matrix<Scalar, 3, 1> sc = Base::vec().cross(s);
177
178 return Point3<Scalar>(p + sc * 2.0);
179 }
180};
181
191
202
203} // namespace vcl
204
205#endif // VCL_SPACE_CORE_QUATERNION_H
Quaternion class.
Definition quaternion.h:51
Quaternion()
Constructs a quaternion representing the identity rotation (w = 1, 0, 0, 0).
Definition quaternion.h:71
Quaternion(const Point3< Scalar > &a, const Point3< Scalar > &b)
Constructs the quaternion that will represent the rotation between the two arbitrary vectors a and b.
Definition quaternion.h:99
auto cast() const
Casts the Quaternion object to a different scalar type.
Definition quaternion.h:116
Point3< Scalar > operator*(const Point3< Scalar > &p) const
Quaternion-Vector multiplication.
Definition quaternion.h:168
std::size_t hash() const
Computes the hash value of the quaternion.
Definition quaternion.h:151
Scalar ScalarType
The Scalar type of the Quaternion.
Definition quaternion.h:65
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43