0026106: BRepMesh - revision of data model
[occt.git] / src / BRepMesh / BRepMesh_Delaun.hxx
CommitLineData
d5f74e42 1// Copyright (c) 2013-2014 OPEN CASCADE SAS
304c45c8 2//
973c2be1 3// This file is part of Open CASCADE Technology software library.
304c45c8 4//
d5f74e42 5// This library is free software; you can redistribute it and/or modify it under
6// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 7// by the Free Software Foundation, with special exception defined in the file
8// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9// distribution for complete text of the license and disclaimer of any warranty.
304c45c8 10//
973c2be1 11// Alternatively, this file may be used under the terms of Open CASCADE
12// commercial license or contractual agreement.
304c45c8 13
14#ifndef _BRepMesh_Delaun_HeaderFile
15#define _BRepMesh_Delaun_HeaderFile
16
17#include <Standard.hxx>
18#include <Standard_DefineAlloc.hxx>
19#include <Standard_Macro.hxx>
20
fc9b36d6 21#include <gp_XY.hxx>
22#include <gp_XYZ.hxx>
304c45c8 23#include <BRepMesh_CircleTool.hxx>
24#include <BRepMesh_Triangle.hxx>
25#include <BRepMesh_Edge.hxx>
7bd071ed 26#include <IMeshData_Types.hxx>
304c45c8 27#include <BRepMesh_DataStructureOfDelaun.hxx>
fc9b36d6 28#include <BRepMesh_GeomTool.hxx>
b7c077b9 29#include <TColStd_Array1OfInteger.hxx>
30#include <TColStd_SequenceOfInteger.hxx>
31#include <TColStd_MapOfInteger.hxx>
304c45c8 32
33class Bnd_B2d;
34class Bnd_Box2d;
304c45c8 35class BRepMesh_Vertex;
304c45c8 36
ebc93ae7 37//! Compute the Delaunay's triangulation with the algorithm of Watson.
38class BRepMesh_Delaun
39{
304c45c8 40public:
41
42 DEFINE_STANDARD_ALLOC
43
ebc93ae7 44 //! Creates the triangulation with an empty Mesh data structure.
7bd071ed 45 Standard_EXPORT BRepMesh_Delaun (IMeshData::Array1OfVertexOfDelaun& theVertices);
304c45c8 46
ebc93ae7 47 //! Creates the triangulation with an existent Mesh data structure.
48 Standard_EXPORT BRepMesh_Delaun (const Handle(BRepMesh_DataStructureOfDelaun)& theOldMesh,
7bd071ed 49 IMeshData::Array1OfVertexOfDelaun& theVertices);
304c45c8 50
ebc93ae7 51 //! Creates the triangulation with an existant Mesh data structure.
52 Standard_EXPORT BRepMesh_Delaun (const Handle(BRepMesh_DataStructureOfDelaun)& theOldMesh,
7bd071ed 53 IMeshData::VectorOfInteger& theVertexIndices);
304c45c8 54
3e782664 55 //! Creates the triangulation with an existant Mesh data structure.
56 Standard_EXPORT BRepMesh_Delaun (const Handle (BRepMesh_DataStructureOfDelaun)& theOldMesh,
7bd071ed 57 IMeshData::VectorOfInteger& theVertexIndices,
3e782664 58 const Standard_Integer theCellsCountU,
59 const Standard_Integer theCellsCountV);
60
ebc93ae7 61 //! Initializes the triangulation with an array of vertices.
7bd071ed 62 Standard_EXPORT void Init (IMeshData::Array1OfVertexOfDelaun& theVertices);
304c45c8 63
ebc93ae7 64 //! Removes a vertex from the triangulation.
65 Standard_EXPORT void RemoveVertex (const BRepMesh_Vertex& theVertex);
304c45c8 66
ebc93ae7 67 //! Adds some vertices into the triangulation.
7bd071ed 68 Standard_EXPORT void AddVertices (IMeshData::VectorOfInteger& theVerticesIndices);
304c45c8 69
ebc93ae7 70 //! Modify mesh to use the edge.
71 //! @return True if done
72 Standard_EXPORT Standard_Boolean UseEdge (const Standard_Integer theEdge);
304c45c8 73
ebc93ae7 74 //! Gives the Mesh data structure.
fc9b36d6 75 inline const Handle(BRepMesh_DataStructureOfDelaun)& Result() const
304c45c8 76 {
77 return myMeshData;
78 }
79
ebc93ae7 80 //! Gives the list of frontier edges.
7bd071ed 81 inline Handle(IMeshData::MapOfInteger) Frontier() const
304c45c8 82 {
ebc93ae7 83 return getEdgesByType (BRepMesh_Frontier);
304c45c8 84 }
85
ebc93ae7 86 //! Gives the list of internal edges.
7bd071ed 87 inline Handle(IMeshData::MapOfInteger) InternalEdges() const
304c45c8 88 {
ebc93ae7 89 return getEdgesByType (BRepMesh_Fixed);
304c45c8 90 }
91
ebc93ae7 92 //! Gives the list of free edges used only one time
7bd071ed 93 inline Handle(IMeshData::MapOfInteger) FreeEdges() const
304c45c8 94 {
ebc93ae7 95 return getEdgesByType (BRepMesh_Free);
304c45c8 96 }
ebc93ae7 97
98 //! Gives vertex with the given index
99 inline const BRepMesh_Vertex& GetVertex (const Standard_Integer theIndex) const
304c45c8 100 {
ebc93ae7 101 return myMeshData->GetNode (theIndex);
304c45c8 102 }
103
ebc93ae7 104 //! Gives edge with the given index
105 inline const BRepMesh_Edge& GetEdge (const Standard_Integer theIndex) const
304c45c8 106 {
ebc93ae7 107 return myMeshData->GetLink (theIndex);
304c45c8 108 }
109
ebc93ae7 110 //! Gives triangle with the given index
111 inline const BRepMesh_Triangle& GetTriangle (const Standard_Integer theIndex) const
304c45c8 112 {
ebc93ae7 113 return myMeshData->GetElement (theIndex);
304c45c8 114 }
115
74da0216 116 //! Returns tool used to build mesh consistent to Delaunay criteria.
117 inline const BRepMesh_CircleTool& Circles() const
118 {
119 return myCircles;
120 }
121
ebc93ae7 122 //! Test is the given triangle contains the given vertex.
71316196 123 //! @param theSqTolerance square tolerance to check closeness to some edge
124 //! @param theEdgeOn If it is != 0 the vertex lies onto the edge index
125 //! returned through this parameter.
ebc93ae7 126 Standard_EXPORT Standard_Boolean Contains (const Standard_Integer theTriangleId,
304c45c8 127 const BRepMesh_Vertex& theVertex,
71316196 128 const Standard_Real theSqTolerance,
ebc93ae7 129 Standard_Integer& theEdgeOn) const;
304c45c8 130
131private:
132
304c45c8 133 enum ReplaceFlag
134 {
135 Replace,
136 InsertAfter,
137 InsertBefore
138 };
139
7bd071ed 140 typedef NCollection_DataMap<Standard_Integer, IMeshData::MapOfInteger> DataMapOfMap;
304c45c8 141
ebc93ae7 142 //! Add boundig box for edge defined by start & end point to
143 //! the given vector of bounding boxes for triangulation edges.
7bd071ed 144 void fillBndBox (IMeshData::SequenceOfBndB2d& theBoxes,
848fa7e3 145 const BRepMesh_Vertex& theV1,
146 const BRepMesh_Vertex& theV2);
304c45c8 147
ebc93ae7 148 //! Gives the list of edges with type defined by the input parameter.
149 //! If the given type is BRepMesh_Free returns list of edges
150 //! that have number of connected elements less or equal 1.
7bd071ed 151 Handle(IMeshData::MapOfInteger) getEdgesByType (const BRepMesh_DegreeOfFreedom theEdgeType) const;
304c45c8 152
3e782664 153 //! Run triangulation procedure.
7bd071ed 154 void perform (IMeshData::VectorOfInteger& theVertexIndices,
155 const Standard_Integer theCellsCountU = -1,
156 const Standard_Integer theCellsCountV = -1);
304c45c8 157
ebc93ae7 158 //! Build the super mesh.
3e782664 159 void superMesh (const Bnd_Box2d& theBox,
160 const Standard_Integer theCellsCountU,
161 const Standard_Integer theCellsCountV);
304c45c8 162
ebc93ae7 163 //! Computes the triangulation and adds the vertices,
164 //! edges and triangles to the Mesh data structure.
7bd071ed 165 void compute (IMeshData::VectorOfInteger& theVertexIndices);
304c45c8 166
ebc93ae7 167 //! Adjust the mesh on the frontier.
304c45c8 168 void frontierAdjust();
169
ebc93ae7 170 //! Find left polygon of the given edge and call meshPolygon.
2caff0b3 171 Standard_Boolean meshLeftPolygonOf(
7bd071ed 172 const Standard_Integer theEdgeIndex,
173 const Standard_Boolean isForward,
174 Handle(IMeshData::MapOfInteger) theSkipped = NULL);
ebc93ae7 175
176 //! Find next link starting from the given node and has maximum
177 //! angle respect the given reference link.
178 //! Each time the next link is found other neighbor links at the pivot
179 //! node are marked as leprous and will be excluded from consideration
180 //! next time until a hanging end is occured.
7bd071ed 181 Standard_Integer findNextPolygonLink (const Standard_Integer& theFirstNode,
182 const Standard_Integer& thePivotNode,
183 const BRepMesh_Vertex& thePivotVertex,
184 const gp_Vec2d& theRefLinkDir,
185 const IMeshData::SequenceOfBndB2d& theBoxes,
186 const IMeshData::SequenceOfInteger& thePolygon,
187 const Handle(IMeshData::MapOfInteger) theSkipped,
188 const Standard_Boolean& isSkipLeprous,
189 IMeshData::MapOfInteger& theLeprousLinks,
190 IMeshData::MapOfInteger& theDeadLinks,
191 Standard_Integer& theNextPivotNode,
192 gp_Vec2d& theNextLinkDir,
193 Bnd_B2d& theNextLinkBndBox);
304c45c8 194
ebc93ae7 195 //! Check is the given link intersects the polygon boundaries.
196 //! Returns bounding box for the given link trough the theLinkBndBox parameter.
7bd071ed 197 Standard_Boolean checkIntersection (const BRepMesh_Edge& theLink,
198 const IMeshData::SequenceOfInteger& thePolygon,
199 const IMeshData::SequenceOfBndB2d& thePolyBoxes,
200 const Standard_Boolean isConsiderEndPointTouch,
201 const Standard_Boolean isConsiderPointOnEdge,
202 const Standard_Boolean isSkipLastEdge,
203 Bnd_B2d& theLinkBndBox) const;
304c45c8 204
ebc93ae7 205 //! Triangulatiion of a closed polygon described by the list
206 //! of indexes of its edges in the structure.
207 //! (negative index means reversed edge)
7bd071ed 208 void meshPolygon (IMeshData::SequenceOfInteger& thePolygon,
209 IMeshData::SequenceOfBndB2d& thePolyBoxes,
210 Handle(IMeshData::MapOfInteger) theSkipped = NULL);
304c45c8 211
0a9b38ef 212 //! Decomposes the given closed simple polygon (polygon without glued edges
213 //! and loops) on two simpler ones by adding new link at the most thin part
214 //! in respect to end point of the first link.
215 //! In case if source polygon consists of three links, creates new triangle
216 //! and clears source container.
217 //! @param thePolygon source polygon to be decomposed (first part of decomposition).
218 //! @param thePolyBoxes bounding boxes corresponded to source polygon's links.
219 //! @param thePolygonCut product of decomposition of source polygon (second part of decomposition).
220 //! @param thePolyBoxesCut bounding boxes corresponded to resulting polygon's links.
221 void decomposeSimplePolygon (
7bd071ed 222 IMeshData::SequenceOfInteger& thePolygon,
223 IMeshData::SequenceOfBndB2d& thePolyBoxes,
224 IMeshData::SequenceOfInteger& thePolygonCut,
225 IMeshData::SequenceOfBndB2d& thePolyBoxesCut);
304c45c8 226
227 //! Triangulation of closed polygon containing only three edges.
7bd071ed 228 inline Standard_Boolean meshElementaryPolygon (const IMeshData::SequenceOfInteger& thePolygon);
304c45c8 229
ebc93ae7 230 //! Creates the triangles beetween the given node and the given polyline.
848fa7e3 231 void createTriangles (const Standard_Integer theVertexIndex,
7bd071ed 232 IMeshData::MapOfIntegerInteger& thePoly);
304c45c8 233
ebc93ae7 234 //! Add a triangle based on the given oriented edges into mesh
7bd071ed 235 inline void addTriangle (const Standard_Integer (&theEdgesId)[3],
236 const Standard_Boolean (&theEdgesOri)[3],
237 const Standard_Integer (&theNodesId)[3]);
ebc93ae7 238
239 //! Deletes the triangle with the given index and adds the free edges into the map.
240 //! When an edge is suppressed more than one time it is destroyed.
848fa7e3 241 void deleteTriangle (const Standard_Integer theIndex,
7bd071ed 242 IMeshData::MapOfIntegerInteger& theLoopEdges);
ebc93ae7 243
244 //! Returns start and end nodes of the given edge in respect to its orientation.
245 void getOrientedNodes (const BRepMesh_Edge& theEdge,
246 const Standard_Boolean isForward,
247 Standard_Integer* theNodes) const;
248
249 //! Processes loop within the given polygon formed by range of its
250 //! links specified by start and end link indices.
7bd071ed 251 void processLoop (const Standard_Integer theLinkFrom,
252 const Standard_Integer theLinkTo,
253 const IMeshData::SequenceOfInteger& thePolygon,
254 const IMeshData::SequenceOfBndB2d& thePolyBoxes);
304c45c8 255
256 //! Creates new link based on the given nodes and updates the given polygon.
7bd071ed 257 Standard_Integer createAndReplacePolygonLink (const Standard_Integer theNodes[],
258 const gp_Pnt2d thePnts [],
259 const Standard_Integer theRootIndex,
260 const ReplaceFlag theReplaceFlag,
261 IMeshData::SequenceOfInteger& thePolygon,
262 IMeshData::SequenceOfBndB2d& thePolyBoxes);
304c45c8 263
ebc93ae7 264 //! Creates the triangles on new nodes.
7bd071ed 265 void createTrianglesOnNewVertices (IMeshData::VectorOfInteger& theVertexIndices);
304c45c8 266
ebc93ae7 267 //! Cleanup mesh from the free triangles.
304c45c8 268 void cleanupMesh();
269
270 //! Goes through the neighbour triangles around the given node started
271 //! from the given link, returns TRUE if some triangle has a bounding
272 //! frontier edge or FALSE elsewhere.
273 //! Stop link is used to prevent cycles.
274 //! Previous element Id is used to identify next neighbor element.
ebc93ae7 275 Standard_Boolean isBoundToFrontier (const Standard_Integer theRefNodeId,
276 const Standard_Integer theRefLinkId,
277 const Standard_Integer theStopLinkId,
278 const Standard_Integer thePrevElementId);
279
280 //! Remove internal triangles from the given polygon.
7bd071ed 281 void cleanupPolygon (const IMeshData::SequenceOfInteger& thePolygon,
282 const IMeshData::SequenceOfBndB2d& thePolyBoxes);
ebc93ae7 283
284 //! Checks is the given vertex lies inside the polygon.
7bd071ed 285 Standard_Boolean isVertexInsidePolygon (const Standard_Integer& theVertexId,
286 const IMeshData::VectorOfInteger& thePolygonVertices) const;
ebc93ae7 287
288 //! Remove all triangles and edges that are placed inside the polygon or crossed it.
7bd071ed 289 void killTrianglesAroundVertex (const Standard_Integer theZombieNodeId,
290 const IMeshData::VectorOfInteger& thePolyVertices,
291 const IMeshData::MapOfInteger& thePolyVerticesFindMap,
292 const IMeshData::SequenceOfInteger& thePolygon,
293 const IMeshData::SequenceOfBndB2d& thePolyBoxes,
294 IMeshData::MapOfInteger& theSurvivedLinks,
295 IMeshData::MapOfIntegerInteger& theLoopEdges);
304c45c8 296
ebc93ae7 297 //! Checks is the given link crosses the polygon boundary.
298 //! If yes, kills its triangles and checks neighbor links on boundary intersection. Does nothing elsewhere.
7bd071ed 299 void killTrianglesOnIntersectingLinks (const Standard_Integer& theLinkToCheckId,
300 const BRepMesh_Edge& theLinkToCheck,
301 const Standard_Integer& theEndPoint,
302 const IMeshData::SequenceOfInteger& thePolygon,
303 const IMeshData::SequenceOfBndB2d& thePolyBoxes,
304 IMeshData::MapOfInteger& theSurvivedLinks,
305 IMeshData::MapOfIntegerInteger& theLoopEdges);
304c45c8 306
ebc93ae7 307 //! Kill triangles bound to the given link.
7bd071ed 308 void killLinkTriangles (const Standard_Integer& theLinkId,
309 IMeshData::MapOfIntegerInteger& theLoopEdges);
304c45c8 310
ebc93ae7 311 //! Calculates distances between the given point and edges of triangle.
312 Standard_Real calculateDist (const gp_XY theVEdges[3],
304c45c8 313 const gp_XY thePoints[3],
304c45c8 314 const BRepMesh_Vertex& theVertex,
315 Standard_Real theDistance[3],
316 Standard_Real theSqModulus[3],
ebc93ae7 317 Standard_Integer& theEdgeOn) const;
318
ebc93ae7 319 //! Checks intersection between the two segments.
fc9b36d6 320 BRepMesh_GeomTool::IntFlag intSegSeg(
01a6e62b 321 const BRepMesh_Edge& theEdge1,
322 const BRepMesh_Edge& theEdge2,
323 const Standard_Boolean isConsiderEndPointTouch,
324 const Standard_Boolean isConsiderPointOnEdge,
325 gp_Pnt2d& theIntPnt) const;
304c45c8 326
ebc93ae7 327 //! Returns area of the loop of the given polygon defined by indices of its start and end links.
7bd071ed 328 Standard_Real polyArea (const IMeshData::SequenceOfInteger& thePolygon,
329 const Standard_Integer theStartIndex,
330 const Standard_Integer theEndIndex) const;
ebc93ae7 331
df18769e 332 //! Performs insertion of internal edges into mesh.
333 void insertInternalEdges();
334
304c45c8 335private:
336
ebc93ae7 337 Handle(BRepMesh_DataStructureOfDelaun) myMeshData;
ebc93ae7 338 BRepMesh_CircleTool myCircles;
339 Standard_Integer mySupVert[3];
340 BRepMesh_Triangle mySupTrian;
341
304c45c8 342};
343
344#endif