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:
60  typedef Q QuaternionType;
61 
65  typedef typename std::size_t SizeType;
66 
70  typedef typename std::ptrdiff_t DifferenceType;
71 
75  typedef typename Q::ValueType ValueType;
76 
80  typedef typename Q::ConstReference ConstReference;
81 
85  typedef typename std::conditional<std::is_const<Q>::value,
86  typename Q::ConstReference,
87  typename Q::Reference>::type Reference;
88 
92  typedef typename std::conditional<std::is_const<Q>::value,
93  typename Q::ConstClosureType,
94  typename Q::ClosureType>::type QuaternionClosureType;
95 
99  typedef const SelfType ConstClosureType;
100 
105 
111  data(q) {}
112 
120  {
121  switch (i) {
122 
123  case 0:
124  return data.getC1();
125 
126  case 1:
127  return data.getC2();
128 
129  case 2:
130  return data.getC3();
131 
132  case 3:
133  return data.getC4();
134 
135  default:
136  throw Base::IndexError("QuaternionVectorAdapter: Index out of range");
137  }
138  }
139 
147  {
148  switch (i) {
149 
150  case 0:
151  return data.getC1();
152 
153  case 1:
154  return data.getC2();
155 
156  case 2:
157  return data.getC3();
158 
159  case 3:
160  return data.getC4();
161 
162  default:
163  throw Base::IndexError("QuaternionVectorAdapter: Index out of range");
164  }
165  }
166 
174  {
175  return this->operator()(i);
176  }
177 
185  {
186  return this->operator()(i);
187  }
188 
194  {
195  return 4;
196  }
197 
202  bool isEmpty() const
203  {
204  return false;
205  }
206 
212  {
213  return data;
214  }
215 
221  {
222  return data;
223  }
224 
231  {
232  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<SelfType>::Type(a));
233  return *this;
234  }
235 
242  template <typename E>
244  {
245  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<SelfType>::Type(e));
246  return *this;
247  }
248 
255  template <typename E>
257  {
258  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<SelfType>::Type(*this + e));
259  return *this;
260  }
261 
268  template <typename E>
270  {
271  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<SelfType>::Type(*this - e));
272  return *this;
273  }
274 
281  template <typename T>
282  typename std::enable_if<IsScalar<T>::value, QuaternionVectorAdapter>::type& operator*=(const T& t)
283  {
284  vectorAssignScalar<ScalarMultiplicationAssignment>(*this, t);
285  return *this;
286  }
287 
294  template <typename T>
295  typename std::enable_if<IsScalar<T>::value, QuaternionVectorAdapter>::type& operator/=(const T& t)
296  {
297  vectorAssignScalar<ScalarDivisionAssignment>(*this, t);
298  return *this;
299  }
300 
308  template <typename E>
310  {
311  vectorAssignVector<ScalarAssignment>(*this, e);
312  return *this;
313  }
314 
321  template <typename E>
323  {
324  vectorAssignVector<ScalarAdditionAssignment>(*this, e);
325  return *this;
326  }
327 
334  template <typename E>
336  {
337  vectorAssignVector<ScalarSubtractionAssignment>(*this, e);
338  return *this;
339  }
340 
346  {
347  if (this != &a)
348  vectorSwap(*this, a);
349  }
350 
357  {
358  a1.swap(a2);
359  }
360 
361  private:
363  };
364 
365  template <typename T, std::size_t N>
366  class CVector;
367 
372  template <typename Q>
374  {
375 
380  };
381 
386  template <typename Q>
388  {
389 
394  };
395 
402  template <typename E>
405  {
406  return QuaternionVectorAdapter<E>(e());
407  }
408 
415  template <typename E>
416  QuaternionVectorAdapter<const E>
418  {
420  }
421  } // namespace Math
422 } // namespace CDPL
423 
424 #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:2047
CRTP base class of all quaternion expression types.
Definition: Expression.hpp:148
Adapter that exposes a quaternion as a 4 element vector expression (indices map to the components C1,...
Definition: QuaternionAdapter.hpp:52
QuaternionVectorAdapter(QuaternionType &q)
Constructs the adapter wrapping the quaternion q.
Definition: QuaternionAdapter.hpp:110
const SelfType ConstClosureType
Constant closure type used when this adapter appears inside another expression.
Definition: QuaternionAdapter.hpp:99
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:309
QuaternionClosureType & getData()
Returns a reference to the wrapped quaternion (via its stored closure).
Definition: QuaternionAdapter.hpp:211
SizeType getSize() const
Returns the dimensionality of the view (always 4).
Definition: QuaternionAdapter.hpp:193
Q::ConstReference ConstReference
Constant element reference type of the wrapped quaternion.
Definition: QuaternionAdapter.hpp:80
friend void swap(QuaternionVectorAdapter &a1, QuaternionVectorAdapter &a2)
ADL-enabled free-function form of swap().
Definition: QuaternionAdapter.hpp:356
Reference operator()(SizeType i)
Returns a mutable reference to the quaternion component at index i.
Definition: QuaternionAdapter.hpp:119
ConstReference operator[](SizeType i) const
Returns a const reference to the quaternion component at index i.
Definition: QuaternionAdapter.hpp:184
QuaternionVectorAdapter & minusAssign(const VectorExpression< E > &e)
Subtracts the vector expression e from this view without intermediate temporary.
Definition: QuaternionAdapter.hpp:335
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:94
QuaternionVectorAdapter & operator+=(const VectorExpression< E > &e)
Adds the vector expression e componentwise to this view.
Definition: QuaternionAdapter.hpp:256
bool isEmpty() const
Tells whether the view is empty (always false, the view is fixed-size with 4 components).
Definition: QuaternionAdapter.hpp:202
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:295
std::conditional< std::is_const< Q >::value, typename Q::ConstReference, typename Q::Reference >::type Reference
Mutable element reference type (degrades to ConstReference when the wrapped quaternion is const).
Definition: QuaternionAdapter.hpp:87
QuaternionVectorAdapter & operator-=(const VectorExpression< E > &e)
Subtracts the vector expression e componentwise from this view.
Definition: QuaternionAdapter.hpp:269
std::size_t SizeType
The size type (std::size_t).
Definition: QuaternionAdapter.hpp:65
void swap(QuaternionVectorAdapter &a)
Swaps the components of this view with those of a.
Definition: QuaternionAdapter.hpp:345
QuaternionVectorAdapter & plusAssign(const VectorExpression< E > &e)
Adds the vector expression e to this view without intermediate temporary.
Definition: QuaternionAdapter.hpp:322
SelfType ClosureType
Closure type used when this adapter appears inside another expression.
Definition: QuaternionAdapter.hpp:104
std::ptrdiff_t DifferenceType
The signed difference type (std::ptrdiff_t).
Definition: QuaternionAdapter.hpp:70
Reference operator[](SizeType i)
Returns a mutable reference to the quaternion component at index i.
Definition: QuaternionAdapter.hpp:173
ConstReference operator()(SizeType i) const
Returns a const reference to the quaternion component at index i.
Definition: QuaternionAdapter.hpp:146
QuaternionVectorAdapter & operator=(const QuaternionVectorAdapter &a)
Copies the components of a into this view (writing through to the wrapped quaternion).
Definition: QuaternionAdapter.hpp:230
Q::ValueType ValueType
The element value type of the wrapped quaternion.
Definition: QuaternionAdapter.hpp:75
const QuaternionClosureType & getData() const
Returns a const reference to the wrapped quaternion (via its stored closure).
Definition: QuaternionAdapter.hpp:220
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:282
Q QuaternionType
The wrapped quaternion type.
Definition: QuaternionAdapter.hpp:60
QuaternionVectorAdapter & operator=(const VectorExpression< E > &e)
Assigns the vector expression e to this view (writing through to the wrapped quaternion).
Definition: QuaternionAdapter.hpp:243
CRTP base class of all vector expression types.
Definition: Expression.hpp:68
const ExpressionType & operator()() const
Returns a const reference to the derived vector expression class instance.
Definition: Expression.hpp:80
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:404
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
The Math::CVector specialization used as the temporary vector type.
Definition: QuaternionAdapter.hpp:379
CVector< typename Q::ValueType, 4 > Type
The Math::CVector specialization used as the temporary vector type.
Definition: QuaternionAdapter.hpp:393
Selects a concrete temporary vector type compatible with the vector expression V.
Definition: TypeTraits.hpp:323
V::VectorTemporaryType Type
The concrete temporary vector type compatible with the vector expression V.
Definition: TypeTraits.hpp:328