1 // Copyright (C) 2018-2019 OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // File : SMESH_Slot.cxx
20 // Created : Fri Nov 30 15:58:37 2018
21 // Author : Edward AGAPOV (eap)
23 #include "SMESH_MeshAlgos.hxx"
25 #include "ObjectPool.hxx"
26 #include "SMDS_LinearEdge.hxx"
27 #include "SMDS_Mesh.hxx"
28 #include "SMDS_MeshGroup.hxx"
30 #include <IntAna_IntConicQuad.hxx>
31 #include <IntAna_Quadric.hxx>
32 #include <NCollection_DataMap.hxx>
33 #include <NCollection_Map.hxx>
34 #include <Precision.hxx>
36 #include <gp_Cylinder.hxx>
43 #include <Utils_SALOME_Exception.hxx>
45 #include <boost/container/flat_set.hpp>
49 typedef SMESH_MeshAlgos::Edge TEdge;
51 //================================================================================
52 //! point of intersection of a face edge with the cylinder
55 SMESH_NodeXYZ myNode; // point and a node
56 int myEdgeIndex; // face edge index
57 bool myIsOutPln[2]; // isOut of two planes
60 //================================================================================
64 typedef boost::container::flat_set< const SMDS_MeshNode* > TNodeSet;
65 //typedef std::list< TEdge > TEdgeList;
67 const SMDS_MeshElement* myEdge;
68 TNodeSet myEndNodes; // ends of cut edges
69 //TEdgeList myCutEdges[2];
73 gp_Ax1 Ax1( bool reversed = false ) const
75 SMESH_NodeXYZ n1 = myEdge->GetNode( reversed );
76 SMESH_NodeXYZ n2 = myEdge->GetNode( !reversed );
77 return gp_Ax1( n1, gp_Dir( n2 - n1 ));
80 const SMDS_MeshNode* Node(int i) const
82 return myEdge->GetNode( i % 2 );
84 // store an intersection edge forming the slot border
85 void AddEdge( TEdge& e, double tol )
87 const SMDS_MeshNode** nodes = & e._node1;
88 for ( int i = 0; i < 2; ++i )
90 std::pair< TNodeSet::iterator, bool > nItAdded = myEndNodes.insert( nodes[ i ]);
91 if ( !nItAdded.second )
92 myEndNodes.erase( nItAdded.first );
96 // int i = myCutEdges[0].empty() ? 0 : 1;
97 // std::insert_iterator< TEdgeList > where = inserter( myCutEdges[i], myCutEdges[i].begin() );
99 // //double minDist = 1e100;
100 // SMESH_NodeXYZ nNew[2] = { e._node1, e._node2 };
101 // int iNewMin = 0, iCurMin = 1;
102 // for ( i = 0; i < 2; ++i )
104 // if ( myCutEdges[i].empty() )
106 // SMESH_NodeXYZ nCur[2] = { myCutEdges[i].front()._node1,
107 // myCutEdges[i].back()._node2 };
108 // for ( int iN = 0; iN < 2; ++iN )
109 // for ( int iC = 0; iC < 2; ++iC )
111 // if (( nCur[iC].Node() && nCur[iC] == nNew[iN] ) ||
112 // ( nCur[iC] - nNew[iN] ).SquareModulus() < tol * tol )
114 // where = inserter( myCutEdges[i], iC ? myCutEdges[i].end() : myCutEdges[i].begin() );
123 // if ( iNewMin == iCurMin )
124 // std::swap( e._node1, e._node2 );
128 Segment( const SMDS_MeshElement* e = 0 ): myEdge(e) { myEndNodes.reserve( 4 ); }
130 typedef ObjectPoolIterator<Segment> TSegmentIterator;
133 //================================================================================
135 * \brief Intersect a face edge given by its nodes with a cylinder.
137 //================================================================================
139 void intersectEdge( const gp_Cylinder& cyl,
140 const SMESH_NodeXYZ& n1,
141 const SMESH_NodeXYZ& n2,
143 std::vector< IntPoint >& intPoints )
145 gp_Lin line( gp_Ax1( n1, gp_Dir( n2 - n1 )));
146 IntAna_IntConicQuad intersection( line, IntAna_Quadric( cyl ));
148 if ( !intersection.IsDone() ||
149 intersection.IsParallel() ||
150 intersection.IsInQuadric() ||
151 intersection.NbPoints() == 0 )
154 gp_Vec edge( n1, n2 );
156 size_t oldNbPnts = intPoints.size();
157 for ( int iP = 1; iP <= intersection.NbPoints(); ++iP )
159 const gp_Pnt& p = intersection.Point( iP );
161 gp_Vec n1p ( n1, p );
162 const SMDS_MeshNode* n = 0;
164 double u = ( edge * n1p ) / edge.SquareMagnitude(); // param [0,1] on the edge
166 if ( p.SquareDistance( n1 ) < tol * tol )
171 else if ( u >= 1. ) {
172 if ( p.SquareDistance( n2 ) < tol * tol )
178 if ( p.SquareDistance( n1 ) < tol * tol )
180 else if ( p.SquareDistance( n2 ) < tol * tol )
184 intPoints.push_back( IntPoint() );
186 intPoints.back().myNode.Set( n );
188 intPoints.back().myNode.SetCoord( p.X(),p.Y(),p.Z() );
191 // set points order along an edge
192 if ( intPoints.size() - oldNbPnts == 2 &&
193 intersection.ParamOnConic( 1 ) > intersection.ParamOnConic( 2 ))
195 int i = intPoints.size() - 1;
196 std::swap( intPoints[ i ], intPoints[ i - 1 ]);
202 //================================================================================
204 * \brief Return signed distance between a point and a plane
206 //================================================================================
208 double signedDist( const gp_Pnt& p, const gp_Ax1& planeNormal )
210 const gp_Pnt& O = planeNormal.Location();
212 return Op * planeNormal.Direction();
215 //================================================================================
217 * \brief Check if a point is outside a segment domain bound by two planes
219 //================================================================================
221 bool isOut( const gp_Pnt& p, const gp_Ax1* planeNormal, bool* isOutPtr )
223 isOutPtr[0] = isOutPtr[1] = false;
225 for ( int i = 0; i < 2; ++i )
227 isOutPtr[i] = ( signedDist( p, planeNormal[i] ) <= 0. );
229 return ( isOutPtr[0] && isOutPtr[1] );
232 //================================================================================
234 * \brief Check if a segment between two points is outside a segment domain bound by two planes
236 //================================================================================
238 bool isSegmentOut( bool* isOutPtr1, bool* isOutPtr2 )
240 return (( isOutPtr1[0] && isOutPtr2[0] ) ||
241 ( isOutPtr1[1] && isOutPtr2[1] ));
244 //================================================================================
246 * \brief cut off ip1 from edge (ip1 - ip2) by a plane
248 //================================================================================
250 void cutOff( IntPoint & ip1, const IntPoint & ip2, const gp_Ax1& planeNormal, double tol )
252 gp_Lin lin( ip1.myNode, ( ip2.myNode - ip1.myNode ));
253 gp_Pln pln( planeNormal.Location(), planeNormal.Direction() );
255 IntAna_IntConicQuad intersection( lin, pln, Precision::Angular/*Tolerance*/() );
256 if ( intersection.IsDone() &&
257 !intersection.IsParallel() &&
258 !intersection.IsInQuadric() &&
259 intersection.NbPoints() == 1 )
261 if ( intersection.Point( 1 ).SquareDistance( ip1.myNode ) > tol * tol )
263 static_cast< gp_XYZ& >( ip1.myNode ) = intersection.Point( 1 ).XYZ();
264 ip1.myNode._node = 0;
265 ip1.myEdgeIndex = -1;
270 //================================================================================
272 * \brief Assure that face normal is computed in faceNormals vector
274 //================================================================================
276 const gp_XYZ& computeNormal( const SMDS_MeshElement* face,
277 std::vector< gp_XYZ >& faceNormals )
280 if ((int) faceNormals.size() <= face->GetID() )
283 faceNormals.resize( face->GetID() + 1 );
287 toCompute = faceNormals[ face->GetID() ].SquareModulus() == 0.;
290 SMESH_MeshAlgos::FaceNormal( face, faceNormals[ face->GetID() ], /*normalized=*/false );
292 return faceNormals[ face->GetID() ];
295 typedef std::vector< SMDS_MeshGroup* > TGroupVec;
297 //================================================================================
299 * \brief Fill theFaceID2Groups map for a given face
300 * \param [in] theFace - the face
301 * \param [in] theGroupsToUpdate - list of groups to treat
302 * \param [out] theFaceID2Groups - the map to fill in
303 * \param [out] theWorkGroups - a working buffer of groups
305 //================================================================================
307 void findGroups( const SMDS_MeshElement * theFace,
308 TGroupVec & theGroupsToUpdate,
309 NCollection_DataMap< int, TGroupVec > & theFaceID2Groups,
310 TGroupVec & theWorkGroups )
312 theWorkGroups.clear();
313 for ( size_t i = 0; i < theGroupsToUpdate.size(); ++i )
314 if ( theGroupsToUpdate[i]->Contains( theFace ))
315 theWorkGroups.push_back( theGroupsToUpdate[i] );
317 if ( !theWorkGroups.empty() )
318 theFaceID2Groups.Bind( theFace->GetID(), theWorkGroups );
322 //================================================================================
324 * \brief Create a slot of given width around given 1D elements lying on a triangle mesh.
325 * The slot is consrtucted by cutting faces by cylindrical surfaces made around each segment.
326 * \return Edges located at the slot boundary
328 //================================================================================
330 std::vector< SMESH_MeshAlgos::Edge >
331 SMESH_MeshAlgos::MakeSlot( SMDS_ElemIteratorPtr theSegmentIt,
334 std::vector< SMDS_MeshGroup* > & theGroupsToUpdate)
336 std::vector< Edge > bndEdges;
338 if ( !theSegmentIt || !theSegmentIt->more() || !theMesh || theWidth == 0.)
341 // put the input segments to a data map in order to be able finding neighboring ones
343 typedef std::vector< Segment* > TSegmentVec;
344 typedef NCollection_DataMap< const SMDS_MeshNode*, TSegmentVec, SMESH_Hasher > TSegmentsOfNode;
345 TSegmentsOfNode segmentsOfNode;
346 ObjectPool< Segment > segmentPool;
348 while( theSegmentIt->more() )
350 const SMDS_MeshElement* edge = theSegmentIt->next();
351 if ( edge->GetType() != SMDSAbs_Edge )
352 throw SALOME_Exception( "A segment is not a mesh edge");
354 Segment* segment = segmentPool.getNew();
355 segment->myEdge = edge;
357 for ( SMDS_NodeIteratorPtr nIt = edge->nodeIterator(); nIt->more(); )
359 const SMDS_MeshNode* n = nIt->next();
360 TSegmentVec* segVec = segmentsOfNode.ChangeSeek( n );
362 segVec = segmentsOfNode.Bound( n, TSegmentVec() );
364 segVec->push_back( segment );
368 // Cut the mesh around the segments
370 const double tol = Precision::Confusion();
371 std::vector< gp_XYZ > faceNormals;
372 SMESH_MeshAlgos::Intersector meshIntersector( theMesh, tol, faceNormals );
373 std::unique_ptr< SMESH_ElementSearcher> faceSearcher;
375 std::vector< NLink > startEdges;
376 std::vector< const SMDS_MeshNode* > faceNodes(4), edgeNodes(2);
377 std::vector<const SMDS_MeshElement *> faces(2);
378 NCollection_Map<const SMDS_MeshElement*, SMESH_Hasher > checkedFaces;
379 std::vector< IntPoint > intPoints, p(2);
380 std::vector< SMESH_NodeXYZ > facePoints(4);
381 std::vector< Intersector::TFace > cutFacePoints;
383 NCollection_DataMap< int, TGroupVec > faceID2Groups;
386 std::vector< gp_Ax1 > planeNormalVec(2);
387 gp_Ax1 * planeNormal = & planeNormalVec[0];
389 for ( TSegmentIterator segIt( segmentPool ); segIt.more(); ) // loop on all segments
391 Segment* segment = const_cast< Segment* >( segIt.next() );
393 gp_Lin segLine( segment->Ax1() );
394 gp_Ax3 cylAxis( segLine.Location(), segLine.Direction() );
395 gp_Cylinder segCylinder( cylAxis, 0.5 * theWidth );
396 double radius2( segCylinder.Radius() * segCylinder.Radius() );
398 // get normals of planes separating domains of neighboring segments
399 for ( int i = 0; i < 2; ++i ) // loop on 2 segment ends
401 planeNormal[i] = segment->Ax1(i);
403 const SMDS_MeshNode* n = segment->Node( i );
404 const TSegmentVec& segVec = segmentsOfNode( n );
405 for ( size_t iS = 0; iS < segVec.size(); ++iS )
407 if ( segVec[iS] == segment )
410 gp_Ax1 axis2 = segVec[iS]->Ax1();
411 if ( n != segVec[iS]->Node( 1 ))
412 axis2.Reverse(); // along a wire
414 planeNormal[i].SetDirection( planeNormal[i].Direction().XYZ() + axis2.Direction().XYZ() );
418 // we explore faces around a segment starting from face edges;
419 // initialize a list of starting edges
422 // get a face to start searching intersected faces from
423 const SMDS_MeshNode* n0 = segment->Node( 0 );
424 SMDS_ElemIteratorPtr fIt = n0->GetInverseElementIterator( SMDSAbs_Face );
425 const SMDS_MeshElement* face = ( fIt->more() ) ? fIt->next() : 0;
426 if ( !theMesh->Contains( face ))
429 faceSearcher.reset( SMESH_MeshAlgos::GetElementSearcher( *theMesh ));
430 face = faceSearcher->FindClosestTo( SMESH_NodeXYZ( n0 ), SMDSAbs_Face );
432 // collect face edges
433 int nbNodes = face->NbCornerNodes();
434 faceNodes.assign( face->begin_nodes(), face->end_nodes() );
435 faceNodes.resize( nbNodes + 1 );
436 faceNodes[ nbNodes ] = faceNodes[ 0 ];
437 for ( int i = 0; i < nbNodes; ++i )
438 startEdges.push_back( NLink( faceNodes[i], faceNodes[i+1] ));
441 // intersect faces located around a segment
442 checkedFaces.Clear();
443 while ( !startEdges.empty() )
445 edgeNodes[0] = startEdges[0].first;
446 edgeNodes[1] = startEdges[0].second;
448 theMesh->GetElementsByNodes( edgeNodes, faces, SMDSAbs_Face );
449 for ( size_t iF = 0; iF < faces.size(); ++iF ) // loop on faces sharing a start edge
451 const SMDS_MeshElement* face = faces[iF];
452 if ( !checkedFaces.Add( face ))
455 int nbNodes = face->NbCornerNodes();
457 throw SALOME_Exception( "MakeSlot() accepts triangles only" );
458 facePoints.assign( face->begin_nodes(), face->end_nodes() );
459 facePoints.resize( nbNodes + 1 );
460 facePoints[ nbNodes ] = facePoints[ 0 ];
462 // check if cylinder axis || face
463 const gp_XYZ& faceNorm = computeNormal( face, faceNormals );
464 bool isCylinderOnFace = ( Abs( faceNorm * cylAxis.Direction().XYZ() ) < tol );
466 if ( !isCylinderOnFace )
468 if ( Intersector::CutByPlanes( face, planeNormalVec, tol, cutFacePoints ))
469 continue; // whole face cut off
470 facePoints.swap( cutFacePoints[0] );
471 facePoints.push_back( facePoints[0] );
474 // find intersection points on face edges
476 int nbPoints = facePoints.size()-1;
478 for ( int i = 0; i < nbPoints; ++i )
480 const SMESH_NodeXYZ& n1 = facePoints[i];
481 const SMESH_NodeXYZ& n2 = facePoints[i+1];
483 size_t iP = intPoints.size();
484 intersectEdge( segCylinder, n1, n2, tol, intPoints );
487 if ( isCylinderOnFace )
488 for ( ; iP < intPoints.size(); ++iP )
489 intPoints[ iP ].myEdgeIndex = i;
491 for ( ; iP < intPoints.size(); ++iP )
492 if ( n1.Node() && n2.Node() )
493 intPoints[ iP ].myEdgeIndex = face->GetNodeIndex( n1.Node() );
495 intPoints[ iP ].myEdgeIndex = -(i+1);
497 nbFarPoints += ( segLine.SquareDistance( n1 ) > radius2 );
501 if ( nbFarPoints < nbPoints || !intPoints.empty() )
502 for ( int i = 0; i < nbPoints; ++i )
504 const SMESH_NodeXYZ& n1 = facePoints[i];
505 const SMESH_NodeXYZ& n2 = facePoints[i+1];
506 if ( n1.Node() && n2.Node() )
508 isOut( n1, planeNormal, p[0].myIsOutPln );
509 isOut( n2, planeNormal, p[1].myIsOutPln );
510 if ( !isSegmentOut( p[0].myIsOutPln, p[1].myIsOutPln ))
512 startEdges.push_back( NLink( n1.Node(), n2.Node() ));
517 if ( intPoints.size() < 2 )
520 // classify intPoints by planes
521 for ( size_t i = 0; i < intPoints.size(); ++i )
522 isOut( intPoints[i].myNode, planeNormal, intPoints[i].myIsOutPln );
526 if ( intPoints.size() > 2 )
527 intPoints.push_back( intPoints[0] );
529 for ( size_t iE = 1; iE < intPoints.size(); ++iE ) // 2 <= intPoints.size() <= 5
531 if (( intPoints[iE].myIsOutPln[0] && intPoints[iE].myIsOutPln[1] ) ||
532 ( isSegmentOut( intPoints[iE].myIsOutPln, intPoints[iE-1].myIsOutPln )))
533 continue; // intPoint is out of domain
535 // check if a cutting edge connecting two intPoints is on cylinder surface
536 if ( intPoints[iE].myEdgeIndex == intPoints[iE-1].myEdgeIndex )
537 continue; // on same edge
538 if ( intPoints[iE].myNode.Node() &&
539 intPoints[iE].myNode == intPoints[iE-1].myNode ) // coincide
542 gp_XYZ edegDir = intPoints[iE].myNode - intPoints[iE-1].myNode;
544 bool toCut; // = edegDir.SquareModulus() > tol * tol;
545 if ( intPoints.size() == 2 )
547 else if ( isCylinderOnFace )
548 toCut = cylAxis.Direction().IsParallel( edegDir, tol );
551 SMESH_NodeXYZ nBetween;
552 int eInd = intPoints[iE-1].myEdgeIndex;
554 nBetween = facePoints[( 1 - (eInd-1)) % nbPoints ];
556 nBetween = faceNodes[( 1 + eInd ) % nbNodes ];
557 toCut = ( segLine.SquareDistance( nBetween ) > radius2 );
562 // limit the edge by planes
563 if ( intPoints[iE].myIsOutPln[0] ||
564 intPoints[iE].myIsOutPln[1] )
565 cutOff( intPoints[iE], intPoints[iE-1],
566 planeNormal[ intPoints[iE].myIsOutPln[1] ], tol );
568 if ( intPoints[iE-1].myIsOutPln[0] ||
569 intPoints[iE-1].myIsOutPln[1] )
570 cutOff( intPoints[iE-1], intPoints[iE],
571 planeNormal[ intPoints[iE-1].myIsOutPln[1] ], tol );
573 edegDir = intPoints[iE].myNode - intPoints[iE-1].myNode;
574 if ( edegDir.SquareModulus() < tol * tol )
575 continue; // fully cut off
578 meshIntersector.Cut( face,
579 intPoints[iE-1].myNode, intPoints[iE-1].myEdgeIndex,
580 intPoints[iE ].myNode, intPoints[iE ].myEdgeIndex );
582 Edge e = { intPoints[iE].myNode.Node(), intPoints[iE-1].myNode.Node(), 0 };
583 segment->AddEdge( e, tol );
584 bndEdges.push_back( e );
586 findGroups( face, theGroupsToUpdate, faceID2Groups, groupVec );
589 } // loop on faces sharing an edge
591 startEdges[0] = startEdges.back();
592 startEdges.pop_back();
594 } // loop on startEdges
595 } // loop on all input segments
598 // Make cut at the end of group of segments
600 std::vector<const SMDS_MeshElement*> polySegments;
602 for ( TSegmentsOfNode::Iterator nSegsIt( segmentsOfNode ); nSegsIt.More(); nSegsIt.Next() )
604 const TSegmentVec& segVec = nSegsIt.Value();
605 if ( segVec.size() != 1 )
608 const Segment* segment = segVec[0];
609 const SMDS_MeshNode* segNode = nSegsIt.Key();
611 // find two end nodes of cut edges to make a cut between
612 if ( segment->myEndNodes.size() != 4 )
613 throw SALOME_Exception( "MakeSlot(): too short end edge?" );
614 SMESH_MeshAlgos::PolySegment linkNodes;
615 gp_Ax1 planeNorm = segment->Ax1( segNode != segment->Node(0) );
616 double minDist[2] = { 1e100, 1e100 };
617 Segment::TNodeSet::const_iterator nIt = segment->myEndNodes.begin();
618 for ( ; nIt != segment->myEndNodes.end(); ++nIt )
620 SMESH_NodeXYZ n = *nIt;
621 double d = Abs( signedDist( n, planeNorm ));
622 double diff1 = minDist[0] - d, diff2 = minDist[1] - d;
624 if ( diff1 > 0 && diff2 > 0 )
626 i = ( diff1 < diff2 );
628 else if ( diff1 > 0 )
632 else if ( diff2 > 0 )
640 linkNodes.myXYZ[ i ] = n;
643 // for ( int iSide = 0; iSide < 2; ++iSide )
645 // if ( segment->myCutEdges[ iSide ].empty() )
646 // throw SALOME_Exception( "MakeSlot(): too short end edge?" );
647 // SMESH_NodeXYZ n1 = segment->myCutEdges[ iSide ].front()._node1;
648 // SMESH_NodeXYZ n2 = segment->myCutEdges[ iSide ].back ()._node2;
649 // double d1 = Abs( signedDist( n1, planeNorm ));
650 // double d2 = Abs( signedDist( n2, planeNorm ));
651 // linkNodes.myXYZ [ iSide ] = ( d1 < d2 ) ? n1 : n2;
652 // linkNodes.myNode1[ iSide ] = linkNodes.myNode2[ iSide ] = 0;
654 linkNodes.myVector = planeNorm.Direction() ^ (linkNodes.myXYZ[0] - linkNodes.myXYZ[1]);
655 linkNodes.myNode1[ 0 ] = linkNodes.myNode2[ 0 ] = 0;
656 linkNodes.myNode1[ 1 ] = linkNodes.myNode2[ 1 ] = 0;
658 // create segments connecting linkNodes
659 std::vector<const SMDS_MeshElement*> newSegments;
660 std::vector<const SMDS_MeshNode*> newNodes;
661 SMESH_MeshAlgos::TListOfPolySegments polySegs(1, linkNodes);
662 SMESH_MeshAlgos::MakePolyLine( theMesh, polySegs, newSegments, newNodes,
663 /*group=*/0, faceSearcher.get() );
664 // cut faces by newSegments
666 for ( size_t i = 0; i < newSegments.size(); ++i )
668 intPoints[0].myNode = edgeNodes[0] = newSegments[i]->GetNode(0);
669 intPoints[1].myNode = edgeNodes[1] = newSegments[i]->GetNode(1);
671 // find an underlying face
672 gp_XYZ middle = 0.5 * ( intPoints[0].myNode + intPoints[1].myNode );
673 const SMDS_MeshElement* face = faceSearcher->FindClosestTo( middle, SMDSAbs_Face );
675 // find intersected edges of the face
676 int nbNodes = face->NbCornerNodes();
677 faceNodes.assign( face->begin_nodes(), face->end_nodes() );
678 faceNodes.resize( nbNodes + 1 );
679 faceNodes[ nbNodes ] = faceNodes[ 0 ];
680 for ( int iP = 0; iP < 2; ++iP )
682 intPoints[iP].myEdgeIndex = -1;
683 for ( int iN = 0; iN < nbNodes && intPoints[iP].myEdgeIndex < 0; ++iN )
685 SMDS_LinearEdge edge( faceNodes[iN], faceNodes[iN+1] );
686 if ( SMESH_MeshAlgos::GetDistance( &edge, intPoints[iP].myNode) < tol )
687 intPoints[iP].myEdgeIndex = iN;
693 computeNormal( face, faceNormals );
694 meshIntersector.Cut( face,
695 intPoints[0].myNode, intPoints[0].myEdgeIndex,
696 intPoints[1].myNode, intPoints[1].myEdgeIndex );
698 Edge e = { intPoints[0].myNode.Node(), intPoints[1].myNode.Node(), 0 };
699 bndEdges.push_back( e );
701 findGroups( face, theGroupsToUpdate, faceID2Groups, groupVec );
703 // add cut points to an adjacent face at ends of poly-line
704 // if they fall onto face edges
705 if (( i == 0 && intPoints[0].myEdgeIndex >= 0 ) ||
706 ( i == newSegments.size() - 1 && intPoints[1].myEdgeIndex >= 0 ))
708 for ( int iE = 0; iE < 2; ++iE ) // loop on ends of a new segment
710 if ( iE ? ( i != newSegments.size() - 1 ) : ( i != 0 ))
712 int iEdge = intPoints[ iE ].myEdgeIndex;
713 edgeNodes[0] = faceNodes[ iEdge ];
714 edgeNodes[1] = faceNodes[ iEdge+1 ];
715 theMesh->GetElementsByNodes( edgeNodes, faces, SMDSAbs_Face );
716 for ( size_t iF = 0; iF < faces.size(); ++iF )
717 if ( faces[iF] != face )
719 int iN1 = faces[iF]->GetNodeIndex( edgeNodes[0] );
720 int iN2 = faces[iF]->GetNodeIndex( edgeNodes[1] );
721 intPoints[ iE ].myEdgeIndex = Abs( iN1 - iN2 ) == 1 ? Min( iN1, iN2 ) : 2;
722 computeNormal( faces[iF], faceNormals );
723 meshIntersector.Cut( faces[iF],
724 intPoints[iE].myNode, intPoints[iE].myEdgeIndex,
725 intPoints[iE].myNode, intPoints[iE].myEdgeIndex );
727 findGroups( faces[iF], theGroupsToUpdate, faceID2Groups, groupVec );
732 } // loop on newSegments
734 polySegments.insert( polySegments.end(), newSegments.begin(), newSegments.end() );
736 } // loop on map of input segments
738 // actual mesh splitting
739 TElemIntPairVec new2OldFaces;
740 TNodeIntPairVec new2OldNodes;
741 meshIntersector.MakeNewFaces( new2OldFaces, new2OldNodes, /*sign=*/1, /*optimize=*/true );
743 // add new faces to theGroupsToUpdate
744 for ( size_t i = 0; i < new2OldFaces.size(); ++i )
746 const SMDS_MeshElement* newFace = new2OldFaces[i].first;
747 const int oldFaceID = new2OldFaces[i].second;
748 if ( !newFace ) continue;
750 if ( TGroupVec* groups = const_cast< TGroupVec* >( faceID2Groups.Seek( oldFaceID )))
751 for ( size_t iG = 0; iG < groups->size(); ++iG )
752 (*groups)[ iG ]->Add( newFace );
755 // remove poly-line edges
756 for ( size_t i = 0; i < polySegments.size(); ++i )
758 edgeNodes[0] = polySegments[i]->GetNode(0);
759 edgeNodes[1] = polySegments[i]->GetNode(1);
761 theMesh->RemoveFreeElement( polySegments[i] );
763 if ( edgeNodes[0]->NbInverseElements() == 0 )
764 theMesh->RemoveNode( edgeNodes[0] );
765 if ( edgeNodes[1]->NbInverseElements() == 0 )
766 theMesh->RemoveNode( edgeNodes[1] );