Graphite  Version 3
An experimental 3D geometry processing program
quaternion.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2000-2022 Inria
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * * Neither the name of the ALICE Project-Team nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  * Contact: Bruno Levy
30  *
31  * https://www.inria.fr/fr/bruno-levy
32  *
33  * Inria,
34  * Domaine de Voluceau,
35  * 78150 Le Chesnay - Rocquencourt
36  * FRANCE
37  *
38  */
39 
40 #ifndef GEOGRAM_BASIC_QUATERNION
41 #define GEOGRAM_BASIC_QUATERNION
42 
43 #include <geogram/basic/common.h>
44 #include <geogram/basic/geometry.h>
45 #include <iostream>
46 
47 
54 namespace GEO {
55 
61  class GEOGRAM_API Quaternion {
62  public:
63 
67  Quaternion() : v_(0.0,0.0,0.0), s_(1.0) {
68  }
69 
74  Quaternion(const Quaternion& rhs) : v_(rhs.v_), s_(rhs.s_) {
75  }
76 
85  double x, double y, double z, double w
86  ) : v_(x,y,z), s_(w) {
87  }
88 
95  Quaternion( const vec3& v, double s ) : v_(v), s_(s) {
96  }
97 
103  Quaternion& operator = ( const Quaternion &q ) {
104  v_ = q.v_ ;
105  s_ = q.s_ ;
106  return *this ;
107  }
108 
114  void set( const vec3& v, double s ) {
115  v_ = v;
116  s_ = s;
117  }
118 
124  void print( std::ostream& out ) const {
125  out << v_.x << " " << v_.y << " " << v_.z << " " << s_ ;
126  }
127 
128 
133  mat4 to_matrix() const;
134 
139  void set_angle( double f ) {
140  vec3 ax = axis();
141  s_ = ::cos(f / 2.0);
142  v_ = ax * ::sin(f / 2.0);
143  }
144 
148  void scale_angle( double f ) {
149  set_angle( f * angle() );
150  }
151 
156  double angle() const {
157  return 2.0 * acos( s_ ) ;
158  }
159 
164  vec3 axis() const;
165 
175  const Quaternion& from, const Quaternion& to,
176  double t
177  );
178 
185  const vec3& v() const {
186  return v_;
187  }
188 
195  double s() const {
196  return s_;
197  }
198 
199  private:
200  vec3 v_ ;
201  double s_ ;
202  } ;
203 
204 
205  /*************************************************************************/
206 
213  inline std::ostream& operator<<(std::ostream& out, const Quaternion& q) {
214  q.print(out) ;
215  return out ;
216  }
217 
218 
225  inline std::istream& operator>>(std::istream& in, Quaternion& q) {
226  double x=0.0,y=0.0,z=0.0,w=0.0 ;
227  in >> x >> y >> z >> w ;
228  q.set(vec3(x,y,z),w) ;
229  return in ;
230  }
231 
232 
239  inline Quaternion operator + (const Quaternion& a, const Quaternion& b) {
240  return Quaternion(
241  a.v() + b.v(),
242  a.s() + b.s()
243  ) ;
244  }
245 
252  inline Quaternion operator - (const Quaternion& a, const Quaternion& b) {
253  return Quaternion(
254  a.v() - b.v(),
255  a.s() - b.s()
256  ) ;
257  }
258 
264  inline Quaternion operator - (const Quaternion& a ) {
265  return Quaternion( -1.0 * a.v(), -a.s() );
266  }
267 
274  inline Quaternion operator * ( const Quaternion& a, const Quaternion& b) {
275  return Quaternion(
276  a.s() * b.v() + b.s() * a.v() + cross(a.v(),b.v()),
277  a.s() * b.s() - dot(a.v() , b.v())
278  );
279  }
280 
287  inline Quaternion operator * ( const Quaternion& a, double t ) {
288  return Quaternion( t * a.v(), a.s() * t );
289  }
290 
291 
298  inline Quaternion operator * ( double t, const Quaternion& a ) {
299  return Quaternion( t * a.v(), a.s() * t );
300  }
301 
302 }
303 
304 #endif
Quaternions are useful for representing rotations.
Definition: quaternion.h:61
static Quaternion spherical_interpolation(const Quaternion &from, const Quaternion &to, double t)
Computes the interpolation between two quaternions.
Quaternion(double x, double y, double z, double w)
Constructs a new quaternion from its coefficients.
Definition: quaternion.h:84
double angle() const
Gets the rotation angle.
Definition: quaternion.h:156
void set(const vec3 &v, double s)
Sets the coefficients of this quaterion.
Definition: quaternion.h:114
vec3 axis() const
Gets the axis.
Quaternion()
Constructs a new Quaternion.
Definition: quaternion.h:67
const vec3 & v() const
Gets the vector component.
Definition: quaternion.h:185
double s() const
Gets the scalar component.
Definition: quaternion.h:195
void print(std::ostream &out) const
Displays this Quaternion.
Definition: quaternion.h:124
void set_angle(double f)
Sets the rotation angle.
Definition: quaternion.h:139
Quaternion(const Quaternion &rhs)
Copy-constructs a new Quaternion.
Definition: quaternion.h:74
void scale_angle(double f)
Scales the rotation angle.
Definition: quaternion.h:148
Quaternion(const vec3 &v, double s)
Constructs a new Quaternion from a vector and a scalar.
Definition: quaternion.h:95
mat4 to_matrix() const
Converts this Quaternion into a matrix.
Common include file, providing basic definitions. Should be included before anything else by all head...
Geometric functions in 2d and 3d.
double angle(const vec3 &a, const vec3 &b)
Computes the angle between two 3d vectors.
Definition: geometry.h:266
Global Vorpaline namespace.
Quaternion operator-(const Quaternion &a, const Quaternion &b)
Computes the difference between two Quaternion.
Definition: quaternion.h:252
vecng< 3, Numeric::float64 > vec3
Represents points and vectors in 3d.
Definition: geometry.h:65
std::ostream & operator<<(std::ostream &out, const Quaternion &q)
Writes a Quaternion to a stream.
Definition: quaternion.h:213
vecng< DIM, FT > operator*(const Matrix< DIM, FT > &M, const vecng< DIM, FT > &x)
Computes a matrix vector product.
Definition: matrix.h:536
Quaternion operator+(const Quaternion &a, const Quaternion &b)
Computes the sum of two Quaternion.
Definition: quaternion.h:239
std::istream & operator>>(std::istream &in, Quaternion &q)
Reads a Quaternion from a stream.
Definition: quaternion.h:225