SUAPI-CppWrapper
C++WrapperforSketchUpCAPI
Transformation.hpp
1 //
2 // Transformation.hpp
3 //
4 // Sketchup C++ Wrapper for C API
5 // MIT License
6 //
7 // Copyright (c) 2017 Tom Kaneko
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 
16 // The above copyright notice and this permission notice shall be included in all
17 // copies or substantial portions of the Software.
18 
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 // SOFTWARE.
26 //
27 
28 #ifndef Transformation_hpp
29 #define Transformation_hpp
30 
31 #include <stdio.h>
32 #include <array>
33 
34 #include <SketchUpAPI/geometry/transformation.h>
35 
36 #include "SUAPI-CppWrapper/Geometry.hpp"
37 
38 namespace CW {
39 
40 class Axes;
41 class Face;
42 
44  private:
45  SUTransformation m_transformation;
46  constexpr static double EPSILON = 0.001; // Sketchup Tolerance is 1/1000"
47 
48  /**
49  * Multiplies 4x1 matrix by this transformation matrix
50  * @param matrix4_1 array of size 4 to mulitply with this transformation.
51  * @return double[4] array representing 4x1 matrix
52  */
53  std::array<double, 4> multiply4x1(std::array<double, 4> matrix4_1) const;
54 
55  /**
56  * Returns the determinant of the matrix.
57  */
58  double determinant() const;
59 
60  public:
61  /**
62  * Construct a Transformation with a simple scale of 1 (no change).
63  * @since SketchUp 2018, API v6.0
64  */
66 
67  /**
68  * Construct a Transformation from native SUTransformation object.
69  */
70  Transformation(SUTransformation transformation);
71 
72  /**
73  * Construct a Transformation by setting axes and translation.
74  * @since SketchUp 2018, API v6.0
75  */
76  Transformation(const Axes& axes, const Vector3D& translation, double scalar = 1.0);
77 
78  /**
79  * Construct a Transformation by setting axes and translation.
80  * @since SketchUp 2018, API v6.0
81  */
82  Transformation(const Point3D& origin, const Vector3D& x_axis, const Vector3D& y_axis, const Vector3D& z_axis, double scalar = 1.0);
83 
84  /**
85  * Construct a scale Transformation.
86  * @since SketchUp 2018, API v6.0
87  */
88  explicit Transformation(double scalar);
89 
90  /**
91  * Construct a non-uniform scale Transformation.
92  * @since SketchUp 2018, API v6.0
93  */
94  Transformation(double x_scale, double y_scale, double z_scale);
95 
96  /**
97  * Construct a translation Transformation.
98  * @since SketchUp 2018, API v6.0
99  */
100  explicit Transformation(const Vector3D& translation);
101 
102  /**
103  * Construct a Transformation from a point representing the translation, and a scale.
104  * @since SketchUp 2018, API v6.0
105  */
106  Transformation(const Point3D& translation, double scalar);
107 
108  /**
109  * Construct a Transformation from a point representing the origin (translation), and vector representing the Z-Axis. The other two axes in the transformed space are computed using the "Arbitrary axis algorithm".
110  * @since SketchUp 2017, API v5.0
111  */
112  Transformation(const Point3D& translation, const Vector3D& normal);
113 
114  /**
115  * Construct a Transformation given an origin, vector of rotation, and angle.
116  * @since SketchUp 2018, API v6.0
117  * @param point - The point specifying the translation component of the transformation.
118  * @param vector - The vector about which rotation will occur.
119  * @param angle - The rotation in radians for the transformation.
120  */
121  Transformation(const Point3D& point, const Vector3D& vector, double angle);
122 
123  /**
124  * Construct a Transformation object from an interpolation between two transformations. The weight determines the amount of interpolation. A weight of 0.0 would return a transformation of t1, while a weight of 1.0 would return a transformation of t2.
125  * @since SketchUp 2018, API v6.0
126  * @param transform1 - The first transformation object.
127  * @param transform2 - The second transformation object.
128  * @param weight - The weight determines the amount of interpolation from t1 to t2.
129  */
130  Transformation(const Transformation& transform1, const Transformation& transform2, double weight);
131 
132  /*
133  * Allows access to the array of numbers in the SUTransformation struct.
134  */
135  double operator[](size_t i) const;
136  double& operator[](size_t i);
137 
138  /*
139  * Cast to SUTransformation struct
140  */
141  SUTransformation ref() const;
142  operator SUTransformation() const;
143  operator const SUTransformation*() const;
144 
145  /**
146  * Returns true if this Transformation is identity (no change).
147  */
148  bool is_identity() const;
149 
150  /**
151  * Return the inverse Transformation object (see inverse Transformation matrices)
152  * @since SketchUp 2018, API v6.0
153  */
154  Transformation inverse() const;
155 
156  /**
157  * Returns the X-axis of the rigid transformation.
158  * @since SketchUp 2018, API v6.0
159  */
160  Vector3D x_axis() const;
161 
162  /**
163  * Returns the Y-axis of the rigid transformation.
164  * @since SketchUp 2018, API v6.0
165  */
166  Vector3D y_axis() const;
167 
168  /**
169  * Returns the Z-axis of the rigid transformation.
170  * @since SketchUp 2018, API v6.0
171  */
172  Vector3D z_axis() const;
173 
174  /**
175  * Returns the rotation about the Z-axis in radians.
176  * @since SketchUp 2018, API v6.0
177  */
178  double z_rotation() const;
179 
180  /**
181  * Normalise the transformation, so that the bottom row of the 4x4 matrix reads (0,0,0,1)
182  */
184 
185  /**
186  * Retrieves the origin of a rigid transformation.
187  * @since SketchUp 2018, API v6.0
188  */
189  Point3D origin() const;
190 
191  /**
192  * Retrieves the translation part of the transformation
193  */
194  Vector3D translation() const;
195 
196  /**
197  * Mulitplication of Transformation matrices.
198  * @since SketchUp 2018, API v6.0
199  */
201 
202  /**
203  * Return transformed vectors.
204  * @since SketchUp 2018, API v6.0
205  */
206  friend Vector3D operator*(const Transformation &lhs, const Vector3D &rhs);
207  /** You can't technically multiply a 4x1 matrix by a 4x4 matrix (only the other way around), so return it flipped */
208  friend Vector3D operator*(const Vector3D &lhs, const Transformation &rhs);
209 
210  /**
211  * Return transformed point.
212  * @since SketchUp 2018, API v6.0
213  */
214  friend Point3D operator*(const Transformation &lhs, const Point3D &rhs);
215  /** You can't technically multiply a 4x1 matrix by a 4x4 matrix (only the other way around), so return it flipped */
216  friend Point3D operator*(const Point3D &lhs, const Transformation &rhs);
217 
218  /**
219  * Return transformed plane.
220  * @since SketchUp 2018, API v6.0
221  */
222  friend Plane3D operator*(const Plane3D &lhs, const Transformation &rhs);
223  friend Plane3D operator*(const Transformation &lhs, const Plane3D &rhs);
224 
225  /**
226  * Return transformed face.
227  */
228  friend Face operator*(const Face &lhs, const Transformation &rhs);
229 
230  /**
231  * Compare equality of tranformation objects.
232  */
233  bool equal(const Transformation transform, const double epsilon = EPSILON) const;
234  bool operator==(const Transformation transform) const;
235 
236  /**
237  * Returns a Transformation object that represents rotation of the given angle in radians about a line. The rotation direction follows the right hand rule along the direction vector of the line.
238  */
239  static Transformation transformation_rotate_about_line(const double angle, const Line3D line);
240 };
241 
242 } /* namespace CW */
243 #endif /* Transformation_hpp */
Vector3D x_axis() const
Point3D origin() const
Transformation inverse() const
Transformation operator*(Transformation transform)
Vector3D z_axis() const
double z_rotation() const
Vector3D translation() const
bool is_identity() const
Definition: Color.hpp:34
static Transformation transformation_rotate_about_line(const double angle, const Line3D line)
bool equal(const Transformation transform, const double epsilon=EPSILON) const
Vector3D y_axis() const
Transformation & normalize()