Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:04:43

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