Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-29 09:20:09

0001 // Author: Kirill Gavrilov
0002 // Copyright (c) 2019 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 _RWObj_TriangulationReader_HeaderFile
0016 #define _RWObj_TriangulationReader_HeaderFile
0017 
0018 #include <RWObj_Reader.hxx>
0019 
0020 #include <Poly_Triangulation.hxx>
0021 #include <TopoDS_Compound.hxx>
0022 
0023 //! Interface to store shape attributes into document.
0024 class RWObj_IShapeReceiver
0025 {
0026 public:
0027   //! @param theShape       shape to register
0028   //! @param theName        shape name
0029   //! @param theMaterial    shape material
0030   //! @param theIsRootShape indicates that this is a root object (free shape)
0031   virtual void BindNamedShape(const TopoDS_Shape&            theShape,
0032                               const TCollection_AsciiString& theName,
0033                               const RWObj_Material*          theMaterial,
0034                               const bool                     theIsRootShape) = 0;
0035 };
0036 
0037 //! RWObj_Reader implementation dumping OBJ file into Poly_Triangulation.
0038 class RWObj_TriangulationReader : public RWObj_Reader
0039 {
0040   DEFINE_STANDARD_RTTIEXT(RWObj_TriangulationReader, RWObj_Reader)
0041 public:
0042   //! Constructor.
0043   RWObj_TriangulationReader()
0044       : myShapeReceiver(nullptr),
0045         myToCreateShapes(true)
0046   {
0047   }
0048 
0049   //! Set flag to create shapes.
0050   void SetCreateShapes(bool theToCreateShapes) { myToCreateShapes = theToCreateShapes; }
0051 
0052   //! Set shape receiver callback.
0053   void SetShapeReceiver(RWObj_IShapeReceiver* theReceiver) { myShapeReceiver = theReceiver; }
0054 
0055   //! Create Poly_Triangulation from collected data
0056   Standard_EXPORT virtual occ::handle<Poly_Triangulation> GetTriangulation();
0057 
0058   //! Return result shape.
0059   Standard_EXPORT TopoDS_Shape ResultShape();
0060 
0061 protected:
0062   //! Flush active sub-mesh.
0063   Standard_EXPORT bool addMesh(const RWObj_SubMesh&      theMesh,
0064                                const RWObj_SubMeshReason theReason) override;
0065 
0066   //! Retrieve sub-mesh node position.
0067   gp_Pnt getNode(int theIndex) const override { return myNodes.Value(theIndex - 1); }
0068 
0069   //! Add new node.
0070   int addNode(const gp_Pnt& thePnt) override
0071   {
0072     myNodes.Append(thePnt);
0073     return myNodes.Length();
0074   }
0075 
0076   //! Ignore normal.
0077   void setNodeNormal(const int theIndex, const NCollection_Vec3<float>& theNormal) override
0078   {
0079     myNormals.SetValue(theIndex - 1, theNormal);
0080   }
0081 
0082   //! Ignore texture coordinates.
0083   void setNodeUV(const int theIndex, const NCollection_Vec2<float>& theUV) override
0084   {
0085     myNodesUV.SetValue(theIndex - 1, theUV);
0086   }
0087 
0088   //! Add element.
0089   void addElement(int theN1, int theN2, int theN3, int theN4) override
0090   {
0091     myTriangles.Append(Poly_Triangle(theN1, theN2, theN3));
0092     if (theN4 != -1)
0093     {
0094       myTriangles.Append(Poly_Triangle(theN1, theN3, theN4));
0095     }
0096   }
0097 
0098 protected:
0099   //! Add sub-shape into specified shape
0100   Standard_EXPORT bool addSubShape(TopoDS_Shape&       theParent,
0101                                    const TopoDS_Shape& theSubShape,
0102                                    const bool          theToExpandCompound);
0103 
0104 protected:
0105   NCollection_DynamicArray<gp_Pnt> myNodes; //!< nodes   of currently filled triangulation
0106   NCollection_DynamicArray<NCollection_Vec3<float>>
0107     myNormals; //!< normals of currently filled triangulation
0108   NCollection_DynamicArray<NCollection_Vec2<float>>
0109     myNodesUV; //!< UVs     of currently filled triangulation
0110   NCollection_DynamicArray<Poly_Triangle>
0111     myTriangles; //!< indexes of currently filled triangulation
0112 
0113   RWObj_IShapeReceiver* myShapeReceiver;      //!< optional shape receiver
0114   TopoDS_Compound       myResultShape;        //!< result shape as Compound of objects
0115   TopoDS_Compound       myLastObjectShape;    //!< Compound containing current object groups
0116                                               // clang-format off
0117   TopoDS_Shape            myLastGroupShape;  //!< current group shape - either a single Face or Compound of Faces
0118                                               // clang-format on
0119   TCollection_AsciiString myLastGroupName;    //!< current group name
0120   TCollection_AsciiString myLastFaceMaterial; //!< last face material name
0121   bool                    myToCreateShapes;   //!< create a single triangulation
0122 };
0123 
0124 #endif // _RWObj_TriangulationReader_HeaderFile