Chemical Data Processing Library C++ API - Version 1.4.0
AffineTransform.hpp
Go to the documentation of this file.
1 /*
2  * AffineTransform.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_AFFINETRANSFORM_HPP
28 #define CDPL_MATH_AFFINETRANSFORM_HPP
29 
30 #include <algorithm>
31 #include <cmath>
32 #include <limits>
33 #include <cstddef>
34 #include <vector>
35 #include <utility>
36 
37 #include "CDPL/Math/Check.hpp"
38 #include "CDPL/Math/Expression.hpp"
39 #include "CDPL/Base/Exceptions.hpp"
40 
41 
42 namespace CDPL
43 {
44 
45  namespace Math
46  {
47 
48  template <typename V>
49  class MatrixReference;
50  template <typename T, typename A>
51  class Vector;
52  template <typename T, typename A>
53  class Matrix;
54 
64  template <typename T>
65  class RotationMatrix : public MatrixContainer<RotationMatrix<T> >
66  {
67 
69 
70  public:
74  typedef T ValueType;
75 
79  typedef const T Reference;
80 
84  typedef const T ConstReference;
85 
89  typedef std::size_t SizeType;
90 
94  typedef std::ptrdiff_t DifferenceType;
95 
100 
105 
110 
115 
122  template <typename E>
124  size(n)
125  {
126  set(q);
127  }
128 
141  template <typename T1, typename T2, typename T3, typename T4>
142  RotationMatrix(SizeType n, const T1& w, const T2& ux, const T3& uy, const T4& uz):
143  size(n)
144  {
145  set(w, ux, uy, uz);
146  }
147 
153  size(m.size)
154  {
155  std::copy(m.data, m.data + 4, data);
156  }
157 
163  template <typename E>
165  {
166  data[0] = q().getC1();
167  data[1] = q().getC2();
168  data[2] = q().getC3();
169  data[3] = q().getC4();
170  }
171 
183  template <typename T1, typename T2, typename T3, typename T4>
184  void set(const T1& w, const T2& ux, const T3& uy, const T4& uz)
185  {
186  data[0] = std::cos(w / 2.0);
187  data[1] = std::sin(w / 2.0) * ux;
188  data[2] = std::sin(w / 2.0) * uy;
189  data[3] = std::sin(w / 2.0) * uz;
190  }
191 
200  {
201  CDPL_MATH_CHECK(i < getSize1() && j < getSize2(), "Index out of range", Base::IndexError);
202 
203  if (i >= 3 || i >= size || j >= 3 || j >= size)
204  return (i == j ? ValueType(1) : ValueType());
205 
206  switch (i) {
207 
208  case 0:
209  switch (j) {
210 
211  case 0:
212  // a2 + b2 - c2 - d2
213  return (data[0] * data[0] + data[1] * data[1] - data[2] * data[2] - data[3] * data[3]);
214 
215  case 1:
216  // 2(bc - ad)
217  return 2 * (data[1] * data[2] - data[0] * data[3]);
218 
219  case 2:
220  // 2(bd + ac)
221  return 2 * (data[1] * data[3] + data[0] * data[2]);
222 
223  default:
224  return ValueType();
225  }
226 
227  case 1:
228  switch (j) {
229 
230  case 0:
231  // 2(bc + ad)
232  return 2 * (data[1] * data[2] + data[0] * data[3]);
233 
234  case 1:
235  // a2 - b2 + c2 - d2
236  return (data[0] * data[0] - data[1] * data[1] + data[2] * data[2] - data[3] * data[3]);
237 
238  case 2:
239  // 2(cd - ab)
240  return 2 * (data[2] * data[3] - data[0] * data[1]);
241 
242  default:
243  return ValueType();
244  }
245 
246  case 2:
247  switch (j) {
248 
249  case 0:
250  // 2(bd - ac)
251  return 2 * (data[1] * data[3] - data[0] * data[2]);
252 
253  case 1:
254  // 2(cd + ab)
255  return 2 * (data[2] * data[3] + data[0] * data[1]);
256 
257  case 2:
258  // a2 - b2 - c2 + d2
259  return (data[0] * data[0] - data[1] * data[1] - data[2] * data[2] + data[3] * data[3]);
260 
261  default:
262  return ValueType();
263  }
264 
265  default:
266  return (i == j ? ValueType(1) : ValueType());
267  }
268  }
269 
274  bool isEmpty() const
275  {
276  return (size == 0);
277  }
278 
284  {
285  return size;
286  }
287 
293  {
294  return size;
295  }
296 
302  {
303  return std::numeric_limits<SizeType>::max();
304  }
305 
311  {
312  return std::numeric_limits<SizeType>::max();
313  }
314 
321  {
322  if (this != &m) {
323  std::copy(m.data, m.data + 4, data);
324  size = m.size;
325  }
326 
327  return *this;
328  }
329 
335  {
336  if (this != &m) {
337  std::swap_ranges(data, data + 4, m.data);
338  std::swap(size, m.size);
339  }
340  }
341 
347  friend void swap(RotationMatrix& m1, RotationMatrix& m2)
348  {
349  m1.swap(m2);
350  }
351 
356  void resize(SizeType n)
357  {
358  size = n;
359  }
360 
361  private:
362  typedef ValueType ArrayType[4];
363 
364  SizeType size;
365  ArrayType data;
366  };
367 
376  template <typename T>
377  class ScalingMatrix : public MatrixContainer<ScalingMatrix<T> >
378  {
379 
380  typedef ScalingMatrix<T> SelfType;
381 
382  public:
386  typedef T ValueType;
387 
391  typedef const T Reference;
392 
396  typedef const T ConstReference;
397 
401  typedef std::size_t SizeType;
402 
406  typedef std::ptrdiff_t DifferenceType;
407 
412 
417 
422 
427 
435  explicit ScalingMatrix(SizeType n, const ValueType& sx = ValueType(1),
436  const ValueType& sy = ValueType(1), const ValueType& sz = ValueType(1)):
437  size(n)
438  {
439  set(sx, sy, sz);
440  }
441 
447  size(m.size)
448  {
449  std::copy(m.data, m.data + 3, data);
450  }
451 
458  void set(const ValueType& sx = ValueType(1), const ValueType& sy = ValueType(1),
459  const ValueType& sz = ValueType(1))
460  {
461  data[0] = sx;
462  data[1] = sy;
463  data[2] = sz;
464  }
465 
474  {
475  CDPL_MATH_CHECK(i < getSize1() && j < getSize2(), "Index out of range", Base::IndexError);
476 
477  if (i != j)
478  return ValueType();
479 
480  if (i < size && i < 3)
481  return data[i];
482 
483  return ValueType(1);
484  }
485 
490  bool isEmpty() const
491  {
492  return (size == 0);
493  }
494 
500  {
501  return size;
502  }
503 
509  {
510  return size;
511  }
512 
518  {
519  return std::numeric_limits<SizeType>::max();
520  }
521 
527  {
528  return std::numeric_limits<SizeType>::max();
529  }
530 
537  {
538  if (this != &m) {
539  std::copy(m.data, m.data + 3, data);
540  size = m.size;
541  }
542 
543  return *this;
544  }
545 
551  {
552  if (this != &m) {
553  std::swap_ranges(data, data + 3, m.data);
554  std::swap(size, m.size);
555  }
556  }
557 
563  friend void swap(ScalingMatrix& m1, ScalingMatrix& m2)
564  {
565  m1.swap(m2);
566  }
567 
572  void resize(SizeType n)
573  {
574  size = n;
575  }
576 
577  private:
578  typedef ValueType ArrayType[3];
579 
580  SizeType size;
581  ArrayType data;
582  };
583 
593  template <typename T>
594  class TranslationMatrix : public MatrixContainer<TranslationMatrix<T> >
595  {
596 
598 
599  public:
603  typedef T ValueType;
604 
608  typedef const T Reference;
609 
613  typedef const T ConstReference;
614 
618  typedef std::size_t SizeType;
619 
623  typedef std::ptrdiff_t DifferenceType;
624 
629 
634 
639 
644 
652  explicit TranslationMatrix(SizeType n, const ValueType& tx = ValueType(),
653  const ValueType& ty = ValueType(), const ValueType& tz = ValueType()):
654  size(n)
655  {
656  set(tx, ty, tz);
657  }
658 
664  size(m.size)
665  {
666  std::copy(m.data, m.data + 3, data);
667  }
668 
675  void set(const ValueType& tx = ValueType(), const ValueType& ty = ValueType(),
676  const ValueType& tz = ValueType())
677  {
678  data[0] = tx;
679  data[1] = ty;
680  data[2] = tz;
681  }
682 
691  {
692  CDPL_MATH_CHECK(i < getSize1() && j < getSize2(), "Index out of range", Base::IndexError);
693 
694  if (i == j)
695  return ValueType(1);
696 
697  if (j == (size - 1) && i < size && i < 3)
698  return data[i];
699 
700  return ValueType();
701  }
702 
707  bool isEmpty() const
708  {
709  return (size == 0);
710  }
711 
717  {
718  return size;
719  }
720 
726  {
727  return size;
728  }
729 
735  {
736  return std::numeric_limits<SizeType>::max();
737  }
738 
744  {
745  return std::numeric_limits<SizeType>::max();
746  }
747 
754  {
755  if (this != &m) {
756  std::copy(m.data, m.data + 3, data);
757  size = m.size;
758  }
759 
760  return *this;
761  }
762 
768  {
769  if (this != &m) {
770  std::swap_ranges(data, data + 3, m.data);
771  std::swap(size, m.size);
772  }
773  }
774 
781  {
782  m1.swap(m2);
783  }
784 
789  void resize(SizeType n)
790  {
791  size = n;
792  }
793 
794  private:
795  typedef ValueType ArrayType[3];
796 
797  SizeType size;
798  ArrayType data;
799  };
800 
805 
810 
815 
820 
825 
830 
835 
840 
845 
850 
855 
860  } // namespace Math
861 } // namespace CDPL
862 
863 #endif // CDPL_MATH_AFFINETRANSFORM_HPP
Definition of exception classes.
Definition of various preprocessor macros for error checking.
#define CDPL_MATH_CHECK(expr, msg, e)
Throws the exception e with message msg when the boolean expression expr evaluates to false.
Definition: Check.hpp:47
Definition of basic expression types.
Thrown to indicate that an index is out of range.
Definition: Base/Exceptions.hpp:152
Refinement of Math::MatrixExpression marking the derived type as a concrete (writable) matrix contain...
Definition: Expression.hpp:262
Lightweight matrix expression that proxies a reference to an underlying matrix container.
Definition: Matrix.hpp:64
Dynamically-sized dense row-major matrix with configurable underlying storage.
Definition: Matrix.hpp:510
CRTP base class of all quaternion expression types.
Definition: Expression.hpp:148
N×N rotation matrix backed by a unit quaternion (or an axis-angle representation).
Definition: AffineTransform.hpp:66
void swap(RotationMatrix &m)
Swaps the dimension and the underlying quaternion components with m.
Definition: AffineTransform.hpp:334
const MatrixReference< const SelfType > ConstClosureType
Constant closure type used when this matrix appears inside another expression.
Definition: AffineTransform.hpp:104
void resize(SizeType n)
Resizes the matrix dimension to n (the underlying quaternion is left unchanged).
Definition: AffineTransform.hpp:356
void set(const T1 &w, const T2 &ux, const T3 &uy, const T4 &uz)
Sets the rotation from an axis-angle representation.
Definition: AffineTransform.hpp:184
SizeType getSize1() const
Returns the dimension N (the row count).
Definition: AffineTransform.hpp:283
friend void swap(RotationMatrix &m1, RotationMatrix &m2)
ADL-enabled free-function form of swap().
Definition: AffineTransform.hpp:347
SizeType getMaxSize1() const
Returns the maximum representable row count.
Definition: AffineTransform.hpp:301
RotationMatrix(SizeType n, const QuaternionExpression< E > &q)
Constructs an N×N rotation matrix from the unit quaternion q.
Definition: AffineTransform.hpp:123
RotationMatrix & operator=(const RotationMatrix &m)
Copy-assigns the dimension and the underlying quaternion components from m.
Definition: AffineTransform.hpp:320
std::size_t SizeType
The unsigned size type.
Definition: AffineTransform.hpp:89
MatrixReference< SelfType > ClosureType
Closure type used when this matrix appears inside another expression.
Definition: AffineTransform.hpp:99
bool isEmpty() const
Tells whether the matrix is empty (size zero).
Definition: AffineTransform.hpp:274
RotationMatrix(const RotationMatrix &m)
Constructs a copy of the rotation matrix m.
Definition: AffineTransform.hpp:152
const T Reference
Element reference type (always a const reference — elements are computed).
Definition: AffineTransform.hpp:79
void set(const QuaternionExpression< E > &q)
Sets the rotation from the unit quaternion q.
Definition: AffineTransform.hpp:164
const T ConstReference
Constant element reference type.
Definition: AffineTransform.hpp:84
RotationMatrix(SizeType n, const T1 &w, const T2 &ux, const T3 &uy, const T4 &uz)
Constructs an N×N rotation matrix from an axis-angle representation.
Definition: AffineTransform.hpp:142
SizeType getMaxSize2() const
Returns the maximum representable column count.
Definition: AffineTransform.hpp:310
Matrix< T, std::vector< T > > MatrixTemporaryType
Concrete temporary matrix type used by expression template machinery.
Definition: AffineTransform.hpp:109
T ValueType
The scalar value type.
Definition: AffineTransform.hpp:74
Vector< T, std::vector< T > > VectorTemporaryType
Concrete temporary vector type used when assembling vectors from this matrix.
Definition: AffineTransform.hpp:114
std::ptrdiff_t DifferenceType
The signed difference type.
Definition: AffineTransform.hpp:94
SizeType getSize2() const
Returns the dimension N (the column count).
Definition: AffineTransform.hpp:292
ConstReference operator()(SizeType i, SizeType j) const
Returns the rotation matrix element at (i, j).
Definition: AffineTransform.hpp:199
N×N diagonal scaling matrix.
Definition: AffineTransform.hpp:378
void resize(SizeType n)
Resizes the matrix dimension to n (the scale factors are left unchanged).
Definition: AffineTransform.hpp:572
ScalingMatrix(const ScalingMatrix &m)
Constructs a copy of the scaling matrix m.
Definition: AffineTransform.hpp:446
Matrix< T, std::vector< T > > MatrixTemporaryType
Concrete temporary matrix type used by expression template machinery.
Definition: AffineTransform.hpp:421
SizeType getMaxSize2() const
Returns the maximum representable column count.
Definition: AffineTransform.hpp:526
Vector< T, std::vector< T > > VectorTemporaryType
Concrete temporary vector type used when assembling vectors from this matrix.
Definition: AffineTransform.hpp:426
const MatrixReference< const SelfType > ConstClosureType
Constant closure type used when this matrix appears inside another expression.
Definition: AffineTransform.hpp:416
friend void swap(ScalingMatrix &m1, ScalingMatrix &m2)
ADL-enabled free-function form of swap().
Definition: AffineTransform.hpp:563
bool isEmpty() const
Tells whether the matrix is empty (size zero).
Definition: AffineTransform.hpp:490
std::ptrdiff_t DifferenceType
The signed difference type.
Definition: AffineTransform.hpp:406
std::size_t SizeType
The unsigned size type.
Definition: AffineTransform.hpp:401
ConstReference operator()(SizeType i, SizeType j) const
Returns the scaling matrix element at (i, j).
Definition: AffineTransform.hpp:473
MatrixReference< SelfType > ClosureType
Closure type used when this matrix appears inside another expression.
Definition: AffineTransform.hpp:411
SizeType getSize2() const
Returns the dimension N (the column count).
Definition: AffineTransform.hpp:508
T ValueType
The scalar value type.
Definition: AffineTransform.hpp:386
SizeType getSize1() const
Returns the dimension N (the row count).
Definition: AffineTransform.hpp:499
ScalingMatrix & operator=(const ScalingMatrix &m)
Copy-assigns the dimension and the per-axis scale factors from m.
Definition: AffineTransform.hpp:536
SizeType getMaxSize1() const
Returns the maximum representable row count.
Definition: AffineTransform.hpp:517
void swap(ScalingMatrix &m)
Swaps the dimension and the per-axis scale factors with m.
Definition: AffineTransform.hpp:550
void set(const ValueType &sx=ValueType(1), const ValueType &sy=ValueType(1), const ValueType &sz=ValueType(1))
Sets the per-axis scale factors.
Definition: AffineTransform.hpp:458
ScalingMatrix(SizeType n, const ValueType &sx=ValueType(1), const ValueType &sy=ValueType(1), const ValueType &sz=ValueType(1))
Constructs an N×N scaling matrix with the supplied per-axis scale factors.
Definition: AffineTransform.hpp:435
const T ConstReference
Constant element reference type.
Definition: AffineTransform.hpp:396
const T Reference
Element reference type (always a const reference — elements are computed).
Definition: AffineTransform.hpp:391
N×N translation matrix in homogeneous coordinates.
Definition: AffineTransform.hpp:595
const MatrixReference< const SelfType > ConstClosureType
Constant closure type used when this matrix appears inside another expression.
Definition: AffineTransform.hpp:633
friend void swap(TranslationMatrix &m1, TranslationMatrix &m2)
ADL-enabled free-function form of swap().
Definition: AffineTransform.hpp:780
std::ptrdiff_t DifferenceType
The signed difference type.
Definition: AffineTransform.hpp:623
const T Reference
Element reference type (always a const reference — elements are computed).
Definition: AffineTransform.hpp:608
SizeType getSize1() const
Returns the dimension N (the row count).
Definition: AffineTransform.hpp:716
Vector< T, std::vector< T > > VectorTemporaryType
Concrete temporary vector type used when assembling vectors from this matrix.
Definition: AffineTransform.hpp:643
const T ConstReference
Constant element reference type.
Definition: AffineTransform.hpp:613
TranslationMatrix(const TranslationMatrix &m)
Constructs a copy of the translation matrix m.
Definition: AffineTransform.hpp:663
TranslationMatrix & operator=(const TranslationMatrix &m)
Copy-assigns the dimension and the translation components from m.
Definition: AffineTransform.hpp:753
bool isEmpty() const
Tells whether the matrix is empty (size zero).
Definition: AffineTransform.hpp:707
std::size_t SizeType
The unsigned size type.
Definition: AffineTransform.hpp:618
TranslationMatrix(SizeType n, const ValueType &tx=ValueType(), const ValueType &ty=ValueType(), const ValueType &tz=ValueType())
Constructs an N×N translation matrix with the supplied per-axis translation components.
Definition: AffineTransform.hpp:652
void set(const ValueType &tx=ValueType(), const ValueType &ty=ValueType(), const ValueType &tz=ValueType())
Sets the per-axis translation components.
Definition: AffineTransform.hpp:675
void resize(SizeType n)
Resizes the matrix dimension to n (the translation components are left unchanged).
Definition: AffineTransform.hpp:789
Matrix< T, std::vector< T > > MatrixTemporaryType
Concrete temporary matrix type used by expression template machinery.
Definition: AffineTransform.hpp:638
T ValueType
The scalar value type.
Definition: AffineTransform.hpp:603
SizeType getMaxSize1() const
Returns the maximum representable row count.
Definition: AffineTransform.hpp:734
ConstReference operator()(SizeType i, SizeType j) const
Returns the translation matrix element at (i, j).
Definition: AffineTransform.hpp:690
void swap(TranslationMatrix &m)
Swaps the dimension and the translation components with m.
Definition: AffineTransform.hpp:767
SizeType getSize2() const
Returns the dimension N (the column count).
Definition: AffineTransform.hpp:725
MatrixReference< SelfType > ClosureType
Closure type used when this matrix appears inside another expression.
Definition: AffineTransform.hpp:628
SizeType getMaxSize2() const
Returns the maximum representable column count.
Definition: AffineTransform.hpp:743
Dynamically-sized dense vector with configurable underlying storage.
Definition: Vector.hpp:480
constexpr unsigned int T
Specifies Hydrogen (Tritium).
Definition: AtomType.hpp:67
constexpr unsigned int m
Specifies that the stereocenter has m configuration.
Definition: CIPDescriptor.hpp:116
ScalingMatrix< float > FScalingMatrix
Scaling matrix holding floating-point values of type float.
Definition: AffineTransform.hpp:804
RotationMatrix< float > FRotationMatrix
Rotation matrix holding floating-point values of type float.
Definition: AffineTransform.hpp:824
TranslationMatrix< long > LTranslationMatrix
Translation matrix holding integer values of type long.
Definition: AffineTransform.hpp:854
ScalingMatrix< long > LScalingMatrix
Scaling matrix holding integer values of type long.
Definition: AffineTransform.hpp:814
RotationMatrix< double > DRotationMatrix
Rotation matrix holding floating-point values of type double.
Definition: AffineTransform.hpp:829
ScalingMatrix< double > DScalingMatrix
Scaling matrix holding floating-point values of type double.
Definition: AffineTransform.hpp:809
TranslationMatrix< double > DTranslationMatrix
Translation matrix holding floating-point values of type double.
Definition: AffineTransform.hpp:849
TranslationMatrix< unsigned long > ULTranslationMatrix
Translation matrix holding unsigned integer values of type unsigned long.
Definition: AffineTransform.hpp:859
TranslationMatrix< float > FTranslationMatrix
Translation matrix holding floating-point values of type float.
Definition: AffineTransform.hpp:844
ScalingMatrix< unsigned long > ULScalingMatrix
Scaling matrix holding unsigned integer values of type unsigned long.
Definition: AffineTransform.hpp:819
RotationMatrix< unsigned long > ULRotationMatrix
Rotation matrix holding unsigned integer values of type unsigned long.
Definition: AffineTransform.hpp:839
RotationMatrix< long > LRotationMatrix
Rotation matrix holding integer values of type long.
Definition: AffineTransform.hpp:834
The namespace of the Chemical Data Processing Library.