Chemical Data Processing Library C++ API - Version 1.4.0
VectorAdapter.hpp
Go to the documentation of this file.
1 /*
2  * VectorAdapter.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_VECTORADAPTER_HPP
28 #define CDPL_MATH_VECTORADAPTER_HPP
29 
30 #include <type_traits>
31 
32 #include "CDPL/Math/Expression.hpp"
35 #include "CDPL/Math/TypeTraits.hpp"
36 #include "CDPL/Math/Functional.hpp"
37 
38 
39 namespace CDPL
40 {
41 
42  namespace Math
43  {
44 
49  template <typename V>
50  class HomogenousCoordsAdapter : public VectorExpression<HomogenousCoordsAdapter<V> >
51  {
52 
54 
55  public:
57  typedef V VectorType;
59  typedef typename V::SizeType SizeType;
61  typedef typename V::DifferenceType DifferenceType;
63  typedef typename V::ValueType ValueType;
65  typedef typename V::ConstReference ConstReference;
67  typedef typename std::conditional<std::is_const<V>::value,
68  typename V::ConstReference,
69  typename V::Reference>::type Reference;
71  typedef typename std::conditional<std::is_const<V>::value,
72  typename V::ConstClosureType,
73  typename V::ClosureType>::type VectorClosureType;
75  typedef const SelfType ConstClosureType;
78 
84  data(v), extElem(1) {}
85 
96  {
97  if (i == data.getSize())
98  return extElem;
99 
100  return data(i);
101  }
102 
109  {
110  if (i == data.getSize())
111  return extElem;
112 
113  return data(i);
114  }
115 
122  {
123  if (i == data.getSize())
124  return extElem;
125 
126  return data[i];
127  }
128 
135  {
136  if (i == data.getSize())
137  return extElem;
138 
139  return data[i];
140  }
141 
147  {
148  return (data.getSize() + SizeType(1));
149  }
150 
155  bool isEmpty() const
156  {
157  return false;
158  }
159 
165  {
166  return data;
167  }
168 
173  const VectorClosureType& getData() const
174  {
175  return data;
176  }
177 
184  {
185  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(va));
186  return *this;
187  }
188 
195  template <typename E>
197  {
198  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(e));
199  return *this;
200  }
201 
208  template <typename E>
210  {
211  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(*this + e));
212  return *this;
213  }
214 
221  template <typename E>
223  {
224  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(*this - e));
225  return *this;
226  }
227 
234  template <typename T>
235  typename std::enable_if<IsScalar<T>::value, HomogenousCoordsAdapter>::type& operator*=(const T& t)
236  {
237  vectorAssignScalar<ScalarMultiplicationAssignment>(*this, t);
238  return *this;
239  }
240 
247  template <typename T>
248  typename std::enable_if<IsScalar<T>::value, HomogenousCoordsAdapter>::type& operator/=(const T& t)
249  {
250  vectorAssignScalar<ScalarDivisionAssignment>(*this, t);
251  return *this;
252  }
253 
260  template <typename E>
262  {
263  vectorAssignVector<ScalarAssignment>(*this, e);
264  return *this;
265  }
266 
273  template <typename E>
275  {
276  vectorAssignVector<ScalarAdditionAssignment>(*this, e);
277  return *this;
278  }
279 
286  template <typename E>
288  {
289  vectorAssignVector<ScalarSubtractionAssignment>(*this, e);
290  return *this;
291  }
292 
298  {
299  if (this != &va)
300  vectorSwap(*this, va);
301  }
302 
309  {
310  va1.swap(va2);
311  }
312 
313  private:
314  VectorClosureType data;
315  ValueType extElem;
316  };
317 
322  template <typename V>
323  class VectorQuaternionAdapter : public QuaternionExpression<VectorQuaternionAdapter<V> >
324  {
325 
327 
328  public:
330  typedef V VectorType;
332  typedef typename V::ValueType ValueType;
334  typedef typename V::ConstReference ConstReference;
336  typedef typename std::conditional<std::is_const<V>::value,
337  typename V::ConstReference,
338  typename V::Reference>::type Reference;
340  typedef typename std::conditional<std::is_const<V>::value,
341  typename V::ConstClosureType,
342  typename V::ClosureType>::type VectorClosureType;
344  typedef const SelfType ConstClosureType;
347 
353  data(v) {}
354 
360  {
361  return data(0);
362  }
363 
369  {
370  return data(1);
371  }
372 
378  {
379  return data(2);
380  }
381 
387  {
388  return data(3);
389  }
390 
396  {
397  return data(0);
398  }
399 
405  {
406  return data(1);
407  }
408 
414  {
415  return data(2);
416  }
417 
423  {
424  return data(3);
425  }
426 
432  {
433  return data;
434  }
435 
440  const VectorClosureType& getData() const
441  {
442  return data;
443  }
444 
451  {
452  quaternionAssignQuaternion<ScalarAssignment>(*this, typename QuaternionTemporaryTraits<SelfType>::Type(a));
453  return *this;
454  }
455 
462  template <typename E>
464  {
465  quaternionAssignQuaternion<ScalarAssignment>(*this, typename QuaternionTemporaryTraits<SelfType>::Type(e));
466  return *this;
467  }
468 
475  template <typename T>
476  typename std::enable_if<IsScalar<T>::value, VectorQuaternionAdapter>::type&
477  operator=(const T& t)
478  {
479  data(0) = t;
480  data(1) = ValueType();
481  data(2) = ValueType();
482  data(3) = ValueType();
483  return *this;
484  }
485 
492  template <typename E>
494  {
495  quaternionAssignQuaternion<ScalarAssignment>(*this, typename QuaternionTemporaryTraits<SelfType>::Type(*this + e));
496  return *this;
497  }
498 
505  template <typename T>
506  typename std::enable_if<IsScalar<T>::value, VectorQuaternionAdapter>::type&
507  operator+=(const T& t)
508  {
509  data(0) += t;
510  return *this;
511  }
512 
519  template <typename E>
521  {
522  quaternionAssignQuaternion<ScalarAssignment>(*this, typename QuaternionTemporaryTraits<SelfType>::Type(*this - e));
523  return *this;
524  }
525 
532  template <typename T>
533  typename std::enable_if<IsScalar<T>::value, VectorQuaternionAdapter>::type&
534  operator-=(const T& t)
535  {
536  data(0) -= t;
537  return *this;
538  }
539 
546  template <typename E>
548  {
549  quaternionAssignQuaternion<ScalarAssignment>(*this, typename QuaternionTemporaryTraits<SelfType>::Type(*this * e));
550  return *this;
551  }
552 
559  template <typename T>
560  typename std::enable_if<IsScalar<T>::value, VectorQuaternionAdapter>::type&
561  operator*=(const T& t)
562  {
563  quaternionAssignScalar<ScalarMultiplicationAssignment>(*this, t);
564  return *this;
565  }
566 
573  template <typename E>
575  {
576  quaternionAssignQuaternion<ScalarAssignment>(*this, typename QuaternionTemporaryTraits<SelfType>::Type(*this / e));
577  return *this;
578  }
579 
586  template <typename T>
587  typename std::enable_if<IsScalar<T>::value, VectorQuaternionAdapter>::type&
588  operator/=(const T& t)
589  {
590  quaternionAssignScalar<ScalarDivisionAssignment>(*this, t);
591  return *this;
592  }
593 
601  template <typename E>
603  {
604  quaternionAssignQuaternion<ScalarAssignment>(*this, e);
605  return *this;
606  }
607 
614  template <typename E>
616  {
617  quaternionAssignQuaternion<ScalarAdditionAssignment>(*this, e);
618  return *this;
619  }
620 
627  template <typename E>
629  {
630  quaternionAssignQuaternion<ScalarSubtractionAssignment>(*this, e);
631  return *this;
632  }
633 
639  {
640  if (this != &a)
641  quaternionSwap(*this, a);
642  }
643 
650  {
651  a1.swap(a2);
652  }
653 
661  void set(const ValueType& c1 = ValueType(), const ValueType& c2 = ValueType(),
662  const ValueType& c3 = ValueType(), const ValueType& c4 = ValueType())
663  {
664  data(0) = c1;
665  data(1) = c2;
666  data(2) = c3;
667  data(3) = c4;
668  }
669 
670  private:
671  VectorClosureType data;
672  };
673 
674  template <typename T>
675  class Quaternion;
676 
681  template <typename V>
683  {
684 
687  };
688 
693  template <typename V>
695  {
696 
699  };
700 
705  template <typename V>
707  {};
708 
713  template <typename V>
715  {};
716 
723  template <typename E>
726  {
727  return VectorQuaternionAdapter<E>(e());
728  }
729 
736  template <typename E>
737  VectorQuaternionAdapter<const E>
739  {
741  }
742 
749  template <typename E>
750  HomogenousCoordsAdapter<E>
752  {
753  return HomogenousCoordsAdapter<E>(e());
754  }
755 
762  template <typename E>
763  HomogenousCoordsAdapter<const E>
765  {
767  }
768  } // namespace Math
769 } // namespace CDPL
770 
771 #endif // CDPL_MATH_VECTORADAPTER_HPP
Definition of basic expression types.
Definition of various functors.
Implementation of quaternion assignment routines.
Definition of type traits.
Implementation of vector assignment routines.
Vector expression that exposes a vector V as its homogeneous-coordinate extension by appending an imp...
Definition: VectorAdapter.hpp:51
const SelfType ConstClosureType
Constant closure type used when this adapter appears inside another expression.
Definition: VectorAdapter.hpp:75
std::enable_if< IsScalar< T >::value, HomogenousCoordsAdapter >::type & operator*=(const T &t)
Multiplies every element of this view by the scalar t.
Definition: VectorAdapter.hpp:235
ConstReference operator[](SizeType i) const
Returns a const reference to the element at index i of the view (alias for operator()).
Definition: VectorAdapter.hpp:134
HomogenousCoordsAdapter & minusAssign(const VectorExpression< E > &e)
Subtracts the vector expression e from this view without intermediate temporary.
Definition: VectorAdapter.hpp:287
V VectorType
The wrapped vector type.
Definition: VectorAdapter.hpp:57
SelfType ClosureType
Closure type used when this adapter appears inside another expression.
Definition: VectorAdapter.hpp:77
std::conditional< std::is_const< V >::value, typename V::ConstReference, typename V::Reference >::type Reference
Mutable reference type (degrades to ConstReference when the wrapped vector is const).
Definition: VectorAdapter.hpp:69
Reference operator()(SizeType i)
Returns a mutable reference to the element at index i of the view.
Definition: VectorAdapter.hpp:95
Reference operator[](SizeType i)
Returns a mutable reference to the element at index i of the view (alias for operator()).
Definition: VectorAdapter.hpp:121
const VectorClosureType & getData() const
Returns a const reference to the wrapped vector (via its stored closure).
Definition: VectorAdapter.hpp:173
HomogenousCoordsAdapter & operator=(const HomogenousCoordsAdapter &va)
Copies the elements of va into this view (writing through to the wrapped vector and to the extension ...
Definition: VectorAdapter.hpp:183
bool isEmpty() const
Tells whether the view is empty (always false; the view always has at least the extension element).
Definition: VectorAdapter.hpp:155
std::conditional< std::is_const< V >::value, typename V::ConstClosureType, typename V::ClosureType >::type VectorClosureType
Closure type used to store the wrapped vector internally (mutable or const flavor).
Definition: VectorAdapter.hpp:73
HomogenousCoordsAdapter & operator+=(const VectorExpression< E > &e)
Adds the vector expression e componentwise to this view.
Definition: VectorAdapter.hpp:209
HomogenousCoordsAdapter & operator-=(const VectorExpression< E > &e)
Subtracts the vector expression e componentwise from this view.
Definition: VectorAdapter.hpp:222
SizeType getSize() const
Returns the dimensionality of the view (wrapped vector size + 1).
Definition: VectorAdapter.hpp:146
HomogenousCoordsAdapter(VectorType &v)
Constructs the adapter wrapping v.
Definition: VectorAdapter.hpp:83
HomogenousCoordsAdapter & operator=(const VectorExpression< E > &e)
Assigns the vector expression e to this view (writing through to the wrapped vector).
Definition: VectorAdapter.hpp:196
void swap(HomogenousCoordsAdapter &va)
Swaps the contents of this view with those of va.
Definition: VectorAdapter.hpp:297
V::ValueType ValueType
The element value type of the wrapped vector.
Definition: VectorAdapter.hpp:63
std::enable_if< IsScalar< T >::value, HomogenousCoordsAdapter >::type & operator/=(const T &t)
Divides every element of this view by the scalar t.
Definition: VectorAdapter.hpp:248
V::ConstReference ConstReference
Constant reference type to an element.
Definition: VectorAdapter.hpp:65
ConstReference operator()(SizeType i) const
Returns a const reference to the element at index i of the view.
Definition: VectorAdapter.hpp:108
V::DifferenceType DifferenceType
The signed difference type used by the wrapped vector.
Definition: VectorAdapter.hpp:61
HomogenousCoordsAdapter & plusAssign(const VectorExpression< E > &e)
Adds the vector expression e to this view without intermediate temporary.
Definition: VectorAdapter.hpp:274
VectorClosureType & getData()
Returns a reference to the wrapped vector (via its stored closure).
Definition: VectorAdapter.hpp:164
friend void swap(HomogenousCoordsAdapter &va1, HomogenousCoordsAdapter &va2)
ADL-enabled free-function form of swap().
Definition: VectorAdapter.hpp:308
V::SizeType SizeType
The size type used by the wrapped vector.
Definition: VectorAdapter.hpp:59
HomogenousCoordsAdapter & assign(const VectorExpression< E > &e)
Assigns the vector expression e to this view without intermediate temporary.
Definition: VectorAdapter.hpp:261
CRTP base class for all quaternion expression types.
Definition: Expression.hpp:142
General 4-component quaternion .
Definition: Quaternion.hpp:396
CRTP base class for all vector expression types.
Definition: Expression.hpp:66
Quaternion expression that exposes a 4-element vector as a quaternion (component indices 0-3 map to C...
Definition: VectorAdapter.hpp:324
VectorQuaternionAdapter & assign(const QuaternionExpression< E > &e)
Assigns the quaternion expression e to this view without intermediate temporary (use only when e does...
Definition: VectorAdapter.hpp:602
Reference getC4()
Returns a mutable reference to component C4 (mapped to element 3 of the wrapped vector).
Definition: VectorAdapter.hpp:386
Reference getC3()
Returns a mutable reference to component C3 (mapped to element 2 of the wrapped vector).
Definition: VectorAdapter.hpp:377
VectorClosureType & getData()
Returns a reference to the wrapped vector (via its stored closure).
Definition: VectorAdapter.hpp:431
V VectorType
The wrapped 4-element vector type.
Definition: VectorAdapter.hpp:330
ConstReference getC4() const
Returns a const reference to component C4.
Definition: VectorAdapter.hpp:422
ConstReference getC1() const
Returns a const reference to component C1.
Definition: VectorAdapter.hpp:395
std::enable_if< IsScalar< T >::value, VectorQuaternionAdapter >::type & operator-=(const T &t)
Subtracts the scalar t from component C1 of this view.
Definition: VectorAdapter.hpp:534
std::conditional< std::is_const< V >::value, typename V::ConstClosureType, typename V::ClosureType >::type VectorClosureType
Closure type used to store the wrapped vector internally.
Definition: VectorAdapter.hpp:342
V::ValueType ValueType
The element value type.
Definition: VectorAdapter.hpp:332
ConstReference getC2() const
Returns a const reference to component C2.
Definition: VectorAdapter.hpp:404
VectorQuaternionAdapter & plusAssign(const QuaternionExpression< E > &e)
Adds the quaternion expression e to this view without intermediate temporary.
Definition: VectorAdapter.hpp:615
std::enable_if< IsScalar< T >::value, VectorQuaternionAdapter >::type & operator+=(const T &t)
Adds the scalar t to component C1 of this view.
Definition: VectorAdapter.hpp:507
void set(const ValueType &c1=ValueType(), const ValueType &c2=ValueType(), const ValueType &c3=ValueType(), const ValueType &c4=ValueType())
Assigns the four supplied component values to this view (writing through to the wrapped vector).
Definition: VectorAdapter.hpp:661
ConstReference getC3() const
Returns a const reference to component C3.
Definition: VectorAdapter.hpp:413
VectorQuaternionAdapter & operator+=(const QuaternionExpression< E > &e)
Adds the quaternion expression e componentwise to this view.
Definition: VectorAdapter.hpp:493
const VectorClosureType & getData() const
Returns a const reference to the wrapped vector (via its stored closure).
Definition: VectorAdapter.hpp:440
Reference getC1()
Returns a mutable reference to component C1 (mapped to element 0 of the wrapped vector).
Definition: VectorAdapter.hpp:359
SelfType ClosureType
Closure type used when this adapter appears inside another expression.
Definition: VectorAdapter.hpp:346
VectorQuaternionAdapter & operator*=(const QuaternionExpression< E > &e)
Multiplies this view by the quaternion expression e (Hamilton-product semantics).
Definition: VectorAdapter.hpp:547
std::conditional< std::is_const< V >::value, typename V::ConstReference, typename V::Reference >::type Reference
Mutable reference type (degrades to ConstReference when the wrapped vector is const).
Definition: VectorAdapter.hpp:338
VectorQuaternionAdapter & operator=(const VectorQuaternionAdapter &a)
Copies the components of a into this view (writing through to the wrapped vector).
Definition: VectorAdapter.hpp:450
std::enable_if< IsScalar< T >::value, VectorQuaternionAdapter >::type & operator=(const T &t)
Assigns the scalar t to this view (sets C1 to t and the remaining components to the default value).
Definition: VectorAdapter.hpp:477
std::enable_if< IsScalar< T >::value, VectorQuaternionAdapter >::type & operator/=(const T &t)
Divides every component of this view by the scalar t.
Definition: VectorAdapter.hpp:588
VectorQuaternionAdapter(VectorType &v)
Constructs the adapter wrapping the 4-element vector v.
Definition: VectorAdapter.hpp:352
V::ConstReference ConstReference
Constant reference type to a component.
Definition: VectorAdapter.hpp:334
Reference getC2()
Returns a mutable reference to component C2 (mapped to element 1 of the wrapped vector).
Definition: VectorAdapter.hpp:368
std::enable_if< IsScalar< T >::value, VectorQuaternionAdapter >::type & operator*=(const T &t)
Multiplies every component of this view by the scalar t.
Definition: VectorAdapter.hpp:561
const SelfType ConstClosureType
Constant closure type used when this adapter appears inside another expression.
Definition: VectorAdapter.hpp:344
void swap(VectorQuaternionAdapter &a)
Swaps the components of this view with those of a.
Definition: VectorAdapter.hpp:638
VectorQuaternionAdapter & minusAssign(const QuaternionExpression< E > &e)
Subtracts the quaternion expression e from this view without intermediate temporary.
Definition: VectorAdapter.hpp:628
VectorQuaternionAdapter & operator=(const QuaternionExpression< E > &e)
Assigns the quaternion expression e to this view (writing through to the wrapped vector).
Definition: VectorAdapter.hpp:463
VectorQuaternionAdapter & operator-=(const QuaternionExpression< E > &e)
Subtracts the quaternion expression e componentwise from this view.
Definition: VectorAdapter.hpp:520
friend void swap(VectorQuaternionAdapter &a1, VectorQuaternionAdapter &a2)
ADL-enabled free-function form of swap().
Definition: VectorAdapter.hpp:649
VectorQuaternionAdapter & operator/=(const QuaternionExpression< E > &e)
Divides this view by the quaternion expression e (Hamilton-quotient semantics).
Definition: VectorAdapter.hpp:574
constexpr unsigned int V
Specifies Vanadium.
Definition: AtomType.hpp:177
constexpr unsigned int T
Specifies Hydrogen (Tritium).
Definition: AtomType.hpp:67
HomogenousCoordsAdapter< E > homog(VectorExpression< E > &e)
Creates a mutable Math::HomogenousCoordsAdapter view of the vector expression e (extends e by an impl...
Definition: VectorAdapter.hpp:751
void quaternionSwap(Q &q, QuaternionExpression< E > &e)
Swaps the components of two quaternion expressions component by component.
Definition: QuaternionAssignment.hpp:93
void vectorSwap(V &v, VectorExpression< E > &e)
Swaps the elements of two equally sized vector expressions element by element.
Definition: VectorAssignment.hpp:97
std::enable_if< IsScalar< T >::value, RealQuaternion< T > >::type quat(const T &t)
Constructs a Math::RealQuaternion from the scalar t (its real component).
Definition: Quaternion.hpp:1152
The namespace of the Chemical Data Processing Library.
Quaternion< typename V::ValueType > Type
The Math::Quaternion specialization used as the temporary quaternion type.
Definition: VectorAdapter.hpp:686
Quaternion< typename V::ValueType > Type
The Math::Quaternion specialization used as the temporary quaternion type.
Definition: VectorAdapter.hpp:698
Selects a concrete temporary quaternion type compatible with the quaternion expression Q.
Definition: TypeTraits.hpp:325
Q::QuaternionTemporaryType Type
The concrete temporary quaternion type compatible with the quaternion expression Q.
Definition: TypeTraits.hpp:328
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