Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // Copyright (c) 2021 OPEN CASCADE SAS
0002 //
0003 // This file is part of Open CASCADE Technology software library.
0004 //
0005 // This library is free software; you can redistribute it and/or modify it under
0006 // the terms of the GNU Lesser General Public License version 2.1 as published
0007 // by the Free Software Foundation, with special exception defined in the file
0008 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0009 // distribution for complete text of the license and disclaimer of any warranty.
0010 //
0011 // Alternatively, this file may be used under the terms of Open CASCADE
0012 // commercial license or contractual agreement.
0013 
0014 #ifndef _RWMesh_TriangulationReader_HeaderFile
0015 #define _RWMesh_TriangulationReader_HeaderFile
0016 
0017 #include <Poly_Triangulation.hxx>
0018 #include <RWMesh_CoordinateSystemConverter.hxx>
0019 #include <Standard_Mutex.hxx>
0020 
0021 class OSD_FileSystem;
0022 class RWMesh_TriangulationSource;
0023 
0024 //! Interface for reading primitive array from the buffer.
0025 class RWMesh_TriangulationReader : public Standard_Transient
0026 {
0027   DEFINE_STANDARD_RTTIEXT(RWMesh_TriangulationReader, Standard_Transient)
0028 public:
0029 
0030   struct LoadingStatistic
0031   {
0032     LoadingStatistic()
0033     : ExpectedNodesNb(0),
0034       LoadedNodesNb(0),
0035       ExpectedTrianglesNb(0),
0036       DegeneratedTrianglesNb(0),
0037       LoadedTrianglesNb(0) {}
0038 
0039     void Reset()
0040     {
0041       ExpectedNodesNb = 0;
0042       LoadedNodesNb = 0;
0043       ExpectedTrianglesNb = 0;
0044       DegeneratedTrianglesNb = 0;
0045       LoadedTrianglesNb = 0;
0046     }
0047 
0048     Standard_EXPORT void PrintStatistic (const TCollection_AsciiString& thePrefix = "") const;
0049 
0050     Standard_Integer ExpectedNodesNb;
0051     Standard_Integer LoadedNodesNb;
0052     Standard_Integer ExpectedTrianglesNb;
0053     Standard_Integer DegeneratedTrianglesNb;
0054     Standard_Integer LoadedTrianglesNb;
0055   };
0056 
0057   //! Constructor.
0058   Standard_EXPORT RWMesh_TriangulationReader();
0059 
0060   //! Destructor.
0061   Standard_EXPORT virtual ~RWMesh_TriangulationReader();
0062 
0063   //! Returns file name for reporting issues.
0064   const TCollection_AsciiString& FileName() const { return myFileName; }
0065 
0066   //! Sets file name for reporting issues.
0067   void SetFileName(const TCollection_AsciiString& theFileName) { myFileName = theFileName; }
0068 
0069   //! Returns coordinate system converter using for correct data loading.
0070   const RWMesh_CoordinateSystemConverter& CoordinateSystemConverter() const { return myCoordSysConverter; }
0071 
0072   //! Sets coordinate system converter.
0073   void SetCoordinateSystemConverter (const RWMesh_CoordinateSystemConverter& theConverter) { myCoordSysConverter = theConverter; }
0074 
0075   //! Returns flag to fill in triangulation using double or single precision; FALSE by default.
0076   bool IsDoublePrecision() const { return myIsDoublePrecision; }
0077 
0078   //! Sets flag to fill in triangulation using double or single precision.
0079   void SetDoublePrecision (bool theIsDouble) { myIsDoublePrecision = theIsDouble; }
0080 
0081   //! Returns TRUE if degenerated triangles should be skipped during mesh loading (only indexes will be checked).
0082   Standard_Boolean ToSkipDegenerates() const { return myToSkipDegenerateTris; }
0083 
0084   //! Sets flag to skip degenerated triangles during mesh loading (only indexes will be checked).
0085   void SetToSkipDegenerates (const Standard_Boolean theToSkip) { myToSkipDegenerateTris = theToSkip; }
0086 
0087   //! Returns TRUE if additional debug information should be print.
0088   Standard_Boolean ToPrintDebugMessages() const { return myToPrintDebugMessages; }
0089 
0090   //! Sets flag to print debug information.
0091   void SetToPrintDebugMessages (const Standard_Boolean theToPrint) { myToPrintDebugMessages = theToPrint; }
0092 
0093   //! Starts and reset internal object that accumulates nodes/triangles statistic during data reading.
0094   void StartStatistic()
0095   {
0096     if (myLoadingStatistic)
0097     {
0098       myLoadingStatistic->Reset();
0099     }
0100     else
0101     {
0102       myLoadingStatistic = new LoadingStatistic();
0103     }
0104   }
0105 
0106   //! Stops and nullify internal object that accumulates nodes/triangles statistic during data reading.
0107   void StopStatistic()
0108   {
0109     if (myLoadingStatistic)
0110     {
0111       delete myLoadingStatistic;
0112       myLoadingStatistic = NULL;
0113     }
0114   }
0115 
0116   //! Prints loading statistic.
0117   //! This method should be used between StartStatistic() and StopStatistic() calls
0118   //! for correct results.
0119   void PrintStatistic() const
0120   {
0121     if (myLoadingStatistic)
0122     {
0123       myLoadingStatistic->PrintStatistic (TCollection_AsciiString("[Mesh reader. File '") + myFileName + "']. ");
0124     }
0125   }
0126 
0127   //! Loads primitive array.
0128   Standard_EXPORT bool Load (const Handle(RWMesh_TriangulationSource)& theSourceMesh,
0129                              const Handle(Poly_Triangulation)& theDestMesh,
0130                              const Handle(OSD_FileSystem)& theFileSystem) const;
0131 
0132 protected:
0133 
0134   //! Loads primitive array.
0135   Standard_EXPORT virtual bool load (const Handle(RWMesh_TriangulationSource)& theSourceMesh,
0136                                      const Handle(Poly_Triangulation)& theDestMesh,
0137                                      const Handle(OSD_FileSystem)& theFileSystem) const = 0;
0138 
0139   //! Performs additional actions to finalize data loading.
0140   Standard_EXPORT virtual bool finalizeLoading (const Handle(RWMesh_TriangulationSource)& theSourceMesh,
0141                                                 const Handle(Poly_Triangulation)& theDestMesh) const;
0142 
0143 protected: //! @name interface for filling triangulation data
0144 
0145   //! Resizes array of position nodes to specified size.
0146   //! @param theMesh [in] triangulation to be modified
0147   //! @param theNbNodes [in] nodes number
0148   //! @param theToCopyData [in] copy old nodes into new array
0149   //! @return TRUE in case of success operation
0150   virtual bool setNbPositionNodes (const Handle(Poly_Triangulation)& theMesh,
0151                                    Standard_Integer theNbNodes,
0152                                    Standard_Boolean theToCopyData = false) const
0153   {
0154     if (theNbNodes <= 0)
0155     {
0156       return false;
0157     }
0158     theMesh->ResizeNodes (theNbNodes, theToCopyData);
0159     return true;
0160   }
0161 
0162   //! Sets node position.
0163   //! @param theMesh [in] triangulation to be modified
0164   //! @param theIndex [in] node index starting from 1
0165   //! @param thePnt [in] node position
0166   virtual void setNodePosition (const Handle(Poly_Triangulation)& theMesh,
0167                                 Standard_Integer theIndex,
0168                                 const gp_Pnt& thePnt) const
0169   {
0170     theMesh->SetNode (theIndex, thePnt);
0171   }
0172 
0173   //! Resizes array of UV nodes to specified size.
0174   //! @param theMesh [in] triangulation to be modified
0175   //! @param theNbNodes [in] nodes number
0176   //! @return TRUE in case of success operation
0177   virtual bool setNbUVNodes (const Handle(Poly_Triangulation)& theMesh,
0178                              Standard_Integer theNbNodes) const
0179   {
0180     if (theNbNodes <= 0
0181      || theMesh->NbNodes() != theNbNodes)
0182     {
0183       return false;
0184     }
0185     theMesh->AddUVNodes();
0186     return true;
0187   }
0188 
0189   //! Sets node UV texture coordinates.
0190   //! @param theMesh [in] triangulation to be modified
0191   //! @param theIndex [in] node index starting from 1
0192   //! @param theUV [in] node UV coordinates
0193   virtual void setNodeUV (const Handle(Poly_Triangulation)& theMesh,
0194                           Standard_Integer theIndex,
0195                           const gp_Pnt2d& theUV) const
0196   {
0197     theMesh->SetUVNode (theIndex, theUV);
0198   }
0199 
0200   //! Resizes array of nodes normals to specified size.
0201   //! @param theMesh [in] triangulation to be modified
0202   //! @param theNbNodes [in] nodes number
0203   //! @return TRUE in case of success operation
0204   virtual bool setNbNormalNodes (const Handle(Poly_Triangulation)& theMesh,
0205                                  Standard_Integer theNbNodes) const
0206   {
0207     if (theNbNodes <= 0
0208      || theMesh->NbNodes() != theNbNodes)
0209     {
0210       return false;
0211     }
0212     theMesh->AddNormals();
0213     return true;
0214   }
0215 
0216   //! Sets node normal.
0217   //! @param theMesh [in] triangulation to be modified
0218   //! @param theIndex  node index starting from 1
0219   //! @param theNormal node normal vector
0220   virtual void setNodeNormal (const Handle(Poly_Triangulation)& theMesh,
0221                               Standard_Integer theIndex,
0222                               const gp_Vec3f& theNormal) const
0223   {
0224     theMesh->SetNormal (theIndex, theNormal);
0225   }
0226 
0227   //! Resizes array of triangles to specified size.
0228   //! @param theMesh [in] triangulation to be modified
0229   //! @param theNbTris [in] elements number
0230   //! @param theToCopyData [in] copy old triangles into new array
0231   //! @return TRUE in case of success operation
0232   virtual bool setNbTriangles (const Handle(Poly_Triangulation)& theMesh,
0233                                Standard_Integer theNbTris,
0234                                Standard_Boolean theToCopyData = false) const
0235   {
0236     if (theNbTris >= 1)
0237     {
0238       theMesh->ResizeTriangles (theNbTris, theToCopyData);
0239       return true;
0240     }
0241     return false;
0242   }
0243 
0244   //! Adds triangle element.
0245   //! @param theMesh [in] triangulation to be modified
0246   //! @param theIndex    triangle index starting from 1
0247   //! @param theTriangle triangle nodes starting from 1
0248   //! @return 0 if node indexes are out of range,
0249   //!        -1 if triangle is degenerated and should be skipped,
0250   //!         1 in case of success operation.
0251   virtual Standard_Integer setTriangle (const Handle(Poly_Triangulation)& theMesh,
0252                                         Standard_Integer theIndex,
0253                                         const Poly_Triangle& theTriangle) const
0254   {
0255     if (theTriangle.Value (1) < 1 || theTriangle.Value (1) > theMesh->NbNodes()
0256      || theTriangle.Value (2) < 1 || theTriangle.Value (2) > theMesh->NbNodes()
0257      || theTriangle.Value (3) < 1 || theTriangle.Value (3) > theMesh->NbNodes())
0258     {
0259       return 0;
0260     }
0261     if (myToSkipDegenerateTris
0262         && (theTriangle.Value (1) == theTriangle.Value (2)
0263          || theTriangle.Value (1) == theTriangle.Value (3)
0264          || theTriangle.Value (2) == theTriangle.Value (3)))
0265     {
0266       return -1;
0267     }
0268     theMesh->SetTriangle (theIndex, theTriangle);
0269     return 1;
0270   }
0271 
0272 protected:
0273 
0274   RWMesh_CoordinateSystemConverter myCoordSysConverter;    //!< coordinate system converter
0275   TCollection_AsciiString          myFileName;             //!< file name to use during message printing
0276   mutable Standard_Mutex           myMutex;                //!< internal mutex to collect nodes/triangles statistic
0277   mutable LoadingStatistic*        myLoadingStatistic;     //!< statistic of loaded triangulation
0278   Standard_Boolean                 myIsDoublePrecision;    //!< flag to fill in triangulation using single or double precision
0279   Standard_Boolean                 myToSkipDegenerateTris; //!< flag to skip degenerate triangles during loading, FALSE by default
0280   Standard_Boolean                 myToPrintDebugMessages; //!< flag to print additional debug information
0281 };
0282 
0283 #endif // _RWMesh_TriangulationReader_HeaderFile