Commit | Line | Data |
---|---|---|
b311480e | 1 | // Created on: 1993-05-12 |
2 | // Created by: Didier PIFFAULT | |
3 | // Copyright (c) 1993-1999 Matra Datavision | |
973c2be1 | 4 | // Copyright (c) 1999-2014 OPEN CASCADE SAS |
b311480e | 5 | // |
973c2be1 | 6 | // This file is part of Open CASCADE Technology software library. |
b311480e | 7 | // |
d5f74e42 | 8 | // This library is free software; you can redistribute it and/or modify it under |
9 | // the terms of the GNU Lesser General Public License version 2.1 as published | |
973c2be1 | 10 | // by the Free Software Foundation, with special exception defined in the file |
11 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT | |
12 | // distribution for complete text of the license and disclaimer of any warranty. | |
b311480e | 13 | // |
973c2be1 | 14 | // Alternatively, this file may be used under the terms of Open CASCADE |
15 | // commercial license or contractual agreement. | |
0d88155b | 16 | |
304c45c8 | 17 | #include <BRepMesh_Delaun.hxx> |
18 | ||
19 | #include <gp.hxx> | |
0d88155b O |
20 | #include <gp_XY.hxx> |
21 | #include <gp_Pnt2d.hxx> | |
22 | #include <gp_Vec2d.hxx> | |
304c45c8 | 23 | |
0d88155b O |
24 | #include <Precision.hxx> |
25 | #include <Bnd_Box2d.hxx> | |
304c45c8 | 26 | #include <Bnd_B2d.hxx> |
27 | ||
0d88155b | 28 | #include <BRepMesh_SelectorOfDataStructureOfDelaun.hxx> |
304c45c8 | 29 | |
30 | #include <BRepMesh_Edge.hxx> | |
31 | #include <BRepMesh_Vertex.hxx> | |
32 | #include <BRepMesh_Triangle.hxx> | |
33 | ||
34 | #include <NCollection_Vector.hxx> | |
0d88155b | 35 | |
fc9b36d6 | 36 | #include <algorithm> |
0d88155b | 37 | |
304c45c8 | 38 | const Standard_Real AngDeviation1Deg = M_PI/180.; |
39 | const Standard_Real AngDeviation90Deg = 90 * AngDeviation1Deg; | |
40 | const Standard_Real Angle2PI = 2 * M_PI; | |
0d88155b | 41 | |
fc9b36d6 | 42 | const Standard_Real Precision = Precision::PConfusion(); |
43 | const Standard_Real Precision2 = Precision * Precision; | |
44 | ||
45 | namespace { | |
46 | //! Sort two points in projection on vector (1,1) | |
47 | struct ComparatorOfVertexOfDelaun | |
48 | { | |
49 | bool operator() (const BRepMesh_Vertex& theLeft, const BRepMesh_Vertex& theRight) | |
50 | { | |
51 | return theLeft.Coord().X() + theLeft.Coord().Y() < theRight.Coord().X() + theRight.Coord().Y(); | |
52 | } | |
53 | }; | |
54 | ||
55 | //! Sort two points in projection on vector (1,1) | |
56 | struct ComparatorOfIndexedVertexOfDelaun | |
57 | { | |
58 | public: | |
59 | ComparatorOfIndexedVertexOfDelaun (const Handle(BRepMesh_DataStructureOfDelaun)& theDS) | |
60 | : myStructure(theDS) {} | |
61 | ||
62 | bool operator() (Standard_Integer theLeft, Standard_Integer theRight) | |
63 | { | |
64 | const BRepMesh_Vertex& aLeft = myStructure->GetNode(theLeft); | |
65 | const BRepMesh_Vertex& aRight = myStructure->GetNode(theRight); | |
66 | return ComparatorOfVertexOfDelaun() (aLeft, aRight); | |
67 | } | |
68 | ||
69 | private: | |
70 | Handle(BRepMesh_DataStructureOfDelaun) myStructure; | |
71 | }; | |
71316196 | 72 | |
73 | inline void UpdateBndBox(const gp_XY& thePnt1, const gp_XY& thePnt2, Bnd_B2d& theBox) | |
74 | { | |
75 | theBox.Add( thePnt1 ); | |
76 | theBox.Add( thePnt2 ); | |
77 | theBox.Enlarge(Precision); | |
78 | } | |
fc9b36d6 | 79 | } // anonymous namespace |
0d88155b O |
80 | |
81 | //======================================================================= | |
82 | //function : BRepMesh_Delaun | |
90dc2e5b | 83 | //purpose : Creates the triangulation with an empty Mesh data structure |
0d88155b | 84 | //======================================================================= |
cbff1e55 | 85 | BRepMesh_Delaun::BRepMesh_Delaun(BRepMesh::Array1OfVertexOfDelaun& theVertices) |
86 | : myCircles (theVertices.Length(), new NCollection_IncAllocator( | |
87 | BRepMesh::MEMORY_BLOCK_SIZE_HUGE)) | |
0d88155b | 88 | { |
90dc2e5b | 89 | if ( theVertices.Length() > 2 ) |
90 | { | |
2caff0b3 | 91 | myMeshData = new BRepMesh_DataStructureOfDelaun( |
92 | new NCollection_IncAllocator(BRepMesh::MEMORY_BLOCK_SIZE_HUGE), | |
93 | theVertices.Length() ); | |
90dc2e5b | 94 | Init( theVertices ); |
0d88155b O |
95 | } |
96 | } | |
97 | ||
98 | //======================================================================= | |
99 | //function : BRepMesh_Delaun | |
90dc2e5b | 100 | //purpose : Creates the triangulation with and existent Mesh data structure |
0d88155b | 101 | //======================================================================= |
ceb418e1 | 102 | BRepMesh_Delaun::BRepMesh_Delaun( |
103 | const Handle( BRepMesh_DataStructureOfDelaun )& theOldMesh, | |
848fa7e3 | 104 | BRepMesh::Array1OfVertexOfDelaun& theVertices) |
cbff1e55 | 105 | : myMeshData( theOldMesh ), |
106 | myCircles ( theVertices.Length(), theOldMesh->Allocator() ) | |
0d88155b | 107 | { |
90dc2e5b | 108 | if ( theVertices.Length() > 2 ) |
109 | Init( theVertices ); | |
0d88155b O |
110 | } |
111 | ||
90dc2e5b | 112 | //======================================================================= |
113 | //function : BRepMesh_Delaun | |
114 | //purpose : Creates the triangulation with and existent Mesh data structure | |
115 | //======================================================================= | |
ceb418e1 | 116 | BRepMesh_Delaun::BRepMesh_Delaun( |
117 | const Handle( BRepMesh_DataStructureOfDelaun )& theOldMesh, | |
848fa7e3 | 118 | BRepMesh::Array1OfInteger& theVertexIndices) |
cbff1e55 | 119 | : myMeshData( theOldMesh ), |
120 | myCircles ( theVertexIndices.Length(), theOldMesh->Allocator() ) | |
90dc2e5b | 121 | { |
304c45c8 | 122 | if ( theVertexIndices.Length() > 2 ) |
90dc2e5b | 123 | { |
124 | Bnd_Box2d aBox; | |
304c45c8 | 125 | Standard_Integer anIndex = theVertexIndices.Lower(); |
126 | Standard_Integer anUpper = theVertexIndices.Upper(); | |
90dc2e5b | 127 | for ( ; anIndex <= anUpper; ++anIndex ) |
304c45c8 | 128 | aBox.Add( gp_Pnt2d( GetVertex( theVertexIndices( anIndex) ).Coord() ) ); |
90dc2e5b | 129 | |
304c45c8 | 130 | perform( aBox, theVertexIndices ); |
90dc2e5b | 131 | } |
132 | } | |
0d88155b O |
133 | |
134 | //======================================================================= | |
135 | //function : Init | |
90dc2e5b | 136 | //purpose : Initializes the triangulation with an Array of Vertex |
0d88155b | 137 | //======================================================================= |
848fa7e3 | 138 | void BRepMesh_Delaun::Init(BRepMesh::Array1OfVertexOfDelaun& theVertices) |
0d88155b | 139 | { |
90dc2e5b | 140 | Bnd_Box2d aBox; |
141 | Standard_Integer aLowerIdx = theVertices.Lower(); | |
142 | Standard_Integer anUpperIdx = theVertices.Upper(); | |
848fa7e3 | 143 | BRepMesh::Array1OfInteger aVertexIndexes( aLowerIdx, anUpperIdx ); |
90dc2e5b | 144 | |
145 | Standard_Integer anIndex = aLowerIdx; | |
146 | for ( ; anIndex <= anUpperIdx; ++anIndex ) | |
147 | { | |
148 | aBox.Add( gp_Pnt2d( theVertices( anIndex ).Coord() ) ); | |
149 | aVertexIndexes( anIndex ) = myMeshData->AddNode( theVertices( anIndex ) ); | |
0d88155b O |
150 | } |
151 | ||
304c45c8 | 152 | perform( aBox, aVertexIndexes ); |
0d88155b O |
153 | } |
154 | ||
90dc2e5b | 155 | //======================================================================= |
304c45c8 | 156 | //function : perform |
90dc2e5b | 157 | //purpose : Create super mesh and run triangulation procedure |
158 | //======================================================================= | |
848fa7e3 | 159 | void BRepMesh_Delaun::perform(Bnd_Box2d& theBndBox, |
160 | BRepMesh::Array1OfInteger& theVertexIndexes) | |
90dc2e5b | 161 | { |
304c45c8 | 162 | theBndBox.Enlarge( Precision ); |
163 | superMesh( theBndBox ); | |
90dc2e5b | 164 | |
fc9b36d6 | 165 | ComparatorOfIndexedVertexOfDelaun aCmp(myMeshData); |
166 | std::make_heap(theVertexIndexes.begin(), theVertexIndexes.end(), aCmp); | |
167 | std::sort_heap(theVertexIndexes.begin(), theVertexIndexes.end(), aCmp); | |
90dc2e5b | 168 | |
304c45c8 | 169 | compute( theVertexIndexes ); |
90dc2e5b | 170 | } |
0d88155b O |
171 | |
172 | //======================================================================= | |
304c45c8 | 173 | //function : superMesh |
90dc2e5b | 174 | //purpose : Build the super mesh |
0d88155b | 175 | //======================================================================= |
304c45c8 | 176 | void BRepMesh_Delaun::superMesh( const Bnd_Box2d& theBox ) |
0d88155b | 177 | { |
90dc2e5b | 178 | Standard_Real aMinX, aMinY, aMaxX, aMaxY; |
179 | theBox.Get( aMinX, aMinY, aMaxX, aMaxY ); | |
180 | Standard_Real aDeltaX = aMaxX - aMinX; | |
181 | Standard_Real aDeltaY = aMaxY - aMinY; | |
182 | ||
183 | Standard_Real aDeltaMin = Min( aDeltaX, aDeltaY ); | |
184 | Standard_Real aDeltaMax = Max( aDeltaX, aDeltaY ); | |
185 | Standard_Real aDelta = aDeltaX + aDeltaY; | |
186 | ||
187 | myCircles.SetMinMaxSize( gp_XY( aMinX, aMinY ), gp_XY( aMaxX, aMaxY ) ); | |
188 | ||
189 | Standard_Integer aScaler = 2; | |
190 | if ( myMeshData->NbNodes() > 100 ) | |
191 | aScaler = 5; | |
192 | else if( myMeshData->NbNodes() > 1000 ) | |
193 | aScaler = 7; | |
194 | ||
195 | myCircles.SetCellSize( aDeltaX / aScaler, | |
196 | aDeltaY / aScaler ); | |
0d88155b | 197 | |
304c45c8 | 198 | mySupVert[0] = myMeshData->AddNode( |
90dc2e5b | 199 | BRepMesh_Vertex( ( aMinX + aMaxX ) / 2, aMaxY + aDeltaMax, BRepMesh_Free ) ); |
200 | ||
304c45c8 | 201 | mySupVert[1] = myMeshData->AddNode( |
90dc2e5b | 202 | BRepMesh_Vertex( aMinX - aDelta, aMinY - aDeltaMin, BRepMesh_Free ) ); |
203 | ||
304c45c8 | 204 | mySupVert[2] = myMeshData->AddNode( |
90dc2e5b | 205 | BRepMesh_Vertex( aMaxX + aDelta, aMinY - aDeltaMin, BRepMesh_Free ) ); |
206 | ||
fc9b36d6 | 207 | Standard_Integer e[3]; |
208 | Standard_Boolean o[3]; | |
304c45c8 | 209 | for (Standard_Integer aNodeId = 0; aNodeId < 3; ++aNodeId) |
210 | { | |
211 | Standard_Integer aFirstNode = aNodeId; | |
212 | Standard_Integer aLastNode = (aNodeId + 1) % 3; | |
fc9b36d6 | 213 | Standard_Integer aLinkIndex = myMeshData->AddLink( BRepMesh_Edge( |
304c45c8 | 214 | mySupVert[aFirstNode], mySupVert[aLastNode], BRepMesh_Free ) ); |
fc9b36d6 | 215 | |
216 | e[aNodeId] = Abs(aLinkIndex); | |
217 | o[aNodeId] = (aLinkIndex > 0); | |
304c45c8 | 218 | } |
90dc2e5b | 219 | |
fc9b36d6 | 220 | mySupTrian = BRepMesh_Triangle(e, o, BRepMesh_Free); |
2b59653e E |
221 | } |
222 | ||
223 | //======================================================================= | |
304c45c8 | 224 | //function : deleteTriangle |
225 | //purpose : Deletes the triangle with the given index and adds the free | |
226 | // edges into the map. | |
227 | // When an edge is suppressed more than one time it is destroyed. | |
2b59653e | 228 | //======================================================================= |
848fa7e3 | 229 | void BRepMesh_Delaun::deleteTriangle(const Standard_Integer theIndex, |
230 | BRepMesh::MapOfIntegerInteger& theLoopEdges ) | |
2b59653e | 231 | { |
90dc2e5b | 232 | myCircles.Delete( theIndex ); |
0d88155b | 233 | |
90dc2e5b | 234 | Standard_Integer e[3]; |
235 | Standard_Boolean o[3]; | |
fc9b36d6 | 236 | GetTriangle( theIndex ).Edges( e, o ); |
304c45c8 | 237 | |
90dc2e5b | 238 | myMeshData->RemoveElement( theIndex ); |
0d88155b | 239 | |
90dc2e5b | 240 | for ( Standard_Integer i = 0; i < 3; ++i ) |
241 | { | |
242 | if ( !theLoopEdges.Bind( e[i], o[i] ) ) | |
243 | { | |
244 | theLoopEdges.UnBind( e[i] ); | |
245 | myMeshData->RemoveLink( e[i] ); | |
246 | } | |
247 | } | |
0d88155b O |
248 | } |
249 | ||
250 | //======================================================================= | |
304c45c8 | 251 | //function : compute |
90dc2e5b | 252 | //purpose : Computes the triangulation and add the vertices edges and |
253 | // triangles to the Mesh data structure | |
0d88155b | 254 | //======================================================================= |
848fa7e3 | 255 | void BRepMesh_Delaun::compute(BRepMesh::Array1OfInteger& theVertexIndexes) |
0d88155b O |
256 | { |
257 | // Insertion of edges of super triangles in the list of free edges: | |
2caff0b3 | 258 | BRepMesh::MapOfIntegerInteger aLoopEdges(10, myMeshData->Allocator()); |
90dc2e5b | 259 | Standard_Integer e[3]; |
260 | Standard_Boolean o[3]; | |
fc9b36d6 | 261 | mySupTrian.Edges( e, o ); |
90dc2e5b | 262 | |
263 | aLoopEdges.Bind( e[0], Standard_True ); | |
264 | aLoopEdges.Bind( e[1], Standard_True ); | |
265 | aLoopEdges.Bind( e[2], Standard_True ); | |
266 | ||
267 | if ( theVertexIndexes.Length() > 0 ) | |
268 | { | |
269 | // Creation of 3 trianglers with the first node and the edges of the super triangle: | |
270 | Standard_Integer anVertexIdx = theVertexIndexes.Lower(); | |
304c45c8 | 271 | createTriangles( theVertexIndexes( anVertexIdx ), aLoopEdges ); |
0d88155b | 272 | |
90dc2e5b | 273 | // Add other nodes to the mesh |
304c45c8 | 274 | createTrianglesOnNewVertices( theVertexIndexes ); |
0d88155b O |
275 | } |
276 | ||
90dc2e5b | 277 | // Destruction of triangles containing a top of the super triangle |
278 | BRepMesh_SelectorOfDataStructureOfDelaun aSelector( myMeshData ); | |
304c45c8 | 279 | for (Standard_Integer aSupVertId = 0; aSupVertId < 3; ++aSupVertId) |
280 | aSelector.NeighboursOfNode( mySupVert[aSupVertId] ); | |
90dc2e5b | 281 | |
282 | aLoopEdges.Clear(); | |
848fa7e3 | 283 | BRepMesh::MapOfInteger::Iterator aFreeTriangles( aSelector.Elements() ); |
90dc2e5b | 284 | for ( ; aFreeTriangles.More(); aFreeTriangles.Next() ) |
304c45c8 | 285 | deleteTriangle( aFreeTriangles.Key(), aLoopEdges ); |
90dc2e5b | 286 | |
287 | // All edges that remain free are removed from aLoopEdges; | |
288 | // only the boundary edges of the triangulation remain there | |
848fa7e3 | 289 | BRepMesh::MapOfIntegerInteger::Iterator aFreeEdges( aLoopEdges ); |
90dc2e5b | 290 | for ( ; aFreeEdges.More(); aFreeEdges.Next() ) |
291 | { | |
fc9b36d6 | 292 | if ( myMeshData->ElementsConnectedTo( aFreeEdges.Key() ).IsEmpty() ) |
90dc2e5b | 293 | myMeshData->RemoveLink( aFreeEdges.Key() ); |
0d88155b O |
294 | } |
295 | ||
90dc2e5b | 296 | // The tops of the super triangle are destroyed |
304c45c8 | 297 | for (Standard_Integer aSupVertId = 0; aSupVertId < 3; ++aSupVertId) |
298 | myMeshData->RemoveNode( mySupVert[aSupVertId] ); | |
0d88155b O |
299 | } |
300 | ||
0d88155b | 301 | //======================================================================= |
304c45c8 | 302 | //function : createTriangles |
90dc2e5b | 303 | //purpose : Creates the triangles beetween the node and the polyline. |
0d88155b | 304 | //======================================================================= |
848fa7e3 | 305 | void BRepMesh_Delaun::createTriangles(const Standard_Integer theVertexIndex, |
306 | BRepMesh::MapOfIntegerInteger& thePoly) | |
0d88155b | 307 | { |
848fa7e3 | 308 | BRepMesh::ListOfInteger aLoopEdges, anExternalEdges; |
90dc2e5b | 309 | const gp_XY& aVertexCoord = myMeshData->GetNode( theVertexIndex ).Coord(); |
310 | ||
848fa7e3 | 311 | BRepMesh::MapOfIntegerInteger::Iterator anEdges( thePoly ); |
90dc2e5b | 312 | for ( ; anEdges.More(); anEdges.Next() ) |
2b59653e | 313 | { |
304c45c8 | 314 | Standard_Integer anEdgeId = anEdges.Key(); |
315 | const BRepMesh_Edge& anEdge = GetEdge( anEdgeId ); | |
316 | ||
317 | Standard_Boolean isPositive = (Standard_Boolean)thePoly( anEdgeId ); | |
318 | ||
319 | Standard_Integer aNodes[3]; | |
320 | if ( isPositive ) | |
90dc2e5b | 321 | { |
304c45c8 | 322 | aNodes[0] = anEdge.FirstNode(); |
323 | aNodes[2] = anEdge.LastNode(); | |
0d88155b | 324 | } |
304c45c8 | 325 | else |
326 | { | |
327 | aNodes[0] = anEdge.LastNode(); | |
328 | aNodes[2] = anEdge.FirstNode(); | |
329 | } | |
330 | aNodes[1] = theVertexIndex; | |
0d88155b | 331 | |
304c45c8 | 332 | const BRepMesh_Vertex& aFirstVertex = GetVertex( aNodes[0] ); |
333 | const BRepMesh_Vertex& aLastVertex = GetVertex( aNodes[2] ); | |
0d88155b | 334 | |
304c45c8 | 335 | gp_XY anEdgeDir( aLastVertex.Coord() - aFirstVertex.Coord() ); |
336 | Standard_Real anEdgeLen = anEdgeDir.Modulus(); | |
337 | if ( anEdgeLen < Precision ) | |
338 | continue; | |
0d88155b | 339 | |
304c45c8 | 340 | anEdgeDir.SetCoord( anEdgeDir.X() / anEdgeLen, |
341 | anEdgeDir.Y() / anEdgeLen ); | |
342 | ||
343 | gp_XY aFirstLinkDir( aFirstVertex.Coord() - aVertexCoord ); | |
344 | gp_XY aLastLinkDir ( aVertexCoord - aLastVertex.Coord() ); | |
345 | ||
346 | Standard_Real aDist12 = aFirstLinkDir ^ anEdgeDir; | |
347 | Standard_Real aDist23 = anEdgeDir ^ aLastLinkDir; | |
348 | if ( Abs( aDist12 ) < Precision || | |
349 | Abs( aDist23 ) < Precision ) | |
90dc2e5b | 350 | { |
304c45c8 | 351 | continue; |
2b59653e | 352 | } |
0d88155b | 353 | |
304c45c8 | 354 | BRepMesh_Edge aFirstLink( aNodes[1], aNodes[0], BRepMesh_Free ); |
355 | BRepMesh_Edge aLastLink ( aNodes[2], aNodes[1], BRepMesh_Free ); | |
356 | ||
357 | Standard_Integer anEdgesInfo[3] = { | |
358 | myMeshData->AddLink( aFirstLink ), | |
359 | isPositive ? anEdgeId : -anEdgeId, | |
360 | myMeshData->AddLink( aLastLink ) }; | |
361 | ||
ceb418e1 | 362 | Standard_Boolean isSensOK = (aDist12 > 0. && aDist23 > 0.); |
363 | if (isSensOK) | |
2b59653e | 364 | { |
51740958 | 365 | Standard_Integer anEdgeIds[3]; |
304c45c8 | 366 | Standard_Boolean anEdgesOri[3]; |
367 | for ( Standard_Integer aTriLinkIt = 0; aTriLinkIt < 3; ++aTriLinkIt ) | |
90dc2e5b | 368 | { |
304c45c8 | 369 | const Standard_Integer& anEdgeInfo = anEdgesInfo[aTriLinkIt]; |
51740958 | 370 | anEdgeIds[aTriLinkIt] = Abs( anEdgeInfo ); |
304c45c8 | 371 | anEdgesOri[aTriLinkIt] = anEdgeInfo > 0; |
90dc2e5b | 372 | } |
304c45c8 | 373 | |
51740958 | 374 | addTriangle(anEdgeIds, anEdgesOri, aNodes ); |
304c45c8 | 375 | } |
376 | else | |
377 | { | |
378 | if ( isPositive ) | |
379 | aLoopEdges.Append( anEdges.Key() ); | |
90dc2e5b | 380 | else |
304c45c8 | 381 | aLoopEdges.Append( -anEdges.Key() ); |
382 | ||
383 | if ( aFirstLinkDir.SquareModulus() > aLastLinkDir.SquareModulus() ) | |
384 | anExternalEdges.Append( Abs( anEdgesInfo[0] ) ); | |
385 | else | |
386 | anExternalEdges.Append( Abs( anEdgesInfo[2] ) ); | |
2b59653e | 387 | } |
90dc2e5b | 388 | } |
389 | ||
390 | thePoly.Clear(); | |
391 | while ( !anExternalEdges.IsEmpty() ) | |
392 | { | |
393 | const BRepMesh_PairOfIndex& aPair = | |
fc9b36d6 | 394 | myMeshData->ElementsConnectedTo( Abs( anExternalEdges.First() ) ); |
90dc2e5b | 395 | |
396 | ||
397 | if ( !aPair.IsEmpty() ) | |
304c45c8 | 398 | deleteTriangle( aPair.FirstIndex(), thePoly ); |
90dc2e5b | 399 | |
400 | anExternalEdges.RemoveFirst(); | |
401 | } | |
402 | ||
403 | for ( anEdges.Initialize( thePoly ); anEdges.More(); anEdges.Next() ) | |
404 | { | |
fc9b36d6 | 405 | if ( myMeshData->ElementsConnectedTo( anEdges.Key() ).IsEmpty() ) |
90dc2e5b | 406 | myMeshData->RemoveLink( anEdges.Key() ); |
407 | } | |
408 | ||
409 | while ( !aLoopEdges.IsEmpty() ) | |
410 | { | |
411 | const BRepMesh_Edge& anEdge = GetEdge( Abs( aLoopEdges.First() ) ); | |
412 | if ( anEdge.Movability() != BRepMesh_Deleted ) | |
2b59653e | 413 | { |
90dc2e5b | 414 | Standard_Integer anEdgeIdx = aLoopEdges.First(); |
304c45c8 | 415 | meshLeftPolygonOf( Abs( anEdgeIdx ), ( anEdgeIdx > 0 ) ); |
2b59653e | 416 | } |
90dc2e5b | 417 | |
418 | aLoopEdges.RemoveFirst(); | |
419 | } | |
420 | } | |
421 | ||
422 | //======================================================================= | |
304c45c8 | 423 | //function : createTrianglesOnNewVertices |
90dc2e5b | 424 | //purpose : Creation of triangles from the new nodes |
425 | //======================================================================= | |
848fa7e3 | 426 | void BRepMesh_Delaun::createTrianglesOnNewVertices( |
427 | BRepMesh::Array1OfInteger& theVertexIndexes) | |
90dc2e5b | 428 | { |
2caff0b3 | 429 | Handle(NCollection_IncAllocator) aAllocator = |
430 | new NCollection_IncAllocator(BRepMesh::MEMORY_BLOCK_SIZE_HUGE); | |
431 | ||
432 | BRepMesh::MapOfIntegerInteger aLoopEdges(10, aAllocator); | |
90dc2e5b | 433 | |
71316196 | 434 | Standard_Real aTolU, aTolV; |
435 | myMeshData->Data()->GetTolerance(aTolU, aTolV); | |
436 | const Standard_Real aSqTol = aTolU * aTolU + aTolV * aTolV; | |
437 | ||
90dc2e5b | 438 | // Insertion of nodes : |
439 | Standard_Boolean isModify = Standard_True; | |
440 | ||
441 | Standard_Integer anIndex = theVertexIndexes.Lower(); | |
442 | Standard_Integer anUpper = theVertexIndexes.Upper(); | |
443 | for( ; anIndex <= anUpper; ++anIndex ) | |
444 | { | |
445 | aLoopEdges.Clear(); | |
2caff0b3 | 446 | aAllocator->Reset(Standard_False); |
2b59653e | 447 | |
90dc2e5b | 448 | Standard_Integer aVertexIdx = theVertexIndexes( anIndex ); |
449 | const BRepMesh_Vertex& aVertex = GetVertex( aVertexIdx ); | |
0d88155b | 450 | |
90dc2e5b | 451 | // Iterator in the list of indexes of circles containing the node |
848fa7e3 | 452 | BRepMesh::ListOfInteger& aCirclesList = myCircles.Select( aVertex.Coord() ); |
90dc2e5b | 453 | |
454 | Standard_Integer onEgdeId = 0, aTriangleId = 0; | |
848fa7e3 | 455 | BRepMesh::ListOfInteger::Iterator aCircleIt( aCirclesList ); |
90dc2e5b | 456 | for ( ; aCircleIt.More(); aCircleIt.Next() ) |
457 | { | |
458 | // To add a node in the mesh it is necessary to check conditions: | |
459 | // - the node should be within the boundaries of the mesh and so in an existing triangle | |
460 | // - all adjacent triangles should belong to a component connected with this triangle | |
71316196 | 461 | if ( Contains( aCircleIt.Value(), aVertex, aSqTol, onEgdeId ) ) |
90dc2e5b | 462 | { |
71316196 | 463 | if (onEgdeId != 0 && GetEdge(onEgdeId).Movability() != BRepMesh_Free) |
90dc2e5b | 464 | { |
71316196 | 465 | // We can skip free vertex too close to the frontier edge. |
466 | if (aVertex.Movability() == BRepMesh_Free) | |
467 | continue; | |
468 | ||
469 | // However, we should add vertex that have neighboring frontier edges. | |
0d88155b | 470 | } |
71316196 | 471 | |
472 | // Remove triangle even if it contains frontier edge in order | |
473 | // to prevent appearance of incorrect configurations like free | |
474 | // edge glued with frontier #26407 | |
475 | aTriangleId = aCircleIt.Value(); | |
476 | aCirclesList.Remove( aCircleIt ); | |
477 | break; | |
0d88155b | 478 | } |
90dc2e5b | 479 | } |
0d88155b | 480 | |
90dc2e5b | 481 | if ( aTriangleId > 0 ) |
482 | { | |
304c45c8 | 483 | deleteTriangle( aTriangleId, aLoopEdges ); |
0d88155b | 484 | |
90dc2e5b | 485 | isModify = Standard_True; |
486 | while ( isModify && !aCirclesList.IsEmpty() ) | |
487 | { | |
488 | isModify = Standard_False; | |
848fa7e3 | 489 | BRepMesh::ListOfInteger::Iterator aCircleIt1( aCirclesList ); |
90dc2e5b | 490 | for ( ; aCircleIt1.More(); aCircleIt1.Next() ) |
491 | { | |
492 | Standard_Integer e[3]; | |
493 | Standard_Boolean o[3]; | |
fc9b36d6 | 494 | GetTriangle( aCircleIt1.Value() ).Edges( e, o ); |
90dc2e5b | 495 | |
496 | if ( aLoopEdges.IsBound( e[0] ) || | |
497 | aLoopEdges.IsBound( e[1] ) || | |
498 | aLoopEdges.IsBound( e[2] ) ) | |
499 | { | |
500 | isModify = Standard_True; | |
304c45c8 | 501 | deleteTriangle( aCircleIt1.Value(), aLoopEdges ); |
90dc2e5b | 502 | aCirclesList.Remove( aCircleIt1 ); |
503 | break; | |
504 | } | |
505 | } | |
2b59653e | 506 | } |
0d88155b | 507 | |
90dc2e5b | 508 | // Creation of triangles with the current node and free edges |
509 | // and removal of these edges from the list of free edges | |
304c45c8 | 510 | createTriangles( aVertexIdx, aLoopEdges ); |
2b59653e | 511 | } |
0d88155b | 512 | } |
90dc2e5b | 513 | // Check that internal edges are not crossed by triangles |
848fa7e3 | 514 | BRepMesh::HMapOfInteger anInternalEdges = InternalEdges(); |
0d88155b | 515 | |
90dc2e5b | 516 | // Destruction of triancles intersecting internal edges |
517 | // and their replacement by makeshift triangles | |
848fa7e3 | 518 | BRepMesh::MapOfInteger::Iterator anInernalEdgesIt( *anInternalEdges ); |
90dc2e5b | 519 | for ( ; anInernalEdgesIt.More(); anInernalEdgesIt.Next() ) |
520 | { | |
521 | Standard_Integer aNbC; | |
fc9b36d6 | 522 | aNbC = myMeshData->ElementsConnectedTo( anInernalEdgesIt.Key() ).Extent(); |
90dc2e5b | 523 | if ( aNbC == 0 ) |
524 | { | |
304c45c8 | 525 | meshLeftPolygonOf( anInernalEdgesIt.Key(), Standard_True ); |
526 | meshLeftPolygonOf( anInernalEdgesIt.Key(), Standard_False ); | |
0d88155b O |
527 | } |
528 | } | |
529 | ||
90dc2e5b | 530 | // Adjustment of meshes to boundary edges |
304c45c8 | 531 | frontierAdjust(); |
0d88155b O |
532 | } |
533 | ||
0d88155b | 534 | //======================================================================= |
304c45c8 | 535 | //function : isBoundToFrontier |
536 | //purpose : Goes through the neighbour triangles around the given node | |
537 | // started from the given link, returns TRUE if some triangle | |
538 | // has a bounding frontier edge or FALSE elsewhere. | |
539 | // Stop link is used to prevent cycles. | |
540 | // Previous element Id is used to identify next neighbor element. | |
0d88155b | 541 | //======================================================================= |
304c45c8 | 542 | Standard_Boolean BRepMesh_Delaun::isBoundToFrontier( |
543 | const Standard_Integer theRefNodeId, | |
544 | const Standard_Integer theRefLinkId, | |
545 | const Standard_Integer theStopLinkId, | |
546 | const Standard_Integer thePrevElementId) | |
0d88155b | 547 | { |
304c45c8 | 548 | const BRepMesh_PairOfIndex& aPair = |
fc9b36d6 | 549 | myMeshData->ElementsConnectedTo( theRefLinkId ); |
304c45c8 | 550 | if ( aPair.IsEmpty() ) |
551 | return Standard_False; | |
0d88155b | 552 | |
304c45c8 | 553 | Standard_Integer aNbElements = aPair.Extent(); |
554 | for ( Standard_Integer anElemIt = 1; anElemIt <= aNbElements; ++anElemIt ) | |
90dc2e5b | 555 | { |
304c45c8 | 556 | const Standard_Integer aTriId = aPair.Index(anElemIt); |
557 | if ( aTriId < 0 || aTriId == thePrevElementId ) | |
558 | continue; | |
559 | ||
560 | Standard_Integer anEdges[3]; | |
561 | Standard_Boolean anEdgesOri[3]; | |
fc9b36d6 | 562 | GetTriangle( aTriId ).Edges( anEdges, anEdgesOri ); |
304c45c8 | 563 | |
564 | for ( Standard_Integer anEdgeIt = 0; anEdgeIt < 3; ++anEdgeIt ) | |
565 | { | |
566 | const Standard_Integer anEdgeId = anEdges[anEdgeIt]; | |
567 | if ( anEdgeId == theRefLinkId ) | |
568 | continue; | |
569 | ||
570 | if ( anEdgeId == theStopLinkId ) | |
571 | return Standard_False; | |
572 | ||
573 | const BRepMesh_Edge& anEdge = GetEdge( anEdgeId ); | |
574 | if ( anEdge.FirstNode() != theRefNodeId && | |
575 | anEdge.LastNode() != theRefNodeId ) | |
576 | { | |
577 | continue; | |
578 | } | |
579 | ||
580 | if ( anEdge.Movability() != BRepMesh_Free ) | |
581 | return Standard_True; | |
0d88155b | 582 | |
304c45c8 | 583 | return isBoundToFrontier( theRefNodeId, anEdgeId, theStopLinkId, aTriId ); |
584 | } | |
585 | } | |
586 | ||
587 | return Standard_False; | |
588 | } | |
589 | ||
590 | //======================================================================= | |
591 | //function : cleanupMesh | |
592 | //purpose : Cleanup mesh from the free triangles | |
593 | //======================================================================= | |
594 | void BRepMesh_Delaun::cleanupMesh() | |
595 | { | |
2caff0b3 | 596 | Handle(NCollection_IncAllocator) aAllocator = |
597 | new NCollection_IncAllocator(BRepMesh::MEMORY_BLOCK_SIZE_HUGE); | |
598 | ||
498ce76b | 599 | for(;;) |
304c45c8 | 600 | { |
2caff0b3 | 601 | BRepMesh::MapOfIntegerInteger aLoopEdges(10, aAllocator); |
602 | BRepMesh::MapOfInteger aDelTriangles(10, aAllocator); | |
304c45c8 | 603 | |
848fa7e3 | 604 | BRepMesh::HMapOfInteger aFreeEdges = FreeEdges(); |
605 | BRepMesh::MapOfInteger::Iterator aFreeEdgesIt( *aFreeEdges ); | |
90dc2e5b | 606 | for ( ; aFreeEdgesIt.More(); aFreeEdgesIt.Next() ) |
607 | { | |
304c45c8 | 608 | const Standard_Integer& aFreeEdgeId = aFreeEdgesIt.Key(); |
609 | const BRepMesh_Edge& anEdge = GetEdge( aFreeEdgeId ); | |
610 | if ( anEdge.Movability() == BRepMesh_Frontier ) | |
611 | continue; | |
612 | ||
613 | const BRepMesh_PairOfIndex& aPair = | |
fc9b36d6 | 614 | myMeshData->ElementsConnectedTo( aFreeEdgeId ); |
304c45c8 | 615 | if ( aPair.IsEmpty() ) |
90dc2e5b | 616 | { |
304c45c8 | 617 | aLoopEdges.Bind( aFreeEdgeId, Standard_True ); |
618 | continue; | |
619 | } | |
620 | ||
621 | Standard_Integer aTriId = aPair.FirstIndex(); | |
622 | ||
623 | // Check that the connected triangle is not surrounded by another triangles | |
624 | Standard_Integer anEdges[3]; | |
625 | Standard_Boolean anEdgesOri[3]; | |
fc9b36d6 | 626 | GetTriangle( aTriId ).Edges( anEdges, anEdgesOri ); |
304c45c8 | 627 | |
628 | Standard_Boolean isCanNotBeRemoved = Standard_True; | |
629 | for ( Standard_Integer aCurEdgeIdx = 0; aCurEdgeIdx < 3; ++aCurEdgeIdx ) | |
630 | { | |
631 | if ( anEdges[aCurEdgeIdx] != aFreeEdgeId ) | |
632 | continue; | |
633 | ||
634 | for ( Standard_Integer anOtherEdgeIt = 1; anOtherEdgeIt <= 2; ++anOtherEdgeIt ) | |
90dc2e5b | 635 | { |
304c45c8 | 636 | Standard_Integer anOtherEdgeId = ( aCurEdgeIdx + anOtherEdgeIt ) % 3; |
637 | const BRepMesh_PairOfIndex& anOtherEdgePair = | |
fc9b36d6 | 638 | myMeshData->ElementsConnectedTo( anEdges[anOtherEdgeId] ); |
304c45c8 | 639 | |
640 | if ( anOtherEdgePair.Extent() < 2 ) | |
90dc2e5b | 641 | { |
304c45c8 | 642 | isCanNotBeRemoved = Standard_False; |
643 | break; | |
0d88155b O |
644 | } |
645 | } | |
304c45c8 | 646 | |
647 | break; | |
648 | } | |
649 | ||
650 | if ( isCanNotBeRemoved ) | |
651 | continue; | |
652 | ||
653 | Standard_Boolean isConnected[2] = { Standard_False, Standard_False }; | |
654 | for ( Standard_Integer aLinkNodeIt = 0; aLinkNodeIt < 2; ++aLinkNodeIt ) | |
655 | { | |
656 | isConnected[aLinkNodeIt] = isBoundToFrontier( ( aLinkNodeIt == 0 ) ? | |
657 | anEdge.FirstNode() : anEdge.LastNode(), | |
658 | aFreeEdgeId, aFreeEdgeId, -1); | |
0d88155b | 659 | } |
304c45c8 | 660 | |
661 | if ( !isConnected[0] || !isConnected[1] ) | |
662 | aDelTriangles.Add( aTriId ); | |
0d88155b O |
663 | } |
664 | ||
90dc2e5b | 665 | // Destruction of triangles : |
666 | Standard_Integer aDeletedTrianglesNb = 0; | |
848fa7e3 | 667 | BRepMesh::MapOfInteger::Iterator aDelTrianglesIt( aDelTriangles ); |
304c45c8 | 668 | for ( ; aDelTrianglesIt.More(); aDelTrianglesIt.Next() ) |
90dc2e5b | 669 | { |
304c45c8 | 670 | deleteTriangle( aDelTrianglesIt.Key(), aLoopEdges ); |
90dc2e5b | 671 | aDeletedTrianglesNb++; |
672 | } | |
0d88155b | 673 | |
90dc2e5b | 674 | // Destruction of remaining hanging edges |
848fa7e3 | 675 | BRepMesh::MapOfIntegerInteger::Iterator aLoopEdgesIt( aLoopEdges ); |
90dc2e5b | 676 | for ( ; aLoopEdgesIt.More(); aLoopEdgesIt.Next() ) |
677 | { | |
fc9b36d6 | 678 | if ( myMeshData->ElementsConnectedTo( aLoopEdgesIt.Key() ).IsEmpty() ) |
90dc2e5b | 679 | myMeshData->RemoveLink( aLoopEdgesIt.Key() ); |
680 | } | |
0d88155b | 681 | |
2caff0b3 | 682 | aAllocator->Reset(Standard_False); |
90dc2e5b | 683 | if ( aDeletedTrianglesNb == 0 ) |
684 | break; | |
685 | } | |
686 | } | |
0d88155b | 687 | |
90dc2e5b | 688 | //======================================================================= |
304c45c8 | 689 | //function : frontierAdjust |
90dc2e5b | 690 | //purpose : Adjust the mesh on the frontier |
691 | //======================================================================= | |
304c45c8 | 692 | void BRepMesh_Delaun::frontierAdjust() |
90dc2e5b | 693 | { |
848fa7e3 | 694 | BRepMesh::HMapOfInteger aFrontier = Frontier(); |
2caff0b3 | 695 | |
696 | Handle(NCollection_IncAllocator) aAllocator = | |
697 | new NCollection_IncAllocator(BRepMesh::MEMORY_BLOCK_SIZE_HUGE); | |
698 | ||
699 | BRepMesh::VectorOfInteger aFailedFrontiers(256, aAllocator); | |
700 | BRepMesh::MapOfIntegerInteger aLoopEdges(10, aAllocator); | |
701 | BRepMesh::HMapOfInteger aIntFrontierEdges = | |
702 | new BRepMesh::MapOfInteger(10, aAllocator); | |
703 | ||
90dc2e5b | 704 | for ( Standard_Integer aPass = 1; aPass <= 2; ++aPass ) |
705 | { | |
304c45c8 | 706 | // 1 pass): find external triangles on boundary edges; |
707 | // 2 pass): find external triangles on boundary edges appeared | |
708 | // during triangles replacement. | |
90dc2e5b | 709 | |
848fa7e3 | 710 | BRepMesh::MapOfInteger::Iterator aFrontierIt( *aFrontier ); |
90dc2e5b | 711 | for ( ; aFrontierIt.More(); aFrontierIt.Next() ) |
712 | { | |
304c45c8 | 713 | Standard_Integer aFrontierId = aFrontierIt.Key(); |
fc9b36d6 | 714 | const BRepMesh_PairOfIndex& aPair = myMeshData->ElementsConnectedTo( aFrontierId ); |
304c45c8 | 715 | Standard_Integer aNbElem = aPair.Extent(); |
716 | for( Standard_Integer aElemIt = 1; aElemIt <= aNbElem; ++aElemIt ) | |
90dc2e5b | 717 | { |
304c45c8 | 718 | const Standard_Integer aPriorElemId = aPair.Index( aElemIt ); |
90dc2e5b | 719 | if( aPriorElemId < 0 ) |
304c45c8 | 720 | continue; |
90dc2e5b | 721 | |
722 | Standard_Integer e[3]; | |
723 | Standard_Boolean o[3]; | |
fc9b36d6 | 724 | GetTriangle( aPriorElemId ).Edges( e, o ); |
90dc2e5b | 725 | |
304c45c8 | 726 | Standard_Boolean isTriangleFound = Standard_False; |
90dc2e5b | 727 | for ( Standard_Integer n = 0; n < 3; ++n ) |
728 | { | |
304c45c8 | 729 | if ( aFrontierId == e[n] && !o[n] ) |
90dc2e5b | 730 | { |
304c45c8 | 731 | // Destruction of external triangles on boundary edges |
732 | isTriangleFound = Standard_True; | |
733 | deleteTriangle( aPriorElemId, aLoopEdges ); | |
90dc2e5b | 734 | break; |
0d88155b O |
735 | } |
736 | } | |
304c45c8 | 737 | |
738 | if ( isTriangleFound ) | |
739 | break; | |
0d88155b O |
740 | } |
741 | } | |
90dc2e5b | 742 | |
90dc2e5b | 743 | // destrucrion of remaining hanging edges : |
848fa7e3 | 744 | BRepMesh::MapOfIntegerInteger::Iterator aLoopEdgesIt( aLoopEdges ); |
90dc2e5b | 745 | for ( ; aLoopEdgesIt.More(); aLoopEdgesIt.Next() ) |
746 | { | |
304c45c8 | 747 | Standard_Integer aLoopEdgeId = aLoopEdgesIt.Key(); |
fc9b36d6 | 748 | if (myMeshData->ElementsConnectedTo( aLoopEdgeId ).IsEmpty() ) |
304c45c8 | 749 | myMeshData->RemoveLink( aLoopEdgeId ); |
0d88155b | 750 | } |
90dc2e5b | 751 | |
752 | // destruction of triangles crossing the boundary edges and | |
753 | // their replacement by makeshift triangles | |
304c45c8 | 754 | for ( aFrontierIt.Reset(); aFrontierIt.More(); aFrontierIt.Next() ) |
90dc2e5b | 755 | { |
304c45c8 | 756 | Standard_Integer aFrontierId = aFrontierIt.Key(); |
fc9b36d6 | 757 | if ( !myMeshData->ElementsConnectedTo( aFrontierId ).IsEmpty() ) |
304c45c8 | 758 | continue; |
0d88155b | 759 | |
304c45c8 | 760 | Standard_Boolean isSuccess = |
761 | meshLeftPolygonOf( aFrontierId, Standard_True, aIntFrontierEdges ); | |
762 | ||
763 | if ( aPass == 2 && !isSuccess ) | |
764 | aFailedFrontiers.Append( aFrontierId ); | |
90dc2e5b | 765 | } |
304c45c8 | 766 | } |
0d88155b | 767 | |
304c45c8 | 768 | cleanupMesh(); |
769 | ||
770 | // When the mesh has been cleaned up, try to process frontier edges | |
771 | // once again to fill the possible gaps that might be occured in case of "saw" - | |
772 | // situation when frontier edge has a triangle at a right side, but its free | |
773 | // links cross another frontieres and meshLeftPolygonOf itself can't collect | |
774 | // a closed polygon. | |
848fa7e3 | 775 | BRepMesh::VectorOfInteger::Iterator aFailedFrontiersIt( aFailedFrontiers ); |
304c45c8 | 776 | for ( ; aFailedFrontiersIt.More(); aFailedFrontiersIt.Next() ) |
777 | { | |
778 | Standard_Integer aFrontierId = aFailedFrontiersIt.Value(); | |
fc9b36d6 | 779 | if ( !myMeshData->ElementsConnectedTo( aFrontierId ).IsEmpty() ) |
90dc2e5b | 780 | continue; |
0d88155b | 781 | |
304c45c8 | 782 | meshLeftPolygonOf( aFrontierId, Standard_True, aIntFrontierEdges ); |
90dc2e5b | 783 | } |
0d88155b O |
784 | } |
785 | ||
0d88155b | 786 | //======================================================================= |
304c45c8 | 787 | //function : fillBndBox |
788 | //purpose : Add boundig box for edge defined by start & end point to | |
789 | // the given vector of bounding boxes for triangulation edges | |
790 | //======================================================================= | |
848fa7e3 | 791 | void BRepMesh_Delaun::fillBndBox(BRepMesh::SequenceOfBndB2d& theBoxes, |
792 | const BRepMesh_Vertex& theV1, | |
793 | const BRepMesh_Vertex& theV2) | |
304c45c8 | 794 | { |
71316196 | 795 | Bnd_B2d aBox; |
796 | UpdateBndBox(theV1.Coord(), theV2.Coord(), aBox); | |
304c45c8 | 797 | theBoxes.Append( aBox ); |
798 | } | |
799 | ||
800 | //======================================================================= | |
801 | //function : meshLeftPolygonOf | |
802 | //purpose : Collect the polygon at the left of the given edge (material side) | |
0d88155b | 803 | //======================================================================= |
304c45c8 | 804 | Standard_Boolean BRepMesh_Delaun::meshLeftPolygonOf( |
848fa7e3 | 805 | const Standard_Integer theStartEdgeId, |
806 | const Standard_Boolean isForward, | |
807 | BRepMesh::HMapOfInteger theSkipped ) | |
0d88155b | 808 | { |
304c45c8 | 809 | if ( !theSkipped.IsNull() && theSkipped->Contains( theStartEdgeId ) ) |
810 | return Standard_True; | |
811 | ||
812 | const BRepMesh_Edge& aRefEdge = GetEdge( theStartEdgeId ); | |
0d88155b | 813 | |
848fa7e3 | 814 | BRepMesh::SequenceOfInteger aPolygon; |
304c45c8 | 815 | Standard_Integer aStartNode, aPivotNode; |
816 | if ( isForward ) | |
90dc2e5b | 817 | { |
304c45c8 | 818 | aPolygon.Append( theStartEdgeId ); |
819 | aStartNode = aRefEdge.FirstNode(); | |
820 | aPivotNode = aRefEdge.LastNode(); | |
821 | } | |
822 | else | |
823 | { | |
824 | aPolygon.Append( -theStartEdgeId ); | |
825 | aStartNode = aRefEdge.LastNode(); | |
826 | aPivotNode = aRefEdge.FirstNode(); | |
827 | } | |
828 | ||
829 | ||
830 | const BRepMesh_Vertex& aStartEdgeVertexS = GetVertex( aStartNode ); | |
831 | BRepMesh_Vertex aPivotVertex = GetVertex( aPivotNode ); | |
832 | ||
833 | gp_Vec2d aRefLinkDir( aPivotVertex.Coord() - | |
834 | aStartEdgeVertexS.Coord() ); | |
835 | ||
836 | if ( aRefLinkDir.SquareMagnitude() < Precision2 ) | |
837 | return Standard_True; | |
838 | ||
839 | // Auxilary structures. | |
840 | // Bounding boxes of polygon links to be used for preliminary | |
841 | // analysis of intersections | |
848fa7e3 | 842 | BRepMesh::SequenceOfBndB2d aBoxes; |
304c45c8 | 843 | fillBndBox( aBoxes, aStartEdgeVertexS, aPivotVertex ); |
844 | ||
845 | // Hanging ends | |
848fa7e3 | 846 | BRepMesh::MapOfInteger aDeadLinks; |
304c45c8 | 847 | |
848 | // Links are temporarily excluded from consideration | |
848fa7e3 | 849 | BRepMesh::MapOfInteger aLeprousLinks; |
304c45c8 | 850 | aLeprousLinks.Add( theStartEdgeId ); |
851 | ||
852 | Standard_Boolean isSkipLeprous = Standard_True; | |
853 | Standard_Integer aFirstNode = aStartNode; | |
854 | while ( aPivotNode != aFirstNode ) | |
855 | { | |
856 | Bnd_B2d aNextLinkBndBox; | |
857 | gp_Vec2d aNextLinkDir; | |
858 | Standard_Integer aNextPivotNode = 0; | |
859 | ||
860 | Standard_Integer aNextLinkId = findNextPolygonLink( | |
861 | aFirstNode, | |
862 | aPivotNode, aPivotVertex, aRefLinkDir, | |
863 | aBoxes, aPolygon, theSkipped, | |
864 | isSkipLeprous, aLeprousLinks, aDeadLinks, | |
865 | aNextPivotNode, aNextLinkDir, aNextLinkBndBox ); | |
866 | ||
867 | if ( aNextLinkId != 0 ) | |
868 | { | |
869 | aStartNode = aPivotNode; | |
870 | aRefLinkDir = aNextLinkDir; | |
871 | ||
872 | aPivotNode = aNextPivotNode; | |
873 | aPivotVertex = GetVertex( aNextPivotNode ); | |
0d88155b | 874 | |
304c45c8 | 875 | aBoxes.Append ( aNextLinkBndBox ); |
876 | aPolygon.Append( aNextLinkId ); | |
0d88155b | 877 | |
304c45c8 | 878 | isSkipLeprous = Standard_True; |
879 | } | |
880 | else | |
90dc2e5b | 881 | { |
304c45c8 | 882 | // Nothing to do |
883 | if ( aPolygon.Length() == 1 ) | |
884 | return Standard_False; | |
885 | ||
886 | // Return to the previous point | |
887 | Standard_Integer aDeadLinkId = Abs( aPolygon.Last() ); | |
888 | aDeadLinks.Add ( aDeadLinkId ); | |
889 | ||
890 | aLeprousLinks.Remove( aDeadLinkId ); | |
891 | aPolygon.Remove ( aPolygon.Length() ); | |
892 | aBoxes.Remove ( aBoxes.Length() ); | |
893 | ||
894 | Standard_Integer aPrevLinkInfo = aPolygon.Last(); | |
895 | const BRepMesh_Edge& aPrevLink = GetEdge( Abs( aPrevLinkInfo ) ); | |
896 | ||
897 | if( aPrevLinkInfo > 0 ) | |
90dc2e5b | 898 | { |
304c45c8 | 899 | aStartNode = aPrevLink.FirstNode(); |
900 | aPivotNode = aPrevLink.LastNode(); | |
901 | } | |
902 | else | |
903 | { | |
904 | aStartNode = aPrevLink.LastNode(); | |
905 | aPivotNode = aPrevLink.FirstNode(); | |
0d88155b | 906 | } |
304c45c8 | 907 | |
908 | aPivotVertex = GetVertex( aPivotNode ); | |
909 | aRefLinkDir = | |
910 | aPivotVertex.Coord() - GetVertex( aStartNode ).Coord(); | |
911 | ||
912 | isSkipLeprous = Standard_False; | |
0d88155b | 913 | } |
90dc2e5b | 914 | } |
304c45c8 | 915 | |
916 | if ( aPolygon.Length() < 3 ) | |
917 | return Standard_False; | |
918 | ||
919 | cleanupPolygon( aPolygon, aBoxes ); | |
920 | meshPolygon ( aPolygon, aBoxes, theSkipped ); | |
921 | ||
922 | return Standard_True; | |
90dc2e5b | 923 | } |
0d88155b | 924 | |
90dc2e5b | 925 | //======================================================================= |
304c45c8 | 926 | //function : findNextPolygonLink |
927 | //purpose : Find next link starting from the given node and has maximum | |
928 | // angle respect the given reference link. | |
929 | // Each time the next link is found other neighbor links at the | |
930 | // pivot node are marked as leprous and will be excluded from | |
931 | // consideration next time until a hanging end is occured. | |
90dc2e5b | 932 | //======================================================================= |
304c45c8 | 933 | Standard_Integer BRepMesh_Delaun::findNextPolygonLink( |
848fa7e3 | 934 | const Standard_Integer& theFirstNode, |
935 | const Standard_Integer& thePivotNode, | |
936 | const BRepMesh_Vertex& thePivotVertex, | |
937 | const gp_Vec2d& theRefLinkDir, | |
938 | const BRepMesh::SequenceOfBndB2d& theBoxes, | |
939 | const BRepMesh::SequenceOfInteger& thePolygon, | |
940 | const BRepMesh::HMapOfInteger theSkipped, | |
941 | const Standard_Boolean& isSkipLeprous, | |
942 | BRepMesh::MapOfInteger& theLeprousLinks, | |
943 | BRepMesh::MapOfInteger& theDeadLinks, | |
944 | Standard_Integer& theNextPivotNode, | |
945 | gp_Vec2d& theNextLinkDir, | |
946 | Bnd_B2d& theNextLinkBndBox ) | |
90dc2e5b | 947 | { |
304c45c8 | 948 | // Find the next link having the greatest angle |
949 | // respect to a direction of a reference one | |
ceb418e1 | 950 | Standard_Real aMaxAngle = RealFirst(); |
90dc2e5b | 951 | |
304c45c8 | 952 | Standard_Integer aNextLinkId = 0; |
848fa7e3 | 953 | BRepMesh::ListOfInteger::Iterator aLinkIt( myMeshData->LinksConnectedTo( thePivotNode ) ); |
304c45c8 | 954 | for ( ; aLinkIt.More(); aLinkIt.Next() ) |
90dc2e5b | 955 | { |
304c45c8 | 956 | const Standard_Integer& aNeighbourLinkInfo = aLinkIt.Value(); |
957 | Standard_Integer aNeighbourLinkId = Abs( aNeighbourLinkInfo ); | |
958 | ||
959 | if ( theDeadLinks.Contains( aNeighbourLinkId ) || | |
960 | ( !theSkipped.IsNull() && theSkipped->Contains( aNeighbourLinkId ) ) ) | |
961 | { | |
962 | continue; | |
963 | } | |
964 | ||
965 | Standard_Boolean isLeprous = theLeprousLinks.Contains( aNeighbourLinkId ); | |
966 | if ( isSkipLeprous && isLeprous ) | |
90dc2e5b | 967 | continue; |
968 | ||
304c45c8 | 969 | const BRepMesh_Edge& aNeighbourLink = GetEdge( aNeighbourLinkId ); |
0d88155b | 970 | |
304c45c8 | 971 | // Determine whether the link belongs to the mesh |
972 | if ( aNeighbourLink.Movability() == BRepMesh_Free && | |
fc9b36d6 | 973 | myMeshData->ElementsConnectedTo( aNeighbourLinkInfo ).IsEmpty() ) |
90dc2e5b | 974 | { |
304c45c8 | 975 | theDeadLinks.Add( aNeighbourLinkId ); |
976 | continue; | |
90dc2e5b | 977 | } |
0d88155b | 978 | |
304c45c8 | 979 | Standard_Integer anOtherNode = aNeighbourLink.FirstNode(); |
980 | if ( anOtherNode == thePivotNode ) | |
981 | anOtherNode = aNeighbourLink.LastNode(); | |
982 | ||
983 | gp_Vec2d aCurLinkDir( GetVertex( anOtherNode ).Coord() - | |
984 | thePivotVertex.Coord() ); | |
985 | ||
986 | if ( aCurLinkDir.SquareMagnitude() < Precision2 ) | |
90dc2e5b | 987 | { |
304c45c8 | 988 | theDeadLinks.Add( aNeighbourLinkId ); |
989 | continue; | |
990 | } | |
991 | ||
992 | if ( !isLeprous ) | |
993 | theLeprousLinks.Add( aNeighbourLinkId ); | |
994 | ||
995 | Standard_Real anAngle = theRefLinkDir.Angle( aCurLinkDir ); | |
996 | Standard_Boolean isFrontier = | |
997 | ( aNeighbourLink.Movability() == BRepMesh_Frontier ); | |
998 | ||
999 | Standard_Boolean isCheckPointOnEdge = Standard_True; | |
1000 | if ( isFrontier ) | |
1001 | { | |
1002 | if ( Abs( Abs(anAngle) - M_PI ) < Precision::Angular() ) | |
90dc2e5b | 1003 | { |
304c45c8 | 1004 | // Glued constrains - don't check intersection |
1005 | isCheckPointOnEdge = Standard_False; | |
1006 | anAngle = Abs( anAngle ); | |
0d88155b | 1007 | } |
304c45c8 | 1008 | } |
90dc2e5b | 1009 | |
ceb418e1 | 1010 | if (anAngle <= aMaxAngle) |
304c45c8 | 1011 | continue; |
90dc2e5b | 1012 | |
304c45c8 | 1013 | Standard_Boolean isCheckEndPoints = ( anOtherNode != theFirstNode ); |
90dc2e5b | 1014 | |
304c45c8 | 1015 | Bnd_B2d aBox; |
1016 | Standard_Boolean isNotIntersect = | |
1017 | checkIntersection( aNeighbourLink, thePolygon, theBoxes, | |
1018 | isCheckEndPoints, isCheckPointOnEdge, Standard_True, aBox ); | |
1019 | ||
1020 | if( isNotIntersect ) | |
90dc2e5b | 1021 | { |
304c45c8 | 1022 | aMaxAngle = anAngle; |
90dc2e5b | 1023 | |
304c45c8 | 1024 | theNextLinkDir = aCurLinkDir; |
1025 | theNextPivotNode = anOtherNode; | |
1026 | theNextLinkBndBox = aBox; | |
90dc2e5b | 1027 | |
304c45c8 | 1028 | aNextLinkId = ( aNeighbourLink.FirstNode() == thePivotNode ) ? |
1029 | aNeighbourLinkId : -aNeighbourLinkId; | |
0d88155b O |
1030 | } |
1031 | } | |
304c45c8 | 1032 | |
1033 | return aNextLinkId; | |
0d88155b O |
1034 | } |
1035 | ||
1036 | //======================================================================= | |
304c45c8 | 1037 | //function : checkIntersection |
1038 | //purpose : Check is the given link intersects the polygon boundaries. | |
1039 | // Returns bounding box for the given link trough the | |
1040 | // <theLinkBndBox> parameter. | |
0d88155b | 1041 | //======================================================================= |
304c45c8 | 1042 | Standard_Boolean BRepMesh_Delaun::checkIntersection( |
848fa7e3 | 1043 | const BRepMesh_Edge& theLink, |
1044 | const BRepMesh::SequenceOfInteger& thePolygon, | |
1045 | const BRepMesh::SequenceOfBndB2d& thePolyBoxes, | |
1046 | const Standard_Boolean isConsiderEndPointTouch, | |
1047 | const Standard_Boolean isConsiderPointOnEdge, | |
1048 | const Standard_Boolean isSkipLastEdge, | |
1049 | Bnd_B2d& theLinkBndBox ) const | |
0d88155b | 1050 | { |
71316196 | 1051 | UpdateBndBox(GetVertex(theLink.FirstNode()).Coord(), |
1052 | GetVertex(theLink.LastNode()).Coord(), theLinkBndBox); | |
0d88155b | 1053 | |
304c45c8 | 1054 | Standard_Integer aPolyLen = thePolygon.Length(); |
1055 | // Don't check intersection with the last link | |
1056 | if ( isSkipLastEdge ) | |
1057 | --aPolyLen; | |
0d88155b | 1058 | |
304c45c8 | 1059 | Standard_Boolean isFrontier = |
1060 | ( theLink.Movability() == BRepMesh_Frontier ); | |
0d88155b | 1061 | |
304c45c8 | 1062 | for ( Standard_Integer aPolyIt = 1; aPolyIt <= aPolyLen; ++aPolyIt ) |
90dc2e5b | 1063 | { |
304c45c8 | 1064 | if ( !theLinkBndBox.IsOut( thePolyBoxes.Value( aPolyIt ) ) ) |
90dc2e5b | 1065 | { |
304c45c8 | 1066 | // intersection is possible... |
1067 | Standard_Integer aPolyLinkId = Abs( thePolygon( aPolyIt ) ); | |
1068 | const BRepMesh_Edge& aPolyLink = GetEdge( aPolyLinkId ); | |
0d88155b | 1069 | |
304c45c8 | 1070 | // skip intersections between frontier edges |
1071 | if ( aPolyLink.Movability() == BRepMesh_Frontier && isFrontier ) | |
1072 | continue; | |
90dc2e5b | 1073 | |
304c45c8 | 1074 | gp_Pnt2d anIntPnt; |
fc9b36d6 | 1075 | BRepMesh_GeomTool::IntFlag aIntFlag = intSegSeg( theLink, aPolyLink, |
1076 | isConsiderEndPointTouch, isConsiderPointOnEdge, anIntPnt ); | |
90dc2e5b | 1077 | |
fc9b36d6 | 1078 | if ( aIntFlag != BRepMesh_GeomTool::NoIntersection ) |
304c45c8 | 1079 | return Standard_False; |
0d88155b | 1080 | } |
0d88155b O |
1081 | } |
1082 | ||
304c45c8 | 1083 | // Ok, no intersection |
1084 | return Standard_True; | |
0d88155b O |
1085 | } |
1086 | ||
1087 | //======================================================================= | |
304c45c8 | 1088 | //function : addTriangle |
1089 | //purpose : Add a triangle based on the given oriented edges into mesh | |
0d88155b | 1090 | //======================================================================= |
304c45c8 | 1091 | inline void BRepMesh_Delaun::addTriangle( const Standard_Integer (&theEdgesId)[3], |
1092 | const Standard_Boolean (&theEdgesOri)[3], | |
1093 | const Standard_Integer (&theNodesId)[3] ) | |
0d88155b | 1094 | { |
304c45c8 | 1095 | Standard_Integer aNewTriangleId = |
fc9b36d6 | 1096 | myMeshData->AddElement(BRepMesh_Triangle(theEdgesId, |
1097 | theEdgesOri, BRepMesh_Free)); | |
304c45c8 | 1098 | |
fc9b36d6 | 1099 | Standard_Boolean isAdded = myCircles.Bind( |
1100 | aNewTriangleId, | |
304c45c8 | 1101 | GetVertex( theNodesId[0] ).Coord(), |
1102 | GetVertex( theNodesId[1] ).Coord(), | |
fc9b36d6 | 1103 | GetVertex( theNodesId[2] ).Coord() ); |
304c45c8 | 1104 | |
1105 | if ( !isAdded ) | |
1106 | myMeshData->RemoveElement( aNewTriangleId ); | |
1107 | } | |
1108 | ||
1109 | //======================================================================= | |
1110 | //function : cleanupPolygon | |
1111 | //purpose : Remove internal triangles from the given polygon | |
1112 | //======================================================================= | |
848fa7e3 | 1113 | void BRepMesh_Delaun::cleanupPolygon(const BRepMesh::SequenceOfInteger& thePolygon, |
1114 | const BRepMesh::SequenceOfBndB2d& thePolyBoxes ) | |
304c45c8 | 1115 | { |
1116 | Standard_Integer aPolyLen = thePolygon.Length(); | |
1117 | if ( aPolyLen < 3 ) | |
1118 | return; | |
1119 | ||
2caff0b3 | 1120 | Handle(NCollection_IncAllocator) aAllocator = |
1121 | new NCollection_IncAllocator(BRepMesh::MEMORY_BLOCK_SIZE_HUGE); | |
1122 | ||
1123 | BRepMesh::MapOfIntegerInteger aLoopEdges(10, aAllocator); | |
1124 | BRepMesh::MapOfInteger anIgnoredEdges(10, aAllocator); | |
1125 | BRepMesh::MapOfInteger aPolyVerticesFindMap(10, aAllocator); | |
1126 | BRepMesh::VectorOfInteger aPolyVertices(256, aAllocator); | |
304c45c8 | 1127 | // Collect boundary vertices of the polygon |
1128 | for ( Standard_Integer aPolyIt = 1; aPolyIt <= aPolyLen; ++aPolyIt ) | |
90dc2e5b | 1129 | { |
304c45c8 | 1130 | Standard_Integer aPolyEdgeInfo = thePolygon( aPolyIt ); |
1131 | Standard_Integer aPolyEdgeId = Abs( aPolyEdgeInfo ); | |
1132 | anIgnoredEdges.Add( aPolyEdgeId ); | |
1133 | ||
1134 | Standard_Boolean isForward = ( aPolyEdgeInfo > 0 ); | |
1135 | const BRepMesh_PairOfIndex& aPair = | |
fc9b36d6 | 1136 | myMeshData->ElementsConnectedTo( aPolyEdgeId ); |
304c45c8 | 1137 | |
1138 | Standard_Integer anElemIt = 1; | |
1139 | for ( ; anElemIt <= aPair.Extent(); ++anElemIt ) | |
1140 | { | |
1141 | Standard_Integer anElemId = aPair.Index( anElemIt ); | |
1142 | if ( anElemId < 0 ) | |
1143 | continue; | |
1144 | ||
1145 | Standard_Integer anEdges[3]; | |
1146 | Standard_Boolean anEdgesOri[3]; | |
fc9b36d6 | 1147 | GetTriangle( anElemId ).Edges(anEdges, anEdgesOri); |
304c45c8 | 1148 | |
1149 | Standard_Integer isTriangleFound = Standard_False; | |
1150 | for ( Standard_Integer anEdgeIt = 0; anEdgeIt < 3; ++anEdgeIt ) | |
1151 | { | |
1152 | if ( anEdges[anEdgeIt] == aPolyEdgeId && | |
1153 | anEdgesOri[anEdgeIt] == isForward ) | |
1154 | { | |
1155 | isTriangleFound = Standard_True; | |
1156 | deleteTriangle( anElemId, aLoopEdges ); | |
1157 | break; | |
1158 | } | |
1159 | } | |
1160 | ||
1161 | if ( isTriangleFound ) | |
1162 | break; | |
1163 | } | |
1164 | ||
1165 | // Skip a neighbor link to extract unique vertices each time | |
1166 | if ( aPolyIt % 2 ) | |
1167 | { | |
1168 | const BRepMesh_Edge& aPolyEdge = GetEdge( aPolyEdgeId ); | |
1169 | Standard_Integer aFirstVertex = aPolyEdge.FirstNode(); | |
1170 | Standard_Integer aLastVertex = aPolyEdge.LastNode(); | |
1171 | ||
1172 | aPolyVerticesFindMap.Add( aFirstVertex ); | |
1173 | aPolyVerticesFindMap.Add( aLastVertex ); | |
1174 | ||
1175 | if ( aPolyEdgeInfo > 0 ) | |
1176 | { | |
1177 | aPolyVertices.Append( aFirstVertex ); | |
1178 | aPolyVertices.Append( aLastVertex ); | |
1179 | } | |
1180 | else | |
1181 | { | |
1182 | aPolyVertices.Append( aLastVertex ); | |
1183 | aPolyVertices.Append( aFirstVertex ); | |
1184 | } | |
1185 | } | |
90dc2e5b | 1186 | } |
304c45c8 | 1187 | |
1188 | // Make closed sequence | |
1189 | if ( aPolyVertices.First() != aPolyVertices.Last() ) | |
1190 | aPolyVertices.Append( aPolyVertices.First() ); | |
1191 | ||
848fa7e3 | 1192 | BRepMesh::MapOfInteger aSurvivedLinks( anIgnoredEdges ); |
304c45c8 | 1193 | |
1194 | Standard_Integer aPolyVertIt = 0; | |
1195 | Standard_Integer anUniqueVerticesNum = aPolyVertices.Length() - 1; | |
1196 | for ( ; aPolyVertIt < anUniqueVerticesNum; ++aPolyVertIt ) | |
1197 | { | |
1198 | killTrianglesAroundVertex( aPolyVertices( aPolyVertIt ), | |
1199 | aPolyVertices, aPolyVerticesFindMap, thePolygon, | |
1200 | thePolyBoxes, aSurvivedLinks, aLoopEdges ); | |
1201 | } | |
1202 | ||
848fa7e3 | 1203 | BRepMesh::MapOfIntegerInteger::Iterator aLoopEdgesIt( aLoopEdges ); |
304c45c8 | 1204 | for ( ; aLoopEdgesIt.More(); aLoopEdgesIt.Next() ) |
90dc2e5b | 1205 | { |
304c45c8 | 1206 | const Standard_Integer& aLoopEdgeId = aLoopEdgesIt.Key(); |
1207 | if ( anIgnoredEdges.Contains( aLoopEdgeId ) ) | |
1208 | continue; | |
1209 | ||
fc9b36d6 | 1210 | if ( myMeshData->ElementsConnectedTo( aLoopEdgeId ).IsEmpty() ) |
304c45c8 | 1211 | myMeshData->RemoveLink( aLoopEdgesIt.Key() ); |
90dc2e5b | 1212 | } |
304c45c8 | 1213 | } |
0d88155b | 1214 | |
304c45c8 | 1215 | //======================================================================= |
1216 | //function : killTrianglesAroundVertex | |
1217 | //purpose : Remove all triangles and edges that are placed | |
1218 | // inside the polygon or crossed it. | |
1219 | //======================================================================= | |
1220 | void BRepMesh_Delaun::killTrianglesAroundVertex( | |
848fa7e3 | 1221 | const Standard_Integer theZombieNodeId, |
1222 | const BRepMesh::VectorOfInteger& thePolyVertices, | |
1223 | const BRepMesh::MapOfInteger& thePolyVerticesFindMap, | |
1224 | const BRepMesh::SequenceOfInteger& thePolygon, | |
1225 | const BRepMesh::SequenceOfBndB2d& thePolyBoxes, | |
1226 | BRepMesh::MapOfInteger& theSurvivedLinks, | |
1227 | BRepMesh::MapOfIntegerInteger& theLoopEdges ) | |
304c45c8 | 1228 | { |
848fa7e3 | 1229 | BRepMesh::ListOfInteger::Iterator aNeighborsIt = |
fc9b36d6 | 1230 | myMeshData->LinksConnectedTo( theZombieNodeId ); |
0d88155b | 1231 | |
304c45c8 | 1232 | // Try to infect neighbor nodes |
848fa7e3 | 1233 | BRepMesh::VectorOfInteger aVictimNodes; |
304c45c8 | 1234 | for ( ; aNeighborsIt.More(); aNeighborsIt.Next() ) |
2b59653e | 1235 | { |
304c45c8 | 1236 | const Standard_Integer& aNeighborLinkId = aNeighborsIt.Value(); |
1237 | if ( theSurvivedLinks.Contains( aNeighborLinkId ) ) | |
1238 | continue; | |
1239 | ||
1240 | const BRepMesh_Edge& aNeighborLink = GetEdge( aNeighborLinkId ); | |
1241 | if ( aNeighborLink.Movability() == BRepMesh_Frontier ) | |
90dc2e5b | 1242 | { |
304c45c8 | 1243 | // Though, if it lies onto the polygon boundary - |
1244 | // take its triangles | |
1245 | Bnd_B2d aBox; | |
1246 | Standard_Boolean isNotIntersect = | |
1247 | checkIntersection( aNeighborLink, thePolygon, | |
1248 | thePolyBoxes, Standard_False, Standard_True, | |
1249 | Standard_False, aBox ); | |
1250 | ||
1251 | if ( isNotIntersect ) | |
1252 | { | |
1253 | // Don't touch it | |
1254 | theSurvivedLinks.Add( aNeighborLinkId ); | |
1255 | continue; | |
1256 | } | |
90dc2e5b | 1257 | } |
304c45c8 | 1258 | else |
1259 | { | |
1260 | Standard_Integer anOtherNode = aNeighborLink.FirstNode(); | |
1261 | if ( anOtherNode == theZombieNodeId ) | |
1262 | anOtherNode = aNeighborLink.LastNode(); | |
1263 | ||
1264 | // Possible sacrifice | |
1265 | if ( !thePolyVerticesFindMap.Contains( anOtherNode ) ) | |
1266 | { | |
1267 | if ( isVertexInsidePolygon( anOtherNode, thePolyVertices ) ) | |
1268 | { | |
1269 | // Got you! | |
1270 | aVictimNodes.Append( anOtherNode ); | |
1271 | } | |
1272 | else | |
1273 | { | |
1274 | // Lucky. But it may intersect the polygon boundary. | |
1275 | // Let's check it! | |
1276 | killTrianglesOnIntersectingLinks( aNeighborLinkId, | |
1277 | aNeighborLink, anOtherNode, thePolygon, | |
1278 | thePolyBoxes, theSurvivedLinks, theLoopEdges ); | |
1279 | ||
1280 | continue; | |
1281 | } | |
1282 | } | |
1283 | } | |
1284 | ||
1285 | // Add link to the survivers to avoid cycling | |
1286 | theSurvivedLinks.Add( aNeighborLinkId ); | |
1287 | killLinkTriangles( aNeighborLinkId, theLoopEdges ); | |
90dc2e5b | 1288 | } |
0d88155b | 1289 | |
304c45c8 | 1290 | // Go and do your job! |
848fa7e3 | 1291 | BRepMesh::VectorOfInteger::Iterator aVictimIt( aVictimNodes ); |
304c45c8 | 1292 | for ( ; aVictimIt.More(); aVictimIt.Next() ) |
1293 | { | |
1294 | killTrianglesAroundVertex( aVictimIt.Value(), thePolyVertices, | |
1295 | thePolyVerticesFindMap, thePolygon, thePolyBoxes, | |
1296 | theSurvivedLinks, theLoopEdges ); | |
1297 | } | |
1298 | } | |
1299 | ||
1300 | //======================================================================= | |
1301 | //function : isVertexInsidePolygon | |
1302 | //purpose : Checks is the given vertex lies inside the polygon | |
1303 | //======================================================================= | |
1304 | Standard_Boolean BRepMesh_Delaun::isVertexInsidePolygon( | |
848fa7e3 | 1305 | const Standard_Integer& theVertexId, |
1306 | const BRepMesh::VectorOfInteger& thePolygonVertices ) const | |
304c45c8 | 1307 | { |
1308 | Standard_Integer aPolyLen = thePolygonVertices.Length(); | |
1309 | if ( aPolyLen < 3 ) | |
1310 | return Standard_False; | |
1311 | ||
1312 | ||
1313 | const gp_XY aCenterPointXY = GetVertex( theVertexId ).Coord(); | |
1314 | ||
1315 | const BRepMesh_Vertex& aFirstVertex = GetVertex( thePolygonVertices( 0 ) ); | |
1316 | gp_Vec2d aPrevVertexDir( aFirstVertex.Coord() - aCenterPointXY ); | |
1317 | if ( aPrevVertexDir.SquareMagnitude() < Precision2 ) | |
1318 | return Standard_True; | |
1319 | ||
1320 | Standard_Real aTotalAng = 0.0; | |
1321 | for ( Standard_Integer aPolyIt = 1; aPolyIt < aPolyLen; ++aPolyIt ) | |
1322 | { | |
1323 | const BRepMesh_Vertex& aPolyVertex = GetVertex( thePolygonVertices( aPolyIt ) ); | |
1324 | ||
1325 | gp_Vec2d aCurVertexDir( aPolyVertex.Coord() - aCenterPointXY ); | |
1326 | if ( aCurVertexDir.SquareMagnitude() < Precision2 ) | |
1327 | return Standard_True; | |
90dc2e5b | 1328 | |
304c45c8 | 1329 | aTotalAng += aCurVertexDir.Angle( aPrevVertexDir ); |
1330 | aPrevVertexDir = aCurVertexDir; | |
1331 | } | |
90dc2e5b | 1332 | |
304c45c8 | 1333 | if ( Abs( Angle2PI - aTotalAng ) > Precision::Angular() ) |
1334 | return Standard_False; | |
90dc2e5b | 1335 | |
304c45c8 | 1336 | return Standard_True; |
1337 | } | |
90dc2e5b | 1338 | |
304c45c8 | 1339 | //======================================================================= |
1340 | //function : killTrianglesOnIntersectingLinks | |
1341 | //purpose : Checks is the given link crosses the polygon boundary. | |
1342 | // If yes, kills its triangles and checks neighbor links on | |
1343 | // boundary intersection. Does nothing elsewhere. | |
1344 | //======================================================================= | |
1345 | void BRepMesh_Delaun::killTrianglesOnIntersectingLinks( | |
848fa7e3 | 1346 | const Standard_Integer& theLinkToCheckId, |
1347 | const BRepMesh_Edge& theLinkToCheck, | |
1348 | const Standard_Integer& theEndPoint, | |
1349 | const BRepMesh::SequenceOfInteger& thePolygon, | |
1350 | const BRepMesh::SequenceOfBndB2d& thePolyBoxes, | |
1351 | BRepMesh::MapOfInteger& theSurvivedLinks, | |
1352 | BRepMesh::MapOfIntegerInteger& theLoopEdges ) | |
304c45c8 | 1353 | { |
1354 | if ( theSurvivedLinks.Contains( theLinkToCheckId ) ) | |
1355 | return; | |
1356 | ||
1357 | Bnd_B2d aBox; | |
1358 | Standard_Boolean isNotIntersect = | |
1359 | checkIntersection( theLinkToCheck, thePolygon, | |
1360 | thePolyBoxes, Standard_False, Standard_False, | |
1361 | Standard_False, aBox ); | |
1362 | ||
1363 | theSurvivedLinks.Add( theLinkToCheckId ); | |
1364 | ||
1365 | if ( isNotIntersect ) | |
1366 | return; | |
1367 | ||
1368 | killLinkTriangles( theLinkToCheckId, theLoopEdges ); | |
1369 | ||
2caff0b3 | 1370 | BRepMesh::ListOfInteger::Iterator aNeighborsIt( |
1371 | myMeshData->LinksConnectedTo(theEndPoint)); | |
304c45c8 | 1372 | |
1373 | for ( ; aNeighborsIt.More(); aNeighborsIt.Next() ) | |
90dc2e5b | 1374 | { |
304c45c8 | 1375 | const Standard_Integer& aNeighborLinkId = aNeighborsIt.Value(); |
1376 | const BRepMesh_Edge& aNeighborLink = GetEdge( aNeighborLinkId ); | |
1377 | Standard_Integer anOtherNode = aNeighborLink.FirstNode(); | |
1378 | if ( anOtherNode == theEndPoint ) | |
1379 | anOtherNode = aNeighborLink.LastNode(); | |
1380 | ||
1381 | killTrianglesOnIntersectingLinks( aNeighborLinkId, | |
1382 | aNeighborLink, anOtherNode, thePolygon, | |
1383 | thePolyBoxes, theSurvivedLinks, theLoopEdges ); | |
1384 | } | |
1385 | } | |
1386 | ||
1387 | //======================================================================= | |
1388 | //function : killLinkTriangles | |
1389 | //purpose : Kill triangles bound to the given link. | |
1390 | //======================================================================= | |
1391 | void BRepMesh_Delaun::killLinkTriangles( | |
848fa7e3 | 1392 | const Standard_Integer& theLinkId, |
1393 | BRepMesh::MapOfIntegerInteger& theLoopEdges ) | |
304c45c8 | 1394 | { |
1395 | const BRepMesh_PairOfIndex& aPair = | |
fc9b36d6 | 1396 | myMeshData->ElementsConnectedTo( theLinkId ); |
0d88155b | 1397 | |
304c45c8 | 1398 | Standard_Integer anElemNb = aPair.Extent(); |
1399 | for ( Standard_Integer aPairIt = 1; aPairIt <= anElemNb; ++aPairIt ) | |
1400 | { | |
1401 | Standard_Integer anElemId = aPair.FirstIndex(); | |
1402 | if ( anElemId < 0 ) | |
1403 | continue; | |
90dc2e5b | 1404 | |
304c45c8 | 1405 | deleteTriangle( anElemId, theLoopEdges ); |
1406 | } | |
1407 | } | |
1408 | ||
1409 | //======================================================================= | |
1410 | //function : getOrientedNodes | |
1411 | //purpose : Returns start and end nodes of the given edge in respect to | |
1412 | // its orientation. | |
1413 | //======================================================================= | |
1414 | void BRepMesh_Delaun::getOrientedNodes(const BRepMesh_Edge& theEdge, | |
1415 | const Standard_Boolean isForward, | |
1416 | Standard_Integer *theNodes) const | |
1417 | { | |
1418 | if ( isForward ) | |
1419 | { | |
1420 | theNodes[0] = theEdge.FirstNode(); | |
1421 | theNodes[1] = theEdge.LastNode(); | |
1422 | } | |
1423 | else | |
1424 | { | |
1425 | theNodes[0] = theEdge.LastNode(); | |
1426 | theNodes[1] = theEdge.FirstNode(); | |
1427 | } | |
1428 | } | |
1429 | ||
1430 | //======================================================================= | |
1431 | //function : processLoop | |
1432 | //purpose : Processes loop within the given polygon formed by range of | |
1433 | // its links specified by start and end link indices. | |
1434 | //======================================================================= | |
848fa7e3 | 1435 | void BRepMesh_Delaun::processLoop(const Standard_Integer theLinkFrom, |
1436 | const Standard_Integer theLinkTo, | |
1437 | const BRepMesh::SequenceOfInteger& thePolygon, | |
1438 | const BRepMesh::SequenceOfBndB2d& thePolyBoxes) | |
304c45c8 | 1439 | { |
1440 | Standard_Integer aNbOfLinksInLoop = theLinkTo - theLinkFrom - 1; | |
1441 | if ( aNbOfLinksInLoop < 3 ) | |
1442 | return; | |
1443 | ||
848fa7e3 | 1444 | BRepMesh::SequenceOfInteger aPolygon; |
1445 | BRepMesh::SequenceOfBndB2d aPolyBoxes; | |
304c45c8 | 1446 | for ( ; aNbOfLinksInLoop > 0; --aNbOfLinksInLoop ) |
1447 | { | |
1448 | Standard_Integer aLoopLinkIndex = theLinkFrom + aNbOfLinksInLoop; | |
1449 | aPolygon .Prepend( thePolygon ( aLoopLinkIndex ) ); | |
1450 | aPolyBoxes.Prepend( thePolyBoxes( aLoopLinkIndex ) ); | |
1451 | } | |
1452 | meshPolygon( aPolygon, aPolyBoxes ); | |
1453 | } | |
1454 | ||
1455 | //======================================================================= | |
1456 | //function : createAndReplacePolygonLink | |
1457 | //purpose : Creates new link based on the given nodes and updates the | |
1458 | // given polygon. | |
1459 | //======================================================================= | |
1460 | Standard_Integer BRepMesh_Delaun::createAndReplacePolygonLink( | |
848fa7e3 | 1461 | const Standard_Integer *theNodes, |
1462 | const gp_Pnt2d *thePnts, | |
1463 | const Standard_Integer theRootIndex, | |
1464 | const ReplaceFlag theReplaceFlag, | |
1465 | BRepMesh::SequenceOfInteger& thePolygon, | |
1466 | BRepMesh::SequenceOfBndB2d& thePolyBoxes ) | |
304c45c8 | 1467 | { |
1468 | Standard_Integer aNewEdgeId = | |
1469 | myMeshData->AddLink( BRepMesh_Edge( | |
1470 | theNodes[0], theNodes[1], BRepMesh_Free ) ); | |
1471 | ||
1472 | Bnd_B2d aNewBox; | |
71316196 | 1473 | UpdateBndBox(thePnts[0].Coord(), thePnts[1].Coord(), aNewBox); |
304c45c8 | 1474 | |
1475 | switch ( theReplaceFlag ) | |
1476 | { | |
1477 | case BRepMesh_Delaun::Replace: | |
1478 | thePolygon .SetValue( theRootIndex, aNewEdgeId ); | |
1479 | thePolyBoxes.SetValue( theRootIndex, aNewBox ); | |
1480 | break; | |
1481 | ||
1482 | case BRepMesh_Delaun::InsertAfter: | |
1483 | thePolygon .InsertAfter( theRootIndex, aNewEdgeId ); | |
1484 | thePolyBoxes.InsertAfter( theRootIndex, aNewBox ); | |
1485 | break; | |
1486 | ||
1487 | case BRepMesh_Delaun::InsertBefore: | |
1488 | thePolygon .InsertBefore( theRootIndex, aNewEdgeId ); | |
1489 | thePolyBoxes.InsertBefore( theRootIndex, aNewBox ); | |
1490 | break; | |
1491 | } | |
1492 | ||
1493 | return aNewEdgeId; | |
1494 | } | |
1495 | ||
1496 | //======================================================================= | |
1497 | //function : meshPolygon | |
1498 | //purpose : | |
1499 | //======================================================================= | |
848fa7e3 | 1500 | void BRepMesh_Delaun::meshPolygon(BRepMesh::SequenceOfInteger& thePolygon, |
1501 | BRepMesh::SequenceOfBndB2d& thePolyBoxes, | |
1502 | BRepMesh::HMapOfInteger theSkipped ) | |
304c45c8 | 1503 | { |
1504 | // Check is the source polygon elementary | |
1505 | if ( meshElementaryPolygon( thePolygon ) ) | |
1506 | return; | |
1507 | ||
1508 | // Check and correct boundary edges | |
1509 | Standard_Integer aPolyLen = thePolygon.Length(); | |
1510 | const Standard_Real aPolyArea = Abs( polyArea( thePolygon, 1, aPolyLen ) ); | |
1511 | const Standard_Real aSmallLoopArea = 0.001 * aPolyArea; | |
1512 | for ( Standard_Integer aPolyIt = 1; aPolyIt < aPolyLen; ++aPolyIt ) | |
1513 | { | |
1514 | Standard_Integer aCurEdgeInfo = thePolygon( aPolyIt ); | |
1515 | Standard_Integer aCurEdgeId = Abs( aCurEdgeInfo ); | |
1516 | const BRepMesh_Edge* aCurEdge = &GetEdge( aCurEdgeId ); | |
1517 | if ( aCurEdge->Movability() != BRepMesh_Frontier ) | |
1518 | continue; | |
1519 | ||
1520 | Standard_Integer aCurNodes[2]; | |
1521 | getOrientedNodes( *aCurEdge, aCurEdgeInfo > 0, aCurNodes ); | |
1522 | ||
1523 | gp_Pnt2d aCurPnts[2] = { | |
1524 | GetVertex(aCurNodes[0]).Coord(), | |
1525 | GetVertex(aCurNodes[1]).Coord() | |
1526 | }; | |
1527 | ||
1528 | gp_Vec2d aCurVec( aCurPnts[0], aCurPnts[1] ); | |
1529 | ||
1530 | // check further links | |
1531 | Standard_Integer aNextPolyIt = aPolyIt + 1; | |
1532 | for ( ; aNextPolyIt <= aPolyLen; ++aNextPolyIt ) | |
90dc2e5b | 1533 | { |
304c45c8 | 1534 | Standard_Integer aNextEdgeInfo = thePolygon( aNextPolyIt ); |
1535 | Standard_Integer aNextEdgeId = Abs( aNextEdgeInfo ); | |
1536 | const BRepMesh_Edge* aNextEdge = &GetEdge( aNextEdgeId ); | |
1537 | if ( aNextEdge->Movability() != BRepMesh_Frontier ) | |
1538 | continue; | |
1539 | ||
1540 | Standard_Integer aNextNodes[2]; | |
1541 | getOrientedNodes( *aNextEdge, aNextEdgeInfo > 0, aNextNodes ); | |
1542 | ||
1543 | gp_Pnt2d aNextPnts[2] = { | |
1544 | GetVertex(aNextNodes[0]).Coord(), | |
1545 | GetVertex(aNextNodes[1]).Coord() | |
1546 | }; | |
1547 | ||
1548 | gp_Pnt2d anIntPnt; | |
fc9b36d6 | 1549 | BRepMesh_GeomTool::IntFlag aIntFlag = intSegSeg( *aCurEdge, *aNextEdge, |
1550 | Standard_False, Standard_True, anIntPnt ); | |
304c45c8 | 1551 | |
fc9b36d6 | 1552 | if ( aIntFlag == BRepMesh_GeomTool::NoIntersection ) |
90dc2e5b | 1553 | continue; |
1554 | ||
304c45c8 | 1555 | Standard_Boolean isRemoveFromFirst = Standard_False; |
1556 | Standard_Boolean isAddReplacingEdge = Standard_True; | |
1557 | Standard_Integer aIndexToRemoveTo = aNextPolyIt; | |
fc9b36d6 | 1558 | if ( aIntFlag == BRepMesh_GeomTool::Cross ) |
90dc2e5b | 1559 | { |
304c45c8 | 1560 | Standard_Real aLoopArea = polyArea( thePolygon, aPolyIt + 1, aNextPolyIt ); |
1561 | gp_Vec2d aVec1( anIntPnt, aCurPnts [1] ); | |
1562 | gp_Vec2d aVec2( anIntPnt, aNextPnts[0] ); | |
90dc2e5b | 1563 | |
304c45c8 | 1564 | aLoopArea += ( aVec1 ^ aVec2 ) / 2.; |
1565 | if ( Abs( aLoopArea ) > aSmallLoopArea ) | |
90dc2e5b | 1566 | { |
304c45c8 | 1567 | aNextNodes[1] = aCurNodes[0]; |
1568 | aNextPnts [1] = aCurPnts [0]; | |
1569 | ||
1570 | aNextEdgeId = Abs( createAndReplacePolygonLink( aNextNodes, aNextPnts, | |
1571 | aNextPolyIt, BRepMesh_Delaun::Replace, thePolygon, thePolyBoxes ) ); | |
1572 | ||
1573 | processLoop( aPolyIt, aNextPolyIt, thePolygon, thePolyBoxes ); | |
1574 | return; | |
2b59653e | 1575 | } |
0d88155b | 1576 | |
304c45c8 | 1577 | Standard_Real aDist1 = anIntPnt.SquareDistance(aNextPnts[0]); |
1578 | Standard_Real aDist2 = anIntPnt.SquareDistance(aNextPnts[1]); | |
90dc2e5b | 1579 | |
304c45c8 | 1580 | // Choose node with lower distance |
1581 | const Standard_Boolean isCloseToStart = ( aDist1 < aDist2 ); | |
1582 | const Standard_Integer aEndPointIndex = isCloseToStart ? 0 : 1; | |
1583 | aCurNodes[1] = aNextNodes[aEndPointIndex]; | |
1584 | aCurPnts [1] = aNextPnts [aEndPointIndex]; | |
1585 | ||
1586 | if ( isCloseToStart ) | |
1587 | --aIndexToRemoveTo; | |
1588 | ||
1589 | // In this context only intersections between frontier edges | |
1590 | // are possible. If intersection between edges of different | |
1591 | // types occured - treat this case as invalid (i.e. result | |
1592 | // might not reflect the expectations). | |
1593 | if ( !theSkipped.IsNull() ) | |
1594 | { | |
1595 | Standard_Integer aSkippedLinkIt = aPolyIt; | |
1596 | for ( ; aSkippedLinkIt <= aIndexToRemoveTo; ++aSkippedLinkIt ) | |
1597 | theSkipped->Add( Abs( thePolygon( aSkippedLinkIt ) ) ); | |
1598 | } | |
1599 | } | |
fc9b36d6 | 1600 | else if ( aIntFlag == BRepMesh_GeomTool::PointOnSegment ) |
304c45c8 | 1601 | { |
1602 | // Indentify chopping link | |
1603 | Standard_Boolean isFirstChopping = Standard_False; | |
1604 | Standard_Integer aCheckPointIt = 0; | |
1605 | for ( ; aCheckPointIt < 2; ++aCheckPointIt ) | |
1606 | { | |
1607 | gp_Pnt2d& aRefPoint = aCurPnts[aCheckPointIt]; | |
1608 | // Check is second link touches the first one | |
1609 | gp_Vec2d aVec1( aRefPoint, aNextPnts[0] ); | |
1610 | gp_Vec2d aVec2( aRefPoint, aNextPnts[1] ); | |
1611 | if ( Abs( aVec1 ^ aVec2 ) < Precision ) | |
1612 | { | |
1613 | isFirstChopping = Standard_True; | |
1614 | break; | |
1615 | } | |
1616 | } | |
1617 | ||
1618 | if ( isFirstChopping ) | |
1619 | { | |
1620 | // Split second link | |
1621 | isAddReplacingEdge = Standard_False; | |
1622 | isRemoveFromFirst = ( aCheckPointIt == 0 ); | |
1623 | ||
1624 | Standard_Integer aSplitLink[3] = { | |
1625 | aNextNodes[0], | |
1626 | aCurNodes [aCheckPointIt], | |
1627 | aNextNodes[1] | |
1628 | }; | |
1629 | ||
1630 | gp_Pnt2d aSplitPnts[3] = { | |
1631 | aNextPnts[0], | |
1632 | aCurPnts [aCheckPointIt], | |
1633 | aNextPnts[1] | |
1634 | }; | |
1635 | ||
1636 | Standard_Integer aSplitLinkIt = 0; | |
1637 | for ( ; aSplitLinkIt < 2; ++aSplitLinkIt ) | |
90dc2e5b | 1638 | { |
304c45c8 | 1639 | createAndReplacePolygonLink( &aSplitLink[aSplitLinkIt], |
1640 | &aSplitPnts[aSplitLinkIt], aNextPolyIt, ( aSplitLinkIt == 0 ) ? | |
1641 | BRepMesh_Delaun::Replace : BRepMesh_Delaun::InsertAfter, | |
1642 | thePolygon, thePolyBoxes ); | |
2b59653e | 1643 | } |
304c45c8 | 1644 | |
1645 | processLoop( aPolyIt + aCheckPointIt, aIndexToRemoveTo, | |
1646 | thePolygon, thePolyBoxes ); | |
1647 | } | |
1648 | else | |
1649 | { | |
1650 | // Split first link | |
1651 | Standard_Integer aSplitLinkNodes[2] = { | |
1652 | aNextNodes[1], | |
1653 | aCurNodes [1] | |
1654 | }; | |
1655 | ||
1656 | gp_Pnt2d aSplitLinkPnts[2] = { | |
1657 | aNextPnts[1], | |
1658 | aCurPnts [1] | |
1659 | }; | |
1660 | createAndReplacePolygonLink( aSplitLinkNodes, aSplitLinkPnts, | |
1661 | aPolyIt, BRepMesh_Delaun::InsertAfter, thePolygon, thePolyBoxes ); | |
1662 | ||
1663 | aCurNodes[1] = aNextNodes[1]; | |
1664 | aCurPnts [1] = aNextPnts [1]; | |
1665 | ++aIndexToRemoveTo; | |
1666 | ||
1667 | processLoop( aPolyIt + 1, aIndexToRemoveTo, | |
1668 | thePolygon, thePolyBoxes ); | |
2b59653e | 1669 | } |
0d88155b | 1670 | } |
fc9b36d6 | 1671 | else if ( aIntFlag == BRepMesh_GeomTool::Glued ) |
90dc2e5b | 1672 | { |
304c45c8 | 1673 | if ( aCurNodes[1] == aNextNodes[0] ) |
90dc2e5b | 1674 | { |
304c45c8 | 1675 | aCurNodes[1] = aNextNodes[1]; |
1676 | aCurPnts [1] = aNextPnts [1]; | |
1677 | } | |
1678 | // TODO: Non-adjacent glued links within the polygon | |
1679 | } | |
fc9b36d6 | 1680 | else if ( aIntFlag == BRepMesh_GeomTool::Same ) |
304c45c8 | 1681 | { |
1682 | processLoop( aPolyIt, aNextPolyIt, thePolygon, thePolyBoxes ); | |
90dc2e5b | 1683 | |
304c45c8 | 1684 | isRemoveFromFirst = Standard_True; |
1685 | isAddReplacingEdge = Standard_False; | |
90dc2e5b | 1686 | } |
304c45c8 | 1687 | else |
1688 | continue; // Not supported type | |
0d88155b | 1689 | |
304c45c8 | 1690 | if ( isAddReplacingEdge ) |
1691 | { | |
1692 | aCurEdgeId = Abs( createAndReplacePolygonLink( aCurNodes, aCurPnts, | |
1693 | aPolyIt, BRepMesh_Delaun::Replace, thePolygon, thePolyBoxes ) ); | |
90dc2e5b | 1694 | |
304c45c8 | 1695 | aCurEdge = &GetEdge( aCurEdgeId ); |
1696 | aCurVec = gp_Vec2d( aCurPnts[0], aCurPnts[1] ); | |
1697 | } | |
90dc2e5b | 1698 | |
304c45c8 | 1699 | Standard_Integer aIndexToRemoveFrom = |
1700 | isRemoveFromFirst ? aPolyIt : aPolyIt + 1; | |
90dc2e5b | 1701 | |
304c45c8 | 1702 | thePolygon .Remove( aIndexToRemoveFrom, aIndexToRemoveTo ); |
1703 | thePolyBoxes.Remove( aIndexToRemoveFrom, aIndexToRemoveTo ); | |
90dc2e5b | 1704 | |
304c45c8 | 1705 | aPolyLen = thePolygon.Length(); |
1706 | if ( isRemoveFromFirst ) | |
90dc2e5b | 1707 | { |
304c45c8 | 1708 | --aPolyIt; |
1709 | break; | |
90dc2e5b | 1710 | } |
304c45c8 | 1711 | |
1712 | aNextPolyIt = aPolyIt; | |
0d88155b | 1713 | } |
2b59653e | 1714 | } |
0d88155b | 1715 | |
0a9b38ef | 1716 | BRepMesh::SequenceOfInteger* aPolygon1 = &thePolygon; |
1717 | BRepMesh::SequenceOfBndB2d* aPolyBoxes1 = &thePolyBoxes; | |
1718 | ||
1719 | BRepMesh::HSequenceOfInteger aPolygon2 = new BRepMesh::SequenceOfInteger; | |
1720 | BRepMesh::HSequenceOfBndB2d aPolyBoxes2 = new BRepMesh::SequenceOfBndB2d; | |
1721 | ||
1722 | NCollection_Sequence<BRepMesh::HSequenceOfInteger> aPolyStack; | |
1723 | NCollection_Sequence<BRepMesh::HSequenceOfBndB2d> aPolyBoxStack; | |
1724 | for (;;) | |
1725 | { | |
1726 | decomposeSimplePolygon(*aPolygon1, *aPolyBoxes1, *aPolygon2, *aPolyBoxes2); | |
1727 | if (!aPolygon2->IsEmpty()) | |
1728 | { | |
1729 | aPolyStack.Append(aPolygon2); | |
1730 | aPolyBoxStack.Append(aPolyBoxes2); | |
1731 | ||
1732 | aPolygon2 = new BRepMesh::SequenceOfInteger; | |
1733 | aPolyBoxes2 = new BRepMesh::SequenceOfBndB2d; | |
1734 | } | |
1735 | ||
1736 | if (aPolygon1->IsEmpty()) | |
1737 | { | |
1738 | if (!aPolyStack.IsEmpty() && aPolygon1 == &(*aPolyStack.First())) | |
1739 | { | |
1740 | aPolyStack.Remove(1); | |
1741 | aPolyBoxStack.Remove(1); | |
1742 | } | |
1743 | ||
1744 | if (aPolyStack.IsEmpty()) | |
1745 | return; | |
1746 | ||
1747 | aPolygon1 = &(*aPolyStack.ChangeFirst()); | |
1748 | aPolyBoxes1 = &(*aPolyBoxStack.ChangeFirst()); | |
1749 | } | |
1750 | } | |
2b59653e | 1751 | } |
0d88155b | 1752 | |
2b59653e | 1753 | //======================================================================= |
304c45c8 | 1754 | //function : meshElementaryPolygon |
1755 | //purpose : Triangulation of closed polygon containing only three edges. | |
2b59653e | 1756 | //======================================================================= |
304c45c8 | 1757 | inline Standard_Boolean BRepMesh_Delaun::meshElementaryPolygon( |
848fa7e3 | 1758 | const BRepMesh::SequenceOfInteger& thePolygon) |
2b59653e | 1759 | { |
304c45c8 | 1760 | Standard_Integer aPolyLen = thePolygon.Length(); |
1761 | if ( aPolyLen < 3 ) | |
1762 | return Standard_True; | |
1763 | else if ( aPolyLen > 3 ) | |
1764 | return Standard_False; | |
0d88155b | 1765 | |
304c45c8 | 1766 | // Just create a triangle |
1767 | Standard_Integer anEdges[3]; | |
1768 | Standard_Boolean anEdgesOri[3]; | |
0d88155b | 1769 | |
304c45c8 | 1770 | for ( Standard_Integer anEdgeIt = 0; anEdgeIt < 3; ++anEdgeIt ) |
1771 | { | |
1772 | Standard_Integer anEdgeInfo = thePolygon( anEdgeIt + 1 ); | |
1773 | anEdges[anEdgeIt] = Abs( anEdgeInfo ); | |
1774 | anEdgesOri[anEdgeIt] = ( anEdgeInfo > 0 ); | |
90dc2e5b | 1775 | } |
fc9b36d6 | 1776 | |
304c45c8 | 1777 | const BRepMesh_Edge& anEdge1 = GetEdge( anEdges[0] ); |
1778 | const BRepMesh_Edge& anEdge2 = GetEdge( anEdges[1] ); | |
1779 | ||
1780 | Standard_Integer aNodes[3] = { anEdge1.FirstNode(), | |
1781 | anEdge1.LastNode(), | |
1782 | anEdge2.FirstNode() }; | |
1783 | if ( aNodes[2] == aNodes[0] || | |
1784 | aNodes[2] == aNodes[1] ) | |
2b59653e | 1785 | { |
304c45c8 | 1786 | aNodes[2] = anEdge2.LastNode(); |
1787 | } | |
90dc2e5b | 1788 | |
304c45c8 | 1789 | addTriangle( anEdges, anEdgesOri, aNodes ); |
1790 | return Standard_True; | |
1791 | } | |
90dc2e5b | 1792 | |
304c45c8 | 1793 | //======================================================================= |
1794 | //function : meshSimplePolygon | |
0a9b38ef | 1795 | //purpose : |
304c45c8 | 1796 | //======================================================================= |
0a9b38ef | 1797 | void BRepMesh_Delaun::decomposeSimplePolygon( |
1798 | BRepMesh::SequenceOfInteger& thePolygon, | |
1799 | BRepMesh::SequenceOfBndB2d& thePolyBoxes, | |
1800 | BRepMesh::SequenceOfInteger& thePolygonCut, | |
1801 | BRepMesh::SequenceOfBndB2d& thePolyBoxesCut) | |
304c45c8 | 1802 | { |
1803 | // Check is the given polygon elementary | |
1804 | if ( meshElementaryPolygon( thePolygon ) ) | |
0a9b38ef | 1805 | { |
1806 | thePolygon.Clear(); | |
1807 | thePolyBoxes.Clear(); | |
304c45c8 | 1808 | return; |
0a9b38ef | 1809 | } |
304c45c8 | 1810 | |
1811 | // Polygon contains more than 3 links | |
1812 | Standard_Integer aFirstEdgeInfo = thePolygon(1); | |
1813 | const BRepMesh_Edge& aFirstEdge = GetEdge( Abs( aFirstEdgeInfo ) ); | |
1814 | ||
1815 | Standard_Integer aNodes[3]; | |
1816 | getOrientedNodes( aFirstEdge, aFirstEdgeInfo > 0, aNodes ); | |
1817 | ||
1818 | gp_Pnt2d aRefVertices[3]; | |
1819 | aRefVertices[0] = GetVertex( aNodes[0] ).Coord(); | |
1820 | aRefVertices[1] = GetVertex( aNodes[1] ).Coord(); | |
1821 | ||
1822 | gp_Vec2d aRefEdgeDir( aRefVertices[0], aRefVertices[1] ); | |
1823 | ||
1824 | Standard_Real aRefEdgeLen = aRefEdgeDir.Magnitude(); | |
1825 | if ( aRefEdgeLen < Precision ) | |
0a9b38ef | 1826 | { |
1827 | thePolygon.Clear(); | |
1828 | thePolyBoxes.Clear(); | |
304c45c8 | 1829 | return; |
0a9b38ef | 1830 | } |
304c45c8 | 1831 | |
1832 | aRefEdgeDir /= aRefEdgeLen; | |
1833 | ||
1834 | // Find a point with minimum distance respect | |
1835 | // the end of reference link | |
1836 | Standard_Integer aUsedLinkId = 0; | |
1837 | Standard_Real aOptAngle = 0.0; | |
1838 | Standard_Real aMinDist = RealLast(); | |
1839 | Standard_Integer aPivotNode = aNodes[1]; | |
1840 | Standard_Integer aPolyLen = thePolygon.Length(); | |
1841 | for ( Standard_Integer aLinkIt = 3; aLinkIt <= aPolyLen; ++aLinkIt ) | |
1842 | { | |
1843 | Standard_Integer aLinkInfo = thePolygon( aLinkIt ); | |
1844 | const BRepMesh_Edge& aNextEdge = GetEdge( Abs( aLinkInfo ) ); | |
1845 | ||
1846 | aPivotNode = aLinkInfo > 0 ? | |
1847 | aNextEdge.FirstNode() : | |
1848 | aNextEdge.LastNode(); | |
1849 | ||
1850 | gp_Pnt2d aPivotVertex = GetVertex( aPivotNode ).Coord(); | |
1851 | gp_Vec2d aDistanceDir( aRefVertices[1], aPivotVertex ); | |
1852 | ||
1853 | Standard_Real aDist = aRefEdgeDir ^ aDistanceDir; | |
1854 | Standard_Real aAngle = Abs( aRefEdgeDir.Angle(aDistanceDir) ); | |
1855 | Standard_Real anAbsDist = Abs( aDist ); | |
ceb418e1 | 1856 | if (anAbsDist < Precision || aDist < 0.) |
304c45c8 | 1857 | continue; |
304c45c8 | 1858 | |
1859 | if ( ( anAbsDist >= aMinDist ) && | |
1860 | ( aAngle <= aOptAngle || aAngle > AngDeviation90Deg ) ) | |
2b59653e | 1861 | { |
304c45c8 | 1862 | continue; |
90dc2e5b | 1863 | } |
304c45c8 | 1864 | |
1865 | // Check is the test link crosses the polygon boudaries | |
1866 | Standard_Boolean isIntersect = Standard_False; | |
1867 | for ( Standard_Integer aRefLinkNodeIt = 0; aRefLinkNodeIt < 2; ++aRefLinkNodeIt ) | |
90dc2e5b | 1868 | { |
304c45c8 | 1869 | const Standard_Integer& aLinkFirstNode = aNodes[aRefLinkNodeIt]; |
1870 | const gp_Pnt2d& aLinkFirstVertex = aRefVertices[aRefLinkNodeIt]; | |
90dc2e5b | 1871 | |
304c45c8 | 1872 | Bnd_B2d aBox; |
71316196 | 1873 | UpdateBndBox(aLinkFirstVertex.Coord(), aPivotVertex.Coord(), aBox); |
304c45c8 | 1874 | |
1875 | BRepMesh_Edge aCheckLink( aLinkFirstNode, aPivotNode, BRepMesh_Free ); | |
90dc2e5b | 1876 | |
304c45c8 | 1877 | Standard_Integer aCheckLinkIt = 2; |
1878 | for ( ; aCheckLinkIt <= aPolyLen; ++aCheckLinkIt ) | |
1879 | { | |
1880 | if( aCheckLinkIt == aLinkIt ) | |
1881 | continue; | |
1882 | ||
1883 | if ( !aBox.IsOut( thePolyBoxes.Value( aCheckLinkIt ) ) ) | |
90dc2e5b | 1884 | { |
304c45c8 | 1885 | const BRepMesh_Edge& aPolyLink = |
1886 | GetEdge( Abs( thePolygon( aCheckLinkIt ) ) ); | |
1887 | ||
1888 | if ( aCheckLink.IsEqual( aPolyLink ) ) | |
1889 | continue; | |
1890 | ||
1891 | // intersection is possible... | |
1892 | gp_Pnt2d anIntPnt; | |
fc9b36d6 | 1893 | BRepMesh_GeomTool::IntFlag aIntFlag = intSegSeg( aCheckLink, aPolyLink, |
1894 | Standard_False, Standard_False, anIntPnt ); | |
304c45c8 | 1895 | |
fc9b36d6 | 1896 | if( aIntFlag != BRepMesh_GeomTool::NoIntersection ) |
304c45c8 | 1897 | { |
1898 | isIntersect = Standard_True; | |
1899 | break; | |
90dc2e5b | 1900 | } |
1901 | } | |
1902 | } | |
304c45c8 | 1903 | |
1904 | if ( isIntersect ) | |
1905 | break; | |
90dc2e5b | 1906 | } |
1907 | ||
304c45c8 | 1908 | if( isIntersect ) |
1909 | continue; | |
90dc2e5b | 1910 | |
90dc2e5b | 1911 | |
304c45c8 | 1912 | aOptAngle = aAngle; |
1913 | aMinDist = anAbsDist; | |
1914 | aNodes[2] = aPivotNode; | |
1915 | aRefVertices[2] = aPivotVertex; | |
1916 | aUsedLinkId = aLinkIt; | |
1917 | } | |
1918 | ||
1919 | if ( aUsedLinkId == 0 ) | |
0a9b38ef | 1920 | { |
1921 | thePolygon.Clear(); | |
1922 | thePolyBoxes.Clear(); | |
304c45c8 | 1923 | return; |
0a9b38ef | 1924 | } |
304c45c8 | 1925 | |
1926 | ||
1927 | BRepMesh_Edge aNewEdges[2] = { | |
1928 | BRepMesh_Edge( aNodes[1], aNodes[2], BRepMesh_Free ), | |
1929 | BRepMesh_Edge( aNodes[2], aNodes[0], BRepMesh_Free ) }; | |
1930 | ||
1931 | Standard_Integer aNewEdgesInfo[3] = { | |
1932 | aFirstEdgeInfo, | |
1933 | myMeshData->AddLink( aNewEdges[0] ), | |
1934 | myMeshData->AddLink( aNewEdges[1] ) }; | |
1935 | ||
1936 | ||
1937 | Standard_Integer anEdges[3]; | |
1938 | Standard_Boolean anEdgesOri[3]; | |
1939 | for ( Standard_Integer aTriEdgeIt = 0; aTriEdgeIt < 3; ++aTriEdgeIt ) | |
1940 | { | |
1941 | const Standard_Integer& anEdgeInfo = aNewEdgesInfo[aTriEdgeIt]; | |
1942 | anEdges[aTriEdgeIt] = Abs( anEdgeInfo ); | |
1943 | anEdgesOri[aTriEdgeIt] = anEdgeInfo > 0; | |
1944 | } | |
1945 | addTriangle( anEdges, anEdgesOri, aNodes ); | |
1946 | ||
1947 | // Create triangle and split the source polygon on two | |
1948 | // parts (if possible) and mesh each part as independent | |
1949 | // polygon. | |
1950 | if ( aUsedLinkId < aPolyLen ) | |
1951 | { | |
0a9b38ef | 1952 | thePolygon.Split(aUsedLinkId, thePolygonCut); |
1953 | thePolygonCut.Prepend( -aNewEdgesInfo[2] ); | |
1954 | thePolyBoxes.Split(aUsedLinkId, thePolyBoxesCut); | |
304c45c8 | 1955 | |
1956 | Bnd_B2d aBox; | |
71316196 | 1957 | UpdateBndBox(aRefVertices[0].Coord(), aRefVertices[2].Coord(), aBox); |
0a9b38ef | 1958 | thePolyBoxesCut.Prepend( aBox ); |
304c45c8 | 1959 | } |
1960 | else | |
1961 | { | |
1962 | thePolygon.Remove ( aPolyLen ); | |
1963 | thePolyBoxes.Remove( aPolyLen ); | |
1964 | } | |
1965 | ||
1966 | if ( aUsedLinkId > 3 ) | |
1967 | { | |
1968 | thePolygon.SetValue( 1, -aNewEdgesInfo[1] ); | |
1969 | ||
1970 | Bnd_B2d aBox; | |
71316196 | 1971 | UpdateBndBox(aRefVertices[1].Coord(), aRefVertices[2].Coord(), aBox); |
304c45c8 | 1972 | thePolyBoxes.SetValue( 1, aBox ); |
0d88155b | 1973 | } |
0d88155b O |
1974 | } |
1975 | ||
1976 | //======================================================================= | |
1977 | //function : RemoveVertex | |
90dc2e5b | 1978 | //purpose : Removes a vertex from the triangulation |
0d88155b | 1979 | //======================================================================= |
fc9b36d6 | 1980 | void BRepMesh_Delaun::RemoveVertex( const BRepMesh_Vertex& theVertex ) |
0d88155b | 1981 | { |
90dc2e5b | 1982 | BRepMesh_SelectorOfDataStructureOfDelaun aSelector( myMeshData ); |
1983 | aSelector.NeighboursOf( theVertex ); | |
0d88155b | 1984 | |
2caff0b3 | 1985 | BRepMesh::MapOfIntegerInteger aLoopEdges;//( 10, myMeshData->Allocator() ); |
0d88155b O |
1986 | |
1987 | // Loop on triangles to be destroyed : | |
848fa7e3 | 1988 | BRepMesh::MapOfInteger::Iterator aTriangleIt( aSelector.Elements() ); |
90dc2e5b | 1989 | for ( ; aTriangleIt.More(); aTriangleIt.Next() ) |
304c45c8 | 1990 | deleteTriangle( aTriangleIt.Key(), aLoopEdges ); |
0d88155b | 1991 | |
848fa7e3 | 1992 | BRepMesh::SequenceOfBndB2d aBoxes; |
1993 | BRepMesh::SequenceOfInteger aPolygon; | |
90dc2e5b | 1994 | Standard_Integer aLoopEdgesCount = aLoopEdges.Extent(); |
848fa7e3 | 1995 | BRepMesh::MapOfIntegerInteger::Iterator aLoopEdgesIt( aLoopEdges ); |
90dc2e5b | 1996 | |
1997 | if ( aLoopEdgesIt.More() ) | |
1998 | { | |
1999 | const BRepMesh_Edge& anEdge = GetEdge( aLoopEdgesIt.Key() ); | |
2000 | Standard_Integer aFirstNode = anEdge.FirstNode(); | |
2001 | Standard_Integer aLastNode; | |
2002 | Standard_Integer aPivotNode = anEdge.LastNode(); | |
2003 | Standard_Integer anEdgeId = aLoopEdgesIt.Key(); | |
2004 | ||
2005 | Standard_Boolean isPositive = (Standard_Boolean)aLoopEdges( anEdgeId ); | |
2006 | if ( !isPositive ) | |
2007 | { | |
2008 | Standard_Integer aTmp; | |
2009 | aTmp = aFirstNode; | |
2010 | aFirstNode = aPivotNode; | |
2011 | aPivotNode = aTmp; | |
2012 | ||
2013 | aPolygon.Append( -anEdgeId ); | |
0d88155b | 2014 | } |
90dc2e5b | 2015 | else |
2016 | aPolygon.Append( anEdgeId ); | |
2017 | ||
304c45c8 | 2018 | fillBndBox( aBoxes, GetVertex( aFirstNode ), GetVertex( aPivotNode ) ); |
2019 | ||
90dc2e5b | 2020 | aLoopEdges.UnBind( anEdgeId ); |
2021 | ||
2022 | aLastNode = aFirstNode; | |
2023 | while ( aPivotNode != aLastNode ) | |
2024 | { | |
848fa7e3 | 2025 | BRepMesh::ListOfInteger::Iterator aLinkIt( myMeshData->LinksConnectedTo( aPivotNode ) ); |
90dc2e5b | 2026 | for ( ; aLinkIt.More(); aLinkIt.Next() ) |
2027 | { | |
2028 | if ( aLinkIt.Value() != anEdgeId && | |
2029 | aLoopEdges.IsBound( aLinkIt.Value() ) ) | |
2030 | { | |
2031 | Standard_Integer aCurrentNode; | |
2032 | anEdgeId = aLinkIt.Value(); | |
2033 | const BRepMesh_Edge& anEdge1 = GetEdge( anEdgeId ); | |
2034 | ||
2035 | aCurrentNode = anEdge1.LastNode(); | |
2036 | if ( aCurrentNode != aPivotNode ) | |
2037 | { | |
2038 | aCurrentNode = anEdge1.FirstNode(); | |
2039 | aPolygon.Append( -anEdgeId ); | |
0d88155b O |
2040 | } |
2041 | else | |
90dc2e5b | 2042 | aPolygon.Append( anEdgeId ); |
304c45c8 | 2043 | |
2044 | fillBndBox( aBoxes, GetVertex( aCurrentNode ), GetVertex( aPivotNode ) ); | |
90dc2e5b | 2045 | |
2046 | aPivotNode = aCurrentNode; | |
2047 | aLoopEdges.UnBind( anEdgeId ); | |
0d88155b O |
2048 | break; |
2049 | } | |
2050 | } | |
90dc2e5b | 2051 | |
2052 | if ( aLoopEdgesCount <= 0 ) | |
2053 | break; | |
2054 | --aLoopEdgesCount; | |
0d88155b | 2055 | } |
90dc2e5b | 2056 | |
304c45c8 | 2057 | meshPolygon( aPolygon, aBoxes ); |
0d88155b O |
2058 | } |
2059 | } | |
2060 | ||
2061 | ||
2062 | //======================================================================= | |
2063 | //function : AddVertices | |
90dc2e5b | 2064 | //purpose : Adds some vertices in the triangulation. |
0d88155b | 2065 | //======================================================================= |
848fa7e3 | 2066 | void BRepMesh_Delaun::AddVertices(BRepMesh::Array1OfVertexOfDelaun& theVertices) |
0d88155b | 2067 | { |
fc9b36d6 | 2068 | std::make_heap(theVertices.begin(), theVertices.end(), ComparatorOfVertexOfDelaun()); |
2069 | std::sort_heap(theVertices.begin(), theVertices.end(), ComparatorOfVertexOfDelaun()); | |
0d88155b | 2070 | |
90dc2e5b | 2071 | Standard_Integer aLower = theVertices.Lower(); |
2072 | Standard_Integer anUpper = theVertices.Upper(); | |
2b59653e | 2073 | |
848fa7e3 | 2074 | BRepMesh::Array1OfInteger aVertexIndexes( aLower, anUpper ); |
90dc2e5b | 2075 | for ( Standard_Integer i = aLower; i <= anUpper; ++i ) |
2076 | aVertexIndexes(i) = myMeshData->AddNode( theVertices(i) ); | |
0d88155b | 2077 | |
304c45c8 | 2078 | createTrianglesOnNewVertices( aVertexIndexes ); |
0d88155b O |
2079 | } |
2080 | ||
2081 | //======================================================================= | |
2082 | //function : UseEdge | |
90dc2e5b | 2083 | //purpose : Modify mesh to use the edge. Return True if done |
0d88155b | 2084 | //======================================================================= |
498ce76b | 2085 | Standard_Boolean BRepMesh_Delaun::UseEdge( const Standard_Integer /*theIndex*/ ) |
0d88155b | 2086 | { |
90dc2e5b | 2087 | /* |
2088 | const BRepMesh_PairOfIndex& aPair = myMeshData->ElemConnectedTo( theIndex ); | |
2089 | if ( aPair.Extent() == 0 ) | |
2090 | { | |
2091 | const BRepMesh_Edge& anEdge = GetEdge( theIndex ); | |
2092 | ||
2093 | Standard_Integer aStartNode, aPivotNode, anOtherNode; | |
2094 | aStartNode = anEdge.FirstNode(); | |
2095 | aPivotNode = anEdge.LastNode(); | |
2096 | ||
2097 | const BRepMesh_ListOfInteger& aStartNodeNeighbors = myMeshData->LinkNeighboursOf( aStartNode ); | |
2098 | const BRepMesh_ListOfInteger& aPivotNodeNeighbors = myMeshData->LinkNeighboursOf( aPivotNode ); | |
2099 | ||
2100 | if ( aStartNodeNeighbors.Extent() > 0 && | |
2101 | aPivotNodeNeighbors.Extent() > 0 ) | |
2102 | { | |
2103 | const BRepMesh_Vertex& aStartVertex = GetVertex( aStartNode ); | |
2104 | const BRepMesh_Vertex& aPivotVertex = GetVertex( aPivotNode ); | |
2105 | ||
2106 | gp_XY aVEdge ( aPivotVertex.Coord() ); | |
2107 | aVEdge.Subtract( aStartVertex.Coord() ); | |
2108 | ||
2109 | Standard_Real anAngle = 0.; | |
2110 | Standard_Real anAngleMin = RealLast(); | |
2111 | Standard_Real anAngleMax = RealFirst(); | |
2112 | Standard_Integer aLeftEdge = 0, aRightEdge = 0; | |
2113 | ||
2114 | BRepMesh_ListOfInteger::Iterator aNeighborIt( aPivotNodeNeighbors ); | |
2115 | for ( ; aNeighborIt.More(); aNeighborIt.Next() ) | |
2116 | { | |
2117 | Standard_Integer anEdgeId = aNeighborIt.Value(); | |
2118 | if ( anEdgeId != theIndex ) | |
2119 | { | |
2120 | const BRepMesh_Edge& aNextEdge = GetEdge( anEdgeId ); | |
2121 | ||
2122 | Standard_Boolean isInMesh = Standard_True; | |
2123 | if ( aNextEdge.Movability() == BRepMesh_Free ) | |
2124 | { | |
2125 | if ( myMeshData->ElemConnectedTo( anEdgeId ).IsEmpty() ) | |
2126 | isInMesh = Standard_False; | |
0d88155b O |
2127 | } |
2128 | ||
90dc2e5b | 2129 | if ( isInMesh ) |
2130 | { | |
2131 | anOtherNode = aNextEdge.FirstNode(); | |
2132 | if ( anOtherNode == aPivotNode ) | |
2133 | anOtherNode = aNextEdge.LastNode(); | |
0d88155b | 2134 | |
90dc2e5b | 2135 | gp_XY aVEdgeCur = GetVertex( anOtherNode ).Coord(); |
2136 | aVEdgeCur.Subtract( aPivotVertex.Coord() ); | |
0d88155b | 2137 | |
90dc2e5b | 2138 | anAngle = gp_Vec2d( aVEdge ).Angle( gp_Vec2d( aVEdgeCur ) ); |
0d88155b | 2139 | } |
90dc2e5b | 2140 | |
2141 | if ( anAngle > anAngleMax ) | |
2142 | { | |
2143 | anAngleMax = anAngle; | |
2144 | aLeftEdge = anEdgeId; | |
0d88155b | 2145 | } |
90dc2e5b | 2146 | if ( anAngle < anAngleMin ) |
2147 | { | |
2148 | anAngleMin = anAngle; | |
2149 | aRightEdge = anEdgeId; | |
0d88155b O |
2150 | } |
2151 | } | |
2152 | } | |
90dc2e5b | 2153 | |
2154 | if ( aLeftEdge > 0 ) | |
2155 | { | |
2156 | if (aLeftEdge==aRightEdge) | |
2157 | { | |
0d88155b | 2158 | } |
90dc2e5b | 2159 | else |
2160 | { | |
0d88155b O |
2161 | } |
2162 | } | |
2163 | } | |
2164 | } | |
90dc2e5b | 2165 | */ |
0d88155b O |
2166 | return Standard_False; |
2167 | } | |
2168 | ||
0d88155b | 2169 | //======================================================================= |
304c45c8 | 2170 | //function : getEdgesByType |
2171 | //purpose : Gives the list of edges with type defined by input parameter | |
0d88155b | 2172 | //======================================================================= |
848fa7e3 | 2173 | BRepMesh::HMapOfInteger BRepMesh_Delaun::getEdgesByType( |
304c45c8 | 2174 | const BRepMesh_DegreeOfFreedom theEdgeType ) const |
0d88155b | 2175 | { |
848fa7e3 | 2176 | BRepMesh::HMapOfInteger aResult = new BRepMesh::MapOfInteger; |
2177 | BRepMesh::MapOfInteger::Iterator anEdgeIt( myMeshData->LinksOfDomain() ); | |
0d88155b | 2178 | |
90dc2e5b | 2179 | for ( ; anEdgeIt.More(); anEdgeIt.Next() ) |
2180 | { | |
2181 | Standard_Integer anEdge = anEdgeIt.Key(); | |
304c45c8 | 2182 | Standard_Boolean isToAdd = (theEdgeType == BRepMesh_Free) ? |
fc9b36d6 | 2183 | (myMeshData->ElementsConnectedTo( anEdge ).Extent() <= 1) : |
304c45c8 | 2184 | (GetEdge( anEdge ).Movability() == theEdgeType); |
0d88155b | 2185 | |
304c45c8 | 2186 | if (isToAdd) |
2187 | aResult->Add( anEdge ); | |
0d88155b | 2188 | } |
90dc2e5b | 2189 | |
304c45c8 | 2190 | return aResult; |
0d88155b O |
2191 | } |
2192 | ||
0d88155b | 2193 | //======================================================================= |
90dc2e5b | 2194 | //function : calculateDist |
2195 | //purpose : Calculates distances between the given point and edges of | |
2196 | // triangle | |
0d88155b | 2197 | //======================================================================= |
304c45c8 | 2198 | Standard_Real BRepMesh_Delaun::calculateDist( const gp_XY theVEdges[3], |
2199 | const gp_XY thePoints[3], | |
304c45c8 | 2200 | const BRepMesh_Vertex& theVertex, |
2201 | Standard_Real theDistance[3], | |
2202 | Standard_Real theSqModulus[3], | |
2203 | Standard_Integer& theEdgeOn ) const | |
2b59653e | 2204 | { |
71316196 | 2205 | Standard_Real aMinDist = RealLast(); |
90dc2e5b | 2206 | for( Standard_Integer i = 0; i < 3; ++i ) |
2b59653e | 2207 | { |
90dc2e5b | 2208 | theSqModulus[i] = theVEdges[i].SquareModulus(); |
304c45c8 | 2209 | if ( theSqModulus[i] <= Precision2 ) |
90dc2e5b | 2210 | return -1; |
2211 | ||
2212 | theDistance[i] = theVEdges[i] ^ ( theVertex.Coord() - thePoints[i] ); | |
2213 | ||
2214 | Standard_Real aDist = theDistance[i] * theDistance[i]; | |
2215 | aDist /= theSqModulus[i]; | |
2b59653e | 2216 | |
71316196 | 2217 | if ( aDist < aMinDist ) |
2b59653e | 2218 | { |
71316196 | 2219 | theEdgeOn = i; |
90dc2e5b | 2220 | aMinDist = aDist; |
2b59653e E |
2221 | } |
2222 | } | |
90dc2e5b | 2223 | |
2224 | return aMinDist; | |
2b59653e E |
2225 | } |
2226 | ||
90dc2e5b | 2227 | //======================================================================= |
2228 | //function : Contains | |
2229 | //purpose : Test if triangle of index <TrianIndex> contains geometricaly | |
2230 | // <theVertex>. If <theEdgeOn> is != 0 then theVertex is on Edge | |
2231 | // of index <theEdgeOn> | |
2232 | //======================================================================= | |
2233 | Standard_Boolean BRepMesh_Delaun::Contains( const Standard_Integer theTriangleId, | |
2234 | const BRepMesh_Vertex& theVertex, | |
71316196 | 2235 | const Standard_Real theSqTolerance, |
2236 | Standard_Integer& theEdgeOn) const | |
0d88155b | 2237 | { |
90dc2e5b | 2238 | theEdgeOn = 0; |
2239 | ||
2240 | Standard_Integer e[3]; | |
2241 | Standard_Boolean o[3]; | |
2242 | Standard_Integer p[3]; | |
2243 | ||
fc9b36d6 | 2244 | const BRepMesh_Triangle& aElement = GetTriangle( theTriangleId ); |
2245 | aElement.Edges(e, o); | |
2246 | ||
90dc2e5b | 2247 | const BRepMesh_Edge* anEdges[3] = { &GetEdge( e[0] ), |
2248 | &GetEdge( e[1] ), | |
2249 | &GetEdge( e[2] ) }; | |
fc9b36d6 | 2250 | |
2251 | myMeshData->ElementNodes(aElement, p); | |
90dc2e5b | 2252 | |
2253 | gp_XY aPoints[3]; | |
2254 | aPoints[0] = GetVertex( p[0] ).Coord(); | |
2255 | aPoints[1] = GetVertex( p[1] ).Coord(); | |
2256 | aPoints[2] = GetVertex( p[2] ).Coord(); | |
2257 | ||
2258 | gp_XY aVEdges[3]; | |
2259 | aVEdges[0] = aPoints[1]; | |
2260 | aVEdges[0].Subtract( aPoints[0] ); | |
2261 | ||
2262 | aVEdges[1] = aPoints[2]; | |
2263 | aVEdges[1].Subtract( aPoints[1] ); | |
2b59653e | 2264 | |
90dc2e5b | 2265 | aVEdges[2] = aPoints[0]; |
2266 | aVEdges[2].Subtract( aPoints[2] ); | |
2267 | ||
2268 | Standard_Real aDistance[3]; | |
2269 | Standard_Real aSqModulus[3]; | |
2270 | ||
71316196 | 2271 | Standard_Real aSqMinDist; |
2272 | Standard_Integer aEdgeOnId; | |
2273 | aSqMinDist = calculateDist( aVEdges, aPoints, theVertex, aDistance, aSqModulus, aEdgeOnId ); | |
2274 | if ( aSqMinDist < 0 ) | |
2b59653e | 2275 | return Standard_False; |
71316196 | 2276 | |
2277 | const Standard_Boolean isNotFree = (anEdges[aEdgeOnId]->Movability() != BRepMesh_Free); | |
2278 | if ( aSqMinDist > theSqTolerance ) | |
90dc2e5b | 2279 | { |
71316196 | 2280 | if (isNotFree && aDistance[aEdgeOnId] < ( aSqModulus[aEdgeOnId] / 5. )) |
2281 | theEdgeOn = e[aEdgeOnId]; | |
0d88155b | 2282 | } |
71316196 | 2283 | else if (isNotFree) |
2284 | return Standard_False; | |
2285 | else | |
2286 | theEdgeOn = e[aEdgeOnId]; | |
0d88155b | 2287 | |
71316196 | 2288 | return (aDistance[0] >= 0. && aDistance[1] >= 0. && aDistance[2] >= 0.); |
0d88155b O |
2289 | } |
2290 | ||
90dc2e5b | 2291 | //============================================================================= |
304c45c8 | 2292 | //function : intSegSeg |
2293 | //purpose : Checks intersection between the two segments. | |
90dc2e5b | 2294 | //============================================================================= |
fc9b36d6 | 2295 | BRepMesh_GeomTool::IntFlag BRepMesh_Delaun::intSegSeg( |
01a6e62b | 2296 | const BRepMesh_Edge& theEdg1, |
2297 | const BRepMesh_Edge& theEdg2, | |
2298 | const Standard_Boolean isConsiderEndPointTouch, | |
2299 | const Standard_Boolean isConsiderPointOnEdge, | |
2300 | gp_Pnt2d& theIntPnt) const | |
90dc2e5b | 2301 | { |
2302 | gp_XY p1, p2, p3, p4; | |
2303 | p1 = GetVertex( theEdg1.FirstNode() ).Coord(); | |
2304 | p2 = GetVertex( theEdg1.LastNode() ).Coord(); | |
2305 | p3 = GetVertex( theEdg2.FirstNode() ).Coord(); | |
2306 | p4 = GetVertex( theEdg2.LastNode() ).Coord(); | |
2307 | ||
fc9b36d6 | 2308 | return BRepMesh_GeomTool::IntSegSeg(p1, p2, p3, p4, |
01a6e62b | 2309 | isConsiderEndPointTouch, isConsiderPointOnEdge, theIntPnt); |
304c45c8 | 2310 | } |
2311 | ||
2312 | //============================================================================= | |
2313 | //function : polyArea | |
2314 | //purpose : Returns area of the loop of the given polygon defined by indices | |
2315 | // of its start and end links. | |
2316 | //============================================================================= | |
848fa7e3 | 2317 | Standard_Real BRepMesh_Delaun::polyArea(const BRepMesh::SequenceOfInteger& thePolygon, |
2318 | const Standard_Integer theStartIndex, | |
2319 | const Standard_Integer theEndIndex) const | |
304c45c8 | 2320 | { |
2321 | Standard_Real aArea = 0.0; | |
2322 | Standard_Integer aPolyLen = thePolygon.Length(); | |
2323 | if ( theStartIndex >= theEndIndex || | |
2324 | theStartIndex > aPolyLen ) | |
2325 | { | |
2326 | return aArea; | |
2327 | } | |
304c45c8 | 2328 | Standard_Integer aCurEdgeInfo = thePolygon( theStartIndex ); |
2329 | Standard_Integer aCurEdgeId = Abs( aCurEdgeInfo ); | |
2330 | const BRepMesh_Edge* aCurEdge = &GetEdge( aCurEdgeId ); | |
2331 | ||
2332 | Standard_Integer aNodes[2]; | |
2333 | getOrientedNodes( *aCurEdge, aCurEdgeInfo > 0, aNodes ); | |
2334 | ||
2335 | gp_Pnt2d aRefPnt = GetVertex( aNodes[0] ).Coord(); | |
2336 | Standard_Integer aPolyIt = theStartIndex + 1; | |
2337 | for ( ; aPolyIt <= theEndIndex; ++aPolyIt ) | |
2338 | { | |
2339 | aCurEdgeInfo = thePolygon( aPolyIt ); | |
2340 | aCurEdgeId = Abs( aCurEdgeInfo ); | |
2341 | aCurEdge = &GetEdge( aCurEdgeId ); | |
2342 | ||
2343 | getOrientedNodes( *aCurEdge, aCurEdgeInfo > 0, aNodes ); | |
2344 | gp_Vec2d aVec1( aRefPnt, GetVertex( aNodes[0] ).Coord() ); | |
2345 | gp_Vec2d aVec2( aRefPnt, GetVertex( aNodes[1] ).Coord() ); | |
2346 | ||
2347 | aArea += aVec1 ^ aVec2; | |
2348 | } | |
2349 | ||
2350 | return aArea / 2.; | |
90dc2e5b | 2351 | } |
71316196 | 2352 | |
2353 | #ifdef DEB | |
2354 | //======================================================================= | |
2355 | //function : BRepMesh_DumpPoly | |
2356 | //purpose : | |
2357 | //======================================================================= | |
2358 | #include <TopoDS_Compound.hxx> | |
2359 | #include <BRep_Builder.hxx> | |
2360 | #include <Standard_ErrorHandler.hxx> | |
2361 | #include <BRepBuilderAPI_MakeEdge.hxx> | |
2362 | #include <BRepTools.hxx> | |
2363 | Standard_CString BRepMesh_DumpPoly(void* thePolygon, | |
2364 | void* theMeshHandlePtr, | |
2365 | Standard_CString theFileNameStr) | |
2366 | { | |
2367 | if (thePolygon == 0 || theFileNameStr == 0) | |
2368 | { | |
2369 | return "Error: file name or polygon data is null"; | |
2370 | } | |
2371 | ||
2372 | BRepMesh::SequenceOfInteger& aPolygon = *(BRepMesh::SequenceOfInteger*)thePolygon; | |
2373 | ||
2374 | Handle(BRepMesh_DataStructureOfDelaun) aMeshData = | |
2375 | *(Handle(BRepMesh_DataStructureOfDelaun)*)theMeshHandlePtr; | |
2376 | ||
2377 | if (aMeshData.IsNull()) | |
2378 | return "Error: mesh data is empty"; | |
2379 | ||
2380 | TopoDS_Compound aMesh; | |
2381 | BRep_Builder aBuilder; | |
2382 | aBuilder.MakeCompound(aMesh); | |
2383 | ||
2384 | try | |
2385 | { | |
2386 | OCC_CATCH_SIGNALS | |
2387 | ||
2388 | BRepMesh::SequenceOfInteger::Iterator aLinksIt(aPolygon); | |
2389 | for (; aLinksIt.More(); aLinksIt.Next()) | |
2390 | { | |
2391 | const BRepMesh_Edge& aLink = aMeshData->GetLink(Abs(aLinksIt.Value())); | |
2392 | ||
2393 | gp_Pnt aPnt[2]; | |
2394 | for (Standard_Integer i = 0; i < 2; ++i) | |
2395 | { | |
2396 | const Standard_Integer aNodeId = | |
2397 | (i == 0) ? aLink.FirstNode() : aLink.LastNode(); | |
2398 | ||
2399 | const gp_XY& aNode = aMeshData->GetNode(aNodeId).Coord(); | |
2400 | aPnt[i] = gp_Pnt(aNode.X(), aNode.Y(), 0.); | |
2401 | } | |
2402 | ||
2403 | if (aPnt[0].SquareDistance(aPnt[1]) < Precision::SquareConfusion()) | |
2404 | continue; | |
2405 | ||
2406 | aBuilder.Add(aMesh, BRepBuilderAPI_MakeEdge(aPnt[0], aPnt[1])); | |
2407 | } | |
2408 | ||
2409 | if (!BRepTools::Write(aMesh, theFileNameStr)) | |
2410 | return "Error: write failed"; | |
2411 | } | |
2412 | catch (Standard_Failure) | |
2413 | { | |
2414 | return Standard_Failure::Caught()->GetMessageString(); | |
2415 | } | |
2416 | ||
2417 | return theFileNameStr; | |
2418 | } | |
2419 | #endif |