Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:03:45

0001 // Copyright (c) 1991-1999 Matra Datavision
0002 // Copyright (c) 1999-2014 OPEN CASCADE SAS
0003 //
0004 // This file is part of Open CASCADE Technology software library.
0005 //
0006 // This library is free software; you can redistribute it and/or modify it under
0007 // the terms of the GNU Lesser General Public License version 2.1 as published
0008 // by the Free Software Foundation, with special exception defined in the file
0009 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0010 // distribution for complete text of the license and disclaimer of any warranty.
0011 //
0012 // Alternatively, this file may be used under the terms of Open CASCADE
0013 // commercial license or contractual agreement.
0014 
0015 #ifndef _gp_Trsf_HeaderFile
0016 #define _gp_Trsf_HeaderFile
0017 
0018 #include <gp_TrsfForm.hxx>
0019 #include <gp_Mat.hxx>
0020 #include <gp_XYZ.hxx>
0021 #include <NCollection_Mat4.hxx>
0022 #include <Standard_OStream.hxx>
0023 #include <Standard_OutOfRange.hxx>
0024 #include <Standard_SStream.hxx>
0025 
0026 class gp_Pnt;
0027 class gp_Trsf2d;
0028 class gp_Ax1;
0029 class gp_Ax2;
0030 class gp_Quaternion;
0031 class gp_Ax3;
0032 class gp_Vec;
0033 
0034 // Avoid possible conflict with SetForm macro defined by windows.h
0035 #ifdef SetForm
0036 #undef SetForm
0037 #endif
0038 
0039 //! Defines a non-persistent transformation in 3D space.
0040 //! The following transformations are implemented :
0041 //! . Translation, Rotation, Scale
0042 //! . Symmetry with respect to a point, a line, a plane.
0043 //! Complex transformations can be obtained by combining the
0044 //! previous elementary transformations using the method
0045 //! Multiply.
0046 //! The transformations can be represented as follow :
0047 //! @code
0048 //!    V1   V2   V3    T       XYZ        XYZ
0049 //! | a11  a12  a13   a14 |   | x |      | x'|
0050 //! | a21  a22  a23   a24 |   | y |      | y'|
0051 //! | a31  a32  a33   a34 |   | z |   =  | z'|
0052 //! |  0    0    0     1  |   | 1 |      | 1 |
0053 //! @endcode
0054 //! where {V1, V2, V3} defines the vectorial part of the
0055 //! transformation and T defines the translation part of the
0056 //! transformation.
0057 //! This transformation never change the nature of the objects.
0058 class gp_Trsf 
0059 {
0060 public:
0061 
0062   DEFINE_STANDARD_ALLOC
0063 
0064   //! Returns the identity transformation.
0065   gp_Trsf();
0066 
0067   //! Creates  a 3D transformation from the 2D transformation theT.
0068   //! The resulting transformation has a homogeneous
0069   //! vectorial part, V3, and a translation part, T3, built from theT:
0070   //! a11    a12
0071   //! 0             a13
0072   //! V3 =    a21    a22    0       T3
0073   //! =   a23
0074   //! 0    0    1.
0075   //! 0
0076   //! It also has the same scale factor as theT. This
0077   //! guarantees (by projection) that the transformation
0078   //! which would be performed by theT in a plane (2D space)
0079   //! is performed by the resulting transformation in the xOy
0080   //! plane of the 3D space, (i.e. in the plane defined by the
0081   //! origin (0., 0., 0.) and the vectors DX (1., 0., 0.), and DY
0082   //! (0., 1., 0.)). The scale factor is applied to the entire space.
0083   Standard_EXPORT gp_Trsf (const gp_Trsf2d& theT);
0084 
0085   //! Makes the transformation into a symmetrical transformation.
0086   //! theP is the center of the symmetry.
0087   void SetMirror (const gp_Pnt& theP);
0088 
0089   //! Makes the transformation into a symmetrical transformation.
0090   //! theA1 is the center of the axial symmetry.
0091   Standard_EXPORT void SetMirror (const gp_Ax1& theA1);
0092 
0093   //! Makes the transformation into a symmetrical transformation.
0094   //! theA2 is the center of the planar symmetry
0095   //! and defines the plane of symmetry by its origin, "X
0096   //! Direction" and "Y Direction".
0097   Standard_EXPORT void SetMirror (const gp_Ax2& theA2);
0098 
0099   //! Changes the transformation into a rotation.
0100   //! theA1 is the rotation axis and theAng is the angular value of the
0101   //! rotation in radians.
0102   Standard_EXPORT void SetRotation (const gp_Ax1& theA1, const Standard_Real theAng);
0103 
0104   //! Changes the transformation into a rotation defined by quaternion.
0105   //! Note that rotation is performed around origin, i.e.
0106   //! no translation is involved.
0107   Standard_EXPORT void SetRotation (const gp_Quaternion& theR);
0108 
0109   //! Replaces the rotation part with specified quaternion.
0110   Standard_EXPORT void SetRotationPart (const gp_Quaternion& theR);
0111 
0112   //! Changes the transformation into a scale.
0113   //! theP is the center of the scale and theS is the scaling value.
0114   //! Raises ConstructionError  If <theS> is null.
0115   Standard_EXPORT void SetScale (const gp_Pnt& theP, const Standard_Real theS);
0116 
0117   //! Modifies this transformation so that it transforms the
0118   //! coordinate system defined by theFromSystem1 into the
0119   //! one defined by theToSystem2. After this modification, this
0120   //! transformation transforms:
0121   //! -   the origin of theFromSystem1 into the origin of theToSystem2,
0122   //! -   the "X Direction" of theFromSystem1 into the "X
0123   //! Direction" of theToSystem2,
0124   //! -   the "Y Direction" of theFromSystem1 into the "Y
0125   //! Direction" of theToSystem2, and
0126   //! -   the "main Direction" of theFromSystem1 into the "main
0127   //! Direction" of theToSystem2.
0128   //! Warning
0129   //! When you know the coordinates of a point in one
0130   //! coordinate system and you want to express these
0131   //! coordinates in another one, do not use the
0132   //! transformation resulting from this function. Use the
0133   //! transformation that results from SetTransformation instead.
0134   //! SetDisplacement and SetTransformation create
0135   //! related transformations: the vectorial part of one is the
0136   //! inverse of the vectorial part of the other.
0137   Standard_EXPORT void SetDisplacement (const gp_Ax3& theFromSystem1, const gp_Ax3& theToSystem2);
0138 
0139   //! Modifies this transformation so that it transforms the
0140   //! coordinates of any point, (x, y, z), relative to a source
0141   //! coordinate system into the coordinates (x', y', z') which
0142   //! are relative to a target coordinate system, but which
0143   //! represent the same point
0144   //! The transformation is from the coordinate
0145   //! system "theFromSystem1" to the coordinate system "theToSystem2".
0146   //! Example :
0147   //! @code
0148   //! gp_Ax3 theFromSystem1, theToSystem2;
0149   //! double x1, y1, z1;  // are the coordinates of a point in the local system theFromSystem1
0150   //! double x2, y2, z2;  // are the coordinates of a point in the local system theToSystem2
0151   //! gp_Pnt P1 (x1, y1, z1)
0152   //! gp_Trsf T;
0153   //! T.SetTransformation (theFromSystem1, theToSystem2);
0154   //! gp_Pnt P2 = P1.Transformed (T);
0155   //! P2.Coord (x2, y2, z2);
0156   //! @endcode
0157   Standard_EXPORT void SetTransformation (const gp_Ax3& theFromSystem1, const gp_Ax3& theToSystem2);
0158 
0159   //! Modifies this transformation so that it transforms the
0160   //! coordinates of any point, (x, y, z), relative to a source
0161   //! coordinate system into the coordinates (x', y', z') which
0162   //! are relative to a target coordinate system, but which
0163   //! represent the same point
0164   //! The transformation is from the default coordinate system
0165   //! @code
0166   //! {P(0.,0.,0.), VX (1.,0.,0.), VY (0.,1.,0.), VZ (0., 0. ,1.) }
0167   //! @endcode
0168   //! to the local coordinate system defined with the Ax3 theToSystem.
0169   //! Use in the same way  as the previous method. FromSystem1 is
0170   //! defaulted to the absolute coordinate system.
0171   Standard_EXPORT void SetTransformation (const gp_Ax3& theToSystem);
0172 
0173   //! Sets transformation by directly specified rotation and translation.
0174   Standard_EXPORT void SetTransformation (const gp_Quaternion& R, const gp_Vec& theT);
0175 
0176   //! Changes the transformation into a translation.
0177   //! theV is the vector of the translation.
0178   void SetTranslation (const gp_Vec& theV);
0179 
0180   //! Makes the transformation into a translation where the translation vector
0181   //! is the vector (theP1, theP2) defined from point theP1 to point theP2.
0182   void SetTranslation (const gp_Pnt& theP1, const gp_Pnt& theP2);
0183 
0184   //! Replaces the translation vector with the vector theV.
0185   Standard_EXPORT void SetTranslationPart (const gp_Vec& theV);
0186 
0187   //! Modifies the scale factor.
0188   //! Raises ConstructionError  If theS is null.
0189   Standard_EXPORT void SetScaleFactor (const Standard_Real theS);
0190 
0191   void SetForm (const gp_TrsfForm theP) { shape = theP; }
0192 
0193   //! Sets the coefficients  of the transformation.  The
0194   //! transformation  of the  point  x,y,z is  the point
0195   //! x',y',z' with :
0196   //! @code
0197   //! x' = a11 x + a12 y + a13 z + a14
0198   //! y' = a21 x + a22 y + a23 z + a24
0199   //! z' = a31 x + a32 y + a33 z + a34
0200   //! @endcode
0201   //! The method Value(i,j) will return aij.
0202   //! Raises ConstructionError if the determinant of  the aij is null.
0203   //! The matrix is orthogonalized before future using.
0204   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);
0205 
0206   //! Returns true if the determinant of the vectorial part of
0207   //! this transformation is negative.
0208   Standard_Boolean IsNegative() const { return (scale < 0.0); }
0209 
0210   //! Returns the nature of the transformation. It can be: an
0211   //! identity transformation, a rotation, a translation, a mirror
0212   //! transformation (relative to a point, an axis or a plane), a
0213   //! scaling transformation, or a compound transformation.
0214   gp_TrsfForm Form() const { return shape; }
0215 
0216   //! Returns the scale factor.
0217   Standard_Real ScaleFactor() const { return scale; }
0218 
0219   //! Returns the translation part of the transformation's matrix
0220   const gp_XYZ& TranslationPart() const { return loc; }
0221 
0222   //! Returns the boolean True if there is non-zero rotation.
0223   //! In the presence of rotation, the output parameters store the axis
0224   //! and the angle of rotation. The method always returns positive
0225   //! value "theAngle", i.e., 0. < theAngle <= PI.
0226   //! Note that this rotation is defined only by the vectorial part of
0227   //! the transformation; generally you would need to check also the
0228   //! translational part to obtain the axis (gp_Ax1) of rotation.
0229   Standard_EXPORT Standard_Boolean GetRotation (gp_XYZ& theAxis, Standard_Real& theAngle) const;
0230 
0231   //! Returns quaternion representing rotational part of the transformation.
0232   Standard_EXPORT gp_Quaternion GetRotation() const;
0233 
0234   //! Returns the vectorial part of the transformation. It is
0235   //! a 3*3 matrix which includes the scale factor.
0236   Standard_EXPORT gp_Mat VectorialPart() const;
0237 
0238   //! Computes the homogeneous vectorial part of the transformation.
0239   //! It is a 3*3 matrix which doesn't include the scale factor.
0240   //! In other words, the vectorial part of this transformation is equal
0241   //! to its homogeneous vectorial part, multiplied by the scale factor.
0242   //! The coefficients of this matrix must be multiplied by the
0243   //! scale factor to obtain the coefficients of the transformation.
0244   const gp_Mat& HVectorialPart() const { return matrix; }
0245 
0246   //! Returns the coefficients of the transformation's matrix.
0247   //! It is a 3 rows * 4 columns matrix.
0248   //! This coefficient includes the scale factor.
0249   //! Raises OutOfRanged if theRow < 1 or theRow > 3 or theCol < 1 or theCol > 4
0250   Standard_Real Value (const Standard_Integer theRow, const Standard_Integer theCol) const;
0251 
0252   Standard_EXPORT void Invert();
0253 
0254   //! Computes the reverse transformation
0255   //! Raises an exception if the matrix of the transformation
0256   //! is not inversible, it means that the scale factor is lower
0257   //! or equal to Resolution from package gp.
0258   //! Computes the transformation composed with T and  <me>.
0259   //! In a C++ implementation you can also write Tcomposed = <me> * T.
0260   //! Example :
0261   //! @code
0262   //! gp_Trsf T1, T2, Tcomp; ...............
0263   //! Tcomp = T2.Multiplied(T1);         // or   (Tcomp = T2 * T1)
0264   //! gp_Pnt P1(10.,3.,4.);
0265   //! gp_Pnt P2 = P1.Transformed(Tcomp); // using Tcomp
0266   //! gp_Pnt P3 = P1.Transformed(T1);    // using T1 then T2
0267   //! P3.Transform(T2);                  // P3 = P2 !!!
0268   //! @endcode
0269   Standard_NODISCARD gp_Trsf Inverted() const
0270   {
0271     gp_Trsf aT = *this;
0272     aT.Invert();
0273     return aT;
0274   }
0275   
0276   Standard_NODISCARD gp_Trsf Multiplied (const gp_Trsf& theT) const
0277   {
0278     gp_Trsf aTresult (*this);
0279     aTresult.Multiply (theT);
0280     return aTresult;
0281   }
0282 
0283   Standard_NODISCARD gp_Trsf operator * (const gp_Trsf& theT) const { return Multiplied (theT); }
0284 
0285   //! Computes the transformation composed with <me> and theT.
0286   //! <me> = <me> * theT
0287   Standard_EXPORT void Multiply (const gp_Trsf& theT);
0288 
0289   void operator *= (const gp_Trsf& theT) { Multiply (theT); }
0290 
0291   //! Computes the transformation composed with <me> and T.
0292   //! <me> = theT * <me>
0293   Standard_EXPORT void PreMultiply (const gp_Trsf& theT);
0294 
0295   Standard_EXPORT void Power (const Standard_Integer theN);
0296 
0297   //! Computes the following composition of transformations
0298   //! <me> * <me> * .......* <me>, theN time.
0299   //! if theN = 0 <me> = Identity
0300   //! if theN < 0 <me> = <me>.Inverse() *...........* <me>.Inverse().
0301   //!
0302   //! Raises if theN < 0 and if the matrix of the transformation not
0303   //! inversible.
0304   Standard_NODISCARD gp_Trsf Powered (const Standard_Integer theN) const
0305   {
0306     gp_Trsf aT = *this;
0307     aT.Power (theN);
0308     return aT;
0309   }
0310 
0311   void Transforms (Standard_Real& theX, Standard_Real& theY, Standard_Real& theZ) const;
0312 
0313   //! Transformation of a triplet XYZ with a Trsf
0314   void Transforms (gp_XYZ& theCoord) const;
0315 
0316   //! Convert transformation to 4x4 matrix.
0317   template<class T>
0318   void GetMat4 (NCollection_Mat4<T>& theMat) const
0319   {
0320     if (shape == gp_Identity)
0321     {
0322       theMat.InitIdentity();
0323       return;
0324     }
0325 
0326     theMat.SetValue (0, 0, static_cast<T> (Value (1, 1)));
0327     theMat.SetValue (0, 1, static_cast<T> (Value (1, 2)));
0328     theMat.SetValue (0, 2, static_cast<T> (Value (1, 3)));
0329     theMat.SetValue (0, 3, static_cast<T> (Value (1, 4)));
0330     theMat.SetValue (1, 0, static_cast<T> (Value (2, 1)));
0331     theMat.SetValue (1, 1, static_cast<T> (Value (2, 2)));
0332     theMat.SetValue (1, 2, static_cast<T> (Value (2, 3)));
0333     theMat.SetValue (1, 3, static_cast<T> (Value (2, 4)));
0334     theMat.SetValue (2, 0, static_cast<T> (Value (3, 1)));
0335     theMat.SetValue (2, 1, static_cast<T> (Value (3, 2)));
0336     theMat.SetValue (2, 2, static_cast<T> (Value (3, 3)));
0337     theMat.SetValue (2, 3, static_cast<T> (Value (3, 4)));
0338     theMat.SetValue (3, 0, static_cast<T> (0));
0339     theMat.SetValue (3, 1, static_cast<T> (0));
0340     theMat.SetValue (3, 2, static_cast<T> (0));
0341     theMat.SetValue (3, 3, static_cast<T> (1));
0342   }
0343 
0344   //! Dumps the content of me into the stream
0345   Standard_EXPORT void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const;
0346 
0347   //! Inits the content of me from the stream
0348   Standard_EXPORT Standard_Boolean InitFromJson (const Standard_SStream& theSStream, Standard_Integer& theStreamPos);
0349 
0350 friend class gp_GTrsf;
0351 
0352 protected:
0353 
0354   //! Makes orthogonalization of "matrix"
0355   Standard_EXPORT void Orthogonalize();
0356 
0357 private:
0358 
0359   Standard_Real scale;
0360   gp_TrsfForm shape;
0361   gp_Mat matrix;
0362   gp_XYZ loc;
0363 
0364 };
0365 
0366 #include <gp_Trsf2d.hxx>
0367 #include <gp_Vec.hxx>
0368 #include <gp_Pnt.hxx>
0369 
0370 //=======================================================================
0371 //function : gp_Trsf
0372 // purpose :
0373 //=======================================================================
0374 inline gp_Trsf::gp_Trsf ()
0375 : scale (1.0),
0376   shape (gp_Identity),
0377   matrix (1, 0, 0, 0, 1, 0, 0, 0, 1),
0378   loc (0.0, 0.0, 0.0)
0379 {}
0380 
0381 //=======================================================================
0382 //function : SetMirror
0383 // purpose :
0384 //=======================================================================
0385 inline void gp_Trsf::SetMirror (const gp_Pnt& theP)
0386 {
0387   shape = gp_PntMirror;
0388   scale = -1.0;
0389   loc = theP.XYZ();
0390   matrix.SetIdentity();
0391   loc.Multiply (2.0);
0392 }
0393 
0394 //=======================================================================
0395 //function : SetTranslation
0396 // purpose :
0397 //=======================================================================
0398 inline void gp_Trsf::SetTranslation (const gp_Vec& theV) 
0399 {
0400   shape = gp_Translation;
0401   scale = 1.;
0402   matrix.SetIdentity();
0403   loc = theV.XYZ();
0404 }
0405 
0406 //=======================================================================
0407 //function : SetTranslation
0408 // purpose :
0409 //=======================================================================
0410 inline void gp_Trsf::SetTranslation (const gp_Pnt& theP1,
0411                                      const gp_Pnt& theP2) 
0412 {
0413   shape = gp_Translation;
0414   scale = 1.0;
0415   matrix.SetIdentity();
0416   loc = (theP2.XYZ()).Subtracted (theP1.XYZ());
0417 }
0418 
0419 //=======================================================================
0420 //function : Value
0421 // purpose :
0422 //=======================================================================
0423 inline Standard_Real gp_Trsf::Value (const Standard_Integer theRow, const Standard_Integer theCol) const
0424 {
0425   Standard_OutOfRange_Raise_if (theRow < 1 || theRow > 3 || theCol < 1 || theCol > 4, " ");
0426   if (theCol < 4)
0427   {
0428     return scale * matrix.Value (theRow, theCol);
0429   }
0430   else
0431   {
0432     return loc.Coord (theRow);
0433   }
0434 }
0435 
0436 //=======================================================================
0437 //function : Transforms
0438 // purpose :
0439 //=======================================================================
0440 inline void gp_Trsf::Transforms (Standard_Real& theX,
0441                                  Standard_Real& theY,
0442                                  Standard_Real& theZ) const 
0443 {
0444   gp_XYZ aTriplet (theX, theY, theZ);
0445   aTriplet.Multiply (matrix);
0446   if (scale != 1.0)
0447   {
0448     aTriplet.Multiply (scale);
0449   }
0450   aTriplet.Add (loc);
0451   theX = aTriplet.X();
0452   theY = aTriplet.Y();
0453   theZ = aTriplet.Z();
0454 }
0455 
0456 //=======================================================================
0457 //function : Transforms
0458 // purpose :
0459 //=======================================================================
0460 inline void gp_Trsf::Transforms (gp_XYZ& theCoord) const
0461 {
0462   theCoord.Multiply (matrix);
0463   if (scale != 1.0)
0464   {
0465     theCoord.Multiply (scale);
0466   }
0467   theCoord.Add (loc);
0468 }
0469 
0470 #endif // _gp_Trsf_HeaderFile