1 // Copyright (c) 1991-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 #ifndef _gp_Trsf_HeaderFile
16 #define _gp_Trsf_HeaderFile
18 #include <gp_TrsfForm.hxx>
21 #include <NCollection_Mat4.hxx>
22 #include <Standard_ConstructionError.hxx>
23 #include <Standard_OStream.hxx>
24 #include <Standard_OutOfRange.hxx>
25 #include <Standard_SStream.hxx>
35 // Avoid possible conflict with SetForm macro defined by windows.h
40 //! Defines a non-persistent transformation in 3D space.
41 //! The following transformations are implemented :
42 //! . Translation, Rotation, Scale
43 //! . Symmetry with respect to a point, a line, a plane.
44 //! Complex transformations can be obtained by combining the
45 //! previous elementary transformations using the method
47 //! The transformations can be represented as follow :
49 //! V1 V2 V3 T XYZ XYZ
50 //! | a11 a12 a13 a14 | | x | | x'|
51 //! | a21 a22 a23 a24 | | y | | y'|
52 //! | a31 a32 a33 a34 | | z | = | z'|
53 //! | 0 0 0 1 | | 1 | | 1 |
55 //! where {V1, V2, V3} defines the vectorial part of the
56 //! transformation and T defines the translation part of the
58 //! This transformation never change the nature of the objects.
65 //! Returns the identity transformation.
68 //! Creates a 3D transformation from the 2D transformation theT.
69 //! The resulting transformation has a homogeneous
70 //! vectorial part, V3, and a translation part, T3, built from theT:
77 //! It also has the same scale factor as theT. This
78 //! guarantees (by projection) that the transformation
79 //! which would be performed by theT in a plane (2D space)
80 //! is performed by the resulting transformation in the xOy
81 //! plane of the 3D space, (i.e. in the plane defined by the
82 //! origin (0., 0., 0.) and the vectors DX (1., 0., 0.), and DY
83 //! (0., 1., 0.)). The scale factor is applied to the entire space.
84 Standard_EXPORT gp_Trsf (const gp_Trsf2d& theT);
86 //! Makes the transformation into a symmetrical transformation.
87 //! theP is the center of the symmetry.
88 void SetMirror (const gp_Pnt& theP);
90 //! Makes the transformation into a symmetrical transformation.
91 //! theA1 is the center of the axial symmetry.
92 Standard_EXPORT void SetMirror (const gp_Ax1& theA1);
94 //! Makes the transformation into a symmetrical transformation.
95 //! theA2 is the center of the planar symmetry
96 //! and defines the plane of symmetry by its origin, "X
97 //! Direction" and "Y Direction".
98 Standard_EXPORT void SetMirror (const gp_Ax2& theA2);
100 //! Changes the transformation into a rotation.
101 //! theA1 is the rotation axis and theAng is the angular value of the
102 //! rotation in radians.
103 Standard_EXPORT void SetRotation (const gp_Ax1& theA1, const Standard_Real theAng);
105 //! Changes the transformation into a rotation defined by quaternion.
106 //! Note that rotation is performed around origin, i.e.
107 //! no translation is involved.
108 Standard_EXPORT void SetRotation (const gp_Quaternion& theR);
110 //! Replaces the rotation part with specified quaternion.
111 Standard_EXPORT void SetRotationPart (const gp_Quaternion& theR);
113 //! Changes the transformation into a scale.
114 //! theP is the center of the scale and theS is the scaling value.
115 //! Raises ConstructionError If <theS> is null.
116 Standard_EXPORT void SetScale (const gp_Pnt& theP, const Standard_Real theS);
118 //! Modifies this transformation so that it transforms the
119 //! coordinate system defined by theFromSystem1 into the
120 //! one defined by theToSystem2. After this modification, this
121 //! transformation transforms:
122 //! - the origin of theFromSystem1 into the origin of theToSystem2,
123 //! - the "X Direction" of theFromSystem1 into the "X
124 //! Direction" of theToSystem2,
125 //! - the "Y Direction" of theFromSystem1 into the "Y
126 //! Direction" of theToSystem2, and
127 //! - the "main Direction" of theFromSystem1 into the "main
128 //! Direction" of theToSystem2.
130 //! When you know the coordinates of a point in one
131 //! coordinate system and you want to express these
132 //! coordinates in another one, do not use the
133 //! transformation resulting from this function. Use the
134 //! transformation that results from SetTransformation instead.
135 //! SetDisplacement and SetTransformation create
136 //! related transformations: the vectorial part of one is the
137 //! inverse of the vectorial part of the other.
138 Standard_EXPORT void SetDisplacement (const gp_Ax3& theFromSystem1, const gp_Ax3& theToSystem2);
140 //! Modifies this transformation so that it transforms the
141 //! coordinates of any point, (x, y, z), relative to a source
142 //! coordinate system into the coordinates (x', y', z') which
143 //! are relative to a target coordinate system, but which
144 //! represent the same point
145 //! The transformation is from the coordinate
146 //! system "theFromSystem1" to the coordinate system "theToSystem2".
149 //! gp_Ax3 theFromSystem1, theToSystem2;
150 //! double x1, y1, z1; // are the coordinates of a point in the local system theFromSystem1
151 //! double x2, y2, z2; // are the coordinates of a point in the local system theToSystem2
152 //! gp_Pnt P1 (x1, y1, z1)
154 //! T.SetTransformation (theFromSystem1, theToSystem2);
155 //! gp_Pnt P2 = P1.Transformed (T);
156 //! P2.Coord (x2, y2, z2);
158 Standard_EXPORT void SetTransformation (const gp_Ax3& theFromSystem1, const gp_Ax3& theToSystem2);
160 //! Modifies this transformation so that it transforms the
161 //! coordinates of any point, (x, y, z), relative to a source
162 //! coordinate system into the coordinates (x', y', z') which
163 //! are relative to a target coordinate system, but which
164 //! represent the same point
165 //! The transformation is from the default coordinate system
167 //! {P(0.,0.,0.), VX (1.,0.,0.), VY (0.,1.,0.), VZ (0., 0. ,1.) }
169 //! to the local coordinate system defined with the Ax3 theToSystem.
170 //! Use in the same way as the previous method. FromSystem1 is
171 //! defaulted to the absolute coordinate system.
172 Standard_EXPORT void SetTransformation (const gp_Ax3& theToSystem);
174 //! Sets transformation by directly specified rotation and translation.
175 Standard_EXPORT void SetTransformation (const gp_Quaternion& R, const gp_Vec& theT);
177 //! Changes the transformation into a translation.
178 //! theV is the vector of the translation.
179 void SetTranslation (const gp_Vec& theV);
181 //! Makes the transformation into a translation where the translation vector
182 //! is the vector (theP1, theP2) defined from point theP1 to point theP2.
183 void SetTranslation (const gp_Pnt& theP1, const gp_Pnt& theP2);
185 //! Replaces the translation vector with the vector theV.
186 Standard_EXPORT void SetTranslationPart (const gp_Vec& theV);
188 //! Modifies the scale factor.
189 //! Raises ConstructionError If theS is null.
190 Standard_EXPORT void SetScaleFactor (const Standard_Real theS);
192 void SetForm (const gp_TrsfForm theP) { shape = theP; }
194 //! Sets the coefficients of the transformation. The
195 //! transformation of the point x,y,z is the point
198 //! x' = a11 x + a12 y + a13 z + a14
199 //! y' = a21 x + a22 y + a23 z + a24
200 //! z' = a31 x + a32 y + a33 z + a34
202 //! The method Value(i,j) will return aij.
203 //! Raises ConstructionError if the determinant of the aij is null.
204 //! The matrix is orthogonalized before future using.
205 Standard_EXPORT void SetValues (const Standard_Real a11, const Standard_Real a12, const Standard_Real a13, const Standard_Real a14, const Standard_Real a21, const Standard_Real a22, const Standard_Real a23, const Standard_Real a24, const Standard_Real a31, const Standard_Real a32, const Standard_Real a33, const Standard_Real a34);
207 //! Returns true if the determinant of the vectorial part of
208 //! this transformation is negative.
209 Standard_Boolean IsNegative() const { return (scale < 0.0); }
211 //! Returns the nature of the transformation. It can be: an
212 //! identity transformation, a rotation, a translation, a mirror
213 //! transformation (relative to a point, an axis or a plane), a
214 //! scaling transformation, or a compound transformation.
215 gp_TrsfForm Form() const { return shape; }
217 //! Returns the scale factor.
218 Standard_Real ScaleFactor() const { return scale; }
220 //! Returns the translation part of the transformation's matrix
221 const gp_XYZ& TranslationPart() const { return loc; }
223 //! Returns the boolean True if there is non-zero rotation.
224 //! In the presence of rotation, the output parameters store the axis
225 //! and the angle of rotation. The method always returns positive
226 //! value "theAngle", i.e., 0. < theAngle <= PI.
227 //! Note that this rotation is defined only by the vectorial part of
228 //! the transformation; generally you would need to check also the
229 //! translational part to obtain the axis (gp_Ax1) of rotation.
230 Standard_EXPORT Standard_Boolean GetRotation (gp_XYZ& theAxis, Standard_Real& theAngle) const;
232 //! Returns quaternion representing rotational part of the transformation.
233 Standard_EXPORT gp_Quaternion GetRotation() const;
235 //! Returns the vectorial part of the transformation. It is
236 //! a 3*3 matrix which includes the scale factor.
237 Standard_EXPORT gp_Mat VectorialPart() const;
239 //! Computes the homogeneous vectorial part of the transformation.
240 //! It is a 3*3 matrix which doesn't include the scale factor.
241 //! In other words, the vectorial part of this transformation is equal
242 //! to its homogeneous vectorial part, multiplied by the scale factor.
243 //! The coefficients of this matrix must be multiplied by the
244 //! scale factor to obtain the coefficients of the transformation.
245 const gp_Mat& HVectorialPart() const { return matrix; }
247 //! Returns the coefficients of the transformation's matrix.
248 //! It is a 3 rows * 4 columns matrix.
249 //! This coefficient includes the scale factor.
250 //! Raises OutOfRanged if theRow < 1 or theRow > 3 or theCol < 1 or theCol > 4
251 Standard_Real Value (const Standard_Integer theRow, const Standard_Integer theCol) const;
253 Standard_EXPORT void Invert();
255 //! Computes the reverse transformation
256 //! Raises an exception if the matrix of the transformation
257 //! is not inversible, it means that the scale factor is lower
258 //! or equal to Resolution from package gp.
259 //! Computes the transformation composed with T and <me>.
260 //! In a C++ implementation you can also write Tcomposed = <me> * T.
263 //! gp_Trsf T1, T2, Tcomp; ...............
264 //! Tcomp = T2.Multiplied(T1); // or (Tcomp = T2 * T1)
265 //! gp_Pnt P1(10.,3.,4.);
266 //! gp_Pnt P2 = P1.Transformed(Tcomp); // using Tcomp
267 //! gp_Pnt P3 = P1.Transformed(T1); // using T1 then T2
268 //! P3.Transform(T2); // P3 = P2 !!!
270 Standard_NODISCARD gp_Trsf Inverted() const
277 Standard_NODISCARD gp_Trsf Multiplied (const gp_Trsf& theT) const
279 gp_Trsf aTresult (*this);
280 aTresult.Multiply (theT);
284 Standard_NODISCARD gp_Trsf operator * (const gp_Trsf& theT) const { return Multiplied (theT); }
286 //! Computes the transformation composed with <me> and theT.
287 //! <me> = <me> * theT
288 Standard_EXPORT void Multiply (const gp_Trsf& theT);
290 void operator *= (const gp_Trsf& theT) { Multiply (theT); }
292 //! Computes the transformation composed with <me> and T.
293 //! <me> = theT * <me>
294 Standard_EXPORT void PreMultiply (const gp_Trsf& theT);
296 Standard_EXPORT void Power (const Standard_Integer theN);
298 //! Computes the following composition of transformations
299 //! <me> * <me> * .......* <me>, theN time.
300 //! if theN = 0 <me> = Identity
301 //! if theN < 0 <me> = <me>.Inverse() *...........* <me>.Inverse().
303 //! Raises if theN < 0 and if the matrix of the transformation not
305 Standard_NODISCARD gp_Trsf Powered (const Standard_Integer theN) const
312 void Transforms (Standard_Real& theX, Standard_Real& theY, Standard_Real& theZ) const;
314 //! Transformation of a triplet XYZ with a Trsf
315 void Transforms (gp_XYZ& theCoord) const;
317 //! Convert transformation to 4x4 matrix.
319 void GetMat4 (NCollection_Mat4<T>& theMat) const
321 if (shape == gp_Identity)
323 theMat.InitIdentity();
327 theMat.SetValue (0, 0, static_cast<T> (Value (1, 1)));
328 theMat.SetValue (0, 1, static_cast<T> (Value (1, 2)));
329 theMat.SetValue (0, 2, static_cast<T> (Value (1, 3)));
330 theMat.SetValue (0, 3, static_cast<T> (Value (1, 4)));
331 theMat.SetValue (1, 0, static_cast<T> (Value (2, 1)));
332 theMat.SetValue (1, 1, static_cast<T> (Value (2, 2)));
333 theMat.SetValue (1, 2, static_cast<T> (Value (2, 3)));
334 theMat.SetValue (1, 3, static_cast<T> (Value (2, 4)));
335 theMat.SetValue (2, 0, static_cast<T> (Value (3, 1)));
336 theMat.SetValue (2, 1, static_cast<T> (Value (3, 2)));
337 theMat.SetValue (2, 2, static_cast<T> (Value (3, 3)));
338 theMat.SetValue (2, 3, static_cast<T> (Value (3, 4)));
339 theMat.SetValue (3, 0, static_cast<T> (0));
340 theMat.SetValue (3, 1, static_cast<T> (0));
341 theMat.SetValue (3, 2, static_cast<T> (0));
342 theMat.SetValue (3, 3, static_cast<T> (1));
345 //! Dumps the content of me into the stream
346 Standard_EXPORT void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const;
348 //! Inits the content of me from the stream
349 Standard_EXPORT Standard_Boolean InitFromJson (const Standard_SStream& theSStream, Standard_Integer& theStreamPos);
351 friend class gp_GTrsf;
355 //! Makes orthogonalization of "matrix"
356 Standard_EXPORT void Orthogonalize();
367 #include <gp_Trsf2d.hxx>
368 #include <gp_Vec.hxx>
369 #include <gp_Pnt.hxx>
371 //=======================================================================
374 //=======================================================================
375 inline gp_Trsf::gp_Trsf ()
378 matrix (1, 0, 0, 0, 1, 0, 0, 0, 1),
382 //=======================================================================
383 //function : SetMirror
385 //=======================================================================
386 inline void gp_Trsf::SetMirror (const gp_Pnt& theP)
388 shape = gp_PntMirror;
391 matrix.SetIdentity();
395 //=======================================================================
396 //function : SetTranslation
398 //=======================================================================
399 inline void gp_Trsf::SetTranslation (const gp_Vec& theV)
401 shape = gp_Translation;
403 matrix.SetIdentity();
407 //=======================================================================
408 //function : SetTranslation
410 //=======================================================================
411 inline void gp_Trsf::SetTranslation (const gp_Pnt& theP1,
414 shape = gp_Translation;
416 matrix.SetIdentity();
417 loc = (theP2.XYZ()).Subtracted (theP1.XYZ());
420 //=======================================================================
423 //=======================================================================
424 inline Standard_Real gp_Trsf::Value (const Standard_Integer theRow, const Standard_Integer theCol) const
426 Standard_OutOfRange_Raise_if (theRow < 1 || theRow > 3 || theCol < 1 || theCol > 4, " ");
429 return scale * matrix.Value (theRow, theCol);
433 return loc.Coord (theRow);
437 //=======================================================================
438 //function : Transforms
440 //=======================================================================
441 inline void gp_Trsf::Transforms (Standard_Real& theX,
443 Standard_Real& theZ) const
445 gp_XYZ aTriplet (theX, theY, theZ);
446 aTriplet.Multiply (matrix);
449 aTriplet.Multiply (scale);
457 //=======================================================================
458 //function : Transforms
460 //=======================================================================
461 inline void gp_Trsf::Transforms (gp_XYZ& theCoord) const
463 theCoord.Multiply (matrix);
466 theCoord.Multiply (scale);
471 #endif // _gp_Trsf_HeaderFile