Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:25:04

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_Trsf2d_HeaderFile
0016 #define _gp_Trsf2d_HeaderFile
0017 
0018 #include <gp_TrsfForm.hxx>
0019 #include <gp_Mat2d.hxx>
0020 #include <gp_XY.hxx>
0021 #include <Standard_OutOfRange.hxx>
0022 
0023 class gp_Trsf;
0024 class gp_Pnt2d;
0025 class gp_Ax2d;
0026 class gp_Vec2d;
0027 
0028 //! Defines a non-persistent transformation in 2D space.
0029 //! The following transformations are implemented :
0030 //! - Translation, Rotation, Scale
0031 //! - Symmetry with respect to a point and a line.
0032 //! Complex transformations can be obtained by combining the
0033 //! previous elementary transformations using the method Multiply.
0034 //! The transformations can be represented as follow :
0035 //! @code
0036 //!    V1   V2   T       XY        XY
0037 //! | a11  a12  a13 |   | x |     | x'|
0038 //! | a21  a22  a23 |   | y |     | y'|
0039 //! |  0    0    1  |   | 1 |     | 1 |
0040 //! @endcode
0041 //! where {V1, V2} defines the vectorial part of the transformation
0042 //! and T defines the translation part of the transformation.
0043 //! This transformation never change the nature of the objects.
0044 class gp_Trsf2d
0045 {
0046 public:
0047   DEFINE_STANDARD_ALLOC
0048 
0049   //! Returns identity transformation.
0050   gp_Trsf2d();
0051 
0052   //! Creates a 2d transformation in the XY plane from a
0053   //! 3d transformation .
0054   gp_Trsf2d(const gp_Trsf& theT);
0055 
0056   //! Changes the transformation into a symmetrical transformation.
0057   //! theP is the center of the symmetry.
0058   void SetMirror(const gp_Pnt2d& theP);
0059 
0060   //! Changes the transformation into a symmetrical transformation.
0061   //! theA is the center of the axial symmetry.
0062   Standard_EXPORT void SetMirror(const gp_Ax2d& theA);
0063 
0064   //! Changes the transformation into a rotation.
0065   //! theP is the rotation's center and theAng is the angular value of the
0066   //! rotation in radian.
0067   void SetRotation(const gp_Pnt2d& theP, const Standard_Real theAng);
0068 
0069   //! Changes the transformation into a scale.
0070   //! theP is the center of the scale and theS is the scaling value.
0071   void SetScale(const gp_Pnt2d& theP, const Standard_Real theS);
0072 
0073   //! Changes a transformation allowing passage from the coordinate
0074   //! system "theFromSystem1" to the coordinate system "theToSystem2".
0075   Standard_EXPORT void SetTransformation(const gp_Ax2d& theFromSystem1,
0076                                          const gp_Ax2d& theToSystem2);
0077 
0078   //! Changes the transformation allowing passage from the basic
0079   //! coordinate system
0080   //! {P(0.,0.,0.), VX (1.,0.,0.), VY (0.,1.,0.)}
0081   //! to the local coordinate system defined with the Ax2d theToSystem.
0082   Standard_EXPORT void SetTransformation(const gp_Ax2d& theToSystem);
0083 
0084   //! Changes the transformation into a translation.
0085   //! theV is the vector of the translation.
0086   void SetTranslation(const gp_Vec2d& theV);
0087 
0088   //! Makes the transformation into a translation from
0089   //! the point theP1 to the point theP2.
0090   void SetTranslation(const gp_Pnt2d& theP1, const gp_Pnt2d& theP2);
0091 
0092   //! Replaces the translation vector with theV.
0093   Standard_EXPORT void SetTranslationPart(const gp_Vec2d& theV);
0094 
0095   //! Modifies the scale factor.
0096   Standard_EXPORT void SetScaleFactor(const Standard_Real theS);
0097 
0098   //! Returns true if the determinant of the vectorial part of
0099   //! this transformation is negative..
0100   Standard_Boolean IsNegative() const { return (matrix.Determinant() < 0.0); }
0101 
0102   //! Returns the nature of the transformation. It can be  an
0103   //! identity transformation, a rotation, a translation, a mirror
0104   //! (relative to a point or an axis), a scaling transformation,
0105   //! or a compound transformation.
0106   gp_TrsfForm Form() const { return shape; }
0107 
0108   //! Returns the scale factor.
0109   Standard_Real ScaleFactor() const { return scale; }
0110 
0111   //! Returns the translation part of the transformation's matrix
0112   const gp_XY& TranslationPart() const { return loc; }
0113 
0114   //! Returns the vectorial part of the transformation. It is a
0115   //! 2*2 matrix which includes the scale factor.
0116   Standard_EXPORT gp_Mat2d VectorialPart() const;
0117 
0118   //! Returns the homogeneous vectorial part of the transformation.
0119   //! It is a 2*2 matrix which doesn't include the scale factor.
0120   //! The coefficients of this matrix must be multiplied by the
0121   //! scale factor to obtain the coefficients of the transformation.
0122   const gp_Mat2d& HVectorialPart() const { return matrix; }
0123 
0124   //! Returns the angle corresponding to the rotational component
0125   //! of the transformation matrix (operation opposite to SetRotation()).
0126   Standard_EXPORT Standard_Real RotationPart() const;
0127 
0128   //! Returns the coefficients of the transformation's matrix.
0129   //! It is a 2 rows * 3 columns matrix.
0130   //! Raises OutOfRange if theRow < 1 or theRow > 2 or theCol < 1 or theCol > 3
0131   Standard_Real Value(const Standard_Integer theRow, const Standard_Integer theCol) const;
0132 
0133   Standard_EXPORT void Invert();
0134 
0135   //! Computes the reverse transformation.
0136   //! Raises an exception if the matrix of the transformation
0137   //! is not inversible, it means that the scale factor is lower
0138   //! or equal to Resolution from package gp.
0139   Standard_NODISCARD gp_Trsf2d Inverted() const
0140   {
0141     gp_Trsf2d aT = *this;
0142     aT.Invert();
0143     return aT;
0144   }
0145 
0146   Standard_NODISCARD gp_Trsf2d Multiplied(const gp_Trsf2d& theT) const
0147   {
0148     gp_Trsf2d aTresult(*this);
0149     aTresult.Multiply(theT);
0150     return aTresult;
0151   }
0152 
0153   Standard_NODISCARD gp_Trsf2d operator*(const gp_Trsf2d& theT) const { return Multiplied(theT); }
0154 
0155   //! Computes the transformation composed from <me> and theT.
0156   //! <me> = <me> * theT
0157   Standard_EXPORT void Multiply(const gp_Trsf2d& theT);
0158 
0159   void operator*=(const gp_Trsf2d& theT) { Multiply(theT); }
0160 
0161   //! Computes the transformation composed from <me> and theT.
0162   //! <me> = theT * <me>
0163   Standard_EXPORT void PreMultiply(const gp_Trsf2d& theT);
0164 
0165   Standard_EXPORT void Power(const Standard_Integer theN);
0166 
0167   //! Computes the following composition of transformations
0168   //! <me> * <me> * .......* <me>,  theN time.
0169   //! if theN = 0 <me> = Identity
0170   //! if theN < 0 <me> = <me>.Inverse() *...........* <me>.Inverse().
0171   //!
0172   //! Raises if theN < 0 and if the matrix of the transformation not
0173   //! inversible.
0174   gp_Trsf2d Powered(const Standard_Integer theN)
0175   {
0176     gp_Trsf2d aT = *this;
0177     aT.Power(theN);
0178     return aT;
0179   }
0180 
0181   void Transforms(Standard_Real& theX, Standard_Real& theY) const;
0182 
0183   //! Transforms  a doublet XY with a Trsf2d
0184   void Transforms(gp_XY& theCoord) const;
0185 
0186   //! Sets the coefficients  of the transformation. The
0187   //! transformation  of the  point  x,y is  the point
0188   //! x',y' with :
0189   //! @code
0190   //! x' = a11 x + a12 y + a13
0191   //! y' = a21 x + a22 y + a23
0192   //! @endcode
0193   //! The method Value(i,j) will return aij.
0194   //! Raises ConstructionError if the determinant of the aij is null.
0195   //! If the matrix as not a uniform scale it will be orthogonalized before future using.
0196   Standard_EXPORT void SetValues(const Standard_Real a11,
0197                                  const Standard_Real a12,
0198                                  const Standard_Real a13,
0199                                  const Standard_Real a21,
0200                                  const Standard_Real a22,
0201                                  const Standard_Real a23);
0202 
0203   friend class gp_GTrsf2d;
0204 
0205 protected:
0206   //! Makes orthogonalization of "matrix"
0207   Standard_EXPORT void Orthogonalize();
0208 
0209 private:
0210   Standard_Real scale;
0211   gp_TrsfForm   shape;
0212   gp_Mat2d      matrix;
0213   gp_XY         loc;
0214 };
0215 
0216 #include <gp_Trsf.hxx>
0217 #include <gp_Pnt2d.hxx>
0218 
0219 //=======================================================================
0220 // function : gp_Trsf2d
0221 // purpose :
0222 //=======================================================================
0223 inline gp_Trsf2d::gp_Trsf2d()
0224 {
0225   shape = gp_Identity;
0226   scale = 1.0;
0227   matrix.SetIdentity();
0228   loc.SetCoord(0.0, 0.0);
0229 }
0230 
0231 //=======================================================================
0232 // function : gp_Trsf2d
0233 // purpose :
0234 //=======================================================================
0235 inline gp_Trsf2d::gp_Trsf2d(const gp_Trsf& theT)
0236     : scale(theT.ScaleFactor()),
0237       shape(theT.Form()),
0238       loc(theT.TranslationPart().X(), theT.TranslationPart().Y())
0239 {
0240   const gp_Mat& M = theT.HVectorialPart();
0241   matrix(1, 1)    = M(1, 1);
0242   matrix(1, 2)    = M(1, 2);
0243   matrix(2, 1)    = M(2, 1);
0244   matrix(2, 2)    = M(2, 2);
0245 }
0246 
0247 //=======================================================================
0248 // function : SetRotation
0249 // purpose :
0250 //=======================================================================
0251 inline void gp_Trsf2d::SetRotation(const gp_Pnt2d& theP, const Standard_Real theAng)
0252 {
0253   shape = gp_Rotation;
0254   scale = 1.0;
0255   loc   = theP.XY();
0256   loc.Reverse();
0257   matrix.SetRotation(theAng);
0258   loc.Multiply(matrix);
0259   loc.Add(theP.XY());
0260 }
0261 
0262 //=======================================================================
0263 // function : SetMirror
0264 // purpose :
0265 //=======================================================================
0266 inline void gp_Trsf2d::SetMirror(const gp_Pnt2d& theP)
0267 {
0268   shape = gp_PntMirror;
0269   scale = -1.0;
0270   matrix.SetIdentity();
0271   loc = theP.XY();
0272   loc.Multiply(2.0);
0273 }
0274 
0275 //=======================================================================
0276 // function : SetScale
0277 // purpose :
0278 //=======================================================================
0279 inline void gp_Trsf2d::SetScale(const gp_Pnt2d& theP, const Standard_Real theS)
0280 {
0281   shape = gp_Scale;
0282   scale = theS;
0283   matrix.SetIdentity();
0284   loc = theP.XY();
0285   loc.Multiply(1.0 - theS);
0286 }
0287 
0288 //=======================================================================
0289 // function : SetTranslation
0290 // purpose :
0291 //=======================================================================
0292 inline void gp_Trsf2d::SetTranslation(const gp_Vec2d& theV)
0293 {
0294   shape = gp_Translation;
0295   scale = 1.0;
0296   matrix.SetIdentity();
0297   loc = theV.XY();
0298 }
0299 
0300 //=======================================================================
0301 // function : SetTranslation
0302 // purpose :
0303 //=======================================================================
0304 inline void gp_Trsf2d::SetTranslation(const gp_Pnt2d& theP1, const gp_Pnt2d& theP2)
0305 {
0306   shape = gp_Translation;
0307   scale = 1.0;
0308   matrix.SetIdentity();
0309   loc = (theP2.XY()).Subtracted(theP1.XY());
0310 }
0311 
0312 //=======================================================================
0313 // function : Value
0314 // purpose :
0315 //=======================================================================
0316 inline Standard_Real gp_Trsf2d::Value(const Standard_Integer theRow,
0317                                       const Standard_Integer theCol) const
0318 {
0319   Standard_OutOfRange_Raise_if(theRow < 1 || theRow > 2 || theCol < 1 || theCol > 3, " ");
0320   if (theCol < 3)
0321   {
0322     return scale * matrix.Value(theRow, theCol);
0323   }
0324   else
0325   {
0326     return loc.Coord(theRow);
0327   }
0328 }
0329 
0330 //=======================================================================
0331 // function : Transforms
0332 // purpose :
0333 //=======================================================================
0334 inline void gp_Trsf2d::Transforms(Standard_Real& theX, Standard_Real& theY) const
0335 {
0336   gp_XY aDoublet(theX, theY);
0337   aDoublet.Multiply(matrix);
0338   if (scale != 1.0)
0339   {
0340     aDoublet.Multiply(scale);
0341   }
0342   aDoublet.Add(loc);
0343   aDoublet.Coord(theX, theY);
0344 }
0345 
0346 //=======================================================================
0347 // function : Transforms
0348 // purpose :
0349 //=======================================================================
0350 inline void gp_Trsf2d::Transforms(gp_XY& theCoord) const
0351 {
0352   theCoord.Multiply(matrix);
0353   if (scale != 1.0)
0354   {
0355     theCoord.Multiply(scale);
0356   }
0357   theCoord.Add(loc);
0358 }
0359 
0360 #endif // _gp_Trsf2d_HeaderFile