Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/opencascade/BRepMesh_Delaun.hxx was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // Copyright (c) 2013-2014 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 _BRepMesh_Delaun_HeaderFile
0015 #define _BRepMesh_Delaun_HeaderFile
0016 
0017 #include <Standard.hxx>
0018 #include <Standard_DefineAlloc.hxx>
0019 #include <Standard_Macro.hxx>
0020 
0021 #include <BRepMesh_CircleTool.hxx>
0022 #include <BRepMesh_Triangle.hxx>
0023 #include <BRepMesh_Edge.hxx>
0024 #include <IMeshData_Types.hxx>
0025 #include <BRepMesh_DataStructureOfDelaun.hxx>
0026 #include <BRepMesh_GeomTool.hxx>
0027 #include <Message_ProgressRange.hxx>
0028 
0029 class Bnd_Box2d;
0030 class BRepMesh_Vertex;
0031 
0032 //! Compute the Delaunay's triangulation with the algorithm of Watson.
0033 class BRepMesh_Delaun
0034 {
0035 public:
0036   DEFINE_STANDARD_ALLOC
0037 
0038   //! Creates instance of triangulator, but do not run the algorithm automatically.
0039   Standard_EXPORT BRepMesh_Delaun(const occ::handle<BRepMesh_DataStructureOfDelaun>& theOldMesh,
0040                                   const int                                          theCellsCountU,
0041                                   const int                                          theCellsCountV,
0042                                   const bool                                         isFillCircles);
0043 
0044   //! Creates the triangulation with an empty Mesh data structure.
0045   Standard_EXPORT BRepMesh_Delaun(IMeshData::Array1OfVertexOfDelaun& theVertices);
0046 
0047   //! Creates the triangulation with an existent Mesh data structure.
0048   Standard_EXPORT BRepMesh_Delaun(const occ::handle<BRepMesh_DataStructureOfDelaun>& theOldMesh,
0049                                   IMeshData::Array1OfVertexOfDelaun&                 theVertices);
0050 
0051   //! Creates the triangulation with an existant Mesh data structure.
0052   Standard_EXPORT BRepMesh_Delaun(const occ::handle<BRepMesh_DataStructureOfDelaun>& theOldMesh,
0053                                   IMeshData::VectorOfInteger& theVertexIndices);
0054 
0055   //! Creates the triangulation with an existant Mesh data structure.
0056   Standard_EXPORT BRepMesh_Delaun(const occ::handle<BRepMesh_DataStructureOfDelaun>& theOldMesh,
0057                                   IMeshData::VectorOfInteger& theVertexIndices,
0058                                   const int                   theCellsCountU,
0059                                   const int                   theCellsCountV);
0060 
0061   //! Initializes the triangulation with an array of vertices.
0062   Standard_EXPORT void Init(IMeshData::Array1OfVertexOfDelaun& theVertices);
0063 
0064   //! Forces initialization of circles cell filter using working structure.
0065   Standard_EXPORT void InitCirclesTool(const int theCellsCountU, const int theCellsCountV);
0066 
0067   //! Removes a vertex from the triangulation.
0068   Standard_EXPORT void RemoveVertex(const BRepMesh_Vertex& theVertex);
0069 
0070   //! Adds some vertices into the triangulation.
0071   Standard_EXPORT void AddVertices(IMeshData::VectorOfInteger&  theVerticesIndices,
0072                                    const Message_ProgressRange& theRange = Message_ProgressRange());
0073 
0074   //! Modify mesh to use the edge.
0075   //! @return True if done
0076   Standard_EXPORT bool UseEdge(const int theEdge);
0077 
0078   //! Gives the Mesh data structure.
0079   const occ::handle<BRepMesh_DataStructureOfDelaun>& Result() const { return myMeshData; }
0080 
0081   //! Forces insertion of constraint edges into the base triangulation.
0082   void ProcessConstraints()
0083   {
0084     insertInternalEdges();
0085 
0086     // Adjustment of meshes to boundary edges
0087     frontierAdjust();
0088   }
0089 
0090   //! Gives the list of frontier edges.
0091   Handle(IMeshData::MapOfInteger) Frontier() const { return getEdgesByType(BRepMesh_Frontier); }
0092 
0093   //! Gives the list of internal edges.
0094   Handle(IMeshData::MapOfInteger) InternalEdges() const { return getEdgesByType(BRepMesh_Fixed); }
0095 
0096   //! Gives the list of free edges used only one time
0097   Handle(IMeshData::MapOfInteger) FreeEdges() const { return getEdgesByType(BRepMesh_Free); }
0098 
0099   //! Gives vertex with the given index
0100   const BRepMesh_Vertex& GetVertex(const int theIndex) const
0101   {
0102     return myMeshData->GetNode(theIndex);
0103   }
0104 
0105   //! Gives edge with the given index
0106   const BRepMesh_Edge& GetEdge(const int theIndex) const { return myMeshData->GetLink(theIndex); }
0107 
0108   //! Gives triangle with the given index
0109   const BRepMesh_Triangle& GetTriangle(const int theIndex) const
0110   {
0111     return myMeshData->GetElement(theIndex);
0112   }
0113 
0114   //! Returns tool used to build mesh consistent to Delaunay criteria.
0115   const BRepMesh_CircleTool& Circles() const { return myCircles; }
0116 
0117   //! Test is the given triangle contains the given vertex.
0118   //! @param theSqTolerance square tolerance to check closeness to some edge
0119   //! @param theEdgeOn If it is != 0 the vertex lies onto the edge index
0120   //!        returned through this parameter.
0121   Standard_EXPORT bool Contains(const int              theTriangleId,
0122                                 const BRepMesh_Vertex& theVertex,
0123                                 const double           theSqTolerance,
0124                                 int&                   theEdgeOn) const;
0125 
0126   //! Explicitly sets ids of auxiliary vertices used to build mesh and used by 3rd-party algorithms.
0127   inline void SetAuxVertices(const IMeshData::VectorOfInteger& theSupVert)
0128   {
0129     mySupVert = theSupVert;
0130   }
0131 
0132   //! Destruction of auxiliary triangles containing the given vertices.
0133   //! Removes auxiliary vertices also.
0134   //! @param theAuxVertices auxiliary vertices to be cleaned up.
0135   Standard_EXPORT void RemoveAuxElements();
0136 
0137 private:
0138   enum ReplaceFlag
0139   {
0140     Replace,
0141     InsertAfter,
0142     InsertBefore
0143   };
0144 
0145   typedef NCollection_DataMap<int, IMeshData::MapOfInteger> DataMapOfMap;
0146 
0147   //! Performs initialization of circles cell filter tool.
0148   void initCirclesTool(const Bnd_Box2d& theBox, const int theCellsCountU, const int theCellsCountV);
0149 
0150   //! Add bounding box for edge defined by start & end point to
0151   //! the given vector of bounding boxes for triangulation edges.
0152   void fillBndBox(IMeshData::SequenceOfBndB2d& theBoxes,
0153                   const BRepMesh_Vertex&       theV1,
0154                   const BRepMesh_Vertex&       theV2);
0155 
0156   //! Gives the list of edges with type defined by the input parameter.
0157   //! If the given type is BRepMesh_Free returns list of edges
0158   //! that have number of connected elements less or equal 1.
0159   Handle(IMeshData::MapOfInteger) getEdgesByType(const BRepMesh_DegreeOfFreedom theEdgeType) const;
0160 
0161   //! Run triangulation procedure.
0162   void perform(IMeshData::VectorOfInteger& theVertexIndices,
0163                const int                   theCellsCountU = -1,
0164                const int                   theCellsCountV = -1);
0165 
0166   //! Build the super mesh.
0167   void superMesh(const Bnd_Box2d& theBox);
0168 
0169   //! Computes the triangulation and adds the vertices,
0170   //! edges and triangles to the Mesh data structure.
0171   void compute(IMeshData::VectorOfInteger& theVertexIndices);
0172 
0173   //! Adjust the mesh on the frontier.
0174   void frontierAdjust();
0175 
0176   //! Find left polygon of the given edge and call meshPolygon.
0177   bool meshLeftPolygonOf(const int                       theEdgeIndex,
0178                          const bool                      isForward,
0179                          Handle(IMeshData::MapOfInteger) theSkipped = nullptr);
0180 
0181   //! Find next link starting from the given node and has maximum
0182   //! angle respect the given reference link.
0183   //! Each time the next link is found other neighbor links at the pivot
0184   //! node are marked as leprous and will be excluded from consideration
0185   //! next time until a hanging end is occurred.
0186   int findNextPolygonLink(const int&                             theFirstNode,
0187                           const int&                             thePivotNode,
0188                           const BRepMesh_Vertex&                 thePivotVertex,
0189                           const gp_Vec2d&                        theRefLinkDir,
0190                           const IMeshData::SequenceOfBndB2d&     theBoxes,
0191                           const IMeshData::SequenceOfInteger&    thePolygon,
0192                           const Handle(IMeshData::MapOfInteger)& theSkipped,
0193                           const bool&                            isSkipLeprous,
0194                           IMeshData::MapOfInteger&               theLeprousLinks,
0195                           IMeshData::MapOfInteger&               theDeadLinks,
0196                           int&                                   theNextPivotNode,
0197                           gp_Vec2d&                              theNextLinkDir,
0198                           Bnd_B2d&                               theNextLinkBndBox);
0199 
0200   //! Check is the given link intersects the polygon boundaries.
0201   //! Returns bounding box for the given link through the theLinkBndBox parameter.
0202   bool checkIntersection(const BRepMesh_Edge&                theLink,
0203                          const IMeshData::SequenceOfInteger& thePolygon,
0204                          const IMeshData::SequenceOfBndB2d&  thePolyBoxes,
0205                          const bool                          isConsiderEndPointTouch,
0206                          const bool                          isConsiderPointOnEdge,
0207                          const bool                          isSkipLastEdge,
0208                          Bnd_B2d&                            theLinkBndBox) const;
0209 
0210   //! Triangulatiion of a closed polygon described by the list
0211   //! of indexes of its edges in the structure.
0212   //! (negative index means reversed edge)
0213   void meshPolygon(IMeshData::SequenceOfInteger&   thePolygon,
0214                    IMeshData::SequenceOfBndB2d&    thePolyBoxes,
0215                    Handle(IMeshData::MapOfInteger) theSkipped = nullptr);
0216 
0217   //! Decomposes the given closed simple polygon (polygon without glued edges
0218   //! and loops) on two simpler ones by adding new link at the most thin part
0219   //! in respect to end point of the first link.
0220   //! In case if source polygon consists of three links, creates new triangle
0221   //! and clears source container.
0222   //! @param thePolygon source polygon to be decomposed (first part of decomposition).
0223   //! @param thePolyBoxes bounding boxes corresponded to source polygon's links.
0224   //! @param thePolygonCut product of decomposition of source polygon (second part of
0225   //! decomposition).
0226   //! @param thePolyBoxesCut bounding boxes corresponded to resulting polygon's links.
0227   void decomposeSimplePolygon(IMeshData::SequenceOfInteger& thePolygon,
0228                               IMeshData::SequenceOfBndB2d&  thePolyBoxes,
0229                               IMeshData::SequenceOfInteger& thePolygonCut,
0230                               IMeshData::SequenceOfBndB2d&  thePolyBoxesCut);
0231 
0232   //! Triangulation of closed polygon containing only three edges.
0233   bool meshElementaryPolygon(const IMeshData::SequenceOfInteger& thePolygon);
0234 
0235   //! Creates the triangles between the given node and the given polyline.
0236   void createTriangles(const int theVertexIndex, IMeshData::MapOfIntegerInteger& thePoly);
0237 
0238   //! Add a triangle based on the given oriented edges into mesh
0239   void addTriangle(const int (&theEdgesId)[3],
0240                    const bool (&theEdgesOri)[3],
0241                    const int (&theNodesId)[3]);
0242 
0243   //! Deletes the triangle with the given index and adds the free edges into the map.
0244   //! When an edge is suppressed more than one time it is destroyed.
0245   void deleteTriangle(const int theIndex, IMeshData::MapOfIntegerInteger& theLoopEdges);
0246 
0247   //! Returns start and end nodes of the given edge in respect to its orientation.
0248   void getOrientedNodes(const BRepMesh_Edge& theEdge, const bool isForward, int* theNodes) const;
0249 
0250   //! Processes loop within the given polygon formed by range of its
0251   //! links specified by start and end link indices.
0252   void processLoop(const int                           theLinkFrom,
0253                    const int                           theLinkTo,
0254                    const IMeshData::SequenceOfInteger& thePolygon,
0255                    const IMeshData::SequenceOfBndB2d&  thePolyBoxes);
0256 
0257   //! Creates new link based on the given nodes and updates the given polygon.
0258   int createAndReplacePolygonLink(const int                     theNodes[],
0259                                   const gp_Pnt2d                thePnts[],
0260                                   const int                     theRootIndex,
0261                                   const ReplaceFlag             theReplaceFlag,
0262                                   IMeshData::SequenceOfInteger& thePolygon,
0263                                   IMeshData::SequenceOfBndB2d&  thePolyBoxes);
0264 
0265   //! Creates the triangles on new nodes.
0266   void createTrianglesOnNewVertices(IMeshData::VectorOfInteger&  theVertexIndices,
0267                                     const Message_ProgressRange& theRange);
0268 
0269   //! Cleanup mesh from the free triangles.
0270   void cleanupMesh();
0271 
0272   //! Goes through the neighbour triangles around the given node started
0273   //! from the given link, returns TRUE if some triangle has a bounding
0274   //! frontier edge or FALSE elsewhere.
0275   bool isBoundToFrontier(const int theRefNodeId, const int theRefLinkId);
0276 
0277   //! Remove internal triangles from the given polygon.
0278   void cleanupPolygon(const IMeshData::SequenceOfInteger& thePolygon,
0279                       const IMeshData::SequenceOfBndB2d&  thePolyBoxes);
0280 
0281   //! Checks is the given vertex lies inside the polygon.
0282   bool isVertexInsidePolygon(const int&                        theVertexId,
0283                              const IMeshData::VectorOfInteger& thePolygonVertices) const;
0284 
0285   //! Remove all triangles and edges that are placed inside the polygon or crossed it.
0286   void killTrianglesAroundVertex(const int                           theZombieNodeId,
0287                                  const IMeshData::VectorOfInteger&   thePolyVertices,
0288                                  const IMeshData::MapOfInteger&      thePolyVerticesFindMap,
0289                                  const IMeshData::SequenceOfInteger& thePolygon,
0290                                  const IMeshData::SequenceOfBndB2d&  thePolyBoxes,
0291                                  IMeshData::MapOfInteger&            theSurvivedLinks,
0292                                  IMeshData::MapOfIntegerInteger&     theLoopEdges,
0293                                  IMeshData::VectorOfInteger&         theVictimNodes);
0294 
0295   //! Checks is the given link crosses the polygon boundary.
0296   //! If yes, kills its triangles and checks neighbor links on boundary intersection. Does nothing
0297   //! elsewhere.
0298   void killTrianglesOnIntersectingLinks(const int&                          theLinkToCheckId,
0299                                         const BRepMesh_Edge&                theLinkToCheck,
0300                                         const int&                          theEndPoint,
0301                                         const IMeshData::SequenceOfInteger& thePolygon,
0302                                         const IMeshData::SequenceOfBndB2d&  thePolyBoxes,
0303                                         IMeshData::MapOfInteger&            theSurvivedLinks,
0304                                         IMeshData::MapOfIntegerInteger&     theLoopEdges);
0305 
0306   //! Kill triangles bound to the given link.
0307   void killLinkTriangles(const int& theLinkId, IMeshData::MapOfIntegerInteger& theLoopEdges);
0308 
0309   //! Calculates distances between the given point and edges of triangle.
0310   double calculateDist(const gp_XY            theVEdges[3],
0311                        const gp_XY            thePoints[3],
0312                        const BRepMesh_Vertex& theVertex,
0313                        double                 theDistance[3],
0314                        double                 theSqModulus[3],
0315                        int&                   theEdgeOn) const;
0316 
0317   //! Checks intersection between the two segments.
0318   BRepMesh_GeomTool::IntFlag intSegSeg(const BRepMesh_Edge& theEdge1,
0319                                        const BRepMesh_Edge& theEdge2,
0320                                        const bool           isConsiderEndPointTouch,
0321                                        const bool           isConsiderPointOnEdge,
0322                                        gp_Pnt2d&            theIntPnt) const;
0323 
0324   //! Returns area of the loop of the given polygon defined by indices of its start and end links.
0325   double polyArea(const IMeshData::SequenceOfInteger& thePolygon,
0326                   const int                           theStartIndex,
0327                   const int                           theEndIndex) const;
0328 
0329   //! Performs insertion of internal edges into mesh.
0330   void insertInternalEdges();
0331 
0332   //! Checks whether the given vertex id relates to super contour.
0333   bool isSupVertex(const int theVertexIdx) const
0334   {
0335     for (IMeshData::VectorOfInteger::Iterator aIt(mySupVert); aIt.More(); aIt.Next())
0336     {
0337       if (theVertexIdx == aIt.Value())
0338       {
0339         return true;
0340       }
0341     }
0342 
0343     return false;
0344   }
0345 
0346 private:
0347   occ::handle<BRepMesh_DataStructureOfDelaun> myMeshData;
0348   BRepMesh_CircleTool                         myCircles;
0349   IMeshData::VectorOfInteger                  mySupVert;
0350   bool                                        myInitCircles;
0351   BRepMesh_Triangle                           mySupTrian;
0352 };
0353 
0354 #endif