Chemical Data Processing Library C++ API - Version 1.2.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 
45  template <typename V>
46  class HomogenousCoordsAdapter : public VectorExpression<HomogenousCoordsAdapter<V> >
47  {
48 
50 
51  public:
52  typedef V VectorType;
53  typedef typename V::SizeType SizeType;
54  typedef typename V::DifferenceType DifferenceType;
55  typedef typename V::ValueType ValueType;
56  typedef typename V::ConstReference ConstReference;
57  typedef typename std::conditional<std::is_const<V>::value,
58  typename V::ConstReference,
59  typename V::Reference>::type Reference;
60  typedef typename std::conditional<std::is_const<V>::value,
61  typename V::ConstClosureType,
62  typename V::ClosureType>::type VectorClosureType;
63  typedef const SelfType ConstClosureType;
65 
67  data(v), extElem(1) {}
68 
70  {
71  if (i == data.getSize())
72  return extElem;
73 
74  return data(i);
75  }
76 
78  {
79  if (i == data.getSize())
80  return extElem;
81 
82  return data(i);
83  }
84 
86  {
87  if (i == data.getSize())
88  return extElem;
89 
90  return data[i];
91  }
92 
94  {
95  if (i == data.getSize())
96  return extElem;
97 
98  return data[i];
99  }
100 
102  {
103  return (data.getSize() + SizeType(1));
104  }
105 
106  bool isEmpty() const
107  {
108  return false;
109  }
110 
112  {
113  return data;
114  }
115 
116  const VectorClosureType& getData() const
117  {
118  return data;
119  }
120 
122  {
123  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(va));
124  return *this;
125  }
126 
127  template <typename E>
129  {
130  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(e));
131  return *this;
132  }
133 
134  template <typename E>
136  {
137  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(*this + e));
138  return *this;
139  }
140 
141  template <typename E>
143  {
144  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(*this - e));
145  return *this;
146  }
147 
148  template <typename T>
149  typename std::enable_if<IsScalar<T>::value, HomogenousCoordsAdapter>::type& operator*=(const T& t)
150  {
151  vectorAssignScalar<ScalarMultiplicationAssignment>(*this, t);
152  return *this;
153  }
154 
155  template <typename T>
156  typename std::enable_if<IsScalar<T>::value, HomogenousCoordsAdapter>::type& operator/=(const T& t)
157  {
158  vectorAssignScalar<ScalarDivisionAssignment>(*this, t);
159  return *this;
160  }
161 
162  template <typename E>
164  {
165  vectorAssignVector<ScalarAssignment>(*this, e);
166  return *this;
167  }
168 
169  template <typename E>
171  {
172  vectorAssignVector<ScalarAdditionAssignment>(*this, e);
173  return *this;
174  }
175 
176  template <typename E>
178  {
179  vectorAssignVector<ScalarSubtractionAssignment>(*this, e);
180  return *this;
181  }
182 
184  {
185  if (this != &va)
186  vectorSwap(*this, va);
187  }
188 
190  {
191  va1.swap(va2);
192  }
193 
194  private:
195  VectorClosureType data;
196  ValueType extElem;
197  };
198 
199  template <typename V>
200  class VectorQuaternionAdapter : public QuaternionExpression<VectorQuaternionAdapter<V> >
201  {
202 
204 
205  public:
206  typedef V VectorType;
207  typedef typename V::ValueType ValueType;
208  typedef typename V::ConstReference ConstReference;
209  typedef typename std::conditional<std::is_const<V>::value,
210  typename V::ConstReference,
211  typename V::Reference>::type Reference;
212  typedef typename std::conditional<std::is_const<V>::value,
213  typename V::ConstClosureType,
214  typename V::ClosureType>::type VectorClosureType;
215  typedef const SelfType ConstClosureType;
217 
219  data(v) {}
220 
222  {
223  return data(0);
224  }
225 
227  {
228  return data(1);
229  }
230 
232  {
233  return data(2);
234  }
235 
237  {
238  return data(3);
239  }
240 
242  {
243  return data(0);
244  }
245 
247  {
248  return data(1);
249  }
250 
252  {
253  return data(2);
254  }
255 
257  {
258  return data(3);
259  }
260 
262  {
263  return data;
264  }
265 
266  const VectorClosureType& getData() const
267  {
268  return data;
269  }
270 
272  {
273  quaternionAssignQuaternion<ScalarAssignment>(*this, typename QuaternionTemporaryTraits<SelfType>::Type(a));
274  return *this;
275  }
276 
277  template <typename E>
279  {
280  quaternionAssignQuaternion<ScalarAssignment>(*this, typename QuaternionTemporaryTraits<SelfType>::Type(e));
281  return *this;
282  }
283 
284  template <typename T>
285  typename std::enable_if<IsScalar<T>::value, VectorQuaternionAdapter>::type&
286  operator=(const T& t)
287  {
288  data(0) = t;
289  data(1) = ValueType();
290  data(2) = ValueType();
291  data(3) = ValueType();
292  return *this;
293  }
294 
295  template <typename E>
297  {
298  quaternionAssignQuaternion<ScalarAssignment>(*this, typename QuaternionTemporaryTraits<SelfType>::Type(*this + e));
299  return *this;
300  }
301 
302  template <typename T>
303  typename std::enable_if<IsScalar<T>::value, VectorQuaternionAdapter>::type&
304  operator+=(const T& t)
305  {
306  data(0) += t;
307  return *this;
308  }
309 
310  template <typename E>
312  {
313  quaternionAssignQuaternion<ScalarAssignment>(*this, typename QuaternionTemporaryTraits<SelfType>::Type(*this - e));
314  return *this;
315  }
316 
317  template <typename T>
318  typename std::enable_if<IsScalar<T>::value, VectorQuaternionAdapter>::type&
319  operator-=(const T& t)
320  {
321  data(0) -= t;
322  return *this;
323  }
324 
325  template <typename E>
327  {
328  quaternionAssignQuaternion<ScalarAssignment>(*this, typename QuaternionTemporaryTraits<SelfType>::Type(*this * e));
329  return *this;
330  }
331 
332  template <typename T>
333  typename std::enable_if<IsScalar<T>::value, VectorQuaternionAdapter>::type&
334  operator*=(const T& t)
335  {
336  quaternionAssignScalar<ScalarMultiplicationAssignment>(*this, t);
337  return *this;
338  }
339 
340  template <typename E>
342  {
343  quaternionAssignQuaternion<ScalarAssignment>(*this, typename QuaternionTemporaryTraits<SelfType>::Type(*this / e));
344  return *this;
345  }
346 
347  template <typename T>
348  typename std::enable_if<IsScalar<T>::value, VectorQuaternionAdapter>::type&
349  operator/=(const T& t)
350  {
351  quaternionAssignScalar<ScalarDivisionAssignment>(*this, t);
352  return *this;
353  }
354 
355  template <typename E>
357  {
358  quaternionAssignQuaternion<ScalarAssignment>(*this, e);
359  return *this;
360  }
361 
362  template <typename E>
364  {
365  quaternionAssignQuaternion<ScalarAdditionAssignment>(*this, e);
366  return *this;
367  }
368 
369  template <typename E>
371  {
372  quaternionAssignQuaternion<ScalarSubtractionAssignment>(*this, e);
373  return *this;
374  }
375 
377  {
378  if (this != &a)
379  quaternionSwap(*this, a);
380  }
381 
383  {
384  a1.swap(a2);
385  }
386 
387  void set(const ValueType& c1 = ValueType(), const ValueType& c2 = ValueType(),
388  const ValueType& c3 = ValueType(), const ValueType& c4 = ValueType())
389  {
390  data(0) = c1;
391  data(1) = c2;
392  data(2) = c3;
393  data(3) = c4;
394  }
395 
396  private:
397  VectorClosureType data;
398  };
399 
400  template <typename T>
401  class Quaternion;
402  template <typename V>
404  {
405 
407  };
408 
409  template <typename V>
411  {
412 
414  };
415 
416  template <typename V>
418  {};
419 
420  template <typename V>
422  {};
423 
424  template <typename E>
427  {
428  return VectorQuaternionAdapter<E>(e());
429  }
430 
431  template <typename E>
432  VectorQuaternionAdapter<const E>
434  {
436  }
437 
438  template <typename E>
439  HomogenousCoordsAdapter<E>
441  {
442  return HomogenousCoordsAdapter<E>(e());
443  }
444 
445  template <typename E>
446  HomogenousCoordsAdapter<const E>
448  {
450  }
451  } // namespace Math
452 } // namespace CDPL
453 
454 #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.
Definition: VectorAdapter.hpp:47
const SelfType ConstClosureType
Definition: VectorAdapter.hpp:63
std::enable_if< IsScalar< T >::value, HomogenousCoordsAdapter >::type & operator*=(const T &t)
Definition: VectorAdapter.hpp:149
ConstReference operator[](SizeType i) const
Definition: VectorAdapter.hpp:93
HomogenousCoordsAdapter & minusAssign(const VectorExpression< E > &e)
Definition: VectorAdapter.hpp:177
V VectorType
Definition: VectorAdapter.hpp:52
SelfType ClosureType
Definition: VectorAdapter.hpp:64
std::conditional< std::is_const< V >::value, typename V::ConstReference, typename V::Reference >::type Reference
Definition: VectorAdapter.hpp:59
Reference operator()(SizeType i)
Definition: VectorAdapter.hpp:69
Reference operator[](SizeType i)
Definition: VectorAdapter.hpp:85
const VectorClosureType & getData() const
Definition: VectorAdapter.hpp:116
HomogenousCoordsAdapter & operator=(const HomogenousCoordsAdapter &va)
Definition: VectorAdapter.hpp:121
bool isEmpty() const
Definition: VectorAdapter.hpp:106
std::conditional< std::is_const< V >::value, typename V::ConstClosureType, typename V::ClosureType >::type VectorClosureType
Definition: VectorAdapter.hpp:62
HomogenousCoordsAdapter & operator+=(const VectorExpression< E > &e)
Definition: VectorAdapter.hpp:135
HomogenousCoordsAdapter & operator-=(const VectorExpression< E > &e)
Definition: VectorAdapter.hpp:142
SizeType getSize() const
Definition: VectorAdapter.hpp:101
HomogenousCoordsAdapter(VectorType &v)
Definition: VectorAdapter.hpp:66
HomogenousCoordsAdapter & operator=(const VectorExpression< E > &e)
Definition: VectorAdapter.hpp:128
void swap(HomogenousCoordsAdapter &va)
Definition: VectorAdapter.hpp:183
V::ValueType ValueType
Definition: VectorAdapter.hpp:55
std::enable_if< IsScalar< T >::value, HomogenousCoordsAdapter >::type & operator/=(const T &t)
Definition: VectorAdapter.hpp:156
V::ConstReference ConstReference
Definition: VectorAdapter.hpp:56
ConstReference operator()(SizeType i) const
Definition: VectorAdapter.hpp:77
V::DifferenceType DifferenceType
Definition: VectorAdapter.hpp:54
HomogenousCoordsAdapter & plusAssign(const VectorExpression< E > &e)
Definition: VectorAdapter.hpp:170
VectorClosureType & getData()
Definition: VectorAdapter.hpp:111
friend void swap(HomogenousCoordsAdapter &va1, HomogenousCoordsAdapter &va2)
Definition: VectorAdapter.hpp:189
V::SizeType SizeType
Definition: VectorAdapter.hpp:53
HomogenousCoordsAdapter & assign(const VectorExpression< E > &e)
Definition: VectorAdapter.hpp:163
Definition: Expression.hpp:98
Definition: Quaternion.hpp:239
Definition: Expression.hpp:54
Definition: VectorAdapter.hpp:201
VectorQuaternionAdapter & assign(const QuaternionExpression< E > &e)
Definition: VectorAdapter.hpp:356
Reference getC4()
Definition: VectorAdapter.hpp:236
Reference getC3()
Definition: VectorAdapter.hpp:231
VectorClosureType & getData()
Definition: VectorAdapter.hpp:261
V VectorType
Definition: VectorAdapter.hpp:206
ConstReference getC4() const
Definition: VectorAdapter.hpp:256
ConstReference getC1() const
Definition: VectorAdapter.hpp:241
std::enable_if< IsScalar< T >::value, VectorQuaternionAdapter >::type & operator-=(const T &t)
Definition: VectorAdapter.hpp:319
std::conditional< std::is_const< V >::value, typename V::ConstClosureType, typename V::ClosureType >::type VectorClosureType
Definition: VectorAdapter.hpp:214
V::ValueType ValueType
Definition: VectorAdapter.hpp:207
ConstReference getC2() const
Definition: VectorAdapter.hpp:246
VectorQuaternionAdapter & plusAssign(const QuaternionExpression< E > &e)
Definition: VectorAdapter.hpp:363
std::enable_if< IsScalar< T >::value, VectorQuaternionAdapter >::type & operator+=(const T &t)
Definition: VectorAdapter.hpp:304
void set(const ValueType &c1=ValueType(), const ValueType &c2=ValueType(), const ValueType &c3=ValueType(), const ValueType &c4=ValueType())
Definition: VectorAdapter.hpp:387
ConstReference getC3() const
Definition: VectorAdapter.hpp:251
VectorQuaternionAdapter & operator+=(const QuaternionExpression< E > &e)
Definition: VectorAdapter.hpp:296
const VectorClosureType & getData() const
Definition: VectorAdapter.hpp:266
Reference getC1()
Definition: VectorAdapter.hpp:221
SelfType ClosureType
Definition: VectorAdapter.hpp:216
VectorQuaternionAdapter & operator*=(const QuaternionExpression< E > &e)
Definition: VectorAdapter.hpp:326
std::conditional< std::is_const< V >::value, typename V::ConstReference, typename V::Reference >::type Reference
Definition: VectorAdapter.hpp:211
VectorQuaternionAdapter & operator=(const VectorQuaternionAdapter &a)
Definition: VectorAdapter.hpp:271
std::enable_if< IsScalar< T >::value, VectorQuaternionAdapter >::type & operator=(const T &t)
Definition: VectorAdapter.hpp:286
std::enable_if< IsScalar< T >::value, VectorQuaternionAdapter >::type & operator/=(const T &t)
Definition: VectorAdapter.hpp:349
VectorQuaternionAdapter(VectorType &v)
Definition: VectorAdapter.hpp:218
V::ConstReference ConstReference
Definition: VectorAdapter.hpp:208
Reference getC2()
Definition: VectorAdapter.hpp:226
std::enable_if< IsScalar< T >::value, VectorQuaternionAdapter >::type & operator*=(const T &t)
Definition: VectorAdapter.hpp:334
const SelfType ConstClosureType
Definition: VectorAdapter.hpp:215
void swap(VectorQuaternionAdapter &a)
Definition: VectorAdapter.hpp:376
VectorQuaternionAdapter & minusAssign(const QuaternionExpression< E > &e)
Definition: VectorAdapter.hpp:370
VectorQuaternionAdapter & operator=(const QuaternionExpression< E > &e)
Definition: VectorAdapter.hpp:278
VectorQuaternionAdapter & operator-=(const QuaternionExpression< E > &e)
Definition: VectorAdapter.hpp:311
friend void swap(VectorQuaternionAdapter &a1, VectorQuaternionAdapter &a2)
Definition: VectorAdapter.hpp:382
VectorQuaternionAdapter & operator/=(const QuaternionExpression< E > &e)
Definition: VectorAdapter.hpp:341
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)
Definition: VectorAdapter.hpp:440
void quaternionSwap(Q &q, QuaternionExpression< E > &e)
Definition: QuaternionAssignment.hpp:65
void vectorSwap(V &v, VectorExpression< E > &e)
Definition: VectorAssignment.hpp:72
std::enable_if< IsScalar< T >::value, RealQuaternion< T > >::type quat(const T &t)
Definition: Quaternion.hpp:662
The namespace of the Chemical Data Processing Library.
Quaternion< typename V::ValueType > Type
Definition: VectorAdapter.hpp:406
Quaternion< typename V::ValueType > Type
Definition: VectorAdapter.hpp:413
Definition: TypeTraits.hpp:193
Q::QuaternionTemporaryType Type
Definition: TypeTraits.hpp:195
Definition: TypeTraits.hpp:179
V::VectorTemporaryType Type
Definition: TypeTraits.hpp:181