Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-16 09:01:00

0001 // Created on: 1995-03-06
0002 // Created by: Laurent PAINNOT
0003 // Copyright (c) 1995-1999 Matra Datavision
0004 // Copyright (c) 1999-2014 OPEN CASCADE SAS
0005 //
0006 // This file is part of Open CASCADE Technology software library.
0007 //
0008 // This library is free software; you can redistribute it and/or modify it under
0009 // the terms of the GNU Lesser General Public License version 2.1 as published
0010 // by the Free Software Foundation, with special exception defined in the file
0011 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0012 // distribution for complete text of the license and disclaimer of any warranty.
0013 //
0014 // Alternatively, this file may be used under the terms of Open CASCADE
0015 // commercial license or contractual agreement.
0016 
0017 #ifndef _Poly_Triangulation_HeaderFile
0018 #define _Poly_Triangulation_HeaderFile
0019 
0020 #include <Bnd_Box.hxx>
0021 #include <Poly_HArray1OfTriangle.hxx>
0022 #include <Poly_ArrayOfNodes.hxx>
0023 #include <Poly_ArrayOfUVNodes.hxx>
0024 #include <Poly_MeshPurpose.hxx>
0025 #include <TColgp_HArray1OfPnt.hxx>
0026 #include <TColgp_HArray1OfPnt2d.hxx>
0027 #include <TShort_HArray1OfShortReal.hxx>
0028 
0029 class OSD_FileSystem;
0030 class Poly_Triangulation;
0031 class Poly_TriangulationParameters;
0032 
0033 DEFINE_STANDARD_HANDLE(Poly_Triangulation, Standard_Transient)
0034 
0035 //! Provides a triangulation for a surface, a set of surfaces, or more generally a shape.
0036 //!
0037 //! A triangulation consists of an approximate representation of the actual shape,
0038 //! using a collection of points and triangles.
0039 //! The points are located on the surface.
0040 //! The edges of the triangles connect adjacent points with a straight line that approximates the true curve on the surface.
0041 //!
0042 //! A triangulation comprises:
0043 //! - A table of 3D nodes (3D points on the surface).
0044 //! - A table of triangles.
0045 //!   Each triangle (Poly_Triangle object) comprises a triplet of indices in the table of 3D nodes specific to the triangulation.
0046 //! - An optional table of 2D nodes (2D points), parallel to the table of 3D nodes.
0047 //!   2D point are the (u, v) parameters of the corresponding 3D point on the surface approximated by the triangulation.
0048 //! - An optional table of 3D vectors, parallel to the table of 3D nodes, defining normals to the surface at specified 3D point.
0049 //! - An optional deflection, which maximizes the distance from a point on the surface to the corresponding point on its approximate triangulation.
0050 //!
0051 //! In many cases, algorithms do not need to work with the exact representation of a surface.
0052 //! A triangular representation induces simpler and more robust adjusting, faster performances, and the results are as good.
0053 class Poly_Triangulation : public Standard_Transient
0054 {
0055   DEFINE_STANDARD_RTTIEXT(Poly_Triangulation, Standard_Transient)
0056 public:
0057 
0058   //! Constructs an empty triangulation.
0059   Standard_EXPORT Poly_Triangulation();
0060 
0061   //! Constructs a triangulation from a set of triangles.
0062   //! The triangulation is initialized without a triangle or a node,
0063   //! but capable of containing specified number of nodes and triangles.
0064   //! @param theNbNodes     [in] number of nodes to allocate
0065   //! @param theNbTriangles [in] number of triangles to allocate
0066   //! @param theHasUVNodes  [in] indicates whether 2D nodes will be associated with 3D ones,
0067   //!                            (i.e. to enable a 2D representation)
0068   //! @param theHasNormals  [in] indicates whether normals will be given and associated with nodes
0069   Standard_EXPORT Poly_Triangulation (const Standard_Integer theNbNodes,
0070                                       const Standard_Integer theNbTriangles,
0071                                       const Standard_Boolean theHasUVNodes,
0072                                       const Standard_Boolean theHasNormals = false);
0073 
0074   //! Constructs a triangulation from a set of triangles. The
0075   //! triangulation is initialized with 3D points from Nodes and triangles
0076   //! from Triangles.
0077   Standard_EXPORT Poly_Triangulation(const TColgp_Array1OfPnt& Nodes, const Poly_Array1OfTriangle& Triangles);
0078 
0079   //! Constructs a triangulation from a set of triangles. The
0080   //! triangulation is initialized with 3D points from Nodes, 2D points from
0081   //! UVNodes and triangles from Triangles, where
0082   //! coordinates of a 2D point from UVNodes are the
0083   //! (u, v) parameters of the corresponding 3D point
0084   //! from Nodes on the surface approximated by the
0085   //! constructed triangulation.
0086   Standard_EXPORT Poly_Triangulation(const TColgp_Array1OfPnt& Nodes, const TColgp_Array1OfPnt2d& UVNodes, const Poly_Array1OfTriangle& Triangles);
0087 
0088   //! Destructor
0089   Standard_EXPORT virtual ~Poly_Triangulation();
0090 
0091   //! Creates full copy of current triangulation
0092   Standard_EXPORT virtual Handle(Poly_Triangulation) Copy() const;
0093 
0094   //! Copy constructor for triangulation.
0095   Standard_EXPORT Poly_Triangulation (const Handle(Poly_Triangulation)& theTriangulation);
0096 
0097   //! Returns the deflection of this triangulation.
0098   Standard_Real Deflection() const { return myDeflection; }
0099 
0100   //! Sets the deflection of this triangulation to theDeflection.
0101   //! See more on deflection in Polygon2D
0102   void Deflection (const Standard_Real theDeflection) { myDeflection = theDeflection; }
0103 
0104   //! Returns initial set of parameters used to generate this triangulation.
0105   const Handle(Poly_TriangulationParameters)& Parameters() const { return myParams; }
0106 
0107   //! Updates initial set of parameters used to generate this triangulation.
0108   void Parameters (const Handle(Poly_TriangulationParameters)& theParams) { myParams = theParams; }
0109 
0110   //! Clears internal arrays of nodes and all attributes.
0111   Standard_EXPORT virtual void Clear();
0112 
0113   //! Returns TRUE if triangulation has some geometry.
0114   virtual Standard_Boolean HasGeometry() const { return !myNodes.IsEmpty() && !myTriangles.IsEmpty(); }
0115 
0116   //! Returns the number of nodes for this triangulation.
0117   Standard_Integer NbNodes() const { return myNodes.Length(); }
0118 
0119   //! Returns the number of triangles for this triangulation.
0120   Standard_Integer NbTriangles() const { return myTriangles.Length(); }
0121 
0122   //! Returns Standard_True if 2D nodes are associated with 3D nodes for this triangulation.
0123   Standard_Boolean HasUVNodes() const { return !myUVNodes.IsEmpty(); }
0124 
0125   //! Returns Standard_True if nodal normals are defined.
0126   Standard_Boolean HasNormals() const { return !myNormals.IsEmpty(); }
0127 
0128   //! Returns a node at the given index.
0129   //! @param[in] theIndex node index within [1, NbNodes()] range
0130   //! @return 3D point coordinates
0131   gp_Pnt Node (Standard_Integer theIndex) const { return myNodes.Value (theIndex - 1); }
0132 
0133   //! Sets a node coordinates.
0134   //! @param[in] theIndex node index within [1, NbNodes()] range
0135   //! @param[in] thePnt   3D point coordinates
0136   void SetNode (Standard_Integer theIndex,
0137                 const gp_Pnt& thePnt)
0138   {
0139     myNodes.SetValue (theIndex - 1, thePnt);
0140   }
0141 
0142   //! Returns UV-node at the given index.
0143   //! @param[in] theIndex node index within [1, NbNodes()] range
0144   //! @return 2D point defining UV coordinates
0145   gp_Pnt2d UVNode (Standard_Integer theIndex) const { return myUVNodes.Value (theIndex - 1); }
0146 
0147   //! Sets an UV-node coordinates.
0148   //! @param[in] theIndex node index within [1, NbNodes()] range
0149   //! @param[in] thePnt   UV coordinates
0150   void SetUVNode (Standard_Integer theIndex,
0151                   const gp_Pnt2d&  thePnt)
0152   {
0153     myUVNodes.SetValue (theIndex - 1, thePnt);
0154   }
0155 
0156   //! Returns triangle at the given index.
0157   //! @param[in] theIndex triangle index within [1, NbTriangles()] range
0158   //! @return triangle node indices, with each node defined within [1, NbNodes()] range
0159   const Poly_Triangle& Triangle (Standard_Integer theIndex) const { return myTriangles.Value (theIndex); }
0160 
0161   //! Sets a triangle.
0162   //! @param[in] theIndex triangle index within [1, NbTriangles()] range
0163   //! @param[in] theTriangle triangle node indices, with each node defined within [1, NbNodes()] range
0164   void SetTriangle (Standard_Integer theIndex,
0165                     const Poly_Triangle& theTriangle)
0166   {
0167     myTriangles.SetValue (theIndex, theTriangle);
0168   }
0169 
0170   //! Returns normal at the given index.
0171   //! @param[in] theIndex node index within [1, NbNodes()] range
0172   //! @return normalized 3D vector defining a surface normal
0173   gp_Dir Normal (Standard_Integer theIndex) const
0174   {
0175     const gp_Vec3f& aNorm = myNormals.Value (theIndex - 1);
0176     return gp_Dir (aNorm.x(), aNorm.y(), aNorm.z());
0177   }
0178 
0179   //! Returns normal at the given index.
0180   //! @param[in]  theIndex node index within [1, NbNodes()] range
0181   //! @param[out] theVec3  3D vector defining a surface normal
0182   void Normal (Standard_Integer theIndex,
0183                gp_Vec3f& theVec3) const
0184   {
0185     theVec3 = myNormals.Value (theIndex - 1);
0186   }
0187 
0188   //! Changes normal at the given index.
0189   //! @param[in] theIndex node index within [1, NbNodes()] range
0190   //! @param[in] theVec3  normalized 3D vector defining a surface normal
0191   void SetNormal (const Standard_Integer theIndex,
0192                   const gp_Vec3f& theNormal)
0193   {
0194     myNormals.SetValue (theIndex - 1, theNormal);
0195   }
0196 
0197   //! Changes normal at the given index.
0198   //! @param[in] theIndex  node index within [1, NbNodes()] range
0199   //! @param[in] theNormal normalized 3D vector defining a surface normal
0200   void SetNormal (const Standard_Integer theIndex,
0201                   const gp_Dir& theNormal)
0202   {
0203     SetNormal (theIndex, gp_Vec3f (float(theNormal.X()),
0204                                    float(theNormal.Y()),
0205                                    float(theNormal.Z())));
0206   }
0207 
0208   //! Returns mesh purpose bits.
0209   Poly_MeshPurpose MeshPurpose() const { return myPurpose; }
0210 
0211   //! Sets mesh purpose bits.
0212   void SetMeshPurpose (const Poly_MeshPurpose thePurpose) { myPurpose = thePurpose; }
0213 
0214   //! Returns cached min - max range of triangulation data,
0215   //! which is VOID by default (e.g, no cached information).
0216   Standard_EXPORT const Bnd_Box& CachedMinMax() const;
0217 
0218   //! Sets a cached min - max range of this triangulation.
0219   //! The bounding box should exactly match actual range of triangulation data
0220   //! without a gap or transformation, or otherwise undefined behavior will be observed.
0221   //! Passing a VOID range invalidates the cache.
0222   Standard_EXPORT void SetCachedMinMax (const Bnd_Box& theBox);
0223 
0224   //! Returns TRUE if there is some cached min - max range of this triangulation.
0225   Standard_EXPORT Standard_Boolean HasCachedMinMax() const { return myCachedMinMax != NULL; }
0226 
0227   //! Updates cached min - max range of this triangulation with bounding box of nodal data.
0228   void UpdateCachedMinMax()
0229   {
0230     Bnd_Box aBox;
0231     MinMax (aBox, gp_Trsf(), true);
0232     SetCachedMinMax (aBox);
0233   }
0234 
0235   //! Extends the passed box with bounding box of this triangulation.
0236   //! Uses cached min - max range when available and:
0237   //! - input transformation theTrsf has no rotation part;
0238   //! - theIsAccurate is set to FALSE;
0239   //! - no triangulation data available (e.g. it is deferred and not loaded).
0240   //! @param theBox [in] [out] bounding box to extend by this triangulation
0241   //! @param theTrsf [in] optional transformation
0242   //! @param theIsAccurate [in] when FALSE, allows using a cached min - max range of this triangulation
0243   //!                           even for non-identity transformation.
0244   //! @return FALSE if there is no any data to extend the passed box (no both triangulation and cached min - max range).
0245   Standard_EXPORT Standard_Boolean MinMax (Bnd_Box& theBox, const gp_Trsf& theTrsf, const bool theIsAccurate = false) const;
0246 
0247   //! Dumps the content of me into the stream
0248   Standard_EXPORT virtual void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const;
0249 
0250 public:
0251 
0252   //! Returns TRUE if node positions are defined with double precision; TRUE by default.
0253   bool IsDoublePrecision() const { return myNodes.IsDoublePrecision(); }
0254 
0255   //! Set if node positions should be defined with double or single precision for 3D and UV nodes.
0256   //! Raises exception if data was already allocated.
0257   Standard_EXPORT void SetDoublePrecision (bool theIsDouble);
0258 
0259   //! Method resizing internal arrays of nodes (synchronously for all attributes).
0260   //! @param theNbNodes   [in] new number of nodes
0261   //! @param theToCopyOld [in] copy old nodes into the new array
0262   Standard_EXPORT void ResizeNodes (Standard_Integer theNbNodes,
0263                                     Standard_Boolean theToCopyOld);
0264 
0265   //! Method resizing an internal array of triangles.
0266   //! @param theNbTriangles [in] new number of triangles
0267   //! @param theToCopyOld   [in] copy old triangles into the new array
0268   Standard_EXPORT void ResizeTriangles (Standard_Integer theNbTriangles,
0269                                         Standard_Boolean theToCopyOld);
0270 
0271   //! If an array for UV coordinates is not allocated yet, do it now.
0272   Standard_EXPORT void AddUVNodes();
0273 
0274   //! Deallocates the UV nodes array.
0275   Standard_EXPORT void RemoveUVNodes();
0276 
0277   //! If an array for normals is not allocated yet, do it now.
0278   Standard_EXPORT void AddNormals();
0279 
0280   //! Deallocates the normals array.
0281   Standard_EXPORT void RemoveNormals();
0282 
0283   //! Compute smooth normals by averaging triangle normals.
0284   Standard_EXPORT void ComputeNormals();
0285 
0286 public:
0287 
0288   //! Returns the table of 3D points for read-only access or NULL if nodes array is undefined.
0289   //! Poly_Triangulation::Node() should be used instead when possible.
0290   //! Returned object should not be used after Poly_Triangulation destruction.
0291   Standard_EXPORT Handle(TColgp_HArray1OfPnt) MapNodeArray() const;
0292 
0293   //! Returns the triangle array for read-only access or NULL if triangle array is undefined.
0294   //! Poly_Triangulation::Triangle() should be used instead when possible.
0295   //! Returned object should not be used after Poly_Triangulation destruction.
0296   Standard_EXPORT Handle(Poly_HArray1OfTriangle) MapTriangleArray() const;
0297 
0298   //! Returns the table of 2D nodes for read-only access or NULL if UV nodes array is undefined.
0299   //! Poly_Triangulation::UVNode() should be used instead when possible.
0300   //! Returned object should not be used after Poly_Triangulation destruction.
0301   Standard_EXPORT Handle(TColgp_HArray1OfPnt2d) MapUVNodeArray() const;
0302 
0303   //! Returns the table of per-vertex normals for read-only access or NULL if normals array is undefined.
0304   //! Poly_Triangulation::Normal() should be used instead when possible.
0305   //! Returned object should not be used after Poly_Triangulation destruction.
0306   Standard_EXPORT Handle(TShort_HArray1OfShortReal) MapNormalArray() const;
0307 
0308 public:
0309 
0310   //! Returns an internal array of triangles.
0311   //! Triangle()/SetTriangle() should be used instead in portable code.
0312   Poly_Array1OfTriangle& InternalTriangles() { return myTriangles; }
0313 
0314   //! Returns an internal array of nodes.
0315   //! Node()/SetNode() should be used instead in portable code.
0316   Poly_ArrayOfNodes& InternalNodes() { return myNodes; }
0317 
0318   //! Returns an internal array of UV nodes.
0319   //! UBNode()/SetUVNode() should be used instead in portable code.
0320   Poly_ArrayOfUVNodes& InternalUVNodes() { return myUVNodes; }
0321 
0322   //! Return an internal array of normals.
0323   //! Normal()/SetNormal() should be used instead in portable code.
0324   NCollection_Array1<gp_Vec3f>& InternalNormals() { return myNormals; }
0325 
0326   Standard_DEPRECATED("Deprecated method, SetNormal() should be used instead")
0327   Standard_EXPORT void SetNormals (const Handle(TShort_HArray1OfShortReal)& theNormals);
0328 
0329   Standard_DEPRECATED("Deprecated method, Triangle() should be used instead")
0330   const Poly_Array1OfTriangle& Triangles() const { return myTriangles; }
0331 
0332   Standard_DEPRECATED("Deprecated method, SetTriangle() should be used instead")
0333   Poly_Array1OfTriangle& ChangeTriangles() { return myTriangles; }
0334 
0335   Standard_DEPRECATED("Deprecated method, SetTriangle() should be used instead")
0336   Poly_Triangle& ChangeTriangle (const Standard_Integer theIndex) { return myTriangles.ChangeValue (theIndex); }
0337 
0338 public: //! @name late-load deferred data interface
0339 
0340   //! Returns number of deferred nodes that can be loaded using LoadDeferredData().
0341   //! Note: this is estimated values, which might be different from actually loaded values.
0342   //! Always check triangulation size of actually loaded data in code to avoid out-of-range issues.
0343   virtual Standard_Integer NbDeferredNodes() const { return 0; }
0344 
0345   //! Returns number of deferred triangles that can be loaded using LoadDeferredData().
0346   //! Note: this is estimated values, which might be different from actually loaded values
0347   //! Always check triangulation size of actually loaded data in code to avoid out-of-range issues.
0348   virtual Standard_Integer NbDeferredTriangles() const { return 0; }
0349 
0350   //! Returns TRUE if there is some triangulation data that can be loaded using LoadDeferredData().
0351   virtual Standard_Boolean HasDeferredData() const { return NbDeferredTriangles() > 0; }
0352 
0353   //! Loads triangulation data into itself
0354   //! from some deferred storage using specified shared input file system.
0355   Standard_EXPORT virtual Standard_Boolean LoadDeferredData (const Handle(OSD_FileSystem)& theFileSystem = Handle(OSD_FileSystem)());
0356 
0357   //! Loads triangulation data into new Poly_Triangulation object
0358   //! from some deferred storage using specified shared input file system.
0359   Standard_EXPORT virtual Handle(Poly_Triangulation) DetachedLoadDeferredData
0360     (const Handle(OSD_FileSystem)& theFileSystem = Handle(OSD_FileSystem)()) const;
0361 
0362   //! Releases triangulation data if it has connected deferred storage.
0363   Standard_EXPORT virtual Standard_Boolean UnloadDeferredData();
0364 
0365 protected:
0366 
0367   //! Creates new triangulation object (can be inheritor of Poly_Triangulation).
0368   virtual Handle(Poly_Triangulation) createNewEntity() const
0369   {
0370     return new Poly_Triangulation();
0371   }
0372 
0373   //! Load triangulation data from deferred storage using specified shared input file system.
0374   virtual Standard_Boolean loadDeferredData (const Handle(OSD_FileSystem)& theFileSystem,
0375                                              const Handle(Poly_Triangulation)& theDestTriangulation) const
0376   {
0377     (void )theFileSystem;
0378     (void )theDestTriangulation;
0379     return false;
0380   }
0381 
0382 protected:
0383 
0384   //! Clears cached min - max range saved previously.
0385   Standard_EXPORT void unsetCachedMinMax();
0386 
0387   //! Calculates bounding box of nodal data.
0388   //! @param theTrsf [in] optional transformation.
0389   Standard_EXPORT virtual Bnd_Box computeBoundingBox (const gp_Trsf& theTrsf) const;
0390 
0391 protected:
0392 
0393   Bnd_Box*                     myCachedMinMax;
0394   Standard_Real                myDeflection;
0395   Poly_ArrayOfNodes            myNodes;
0396   Poly_Array1OfTriangle        myTriangles;
0397   Poly_ArrayOfUVNodes          myUVNodes;
0398   NCollection_Array1<gp_Vec3f> myNormals;
0399   Poly_MeshPurpose             myPurpose;
0400 
0401   Handle(Poly_TriangulationParameters) myParams;
0402 };
0403 
0404 #endif // _Poly_Triangulation_HeaderFile