Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-26 10:23:01

0001 // Author: Kirill Gavrilov
0002 // Copyright: Open CASCADE 2015-2019
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 _RWMesh_CoordinateSystemConverter_HeaderFile
0016 #define _RWMesh_CoordinateSystemConverter_HeaderFile
0017 
0018 #include <RWMesh_CoordinateSystem.hxx>
0019 
0020 #include <gp_Ax3.hxx>
0021 #include <gp_XYZ.hxx>
0022 #include <gp_Trsf.hxx>
0023 #include <NCollection_Vec2.hxx>
0024 #include <Standard_TypeDef.hxx>
0025 #include <NCollection_Vec3.hxx>
0026 #include <NCollection_Vec4.hxx>
0027 #include <NCollection_Mat4.hxx>
0028 
0029 //! Coordinate system converter defining the following tools:
0030 //! - Initialization for commonly used coordinate systems Z-up and Y-up.
0031 //! - Perform length unit conversion (scaling).
0032 //! - Conversion of three basic elements:
0033 //!   a) mesh node Positions,
0034 //!   b) mesh node Normals,
0035 //!   c) model nodes Transformations (locations).
0036 //!
0037 //! RWMesh_CoordinateSystem enumeration is used for convenient conversion between two commonly
0038 //! used coordinate systems, to make sure that imported model is oriented up.
0039 //! But gp_Ax3 can be used instead for defining a conversion between arbitrary systems (e.g.
0040 //! including non-zero origin).
0041 //!
0042 //! The converter requires defining explicitly both input and output systems,
0043 //! so that if either input or output is undefined, then conversion will be skipped.
0044 //! Length units conversion and coordinate system conversion are decomposed,
0045 //! so that application might specify no length units conversion but Y-up to Z-up coordinate system
0046 //! conversion.
0047 //!
0048 //! Class defines dedicated methods for parameters of input and output systems.
0049 //! This allows passing tool through several initialization steps,
0050 //! so that a reader can initialize input length units (only if file format defines such
0051 //! information), while application specifies output length units, and conversion will be done only
0052 //! when both defined.
0053 class RWMesh_CoordinateSystemConverter
0054 {
0055 public:
0056   //! Return a standard coordinate system definition.
0057   static gp_Ax3 StandardCoordinateSystem(RWMesh_CoordinateSystem theSys)
0058   {
0059     switch (theSys)
0060     {
0061       case RWMesh_CoordinateSystem_posYfwd_posZup:
0062         return gp_Ax3(gp::Origin(), gp::DZ(), gp::DX());
0063       case RWMesh_CoordinateSystem_negZfwd_posYup:
0064         return gp_Ax3(gp::Origin(), gp::DY(), gp::DX());
0065       case RWMesh_CoordinateSystem_Undefined:
0066         break;
0067     }
0068     return gp_Ax3();
0069   }
0070 
0071 public:
0072   //! Empty constructor.
0073   Standard_EXPORT RWMesh_CoordinateSystemConverter();
0074 
0075   //! Return TRUE if there is no transformation (target and current coordinates systems are same).
0076   bool IsEmpty() const { return myIsEmpty; }
0077 
0078   //! Return source length units, defined as scale factor to m (meters).
0079   //! -1.0 by default, which means that NO conversion will be applied (regardless output length
0080   //! unit).
0081   double InputLengthUnit() const { return myInputLengthUnit; }
0082 
0083   //! Set source length units as scale factor to m (meters).
0084   void SetInputLengthUnit(double theInputScale)
0085   {
0086     Init(myInputAx3, theInputScale, myOutputAx3, myOutputLengthUnit);
0087   }
0088 
0089   //! Return destination length units, defined as scale factor to m (meters).
0090   //! -1.0 by default, which means that NO conversion will be applied (regardless input length
0091   //! unit).
0092   double OutputLengthUnit() const { return myOutputLengthUnit; }
0093 
0094   //! Set destination length units as scale factor to m (meters).
0095   void SetOutputLengthUnit(double theOutputScale)
0096   {
0097     Init(myInputAx3, myInputLengthUnit, myOutputAx3, theOutputScale);
0098   }
0099 
0100   //! Return TRUE if source coordinate system has been set; FALSE by default.
0101   bool HasInputCoordinateSystem() const { return myHasInputAx3; }
0102 
0103   //! Source coordinate system; UNDEFINED by default.
0104   const gp_Ax3& InputCoordinateSystem() const { return myInputAx3; }
0105 
0106   //! Set source coordinate system.
0107   void SetInputCoordinateSystem(const gp_Ax3& theSysFrom)
0108   {
0109     myHasInputAx3 = true;
0110     Init(theSysFrom, myInputLengthUnit, myOutputAx3, myOutputLengthUnit);
0111   }
0112 
0113   //! Set source coordinate system.
0114   void SetInputCoordinateSystem(RWMesh_CoordinateSystem theSysFrom)
0115   {
0116     myHasInputAx3 = theSysFrom != RWMesh_CoordinateSystem_Undefined;
0117     Init(StandardCoordinateSystem(theSysFrom), myInputLengthUnit, myOutputAx3, myOutputLengthUnit);
0118   }
0119 
0120   //! Return TRUE if destination coordinate system has been set; FALSE by default.
0121   bool HasOutputCoordinateSystem() const { return myHasOutputAx3; }
0122 
0123   //! Destination coordinate system; UNDEFINED by default.
0124   const gp_Ax3& OutputCoordinateSystem() const { return myOutputAx3; }
0125 
0126   //! Set destination coordinate system.
0127   void SetOutputCoordinateSystem(const gp_Ax3& theSysTo)
0128   {
0129     myHasOutputAx3 = true;
0130     Init(myInputAx3, myInputLengthUnit, theSysTo, myOutputLengthUnit);
0131   }
0132 
0133   //! Set destination coordinate system.
0134   void SetOutputCoordinateSystem(RWMesh_CoordinateSystem theSysTo)
0135   {
0136     myHasOutputAx3 = theSysTo != RWMesh_CoordinateSystem_Undefined;
0137     Init(myInputAx3, myInputLengthUnit, StandardCoordinateSystem(theSysTo), myOutputLengthUnit);
0138   }
0139 
0140   //! Initialize transformation.
0141   Standard_EXPORT void Init(const gp_Ax3& theInputSystem,
0142                             double        theInputLengthUnit,
0143                             const gp_Ax3& theOutputSystem,
0144                             double        theOutputLengthUnit);
0145 
0146 public:
0147   //! Transform transformation.
0148   void TransformTransformation(gp_Trsf& theTrsf) const
0149   {
0150     if (myHasScale)
0151     {
0152       gp_XYZ aTransPart = theTrsf.TranslationPart();
0153       aTransPart *= myUnitFactor;
0154       theTrsf.SetTranslationPart(aTransPart);
0155     }
0156     if (myTrsf.Form() != gp_Identity)
0157     {
0158       theTrsf = myTrsf * theTrsf * myTrsfInv;
0159     }
0160   }
0161 
0162   //! Transform position.
0163   void TransformPosition(gp_XYZ& thePos) const
0164   {
0165     if (myHasScale)
0166     {
0167       thePos *= myUnitFactor;
0168     }
0169     if (myTrsf.Form() != gp_Identity)
0170     {
0171       myTrsf.Transforms(thePos);
0172     }
0173   }
0174 
0175   //! Transform normal (e.g. exclude translation/scale part of transformation).
0176   void TransformNormal(NCollection_Vec3<float>& theNorm) const
0177   {
0178     if (myTrsf.Form() != gp_Identity)
0179     {
0180       const NCollection_Vec4<float> aNorm = myNormTrsf * NCollection_Vec4<float>(theNorm, 0.0f);
0181       theNorm                             = aNorm.xyz();
0182     }
0183   }
0184 
0185 private:
0186   gp_Ax3 myInputAx3;  //!< source      coordinate system
0187   gp_Ax3 myOutputAx3; //!< destination coordinate system
0188                       // clang-format off
0189   double    myInputLengthUnit;  //!< source      length units, defined as scale factor to m (meters); -1.0 by default which means UNDEFINED
0190   double    myOutputLengthUnit; //!< destination length units, defined as scale factor to m (meters); -1.0 by default which means UNDEFINED
0191   bool myHasInputAx3;      //!< flag indicating if source coordinate system is defined or not
0192   bool myHasOutputAx3;     //!< flag indicating if destination coordinate system is defined or not
0193 
0194   gp_Trsf          myTrsf;             //!< transformation from input Ax3 to output Ax3
0195   gp_Trsf          myTrsfInv;          //!< inversed transformation from input Ax3 to output Ax3
0196   NCollection_Mat4<float>   myNormTrsf;         //!< transformation 4x4 matrix from input Ax3 to output Ax3
0197   double    myUnitFactor;       //!< unit scale factor
0198   bool myHasScale;         //!< flag indicating that length unit transformation should be performed
0199                       // clang-format on
0200   bool myIsEmpty;     //!< flag indicating that transformation is empty
0201 };
0202 
0203 #endif // _RWMesh_CoordinateSystemConverter_HeaderFile