Back to home page

EIC code displayed by LXR

 
 

    


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

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_GTrsf2d_HeaderFile
0016 #define _gp_GTrsf2d_HeaderFile
0017 
0018 #include <gp_Ax2d.hxx>
0019 #include <gp_Mat2d.hxx>
0020 #include <gp_TrsfForm.hxx>
0021 #include <gp_XY.hxx>
0022 #include <Standard_OutOfRange.hxx>
0023 
0024 
0025 //! Defines a non persistent transformation in 2D space.
0026 //! This transformation is a general transformation.
0027 //! It can be a gp_Trsf2d, an affinity, or you can
0028 //! define your own transformation giving the corresponding matrix of transformation.
0029 //!
0030 //! With a gp_GTrsf2d you can transform only a doublet of coordinates gp_XY.
0031 //! It is not possible to transform other geometric objects
0032 //! because these transformations can change the nature of non-elementary geometric objects.
0033 //! A gp_GTrsf2d is represented with a 2 rows * 3 columns matrix:
0034 //! @code
0035 //!    V1   V2   T        XY         XY
0036 //! | a11  a12  a14 |   | x |      | x'|
0037 //! | a21  a22  a24 |   | y |   =  | y'|
0038 //! |  0    0    1  |   | 1 |      | 1 |
0039 //! @endcode
0040 //! where {V1, V2} defines the vectorial part of the
0041 //! transformation and T defines the translation part of the transformation.
0042 //! Warning
0043 //! A gp_GTrsf2d transformation is only applicable on coordinates.
0044 //! Be careful if you apply such a transformation to all the points of a geometric object,
0045 //! as this can change the nature of the object and thus render it incoherent!
0046 //! Typically, a circle is transformed into an ellipse by an affinity transformation.
0047 //! To avoid modifying the nature of an object, use a gp_Trsf2d transformation instead,
0048 //! as objects of this class respect the nature of geometric objects.
0049 class gp_GTrsf2d 
0050 {
0051 public:
0052 
0053   DEFINE_STANDARD_ALLOC
0054 
0055   //! returns identity transformation.
0056   gp_GTrsf2d()
0057   {
0058     shape = gp_Identity;
0059     matrix.SetScale (1.0);
0060     loc.SetCoord (0.0, 0.0);
0061     scale = 1.0;
0062   }
0063 
0064   //! Converts the gp_Trsf2d transformation theT into a
0065   //! general transformation.
0066   gp_GTrsf2d (const gp_Trsf2d& theT);
0067 
0068   //! Creates   a transformation based on the matrix theM and the
0069   //! vector theV where theM defines the vectorial part of the
0070   //! transformation, and theV the translation part.
0071   gp_GTrsf2d (const gp_Mat2d& theM, const gp_XY& theV)
0072   : matrix (theM),
0073     loc (theV)
0074   {
0075     shape = gp_Other;
0076     scale = 0.0;
0077   }
0078 
0079   //! Changes this transformation into an affinity of ratio theRatio
0080   //! with respect to the axis theA.
0081   //! Note: An affinity is a point-by-point transformation that
0082   //! transforms any point P into a point P' such that if H is
0083   //! the orthogonal projection of P on the axis theA, the vectors
0084   //! HP and HP' satisfy: HP' = theRatio * HP.
0085   Standard_EXPORT void SetAffinity (const gp_Ax2d& theA, const Standard_Real theRatio);
0086 
0087   //! Replaces   the coefficient (theRow, theCol) of the matrix representing
0088   //! this transformation by theValue,
0089   //! Raises OutOfRange if theRow < 1 or theRow > 2 or theCol < 1 or theCol > 3
0090   void SetValue (const Standard_Integer theRow, const Standard_Integer theCol, const Standard_Real theValue);
0091 
0092   //! Replaces the translation part of this
0093   //! transformation by the coordinates of the number pair theCoord.
0094   Standard_EXPORT void SetTranslationPart (const gp_XY& theCoord);
0095 
0096   //! Assigns the vectorial and translation parts of theT to this transformation.
0097   void SetTrsf2d (const gp_Trsf2d& theT);
0098 
0099   //! Replaces the vectorial part of this transformation by theMatrix.
0100   void SetVectorialPart (const gp_Mat2d& theMatrix)
0101   {
0102     matrix = theMatrix;
0103     shape = gp_Other;
0104     scale = 0.0;
0105   }
0106 
0107   //! Returns true if the determinant of the vectorial part of
0108   //! this transformation is negative.
0109   Standard_Boolean IsNegative() const { return matrix.Determinant() < 0.0; }
0110 
0111   //! Returns true if this transformation is singular (and
0112   //! therefore, cannot be inverted).
0113   //! Note: The Gauss LU decomposition is used to invert the
0114   //! transformation matrix. Consequently, the transformation
0115   //! is considered as singular if the largest pivot found is less
0116   //! than or equal to gp::Resolution().
0117   //! Warning
0118   //! If this transformation is singular, it cannot be inverted.
0119   Standard_Boolean IsSingular() const { return matrix.IsSingular(); }
0120 
0121   //! Returns the nature of the transformation.  It can be
0122   //! an identity transformation, a rotation, a translation, a mirror
0123   //! transformation (relative to a point or axis), a scaling
0124   //! transformation, a compound transformation or some
0125   //! other type of transformation.
0126   gp_TrsfForm Form() const { return shape; }
0127 
0128   //! Returns the translation part of the GTrsf2d.
0129   const gp_XY& TranslationPart() const { return loc; }
0130 
0131   //! Computes the vectorial part of the GTrsf2d. The returned
0132   //! Matrix is a 2*2 matrix.
0133   const gp_Mat2d& VectorialPart() const { return matrix; }
0134 
0135   //! Returns the coefficients of the global matrix of transformation.
0136   //! Raised OutOfRange if theRow < 1 or theRow > 2 or theCol < 1 or theCol > 3
0137   Standard_Real Value (const Standard_Integer theRow, const Standard_Integer theCol) const;
0138 
0139   Standard_Real operator() (const Standard_Integer theRow, const Standard_Integer theCol) const { return Value (theRow, theCol); }
0140 
0141   Standard_EXPORT void Invert();
0142 
0143   //! Computes the reverse transformation.
0144   //! Raised an exception if the matrix of the transformation
0145   //! is not inversible.
0146   Standard_NODISCARD gp_GTrsf2d Inverted() const
0147   {
0148     gp_GTrsf2d aT = *this;
0149     aT.Invert();
0150     return aT;
0151   }
0152 
0153   //! Computes the transformation composed with theT and <me>.
0154   //! In a C++ implementation you can also write Tcomposed = <me> * theT.
0155   //! Example :
0156   //! @code
0157   //! gp_GTrsf2d T1, T2, Tcomp; ...............
0158   //! //composition :
0159   //! Tcomp = T2.Multiplied(T1);         // or   (Tcomp = T2 * T1)
0160   //! // transformation of a point
0161   //! gp_XY P(10.,3.);
0162   //! gp_XY P1(P);
0163   //! Tcomp.Transforms(P1);               //using Tcomp
0164   //! gp_XY P2(P);
0165   //! T1.Transforms(P2);                  //using T1 then T2
0166   //! T2.Transforms(P2);                  // P1 = P2 !!!
0167   //! @endcode
0168   Standard_NODISCARD gp_GTrsf2d Multiplied (const gp_GTrsf2d& theT) const
0169   {
0170     gp_GTrsf2d aTres = *this;
0171     aTres.Multiply (theT);
0172     return aTres;
0173   }
0174 
0175   Standard_NODISCARD gp_GTrsf2d operator * (const gp_GTrsf2d& theT) const { return Multiplied (theT); }
0176 
0177   Standard_EXPORT void Multiply (const gp_GTrsf2d& theT);
0178 
0179   void operator *= (const gp_GTrsf2d& theT) { Multiply (theT); }
0180 
0181   //! Computes the product of the transformation theT and this
0182   //! transformation, and assigns the result to this transformation:
0183   //! this = theT * this
0184   Standard_EXPORT void PreMultiply (const gp_GTrsf2d& theT);
0185 
0186   Standard_EXPORT void Power (const Standard_Integer theN);
0187 
0188   //! Computes the following composition of transformations
0189   //! <me> * <me> * .......* <me>, theN time.
0190   //! if theN = 0 <me> = Identity
0191   //! if theN < 0 <me> = <me>.Inverse() *...........* <me>.Inverse().
0192   //!
0193   //! Raises an exception if theN < 0 and if the matrix of the
0194   //! transformation is not inversible.
0195   Standard_NODISCARD gp_GTrsf2d Powered (const Standard_Integer theN) const
0196   {
0197     gp_GTrsf2d aT = *this;
0198     aT.Power (theN);
0199     return aT;
0200   }
0201 
0202   void Transforms (gp_XY& theCoord) const;
0203 
0204   Standard_NODISCARD gp_XY Transformed (const gp_XY& theCoord) const
0205   {
0206     gp_XY aNewCoord = theCoord;
0207     Transforms (aNewCoord);
0208     return aNewCoord;
0209   }
0210 
0211   //! Applies this transformation to the coordinates:
0212   //! -   of the number pair Coord, or
0213   //! -   X and Y.
0214   //!
0215   //! Note:
0216   //! -   Transforms modifies theX, theY, or the coordinate pair Coord, while
0217   //! -   Transformed creates a new coordinate pair.
0218   void Transforms (Standard_Real& theX, Standard_Real& theY) const;
0219 
0220   //! Converts this transformation into a gp_Trsf2d transformation.
0221   //! Exceptions
0222   //! Standard_ConstructionError if this transformation
0223   //! cannot be converted, i.e. if its form is gp_Other.
0224   Standard_EXPORT gp_Trsf2d Trsf2d() const;
0225 
0226 private:
0227 
0228   gp_Mat2d matrix;
0229   gp_XY loc;
0230   gp_TrsfForm shape;
0231   Standard_Real scale;
0232 
0233 };
0234 
0235 #include <gp_Trsf2d.hxx>
0236 
0237 //=======================================================================
0238 //function : SetTrsf2d
0239 // purpose :
0240 //=======================================================================
0241 inline void gp_GTrsf2d::SetTrsf2d (const gp_Trsf2d& theT)
0242 {
0243   shape = theT.shape;
0244   matrix = theT.matrix;
0245   loc = theT.loc;
0246   scale = theT.scale;
0247 }
0248 
0249 //=======================================================================
0250 //function : gp_GTrsf2d
0251 // purpose :
0252 //=======================================================================
0253 inline gp_GTrsf2d::gp_GTrsf2d (const gp_Trsf2d& theT)
0254 {
0255   shape = theT.shape;
0256   matrix = theT.matrix;
0257   loc = theT.loc;
0258   scale = theT.scale;
0259 }
0260 
0261 //=======================================================================
0262 //function : SetValue
0263 // purpose :
0264 //=======================================================================
0265 inline void gp_GTrsf2d::SetValue (const Standard_Integer theRow,
0266                                   const Standard_Integer theCol,
0267                                   const Standard_Real theValue)
0268 {
0269   Standard_OutOfRange_Raise_if
0270     (theRow < 1 || theRow > 2 || theCol < 1 || theCol > 3, " ");
0271   if (theCol == 3)
0272   {
0273     loc.SetCoord (theRow, theValue);
0274   }
0275   else
0276   {
0277     matrix.SetValue (theRow, theCol, theValue);
0278   }
0279   shape = gp_Other;
0280 }
0281 
0282 //=======================================================================
0283 //function : Value
0284 // purpose :
0285 //=======================================================================
0286 inline Standard_Real gp_GTrsf2d::Value (const Standard_Integer theRow,
0287                                         const Standard_Integer theCol) const
0288 {
0289   Standard_OutOfRange_Raise_if
0290     (theRow < 1 || theRow > 2 || theCol < 1 || theCol > 3, " ");
0291   if (theCol == 3)
0292   {
0293     return loc.Coord (theRow);
0294   }
0295   if (shape == gp_Other)
0296   {
0297     return matrix.Value (theRow, theCol);
0298   }
0299   return scale * matrix.Value (theRow, theCol);
0300 }
0301 
0302 //=======================================================================
0303 //function : Transforms
0304 // purpose :
0305 //=======================================================================
0306 inline void gp_GTrsf2d::Transforms (gp_XY& theCoord) const
0307 {
0308   theCoord.Multiply (matrix);
0309   if (!(shape == gp_Other) && !(scale == 1.0))
0310   {
0311     theCoord.Multiply (scale);
0312   }
0313   theCoord.Add (loc);
0314 }
0315 
0316 //=======================================================================
0317 //function : Transforms
0318 // purpose :
0319 //=======================================================================
0320 inline void gp_GTrsf2d::Transforms (Standard_Real& theX,
0321                                     Standard_Real& theY) const
0322 {
0323   gp_XY aDoublet(theX, theY);
0324   aDoublet.Multiply (matrix);
0325   if (!(shape == gp_Other) && !(scale == 1.0))
0326   {
0327     aDoublet.Multiply (scale);
0328   }
0329   aDoublet.Add (loc);
0330   aDoublet.Coord (theX, theY);
0331 }
0332 
0333 #endif // _gp_GTrsf2d_HeaderFile