7bfde6c8d60e7a27c6d9bc74d8f73a6f90c7f19f
[occt.git] / src / gp / gp_GTrsf.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_GTrsf_HeaderFile
16 #define _gp_GTrsf_HeaderFile
17
18 #include <Standard.hxx>
19 #include <Standard_DefineAlloc.hxx>
20 #include <Standard_Handle.hxx>
21
22 #include <gp_Mat.hxx>
23 #include <gp_XYZ.hxx>
24 #include <gp_TrsfForm.hxx>
25 #include <Standard_Real.hxx>
26 #include <Standard_Integer.hxx>
27 #include <Standard_Boolean.hxx>
28 #include <gp_Trsf.hxx>
29 class Standard_ConstructionError;
30 class Standard_OutOfRange;
31 class gp_Trsf;
32 class gp_Mat;
33 class gp_XYZ;
34 class gp_Ax1;
35 class gp_Ax2;
36
37 // Avoid possible conflict with SetForm macro defined by windows.h
38 #ifdef SetForm
39 #undef SetForm
40 #endif
41
42 //! Defines a non-persistent transformation in 3D space.
43 //! This transformation is a general transformation.
44 //! It can be a gp_Trsf, an affinity, or you can define
45 //! your own transformation giving the matrix of transformation.
46 //!
47 //! With a gp_GTrsf you can transform only a triplet of coordinates gp_XYZ.
48 //! It is not possible to transform other geometric objects
49 //! because these transformations can change the nature of non-elementary geometric objects.
50 //! The transformation gp_GTrsf can be represented as follow:
51 //! @code
52 //!    V1   V2   V3    T       XYZ        XYZ
53 //! | a11  a12  a13   a14 |   | x |      | x'|
54 //! | a21  a22  a23   a24 |   | y |      | y'|
55 //! | a31  a32  a33   a34 |   | z |   =  | z'|
56 //! |  0    0    0     1  |   | 1 |      | 1 |
57 //! @endcode
58 //! where {V1, V2, V3} define the vectorial part of the
59 //! transformation and T defines the translation part of the transformation.
60 //! Warning
61 //! A gp_GTrsf transformation is only applicable to coordinates.
62 //! Be careful if you apply such a transformation to all points of a geometric object,
63 //! as this can change the nature of the object and thus render it incoherent!
64 //! Typically, a circle is transformed into an ellipse by an affinity transformation.
65 //! To avoid modifying the nature of an object, use a gp_Trsf transformation instead,
66 //! as objects of this class respect the nature of geometric objects.
67 class gp_GTrsf 
68 {
69 public:
70
71   DEFINE_STANDARD_ALLOC
72
73   
74   //! Returns the Identity transformation.
75     gp_GTrsf();
76   
77
78   //! Converts the gp_Trsf transformation T into a
79   //! general transformation, i.e. Returns a GTrsf with
80   //! the same matrix of coefficients as the Trsf T.
81     gp_GTrsf(const gp_Trsf& T);
82   
83
84   //! Creates a transformation based on the matrix M and the
85   //! vector V where M defines the vectorial part of
86   //! the transformation, and V the translation part, or
87     gp_GTrsf(const gp_Mat& M, const gp_XYZ& V);
88   
89   //! Changes this transformation into an affinity of ratio Ratio
90   //! with respect to the axis A1.
91   //! Note: an affinity is a point-by-point transformation that
92   //! transforms any point P into a point P' such that if H is
93   //! the orthogonal projection of P on the axis A1 or the
94   //! plane A2, the vectors HP and HP' satisfy:
95   //! HP' = Ratio * HP.
96     void SetAffinity (const gp_Ax1& A1, const Standard_Real Ratio);
97   
98   //! Changes this transformation into an affinity of ratio Ratio
99   //! with respect to  the plane defined by the origin, the "X Direction" and
100   //! the "Y Direction" of coordinate system A2.
101   //! Note: an affinity is a point-by-point transformation that
102   //! transforms any point P into a point P' such that if H is
103   //! the orthogonal projection of P on the axis A1 or the
104   //! plane A2, the vectors HP and HP' satisfy:
105   //! HP' = Ratio * HP.
106     void SetAffinity (const gp_Ax2& A2, const Standard_Real Ratio);
107   
108
109   //! Replaces  the coefficient (Row, Col) of the matrix representing
110   //! this transformation by Value.  Raises OutOfRange
111   //! if  Row < 1 or Row > 3 or Col < 1 or Col > 4
112     void SetValue (const Standard_Integer Row, const Standard_Integer Col, const Standard_Real Value);
113   
114   //! Replaces the vectorial part of this transformation by Matrix.
115     void SetVectorialPart (const gp_Mat& Matrix);
116   
117   //! Replaces the translation part of
118   //! this transformation by the coordinates of the number triple Coord.
119   Standard_EXPORT void SetTranslationPart (const gp_XYZ& Coord);
120   
121   //! Assigns the vectorial and translation parts of T to this transformation.
122     void SetTrsf (const gp_Trsf& T);
123   
124
125   //! Returns true if the determinant of the vectorial part of
126   //! this transformation is negative.
127     Standard_Boolean IsNegative() const;
128   
129
130   //! Returns true if this transformation is singular (and
131   //! therefore, cannot be inverted).
132   //! Note: The Gauss LU decomposition is used to invert the
133   //! transformation matrix. Consequently, the transformation
134   //! is considered as singular if the largest pivot found is less
135   //! than or equal to gp::Resolution().
136   //! Warning
137   //! If this transformation is singular, it cannot be inverted.
138     Standard_Boolean IsSingular() const;
139   
140
141   //! Returns the nature of the transformation.  It can be an
142   //! identity transformation, a rotation, a translation, a mirror
143   //! transformation (relative to a point, an axis or a plane), a
144   //! scaling transformation, a compound transformation or
145   //! some other type of transformation.
146   gp_TrsfForm Form() const;
147   
148
149   //! verify and set the shape of the GTrsf Other or CompoundTrsf
150   //! Ex :
151   //! @code
152   //! myGTrsf.SetValue(row1,col1,val1);
153   //! myGTrsf.SetValue(row2,col2,val2);
154   //! ...
155   //! myGTrsf.SetForm();
156   //! @endcode
157   Standard_EXPORT void SetForm();
158   
159   //! Returns the translation part of the GTrsf.
160     const gp_XYZ& TranslationPart() const;
161   
162
163   //! Computes the vectorial part of the GTrsf. The returned Matrix
164   //! is a  3*3 matrix.
165     const gp_Mat& VectorialPart() const;
166   
167
168   //! Returns the coefficients of the global matrix of transformation.
169   //! Raises OutOfRange if Row < 1 or Row > 3 or Col < 1 or Col > 4
170     Standard_Real Value (const Standard_Integer Row, const Standard_Integer Col) const;
171   Standard_Real operator() (const Standard_Integer Row, const Standard_Integer Col) const
172 {
173   return Value(Row,Col);
174 }
175   
176   Standard_EXPORT void Invert();
177   
178
179   //! Computes the reverse transformation.
180   //! Raises an exception if the matrix of the transformation
181   //! is not inversible.
182   Standard_NODISCARD gp_GTrsf Inverted() const;
183   
184
185   //! Computes the transformation composed from T and <me>.
186   //! In a C++ implementation you can also write Tcomposed = <me> * T.
187   //! Example :
188   //! @code
189   //! gp_GTrsf T1, T2, Tcomp; ...............
190   //! //composition :
191   //! Tcomp = T2.Multiplied(T1);         // or   (Tcomp = T2 * T1)
192   //! // transformation of a point
193   //! gp_XYZ P(10.,3.,4.);
194   //! gp_XYZ P1(P);
195   //! Tcomp.Transforms(P1);               //using Tcomp
196   //! gp_XYZ P2(P);
197   //! T1.Transforms(P2);                  //using T1 then T2
198   //! T2.Transforms(P2);                  // P1 = P2 !!!
199   //! @endcode
200   Standard_NODISCARD gp_GTrsf Multiplied (const gp_GTrsf& T) const;
201   Standard_NODISCARD gp_GTrsf operator * (const gp_GTrsf& T)  const
202   {
203     return Multiplied(T);
204   }
205   
206
207   //! Computes the transformation composed with <me> and T.
208   //! <me> = <me> * T
209   Standard_EXPORT void Multiply (const gp_GTrsf& T);
210   void operator *= (const gp_GTrsf& T) 
211   {
212     Multiply(T);
213   }
214   
215
216   //! Computes the product of the transformation T and this
217   //! transformation and assigns the result to this transformation.
218   //! this = T * this
219   Standard_EXPORT void PreMultiply (const gp_GTrsf& T);
220   
221   Standard_EXPORT void Power (const Standard_Integer N);
222   
223
224   //! Computes:
225   //! -   the product of this transformation multiplied by itself
226   //! N times, if N is positive, or
227   //! -   the product of the inverse of this transformation
228   //! multiplied by itself |N| times, if N is negative.
229   //! If N equals zero, the result is equal to the Identity
230   //! transformation.
231   //! I.e.:  <me> * <me> * .......* <me>, N time.
232   //! if N =0 <me> = Identity
233   //! if N < 0 <me> = <me>.Inverse() *...........* <me>.Inverse().
234   //!
235   //! Raises an exception if N < 0 and if the matrix of the
236   //! transformation not inversible.
237   Standard_NODISCARD gp_GTrsf Powered (const Standard_Integer N) const;
238   
239     void Transforms (gp_XYZ& Coord) const;
240   
241   //! Transforms a triplet XYZ with a GTrsf.
242     void Transforms (Standard_Real& X, Standard_Real& Y, Standard_Real& Z) const;
243   
244     gp_Trsf Trsf() const;
245
246   //! Convert transformation to 4x4 matrix.
247   template<class T>
248   void GetMat4 (NCollection_Mat4<T>& theMat) const
249   {
250     if (shape == gp_Identity)
251     {
252       theMat.InitIdentity();
253       return;
254     }
255
256     theMat.SetValue (0, 0, static_cast<T> (Value (1, 1)));
257     theMat.SetValue (0, 1, static_cast<T> (Value (1, 2)));
258     theMat.SetValue (0, 2, static_cast<T> (Value (1, 3)));
259     theMat.SetValue (0, 3, static_cast<T> (Value (1, 4)));
260     theMat.SetValue (1, 0, static_cast<T> (Value (2, 1)));
261     theMat.SetValue (1, 1, static_cast<T> (Value (2, 2)));
262     theMat.SetValue (1, 2, static_cast<T> (Value (2, 3)));
263     theMat.SetValue (1, 3, static_cast<T> (Value (2, 4)));
264     theMat.SetValue (2, 0, static_cast<T> (Value (3, 1)));
265     theMat.SetValue (2, 1, static_cast<T> (Value (3, 2)));
266     theMat.SetValue (2, 2, static_cast<T> (Value (3, 3)));
267     theMat.SetValue (2, 3, static_cast<T> (Value (3, 4)));
268     theMat.SetValue (3, 0, static_cast<T> (0));
269     theMat.SetValue (3, 1, static_cast<T> (0));
270     theMat.SetValue (3, 2, static_cast<T> (0));
271     theMat.SetValue (3, 3, static_cast<T> (1));
272   }
273
274   //! Dumps the content of me into the stream
275   Standard_EXPORT void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const;
276
277 private:
278
279   gp_Mat matrix;
280   gp_XYZ loc;
281   gp_TrsfForm shape;
282   Standard_Real scale;
283
284 };
285
286
287 #include <gp_GTrsf.lxx>
288
289
290
291
292
293 #endif // _gp_GTrsf_HeaderFile