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 
139  {
140  if (i == data.getSize())
141  return extElem;
142 
143  return data(i);
144  }
145 
152  {
153  if (i == data.getSize())
154  return extElem;
155 
156  return data[i];
157  }
158 
165  {
166  if (i == data.getSize())
167  return extElem;
168 
169  return data[i];
170  }
171 
177  {
178  return (data.getSize() + SizeType(1));
179  }
180 
185  bool isEmpty() const
186  {
187  return false;
188  }
189 
195  {
196  return data;
197  }
198 
203  const VectorClosureType& getData() const
204  {
205  return data;
206  }
207 
214  {
215  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(va));
216  return *this;
217  }
218 
225  template <typename E>
227  {
228  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(e));
229  return *this;
230  }
231 
238  template <typename E>
240  {
241  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(*this + e));
242  return *this;
243  }
244 
251  template <typename E>
253  {
254  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(*this - e));
255  return *this;
256  }
257 
264  template <typename T>
265  typename std::enable_if<IsScalar<T>::value, HomogenousCoordsAdapter>::type& operator*=(const T& t)
266  {
267  vectorAssignScalar<ScalarMultiplicationAssignment>(*this, t);
268  return *this;
269  }
270 
277  template <typename T>
278  typename std::enable_if<IsScalar<T>::value, HomogenousCoordsAdapter>::type& operator/=(const T& t)
279  {
280  vectorAssignScalar<ScalarDivisionAssignment>(*this, t);
281  return *this;
282  }
283 
290  template <typename E>
292  {
293  vectorAssignVector<ScalarAssignment>(*this, e);
294  return *this;
295  }
296 
303  template <typename E>
305  {
306  vectorAssignVector<ScalarAdditionAssignment>(*this, e);
307  return *this;
308  }
309 
316  template <typename E>
318  {
319  vectorAssignVector<ScalarSubtractionAssignment>(*this, e);
320  return *this;
321  }
322 
328  {
329  if (this != &va)
330  vectorSwap(*this, va);
331  }
332 
339  {
340  va1.swap(va2);
341  }
342 
343  private:
344  VectorClosureType data;
345  ValueType extElem;
346  };
347 
352  template <typename V>
353  class VectorQuaternionAdapter : public QuaternionExpression<VectorQuaternionAdapter<V> >
354  {
355 
357 
358  public:
362  typedef V VectorType;
363 
367  typedef typename V::ValueType ValueType;
368 
372  typedef typename V::ConstReference ConstReference;
373 
377  typedef typename std::conditional<std::is_const<V>::value,
378  typename V::ConstReference,
379  typename V::Reference>::type Reference;
380 
384  typedef typename std::conditional<std::is_const<V>::value,
385  typename V::ConstClosureType,
386  typename V::ClosureType>::type VectorClosureType;
387 
391  typedef const SelfType ConstClosureType;
392 
397 
403  data(v) {}
404 
410  {
411  return data(0);
412  }
413 
419  {
420  return data(1);
421  }
422 
428  {
429  return data(2);
430  }
431 
437  {
438  return data(3);
439  }
440 
446  {
447  return data(0);
448  }
449 
455  {
456  return data(1);
457  }
458 
464  {
465  return data(2);
466  }
467 
473  {
474  return data(3);
475  }
476 
482  {
483  return data;
484  }
485 
490  const VectorClosureType& getData() const
491  {
492  return data;
493  }
494 
501  {
502  quaternionAssignQuaternion<ScalarAssignment>(*this, typename QuaternionTemporaryTraits<SelfType>::Type(va));
503  return *this;
504  }
505 
512  template <typename E>
514  {
515  quaternionAssignQuaternion<ScalarAssignment>(*this, typename QuaternionTemporaryTraits<SelfType>::Type(e));
516  return *this;
517  }
518 
525  template <typename T>
526  typename std::enable_if<IsScalar<T>::value, VectorQuaternionAdapter>::type&
527  operator=(const T& t)
528  {
529  data(0) = t;
530  data(1) = ValueType();
531  data(2) = ValueType();
532  data(3) = ValueType();
533  return *this;
534  }
535 
542  template <typename E>
544  {
545  quaternionAssignQuaternion<ScalarAssignment>(*this, typename QuaternionTemporaryTraits<SelfType>::Type(*this + e));
546  return *this;
547  }
548 
555  template <typename T>
556  typename std::enable_if<IsScalar<T>::value, VectorQuaternionAdapter>::type&
557  operator+=(const T& t)
558  {
559  data(0) += t;
560  return *this;
561  }
562 
569  template <typename E>
571  {
572  quaternionAssignQuaternion<ScalarAssignment>(*this, typename QuaternionTemporaryTraits<SelfType>::Type(*this - e));
573  return *this;
574  }
575 
582  template <typename T>
583  typename std::enable_if<IsScalar<T>::value, VectorQuaternionAdapter>::type&
584  operator-=(const T& t)
585  {
586  data(0) -= t;
587  return *this;
588  }
589 
596  template <typename E>
598  {
599  quaternionAssignQuaternion<ScalarAssignment>(*this, typename QuaternionTemporaryTraits<SelfType>::Type(*this * e));
600  return *this;
601  }
602 
609  template <typename T>
610  typename std::enable_if<IsScalar<T>::value, VectorQuaternionAdapter>::type&
611  operator*=(const T& t)
612  {
613  quaternionAssignScalar<ScalarMultiplicationAssignment>(*this, t);
614  return *this;
615  }
616 
623  template <typename E>
625  {
626  quaternionAssignQuaternion<ScalarAssignment>(*this, typename QuaternionTemporaryTraits<SelfType>::Type(*this / e));
627  return *this;
628  }
629 
636  template <typename T>
637  typename std::enable_if<IsScalar<T>::value, VectorQuaternionAdapter>::type&
638  operator/=(const T& t)
639  {
640  quaternionAssignScalar<ScalarDivisionAssignment>(*this, t);
641  return *this;
642  }
643 
651  template <typename E>
653  {
654  quaternionAssignQuaternion<ScalarAssignment>(*this, e);
655  return *this;
656  }
657 
664  template <typename E>
666  {
667  quaternionAssignQuaternion<ScalarAdditionAssignment>(*this, e);
668  return *this;
669  }
670 
677  template <typename E>
679  {
680  quaternionAssignQuaternion<ScalarSubtractionAssignment>(*this, e);
681  return *this;
682  }
683 
689  {
690  if (this != &va)
691  quaternionSwap(*this, va);
692  }
693 
700  {
701  va1.swap(va2);
702  }
703 
711  void set(const ValueType& c1 = ValueType(), const ValueType& c2 = ValueType(),
712  const ValueType& c3 = ValueType(), const ValueType& c4 = ValueType())
713  {
714  data(0) = c1;
715  data(1) = c2;
716  data(2) = c3;
717  data(3) = c4;
718  }
719 
720  private:
721  VectorClosureType data;
722  };
723 
724  template <typename T>
725  class Quaternion;
726 
731  template <typename V>
733  {
734 
739  };
740 
745  template <typename V>
747  {
748 
753  };
754 
759  template <typename V>
761  {};
762 
767  template <typename V>
769  {};
770 
777  template <typename E>
780  {
781  return VectorQuaternionAdapter<E>(me());
782  }
783 
790  template <typename E>
791  VectorQuaternionAdapter<const E>
793  {
795  }
796 
803  template <typename E>
804  HomogenousCoordsAdapter<E>
806  {
807  return HomogenousCoordsAdapter<E>(me());
808  }
809 
816  template <typename E>
817  HomogenousCoordsAdapter<const E>
819  {
821  }
822  } // namespace Math
823 } // namespace CDPL
824 
825 #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:265
ConstReference operator[](SizeType i) const
Returns a const reference to the element at index i of the view.
Definition: VectorAdapter.hpp:164
HomogenousCoordsAdapter & minusAssign(const VectorExpression< E > &e)
Subtracts the vector expression e from this view without intermediate temporary.
Definition: VectorAdapter.hpp:317
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:151
const VectorClosureType & getData() const
Returns a const reference to the wrapped vector (via its stored closure).
Definition: VectorAdapter.hpp:203
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:213
bool isEmpty() const
Tells whether the view is empty (always false).
Definition: VectorAdapter.hpp:185
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:239
HomogenousCoordsAdapter & operator-=(const VectorExpression< E > &e)
Subtracts the vector expression e componentwise from this view.
Definition: VectorAdapter.hpp:252
SizeType getSize() const
Returns the dimensionality of the view (wrapped vector size + 1).
Definition: VectorAdapter.hpp:176
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:226
void swap(HomogenousCoordsAdapter &va)
Swaps the contents of this view with those of va.
Definition: VectorAdapter.hpp:327
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:278
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:138
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:304
VectorClosureType & getData()
Returns a reference to the wrapped vector (via its stored closure).
Definition: VectorAdapter.hpp:194
friend void swap(HomogenousCoordsAdapter &va1, HomogenousCoordsAdapter &va2)
ADL-enabled free-function form of swap().
Definition: VectorAdapter.hpp:338
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:291
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:354
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:652
Reference getC4()
Returns a mutable reference to the imaginary component C4.
Definition: VectorAdapter.hpp:436
Reference getC3()
Returns a mutable reference to the imaginary component C3.
Definition: VectorAdapter.hpp:427
VectorClosureType & getData()
Returns a reference to the wrapped vector (via its stored closure).
Definition: VectorAdapter.hpp:481
V VectorType
The wrapped 4-element vector type.
Definition: VectorAdapter.hpp:362
ConstReference getC4() const
Returns a const reference to the imaginary component C4.
Definition: VectorAdapter.hpp:472
ConstReference getC1() const
Returns a const reference to the real component C1.
Definition: VectorAdapter.hpp:445
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:584
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:386
V::ValueType ValueType
The element value type.
Definition: VectorAdapter.hpp:367
ConstReference getC2() const
Returns a const reference to the imaginary component C2.
Definition: VectorAdapter.hpp:454
VectorQuaternionAdapter & plusAssign(const QuaternionExpression< E > &e)
Adds the quaternion expression e to this view without intermediate temporary.
Definition: VectorAdapter.hpp:665
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:557
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:711
ConstReference getC3() const
Returns a const reference to the imaginary component C3.
Definition: VectorAdapter.hpp:463
VectorQuaternionAdapter & operator+=(const QuaternionExpression< E > &e)
Adds the quaternion expression e componentwise to this view.
Definition: VectorAdapter.hpp:543
const VectorClosureType & getData() const
Returns a const reference to the wrapped vector (via its stored closure).
Definition: VectorAdapter.hpp:490
Reference getC1()
Returns a mutable reference to the real component C1.
Definition: VectorAdapter.hpp:409
SelfType ClosureType
Closure type used when this adapter appears inside another expression.
Definition: VectorAdapter.hpp:396
VectorQuaternionAdapter & operator*=(const QuaternionExpression< E > &e)
Multiplies this view by the quaternion expression e (Hamilton-product semantics).
Definition: VectorAdapter.hpp:597
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:379
friend void swap(VectorQuaternionAdapter &va1, VectorQuaternionAdapter &va2)
ADL-enabled free-function form of swap().
Definition: VectorAdapter.hpp:699
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:527
void swap(VectorQuaternionAdapter &va)
Swaps the components of this view with those of va.
Definition: VectorAdapter.hpp:688
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:638
VectorQuaternionAdapter(VectorType &v)
Constructs the adapter wrapping the 4-element vector v.
Definition: VectorAdapter.hpp:402
V::ConstReference ConstReference
Constant component reference type.
Definition: VectorAdapter.hpp:372
Reference getC2()
Returns a mutable reference to the imaginary component C2.
Definition: VectorAdapter.hpp:418
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:611
VectorQuaternionAdapter & operator=(const VectorQuaternionAdapter &va)
Copies the components of va into this view (writing through to the wrapped vector).
Definition: VectorAdapter.hpp:500
const SelfType ConstClosureType
Constant closure type used when this adapter appears inside another expression.
Definition: VectorAdapter.hpp:391
VectorQuaternionAdapter & minusAssign(const QuaternionExpression< E > &e)
Subtracts the quaternion expression e from this view without intermediate temporary.
Definition: VectorAdapter.hpp:678
VectorQuaternionAdapter & operator=(const QuaternionExpression< E > &e)
Assigns the quaternion expression e to this view (writing through to the wrapped vector).
Definition: VectorAdapter.hpp:513
VectorQuaternionAdapter & operator-=(const QuaternionExpression< E > &e)
Subtracts the quaternion expression e componentwise from this view.
Definition: VectorAdapter.hpp:570
VectorQuaternionAdapter & operator/=(const QuaternionExpression< E > &e)
Divides this view by the quaternion expression e (Hamilton-quotient semantics).
Definition: VectorAdapter.hpp:624
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 > &me)
Returns a mutable vector adapter that exposes me as its homogeneous-coordinate extension.
Definition: VectorAdapter.hpp:805
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:738
Quaternion< typename V::ValueType > Type
The Math::Quaternion specialization used as the temporary quaternion type.
Definition: VectorAdapter.hpp:752
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