1 // Created on: 1993-05-12
2 // Created by: Didier PIFFAULT
3 // Copyright (c) 1993-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
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
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.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 #include <BRepMesh_Delaun.hxx>
21 #include <gp_Pnt2d.hxx>
22 #include <gp_Vec2d.hxx>
24 #include <Precision.hxx>
25 #include <Bnd_Box2d.hxx>
26 #include <Bnd_B2d.hxx>
28 #include <BRepMesh_SelectorOfDataStructureOfDelaun.hxx>
30 #include <BRepMesh_Edge.hxx>
31 #include <BRepMesh_Vertex.hxx>
32 #include <BRepMesh_Triangle.hxx>
34 #include <NCollection_Vector.hxx>
38 const Standard_Real AngDeviation1Deg = M_PI/180.;
39 const Standard_Real AngDeviation90Deg = 90 * AngDeviation1Deg;
40 const Standard_Real Angle2PI = 2 * M_PI;
42 const Standard_Real Precision = Precision::PConfusion();
43 const Standard_Real Precision2 = Precision * Precision;
46 //! Sort two points in projection on vector (1,1)
47 struct ComparatorOfVertexOfDelaun
49 bool operator() (const BRepMesh_Vertex& theLeft, const BRepMesh_Vertex& theRight)
51 return theLeft.Coord().X() + theLeft.Coord().Y() < theRight.Coord().X() + theRight.Coord().Y();
55 //! Sort two points in projection on vector (1,1)
56 struct ComparatorOfIndexedVertexOfDelaun
59 ComparatorOfIndexedVertexOfDelaun (const Handle(BRepMesh_DataStructureOfDelaun)& theDS)
60 : myStructure(theDS) {}
62 bool operator() (Standard_Integer theLeft, Standard_Integer theRight)
64 const BRepMesh_Vertex& aLeft = myStructure->GetNode(theLeft);
65 const BRepMesh_Vertex& aRight = myStructure->GetNode(theRight);
66 return ComparatorOfVertexOfDelaun() (aLeft, aRight);
70 Handle(BRepMesh_DataStructureOfDelaun) myStructure;
73 inline void UpdateBndBox(const gp_XY& thePnt1, const gp_XY& thePnt2, Bnd_B2d& theBox)
75 theBox.Add( thePnt1 );
76 theBox.Add( thePnt2 );
77 theBox.Enlarge(Precision);
79 } // anonymous namespace
81 //=======================================================================
82 //function : BRepMesh_Delaun
83 //purpose : Creates the triangulation with an empty Mesh data structure
84 //=======================================================================
85 BRepMesh_Delaun::BRepMesh_Delaun(BRepMesh::Array1OfVertexOfDelaun& theVertices)
86 : myCircles (theVertices.Length(), new NCollection_IncAllocator(
87 BRepMesh::MEMORY_BLOCK_SIZE_HUGE))
89 if ( theVertices.Length() > 2 )
91 myMeshData = new BRepMesh_DataStructureOfDelaun(
92 new NCollection_IncAllocator(BRepMesh::MEMORY_BLOCK_SIZE_HUGE),
93 theVertices.Length() );
98 //=======================================================================
99 //function : BRepMesh_Delaun
100 //purpose : Creates the triangulation with and existent Mesh data structure
101 //=======================================================================
102 BRepMesh_Delaun::BRepMesh_Delaun(
103 const Handle( BRepMesh_DataStructureOfDelaun )& theOldMesh,
104 BRepMesh::Array1OfVertexOfDelaun& theVertices)
105 : myMeshData( theOldMesh ),
106 myCircles ( theVertices.Length(), theOldMesh->Allocator() )
108 if ( theVertices.Length() > 2 )
112 //=======================================================================
113 //function : BRepMesh_Delaun
114 //purpose : Creates the triangulation with and existent Mesh data structure
115 //=======================================================================
116 BRepMesh_Delaun::BRepMesh_Delaun(
117 const Handle( BRepMesh_DataStructureOfDelaun )& theOldMesh,
118 BRepMesh::Array1OfInteger& theVertexIndices)
119 : myMeshData( theOldMesh ),
120 myCircles ( theVertexIndices.Length(), theOldMesh->Allocator() )
122 if ( theVertexIndices.Length() > 2 )
125 Standard_Integer anIndex = theVertexIndices.Lower();
126 Standard_Integer anUpper = theVertexIndices.Upper();
127 for ( ; anIndex <= anUpper; ++anIndex )
128 aBox.Add( gp_Pnt2d( GetVertex( theVertexIndices( anIndex) ).Coord() ) );
130 perform( aBox, theVertexIndices );
134 //=======================================================================
136 //purpose : Initializes the triangulation with an Array of Vertex
137 //=======================================================================
138 void BRepMesh_Delaun::Init(BRepMesh::Array1OfVertexOfDelaun& theVertices)
141 Standard_Integer aLowerIdx = theVertices.Lower();
142 Standard_Integer anUpperIdx = theVertices.Upper();
143 BRepMesh::Array1OfInteger aVertexIndexes( aLowerIdx, anUpperIdx );
145 Standard_Integer anIndex = aLowerIdx;
146 for ( ; anIndex <= anUpperIdx; ++anIndex )
148 aBox.Add( gp_Pnt2d( theVertices( anIndex ).Coord() ) );
149 aVertexIndexes( anIndex ) = myMeshData->AddNode( theVertices( anIndex ) );
152 perform( aBox, aVertexIndexes );
155 //=======================================================================
157 //purpose : Create super mesh and run triangulation procedure
158 //=======================================================================
159 void BRepMesh_Delaun::perform(Bnd_Box2d& theBndBox,
160 BRepMesh::Array1OfInteger& theVertexIndexes)
162 theBndBox.Enlarge( Precision );
163 superMesh( theBndBox );
165 ComparatorOfIndexedVertexOfDelaun aCmp(myMeshData);
166 std::make_heap(theVertexIndexes.begin(), theVertexIndexes.end(), aCmp);
167 std::sort_heap(theVertexIndexes.begin(), theVertexIndexes.end(), aCmp);
169 compute( theVertexIndexes );
172 //=======================================================================
173 //function : superMesh
174 //purpose : Build the super mesh
175 //=======================================================================
176 void BRepMesh_Delaun::superMesh( const Bnd_Box2d& theBox )
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;
183 Standard_Real aDeltaMin = Min( aDeltaX, aDeltaY );
184 Standard_Real aDeltaMax = Max( aDeltaX, aDeltaY );
185 Standard_Real aDelta = aDeltaX + aDeltaY;
187 myCircles.SetMinMaxSize( gp_XY( aMinX, aMinY ), gp_XY( aMaxX, aMaxY ) );
189 Standard_Integer aScaler = 2;
190 if ( myMeshData->NbNodes() > 100 )
192 else if( myMeshData->NbNodes() > 1000 )
195 myCircles.SetCellSize( aDeltaX / aScaler,
198 mySupVert[0] = myMeshData->AddNode(
199 BRepMesh_Vertex( ( aMinX + aMaxX ) / 2, aMaxY + aDeltaMax, BRepMesh_Free ) );
201 mySupVert[1] = myMeshData->AddNode(
202 BRepMesh_Vertex( aMinX - aDelta, aMinY - aDeltaMin, BRepMesh_Free ) );
204 mySupVert[2] = myMeshData->AddNode(
205 BRepMesh_Vertex( aMaxX + aDelta, aMinY - aDeltaMin, BRepMesh_Free ) );
207 Standard_Integer e[3];
208 Standard_Boolean o[3];
209 for (Standard_Integer aNodeId = 0; aNodeId < 3; ++aNodeId)
211 Standard_Integer aFirstNode = aNodeId;
212 Standard_Integer aLastNode = (aNodeId + 1) % 3;
213 Standard_Integer aLinkIndex = myMeshData->AddLink( BRepMesh_Edge(
214 mySupVert[aFirstNode], mySupVert[aLastNode], BRepMesh_Free ) );
216 e[aNodeId] = Abs(aLinkIndex);
217 o[aNodeId] = (aLinkIndex > 0);
220 mySupTrian = BRepMesh_Triangle(e, o, BRepMesh_Free);
223 //=======================================================================
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.
228 //=======================================================================
229 void BRepMesh_Delaun::deleteTriangle(const Standard_Integer theIndex,
230 BRepMesh::MapOfIntegerInteger& theLoopEdges )
232 myCircles.Delete( theIndex );
234 Standard_Integer e[3];
235 Standard_Boolean o[3];
236 GetTriangle( theIndex ).Edges( e, o );
238 myMeshData->RemoveElement( theIndex );
240 for ( Standard_Integer i = 0; i < 3; ++i )
242 if ( !theLoopEdges.Bind( e[i], o[i] ) )
244 theLoopEdges.UnBind( e[i] );
245 myMeshData->RemoveLink( e[i] );
250 //=======================================================================
252 //purpose : Computes the triangulation and add the vertices edges and
253 // triangles to the Mesh data structure
254 //=======================================================================
255 void BRepMesh_Delaun::compute(BRepMesh::Array1OfInteger& theVertexIndexes)
257 // Insertion of edges of super triangles in the list of free edges:
258 BRepMesh::MapOfIntegerInteger aLoopEdges(10, myMeshData->Allocator());
259 Standard_Integer e[3];
260 Standard_Boolean o[3];
261 mySupTrian.Edges( e, o );
263 aLoopEdges.Bind( e[0], Standard_True );
264 aLoopEdges.Bind( e[1], Standard_True );
265 aLoopEdges.Bind( e[2], Standard_True );
267 if ( theVertexIndexes.Length() > 0 )
269 // Creation of 3 trianglers with the first node and the edges of the super triangle:
270 Standard_Integer anVertexIdx = theVertexIndexes.Lower();
271 createTriangles( theVertexIndexes( anVertexIdx ), aLoopEdges );
273 // Add other nodes to the mesh
274 createTrianglesOnNewVertices( theVertexIndexes );
277 // Destruction of triangles containing a top of the super triangle
278 BRepMesh_SelectorOfDataStructureOfDelaun aSelector( myMeshData );
279 for (Standard_Integer aSupVertId = 0; aSupVertId < 3; ++aSupVertId)
280 aSelector.NeighboursOfNode( mySupVert[aSupVertId] );
283 BRepMesh::MapOfInteger::Iterator aFreeTriangles( aSelector.Elements() );
284 for ( ; aFreeTriangles.More(); aFreeTriangles.Next() )
285 deleteTriangle( aFreeTriangles.Key(), aLoopEdges );
287 // All edges that remain free are removed from aLoopEdges;
288 // only the boundary edges of the triangulation remain there
289 BRepMesh::MapOfIntegerInteger::Iterator aFreeEdges( aLoopEdges );
290 for ( ; aFreeEdges.More(); aFreeEdges.Next() )
292 if ( myMeshData->ElementsConnectedTo( aFreeEdges.Key() ).IsEmpty() )
293 myMeshData->RemoveLink( aFreeEdges.Key() );
296 // The tops of the super triangle are destroyed
297 for (Standard_Integer aSupVertId = 0; aSupVertId < 3; ++aSupVertId)
298 myMeshData->RemoveNode( mySupVert[aSupVertId] );
301 //=======================================================================
302 //function : createTriangles
303 //purpose : Creates the triangles beetween the node and the polyline.
304 //=======================================================================
305 void BRepMesh_Delaun::createTriangles(const Standard_Integer theVertexIndex,
306 BRepMesh::MapOfIntegerInteger& thePoly)
308 BRepMesh::ListOfInteger aLoopEdges, anExternalEdges;
309 const gp_XY& aVertexCoord = myMeshData->GetNode( theVertexIndex ).Coord();
311 BRepMesh::MapOfIntegerInteger::Iterator anEdges( thePoly );
312 for ( ; anEdges.More(); anEdges.Next() )
314 Standard_Integer anEdgeId = anEdges.Key();
315 const BRepMesh_Edge& anEdge = GetEdge( anEdgeId );
317 Standard_Boolean isPositive = (Standard_Boolean)thePoly( anEdgeId );
319 Standard_Integer aNodes[3];
322 aNodes[0] = anEdge.FirstNode();
323 aNodes[2] = anEdge.LastNode();
327 aNodes[0] = anEdge.LastNode();
328 aNodes[2] = anEdge.FirstNode();
330 aNodes[1] = theVertexIndex;
332 const BRepMesh_Vertex& aFirstVertex = GetVertex( aNodes[0] );
333 const BRepMesh_Vertex& aLastVertex = GetVertex( aNodes[2] );
335 gp_XY anEdgeDir( aLastVertex.Coord() - aFirstVertex.Coord() );
336 Standard_Real anEdgeLen = anEdgeDir.Modulus();
337 if ( anEdgeLen < Precision )
340 anEdgeDir.SetCoord( anEdgeDir.X() / anEdgeLen,
341 anEdgeDir.Y() / anEdgeLen );
343 gp_XY aFirstLinkDir( aFirstVertex.Coord() - aVertexCoord );
344 gp_XY aLastLinkDir ( aVertexCoord - aLastVertex.Coord() );
346 Standard_Real aDist12 = aFirstLinkDir ^ anEdgeDir;
347 Standard_Real aDist23 = anEdgeDir ^ aLastLinkDir;
348 if ( Abs( aDist12 ) < Precision ||
349 Abs( aDist23 ) < Precision )
354 BRepMesh_Edge aFirstLink( aNodes[1], aNodes[0], BRepMesh_Free );
355 BRepMesh_Edge aLastLink ( aNodes[2], aNodes[1], BRepMesh_Free );
357 Standard_Integer anEdgesInfo[3] = {
358 myMeshData->AddLink( aFirstLink ),
359 isPositive ? anEdgeId : -anEdgeId,
360 myMeshData->AddLink( aLastLink ) };
362 Standard_Boolean isSensOK = (aDist12 > 0. && aDist23 > 0.);
365 Standard_Integer anEdgeIds[3];
366 Standard_Boolean anEdgesOri[3];
367 for ( Standard_Integer aTriLinkIt = 0; aTriLinkIt < 3; ++aTriLinkIt )
369 const Standard_Integer& anEdgeInfo = anEdgesInfo[aTriLinkIt];
370 anEdgeIds[aTriLinkIt] = Abs( anEdgeInfo );
371 anEdgesOri[aTriLinkIt] = anEdgeInfo > 0;
374 addTriangle(anEdgeIds, anEdgesOri, aNodes );
379 aLoopEdges.Append( anEdges.Key() );
381 aLoopEdges.Append( -anEdges.Key() );
383 if ( aFirstLinkDir.SquareModulus() > aLastLinkDir.SquareModulus() )
384 anExternalEdges.Append( Abs( anEdgesInfo[0] ) );
386 anExternalEdges.Append( Abs( anEdgesInfo[2] ) );
391 while ( !anExternalEdges.IsEmpty() )
393 const BRepMesh_PairOfIndex& aPair =
394 myMeshData->ElementsConnectedTo( Abs( anExternalEdges.First() ) );
397 if ( !aPair.IsEmpty() )
398 deleteTriangle( aPair.FirstIndex(), thePoly );
400 anExternalEdges.RemoveFirst();
403 for ( anEdges.Initialize( thePoly ); anEdges.More(); anEdges.Next() )
405 if ( myMeshData->ElementsConnectedTo( anEdges.Key() ).IsEmpty() )
406 myMeshData->RemoveLink( anEdges.Key() );
409 while ( !aLoopEdges.IsEmpty() )
411 const BRepMesh_Edge& anEdge = GetEdge( Abs( aLoopEdges.First() ) );
412 if ( anEdge.Movability() != BRepMesh_Deleted )
414 Standard_Integer anEdgeIdx = aLoopEdges.First();
415 meshLeftPolygonOf( Abs( anEdgeIdx ), ( anEdgeIdx > 0 ) );
418 aLoopEdges.RemoveFirst();
422 //=======================================================================
423 //function : createTrianglesOnNewVertices
424 //purpose : Creation of triangles from the new nodes
425 //=======================================================================
426 void BRepMesh_Delaun::createTrianglesOnNewVertices(
427 BRepMesh::Array1OfInteger& theVertexIndexes)
429 Handle(NCollection_IncAllocator) aAllocator =
430 new NCollection_IncAllocator(BRepMesh::MEMORY_BLOCK_SIZE_HUGE);
432 Standard_Real aTolU, aTolV;
433 myMeshData->Data()->GetTolerance(aTolU, aTolV);
434 const Standard_Real aSqTol = aTolU * aTolU + aTolV * aTolV;
436 // Insertion of nodes :
437 Standard_Boolean isModify = Standard_True;
439 Standard_Integer anIndex = theVertexIndexes.Lower();
440 Standard_Integer anUpper = theVertexIndexes.Upper();
441 for( ; anIndex <= anUpper; ++anIndex )
443 aAllocator->Reset(Standard_False);
444 BRepMesh::MapOfIntegerInteger aLoopEdges(10, aAllocator);
446 Standard_Integer aVertexIdx = theVertexIndexes( anIndex );
447 const BRepMesh_Vertex& aVertex = GetVertex( aVertexIdx );
449 // Iterator in the list of indexes of circles containing the node
450 BRepMesh::ListOfInteger& aCirclesList = myCircles.Select( aVertex.Coord() );
452 Standard_Integer onEgdeId = 0, aTriangleId = 0;
453 BRepMesh::ListOfInteger::Iterator aCircleIt( aCirclesList );
454 for ( ; aCircleIt.More(); aCircleIt.Next() )
456 // To add a node in the mesh it is necessary to check conditions:
457 // - the node should be within the boundaries of the mesh and so in an existing triangle
458 // - all adjacent triangles should belong to a component connected with this triangle
459 if ( Contains( aCircleIt.Value(), aVertex, aSqTol, onEgdeId ) )
461 if (onEgdeId != 0 && GetEdge(onEgdeId).Movability() != BRepMesh_Free)
463 // We can skip free vertex too close to the frontier edge.
464 if (aVertex.Movability() == BRepMesh_Free)
467 // However, we should add vertex that have neighboring frontier edges.
470 // Remove triangle even if it contains frontier edge in order
471 // to prevent appearance of incorrect configurations like free
472 // edge glued with frontier #26407
473 aTriangleId = aCircleIt.Value();
474 aCirclesList.Remove( aCircleIt );
479 if ( aTriangleId > 0 )
481 deleteTriangle( aTriangleId, aLoopEdges );
483 isModify = Standard_True;
484 while ( isModify && !aCirclesList.IsEmpty() )
486 isModify = Standard_False;
487 BRepMesh::ListOfInteger::Iterator aCircleIt1( aCirclesList );
488 for ( ; aCircleIt1.More(); aCircleIt1.Next() )
490 Standard_Integer e[3];
491 Standard_Boolean o[3];
492 GetTriangle( aCircleIt1.Value() ).Edges( e, o );
494 if ( aLoopEdges.IsBound( e[0] ) ||
495 aLoopEdges.IsBound( e[1] ) ||
496 aLoopEdges.IsBound( e[2] ) )
498 isModify = Standard_True;
499 deleteTriangle( aCircleIt1.Value(), aLoopEdges );
500 aCirclesList.Remove( aCircleIt1 );
506 // Creation of triangles with the current node and free edges
507 // and removal of these edges from the list of free edges
508 createTriangles( aVertexIdx, aLoopEdges );
511 // Check that internal edges are not crossed by triangles
512 BRepMesh::HMapOfInteger anInternalEdges = InternalEdges();
514 // Destruction of triancles intersecting internal edges
515 // and their replacement by makeshift triangles
516 BRepMesh::MapOfInteger::Iterator anInernalEdgesIt( *anInternalEdges );
517 for ( ; anInernalEdgesIt.More(); anInernalEdgesIt.Next() )
519 Standard_Integer aNbC;
520 aNbC = myMeshData->ElementsConnectedTo( anInernalEdgesIt.Key() ).Extent();
523 meshLeftPolygonOf( anInernalEdgesIt.Key(), Standard_True );
524 meshLeftPolygonOf( anInernalEdgesIt.Key(), Standard_False );
528 // Adjustment of meshes to boundary edges
532 //=======================================================================
533 //function : isBoundToFrontier
534 //purpose : Goes through the neighbour triangles around the given node
535 // started from the given link, returns TRUE if some triangle
536 // has a bounding frontier edge or FALSE elsewhere.
537 // Stop link is used to prevent cycles.
538 // Previous element Id is used to identify next neighbor element.
539 //=======================================================================
540 Standard_Boolean BRepMesh_Delaun::isBoundToFrontier(
541 const Standard_Integer theRefNodeId,
542 const Standard_Integer theRefLinkId,
543 const Standard_Integer theStopLinkId,
544 const Standard_Integer thePrevElementId)
546 const BRepMesh_PairOfIndex& aPair =
547 myMeshData->ElementsConnectedTo( theRefLinkId );
548 if ( aPair.IsEmpty() )
549 return Standard_False;
551 Standard_Integer aNbElements = aPair.Extent();
552 for ( Standard_Integer anElemIt = 1; anElemIt <= aNbElements; ++anElemIt )
554 const Standard_Integer aTriId = aPair.Index(anElemIt);
555 if ( aTriId < 0 || aTriId == thePrevElementId )
558 Standard_Integer anEdges[3];
559 Standard_Boolean anEdgesOri[3];
560 GetTriangle( aTriId ).Edges( anEdges, anEdgesOri );
562 for ( Standard_Integer anEdgeIt = 0; anEdgeIt < 3; ++anEdgeIt )
564 const Standard_Integer anEdgeId = anEdges[anEdgeIt];
565 if ( anEdgeId == theRefLinkId )
568 if ( anEdgeId == theStopLinkId )
569 return Standard_False;
571 const BRepMesh_Edge& anEdge = GetEdge( anEdgeId );
572 if ( anEdge.FirstNode() != theRefNodeId &&
573 anEdge.LastNode() != theRefNodeId )
578 if ( anEdge.Movability() != BRepMesh_Free )
579 return Standard_True;
581 return isBoundToFrontier( theRefNodeId, anEdgeId, theStopLinkId, aTriId );
585 return Standard_False;
588 //=======================================================================
589 //function : cleanupMesh
590 //purpose : Cleanup mesh from the free triangles
591 //=======================================================================
592 void BRepMesh_Delaun::cleanupMesh()
594 Handle(NCollection_IncAllocator) aAllocator =
595 new NCollection_IncAllocator(BRepMesh::MEMORY_BLOCK_SIZE_HUGE);
599 aAllocator->Reset(Standard_False);
600 BRepMesh::MapOfIntegerInteger aLoopEdges(10, aAllocator);
601 BRepMesh::MapOfInteger aDelTriangles(10, aAllocator);
603 BRepMesh::HMapOfInteger aFreeEdges = FreeEdges();
604 BRepMesh::MapOfInteger::Iterator aFreeEdgesIt( *aFreeEdges );
605 for ( ; aFreeEdgesIt.More(); aFreeEdgesIt.Next() )
607 const Standard_Integer& aFreeEdgeId = aFreeEdgesIt.Key();
608 const BRepMesh_Edge& anEdge = GetEdge( aFreeEdgeId );
609 if ( anEdge.Movability() == BRepMesh_Frontier )
612 const BRepMesh_PairOfIndex& aPair =
613 myMeshData->ElementsConnectedTo( aFreeEdgeId );
614 if ( aPair.IsEmpty() )
616 aLoopEdges.Bind( aFreeEdgeId, Standard_True );
620 Standard_Integer aTriId = aPair.FirstIndex();
622 // Check that the connected triangle is not surrounded by another triangles
623 Standard_Integer anEdges[3];
624 Standard_Boolean anEdgesOri[3];
625 GetTriangle( aTriId ).Edges( anEdges, anEdgesOri );
627 Standard_Boolean isCanNotBeRemoved = Standard_True;
628 for ( Standard_Integer aCurEdgeIdx = 0; aCurEdgeIdx < 3; ++aCurEdgeIdx )
630 if ( anEdges[aCurEdgeIdx] != aFreeEdgeId )
633 for ( Standard_Integer anOtherEdgeIt = 1; anOtherEdgeIt <= 2; ++anOtherEdgeIt )
635 Standard_Integer anOtherEdgeId = ( aCurEdgeIdx + anOtherEdgeIt ) % 3;
636 const BRepMesh_PairOfIndex& anOtherEdgePair =
637 myMeshData->ElementsConnectedTo( anEdges[anOtherEdgeId] );
639 if ( anOtherEdgePair.Extent() < 2 )
641 isCanNotBeRemoved = Standard_False;
649 if ( isCanNotBeRemoved )
652 Standard_Boolean isConnected[2] = { Standard_False, Standard_False };
653 for ( Standard_Integer aLinkNodeIt = 0; aLinkNodeIt < 2; ++aLinkNodeIt )
655 isConnected[aLinkNodeIt] = isBoundToFrontier( ( aLinkNodeIt == 0 ) ?
656 anEdge.FirstNode() : anEdge.LastNode(),
657 aFreeEdgeId, aFreeEdgeId, -1);
660 if ( !isConnected[0] || !isConnected[1] )
661 aDelTriangles.Add( aTriId );
664 // Destruction of triangles :
665 Standard_Integer aDeletedTrianglesNb = 0;
666 BRepMesh::MapOfInteger::Iterator aDelTrianglesIt( aDelTriangles );
667 for ( ; aDelTrianglesIt.More(); aDelTrianglesIt.Next() )
669 deleteTriangle( aDelTrianglesIt.Key(), aLoopEdges );
670 aDeletedTrianglesNb++;
673 // Destruction of remaining hanging edges
674 BRepMesh::MapOfIntegerInteger::Iterator aLoopEdgesIt( aLoopEdges );
675 for ( ; aLoopEdgesIt.More(); aLoopEdgesIt.Next() )
677 if ( myMeshData->ElementsConnectedTo( aLoopEdgesIt.Key() ).IsEmpty() )
678 myMeshData->RemoveLink( aLoopEdgesIt.Key() );
681 if ( aDeletedTrianglesNb == 0 )
686 //=======================================================================
687 //function : frontierAdjust
688 //purpose : Adjust the mesh on the frontier
689 //=======================================================================
690 void BRepMesh_Delaun::frontierAdjust()
692 BRepMesh::HMapOfInteger aFrontier = Frontier();
694 Handle(NCollection_IncAllocator) aAllocator =
695 new NCollection_IncAllocator(BRepMesh::MEMORY_BLOCK_SIZE_HUGE);
697 BRepMesh::VectorOfInteger aFailedFrontiers(256, aAllocator);
698 BRepMesh::MapOfIntegerInteger aLoopEdges(10, aAllocator);
699 BRepMesh::HMapOfInteger aIntFrontierEdges =
700 new BRepMesh::MapOfInteger(10, aAllocator);
702 for ( Standard_Integer aPass = 1; aPass <= 2; ++aPass )
704 // 1 pass): find external triangles on boundary edges;
705 // 2 pass): find external triangles on boundary edges appeared
706 // during triangles replacement.
708 BRepMesh::MapOfInteger::Iterator aFrontierIt( *aFrontier );
709 for ( ; aFrontierIt.More(); aFrontierIt.Next() )
711 Standard_Integer aFrontierId = aFrontierIt.Key();
712 const BRepMesh_PairOfIndex& aPair = myMeshData->ElementsConnectedTo( aFrontierId );
713 Standard_Integer aNbElem = aPair.Extent();
714 for( Standard_Integer aElemIt = 1; aElemIt <= aNbElem; ++aElemIt )
716 const Standard_Integer aPriorElemId = aPair.Index( aElemIt );
717 if( aPriorElemId < 0 )
720 Standard_Integer e[3];
721 Standard_Boolean o[3];
722 GetTriangle( aPriorElemId ).Edges( e, o );
724 Standard_Boolean isTriangleFound = Standard_False;
725 for ( Standard_Integer n = 0; n < 3; ++n )
727 if ( aFrontierId == e[n] && !o[n] )
729 // Destruction of external triangles on boundary edges
730 isTriangleFound = Standard_True;
731 deleteTriangle( aPriorElemId, aLoopEdges );
736 if ( isTriangleFound )
741 // destrucrion of remaining hanging edges :
742 BRepMesh::MapOfIntegerInteger::Iterator aLoopEdgesIt( aLoopEdges );
743 for ( ; aLoopEdgesIt.More(); aLoopEdgesIt.Next() )
745 Standard_Integer aLoopEdgeId = aLoopEdgesIt.Key();
746 if (myMeshData->ElementsConnectedTo( aLoopEdgeId ).IsEmpty() )
747 myMeshData->RemoveLink( aLoopEdgeId );
750 // destruction of triangles crossing the boundary edges and
751 // their replacement by makeshift triangles
752 for ( aFrontierIt.Reset(); aFrontierIt.More(); aFrontierIt.Next() )
754 Standard_Integer aFrontierId = aFrontierIt.Key();
755 if ( !myMeshData->ElementsConnectedTo( aFrontierId ).IsEmpty() )
758 Standard_Boolean isSuccess =
759 meshLeftPolygonOf( aFrontierId, Standard_True, aIntFrontierEdges );
761 if ( aPass == 2 && !isSuccess )
762 aFailedFrontiers.Append( aFrontierId );
768 // When the mesh has been cleaned up, try to process frontier edges
769 // once again to fill the possible gaps that might be occured in case of "saw" -
770 // situation when frontier edge has a triangle at a right side, but its free
771 // links cross another frontieres and meshLeftPolygonOf itself can't collect
773 BRepMesh::VectorOfInteger::Iterator aFailedFrontiersIt( aFailedFrontiers );
774 for ( ; aFailedFrontiersIt.More(); aFailedFrontiersIt.Next() )
776 Standard_Integer aFrontierId = aFailedFrontiersIt.Value();
777 if ( !myMeshData->ElementsConnectedTo( aFrontierId ).IsEmpty() )
780 meshLeftPolygonOf( aFrontierId, Standard_True, aIntFrontierEdges );
784 //=======================================================================
785 //function : fillBndBox
786 //purpose : Add boundig box for edge defined by start & end point to
787 // the given vector of bounding boxes for triangulation edges
788 //=======================================================================
789 void BRepMesh_Delaun::fillBndBox(BRepMesh::SequenceOfBndB2d& theBoxes,
790 const BRepMesh_Vertex& theV1,
791 const BRepMesh_Vertex& theV2)
794 UpdateBndBox(theV1.Coord(), theV2.Coord(), aBox);
795 theBoxes.Append( aBox );
798 //=======================================================================
799 //function : meshLeftPolygonOf
800 //purpose : Collect the polygon at the left of the given edge (material side)
801 //=======================================================================
802 Standard_Boolean BRepMesh_Delaun::meshLeftPolygonOf(
803 const Standard_Integer theStartEdgeId,
804 const Standard_Boolean isForward,
805 BRepMesh::HMapOfInteger theSkipped )
807 if ( !theSkipped.IsNull() && theSkipped->Contains( theStartEdgeId ) )
808 return Standard_True;
810 const BRepMesh_Edge& aRefEdge = GetEdge( theStartEdgeId );
812 BRepMesh::SequenceOfInteger aPolygon;
813 Standard_Integer aStartNode, aPivotNode;
816 aPolygon.Append( theStartEdgeId );
817 aStartNode = aRefEdge.FirstNode();
818 aPivotNode = aRefEdge.LastNode();
822 aPolygon.Append( -theStartEdgeId );
823 aStartNode = aRefEdge.LastNode();
824 aPivotNode = aRefEdge.FirstNode();
828 const BRepMesh_Vertex& aStartEdgeVertexS = GetVertex( aStartNode );
829 BRepMesh_Vertex aPivotVertex = GetVertex( aPivotNode );
831 gp_Vec2d aRefLinkDir( aPivotVertex.Coord() -
832 aStartEdgeVertexS.Coord() );
834 if ( aRefLinkDir.SquareMagnitude() < Precision2 )
835 return Standard_True;
837 // Auxilary structures.
838 // Bounding boxes of polygon links to be used for preliminary
839 // analysis of intersections
840 BRepMesh::SequenceOfBndB2d aBoxes;
841 fillBndBox( aBoxes, aStartEdgeVertexS, aPivotVertex );
844 BRepMesh::MapOfInteger aDeadLinks;
846 // Links are temporarily excluded from consideration
847 BRepMesh::MapOfInteger aLeprousLinks;
848 aLeprousLinks.Add( theStartEdgeId );
850 Standard_Boolean isSkipLeprous = Standard_True;
851 Standard_Integer aFirstNode = aStartNode;
852 while ( aPivotNode != aFirstNode )
854 Bnd_B2d aNextLinkBndBox;
855 gp_Vec2d aNextLinkDir;
856 Standard_Integer aNextPivotNode = 0;
858 Standard_Integer aNextLinkId = findNextPolygonLink(
860 aPivotNode, aPivotVertex, aRefLinkDir,
861 aBoxes, aPolygon, theSkipped,
862 isSkipLeprous, aLeprousLinks, aDeadLinks,
863 aNextPivotNode, aNextLinkDir, aNextLinkBndBox );
865 if ( aNextLinkId != 0 )
867 aStartNode = aPivotNode;
868 aRefLinkDir = aNextLinkDir;
870 aPivotNode = aNextPivotNode;
871 aPivotVertex = GetVertex( aNextPivotNode );
873 aBoxes.Append ( aNextLinkBndBox );
874 aPolygon.Append( aNextLinkId );
876 isSkipLeprous = Standard_True;
881 if ( aPolygon.Length() == 1 )
882 return Standard_False;
884 // Return to the previous point
885 Standard_Integer aDeadLinkId = Abs( aPolygon.Last() );
886 aDeadLinks.Add ( aDeadLinkId );
888 aLeprousLinks.Remove( aDeadLinkId );
889 aPolygon.Remove ( aPolygon.Length() );
890 aBoxes.Remove ( aBoxes.Length() );
892 Standard_Integer aPrevLinkInfo = aPolygon.Last();
893 const BRepMesh_Edge& aPrevLink = GetEdge( Abs( aPrevLinkInfo ) );
895 if( aPrevLinkInfo > 0 )
897 aStartNode = aPrevLink.FirstNode();
898 aPivotNode = aPrevLink.LastNode();
902 aStartNode = aPrevLink.LastNode();
903 aPivotNode = aPrevLink.FirstNode();
906 aPivotVertex = GetVertex( aPivotNode );
908 aPivotVertex.Coord() - GetVertex( aStartNode ).Coord();
910 isSkipLeprous = Standard_False;
914 if ( aPolygon.Length() < 3 )
915 return Standard_False;
917 cleanupPolygon( aPolygon, aBoxes );
918 meshPolygon ( aPolygon, aBoxes, theSkipped );
920 return Standard_True;
923 //=======================================================================
924 //function : findNextPolygonLink
925 //purpose : Find next link starting from the given node and has maximum
926 // angle respect the given reference link.
927 // Each time the next link is found other neighbor links at the
928 // pivot node are marked as leprous and will be excluded from
929 // consideration next time until a hanging end is occured.
930 //=======================================================================
931 Standard_Integer BRepMesh_Delaun::findNextPolygonLink(
932 const Standard_Integer& theFirstNode,
933 const Standard_Integer& thePivotNode,
934 const BRepMesh_Vertex& thePivotVertex,
935 const gp_Vec2d& theRefLinkDir,
936 const BRepMesh::SequenceOfBndB2d& theBoxes,
937 const BRepMesh::SequenceOfInteger& thePolygon,
938 const BRepMesh::HMapOfInteger theSkipped,
939 const Standard_Boolean& isSkipLeprous,
940 BRepMesh::MapOfInteger& theLeprousLinks,
941 BRepMesh::MapOfInteger& theDeadLinks,
942 Standard_Integer& theNextPivotNode,
943 gp_Vec2d& theNextLinkDir,
944 Bnd_B2d& theNextLinkBndBox )
946 // Find the next link having the greatest angle
947 // respect to a direction of a reference one
948 Standard_Real aMaxAngle = RealFirst();
950 Standard_Integer aNextLinkId = 0;
951 BRepMesh::ListOfInteger::Iterator aLinkIt( myMeshData->LinksConnectedTo( thePivotNode ) );
952 for ( ; aLinkIt.More(); aLinkIt.Next() )
954 const Standard_Integer& aNeighbourLinkInfo = aLinkIt.Value();
955 Standard_Integer aNeighbourLinkId = Abs( aNeighbourLinkInfo );
957 if ( theDeadLinks.Contains( aNeighbourLinkId ) ||
958 ( !theSkipped.IsNull() && theSkipped->Contains( aNeighbourLinkId ) ) )
963 Standard_Boolean isLeprous = theLeprousLinks.Contains( aNeighbourLinkId );
964 if ( isSkipLeprous && isLeprous )
967 const BRepMesh_Edge& aNeighbourLink = GetEdge( aNeighbourLinkId );
969 // Determine whether the link belongs to the mesh
970 if ( aNeighbourLink.Movability() == BRepMesh_Free &&
971 myMeshData->ElementsConnectedTo( aNeighbourLinkInfo ).IsEmpty() )
973 theDeadLinks.Add( aNeighbourLinkId );
977 Standard_Integer anOtherNode = aNeighbourLink.FirstNode();
978 if ( anOtherNode == thePivotNode )
979 anOtherNode = aNeighbourLink.LastNode();
981 gp_Vec2d aCurLinkDir( GetVertex( anOtherNode ).Coord() -
982 thePivotVertex.Coord() );
984 if ( aCurLinkDir.SquareMagnitude() < Precision2 )
986 theDeadLinks.Add( aNeighbourLinkId );
991 theLeprousLinks.Add( aNeighbourLinkId );
993 Standard_Real anAngle = theRefLinkDir.Angle( aCurLinkDir );
994 Standard_Boolean isFrontier =
995 ( aNeighbourLink.Movability() == BRepMesh_Frontier );
997 Standard_Boolean isCheckPointOnEdge = Standard_True;
1000 if ( Abs( Abs(anAngle) - M_PI ) < Precision::Angular() )
1002 // Glued constrains - don't check intersection
1003 isCheckPointOnEdge = Standard_False;
1004 anAngle = Abs( anAngle );
1008 if (anAngle <= aMaxAngle)
1011 Standard_Boolean isCheckEndPoints = ( anOtherNode != theFirstNode );
1014 Standard_Boolean isNotIntersect =
1015 checkIntersection( aNeighbourLink, thePolygon, theBoxes,
1016 isCheckEndPoints, isCheckPointOnEdge, Standard_True, aBox );
1018 if( isNotIntersect )
1020 aMaxAngle = anAngle;
1022 theNextLinkDir = aCurLinkDir;
1023 theNextPivotNode = anOtherNode;
1024 theNextLinkBndBox = aBox;
1026 aNextLinkId = ( aNeighbourLink.FirstNode() == thePivotNode ) ?
1027 aNeighbourLinkId : -aNeighbourLinkId;
1034 //=======================================================================
1035 //function : checkIntersection
1036 //purpose : Check is the given link intersects the polygon boundaries.
1037 // Returns bounding box for the given link trough the
1038 // <theLinkBndBox> parameter.
1039 //=======================================================================
1040 Standard_Boolean BRepMesh_Delaun::checkIntersection(
1041 const BRepMesh_Edge& theLink,
1042 const BRepMesh::SequenceOfInteger& thePolygon,
1043 const BRepMesh::SequenceOfBndB2d& thePolyBoxes,
1044 const Standard_Boolean isConsiderEndPointTouch,
1045 const Standard_Boolean isConsiderPointOnEdge,
1046 const Standard_Boolean isSkipLastEdge,
1047 Bnd_B2d& theLinkBndBox ) const
1049 UpdateBndBox(GetVertex(theLink.FirstNode()).Coord(),
1050 GetVertex(theLink.LastNode()).Coord(), theLinkBndBox);
1052 Standard_Integer aPolyLen = thePolygon.Length();
1053 // Don't check intersection with the last link
1054 if ( isSkipLastEdge )
1057 Standard_Boolean isFrontier =
1058 ( theLink.Movability() == BRepMesh_Frontier );
1060 for ( Standard_Integer aPolyIt = 1; aPolyIt <= aPolyLen; ++aPolyIt )
1062 if ( !theLinkBndBox.IsOut( thePolyBoxes.Value( aPolyIt ) ) )
1064 // intersection is possible...
1065 Standard_Integer aPolyLinkId = Abs( thePolygon( aPolyIt ) );
1066 const BRepMesh_Edge& aPolyLink = GetEdge( aPolyLinkId );
1068 // skip intersections between frontier edges
1069 if ( aPolyLink.Movability() == BRepMesh_Frontier && isFrontier )
1073 BRepMesh_GeomTool::IntFlag aIntFlag = intSegSeg( theLink, aPolyLink,
1074 isConsiderEndPointTouch, isConsiderPointOnEdge, anIntPnt );
1076 if ( aIntFlag != BRepMesh_GeomTool::NoIntersection )
1077 return Standard_False;
1081 // Ok, no intersection
1082 return Standard_True;
1085 //=======================================================================
1086 //function : addTriangle
1087 //purpose : Add a triangle based on the given oriented edges into mesh
1088 //=======================================================================
1089 inline void BRepMesh_Delaun::addTriangle( const Standard_Integer (&theEdgesId)[3],
1090 const Standard_Boolean (&theEdgesOri)[3],
1091 const Standard_Integer (&theNodesId)[3] )
1093 Standard_Integer aNewTriangleId =
1094 myMeshData->AddElement(BRepMesh_Triangle(theEdgesId,
1095 theEdgesOri, BRepMesh_Free));
1097 Standard_Boolean isAdded = myCircles.Bind(
1099 GetVertex( theNodesId[0] ).Coord(),
1100 GetVertex( theNodesId[1] ).Coord(),
1101 GetVertex( theNodesId[2] ).Coord() );
1104 myMeshData->RemoveElement( aNewTriangleId );
1107 //=======================================================================
1108 //function : cleanupPolygon
1109 //purpose : Remove internal triangles from the given polygon
1110 //=======================================================================
1111 void BRepMesh_Delaun::cleanupPolygon(const BRepMesh::SequenceOfInteger& thePolygon,
1112 const BRepMesh::SequenceOfBndB2d& thePolyBoxes )
1114 Standard_Integer aPolyLen = thePolygon.Length();
1118 Handle(NCollection_IncAllocator) aAllocator =
1119 new NCollection_IncAllocator(BRepMesh::MEMORY_BLOCK_SIZE_HUGE);
1121 BRepMesh::MapOfIntegerInteger aLoopEdges(10, aAllocator);
1122 BRepMesh::MapOfInteger anIgnoredEdges(10, aAllocator);
1123 BRepMesh::MapOfInteger aPolyVerticesFindMap(10, aAllocator);
1124 BRepMesh::VectorOfInteger aPolyVertices(256, aAllocator);
1125 // Collect boundary vertices of the polygon
1126 for ( Standard_Integer aPolyIt = 1; aPolyIt <= aPolyLen; ++aPolyIt )
1128 Standard_Integer aPolyEdgeInfo = thePolygon( aPolyIt );
1129 Standard_Integer aPolyEdgeId = Abs( aPolyEdgeInfo );
1130 anIgnoredEdges.Add( aPolyEdgeId );
1132 Standard_Boolean isForward = ( aPolyEdgeInfo > 0 );
1133 const BRepMesh_PairOfIndex& aPair =
1134 myMeshData->ElementsConnectedTo( aPolyEdgeId );
1136 Standard_Integer anElemIt = 1;
1137 for ( ; anElemIt <= aPair.Extent(); ++anElemIt )
1139 Standard_Integer anElemId = aPair.Index( anElemIt );
1143 Standard_Integer anEdges[3];
1144 Standard_Boolean anEdgesOri[3];
1145 GetTriangle( anElemId ).Edges(anEdges, anEdgesOri);
1147 Standard_Integer isTriangleFound = Standard_False;
1148 for ( Standard_Integer anEdgeIt = 0; anEdgeIt < 3; ++anEdgeIt )
1150 if ( anEdges[anEdgeIt] == aPolyEdgeId &&
1151 anEdgesOri[anEdgeIt] == isForward )
1153 isTriangleFound = Standard_True;
1154 deleteTriangle( anElemId, aLoopEdges );
1159 if ( isTriangleFound )
1163 // Skip a neighbor link to extract unique vertices each time
1166 const BRepMesh_Edge& aPolyEdge = GetEdge( aPolyEdgeId );
1167 Standard_Integer aFirstVertex = aPolyEdge.FirstNode();
1168 Standard_Integer aLastVertex = aPolyEdge.LastNode();
1170 aPolyVerticesFindMap.Add( aFirstVertex );
1171 aPolyVerticesFindMap.Add( aLastVertex );
1173 if ( aPolyEdgeInfo > 0 )
1175 aPolyVertices.Append( aFirstVertex );
1176 aPolyVertices.Append( aLastVertex );
1180 aPolyVertices.Append( aLastVertex );
1181 aPolyVertices.Append( aFirstVertex );
1186 // Make closed sequence
1187 if ( aPolyVertices.First() != aPolyVertices.Last() )
1188 aPolyVertices.Append( aPolyVertices.First() );
1190 BRepMesh::MapOfInteger aSurvivedLinks( anIgnoredEdges );
1192 Standard_Integer aPolyVertIt = 0;
1193 Standard_Integer anUniqueVerticesNum = aPolyVertices.Length() - 1;
1194 for ( ; aPolyVertIt < anUniqueVerticesNum; ++aPolyVertIt )
1196 killTrianglesAroundVertex( aPolyVertices( aPolyVertIt ),
1197 aPolyVertices, aPolyVerticesFindMap, thePolygon,
1198 thePolyBoxes, aSurvivedLinks, aLoopEdges );
1201 BRepMesh::MapOfIntegerInteger::Iterator aLoopEdgesIt( aLoopEdges );
1202 for ( ; aLoopEdgesIt.More(); aLoopEdgesIt.Next() )
1204 const Standard_Integer& aLoopEdgeId = aLoopEdgesIt.Key();
1205 if ( anIgnoredEdges.Contains( aLoopEdgeId ) )
1208 if ( myMeshData->ElementsConnectedTo( aLoopEdgeId ).IsEmpty() )
1209 myMeshData->RemoveLink( aLoopEdgesIt.Key() );
1213 //=======================================================================
1214 //function : killTrianglesAroundVertex
1215 //purpose : Remove all triangles and edges that are placed
1216 // inside the polygon or crossed it.
1217 //=======================================================================
1218 void BRepMesh_Delaun::killTrianglesAroundVertex(
1219 const Standard_Integer theZombieNodeId,
1220 const BRepMesh::VectorOfInteger& thePolyVertices,
1221 const BRepMesh::MapOfInteger& thePolyVerticesFindMap,
1222 const BRepMesh::SequenceOfInteger& thePolygon,
1223 const BRepMesh::SequenceOfBndB2d& thePolyBoxes,
1224 BRepMesh::MapOfInteger& theSurvivedLinks,
1225 BRepMesh::MapOfIntegerInteger& theLoopEdges )
1227 BRepMesh::ListOfInteger::Iterator aNeighborsIt =
1228 myMeshData->LinksConnectedTo( theZombieNodeId );
1230 // Try to infect neighbor nodes
1231 BRepMesh::VectorOfInteger aVictimNodes;
1232 for ( ; aNeighborsIt.More(); aNeighborsIt.Next() )
1234 const Standard_Integer& aNeighborLinkId = aNeighborsIt.Value();
1235 if ( theSurvivedLinks.Contains( aNeighborLinkId ) )
1238 const BRepMesh_Edge& aNeighborLink = GetEdge( aNeighborLinkId );
1239 if ( aNeighborLink.Movability() == BRepMesh_Frontier )
1241 // Though, if it lies onto the polygon boundary -
1242 // take its triangles
1244 Standard_Boolean isNotIntersect =
1245 checkIntersection( aNeighborLink, thePolygon,
1246 thePolyBoxes, Standard_False, Standard_True,
1247 Standard_False, aBox );
1249 if ( isNotIntersect )
1252 theSurvivedLinks.Add( aNeighborLinkId );
1258 Standard_Integer anOtherNode = aNeighborLink.FirstNode();
1259 if ( anOtherNode == theZombieNodeId )
1260 anOtherNode = aNeighborLink.LastNode();
1262 // Possible sacrifice
1263 if ( !thePolyVerticesFindMap.Contains( anOtherNode ) )
1265 if ( isVertexInsidePolygon( anOtherNode, thePolyVertices ) )
1268 aVictimNodes.Append( anOtherNode );
1272 // Lucky. But it may intersect the polygon boundary.
1274 killTrianglesOnIntersectingLinks( aNeighborLinkId,
1275 aNeighborLink, anOtherNode, thePolygon,
1276 thePolyBoxes, theSurvivedLinks, theLoopEdges );
1283 // Add link to the survivers to avoid cycling
1284 theSurvivedLinks.Add( aNeighborLinkId );
1285 killLinkTriangles( aNeighborLinkId, theLoopEdges );
1288 // Go and do your job!
1289 BRepMesh::VectorOfInteger::Iterator aVictimIt( aVictimNodes );
1290 for ( ; aVictimIt.More(); aVictimIt.Next() )
1292 killTrianglesAroundVertex( aVictimIt.Value(), thePolyVertices,
1293 thePolyVerticesFindMap, thePolygon, thePolyBoxes,
1294 theSurvivedLinks, theLoopEdges );
1298 //=======================================================================
1299 //function : isVertexInsidePolygon
1300 //purpose : Checks is the given vertex lies inside the polygon
1301 //=======================================================================
1302 Standard_Boolean BRepMesh_Delaun::isVertexInsidePolygon(
1303 const Standard_Integer& theVertexId,
1304 const BRepMesh::VectorOfInteger& thePolygonVertices ) const
1306 Standard_Integer aPolyLen = thePolygonVertices.Length();
1308 return Standard_False;
1311 const gp_XY aCenterPointXY = GetVertex( theVertexId ).Coord();
1313 const BRepMesh_Vertex& aFirstVertex = GetVertex( thePolygonVertices( 0 ) );
1314 gp_Vec2d aPrevVertexDir( aFirstVertex.Coord() - aCenterPointXY );
1315 if ( aPrevVertexDir.SquareMagnitude() < Precision2 )
1316 return Standard_True;
1318 Standard_Real aTotalAng = 0.0;
1319 for ( Standard_Integer aPolyIt = 1; aPolyIt < aPolyLen; ++aPolyIt )
1321 const BRepMesh_Vertex& aPolyVertex = GetVertex( thePolygonVertices( aPolyIt ) );
1323 gp_Vec2d aCurVertexDir( aPolyVertex.Coord() - aCenterPointXY );
1324 if ( aCurVertexDir.SquareMagnitude() < Precision2 )
1325 return Standard_True;
1327 aTotalAng += aCurVertexDir.Angle( aPrevVertexDir );
1328 aPrevVertexDir = aCurVertexDir;
1331 if ( Abs( Angle2PI - aTotalAng ) > Precision::Angular() )
1332 return Standard_False;
1334 return Standard_True;
1337 //=======================================================================
1338 //function : killTrianglesOnIntersectingLinks
1339 //purpose : Checks is the given link crosses the polygon boundary.
1340 // If yes, kills its triangles and checks neighbor links on
1341 // boundary intersection. Does nothing elsewhere.
1342 //=======================================================================
1343 void BRepMesh_Delaun::killTrianglesOnIntersectingLinks(
1344 const Standard_Integer& theLinkToCheckId,
1345 const BRepMesh_Edge& theLinkToCheck,
1346 const Standard_Integer& theEndPoint,
1347 const BRepMesh::SequenceOfInteger& thePolygon,
1348 const BRepMesh::SequenceOfBndB2d& thePolyBoxes,
1349 BRepMesh::MapOfInteger& theSurvivedLinks,
1350 BRepMesh::MapOfIntegerInteger& theLoopEdges )
1352 if ( theSurvivedLinks.Contains( theLinkToCheckId ) )
1356 Standard_Boolean isNotIntersect =
1357 checkIntersection( theLinkToCheck, thePolygon,
1358 thePolyBoxes, Standard_False, Standard_False,
1359 Standard_False, aBox );
1361 theSurvivedLinks.Add( theLinkToCheckId );
1363 if ( isNotIntersect )
1366 killLinkTriangles( theLinkToCheckId, theLoopEdges );
1368 BRepMesh::ListOfInteger::Iterator aNeighborsIt(
1369 myMeshData->LinksConnectedTo(theEndPoint));
1371 for ( ; aNeighborsIt.More(); aNeighborsIt.Next() )
1373 const Standard_Integer& aNeighborLinkId = aNeighborsIt.Value();
1374 const BRepMesh_Edge& aNeighborLink = GetEdge( aNeighborLinkId );
1375 Standard_Integer anOtherNode = aNeighborLink.FirstNode();
1376 if ( anOtherNode == theEndPoint )
1377 anOtherNode = aNeighborLink.LastNode();
1379 killTrianglesOnIntersectingLinks( aNeighborLinkId,
1380 aNeighborLink, anOtherNode, thePolygon,
1381 thePolyBoxes, theSurvivedLinks, theLoopEdges );
1385 //=======================================================================
1386 //function : killLinkTriangles
1387 //purpose : Kill triangles bound to the given link.
1388 //=======================================================================
1389 void BRepMesh_Delaun::killLinkTriangles(
1390 const Standard_Integer& theLinkId,
1391 BRepMesh::MapOfIntegerInteger& theLoopEdges )
1393 const BRepMesh_PairOfIndex& aPair =
1394 myMeshData->ElementsConnectedTo( theLinkId );
1396 Standard_Integer anElemNb = aPair.Extent();
1397 for ( Standard_Integer aPairIt = 1; aPairIt <= anElemNb; ++aPairIt )
1399 Standard_Integer anElemId = aPair.FirstIndex();
1403 deleteTriangle( anElemId, theLoopEdges );
1407 //=======================================================================
1408 //function : getOrientedNodes
1409 //purpose : Returns start and end nodes of the given edge in respect to
1411 //=======================================================================
1412 void BRepMesh_Delaun::getOrientedNodes(const BRepMesh_Edge& theEdge,
1413 const Standard_Boolean isForward,
1414 Standard_Integer *theNodes) const
1418 theNodes[0] = theEdge.FirstNode();
1419 theNodes[1] = theEdge.LastNode();
1423 theNodes[0] = theEdge.LastNode();
1424 theNodes[1] = theEdge.FirstNode();
1428 //=======================================================================
1429 //function : processLoop
1430 //purpose : Processes loop within the given polygon formed by range of
1431 // its links specified by start and end link indices.
1432 //=======================================================================
1433 void BRepMesh_Delaun::processLoop(const Standard_Integer theLinkFrom,
1434 const Standard_Integer theLinkTo,
1435 const BRepMesh::SequenceOfInteger& thePolygon,
1436 const BRepMesh::SequenceOfBndB2d& thePolyBoxes)
1438 Standard_Integer aNbOfLinksInLoop = theLinkTo - theLinkFrom - 1;
1439 if ( aNbOfLinksInLoop < 3 )
1442 BRepMesh::SequenceOfInteger aPolygon;
1443 BRepMesh::SequenceOfBndB2d aPolyBoxes;
1444 for ( ; aNbOfLinksInLoop > 0; --aNbOfLinksInLoop )
1446 Standard_Integer aLoopLinkIndex = theLinkFrom + aNbOfLinksInLoop;
1447 aPolygon .Prepend( thePolygon ( aLoopLinkIndex ) );
1448 aPolyBoxes.Prepend( thePolyBoxes( aLoopLinkIndex ) );
1450 meshPolygon( aPolygon, aPolyBoxes );
1453 //=======================================================================
1454 //function : createAndReplacePolygonLink
1455 //purpose : Creates new link based on the given nodes and updates the
1457 //=======================================================================
1458 Standard_Integer BRepMesh_Delaun::createAndReplacePolygonLink(
1459 const Standard_Integer *theNodes,
1460 const gp_Pnt2d *thePnts,
1461 const Standard_Integer theRootIndex,
1462 const ReplaceFlag theReplaceFlag,
1463 BRepMesh::SequenceOfInteger& thePolygon,
1464 BRepMesh::SequenceOfBndB2d& thePolyBoxes )
1466 Standard_Integer aNewEdgeId =
1467 myMeshData->AddLink( BRepMesh_Edge(
1468 theNodes[0], theNodes[1], BRepMesh_Free ) );
1471 UpdateBndBox(thePnts[0].Coord(), thePnts[1].Coord(), aNewBox);
1473 switch ( theReplaceFlag )
1475 case BRepMesh_Delaun::Replace:
1476 thePolygon .SetValue( theRootIndex, aNewEdgeId );
1477 thePolyBoxes.SetValue( theRootIndex, aNewBox );
1480 case BRepMesh_Delaun::InsertAfter:
1481 thePolygon .InsertAfter( theRootIndex, aNewEdgeId );
1482 thePolyBoxes.InsertAfter( theRootIndex, aNewBox );
1485 case BRepMesh_Delaun::InsertBefore:
1486 thePolygon .InsertBefore( theRootIndex, aNewEdgeId );
1487 thePolyBoxes.InsertBefore( theRootIndex, aNewBox );
1494 //=======================================================================
1495 //function : meshPolygon
1497 //=======================================================================
1498 void BRepMesh_Delaun::meshPolygon(BRepMesh::SequenceOfInteger& thePolygon,
1499 BRepMesh::SequenceOfBndB2d& thePolyBoxes,
1500 BRepMesh::HMapOfInteger theSkipped )
1502 // Check is the source polygon elementary
1503 if ( meshElementaryPolygon( thePolygon ) )
1506 // Check and correct boundary edges
1507 Standard_Integer aPolyLen = thePolygon.Length();
1508 const Standard_Real aPolyArea = Abs( polyArea( thePolygon, 1, aPolyLen ) );
1509 const Standard_Real aSmallLoopArea = 0.001 * aPolyArea;
1510 for ( Standard_Integer aPolyIt = 1; aPolyIt < aPolyLen; ++aPolyIt )
1512 Standard_Integer aCurEdgeInfo = thePolygon( aPolyIt );
1513 Standard_Integer aCurEdgeId = Abs( aCurEdgeInfo );
1514 const BRepMesh_Edge* aCurEdge = &GetEdge( aCurEdgeId );
1515 if ( aCurEdge->Movability() != BRepMesh_Frontier )
1518 Standard_Integer aCurNodes[2];
1519 getOrientedNodes( *aCurEdge, aCurEdgeInfo > 0, aCurNodes );
1521 gp_Pnt2d aCurPnts[2] = {
1522 GetVertex(aCurNodes[0]).Coord(),
1523 GetVertex(aCurNodes[1]).Coord()
1526 gp_Vec2d aCurVec( aCurPnts[0], aCurPnts[1] );
1528 // check further links
1529 Standard_Integer aNextPolyIt = aPolyIt + 1;
1530 for ( ; aNextPolyIt <= aPolyLen; ++aNextPolyIt )
1532 Standard_Integer aNextEdgeInfo = thePolygon( aNextPolyIt );
1533 Standard_Integer aNextEdgeId = Abs( aNextEdgeInfo );
1534 const BRepMesh_Edge* aNextEdge = &GetEdge( aNextEdgeId );
1535 if ( aNextEdge->Movability() != BRepMesh_Frontier )
1538 Standard_Integer aNextNodes[2];
1539 getOrientedNodes( *aNextEdge, aNextEdgeInfo > 0, aNextNodes );
1541 gp_Pnt2d aNextPnts[2] = {
1542 GetVertex(aNextNodes[0]).Coord(),
1543 GetVertex(aNextNodes[1]).Coord()
1547 BRepMesh_GeomTool::IntFlag aIntFlag = intSegSeg( *aCurEdge, *aNextEdge,
1548 Standard_False, Standard_True, anIntPnt );
1550 if ( aIntFlag == BRepMesh_GeomTool::NoIntersection )
1553 Standard_Boolean isRemoveFromFirst = Standard_False;
1554 Standard_Boolean isAddReplacingEdge = Standard_True;
1555 Standard_Integer aIndexToRemoveTo = aNextPolyIt;
1556 if ( aIntFlag == BRepMesh_GeomTool::Cross )
1558 Standard_Real aLoopArea = polyArea( thePolygon, aPolyIt + 1, aNextPolyIt );
1559 gp_Vec2d aVec1( anIntPnt, aCurPnts [1] );
1560 gp_Vec2d aVec2( anIntPnt, aNextPnts[0] );
1562 aLoopArea += ( aVec1 ^ aVec2 ) / 2.;
1563 if ( Abs( aLoopArea ) > aSmallLoopArea )
1565 aNextNodes[1] = aCurNodes[0];
1566 aNextPnts [1] = aCurPnts [0];
1568 aNextEdgeId = Abs( createAndReplacePolygonLink( aNextNodes, aNextPnts,
1569 aNextPolyIt, BRepMesh_Delaun::Replace, thePolygon, thePolyBoxes ) );
1571 processLoop( aPolyIt, aNextPolyIt, thePolygon, thePolyBoxes );
1575 Standard_Real aDist1 = anIntPnt.SquareDistance(aNextPnts[0]);
1576 Standard_Real aDist2 = anIntPnt.SquareDistance(aNextPnts[1]);
1578 // Choose node with lower distance
1579 const Standard_Boolean isCloseToStart = ( aDist1 < aDist2 );
1580 const Standard_Integer aEndPointIndex = isCloseToStart ? 0 : 1;
1581 aCurNodes[1] = aNextNodes[aEndPointIndex];
1582 aCurPnts [1] = aNextPnts [aEndPointIndex];
1584 if ( isCloseToStart )
1587 // In this context only intersections between frontier edges
1588 // are possible. If intersection between edges of different
1589 // types occured - treat this case as invalid (i.e. result
1590 // might not reflect the expectations).
1591 if ( !theSkipped.IsNull() )
1593 Standard_Integer aSkippedLinkIt = aPolyIt;
1594 for ( ; aSkippedLinkIt <= aIndexToRemoveTo; ++aSkippedLinkIt )
1595 theSkipped->Add( Abs( thePolygon( aSkippedLinkIt ) ) );
1598 else if ( aIntFlag == BRepMesh_GeomTool::PointOnSegment )
1600 // Indentify chopping link
1601 Standard_Boolean isFirstChopping = Standard_False;
1602 Standard_Integer aCheckPointIt = 0;
1603 for ( ; aCheckPointIt < 2; ++aCheckPointIt )
1605 gp_Pnt2d& aRefPoint = aCurPnts[aCheckPointIt];
1606 // Check is second link touches the first one
1607 gp_Vec2d aVec1( aRefPoint, aNextPnts[0] );
1608 gp_Vec2d aVec2( aRefPoint, aNextPnts[1] );
1609 if ( Abs( aVec1 ^ aVec2 ) < Precision )
1611 isFirstChopping = Standard_True;
1616 if ( isFirstChopping )
1618 // Split second link
1619 isAddReplacingEdge = Standard_False;
1620 isRemoveFromFirst = ( aCheckPointIt == 0 );
1622 Standard_Integer aSplitLink[3] = {
1624 aCurNodes [aCheckPointIt],
1628 gp_Pnt2d aSplitPnts[3] = {
1630 aCurPnts [aCheckPointIt],
1634 Standard_Integer aSplitLinkIt = 0;
1635 for ( ; aSplitLinkIt < 2; ++aSplitLinkIt )
1637 createAndReplacePolygonLink( &aSplitLink[aSplitLinkIt],
1638 &aSplitPnts[aSplitLinkIt], aNextPolyIt, ( aSplitLinkIt == 0 ) ?
1639 BRepMesh_Delaun::Replace : BRepMesh_Delaun::InsertAfter,
1640 thePolygon, thePolyBoxes );
1643 processLoop( aPolyIt + aCheckPointIt, aIndexToRemoveTo,
1644 thePolygon, thePolyBoxes );
1649 Standard_Integer aSplitLinkNodes[2] = {
1654 gp_Pnt2d aSplitLinkPnts[2] = {
1658 createAndReplacePolygonLink( aSplitLinkNodes, aSplitLinkPnts,
1659 aPolyIt, BRepMesh_Delaun::InsertAfter, thePolygon, thePolyBoxes );
1661 aCurNodes[1] = aNextNodes[1];
1662 aCurPnts [1] = aNextPnts [1];
1665 processLoop( aPolyIt + 1, aIndexToRemoveTo,
1666 thePolygon, thePolyBoxes );
1669 else if ( aIntFlag == BRepMesh_GeomTool::Glued )
1671 if ( aCurNodes[1] == aNextNodes[0] )
1673 aCurNodes[1] = aNextNodes[1];
1674 aCurPnts [1] = aNextPnts [1];
1676 // TODO: Non-adjacent glued links within the polygon
1678 else if ( aIntFlag == BRepMesh_GeomTool::Same )
1680 processLoop( aPolyIt, aNextPolyIt, thePolygon, thePolyBoxes );
1682 isRemoveFromFirst = Standard_True;
1683 isAddReplacingEdge = Standard_False;
1686 continue; // Not supported type
1688 if ( isAddReplacingEdge )
1690 aCurEdgeId = Abs( createAndReplacePolygonLink( aCurNodes, aCurPnts,
1691 aPolyIt, BRepMesh_Delaun::Replace, thePolygon, thePolyBoxes ) );
1693 aCurEdge = &GetEdge( aCurEdgeId );
1694 aCurVec = gp_Vec2d( aCurPnts[0], aCurPnts[1] );
1697 Standard_Integer aIndexToRemoveFrom =
1698 isRemoveFromFirst ? aPolyIt : aPolyIt + 1;
1700 thePolygon .Remove( aIndexToRemoveFrom, aIndexToRemoveTo );
1701 thePolyBoxes.Remove( aIndexToRemoveFrom, aIndexToRemoveTo );
1703 aPolyLen = thePolygon.Length();
1704 if ( isRemoveFromFirst )
1710 aNextPolyIt = aPolyIt;
1714 BRepMesh::SequenceOfInteger* aPolygon1 = &thePolygon;
1715 BRepMesh::SequenceOfBndB2d* aPolyBoxes1 = &thePolyBoxes;
1717 BRepMesh::HSequenceOfInteger aPolygon2 = new BRepMesh::SequenceOfInteger;
1718 BRepMesh::HSequenceOfBndB2d aPolyBoxes2 = new BRepMesh::SequenceOfBndB2d;
1720 NCollection_Sequence<BRepMesh::HSequenceOfInteger> aPolyStack;
1721 NCollection_Sequence<BRepMesh::HSequenceOfBndB2d> aPolyBoxStack;
1724 decomposeSimplePolygon(*aPolygon1, *aPolyBoxes1, *aPolygon2, *aPolyBoxes2);
1725 if (!aPolygon2->IsEmpty())
1727 aPolyStack.Append(aPolygon2);
1728 aPolyBoxStack.Append(aPolyBoxes2);
1730 aPolygon2 = new BRepMesh::SequenceOfInteger;
1731 aPolyBoxes2 = new BRepMesh::SequenceOfBndB2d;
1734 if (aPolygon1->IsEmpty())
1736 if (!aPolyStack.IsEmpty() && aPolygon1 == &(*aPolyStack.First()))
1738 aPolyStack.Remove(1);
1739 aPolyBoxStack.Remove(1);
1742 if (aPolyStack.IsEmpty())
1745 aPolygon1 = &(*aPolyStack.ChangeFirst());
1746 aPolyBoxes1 = &(*aPolyBoxStack.ChangeFirst());
1751 //=======================================================================
1752 //function : meshElementaryPolygon
1753 //purpose : Triangulation of closed polygon containing only three edges.
1754 //=======================================================================
1755 inline Standard_Boolean BRepMesh_Delaun::meshElementaryPolygon(
1756 const BRepMesh::SequenceOfInteger& thePolygon)
1758 Standard_Integer aPolyLen = thePolygon.Length();
1760 return Standard_True;
1761 else if ( aPolyLen > 3 )
1762 return Standard_False;
1764 // Just create a triangle
1765 Standard_Integer anEdges[3];
1766 Standard_Boolean anEdgesOri[3];
1768 for ( Standard_Integer anEdgeIt = 0; anEdgeIt < 3; ++anEdgeIt )
1770 Standard_Integer anEdgeInfo = thePolygon( anEdgeIt + 1 );
1771 anEdges[anEdgeIt] = Abs( anEdgeInfo );
1772 anEdgesOri[anEdgeIt] = ( anEdgeInfo > 0 );
1775 const BRepMesh_Edge& anEdge1 = GetEdge( anEdges[0] );
1776 const BRepMesh_Edge& anEdge2 = GetEdge( anEdges[1] );
1778 Standard_Integer aNodes[3] = { anEdge1.FirstNode(),
1780 anEdge2.FirstNode() };
1781 if ( aNodes[2] == aNodes[0] ||
1782 aNodes[2] == aNodes[1] )
1784 aNodes[2] = anEdge2.LastNode();
1787 addTriangle( anEdges, anEdgesOri, aNodes );
1788 return Standard_True;
1791 //=======================================================================
1792 //function : meshSimplePolygon
1794 //=======================================================================
1795 void BRepMesh_Delaun::decomposeSimplePolygon(
1796 BRepMesh::SequenceOfInteger& thePolygon,
1797 BRepMesh::SequenceOfBndB2d& thePolyBoxes,
1798 BRepMesh::SequenceOfInteger& thePolygonCut,
1799 BRepMesh::SequenceOfBndB2d& thePolyBoxesCut)
1801 // Check is the given polygon elementary
1802 if ( meshElementaryPolygon( thePolygon ) )
1805 thePolyBoxes.Clear();
1809 // Polygon contains more than 3 links
1810 Standard_Integer aFirstEdgeInfo = thePolygon(1);
1811 const BRepMesh_Edge& aFirstEdge = GetEdge( Abs( aFirstEdgeInfo ) );
1813 Standard_Integer aNodes[3];
1814 getOrientedNodes( aFirstEdge, aFirstEdgeInfo > 0, aNodes );
1816 gp_Pnt2d aRefVertices[3];
1817 aRefVertices[0] = GetVertex( aNodes[0] ).Coord();
1818 aRefVertices[1] = GetVertex( aNodes[1] ).Coord();
1820 gp_Vec2d aRefEdgeDir( aRefVertices[0], aRefVertices[1] );
1822 Standard_Real aRefEdgeLen = aRefEdgeDir.Magnitude();
1823 if ( aRefEdgeLen < Precision )
1826 thePolyBoxes.Clear();
1830 aRefEdgeDir /= aRefEdgeLen;
1832 // Find a point with minimum distance respect
1833 // the end of reference link
1834 Standard_Integer aUsedLinkId = 0;
1835 Standard_Real aOptAngle = 0.0;
1836 Standard_Real aMinDist = RealLast();
1837 Standard_Integer aPivotNode = aNodes[1];
1838 Standard_Integer aPolyLen = thePolygon.Length();
1839 for ( Standard_Integer aLinkIt = 3; aLinkIt <= aPolyLen; ++aLinkIt )
1841 Standard_Integer aLinkInfo = thePolygon( aLinkIt );
1842 const BRepMesh_Edge& aNextEdge = GetEdge( Abs( aLinkInfo ) );
1844 aPivotNode = aLinkInfo > 0 ?
1845 aNextEdge.FirstNode() :
1846 aNextEdge.LastNode();
1848 gp_Pnt2d aPivotVertex = GetVertex( aPivotNode ).Coord();
1849 gp_Vec2d aDistanceDir( aRefVertices[1], aPivotVertex );
1851 Standard_Real aDist = aRefEdgeDir ^ aDistanceDir;
1852 Standard_Real aAngle = Abs( aRefEdgeDir.Angle(aDistanceDir) );
1853 Standard_Real anAbsDist = Abs( aDist );
1854 if (anAbsDist < Precision || aDist < 0.)
1857 if ( ( anAbsDist >= aMinDist ) &&
1858 ( aAngle <= aOptAngle || aAngle > AngDeviation90Deg ) )
1863 // Check is the test link crosses the polygon boudaries
1864 Standard_Boolean isIntersect = Standard_False;
1865 for ( Standard_Integer aRefLinkNodeIt = 0; aRefLinkNodeIt < 2; ++aRefLinkNodeIt )
1867 const Standard_Integer& aLinkFirstNode = aNodes[aRefLinkNodeIt];
1868 const gp_Pnt2d& aLinkFirstVertex = aRefVertices[aRefLinkNodeIt];
1871 UpdateBndBox(aLinkFirstVertex.Coord(), aPivotVertex.Coord(), aBox);
1873 BRepMesh_Edge aCheckLink( aLinkFirstNode, aPivotNode, BRepMesh_Free );
1875 Standard_Integer aCheckLinkIt = 2;
1876 for ( ; aCheckLinkIt <= aPolyLen; ++aCheckLinkIt )
1878 if( aCheckLinkIt == aLinkIt )
1881 if ( !aBox.IsOut( thePolyBoxes.Value( aCheckLinkIt ) ) )
1883 const BRepMesh_Edge& aPolyLink =
1884 GetEdge( Abs( thePolygon( aCheckLinkIt ) ) );
1886 if ( aCheckLink.IsEqual( aPolyLink ) )
1889 // intersection is possible...
1891 BRepMesh_GeomTool::IntFlag aIntFlag = intSegSeg( aCheckLink, aPolyLink,
1892 Standard_False, Standard_False, anIntPnt );
1894 if( aIntFlag != BRepMesh_GeomTool::NoIntersection )
1896 isIntersect = Standard_True;
1911 aMinDist = anAbsDist;
1912 aNodes[2] = aPivotNode;
1913 aRefVertices[2] = aPivotVertex;
1914 aUsedLinkId = aLinkIt;
1917 if ( aUsedLinkId == 0 )
1920 thePolyBoxes.Clear();
1925 BRepMesh_Edge aNewEdges[2] = {
1926 BRepMesh_Edge( aNodes[1], aNodes[2], BRepMesh_Free ),
1927 BRepMesh_Edge( aNodes[2], aNodes[0], BRepMesh_Free ) };
1929 Standard_Integer aNewEdgesInfo[3] = {
1931 myMeshData->AddLink( aNewEdges[0] ),
1932 myMeshData->AddLink( aNewEdges[1] ) };
1935 Standard_Integer anEdges[3];
1936 Standard_Boolean anEdgesOri[3];
1937 for ( Standard_Integer aTriEdgeIt = 0; aTriEdgeIt < 3; ++aTriEdgeIt )
1939 const Standard_Integer& anEdgeInfo = aNewEdgesInfo[aTriEdgeIt];
1940 anEdges[aTriEdgeIt] = Abs( anEdgeInfo );
1941 anEdgesOri[aTriEdgeIt] = anEdgeInfo > 0;
1943 addTriangle( anEdges, anEdgesOri, aNodes );
1945 // Create triangle and split the source polygon on two
1946 // parts (if possible) and mesh each part as independent
1948 if ( aUsedLinkId < aPolyLen )
1950 thePolygon.Split(aUsedLinkId, thePolygonCut);
1951 thePolygonCut.Prepend( -aNewEdgesInfo[2] );
1952 thePolyBoxes.Split(aUsedLinkId, thePolyBoxesCut);
1955 UpdateBndBox(aRefVertices[0].Coord(), aRefVertices[2].Coord(), aBox);
1956 thePolyBoxesCut.Prepend( aBox );
1960 thePolygon.Remove ( aPolyLen );
1961 thePolyBoxes.Remove( aPolyLen );
1964 if ( aUsedLinkId > 3 )
1966 thePolygon.SetValue( 1, -aNewEdgesInfo[1] );
1969 UpdateBndBox(aRefVertices[1].Coord(), aRefVertices[2].Coord(), aBox);
1970 thePolyBoxes.SetValue( 1, aBox );
1974 //=======================================================================
1975 //function : RemoveVertex
1976 //purpose : Removes a vertex from the triangulation
1977 //=======================================================================
1978 void BRepMesh_Delaun::RemoveVertex( const BRepMesh_Vertex& theVertex )
1980 BRepMesh_SelectorOfDataStructureOfDelaun aSelector( myMeshData );
1981 aSelector.NeighboursOf( theVertex );
1983 BRepMesh::MapOfIntegerInteger aLoopEdges;//( 10, myMeshData->Allocator() );
1985 // Loop on triangles to be destroyed :
1986 BRepMesh::MapOfInteger::Iterator aTriangleIt( aSelector.Elements() );
1987 for ( ; aTriangleIt.More(); aTriangleIt.Next() )
1988 deleteTriangle( aTriangleIt.Key(), aLoopEdges );
1990 BRepMesh::SequenceOfBndB2d aBoxes;
1991 BRepMesh::SequenceOfInteger aPolygon;
1992 Standard_Integer aLoopEdgesCount = aLoopEdges.Extent();
1993 BRepMesh::MapOfIntegerInteger::Iterator aLoopEdgesIt( aLoopEdges );
1995 if ( aLoopEdgesIt.More() )
1997 const BRepMesh_Edge& anEdge = GetEdge( aLoopEdgesIt.Key() );
1998 Standard_Integer aFirstNode = anEdge.FirstNode();
1999 Standard_Integer aLastNode;
2000 Standard_Integer aPivotNode = anEdge.LastNode();
2001 Standard_Integer anEdgeId = aLoopEdgesIt.Key();
2003 Standard_Boolean isPositive = (Standard_Boolean)aLoopEdges( anEdgeId );
2006 Standard_Integer aTmp;
2008 aFirstNode = aPivotNode;
2011 aPolygon.Append( -anEdgeId );
2014 aPolygon.Append( anEdgeId );
2016 fillBndBox( aBoxes, GetVertex( aFirstNode ), GetVertex( aPivotNode ) );
2018 aLoopEdges.UnBind( anEdgeId );
2020 aLastNode = aFirstNode;
2021 while ( aPivotNode != aLastNode )
2023 BRepMesh::ListOfInteger::Iterator aLinkIt( myMeshData->LinksConnectedTo( aPivotNode ) );
2024 for ( ; aLinkIt.More(); aLinkIt.Next() )
2026 if ( aLinkIt.Value() != anEdgeId &&
2027 aLoopEdges.IsBound( aLinkIt.Value() ) )
2029 Standard_Integer aCurrentNode;
2030 anEdgeId = aLinkIt.Value();
2031 const BRepMesh_Edge& anEdge1 = GetEdge( anEdgeId );
2033 aCurrentNode = anEdge1.LastNode();
2034 if ( aCurrentNode != aPivotNode )
2036 aCurrentNode = anEdge1.FirstNode();
2037 aPolygon.Append( -anEdgeId );
2040 aPolygon.Append( anEdgeId );
2042 fillBndBox( aBoxes, GetVertex( aCurrentNode ), GetVertex( aPivotNode ) );
2044 aPivotNode = aCurrentNode;
2045 aLoopEdges.UnBind( anEdgeId );
2050 if ( aLoopEdgesCount <= 0 )
2055 meshPolygon( aPolygon, aBoxes );
2060 //=======================================================================
2061 //function : AddVertices
2062 //purpose : Adds some vertices in the triangulation.
2063 //=======================================================================
2064 void BRepMesh_Delaun::AddVertices(BRepMesh::Array1OfVertexOfDelaun& theVertices)
2066 std::make_heap(theVertices.begin(), theVertices.end(), ComparatorOfVertexOfDelaun());
2067 std::sort_heap(theVertices.begin(), theVertices.end(), ComparatorOfVertexOfDelaun());
2069 Standard_Integer aLower = theVertices.Lower();
2070 Standard_Integer anUpper = theVertices.Upper();
2072 BRepMesh::Array1OfInteger aVertexIndexes( aLower, anUpper );
2073 for ( Standard_Integer i = aLower; i <= anUpper; ++i )
2074 aVertexIndexes(i) = myMeshData->AddNode( theVertices(i) );
2076 createTrianglesOnNewVertices( aVertexIndexes );
2079 //=======================================================================
2080 //function : UseEdge
2081 //purpose : Modify mesh to use the edge. Return True if done
2082 //=======================================================================
2083 Standard_Boolean BRepMesh_Delaun::UseEdge( const Standard_Integer /*theIndex*/ )
2086 const BRepMesh_PairOfIndex& aPair = myMeshData->ElemConnectedTo( theIndex );
2087 if ( aPair.Extent() == 0 )
2089 const BRepMesh_Edge& anEdge = GetEdge( theIndex );
2091 Standard_Integer aStartNode, aPivotNode, anOtherNode;
2092 aStartNode = anEdge.FirstNode();
2093 aPivotNode = anEdge.LastNode();
2095 const BRepMesh_ListOfInteger& aStartNodeNeighbors = myMeshData->LinkNeighboursOf( aStartNode );
2096 const BRepMesh_ListOfInteger& aPivotNodeNeighbors = myMeshData->LinkNeighboursOf( aPivotNode );
2098 if ( aStartNodeNeighbors.Extent() > 0 &&
2099 aPivotNodeNeighbors.Extent() > 0 )
2101 const BRepMesh_Vertex& aStartVertex = GetVertex( aStartNode );
2102 const BRepMesh_Vertex& aPivotVertex = GetVertex( aPivotNode );
2104 gp_XY aVEdge ( aPivotVertex.Coord() );
2105 aVEdge.Subtract( aStartVertex.Coord() );
2107 Standard_Real anAngle = 0.;
2108 Standard_Real anAngleMin = RealLast();
2109 Standard_Real anAngleMax = RealFirst();
2110 Standard_Integer aLeftEdge = 0, aRightEdge = 0;
2112 BRepMesh_ListOfInteger::Iterator aNeighborIt( aPivotNodeNeighbors );
2113 for ( ; aNeighborIt.More(); aNeighborIt.Next() )
2115 Standard_Integer anEdgeId = aNeighborIt.Value();
2116 if ( anEdgeId != theIndex )
2118 const BRepMesh_Edge& aNextEdge = GetEdge( anEdgeId );
2120 Standard_Boolean isInMesh = Standard_True;
2121 if ( aNextEdge.Movability() == BRepMesh_Free )
2123 if ( myMeshData->ElemConnectedTo( anEdgeId ).IsEmpty() )
2124 isInMesh = Standard_False;
2129 anOtherNode = aNextEdge.FirstNode();
2130 if ( anOtherNode == aPivotNode )
2131 anOtherNode = aNextEdge.LastNode();
2133 gp_XY aVEdgeCur = GetVertex( anOtherNode ).Coord();
2134 aVEdgeCur.Subtract( aPivotVertex.Coord() );
2136 anAngle = gp_Vec2d( aVEdge ).Angle( gp_Vec2d( aVEdgeCur ) );
2139 if ( anAngle > anAngleMax )
2141 anAngleMax = anAngle;
2142 aLeftEdge = anEdgeId;
2144 if ( anAngle < anAngleMin )
2146 anAngleMin = anAngle;
2147 aRightEdge = anEdgeId;
2152 if ( aLeftEdge > 0 )
2154 if (aLeftEdge==aRightEdge)
2164 return Standard_False;
2167 //=======================================================================
2168 //function : getEdgesByType
2169 //purpose : Gives the list of edges with type defined by input parameter
2170 //=======================================================================
2171 BRepMesh::HMapOfInteger BRepMesh_Delaun::getEdgesByType(
2172 const BRepMesh_DegreeOfFreedom theEdgeType ) const
2174 Handle(NCollection_IncAllocator) anAlloc = new NCollection_IncAllocator;
2175 BRepMesh::HMapOfInteger aResult = new BRepMesh::MapOfInteger(1, anAlloc);
2176 BRepMesh::MapOfInteger::Iterator anEdgeIt( myMeshData->LinksOfDomain() );
2178 for ( ; anEdgeIt.More(); anEdgeIt.Next() )
2180 Standard_Integer anEdge = anEdgeIt.Key();
2181 Standard_Boolean isToAdd = (theEdgeType == BRepMesh_Free) ?
2182 (myMeshData->ElementsConnectedTo( anEdge ).Extent() <= 1) :
2183 (GetEdge( anEdge ).Movability() == theEdgeType);
2186 aResult->Add( anEdge );
2192 //=======================================================================
2193 //function : calculateDist
2194 //purpose : Calculates distances between the given point and edges of
2196 //=======================================================================
2197 Standard_Real BRepMesh_Delaun::calculateDist( const gp_XY theVEdges[3],
2198 const gp_XY thePoints[3],
2199 const BRepMesh_Vertex& theVertex,
2200 Standard_Real theDistance[3],
2201 Standard_Real theSqModulus[3],
2202 Standard_Integer& theEdgeOn ) const
2204 Standard_Real aMinDist = RealLast();
2205 for( Standard_Integer i = 0; i < 3; ++i )
2207 theSqModulus[i] = theVEdges[i].SquareModulus();
2208 if ( theSqModulus[i] <= Precision2 )
2211 theDistance[i] = theVEdges[i] ^ ( theVertex.Coord() - thePoints[i] );
2213 Standard_Real aDist = theDistance[i] * theDistance[i];
2214 aDist /= theSqModulus[i];
2216 if ( aDist < aMinDist )
2226 //=======================================================================
2227 //function : Contains
2228 //purpose : Test if triangle of index <TrianIndex> contains geometricaly
2229 // <theVertex>. If <theEdgeOn> is != 0 then theVertex is on Edge
2230 // of index <theEdgeOn>
2231 //=======================================================================
2232 Standard_Boolean BRepMesh_Delaun::Contains( const Standard_Integer theTriangleId,
2233 const BRepMesh_Vertex& theVertex,
2234 const Standard_Real theSqTolerance,
2235 Standard_Integer& theEdgeOn) const
2239 Standard_Integer e[3];
2240 Standard_Boolean o[3];
2241 Standard_Integer p[3];
2243 const BRepMesh_Triangle& aElement = GetTriangle( theTriangleId );
2244 aElement.Edges(e, o);
2246 const BRepMesh_Edge* anEdges[3] = { &GetEdge( e[0] ),
2250 myMeshData->ElementNodes(aElement, p);
2253 aPoints[0] = GetVertex( p[0] ).Coord();
2254 aPoints[1] = GetVertex( p[1] ).Coord();
2255 aPoints[2] = GetVertex( p[2] ).Coord();
2258 aVEdges[0] = aPoints[1];
2259 aVEdges[0].Subtract( aPoints[0] );
2261 aVEdges[1] = aPoints[2];
2262 aVEdges[1].Subtract( aPoints[1] );
2264 aVEdges[2] = aPoints[0];
2265 aVEdges[2].Subtract( aPoints[2] );
2267 Standard_Real aDistance[3];
2268 Standard_Real aSqModulus[3];
2270 Standard_Real aSqMinDist;
2271 Standard_Integer aEdgeOnId;
2272 aSqMinDist = calculateDist( aVEdges, aPoints, theVertex, aDistance, aSqModulus, aEdgeOnId );
2273 if ( aSqMinDist < 0 )
2274 return Standard_False;
2276 const Standard_Boolean isNotFree = (anEdges[aEdgeOnId]->Movability() != BRepMesh_Free);
2277 if ( aSqMinDist > theSqTolerance )
2279 if (isNotFree && aDistance[aEdgeOnId] < ( aSqModulus[aEdgeOnId] / 5. ))
2280 theEdgeOn = e[aEdgeOnId];
2283 return Standard_False;
2285 theEdgeOn = e[aEdgeOnId];
2287 return (aDistance[0] >= 0. && aDistance[1] >= 0. && aDistance[2] >= 0.);
2290 //=============================================================================
2291 //function : intSegSeg
2292 //purpose : Checks intersection between the two segments.
2293 //=============================================================================
2294 BRepMesh_GeomTool::IntFlag BRepMesh_Delaun::intSegSeg(
2295 const BRepMesh_Edge& theEdg1,
2296 const BRepMesh_Edge& theEdg2,
2297 const Standard_Boolean isConsiderEndPointTouch,
2298 const Standard_Boolean isConsiderPointOnEdge,
2299 gp_Pnt2d& theIntPnt) const
2301 gp_XY p1, p2, p3, p4;
2302 p1 = GetVertex( theEdg1.FirstNode() ).Coord();
2303 p2 = GetVertex( theEdg1.LastNode() ).Coord();
2304 p3 = GetVertex( theEdg2.FirstNode() ).Coord();
2305 p4 = GetVertex( theEdg2.LastNode() ).Coord();
2307 return BRepMesh_GeomTool::IntSegSeg(p1, p2, p3, p4,
2308 isConsiderEndPointTouch, isConsiderPointOnEdge, theIntPnt);
2311 //=============================================================================
2312 //function : polyArea
2313 //purpose : Returns area of the loop of the given polygon defined by indices
2314 // of its start and end links.
2315 //=============================================================================
2316 Standard_Real BRepMesh_Delaun::polyArea(const BRepMesh::SequenceOfInteger& thePolygon,
2317 const Standard_Integer theStartIndex,
2318 const Standard_Integer theEndIndex) const
2320 Standard_Real aArea = 0.0;
2321 Standard_Integer aPolyLen = thePolygon.Length();
2322 if ( theStartIndex >= theEndIndex ||
2323 theStartIndex > aPolyLen )
2327 Standard_Integer aCurEdgeInfo = thePolygon( theStartIndex );
2328 Standard_Integer aCurEdgeId = Abs( aCurEdgeInfo );
2329 const BRepMesh_Edge* aCurEdge = &GetEdge( aCurEdgeId );
2331 Standard_Integer aNodes[2];
2332 getOrientedNodes( *aCurEdge, aCurEdgeInfo > 0, aNodes );
2334 gp_Pnt2d aRefPnt = GetVertex( aNodes[0] ).Coord();
2335 Standard_Integer aPolyIt = theStartIndex + 1;
2336 for ( ; aPolyIt <= theEndIndex; ++aPolyIt )
2338 aCurEdgeInfo = thePolygon( aPolyIt );
2339 aCurEdgeId = Abs( aCurEdgeInfo );
2340 aCurEdge = &GetEdge( aCurEdgeId );
2342 getOrientedNodes( *aCurEdge, aCurEdgeInfo > 0, aNodes );
2343 gp_Vec2d aVec1( aRefPnt, GetVertex( aNodes[0] ).Coord() );
2344 gp_Vec2d aVec2( aRefPnt, GetVertex( aNodes[1] ).Coord() );
2346 aArea += aVec1 ^ aVec2;
2353 //=======================================================================
2354 //function : BRepMesh_DumpPoly
2356 //=======================================================================
2357 #include <TopoDS_Compound.hxx>
2358 #include <BRep_Builder.hxx>
2359 #include <Standard_ErrorHandler.hxx>
2360 #include <BRepBuilderAPI_MakeEdge.hxx>
2361 #include <BRepTools.hxx>
2362 Standard_CString BRepMesh_DumpPoly(void* thePolygon,
2363 void* theMeshHandlePtr,
2364 Standard_CString theFileNameStr)
2366 if (thePolygon == 0 || theFileNameStr == 0)
2368 return "Error: file name or polygon data is null";
2371 BRepMesh::SequenceOfInteger& aPolygon = *(BRepMesh::SequenceOfInteger*)thePolygon;
2373 Handle(BRepMesh_DataStructureOfDelaun) aMeshData =
2374 *(Handle(BRepMesh_DataStructureOfDelaun)*)theMeshHandlePtr;
2376 if (aMeshData.IsNull())
2377 return "Error: mesh data is empty";
2379 TopoDS_Compound aMesh;
2380 BRep_Builder aBuilder;
2381 aBuilder.MakeCompound(aMesh);
2387 BRepMesh::SequenceOfInteger::Iterator aLinksIt(aPolygon);
2388 for (; aLinksIt.More(); aLinksIt.Next())
2390 const BRepMesh_Edge& aLink = aMeshData->GetLink(Abs(aLinksIt.Value()));
2393 for (Standard_Integer i = 0; i < 2; ++i)
2395 const Standard_Integer aNodeId =
2396 (i == 0) ? aLink.FirstNode() : aLink.LastNode();
2398 const gp_XY& aNode = aMeshData->GetNode(aNodeId).Coord();
2399 aPnt[i] = gp_Pnt(aNode.X(), aNode.Y(), 0.);
2402 if (aPnt[0].SquareDistance(aPnt[1]) < Precision::SquareConfusion())
2405 aBuilder.Add(aMesh, BRepBuilderAPI_MakeEdge(aPnt[0], aPnt[1]));
2408 if (!BRepTools::Write(aMesh, theFileNameStr))
2409 return "Error: write failed";
2411 catch (Standard_Failure)
2413 return Standard_Failure::Caught()->GetMessageString();
2416 return theFileNameStr;