Signal/Geometry Processing Library (SPL) 1.1.24 |

cgalUtil.hpp
Go to the documentation of this file.
1 // Copyright (c) 2011 Michael D. Adams
2 // All rights reserved.
3
4 // __START_OF_LICENSE__
5 //
6 // Copyright (c) 2015 Michael D. Adams
7 // All rights reserved.
8 //
9 // This file is part of the Signal Processing Library (SPL).
10 //
11 // This program is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU General Public License as
13 // published by the Free Software Foundation; either version 3,
14 // or (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 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public
22 // License along with this program; see the file LICENSE. If not,
23 // see <http://www.gnu.org/licenses/>.
24 //
25 // __END_OF_LICENSE__
26
32 #ifndef SPL_cgalUtil_hpp
33 #define SPL_cgalUtil_hpp
34
36 // Header files
38
39 #include <SPL/config.hpp>
40 #include <SPL/math.hpp>
41 #include <cmath>
42 #include <CGAL/Cartesian.h>
43 #include <CGAL/Vector_3.h>
44 #include <CGAL/Point_3.h>
45
47 //
49
50 namespace SPL {
51
57 // Vector utility functions
60
65 template <class T>
66 inline typename T::FT norm(const typename CGAL::Vector_3<T>& v)
67 {
68 return sqrt(v * v);
69 }
70
78 template <class T>
79 inline typename T::Vector_3 normalize(const typename CGAL::Vector_3<T>& v)
80 {
81 typename T::FT norm = sqrt(v * v);
82 if (norm != 0.0) {
83 return v / norm;
84 } else {
85 return v;
86 }
87 }
88
93 template <class T>
94 inline typename T::FT angleBetweenVectors(const typename CGAL::Vector_3<T>& u,
95 const CGAL::Vector_3<T>& v)
96 {
97 return acos((u * v) / (norm(u) * norm(v)));
98 }
99
101 // Rotation class.
103
105 template <class T>
106 struct Rotation_3
107 {
109 typedef typename T::FT Real;
110
112 typedef typename T::Vector_3 Vector_3;
113
117 Rotation_3(const Vector_3& axis_, Real angle_) : axis(axis_),
118 angle(angle_) {}
119
121 Vector_3 axis;
122
124 Real angle;
125 };
126
128 // Quaternion class.
130
132 template <class T>
133 struct Quaternion
134 {
136 typedef typename T::FT Real;
137
139 typedef typename CGAL::Vector_3<T> Vector_3;
140
144 Quaternion() : scalar(0), vector(0, 0, 0) {}
145
149 Quaternion(Real scalar_, const Vector_3& vector_) : scalar(scalar_),
150 vector(vector_) {}
151
153 Real scalar;
154
156 Vector_3 vector;
157 };
158
163 template <class T>
164 Quaternion<T> operator*(const Quaternion<T>& q, const Quaternion<T>& r)
165 {
166 return Quaternion<T>(q.scalar * r.scalar - q.vector * r.vector,
167 q.scalar * r.vector + r.scalar * q.vector +
168 CGAL::cross_product(q.vector, r.vector));
169 }
170
175 template <class T>
176 Quaternion<T> operator/(const Quaternion<T>& q, const Quaternion<T>& r)
177 {
178 typename T::FT sqrNorm = sqr(r.scalar) + r.vector * r.vector;
179 return q * Quaternion<T>(r.scalar / sqrNorm, -r.vector / sqrNorm);
180 }
181
186 template <class T>
187 Quaternion<T> rotationToQuaternion(const Rotation_3<T>& rot)
188 {
189 typename T::FT theta = 0.5 * rot.angle;
190 return Quaternion<T>(cos(theta), sin(theta) * normalize(rot.axis));
191 }
192
197 template <class T>
198 Rotation_3<T> quaternionToRotation(const Quaternion<T>& q)
199 {
200 return Rotation_3<T>(normalize(q.vector), 2.0 * acos(q.scalar));
201 }
202
204 //
206
211 }
212
213 #endif
SPL::normalize
T::Vector_3 normalize(const typename CGAL::Vector_3< T > &v)
Compute a unit vector.
Definition: cgalUtil.hpp:79
SPL::Quaternion
A quaternion represented in terms of its scalar and vector parts.
Definition: cgalUtil.hpp:133
SPL::Rotation_3::Vector_3
T::Vector_3 Vector_3
The 3-dimensional vector type.
Definition: cgalUtil.hpp:112
SPL
Definition: Arcball.hpp:48
SPL::quaternionToRotation
Rotation_3< T > quaternionToRotation(const Quaternion< T > &q)
Convert a unit-norm quaternion into its corresponding rotation.
Definition: cgalUtil.hpp:198
SPL::norm
T::FT norm(const typename CGAL::Vector_3< T > &v)
Compute the norm of a vector.
Definition: cgalUtil.hpp:66
SPL::Rotation_3::angle
Real angle
The angle of rotation.
Definition: cgalUtil.hpp:124
SPL::Quaternion::scalar
Real scalar
The scalar part of the quaternion.
Definition: cgalUtil.hpp:153
SPL::operator*
Quaternion< T > operator*(const Quaternion< T > &q, const Quaternion< T > &r)
Compute the product of two quaternions.
Definition: cgalUtil.hpp:164
SPL::Rotation_3
A 3-D rotation.
Definition: cgalUtil.hpp:106
SPL::Quaternion::vector
Vector_3 vector
The vector part of the quaternion.
Definition: cgalUtil.hpp:156
SPL::rotationToQuaternion
Quaternion< T > rotationToQuaternion(const Rotation_3< T > &rot)
Convert a rotation into its corresponding quaternion.
Definition: cgalUtil.hpp:187
SPL::Quaternion::Real
T::FT Real
The field type for the CGAL kernel.
Definition: cgalUtil.hpp:136
math.hpp
This file contains various mathematical functions/code.
SPL::angleBetweenVectors
T::FT angleBetweenVectors(const typename CGAL::Vector_3< T > &u, const CGAL::Vector_3< T > &v)
Compute the angle between two vectors.
Definition: cgalUtil.hpp:94
SPL::Quaternion::Vector_3
CGAL::Vector_3< T > Vector_3
The 3-dimensional vector type.
Definition: cgalUtil.hpp:139
SPL::Quaternion::Quaternion
Quaternion(Real scalar_, const Vector_3 &vector_)
Definition: cgalUtil.hpp:149
SPL::Rotation_3::Real
T::FT Real
The field type for the CGAL kernel.
Definition: cgalUtil.hpp:109
SPL::sqr
T sqr(const T &x)
The square function.
Definition: math.hpp:107
SPL::Rotation_3::Rotation_3
Rotation_3(const Vector_3 &axis_, Real angle_)
Create a rotation.
Definition: cgalUtil.hpp:117
SPL::Rotation_3::axis
Vector_3 axis
The axis of rotation.
Definition: cgalUtil.hpp:121
SPL::operator/
Quaternion< T > operator/(const Quaternion< T > &q, const Quaternion< T > &r)
Compute the quotient of two quaternions.
Definition: cgalUtil.hpp:176
SPL::Quaternion::Quaternion
Quaternion()
Definition: cgalUtil.hpp:144