Chemical Data Processing Library C++ API - Version 1.4.0
QuaternionAdapter.hpp
Go to the documentation of this file.
1 /*
2  * QuaternionAdapter.hpp
3  *
4  * Copyright (C) 2003 Thomas Seidel <thomas.seidel@univie.ac.at>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library; see the file COPYING. If not, write to
18  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
27 #ifndef CDPL_MATH_QUATERNIONADAPTER_HPP
28 #define CDPL_MATH_QUATERNIONADAPTER_HPP
29 
30 #include <cstddef>
31 #include <type_traits>
32 
33 #include "CDPL/Math/Expression.hpp"
35 #include "CDPL/Math/TypeTraits.hpp"
36 #include "CDPL/Math/Functional.hpp"
37 #include "CDPL/Base/Exceptions.hpp"
38 
39 
40 namespace CDPL
41 {
42 
43  namespace Math
44  {
45 
50  template <typename Q>
51  class QuaternionVectorAdapter : public VectorExpression<QuaternionVectorAdapter<Q> >
52  {
53 
55 
56  public:
58  typedef Q QuaternionType;
60  typedef typename std::size_t SizeType;
62  typedef typename std::ptrdiff_t DifferenceType;
64  typedef typename Q::ValueType ValueType;
66  typedef typename Q::ConstReference ConstReference;
68  typedef typename std::conditional<std::is_const<Q>::value,
69  typename Q::ConstReference,
70  typename Q::Reference>::type Reference;
72  typedef typename std::conditional<std::is_const<Q>::value,
73  typename Q::ConstClosureType,
74  typename Q::ClosureType>::type QuaternionClosureType;
76  typedef const SelfType ConstClosureType;
79 
85  data(q) {}
86 
94  {
95  switch (i) {
96 
97  case 0:
98  return data.getC1();
99 
100  case 1:
101  return data.getC2();
102 
103  case 2:
104  return data.getC3();
105 
106  case 3:
107  return data.getC4();
108 
109  default:
110  throw Base::IndexError("QuaternionVectorAdapter: Index out of range");
111  }
112  }
113 
121  {
122  switch (i) {
123 
124  case 0:
125  return data.getC1();
126 
127  case 1:
128  return data.getC2();
129 
130  case 2:
131  return data.getC3();
132 
133  case 3:
134  return data.getC4();
135 
136  default:
137  throw Base::IndexError("QuaternionVectorAdapter: Index out of range");
138  }
139  }
140 
148  {
149  return this->operator()(i);
150  }
151 
159  {
160  return this->operator()(i);
161  }
162 
168  {
169  return 4;
170  }
171 
176  bool isEmpty() const
177  {
178  return false;
179  }
180 
186  {
187  return data;
188  }
189 
195  {
196  return data;
197  }
198 
205  {
206  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<SelfType>::Type(a));
207  return *this;
208  }
209 
216  template <typename E>
218  {
219  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<SelfType>::Type(e));
220  return *this;
221  }
222 
229  template <typename E>
231  {
232  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<SelfType>::Type(*this + e));
233  return *this;
234  }
235 
242  template <typename E>
244  {
245  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<SelfType>::Type(*this - e));
246  return *this;
247  }
248 
255  template <typename T>
256  typename std::enable_if<IsScalar<T>::value, QuaternionVectorAdapter>::type& operator*=(const T& t)
257  {
258  vectorAssignScalar<ScalarMultiplicationAssignment>(*this, t);
259  return *this;
260  }
261 
268  template <typename T>
269  typename std::enable_if<IsScalar<T>::value, QuaternionVectorAdapter>::type& operator/=(const T& t)
270  {
271  vectorAssignScalar<ScalarDivisionAssignment>(*this, t);
272  return *this;
273  }
274 
282  template <typename E>
284  {
285  vectorAssignVector<ScalarAssignment>(*this, e);
286  return *this;
287  }
288 
295  template <typename E>
297  {
298  vectorAssignVector<ScalarAdditionAssignment>(*this, e);
299  return *this;
300  }
301 
308  template <typename E>
310  {
311  vectorAssignVector<ScalarSubtractionAssignment>(*this, e);
312  return *this;
313  }
314 
320  {
321  if (this != &a)
322  vectorSwap(*this, a);
323  }
324 
331  {
332  a1.swap(a2);
333  }
334 
335  private:
337  };
338 
339  template <typename T, std::size_t N>
340  class CVector;
341 
346  template <typename Q>
348  {
349 
351  };
352 
357  template <typename Q>
359  {
360 
362  };
363 
370  template <typename E>
373  {
374  return QuaternionVectorAdapter<E>(e());
375  }
376 
383  template <typename E>
384  QuaternionVectorAdapter<const E>
386  {
388  }
389  } // namespace Math
390 } // namespace CDPL
391 
392 #endif // CDPL_MATH_QUATERNIONADAPTER_HPP
Definition of exception classes.
Definition of basic expression types.
Definition of various functors.
Definition of type traits.
Implementation of vector assignment routines.
Thrown to indicate that an index is out of range.
Definition: Base/Exceptions.hpp:152
Fixed-size vector of dimension N backed by a C-array (no dynamic allocation).
Definition: Vector.hpp:1876
CRTP base class for all quaternion expression types.
Definition: Expression.hpp:142
View adapter that exposes a quaternion as a 4-element vector expression (indices map to the component...
Definition: QuaternionAdapter.hpp:52
QuaternionVectorAdapter(QuaternionType &q)
Constructs the adapter wrapping the quaternion q.
Definition: QuaternionAdapter.hpp:84
const SelfType ConstClosureType
Constant closure type used when this adapter appears inside another expression.
Definition: QuaternionAdapter.hpp:76
QuaternionVectorAdapter & assign(const VectorExpression< E > &e)
Assigns the vector expression e to this view without intermediate temporary (use only when e does not...
Definition: QuaternionAdapter.hpp:283
QuaternionClosureType & getData()
Returns a reference to the wrapped quaternion (via its stored closure).
Definition: QuaternionAdapter.hpp:185
SizeType getSize() const
Returns the dimensionality of the view (always 4).
Definition: QuaternionAdapter.hpp:167
Q::ConstReference ConstReference
Constant reference type to an element of the wrapped quaternion.
Definition: QuaternionAdapter.hpp:66
friend void swap(QuaternionVectorAdapter &a1, QuaternionVectorAdapter &a2)
ADL-enabled free-function form of swap().
Definition: QuaternionAdapter.hpp:330
Reference operator()(SizeType i)
Returns a mutable reference to the quaternion component at index i.
Definition: QuaternionAdapter.hpp:93
ConstReference operator[](SizeType i) const
Returns a const reference to the quaternion component at index i (alias for operator()).
Definition: QuaternionAdapter.hpp:158
QuaternionVectorAdapter & minusAssign(const VectorExpression< E > &e)
Subtracts the vector expression e from this view without intermediate temporary.
Definition: QuaternionAdapter.hpp:309
std::conditional< std::is_const< Q >::value, typename Q::ConstClosureType, typename Q::ClosureType >::type QuaternionClosureType
Closure type used to store the wrapped quaternion internally (mutable or const flavor).
Definition: QuaternionAdapter.hpp:74
QuaternionVectorAdapter & operator+=(const VectorExpression< E > &e)
Adds the vector expression e componentwise to this view.
Definition: QuaternionAdapter.hpp:230
bool isEmpty() const
Tells whether the view is empty (always false; the view is fixed-size with 4 components).
Definition: QuaternionAdapter.hpp:176
std::enable_if< IsScalar< T >::value, QuaternionVectorAdapter >::type & operator/=(const T &t)
Divides every component of this view by the scalar t.
Definition: QuaternionAdapter.hpp:269
std::conditional< std::is_const< Q >::value, typename Q::ConstReference, typename Q::Reference >::type Reference
Mutable reference type (degrades to ConstReference when the wrapped quaternion is const).
Definition: QuaternionAdapter.hpp:70
QuaternionVectorAdapter & operator-=(const VectorExpression< E > &e)
Subtracts the vector expression e componentwise from this view.
Definition: QuaternionAdapter.hpp:243
std::size_t SizeType
The size type (std::size_t).
Definition: QuaternionAdapter.hpp:60
void swap(QuaternionVectorAdapter &a)
Swaps the components of this view with those of a.
Definition: QuaternionAdapter.hpp:319
QuaternionVectorAdapter & plusAssign(const VectorExpression< E > &e)
Adds the vector expression e to this view without intermediate temporary.
Definition: QuaternionAdapter.hpp:296
SelfType ClosureType
Closure type used when this adapter appears inside another expression.
Definition: QuaternionAdapter.hpp:78
std::ptrdiff_t DifferenceType
The signed difference type (std::ptrdiff_t).
Definition: QuaternionAdapter.hpp:62
Reference operator[](SizeType i)
Returns a mutable reference to the quaternion component at index i (alias for operator()).
Definition: QuaternionAdapter.hpp:147
ConstReference operator()(SizeType i) const
Returns a const reference to the quaternion component at index i.
Definition: QuaternionAdapter.hpp:120
QuaternionVectorAdapter & operator=(const QuaternionVectorAdapter &a)
Copies the components of a into this view (writing through to the wrapped quaternion).
Definition: QuaternionAdapter.hpp:204
Q::ValueType ValueType
The element value type of the wrapped quaternion.
Definition: QuaternionAdapter.hpp:64
const QuaternionClosureType & getData() const
Returns a const reference to the wrapped quaternion (via its stored closure).
Definition: QuaternionAdapter.hpp:194
std::enable_if< IsScalar< T >::value, QuaternionVectorAdapter >::type & operator*=(const T &t)
Multiplies every component of this view by the scalar t.
Definition: QuaternionAdapter.hpp:256
Q QuaternionType
The wrapped quaternion type.
Definition: QuaternionAdapter.hpp:58
QuaternionVectorAdapter & operator=(const VectorExpression< E > &e)
Assigns the vector expression e to this view (writing through to the wrapped quaternion).
Definition: QuaternionAdapter.hpp:217
CRTP base class for all vector expression types.
Definition: Expression.hpp:66
const ExpressionType & operator()() const
Returns a const reference to the derived vector expression.
Definition: Expression.hpp:76
constexpr unsigned int T
Specifies Hydrogen (Tritium).
Definition: AtomType.hpp:67
constexpr unsigned int Q
Generic type that covers any element except hydrogen and carbon.
Definition: AtomType.hpp:647
QuaternionVectorAdapter< E > vec(QuaternionExpression< E > &e)
Creates a mutable Math::QuaternionVectorAdapter view of the quaternion expression e.
Definition: QuaternionAdapter.hpp:372
void vectorSwap(V &v, VectorExpression< E > &e)
Swaps the elements of two equally sized vector expressions element by element.
Definition: VectorAssignment.hpp:97
The namespace of the Chemical Data Processing Library.
CVector< typename Q::ValueType, 4 > Type
Definition: QuaternionAdapter.hpp:350
CVector< typename Q::ValueType, 4 > Type
Definition: QuaternionAdapter.hpp:361
Selects a concrete temporary vector type compatible with the vector expression V.
Definition: TypeTraits.hpp:301
V::VectorTemporaryType Type
The concrete temporary vector type compatible with the vector expression V.
Definition: TypeTraits.hpp:304