IMP logo
IMP Reference Guide  2.9.0
The Integrative Modeling Platform
Transformation3D.h
Go to the documentation of this file.
1 /**
2  * \file IMP/algebra/Transformation3D.h
3  * \brief Simple 3D transformation class.
4  *
5  * Copyright 2007-2018 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPALGEBRA_TRANSFORMATION_3D_H
10 #define IMPALGEBRA_TRANSFORMATION_3D_H
11 
12 #include <IMP/algebra/algebra_config.h>
13 #include "Vector3D.h"
14 #include "Line3D.h"
15 #include "Rotation3D.h"
16 #include "BoundingBoxD.h"
17 #include "GeometricPrimitiveD.h"
18 
19 IMPALGEBRA_BEGIN_NAMESPACE
20 
21 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
22 class Transformation3D;
23 Transformation3D compose(const Transformation3D &a, const Transformation3D &b);
24 #endif
25 
26 //! Simple 3D transformation class
27 /** The rotation is applied first, and then the point is translated.
28  \see IMP::core::Transform
29  \geometry
30 */
31 class IMPALGEBRAEXPORT Transformation3D : public GeometricPrimitiveD<3> {
32  public:
33  IMP_CXX11_DEFAULT_COPY_CONSTRUCTOR(Transformation3D);
34  //! Default construct (makes an invalid transformation)
36  //! Basic constructor
37  Transformation3D(const Rotation3D &r, const Vector3D &t = Vector3D(0, 0, 0))
38  : trans_(t), rot_(r) {}
39  //! Construct a transformation with an identity rotation.
41  : trans_(t), rot_(get_identity_rotation_3d()) {}
43  //! Transform
44  Vector3D get_transformed(const Vector3D &o) const {
45  return rot_.get_rotated(o) + trans_;
46  }
47  //! Apply transformation (rotate and then translate)
48  Vector3D operator*(const Vector3D &v) const { return get_transformed(v); }
49  /** Compose two rigid transformation such that for any vector v
50  (rt1*rt2)*v = rt1*(rt2*v) */
52  return compose(*this, tr);
53  }
54  const Transformation3D &operator*=(const Transformation3D &o) {
55  *this = compose(*this, o);
56  return *this;
57  }
58  //! Compute the transformation which, when composed with b, gives *this.
59  /** That is a(x)== d(b(x)) for all x.
60 
61  For consistency, this should probably have a nice name, but
62  I don't know what name to give it.
63  */
65  Transformation3D ret = compose(*this, b.get_inverse());
66  return ret;
67  }
68  const Transformation3D &operator/=(const Transformation3D &o) {
69  *this = *this / o;
70  return *this;
71  }
72 
73  //! Return the rotation associated with this transformation
74  const Rotation3D &get_rotation() const { return rot_; }
75 
76  //! Return the translation vector associated with this transformation
77  const Vector3D &get_translation() const { return trans_; }
78 
80  rot_.show(out);
81  out << " || " << trans_;
82  });
83  Transformation3D get_inverse() const;
84 
85  /** @return true if the 3D transformation is valid; false if the
86  transformation was initialized only with the empty constructor,
87  or it was initialized with an invalid rotation.
88  */
89  bool get_is_valid() const {
90  return rot_.get_is_valid();
91  }
92 
93  private:
94  Vector3D trans_; // translation
95  Rotation3D rot_; // rotation
96 };
97 
99 
100 //! Return a transformation that does not do anything
101 /** \see Transformation3D */
103  return Transformation3D(get_identity_rotation_3d(), Vector3D(0.0, 0.0, 0.0));
104 }
105 
106 //! Generate a Transformation3D object from a rotation around a point
107 /** Rotate about a point rather than the origin.
108  \param[in] point Center to rotate about
109  \param[in] rotation The rotation to perform
110 
111  \see Transformation3D
112 */
114  const Rotation3D &rotation) {
115  return Transformation3D(rotation, (rotation * (-point) + point));
116 }
117 
118 //! Compose two transformations
119 /** For any vector v (a*b)*v = a*(b*v).
120  \see Transformation3D
121  */
123  const Transformation3D &b) {
125  "composing an invalid transformation");
128 }
129 
130 class Transformation2D;
131 
132 //! Build a 3D transformation from a 2D one.
133 /**
134  \note The 3D transformation is built with the 2D rotation becoming a rotation
135  around the z axis.
136  **/
137 IMPALGEBRAEXPORT Transformation3D
138  get_transformation_3d(const Transformation2D &t2d);
139 
140 //! Get a local transformation
141 /**
142  \note randomly select an axis that passes through the input point
143  and rotate around it
144  \param[in] origin the origin of the rotation
145  \param[in] max_translation default value is 5
146  \param[in] max_angle_in_rad default value is 15 degree in radians
147  **/
148 IMPALGEBRAEXPORT Transformation3D
150  double max_translation = 5.,
151  double max_angle_in_rad = 0.26);
152 
153 //! Return a bounding box containing the transformed box
155  const Transformation3D &tr) {
156  BoundingBoxD<3> nbb;
157  for (unsigned int i = 0; i < 2; ++i) {
158  for (unsigned int j = 0; j < 2; ++j) {
159  for (unsigned int k = 0; k < 2; ++k) {
160  algebra::Vector3D v(bb.get_corner(i)[0], bb.get_corner(j)[1],
161  bb.get_corner(k)[2]);
162  nbb += tr.get_transformed(v);
163  }
164  }
165  }
166  return nbb;
167 }
168 
170  const Transformation3D &tr) {
171  return Line3D(tr.get_rotation().get_rotated(l.get_direction()),
172  tr.get_transformed(l.get_point_on_line()));
173 }
174 
175 IMPALGEBRA_END_NAMESPACE
176 
177 #endif /* IMPALGEBRA_TRANSFORMATION_3D_H */
Base class for geometric types.
Simple 3D transformation class.
Simple implementation of lines in 3D.
Definition: Line3D.h:28
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
const VectorD< D > & get_corner(unsigned int i) const
For 0 return lower corner and for 1, the upper corner.
Definition: BoundingBoxD.h:139
Simple implementation of lines in 3D.
Rotation2D compose(const Rotation2D &a, const Rotation2D &b)
Compose two rotations a and b.
Definition: Rotation2D.h:108
Vector3D get_point_on_line() const
Get the point on the line closest to the origin.
Definition: Line3D.h:41
Transformation3D get_rotation_about_point(const Vector3D &point, const Rotation3D &rotation)
Generate a Transformation3D object from a rotation around a point.
Transformation3D()
Default construct (makes an invalid transformation)
const Rotation3D & get_rotation() const
Return the rotation associated with this transformation.
#define IMP_VALUES(Name, PluralName)
Define the type for storing sets of values.
Definition: value_macros.h:23
Base class for geometric types.
A bounding box in D dimensions.
Transformation3D operator/(const Transformation3D &b) const
Compute the transformation which, when composed with b, gives *this.
Vector3D get_transformed(const Vector3D &o) const
Transform.
Transformation3D compose(const Transformation3D &a, const Transformation3D &b)
Compose two transformations.
Simple 3D rotation class.
3D rotation class.
Definition: Rotation3D.h:46
Transformation3D get_identity_transformation_3d()
Return a transformation that does not do anything.
Transformation3D operator*(const Transformation3D &tr) const
Transformation3D get_transformation_3d(const Transformation2D &t2d)
Build a 3D transformation from a 2D one.
Vector3D operator*(const Vector3D &v) const
Apply transformation (rotate and then translate)
Transformation3D(const Vector3D &t)
Construct a transformation with an identity rotation.
const Vector3D & get_direction() const
Get the unit vector in the direction of the line.
Definition: Line3D.h:38
VectorD< 3 > Vector3D
Definition: VectorD.h:395
Rotation3D get_identity_rotation_3d()
Return a rotation that does not do anything.
Definition: Rotation3D.h:244
Simple 3D vector class.
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Definition: check_macros.h:168
Transformation3D(const Rotation3D &r, const Vector3D &t=Vector3D(0, 0, 0))
Basic constructor.
DensityMap * get_transformed(const DensityMap *input, const algebra::Transformation3D &tr, double threshold)
Return a new density map containing a rotated version of the old one.
const Vector3D & get_translation() const
Return the translation vector associated with this transformation.
Transformation3D get_random_local_transformation(Vector3D origin, double max_translation=5., double max_angle_in_rad=0.26)
Get a local transformation.