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:
59  typedef V VectorType;
60 
64  typedef typename V::SizeType SizeType;
65 
69  typedef typename V::DifferenceType DifferenceType;
70 
74  typedef typename V::ValueType ValueType;
75 
79  typedef typename V::ConstReference ConstReference;
80 
84  typedef typename std::conditional<std::is_const<V>::value,
85  typename V::ConstReference,
86  typename V::Reference>::type Reference;
87 
91  typedef typename std::conditional<std::is_const<V>::value,
92  typename V::ConstClosureType,
93  typename V::ClosureType>::type VectorClosureType;
94 
98  typedef const SelfType ConstClosureType;
99 
104 
110  data(v), extElem(1) {}
111 
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 
148  {
149  if (i == data.getSize())
150  return extElem;
151 
152  return data[i];
153  }
154 
161  {
162  if (i == data.getSize())
163  return extElem;
164 
165  return data[i];
166  }
167 
173  {
174  return (data.getSize() + SizeType(1));
175  }
176 
181  bool isEmpty() const
182  {
183  return false;
184  }
185 
191  {
192  return data;
193  }
194 
199  const VectorClosureType& getData() const
200  {
201  return data;
202  }
203 
210  {
211  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(va));
212  return *this;
213  }
214 
221  template <typename E>
223  {
224  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(e));
225  return *this;
226  }
227 
234  template <typename E>
236  {
237  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(*this + e));
238  return *this;
239  }
240 
247  template <typename E>
249  {
250  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(*this - e));
251  return *this;
252  }
253 
260  template <typename T>
261  typename std::enable_if<IsScalar<T>::value, HomogenousCoordsAdapter>::type& operator*=(const T& t)
262  {
263  vectorAssignScalar<ScalarMultiplicationAssignment>(*this, t);
264  return *this;
265  }
266 
273  template <typename T>
274  typename std::enable_if<IsScalar<T>::value, HomogenousCoordsAdapter>::type& operator/=(const T& t)
275  {
276  vectorAssignScalar<ScalarDivisionAssignment>(*this, t);
277  return *this;
278  }
279 
286  template <typename E>
288  {
289  vectorAssignVector<ScalarAssignment>(*this, e);
290  return *this;
291  }
292 
299  template <typename E>
301  {
302  vectorAssignVector<ScalarAdditionAssignment>(*this, e);
303  return *this;
304  }
305 
312  template <typename E>
314  {
315  vectorAssignVector<ScalarSubtractionAssignment>(*this, e);
316  return *this;
317  }
318 
324  {
325  if (this != &va)
326  vectorSwap(*this, va);
327  }
328 
335  {
336  va1.swap(va2);
337  }
338 
339  private:
340  VectorClosureType data;
341  ValueType extElem;
342  };
343 
348  template <typename V>
349  class VectorQuaternionAdapter : public QuaternionExpression<VectorQuaternionAdapter<V> >
350  {
351 
353 
354  public:
358  typedef V VectorType;
359 
363  typedef typename V::ValueType ValueType;
364 
368  typedef typename V::ConstReference ConstReference;
369 
373  typedef typename std::conditional<std::is_const<V>::value,
374  typename V::ConstReference,
375  typename V::Reference>::type Reference;
376 
380  typedef typename std::conditional<std::is_const<V>::value,
381  typename V::ConstClosureType,
382  typename V::ClosureType>::type VectorClosureType;
383 
387  typedef const SelfType ConstClosureType;
388 
393 
399  data(v) {}
400 
406  {
407  return data(0);
408  }
409 
415  {
416  return data(1);
417  }
418 
424  {
425  return data(2);
426  }
427 
433  {
434  return data(3);
435  }
436 
442  {
443  return data(0);
444  }
445 
451  {
452  return data(1);
453  }
454 
460  {
461  return data(2);
462  }
463 
469  {
470  return data(3);
471  }
472 
478  {
479  return data;
480  }
481 
486  const VectorClosureType& getData() const
487  {
488  return data;
489  }
490 
497  {
498  quaternionAssignQuaternion<ScalarAssignment>(*this, typename QuaternionTemporaryTraits<SelfType>::Type(a));
499  return *this;
500  }
501 
508  template <typename E>
510  {
511  quaternionAssignQuaternion<ScalarAssignment>(*this, typename QuaternionTemporaryTraits<SelfType>::Type(e));
512  return *this;
513  }
514 
521  template <typename T>
522  typename std::enable_if<IsScalar<T>::value, VectorQuaternionAdapter>::type&
523  operator=(const T& t)
524  {
525  data(0) = t;
526  data(1) = ValueType();
527  data(2) = ValueType();
528  data(3) = ValueType();
529  return *this;
530  }
531 
538  template <typename E>
540  {
541  quaternionAssignQuaternion<ScalarAssignment>(*this, typename QuaternionTemporaryTraits<SelfType>::Type(*this + e));
542  return *this;
543  }
544 
551  template <typename T>
552  typename std::enable_if<IsScalar<T>::value, VectorQuaternionAdapter>::type&
553  operator+=(const T& t)
554  {
555  data(0) += t;
556  return *this;
557  }
558 
565  template <typename E>
567  {
568  quaternionAssignQuaternion<ScalarAssignment>(*this, typename QuaternionTemporaryTraits<SelfType>::Type(*this - e));
569  return *this;
570  }
571 
578  template <typename T>
579  typename std::enable_if<IsScalar<T>::value, VectorQuaternionAdapter>::type&
580  operator-=(const T& t)
581  {
582  data(0) -= t;
583  return *this;
584  }
585 
592  template <typename E>
594  {
595  quaternionAssignQuaternion<ScalarAssignment>(*this, typename QuaternionTemporaryTraits<SelfType>::Type(*this * e));
596  return *this;
597  }
598 
605  template <typename T>
606  typename std::enable_if<IsScalar<T>::value, VectorQuaternionAdapter>::type&
607  operator*=(const T& t)
608  {
609  quaternionAssignScalar<ScalarMultiplicationAssignment>(*this, t);
610  return *this;
611  }
612 
619  template <typename E>
621  {
622  quaternionAssignQuaternion<ScalarAssignment>(*this, typename QuaternionTemporaryTraits<SelfType>::Type(*this / e));
623  return *this;
624  }
625 
632  template <typename T>
633  typename std::enable_if<IsScalar<T>::value, VectorQuaternionAdapter>::type&
634  operator/=(const T& t)
635  {
636  quaternionAssignScalar<ScalarDivisionAssignment>(*this, t);
637  return *this;
638  }
639 
647  template <typename E>
649  {
650  quaternionAssignQuaternion<ScalarAssignment>(*this, e);
651  return *this;
652  }
653 
660  template <typename E>
662  {
663  quaternionAssignQuaternion<ScalarAdditionAssignment>(*this, e);
664  return *this;
665  }
666 
673  template <typename E>
675  {
676  quaternionAssignQuaternion<ScalarSubtractionAssignment>(*this, e);
677  return *this;
678  }
679 
685  {
686  if (this != &a)
687  quaternionSwap(*this, a);
688  }
689 
696  {
697  a1.swap(a2);
698  }
699 
707  void set(const ValueType& c1 = ValueType(), const ValueType& c2 = ValueType(),
708  const ValueType& c3 = ValueType(), const ValueType& c4 = ValueType())
709  {
710  data(0) = c1;
711  data(1) = c2;
712  data(2) = c3;
713  data(3) = c4;
714  }
715 
716  private:
717  VectorClosureType data;
718  };
719 
720  template <typename T>
721  class Quaternion;
722 
727  template <typename V>
729  {
730 
735  };
736 
741  template <typename V>
743  {
744 
749  };
750 
755  template <typename V>
757  {};
758 
763  template <typename V>
765  {};
766 
773  template <typename E>
776  {
777  return VectorQuaternionAdapter<E>(e());
778  }
779 
786  template <typename E>
787  VectorQuaternionAdapter<const E>
789  {
791  }
792 
799  template <typename E>
800  HomogenousCoordsAdapter<E>
802  {
803  return HomogenousCoordsAdapter<E>(e());
804  }
805 
812  template <typename E>
813  HomogenousCoordsAdapter<const E>
815  {
817  }
818  } // namespace Math
819 } // namespace CDPL
820 
821 #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:98
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:261
ConstReference operator[](SizeType i) const
Returns a const reference to the element at index i of the view.
Definition: VectorAdapter.hpp:160
HomogenousCoordsAdapter & minusAssign(const VectorExpression< E > &e)
Subtracts the vector expression e from this view without intermediate temporary.
Definition: VectorAdapter.hpp:313
V VectorType
The wrapped vector type.
Definition: VectorAdapter.hpp:59
SelfType ClosureType
Closure type used when this adapter appears inside another expression.
Definition: VectorAdapter.hpp:103
std::conditional< std::is_const< V >::value, typename V::ConstReference, typename V::Reference >::type Reference
Mutable element reference type (degrades to ConstReference when the wrapped vector is const).
Definition: VectorAdapter.hpp:86
Reference operator()(SizeType i)
Returns a mutable reference to the element at index i of the view.
Definition: VectorAdapter.hpp:121
Reference operator[](SizeType i)
Returns a mutable reference to the element at index i of the view.
Definition: VectorAdapter.hpp:147
const VectorClosureType & getData() const
Returns a const reference to the wrapped vector (via its stored closure).
Definition: VectorAdapter.hpp:199
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:209
bool isEmpty() const
Tells whether the view is empty (always false; the view always has at least the extension element).
Definition: VectorAdapter.hpp:181
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:93
HomogenousCoordsAdapter & operator+=(const VectorExpression< E > &e)
Adds the vector expression e componentwise to this view.
Definition: VectorAdapter.hpp:235
HomogenousCoordsAdapter & operator-=(const VectorExpression< E > &e)
Subtracts the vector expression e componentwise from this view.
Definition: VectorAdapter.hpp:248
SizeType getSize() const
Returns the dimensionality of the view (wrapped vector size + 1).
Definition: VectorAdapter.hpp:172
HomogenousCoordsAdapter(VectorType &v)
Constructs the adapter wrapping v.
Definition: VectorAdapter.hpp:109
HomogenousCoordsAdapter & operator=(const VectorExpression< E > &e)
Assigns the vector expression e to this view (writing through to the wrapped vector).
Definition: VectorAdapter.hpp:222
void swap(HomogenousCoordsAdapter &va)
Swaps the contents of this view with those of va.
Definition: VectorAdapter.hpp:323
V::ValueType ValueType
The element value type of the wrapped vector.
Definition: VectorAdapter.hpp:74
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:274
V::ConstReference ConstReference
Constant element reference type.
Definition: VectorAdapter.hpp:79
ConstReference operator()(SizeType i) const
Returns a const reference to the element at index i of the view.
Definition: VectorAdapter.hpp:134
V::DifferenceType DifferenceType
The signed difference type used by the wrapped vector.
Definition: VectorAdapter.hpp:69
HomogenousCoordsAdapter & plusAssign(const VectorExpression< E > &e)
Adds the vector expression e to this view without intermediate temporary.
Definition: VectorAdapter.hpp:300
VectorClosureType & getData()
Returns a reference to the wrapped vector (via its stored closure).
Definition: VectorAdapter.hpp:190
friend void swap(HomogenousCoordsAdapter &va1, HomogenousCoordsAdapter &va2)
ADL-enabled free-function form of swap().
Definition: VectorAdapter.hpp:334
V::SizeType SizeType
The size type used by the wrapped vector.
Definition: VectorAdapter.hpp:64
HomogenousCoordsAdapter & assign(const VectorExpression< E > &e)
Assigns the vector expression e to this view without intermediate temporary.
Definition: VectorAdapter.hpp:287
CRTP base class of all quaternion expression types.
Definition: Expression.hpp:148
General 4-component quaternion .
Definition: Quaternion.hpp:413
CRTP base class of all vector expression types.
Definition: Expression.hpp:68
Quaternion expression that exposes a 4-element vector as a quaternion (component indices 0-3 map to C...
Definition: VectorAdapter.hpp:350
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:648
Reference getC4()
Returns a mutable reference to component C4 (mapped to element 3 of the wrapped vector).
Definition: VectorAdapter.hpp:432
Reference getC3()
Returns a mutable reference to component C3 (mapped to element 2 of the wrapped vector).
Definition: VectorAdapter.hpp:423
VectorClosureType & getData()
Returns a reference to the wrapped vector (via its stored closure).
Definition: VectorAdapter.hpp:477
V VectorType
The wrapped 4-element vector type.
Definition: VectorAdapter.hpp:358
ConstReference getC4() const
Returns a const reference to component C4.
Definition: VectorAdapter.hpp:468
ConstReference getC1() const
Returns a const reference to component C1.
Definition: VectorAdapter.hpp:441
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:580
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:382
V::ValueType ValueType
The element value type.
Definition: VectorAdapter.hpp:363
ConstReference getC2() const
Returns a const reference to component C2.
Definition: VectorAdapter.hpp:450
VectorQuaternionAdapter & plusAssign(const QuaternionExpression< E > &e)
Adds the quaternion expression e to this view without intermediate temporary.
Definition: VectorAdapter.hpp:661
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:553
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:707
ConstReference getC3() const
Returns a const reference to component C3.
Definition: VectorAdapter.hpp:459
VectorQuaternionAdapter & operator+=(const QuaternionExpression< E > &e)
Adds the quaternion expression e componentwise to this view.
Definition: VectorAdapter.hpp:539
const VectorClosureType & getData() const
Returns a const reference to the wrapped vector (via its stored closure).
Definition: VectorAdapter.hpp:486
Reference getC1()
Returns a mutable reference to component C1 (mapped to element 0 of the wrapped vector).
Definition: VectorAdapter.hpp:405
SelfType ClosureType
Closure type used when this adapter appears inside another expression.
Definition: VectorAdapter.hpp:392
VectorQuaternionAdapter & operator*=(const QuaternionExpression< E > &e)
Multiplies this view by the quaternion expression e (Hamilton-product semantics).
Definition: VectorAdapter.hpp:593
std::conditional< std::is_const< V >::value, typename V::ConstReference, typename V::Reference >::type Reference
Mutable component reference type (degrades to ConstReference when the wrapped vector is const).
Definition: VectorAdapter.hpp:375
VectorQuaternionAdapter & operator=(const VectorQuaternionAdapter &a)
Copies the components of a into this view (writing through to the wrapped vector).
Definition: VectorAdapter.hpp:496
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:523
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:634
VectorQuaternionAdapter(VectorType &v)
Constructs the adapter wrapping the 4-element vector v.
Definition: VectorAdapter.hpp:398
V::ConstReference ConstReference
Constant component reference type.
Definition: VectorAdapter.hpp:368
Reference getC2()
Returns a mutable reference to component C2 (mapped to element 1 of the wrapped vector).
Definition: VectorAdapter.hpp:414
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:607
const SelfType ConstClosureType
Constant closure type used when this adapter appears inside another expression.
Definition: VectorAdapter.hpp:387
void swap(VectorQuaternionAdapter &a)
Swaps the components of this view with those of a.
Definition: VectorAdapter.hpp:684
VectorQuaternionAdapter & minusAssign(const QuaternionExpression< E > &e)
Subtracts the quaternion expression e from this view without intermediate temporary.
Definition: VectorAdapter.hpp:674
VectorQuaternionAdapter & operator=(const QuaternionExpression< E > &e)
Assigns the quaternion expression e to this view (writing through to the wrapped vector).
Definition: VectorAdapter.hpp:509
VectorQuaternionAdapter & operator-=(const QuaternionExpression< E > &e)
Subtracts the quaternion expression e componentwise from this view.
Definition: VectorAdapter.hpp:566
friend void swap(VectorQuaternionAdapter &a1, VectorQuaternionAdapter &a2)
ADL-enabled free-function form of swap().
Definition: VectorAdapter.hpp:695
VectorQuaternionAdapter & operator/=(const QuaternionExpression< E > &e)
Divides this view by the quaternion expression e (Hamilton-quotient semantics).
Definition: VectorAdapter.hpp:620
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:801
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:1212
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:734
Quaternion< typename V::ValueType > Type
The Math::Quaternion specialization used as the temporary quaternion type.
Definition: VectorAdapter.hpp:748
Selects a concrete temporary quaternion type compatible with the quaternion expression Q.
Definition: TypeTraits.hpp:351
Q::QuaternionTemporaryType Type
The concrete temporary quaternion type compatible with the quaternion expression Q.
Definition: TypeTraits.hpp:356
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