0031673: Draw Harness, ViewerTest - command vlocation applies transformation in oppos...
[occt.git] / src / gp / gp_Trsf.hxx
1 // Copyright (c) 1991-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
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.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 #ifndef _gp_Trsf_HeaderFile
16 #define _gp_Trsf_HeaderFile
17
18 #include <gp_TrsfForm.hxx>
19 #include <gp_Mat.hxx>
20 #include <gp_XYZ.hxx>
21 #include <NCollection_Mat4.hxx>
22 #include <Standard.hxx>
23 #include <Standard_Boolean.hxx>
24 #include <Standard_DefineAlloc.hxx>
25 #include <Standard_Integer.hxx>
26 #include <Standard_Handle.hxx>
27 #include <Standard_OStream.hxx>
28 #include <Standard_Real.hxx>
29
30 class Standard_ConstructionError;
31 class Standard_OutOfRange;
32 class gp_Pnt;
33 class gp_Ax1;
34 class gp_Ax2;
35 class gp_Quaternion;
36 class gp_Ax3;
37 class gp_Vec;
38
39 // Avoid possible conflict with SetForm macro defined by windows.h
40 #ifdef SetForm
41 #undef SetForm
42 #endif
43
44 //! Defines a non-persistent transformation in 3D space.
45 //! The following transformations are implemented :
46 //! . Translation, Rotation, Scale
47 //! . Symmetry with respect to a point, a line, a plane.
48 //! Complex transformations can be obtained by combining the
49 //! previous elementary transformations using the method
50 //! Multiply.
51 //! The transformations can be represented as follow :
52 //!
53 //! V1   V2   V3    T       XYZ        XYZ
54 //! | a11  a12  a13   a14 |   | x |      | x'|
55 //! | a21  a22  a23   a24 |   | y |      | y'|
56 //! | a31  a32  a33   a34 |   | z |   =  | z'|
57 //! |  0    0    0     1  |   | 1 |      | 1 |
58 //!
59 //! where {V1, V2, V3} defines the vectorial part of the
60 //! transformation and T defines the translation part of the
61 //! transformation.
62 //! This transformation never change the nature of the objects.
63 class gp_Trsf 
64 {
65 public:
66
67   DEFINE_STANDARD_ALLOC
68
69   
70   //! Returns the identity transformation.
71     gp_Trsf();
72   
73   //! Creates  a 3D transformation from the 2D transformation T.
74   //! The resulting transformation has a homogeneous
75   //! vectorial part, V3, and a translation part, T3, built from T:
76   //! a11    a12
77   //! 0             a13
78   //! V3 =    a21    a22    0       T3
79   //! =   a23
80   //! 0    0    1.
81   //! 0
82   //! It also has the same scale factor as T. This
83   //! guarantees (by projection) that the transformation
84   //! which would be performed by T in a plane (2D space)
85   //! is performed by the resulting transformation in the xOy
86   //! plane of the 3D space, (i.e. in the plane defined by the
87   //! origin (0., 0., 0.) and the vectors DX (1., 0., 0.), and DY
88   //! (0., 1., 0.)). The scale factor is applied to the entire space.
89   Standard_EXPORT gp_Trsf(const gp_Trsf2d& T);
90   
91
92   //! Makes the transformation into a symmetrical transformation.
93   //! P is the center of the symmetry.
94     void SetMirror (const gp_Pnt& P);
95   
96
97   //! Makes the transformation into a symmetrical transformation.
98   //! A1 is the center of the axial symmetry.
99   Standard_EXPORT void SetMirror (const gp_Ax1& A1);
100   
101
102   //! Makes the transformation into a symmetrical transformation.
103   //! A2 is the center of the planar symmetry
104   //! and defines the plane of symmetry by its origin, "X
105   //! Direction" and "Y Direction".
106   Standard_EXPORT void SetMirror (const gp_Ax2& A2);
107   
108
109   //! Changes the transformation into a rotation.
110   //! A1 is the rotation axis and Ang is the angular value of the
111   //! rotation in radians.
112   Standard_EXPORT void SetRotation (const gp_Ax1& A1, const Standard_Real Ang);
113
114   //! Changes the transformation into a rotation defined by quaternion.
115   //! Note that rotation is performed around origin, i.e.
116   //! no translation is involved.
117   Standard_EXPORT void SetRotation (const gp_Quaternion& R);
118
119   //! Replaces the rotation part with specified quaternion.
120   Standard_EXPORT void SetRotationPart (const gp_Quaternion& R);
121
122   //! Changes the transformation into a scale.
123   //! P is the center of the scale and S is the scaling value.
124   //! Raises ConstructionError  If <S> is null.
125   Standard_EXPORT void SetScale (const gp_Pnt& P, const Standard_Real S);
126   
127
128   //! Modifies this transformation so that it transforms the
129   //! coordinate system defined by FromSystem1 into the
130   //! one defined by ToSystem2. After this modification, this
131   //! transformation transforms:
132   //! -   the origin of FromSystem1 into the origin of ToSystem2,
133   //! -   the "X Direction" of FromSystem1 into the "X
134   //! Direction" of ToSystem2,
135   //! -   the "Y Direction" of FromSystem1 into the "Y
136   //! Direction" of ToSystem2, and
137   //! -   the "main Direction" of FromSystem1 into the "main
138   //! Direction" of ToSystem2.
139   //! Warning
140   //! When you know the coordinates of a point in one
141   //! coordinate system and you want to express these
142   //! coordinates in another one, do not use the
143   //! transformation resulting from this function. Use the
144   //! transformation that results from SetTransformation instead.
145   //! SetDisplacement and SetTransformation create
146   //! related transformations: the vectorial part of one is the
147   //! inverse of the vectorial part of the other.
148   Standard_EXPORT void SetDisplacement (const gp_Ax3& FromSystem1, const gp_Ax3& ToSystem2);
149   
150   //! Modifies this transformation so that it transforms the
151   //! coordinates of any point, (x, y, z), relative to a source
152   //! coordinate system into the coordinates (x', y', z') which
153   //! are relative to a target coordinate system, but which
154   //! represent the same point
155   //! The transformation is from the coordinate
156   //! system "FromSystem1" to the coordinate system "ToSystem2".
157   //! Example :
158   //! In a C++ implementation :
159   //! Real x1, y1, z1;  // are the coordinates of a point in the
160   //! // local system FromSystem1
161   //! Real x2, y2, z2;  // are the coordinates of a point in the
162   //! // local system ToSystem2
163   //! gp_Pnt P1 (x1, y1, z1)
164   //! Trsf T;
165   //! T.SetTransformation (FromSystem1, ToSystem2);
166   //! gp_Pnt P2 = P1.Transformed (T);
167   //! P2.Coord (x2, y2, z2);
168   Standard_EXPORT void SetTransformation (const gp_Ax3& FromSystem1, const gp_Ax3& ToSystem2);
169   
170   //! Modifies this transformation so that it transforms the
171   //! coordinates of any point, (x, y, z), relative to a source
172   //! coordinate system into the coordinates (x', y', z') which
173   //! are relative to a target coordinate system, but which
174   //! represent the same point
175   //! The transformation is from the default coordinate system
176   //! {P(0.,0.,0.), VX (1.,0.,0.), VY (0.,1.,0.), VZ (0., 0. ,1.) }
177   //! to the local coordinate system defined with the Ax3 ToSystem.
178   //! Use in the same way  as the previous method. FromSystem1 is
179   //! defaulted to the absolute coordinate system.
180   Standard_EXPORT void SetTransformation (const gp_Ax3& ToSystem);
181   
182
183   //! Sets transformation by directly specified rotation and translation.
184   Standard_EXPORT void SetTransformation (const gp_Quaternion& R, const gp_Vec& T);
185   
186
187   //! Changes the transformation into a translation.
188   //! V is the vector of the translation.
189     void SetTranslation (const gp_Vec& V);
190   
191
192   //! Makes the transformation into a translation where the translation vector
193   //! is the vector (P1, P2) defined from point P1 to point P2.
194     void SetTranslation (const gp_Pnt& P1, const gp_Pnt& P2);
195   
196   //! Replaces the translation vector with the vector V.
197   Standard_EXPORT void SetTranslationPart (const gp_Vec& V);
198   
199   //! Modifies the scale factor.
200   //! Raises ConstructionError  If S is null.
201   Standard_EXPORT void SetScaleFactor (const Standard_Real S);
202   
203   void SetForm (const gp_TrsfForm P);
204   
205   //! Sets the coefficients  of the transformation.  The
206   //! transformation  of the  point  x,y,z is  the point
207   //! x',y',z' with :
208   //!
209   //! x' = a11 x + a12 y + a13 z + a14
210   //! y' = a21 x + a22 y + a23 z + a24
211   //! z' = a31 x + a32 y + a33 z + a34
212   //!
213   //! The method Value(i,j) will return aij.
214   //! Raises ConstructionError if the determinant of  the aij is null.
215   //! The matrix is orthogonalized before future using.
216   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);
217   
218   //! Returns true if the determinant of the vectorial part of
219   //! this transformation is negative.
220     Standard_Boolean IsNegative() const;
221   
222
223   //! Returns the nature of the transformation. It can be: an
224   //! identity transformation, a rotation, a translation, a mirror
225   //! transformation (relative to a point, an axis or a plane), a
226   //! scaling transformation, or a compound transformation.
227     gp_TrsfForm Form() const;
228   
229   //! Returns the scale factor.
230     Standard_Real ScaleFactor() const;
231   
232
233   //! Returns the translation part of the transformation's matrix
234     const gp_XYZ& TranslationPart() const;
235   
236
237   //! Returns the boolean True if there is non-zero rotation.
238   //! In the presence of rotation, the output parameters store the axis
239   //! and the angle of rotation. The method always returns positive
240   //! value "theAngle", i.e., 0. < theAngle <= PI.
241   //! Note that this rotation is defined only by the vectorial part of
242   //! the transformation; generally you would need to check also the
243   //! translational part to obtain the axis (gp_Ax1) of rotation.
244   Standard_EXPORT Standard_Boolean GetRotation (gp_XYZ& theAxis, Standard_Real& theAngle) const;
245   
246
247   //! Returns quaternion representing rotational part of the transformation.
248   Standard_EXPORT gp_Quaternion GetRotation() const;
249   
250
251   //! Returns the vectorial part of the transformation. It is
252   //! a 3*3 matrix which includes the scale factor.
253   Standard_EXPORT gp_Mat VectorialPart() const;
254   
255
256   //! Computes the homogeneous vectorial part of the transformation.
257   //! It is a 3*3 matrix which doesn't include the scale factor.
258   //! In other words, the vectorial part of this transformation is equal
259   //! to its homogeneous vectorial part, multiplied by the scale factor.
260   //! The coefficients of this matrix must be multiplied by the
261   //! scale factor to obtain the coefficients of the transformation.
262     const gp_Mat& HVectorialPart() const;
263   
264
265   //! Returns the coefficients of the transformation's matrix.
266   //! It is a 3 rows * 4 columns matrix.
267   //! This coefficient includes the scale factor.
268   //! Raises OutOfRanged if Row < 1 or Row > 3 or Col < 1 or Col > 4
269     Standard_Real Value (const Standard_Integer Row, const Standard_Integer Col) const;
270   
271   Standard_EXPORT void Invert();
272   
273
274   //! Computes the reverse transformation
275   //! Raises an exception if the matrix of the transformation
276   //! is not inversible, it means that the scale factor is lower
277   //! or equal to Resolution from package gp.
278   //! Computes the transformation composed with T and  <me>.
279   //! In a C++ implementation you can also write Tcomposed = <me> * T.
280   //! Example :
281   //! Trsf T1, T2, Tcomp; ...............
282   //! Tcomp = T2.Multiplied(T1);         // or   (Tcomp = T2 * T1)
283   //! Pnt P1(10.,3.,4.);
284   //! Pnt P2 = P1.Transformed(Tcomp);    //using Tcomp
285   //! Pnt P3 = P1.Transformed(T1);       //using T1 then T2
286   //! P3.Transform(T2);                  // P3 = P2 !!!
287   Standard_NODISCARD gp_Trsf Inverted() const;
288   
289   Standard_NODISCARD gp_Trsf Multiplied (const gp_Trsf& T) const;
290   Standard_NODISCARD gp_Trsf operator * (const gp_Trsf& T) const
291 {
292   return Multiplied(T);
293 }
294   
295
296   //! Computes the transformation composed with <me> and T.
297   //! <me> = <me> * T
298   Standard_EXPORT void Multiply (const gp_Trsf& T);
299 void operator *= (const gp_Trsf& T)
300 {
301   Multiply(T);
302 }
303   
304
305   //! Computes the transformation composed with <me> and T.
306   //! <me> = T * <me>
307   Standard_EXPORT void PreMultiply (const gp_Trsf& T);
308   
309   Standard_EXPORT void Power (const Standard_Integer N);
310   
311
312   //! Computes the following composition of transformations
313   //! <me> * <me> * .......* <me>, N time.
314   //! if N = 0 <me> = Identity
315   //! if N < 0 <me> = <me>.Inverse() *...........* <me>.Inverse().
316   //!
317   //! Raises if N < 0 and if the matrix of the transformation not
318   //! inversible.
319   Standard_NODISCARD gp_Trsf Powered (const Standard_Integer N) const;
320   
321     void Transforms (Standard_Real& X, Standard_Real& Y, Standard_Real& Z) const;
322   
323   //! Transformation of a triplet XYZ with a Trsf
324     void Transforms (gp_XYZ& Coord) const;
325
326   //! Convert transformation to 4x4 matrix.
327   template<class T>
328   void GetMat4 (NCollection_Mat4<T>& theMat) const
329   {
330     if (shape == gp_Identity)
331     {
332       theMat.InitIdentity();
333       return;
334     }
335
336     theMat.SetValue (0, 0, static_cast<T> (Value (1, 1)));
337     theMat.SetValue (0, 1, static_cast<T> (Value (1, 2)));
338     theMat.SetValue (0, 2, static_cast<T> (Value (1, 3)));
339     theMat.SetValue (0, 3, static_cast<T> (Value (1, 4)));
340     theMat.SetValue (1, 0, static_cast<T> (Value (2, 1)));
341     theMat.SetValue (1, 1, static_cast<T> (Value (2, 2)));
342     theMat.SetValue (1, 2, static_cast<T> (Value (2, 3)));
343     theMat.SetValue (1, 3, static_cast<T> (Value (2, 4)));
344     theMat.SetValue (2, 0, static_cast<T> (Value (3, 1)));
345     theMat.SetValue (2, 1, static_cast<T> (Value (3, 2)));
346     theMat.SetValue (2, 2, static_cast<T> (Value (3, 3)));
347     theMat.SetValue (2, 3, static_cast<T> (Value (3, 4)));
348     theMat.SetValue (3, 0, static_cast<T> (0));
349     theMat.SetValue (3, 1, static_cast<T> (0));
350     theMat.SetValue (3, 2, static_cast<T> (0));
351     theMat.SetValue (3, 3, static_cast<T> (1));
352   }
353
354   //! Dumps the content of me into the stream
355   Standard_EXPORT void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const;
356
357 friend class gp_GTrsf;
358
359 protected:
360
361   //! Makes orthogonalization of "matrix"
362   Standard_EXPORT void Orthogonalize();
363
364 private:
365
366   Standard_Real scale;
367   gp_TrsfForm shape;
368   gp_Mat matrix;
369   gp_XYZ loc;
370
371 };
372
373 #include <gp_Trsf.lxx>
374
375 #endif // _gp_Trsf_HeaderFile