1 // Copyright (C) 2007-2019 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : SMESH_Offset.cxx
23 // Created : Mon Dec 25 15:52:38 2017
24 // Author : Edward AGAPOV (eap)
26 #include "SMESH_MeshAlgos.hxx"
28 #include <SMDS_PolygonalFaceOfNodes.hxx>
29 #include "SMDS_Mesh.hxx"
31 #include <Utils_SALOME_Exception.hxx>
33 #include <Bnd_B3d.hxx>
34 #include <NCollection_Map.hxx>
38 #include <boost/container/flat_set.hpp>
39 #include <boost/dynamic_bitset.hpp>
43 const int theMaxNbFaces = 256; // max number of faces sharing a node
45 typedef NCollection_DataMap< const SMDS_MeshNode*, const SMDS_MeshNode*, SMESH_Hasher > TNNMap;
46 typedef NCollection_Map< SMESH_Link, SMESH_Link > TLinkMap;
48 //--------------------------------------------------------------------------------
50 * \brief Intersected face side storing a node created at this intersection
51 * and a intersected face
56 const SMDS_MeshNode* myNode[2]; // side nodes. WARNING: don't set them directly, use Set()
57 mutable SMESH_NodeXYZ myIntNode; // intersection node
58 const SMDS_MeshElement* myFace; // cutter face
59 int myIndex; // index of a node on the same link
61 CutLink(const SMDS_MeshNode* node1=0,
62 const SMDS_MeshNode* node2=0,
63 const SMDS_MeshElement* face=0,
64 const int index=0) { Set ( node1, node2, face, index ); }
66 void Set( const SMDS_MeshNode* node1,
67 const SMDS_MeshNode* node2,
68 const SMDS_MeshElement* face,
71 myNode[0] = node1; myNode[1] = node2; myFace = face; myIndex = index; myReverse = false;
72 if ( myNode[0] && ( myReverse = ( myNode[0]->GetID() > myNode[1]->GetID() )))
73 std::swap( myNode[0], myNode[1] );
75 const SMDS_MeshNode* IntNode() const { return myIntNode.Node(); }
76 const SMDS_MeshNode* Node1() const { return myNode[ myReverse ]; }
77 const SMDS_MeshNode* Node2() const { return myNode[ !myReverse ]; }
79 static Standard_Integer HashCode(const CutLink& link,
80 const Standard_Integer upper)
82 Standard_Integer n = ( link.myNode[0]->GetID() +
83 link.myNode[1]->GetID() +
85 return ::HashCode( n, upper );
87 static Standard_Boolean IsEqual(const CutLink& link1, const CutLink& link2 )
89 return ( link1.myNode[0] == link2.myNode[0] &&
90 link1.myNode[1] == link2.myNode[1] &&
91 link1.myIndex == link2.myIndex );
95 typedef NCollection_Map< CutLink, CutLink > TCutLinkMap;
97 //--------------------------------------------------------------------------------
99 * \brief Part of a divided face edge
103 const SMDS_MeshNode* myNode1;
104 const SMDS_MeshNode* myNode2;
105 int myIndex; // positive -> side index, negative -> State
106 const SMDS_MeshElement* myFace;
108 enum State { _INTERNAL = -1, _COPLANAR = -2 };
110 void Set( const SMDS_MeshNode* Node1,
111 const SMDS_MeshNode* Node2,
112 const SMDS_MeshElement* Face = 0,
113 int EdgeIndex = _INTERNAL )
114 { myNode1 = Node1; myNode2 = Node2; myIndex = EdgeIndex; myFace = Face; }
116 // bool HasSameNode( const EdgePart& other ) { return ( myNode1 == other.myNode1 ||
117 // myNode1 == other.myNode2 ||
118 // myNode2 == other.myNode1 ||
119 // myNode2 == other.myNode2 );
121 bool IsInternal() const { return myIndex < 0; }
122 bool IsTwin( const EdgePart& e ) const { return myNode1 == e.myNode2 && myNode2 == e.myNode1; }
123 bool IsSame( const EdgePart& e ) const {
124 return (( myNode1 == e.myNode2 && myNode2 == e.myNode1 ) ||
125 ( myNode1 == e.myNode1 && myNode2 == e.myNode2 )); }
126 bool ReplaceCoplanar( const EdgePart& e );
127 operator SMESH_Link() const { return SMESH_Link( myNode1, myNode2 ); }
128 operator gp_Vec() const { return SMESH_NodeXYZ( myNode2 ) - SMESH_NodeXYZ( myNode1 ); }
131 //--------------------------------------------------------------------------------
133 * \brief Loop of EdgePart's forming a new face which is a part of CutFace
135 struct EdgeLoop : public SMDS_PolygonalFaceOfNodes
137 std::vector< const EdgePart* > myLinks;
138 bool myIsBndConnected; //!< is there a path to CutFace side edges
139 bool myHasPending; //!< an edge encounters twice
141 EdgeLoop() : SMDS_PolygonalFaceOfNodes( std::vector<const SMDS_MeshNode *>() ) {}
142 void Clear() { myLinks.clear(); myIsBndConnected = false; myHasPending = false; }
143 bool SetConnected() { bool was = myIsBndConnected; myIsBndConnected = true; return !was; }
144 size_t Contains( const SMDS_MeshNode* n ) const
146 for ( size_t i = 0; i < myLinks.size(); ++i )
147 if ( myLinks[i]->myNode1 == n ) return i + 1;
150 virtual int NbNodes() const { return myLinks.size(); }
151 virtual SMDS_ElemIteratorPtr nodesIterator() const
153 return setNodes(), SMDS_PolygonalFaceOfNodes::nodesIterator();
155 virtual SMDS_NodeIteratorPtr nodeIterator() const
157 return setNodes(), SMDS_PolygonalFaceOfNodes::nodeIterator();
159 void setNodes() const //!< set nodes to SMDS_PolygonalFaceOfNodes
161 EdgeLoop* me = const_cast<EdgeLoop*>( this );
162 me->myNodes.resize( NbNodes() );
164 for ( size_t i = 1; i < myNodes.size(); ++i ) {
165 if ( myLinks[ i ]->myNode1->GetID() < myLinks[ iMin ]->myNode1->GetID() )
168 for ( size_t i = 0; i < myNodes.size(); ++i )
169 me->myNodes[ i ] = myLinks[ ( iMin + i ) % myNodes.size() ]->myNode1;
173 //--------------------------------------------------------------------------------
175 * \brief Set of EdgeLoop's constructed from a CutFace
179 std::vector< EdgeLoop > myLoops; //!< buffer of EdgeLoop's
180 size_t myNbLoops; //!< number of constructed loops
182 const EdgePart* myEdge0; //!< & CutFace.myLinks[0]
183 size_t myNbUsedEdges; //!< nb of EdgePart's added to myLoops
184 boost::dynamic_bitset<> myIsUsedEdge; //!< is i-th EdgePart of CutFace is in any EdgeLoop
185 std::vector< EdgeLoop* > myLoopOfEdge; //!< EdgeLoop of CutFace.myLinks[i]
186 std::vector< EdgePart* > myCandidates; //!< EdgePart's starting at the same node
188 EdgeLoopSet(): myLoops(100) {}
190 void Init( const std::vector< EdgePart >& edges )
192 size_t nb = edges.size();
193 myEdge0 = & edges[0];
196 myIsUsedEdge.reset();
197 myIsUsedEdge.resize( nb, false );
198 myLoopOfEdge.clear();
199 myLoopOfEdge.resize( nb, (EdgeLoop*) 0 );
201 EdgeLoop& AddNewLoop()
203 if ( ++myNbLoops >= myLoops.size() )
204 myLoops.resize( myNbLoops + 10 );
205 myLoops[ myNbLoops-1 ].Clear();
206 return myLoops[ myNbLoops-1 ];
208 bool AllEdgesUsed() const { return myNbUsedEdges == myLoopOfEdge.size(); }
210 bool AddEdge( EdgePart& edge )
212 size_t i = Index( edge );
213 if ( myIsUsedEdge[ i ])
215 myLoops[ myNbLoops-1 ].myLinks.push_back( &edge );
216 myLoopOfEdge[ i ] = & myLoops[ myNbLoops-1 ];
217 myIsUsedEdge[ i ] = true;
221 void Erase( EdgeLoop* loop )
223 for ( size_t iE = 0; iE < loop->myLinks.size(); ++iE )
224 myLoopOfEdge[ Index( *loop->myLinks[ iE ] )] = 0;
227 void Join( EdgeLoop& loop1, size_t iAfterConcact,
228 EdgeLoop& loop2, size_t iFromEdge2 )
230 std::vector< const EdgePart* > linksAfterContact( loop1.myLinks.begin() + iAfterConcact,
231 loop1.myLinks.end() );
232 loop1.myLinks.reserve( loop2.myLinks.size() + loop1.myLinks.size() );
233 loop1.myLinks.resize( iAfterConcact );
234 loop1.myLinks.insert( loop1.myLinks.end(),
235 loop2.myLinks.begin() + iFromEdge2, loop2.myLinks.end() );
236 loop1.myLinks.insert( loop1.myLinks.end(),
237 loop2.myLinks.begin(), loop2.myLinks.begin() + iFromEdge2 );
238 loop1.myLinks.insert( loop1.myLinks.end(),
239 linksAfterContact.begin(), linksAfterContact.end() );
240 loop1.myIsBndConnected = loop2.myIsBndConnected;
242 for ( size_t iE = 0; iE < loop1.myLinks.size(); ++iE )
243 myLoopOfEdge[ Index( *loop1.myLinks[ iE ] )] = & loop1;
245 size_t Index( const EdgePart& edge ) const { return &edge - myEdge0; }
246 EdgeLoop* GetLoopOf( const EdgePart* edge ) { return myLoopOfEdge[ Index( *edge )]; }
249 //--------------------------------------------------------------------------------
251 * \brief Intersections of a face
255 mutable std::vector< EdgePart > myLinks;
256 const SMDS_MeshElement* myInitFace;
258 CutFace( const SMDS_MeshElement* face ): myInitFace( face ) {}
259 void AddEdge( const CutLink& p1,
261 const SMDS_MeshElement* cutter,
263 const int iNotOnPlane = -1) const;
264 void AddPoint( const CutLink& p1, const CutLink& p2, double tol ) const;
265 bool ReplaceNodes( const TNNMap& theRm2KeepMap ) const;
267 int NbInternalEdges() const;
268 void MakeLoops( EdgeLoopSet& loops, const gp_XYZ& theFaceNorm ) const;
269 bool RemoveInternalLoops( EdgeLoopSet& theLoops ) const;
270 void CutOffLoops( EdgeLoopSet& theLoops,
271 const double theSign,
272 const std::vector< gp_XYZ >& theNormals,
273 std::vector< EdgePart >& theCutOffLinks,
274 TLinkMap& theCutOffCoplanarLinks) const;
275 void InitLinks() const;
276 bool IsCoplanar( const EdgePart* edge ) const;
278 static Standard_Integer HashCode(const CutFace& f, const Standard_Integer upper)
280 return ::HashCode( f.myInitFace->GetID(), upper );
282 static Standard_Boolean IsEqual(const CutFace& f1, const CutFace& f2 )
284 return f1.myInitFace == f2.myInitFace;
290 EdgePart* getTwin( const EdgePart* edge ) const;
293 typedef NCollection_Map< CutFace, CutFace > TCutFaceMap;
295 //--------------------------------------------------------------------------------
297 * \brief Intersection point of two edges of co-planar triangles
301 size_t myEdgeInd[2]; //!< edge indices of triangles
302 double myU [2]; //!< parameter [0,1] on edges of triangles
303 SMESH_NodeXYZ myNode; //!< intersection node
304 bool myIsCollinear;//!< edges are collinear
306 IntPoint2D() : myIsCollinear( false ) {}
308 void InitLink( CutLink& link, int iFace, const std::vector< SMESH_NodeXYZ >& nodes ) const
310 link.Set( nodes[ myEdgeInd[ iFace ] ].Node(),
311 nodes[( myEdgeInd[ iFace ] + 1 ) % nodes.size() ].Node(),
313 link.myIntNode = myNode;
315 const SMDS_MeshNode* Node() const { return myNode.Node(); }
317 struct IntPoint2DCompare
320 IntPoint2DCompare( int iFace=0 ): myI( iFace ) {}
321 bool operator() ( const IntPoint2D* ip1, const IntPoint2D* ip2 ) const
323 return ip1->myU[ myI ] < ip2->myU[ myI ];
325 bool operator() ( const IntPoint2D& ip1, const IntPoint2D& ip2 ) const
327 return ip1.myU[ myI ] < ip2.myU[ myI ];
330 typedef boost::container::flat_set< IntPoint2D, IntPoint2DCompare > TIntPointSet;
331 typedef boost::container::flat_set< IntPoint2D*, IntPoint2DCompare > TIntPointPtrSet;
333 //--------------------------------------------------------------------------------
335 * \brief Face used to find translated position of the node
339 const SMDS_MeshElement* myFace;
340 SMESH_TNodeXYZ myNode1; //!< nodes neighboring another node of myFace
341 SMESH_TNodeXYZ myNode2;
342 const gp_XYZ* myNorm;
343 bool myNodeRightOrder;
344 void operator=(const SMDS_MeshElement* f) { myFace = f; }
345 const SMDS_MeshElement* operator->() { return myFace; }
346 void SetNodes( int i0, int i1 ) //!< set myNode's
348 myNode1.Set( myFace->GetNode( i1 ));
349 int i2 = ( i0 - 1 + myFace->NbCornerNodes() ) % myFace->NbCornerNodes();
351 i2 = ( i0 + 1 ) % myFace->NbCornerNodes();
352 myNode2.Set( myFace->GetNode( i2 ));
353 myNodeRightOrder = ( Abs( i2-i1 ) == 1 ) ? i2 > i1 : i2 < i1;
355 void SetOldNodes( const SMDS_Mesh& theSrcMesh )
357 myNode1.Set( theSrcMesh.FindNode( myNode1->GetID() ));
358 myNode2.Set( theSrcMesh.FindNode( myNode2->GetID() ));
360 bool SetNormal( const std::vector< gp_XYZ >& faceNormals )
362 myNorm = & faceNormals[ myFace->GetID() ];
363 return ( myNorm->SquareModulus() > gp::Resolution() * gp::Resolution() );
365 const gp_XYZ& Norm() const { return *myNorm; }
368 //--------------------------------------------------------------------------------
370 * \brief Offset plane used to find translated position of the node
377 gp_Lin myLines[2]; //!< line of intersection with neighbor OffsetPlane's
381 void Init( const gp_XYZ& node, Face& tria, double offset )
385 myPln = gp_Pln( node + tria.Norm() * offset, tria.Norm() );
386 myIsLineOk[0] = myIsLineOk[1] = false;
387 myWeight[0] = myWeight[1] = 0;
389 bool ComputeIntersectionLine( OffsetPlane& pln );
390 void SetSkewLine( const gp_Lin& line );
391 gp_XYZ GetCommonPoint( int & nbOkPoints, double& sumWeight );
392 gp_XYZ ProjectNodeOnLine( int & nbOkPoints );
393 double Weight() const { return myWeight[0] + myWeight[1]; }
396 //================================================================================
398 * \brief Set the second line
400 //================================================================================
402 void OffsetPlane::SetSkewLine( const gp_Lin& line )
405 gp_XYZ n = myLines[0].Direction().XYZ() ^ myLines[1].Direction().XYZ();
406 if (( myIsLineOk[1] = n.SquareModulus() > gp::Resolution() ))
407 myPln = gp_Pln( myPln.Location(), n );
410 //================================================================================
412 * \brief Project myNode on myLine[0]
414 //================================================================================
416 gp_XYZ OffsetPlane::ProjectNodeOnLine( int & nbOkPoints )
418 gp_XYZ p = gp::Origin().XYZ();
421 gp_Vec l2n( myLines[0].Location(), myNode );
422 double u = l2n * myLines[0].Direction();
423 p = myLines[0].Location().XYZ() + u * myLines[0].Direction().XYZ();
429 //================================================================================
431 * \brief Computes intersection point of myLines
433 //================================================================================
435 gp_XYZ OffsetPlane::GetCommonPoint( int & nbOkPoints, double& sumWeight )
437 if ( !myIsLineOk[0] || !myIsLineOk[1] )
439 // sumWeight += myWeight[0];
440 // return ProjectNodeOnLine( nbOkPoints ) * myWeight[0];
441 return gp::Origin().XYZ();
446 gp_Vec lPerp0 = myLines[0].Direction().XYZ() ^ myPln.Axis().Direction().XYZ();
447 double dot01 = lPerp0 * myLines[1].Direction().XYZ();
448 if ( Abs( dot01 ) > 0.05 )
450 gp_Vec l0l1 = myLines[1].Location().XYZ() - myLines[0].Location().XYZ();
451 double u1 = - ( lPerp0 * l0l1 ) / dot01;
452 p = ( myLines[1].Location().XYZ() + myLines[1].Direction().XYZ() * u1 );
456 gp_Vec lv0( myLines[0].Location(), myNode), lv1(myLines[1].Location(), myNode );
457 double dot0( lv0 * myLines[0].Direction() ), dot1( lv1 * myLines[1].Direction() );
458 p = 0.5 * ( myLines[0].Location().XYZ() + myLines[0].Direction().XYZ() * dot0 );
459 p += 0.5 * ( myLines[1].Location().XYZ() + myLines[1].Direction().XYZ() * dot1 );
462 sumWeight += Weight();
468 //================================================================================
470 * \brief Compute line of intersection of 2 planes
472 //================================================================================
474 bool OffsetPlane::ComputeIntersectionLine( OffsetPlane& theNextPln )
476 const gp_XYZ& n1 = myFace->Norm();
477 const gp_XYZ& n2 = theNextPln.myFace->Norm();
479 gp_XYZ lineDir = n1 ^ n2;
482 double x = Abs( lineDir.X() );
483 double y = Abs( lineDir.Y() );
484 double z = Abs( lineDir.Z() );
486 int cooMax; // max coordinate
488 if (x > z) cooMax = 1;
492 if (y > z) cooMax = 2;
497 if ( Abs( lineDir.Coord( cooMax )) < 0.05 )
499 // parallel planes - intersection is an offset of the common edge
500 linePos = 0.5 * ( myPln.Location().XYZ() + theNextPln.myPln.Location().XYZ() );
501 lineDir = myNode - myFace->myNode2;
507 // the constants in the 2 plane equations
508 double d1 = - ( n1 * myPln.Location().XYZ() );
509 double d2 = - ( n2 * theNextPln.myPln.Location().XYZ() );
514 linePos.SetY(( d2*n1.Z() - d1*n2.Z()) / lineDir.X() );
515 linePos.SetZ(( d1*n2.Y() - d2*n1.Y()) / lineDir.X() );
518 linePos.SetX(( d1*n2.Z() - d2*n1.Z()) / lineDir.Y() );
520 linePos.SetZ(( d2*n1.X() - d1*n2.X()) / lineDir.Y() );
523 linePos.SetX(( d2*n1.Y() - d1*n2.Y()) / lineDir.Z() );
524 linePos.SetY(( d1*n2.X() - d2*n1.X()) / lineDir.Z() );
527 myWeight[0] = lineDir.SquareModulus();
529 myWeight[0] = 2. - myWeight[0];
531 myLines [ 0 ].SetDirection( lineDir );
532 myLines [ 0 ].SetLocation ( linePos );
533 myIsLineOk[ 0 ] = ok;
535 theNextPln.myLines [ 1 ] = myLines[ 0 ];
536 theNextPln.myIsLineOk[ 1 ] = ok;
537 theNextPln.myWeight [ 1 ] = myWeight[ 0 ];
542 //================================================================================
544 * \brief Return a translated position of a node
545 * \param [in] new2OldNodes - new and old nodes
546 * \param [in] faceNormals - normals to input faces
547 * \param [in] theSrcMesh - initial mesh
548 * \param [in] theNewPos - a computed normal
549 * \return bool - true if theNewPos is computed
551 //================================================================================
553 bool getTranslatedPosition( const SMDS_MeshNode* theNewNode,
554 const double theOffset,
556 const double theSign,
557 const std::vector< gp_XYZ >& theFaceNormals,
558 SMDS_Mesh& theSrcMesh,
561 bool useOneNormal = true;
563 // check if theNewNode needs an average position, i.e. theNewNode is convex
564 // SMDS_ElemIteratorPtr faceIt = theNewNode->GetInverseElementIterator();
565 // const SMDS_MeshElement* f0 = faceIt->next();
566 // const gp_XYZ& norm0 = theFaceNormals[ f0->GetID() ];
567 // const SMESH_NodeXYZ nodePos = theNewNode;
568 // while ( faceIt->more() )
570 // const SMDS_MeshElement* f = faceIt->next();
571 // const int nodeInd = f->GetNodeIndex( theNewNode );
572 // SMESH_NodeXYZ nodePos2 = f->GetWrapNode( nodeInd + 1 );
574 // const gp_XYZ nnDir = ( nodePos2 - nodePos ).Normalized();
579 // const double dot = norm0 * nnDir;
584 // get faces surrounding theNewNode and sort them
585 Face faces[ theMaxNbFaces ];
586 SMDS_ElemIteratorPtr faceIt = theNewNode->GetInverseElementIterator();
587 faces[0] = faceIt->next();
588 while ( !faces[0].SetNormal( theFaceNormals ) && faceIt->more() )
589 faces[0] = faceIt->next();
590 int i0 = faces[0]->GetNodeIndex( theNewNode );
591 int i1 = ( i0 + 1 ) % faces[0]->NbCornerNodes();
592 faces[0].SetNodes( i0, i1 );
593 TIDSortedElemSet elemSet, avoidSet;
595 const SMDS_MeshElement* f;
596 for ( ; faceIt->more() && iFace < theMaxNbFaces; faceIt->next() )
598 avoidSet.insert( faces[ iFace ].myFace );
599 f = SMESH_MeshAlgos::FindFaceInSet( theNewNode, faces[ iFace ].myNode2.Node(),
600 elemSet, avoidSet, &i0, &i1 );
603 std::reverse( &faces[0], &faces[0] + iFace + 1 );
604 for ( int i = 0; i <= iFace; ++i )
606 std::swap( faces[i].myNode1, faces[i].myNode2 );
607 faces[i].myNodeRightOrder = !faces[i].myNodeRightOrder;
609 f = SMESH_MeshAlgos::FindFaceInSet( theNewNode, faces[ iFace ].myNode2.Node(),
610 elemSet, avoidSet, &i0, &i1 );
614 faces[ ++iFace ] = f;
615 faces[ iFace ].SetNodes( i0, i1 );
616 faces[ iFace ].SetNormal( theFaceNormals );
618 int nbFaces = iFace + 1;
620 theNewPos.SetCoord( 0, 0, 0 );
621 gp_XYZ oldXYZ = SMESH_NodeXYZ( theNewNode );
623 // check if all faces are co-planar
624 bool isPlanar = true;
625 const double tol = 1e-2;
626 for ( int i = 1; i < nbFaces && isPlanar; ++i )
627 isPlanar = ( faces[i].Norm() - faces[i-1].Norm() ).SquareModulus() < tol*tol;
631 theNewPos = oldXYZ + faces[0].Norm() * theOffset;
635 // prepare OffsetPlane's
636 OffsetPlane pln[ theMaxNbFaces ];
637 for ( int i = 0; i < nbFaces; ++i )
639 faces[i].SetOldNodes( theSrcMesh );
640 pln[i].Init( oldXYZ, faces[i], theOffset );
642 // intersect neighboring OffsetPlane's
644 for ( int i = 1; i < nbFaces; ++i )
645 nbOkPoints += pln[ i-1 ].ComputeIntersectionLine( pln[ i ]);
646 nbOkPoints += pln[ nbFaces-1 ].ComputeIntersectionLine( pln[ 0 ]);
648 // move intersection lines to over parallel planes
649 if ( nbOkPoints > 1 )
650 for ( int i = 0; i < nbFaces; ++i )
651 if ( pln[i].myIsLineOk[0] && !pln[i].myIsLineOk[1] )
652 for ( int j = 1; j < nbFaces && !pln[i].myIsLineOk[1]; ++j )
654 int i2 = ( i + j ) % nbFaces;
655 if ( pln[i2].myIsLineOk[0] )
656 pln[i].SetSkewLine( pln[i2].myLines[0] );
659 // get the translated position
661 double sumWegith = 0;
662 const double minWeight = Sin( 30 * M_PI / 180. ) * Sin( 30 * M_PI / 180. );
663 for ( int i = 0; i < nbFaces; ++i )
664 if ( pln[ i ].Weight() > minWeight )
665 theNewPos += pln[ i ].GetCommonPoint( nbOkPoints, sumWegith );
667 if ( nbOkPoints == 0 )
669 // there is only one feature edge;
670 // find the theNewPos by projecting oldXYZ to any intersection line
671 for ( int i = 0; i < nbFaces; ++i )
672 theNewPos += pln[ i ].ProjectNodeOnLine( nbOkPoints );
674 if ( nbOkPoints == 0 )
676 theNewPos = oldXYZ + faces[0].Norm() * theOffset;
679 sumWegith = nbOkPoints;
681 theNewPos /= sumWegith;
684 // mark theNewNode if it is concave
685 useOneNormal = false;
686 gp_Vec moveVec( oldXYZ, theNewPos );
687 for ( int i = 0, iPrev = nbFaces-1; i < nbFaces; iPrev = i++ )
689 gp_Vec nodeVec( oldXYZ, faces[ i ].myNode1 );
690 double u = ( moveVec * nodeVec ) / nodeVec.SquareMagnitude();
691 if ( u > 0.5 ) // param [0,1] on nodeVec
693 theNewNode->setIsMarked( true );
697 gp_XYZ inFaceVec = faces[ i ].Norm() ^ nodeVec.XYZ();
698 double dot = inFaceVec * faces[ iPrev ].Norm();
699 if ( !faces[ i ].myNodeRightOrder )
701 if ( dot * theSign < 0 )
703 gp_XYZ p1 = oldXYZ + faces[ i ].Norm() * theOffset;
704 gp_XYZ p2 = oldXYZ + faces[ iPrev ].Norm() * theOffset;
705 useOneNormal = ( p1 - p2 ).SquareModulus() > 1e-12;
708 if ( useOneNormal && theNewNode->isMarked() )
717 namespace SMESH_MeshAlgos
719 //--------------------------------------------------------------------------------
721 * \brief Intersect faces of a mesh
723 struct Intersector::Algo
727 const std::vector< gp_XYZ >& myNormals;
728 TCutLinkMap myCutLinks; //!< assure sharing of new nodes
729 TCutFaceMap myCutFaces;
730 TNNMap myRemove2KeepNodes; //!< node merge map
732 // data to intersect 2 faces
733 const SMDS_MeshElement* myFace1;
734 const SMDS_MeshElement* myFace2;
735 std::vector< SMESH_NodeXYZ > myNodes1, myNodes2;
736 std::vector< double > myDist1, myDist2;
737 int myInd1, myInd2; // coordinate indices on an axis-aligned plane
738 int myNbOnPlane1, myNbOnPlane2;
739 TIntPointSet myIntPointSet;
741 Algo( SMDS_Mesh* mesh, double tol, const std::vector< gp_XYZ >& normals )
745 //myEps( Sqrt( std::numeric_limits<double>::min() )),
746 //myEps( gp::Resolution() ),
749 void Cut( const SMDS_MeshElement* face1,
750 const SMDS_MeshElement* face2,
751 const int nbCommonNodes );
752 void Cut( const SMDS_MeshElement* face,
753 SMESH_NodeXYZ& lineEnd1,
755 SMESH_NodeXYZ& lineEnd2,
757 void MakeNewFaces( TElemIntPairVec& theNew2OldFaces,
758 TNodeIntPairVec& theNew2OldNodes,
759 const double theSign,
760 const bool theOptimize );
762 void IntersectNewEdges( const CutFace& theCFace );
766 bool isPlaneIntersected( const gp_XYZ& n2,
768 const std::vector< SMESH_NodeXYZ >& nodes1,
769 std::vector< double > & dist1,
772 void computeIntervals( const std::vector< SMESH_NodeXYZ >& nodes,
773 const std::vector< double >& dist,
779 void addLink ( CutLink& link );
780 bool findLink( CutLink& link );
781 bool coincide( const gp_XYZ& p1, const gp_XYZ& p2, const double tol ) const
783 return ( p1 - p2 ).SquareModulus() < tol * tol;
785 gp_XY p2D( const gp_XYZ& p ) const { return gp_XY( p.Coord( myInd1 ), p.Coord( myInd2 )); }
787 void intersectLink( const std::vector< SMESH_NodeXYZ >& nodes1,
788 const std::vector< double > & dist1,
790 const SMDS_MeshElement* face2,
792 void findIntPointOnPlane( const std::vector< SMESH_NodeXYZ >& nodes,
793 const std::vector< double > & dist,
795 void replaceIntNode( const SMDS_MeshNode* nToKeep, const SMDS_MeshNode* nToRemove );
796 void computeIntPoint( const double u1,
801 const SMDS_MeshNode* & node1,
802 const SMDS_MeshNode* & node2);
803 void cutCollinearLink( const int iNotOnPlane1,
804 const std::vector< SMESH_NodeXYZ >& nodes1,
805 const SMDS_MeshElement* face2,
806 const CutLink& link1,
807 const CutLink& link2);
808 void setPlaneIndices( const gp_XYZ& planeNorm );
809 bool intersectEdgeEdge( const gp_XY s1p0, const gp_XY s1p1,
810 const gp_XY s2p0, const gp_XY s2p1,
811 double & t1, double & t2,
812 bool & isCollinear );
813 bool intersectEdgeEdge( int iE1, int iE2, IntPoint2D& intPoint );
814 bool isPointInTriangle( const gp_XYZ& p, const std::vector< SMESH_NodeXYZ >& nodes );
815 const SMDS_MeshNode* createNode( const gp_XYZ& p );
818 //================================================================================
820 * \brief Return coordinate index with maximal abs value
822 //================================================================================
824 int MaxIndex( const gp_XYZ& x )
826 int iMaxCoo = ( Abs( x.X()) < Abs( x.Y() )) + 1;
827 if ( Abs( x.Coord( iMaxCoo )) < Abs( x.Z() ))
831 //================================================================================
833 * \brief Store a CutLink
835 //================================================================================
837 const SMDS_MeshNode* Intersector::Algo::createNode( const gp_XYZ& p )
839 const SMDS_MeshNode* n = myMesh->AddNode( p.X(), p.Y(), p.Z() );
840 n->setIsMarked( true ); // cut nodes are marked
844 //================================================================================
846 * \brief Store a CutLink
848 //================================================================================
850 void Intersector::Algo::addLink( CutLink& link )
853 const CutLink* added = & myCutLinks.Added( link );
854 while ( added->myIntNode.Node() != link.myIntNode.Node() )
856 if ( !added->myIntNode )
858 added->myIntNode = link.myIntNode;
864 added = & myCutLinks.Added( link );
870 //================================================================================
872 * \brief Find a CutLink with an intersection point coincident with that of a given link
874 //================================================================================
876 bool Intersector::Algo::findLink( CutLink& link )
879 while ( myCutLinks.Contains( link ))
881 const CutLink* added = & myCutLinks.Added( link );
882 if ( !!added->myIntNode && coincide( added->myIntNode, link.myIntNode, myTol ))
884 link.myIntNode = added->myIntNode;
892 //================================================================================
894 * \brief Check if a triangle intersects the plane of another triangle
895 * \param [in] nodes1 - nodes of triangle 1
896 * \param [in] n2 - normal of triangle 2
897 * \param [in] d2 - a constant of the plane equation 2
898 * \param [out] dist1 - distance of nodes1 from the plane 2
899 * \param [out] nbOnPlane - number of nodes1 lying on the plane 2
900 * \return bool - true if the triangle intersects the plane 2
902 //================================================================================
904 bool Intersector::Algo::isPlaneIntersected( const gp_XYZ& n2,
906 const std::vector< SMESH_NodeXYZ >& nodes1,
907 std::vector< double > & dist1,
911 iNotOnPlane1 = nbOnPlane1 = 0;
912 dist1.resize( nodes1.size() );
913 for ( size_t i = 0; i < nodes1.size(); ++i )
915 dist1[i] = n2 * nodes1[i] + d2;
916 if ( Abs( dist1[i] ) < myTol )
926 if ( nbOnPlane1 == 0 )
927 for ( size_t i = 0; i < nodes1.size(); ++i )
928 if ( dist1[iNotOnPlane1] * dist1[i] < 0 )
934 //================================================================================
936 * \brief Compute parameters on the plane intersection line of intersections
937 * of edges of a triangle
938 * \param [in] nodes - triangle nodes
939 * \param [in] dist - distance of triangle nodes from the plane of another triangle
940 * \param [in] nbOnPln - number of nodes lying on the plane of another triangle
941 * \param [in] iMaxCoo - index of coordinate of max component of the plane intersection line
942 * \param [out] u - two computed parameters on the plane intersection line
943 * \param [out] iE - indices of intersected edges
945 //================================================================================
947 void Intersector::Algo::computeIntervals( const std::vector< SMESH_NodeXYZ >& nodes,
948 const std::vector< double >& dist,
956 u[0] = u[1] = 1e+100;
961 if ( nbOnPln == 1 && ( dist[i1] == 0. || dist[i2] == 0 ))
963 int i = dist[i1] == 0 ? i1 : i2;
964 u [ 1 ] = nodes[ i ].Coord( iMaxCoo );
968 for ( ; i2 < 3 && nb < 2; i1 = i2++ )
970 double dd = dist[i1] - dist[i2];
971 if ( dd != 0. && dist[i2] * dist[i1] <= 0. )
973 double x1 = nodes[i1].Coord( iMaxCoo );
974 double x2 = nodes[i2].Coord( iMaxCoo );
975 u [ nb ] = x1 + ( x2 - x1 ) * dist[i1] / dd;
982 std::swap( u [0], u [1] );
983 std::swap( iE[0], iE[1] );
987 //================================================================================
989 * \brief Try to find an intersection node on a link collinear with the plane intersection line
991 //================================================================================
993 void Intersector::Algo::findIntPointOnPlane( const std::vector< SMESH_NodeXYZ >& nodes,
994 const std::vector< double > & dist,
997 int i1 = ( dist[0] == 0 ? 0 : 1 ), i2 = ( dist[2] == 0 ? 2 : 1 );
998 CutLink link2 = link;
999 link2.Set( nodes[i1].Node(), nodes[i2].Node(), 0 );
1000 if ( findLink( link2 ))
1001 link.myIntNode = link2.myIntNode;
1004 //================================================================================
1006 * \brief Compute intersection point of a link1 with a face2
1008 //================================================================================
1010 void Intersector::Algo::intersectLink( const std::vector< SMESH_NodeXYZ >& nodes1,
1011 const std::vector< double > & dist1,
1013 const SMDS_MeshElement* face2,
1016 const int iEdge2 = ( iEdge1 + 1 ) % nodes1.size();
1017 const SMESH_NodeXYZ& p1 = nodes1[ iEdge1 ];
1018 const SMESH_NodeXYZ& p2 = nodes1[ iEdge2 ];
1020 link1.Set( p1.Node(), p2.Node(), face2 );
1021 const CutLink* link = & myCutLinks.Added( link1 );
1022 if ( !link->IntNode() )
1024 if ( dist1[ iEdge1 ] == 0. ) link1.myIntNode = p1;
1025 else if ( dist1[ iEdge2 ] == 0. ) link1.myIntNode = p2;
1028 gp_XYZ p = p1 + ( p2 - p1 ) * dist1[ iEdge1 ] / ( dist1[ iEdge1 ] - dist1[ iEdge2 ]);
1029 (gp_XYZ&)link1.myIntNode = p;
1034 gp_XYZ p = p1 + ( p2 - p1 ) * dist1[ iEdge1 ] / ( dist1[ iEdge1 ] - dist1[ iEdge2 ]);
1035 while ( link->IntNode() )
1037 if ( coincide( p, link->myIntNode, myTol ))
1039 link1.myIntNode = link->myIntNode;
1043 link = & myCutLinks.Added( link1 );
1045 if ( !link1.IntNode() )
1047 if ( dist1[ iEdge1 ] == 0. ) link1.myIntNode = p1;
1048 else if ( dist1[ iEdge2 ] == 0. ) link1.myIntNode = p2;
1049 else (gp_XYZ&)link1.myIntNode = p;
1054 //================================================================================
1056 * \brief Store node replacement in myCutFaces
1058 //================================================================================
1060 void Intersector::Algo::replaceIntNode( const SMDS_MeshNode* nToKeep,
1061 const SMDS_MeshNode* nToRemove )
1063 if ( nToKeep == nToRemove )
1065 if ( nToRemove->GetID() < nToKeep->GetID() ) // keep node with lower ID
1066 myRemove2KeepNodes.Bind( nToKeep, nToRemove );
1068 myRemove2KeepNodes.Bind( nToRemove, nToKeep );
1071 //================================================================================
1073 * \brief Compute intersection point on a link of either of faces by choosing
1074 * a link whose parameter on the intersection line in maximal
1075 * \param [in] u1 - parameter on the intersection line of link iE1 of myFace1
1076 * \param [in] u2 - parameter on the intersection line of link iE2 of myFace2
1077 * \param [in] iE1 - index of a link myFace1
1078 * \param [in] iE2 - index of a link myFace2
1079 * \param [out] link - CutLink storing the intersection point
1080 * \param [out] node1 - a node of the 2nd link if two links intersect
1081 * \param [out] node2 - a node of the 2nd link if two links intersect
1083 //================================================================================
1085 void Intersector::Algo::computeIntPoint( const double u1,
1090 const SMDS_MeshNode* & node1,
1091 const SMDS_MeshNode* & node2)
1093 if ( u1 > u2 + myTol )
1095 intersectLink( myNodes1, myDist1, iE1, myFace2, link );
1097 if ( myNbOnPlane2 == 2 )
1098 findIntPointOnPlane( myNodes2, myDist2, link );
1100 else if ( u2 > u1 + myTol )
1102 intersectLink( myNodes2, myDist2, iE2, myFace1, link );
1104 if ( myNbOnPlane1 == 2 )
1105 findIntPointOnPlane( myNodes1, myDist1, link );
1107 else // edges of two faces intersect the line at the same point
1110 intersectLink( myNodes1, myDist1, iE1, myFace2, link );
1111 intersectLink( myNodes2, myDist2, iE2, myFace1, link2 );
1112 node1 = link2.Node1();
1113 node2 = link2.Node2();
1115 if ( !link.IntNode() && link2.IntNode() )
1116 link.myIntNode = link2.myIntNode;
1118 else if ( !link.IntNode() && !link2.IntNode() )
1119 (gp_XYZ&)link.myIntNode = 0.5 * ( link.myIntNode + link2.myIntNode );
1121 else if ( link.IntNode() && link2.IntNode() )
1122 replaceIntNode( link.IntNode(), link2.IntNode() );
1126 //================================================================================
1128 * \brief Add intersections to a link collinear with the intersection line
1130 //================================================================================
1132 void Intersector::Algo::cutCollinearLink( const int iNotOnPlane1,
1133 const std::vector< SMESH_NodeXYZ >& nodes1,
1134 const SMDS_MeshElement* face2,
1135 const CutLink& link1,
1136 const CutLink& link2)
1139 int iN1 = ( iNotOnPlane1 + 1 ) % 3;
1140 int iN2 = ( iNotOnPlane1 + 2 ) % 3;
1141 CutLink link( nodes1[ iN1 ].Node(), nodes1[ iN2 ].Node(), face2 );
1142 if ( link1.myFace != face2 )
1144 link.myIntNode = link1.myIntNode;
1147 if ( link2.myFace != face2 )
1149 link.myIntNode = link2.myIntNode;
1154 //================================================================================
1156 * \brief Choose indices on an axis-aligned plane
1158 //================================================================================
1160 void Intersector::Algo::setPlaneIndices( const gp_XYZ& planeNorm )
1162 switch ( MaxIndex( planeNorm )) {
1163 case 1: myInd1 = 2; myInd2 = 3; break;
1164 case 2: myInd1 = 3; myInd2 = 1; break;
1165 case 3: myInd1 = 1; myInd2 = 2; break;
1169 //================================================================================
1171 * \brief Intersect two faces
1173 //================================================================================
1175 void Intersector::Algo::Cut( const SMDS_MeshElement* face1,
1176 const SMDS_MeshElement* face2,
1177 const int nbCommonNodes)
1181 myNodes1.assign( face1->begin_nodes(), face1->end_nodes() );
1182 myNodes2.assign( face2->begin_nodes(), face2->end_nodes() );
1184 const gp_XYZ& n1 = myNormals[ face1->GetID() ];
1185 const gp_XYZ& n2 = myNormals[ face2->GetID() ];
1187 // check if triangles intersect
1188 int iNotOnPlane1, iNotOnPlane2;
1189 const double d2 = -( n2 * myNodes2[0]);
1190 if ( !isPlaneIntersected( n2, d2, myNodes1, myDist1, myNbOnPlane1, iNotOnPlane1 ))
1192 const double d1 = -( n1 * myNodes1[0]);
1193 if ( !isPlaneIntersected( n1, d1, myNodes2, myDist2, myNbOnPlane2, iNotOnPlane2 ))
1196 if ( myNbOnPlane1 == 3 || myNbOnPlane2 == 3 )// triangles are co-planar
1198 setPlaneIndices( myNbOnPlane1 == 3 ? n2 : n1 ); // choose indices on an axis-aligned plane
1201 else if ( nbCommonNodes < 2 ) // triangle planes intersect
1203 gp_XYZ lineDir = n1 ^ n2; // intersection line
1205 // check if intervals of intersections of triangles with lineDir overlap
1207 double u1[2], u2 [2]; // parameters on lineDir of edge intersection points { minU, maxU }
1208 int iE1[2], iE2[2]; // indices of edges
1209 int iMaxCoo = MaxIndex( lineDir );
1210 computeIntervals( myNodes1, myDist1, myNbOnPlane1, iMaxCoo, u1, iE1 );
1211 computeIntervals( myNodes2, myDist2, myNbOnPlane2, iMaxCoo, u2, iE2 );
1212 if ( u1[1] < u2[0] - myTol || u2[1] < u1[0] - myTol )
1213 return; // intervals do not overlap
1215 // make intersection nodes
1217 const SMDS_MeshNode *l1n1, *l1n2, *l2n1, *l2n2;
1218 CutLink link1; // intersection with smaller u on lineDir
1219 computeIntPoint( u1[0], u2[0], iE1[0], iE2[0], link1, l1n1, l1n2 );
1220 CutLink link2; // intersection with larger u on lineDir
1221 computeIntPoint( -u1[1], -u2[1], iE1[1], iE2[1], link2, l2n1, l2n2 );
1223 const CutFace& cf1 = myCutFaces.Added( CutFace( face1 ));
1224 const CutFace& cf2 = myCutFaces.Added( CutFace( face2 ));
1226 if ( coincide( link1.myIntNode, link2.myIntNode, myTol ))
1228 // intersection is a point
1229 if ( link1.IntNode() && link2.IntNode() )
1230 replaceIntNode( link1.IntNode(), link2.IntNode() );
1232 CutLink* link = link2.IntNode() ? &link2 : &link1;
1233 if ( !link->IntNode() )
1235 gp_XYZ p = 0.5 * ( link1.myIntNode + link2.myIntNode );
1236 link->myIntNode.Set( createNode( p ));
1238 if ( !link1.IntNode() ) link1.myIntNode = link2.myIntNode;
1239 if ( !link2.IntNode() ) link2.myIntNode = link1.myIntNode;
1241 cf1.AddPoint( link1, link2, myTol );
1242 cf2.AddPoint( link1, link2, myTol );
1246 // intersection is a line segment
1247 if ( !link1.IntNode() )
1248 link1.myIntNode.Set( createNode( link1.myIntNode ));
1249 if ( !link2.IntNode() )
1250 link2.myIntNode.Set( createNode( link2.myIntNode ));
1252 cf1.AddEdge( link1, link2, face2, myNbOnPlane1, iNotOnPlane1 );
1253 if ( l1n1 ) link1.Set( l1n1, l1n2, face2 );
1254 if ( l2n1 ) link2.Set( l2n1, l2n2, face2 );
1255 cf2.AddEdge( link1, link2, face1, myNbOnPlane2, iNotOnPlane2 );
1257 // add intersections to a link collinear with the intersection line
1258 if ( myNbOnPlane1 == 2 && ( link1.myFace != face2 || link2.myFace != face2 ))
1259 cutCollinearLink( iNotOnPlane1, myNodes1, face2, link1, link2 );
1261 if ( myNbOnPlane2 == 2 && ( link1.myFace != face1 || link2.myFace != face1 ))
1262 cutCollinearLink( iNotOnPlane2, myNodes2, face1, link1, link2 );
1268 } // non co-planar case
1273 //================================================================================
1275 * \brief Store a face cut by a line given by its ends
1276 * accompanied by indices of intersected face edges.
1277 * Edge index is <0 if a line end is inside the face.
1278 * \param [in] face - a face to cut
1279 * \param [inout] lineEnd1 - line end coordinates + optional node existing at this point
1280 * \param [in] edgeIndex1 - index of face edge cut by lineEnd1
1281 * \param [inout] lineEnd2 - line end coordinates + optional node existing at this point
1282 * \param [in] edgeIndex2 - index of face edge cut by lineEnd2
1284 //================================================================================
1286 void Intersector::Algo::Cut( const SMDS_MeshElement* face,
1287 SMESH_NodeXYZ& lineEnd1,
1289 SMESH_NodeXYZ& lineEnd2,
1292 if ( lineEnd1.Node() && lineEnd2.Node() &&
1293 face->GetNodeIndex( lineEnd1.Node() ) >= 0 &&
1294 face->GetNodeIndex( lineEnd2.Node() ) >= 0 )
1295 return; // intersection at a face node or edge
1297 if ((int) myNormals.size() <= face->GetID() )
1298 const_cast< std::vector< gp_XYZ >& >( myNormals ).resize( face->GetID() + 1 );
1300 const CutFace& cf = myCutFaces.Added( CutFace( face ));
1303 // look for intersection nodes coincident with line ends
1305 for ( int is2nd = 0; is2nd < 2; ++is2nd )
1307 SMESH_NodeXYZ& lineEnd = is2nd ? lineEnd2 : lineEnd1;
1308 int edgeIndex = is2nd ? edgeIndex2 : edgeIndex1;
1309 CutLink & link = links[ is2nd ];
1311 link.myIntNode = lineEnd;
1313 for ( size_t i = ( edgeIndex < 0 ? 3 : 0 ); i < cf.myLinks.size(); ++i )
1314 if ( coincide( lineEnd, SMESH_NodeXYZ( cf.myLinks[i].myNode1 ), myTol ))
1316 link.myIntNode = cf.myLinks[i].myNode1;
1320 if ( edgeIndex >= 0 )
1322 link.Set( face->GetNode ( edgeIndex ),
1323 face->GetNodeWrap( edgeIndex + 1 ),
1328 if ( !link.myIntNode )
1329 link.myIntNode.Set( createNode( lineEnd ));
1331 lineEnd._node = link.IntNode();
1333 if ( link.myNode[0] )
1337 cf.AddEdge( links[0], links[1], /*face=*/0, /*nbOnPlane=*/0, /*iNotOnPlane=*/-1 );
1340 //================================================================================
1342 * \brief Intersect two 2D line segments
1344 //================================================================================
1346 bool Intersector::Algo::intersectEdgeEdge( const gp_XY s1p0, const gp_XY s1p1,
1347 const gp_XY s2p0, const gp_XY s2p1,
1348 double & t1, double & t2,
1349 bool & isCollinear )
1351 gp_XY u = s1p1 - s1p0;
1352 gp_XY v = s2p1 - s2p0;
1353 gp_XY w = s1p0 - s2p0;
1354 double perpDotUV = u * gp_XY( -v.Y(), v.X() );
1355 double perpDotVW = v * gp_XY( -w.Y(), w.X() );
1356 double perpDotUW = u * gp_XY( -w.Y(), w.X() );
1357 double u2 = u.SquareModulus();
1358 double v2 = v.SquareModulus();
1359 if ( u2 < myEps * myEps || v2 < myEps * myEps )
1361 if ( perpDotUV * perpDotUV / u2 / v2 < 1e-6 ) // cos ^ 2
1364 return false; // no need in collinear solution
1365 if ( perpDotUW * perpDotUW / u2 > myTol * myTol )
1366 return false; // parallel
1369 gp_XY w2 = s1p1 - s2p0;
1370 if ( Abs( v.X()) + Abs( u.X()) > Abs( v.Y()) + Abs( u.Y())) {
1371 t1 = w.X() / v.X(); // params on segment 2
1372 t2 = w2.X() / v.X();
1376 t2 = w2.Y() / v.Y();
1378 if ( Max( t1,t2 ) <= 0 || Min( t1,t2 ) >= 1 )
1379 return false; // no overlap
1382 isCollinear = false;
1384 t1 = perpDotVW / perpDotUV; // param on segment 1
1385 if ( t1 < 0. || t1 > 1. )
1386 return false; // intersection not within the segment
1388 t2 = perpDotUW / perpDotUV; // param on segment 2
1389 if ( t2 < 0. || t2 > 1. )
1390 return false; // intersection not within the segment
1395 //================================================================================
1397 * \brief Intersect two edges of co-planar triangles
1398 * \param [inout] iE1 - edge index of triangle 1
1399 * \param [inout] iE2 - edge index of triangle 2
1400 * \param [inout] intPoints - intersection points
1401 * \param [inout] nbIntPoints - nb of found intersection points
1403 //================================================================================
1405 bool Intersector::Algo::intersectEdgeEdge( int iE1, int iE2, IntPoint2D& intPoint )
1407 int i01 = iE1, i11 = ( iE1 + 1 ) % 3;
1408 int i02 = iE2, i12 = ( iE2 + 1 ) % 3;
1409 if (( !intPoint.myIsCollinear ) &&
1410 ( myNodes1[ i01 ] == myNodes2[ i02 ] ||
1411 myNodes1[ i01 ] == myNodes2[ i12 ] ||
1412 myNodes1[ i11 ] == myNodes2[ i02 ] ||
1413 myNodes1[ i11 ] == myNodes2[ i12 ] ))
1417 gp_XY s1p0 = p2D( myNodes1[ i01 ]);
1418 gp_XY s1p1 = p2D( myNodes1[ i11 ]);
1421 gp_XY s2p0 = p2D( myNodes2[ i02 ]);
1422 gp_XY s2p1 = p2D( myNodes2[ i12 ]);
1425 if ( !intersectEdgeEdge( s1p0,s1p1, s2p0,s2p1, t1, t2, intPoint.myIsCollinear ))
1428 intPoint.myEdgeInd[0] = iE1;
1429 intPoint.myEdgeInd[1] = iE2;
1430 intPoint.myU[0] = t1;
1431 intPoint.myU[1] = t2;
1432 (gp_XYZ&)intPoint.myNode = myNodes1[i01] * ( 1 - t1 ) + myNodes1[i11] * t1;
1434 if ( intPoint.myIsCollinear )
1437 // try to find existing node at intPoint.myNode
1439 if ( myNodes1[ i01 ] == myNodes2[ i02 ] ||
1440 myNodes1[ i01 ] == myNodes2[ i12 ] ||
1441 myNodes1[ i11 ] == myNodes2[ i02 ] ||
1442 myNodes1[ i11 ] == myNodes2[ i12 ] )
1445 const double coincTol = myTol * 1e-3;
1447 CutLink link1( myNodes1[i01].Node(), myNodes1[i11].Node(), myFace2 );
1448 CutLink link2( myNodes2[i02].Node(), myNodes2[i12].Node(), myFace1 );
1450 SMESH_NodeXYZ& n1 = myNodes1[ t1 < 0.5 ? i01 : i11 ];
1451 bool same1 = coincide( n1, intPoint.myNode, coincTol );
1454 link2.myIntNode = intPoint.myNode = n1;
1457 SMESH_NodeXYZ& n2 = myNodes2[ t2 < 0.5 ? i02 : i12 ];
1458 bool same2 = coincide( n2, intPoint.myNode, coincTol );
1461 link1.myIntNode = intPoint.myNode = n2;
1465 replaceIntNode( n1.Node(), n2.Node() );
1473 link1.myIntNode = intPoint.myNode;
1474 if ( findLink( link1 ))
1476 intPoint.myNode = link2.myIntNode = link1.myIntNode;
1481 link2.myIntNode = intPoint.myNode;
1482 if ( findLink( link2 ))
1484 intPoint.myNode = link1.myIntNode = link2.myIntNode;
1489 for ( int is2nd = 0; is2nd < 2; ++is2nd )
1491 const SMDS_MeshElement* f = is2nd ? myFace1 : myFace2;
1493 const CutFace& cf = myCutFaces.Added( CutFace( is2nd ? myFace2 : myFace1 ));
1494 for ( size_t i = 0; i < cf.myLinks.size(); ++i )
1495 if ( cf.myLinks[i].myFace == f &&
1496 //cf.myLinks[i].myIndex != EdgePart::_COPLANAR &&
1497 coincide( intPoint.myNode, SMESH_NodeXYZ( cf.myLinks[i].myNode1 ), coincTol ))
1499 intPoint.myNode.Set( cf.myLinks[i].myNode1 );
1506 intPoint.myNode._node = createNode( intPoint.myNode );
1507 link1.myIntNode = link2.myIntNode = intPoint.myNode;
1515 //================================================================================
1517 * \brief Check if a point is contained in a triangle
1519 //================================================================================
1521 bool Intersector::Algo::isPointInTriangle( const gp_XYZ& p, const std::vector< SMESH_NodeXYZ >& nodes )
1524 SMESH_MeshAlgos::GetBarycentricCoords( p2D( p ),
1525 p2D( nodes[0] ), p2D( nodes[1] ), p2D( nodes[2] ),
1527 return ( 0. < bc1 && 0. < bc2 && bc1 + bc2 < 1. );
1530 //================================================================================
1532 * \brief Intersect two co-planar faces
1534 //================================================================================
1536 void Intersector::Algo::cutCoplanar()
1538 // find intersections of edges
1540 IntPoint2D intPoints[ 6 ];
1541 int nbIntPoints = 0;
1542 for ( int iE1 = 0; iE1 < 3; ++iE1 )
1544 int maxNbIntPoints = nbIntPoints + 2;
1545 for ( int iE2 = 0; iE2 < 3 && nbIntPoints < maxNbIntPoints; ++iE2 )
1546 nbIntPoints += intersectEdgeEdge( iE1, iE2, intPoints[ nbIntPoints ]);
1548 const int minNbOnPlane = Min( myNbOnPlane1, myNbOnPlane2 );
1550 if ( nbIntPoints == 0 ) // no intersections of edges
1553 if ( isPointInTriangle( myNodes1[0], myNodes2 )) // face2 includes face1
1555 else if ( isPointInTriangle( myNodes2[0], myNodes1 )) // face1 includes face2
1560 // add edges of an inner triangle to an outer one
1562 const std::vector< SMESH_NodeXYZ >& nodesIn = is1in2 ? myNodes1 : myNodes2;
1563 const SMDS_MeshElement* faceOut = is1in2 ? myFace2 : myFace1;
1564 const SMDS_MeshElement* faceIn = is1in2 ? myFace1 : myFace2;
1566 const CutFace& outFace = myCutFaces.Added( CutFace( faceOut ));
1567 CutLink link1( nodesIn.back().Node(), nodesIn.back().Node(), faceOut );
1568 CutLink link2( nodesIn.back().Node(), nodesIn.back().Node(), faceOut );
1570 link1.myIntNode = nodesIn.back();
1571 for ( size_t i = 0; i < nodesIn.size(); ++i )
1573 link2.myIntNode = nodesIn[ i ];
1574 outFace.AddEdge( link1, link2, faceIn, minNbOnPlane );
1575 link1.myIntNode = link2.myIntNode;
1580 // add parts of edges to a triangle including them
1582 CutLink link1, link2;
1583 IntPoint2D ip0, ip1;
1584 ip0.myU[0] = ip0.myU[1] = 0.;
1585 ip1.myU[0] = ip1.myU[1] = 1.;
1586 ip0.myEdgeInd[0] = ip0.myEdgeInd[1] = ip1.myEdgeInd[0] = ip1.myEdgeInd[1] = 0;
1588 for ( int isFromFace1 = 0; isFromFace1 < 2; ++isFromFace1 )
1590 const SMDS_MeshElement* faceTo = isFromFace1 ? myFace2 : myFace1;
1591 const SMDS_MeshElement* faceFrom = isFromFace1 ? myFace1 : myFace2;
1592 const std::vector< SMESH_NodeXYZ >& nodesTo = isFromFace1 ? myNodes2 : myNodes1;
1593 const std::vector< SMESH_NodeXYZ >& nodesFrom = isFromFace1 ? myNodes1 : myNodes2;
1594 const int iTo = isFromFace1 ? 1 : 0;
1595 const int iFrom = isFromFace1 ? 0 : 1;
1596 //const int nbOnPlaneFrom = isFromFace1 ? myNbOnPlane1 : myNbOnPlane2;
1598 const CutFace* cutFaceTo = & myCutFaces.Added( CutFace( faceTo ));
1599 // const CutFace* cutFaceFrom = 0;
1600 // if ( nbOnPlaneFrom > minNbOnPlane )
1601 // cutFaceFrom = & myCutFaces.Added( CutFace( faceTo ));
1603 link1.myFace = link2.myFace = faceTo;
1605 IntPoint2DCompare ipCompare( iFrom );
1606 TIntPointPtrSet pointsOnEdge( ipCompare ); // IntPoint2D sorted by parameter on edge
1608 for ( size_t iE = 0; iE < nodesFrom.size(); ++iE )
1610 // get parts of an edge iE
1612 ip0.myEdgeInd[ iTo ] = iE;
1613 ip1.myEdgeInd[ iTo ] = ( iE + 1 ) % nodesFrom.size();
1614 ip0.myNode = nodesFrom[ ip0.myEdgeInd[ iTo ]];
1615 ip1.myNode = nodesFrom[ ip1.myEdgeInd[ iTo ]];
1617 pointsOnEdge.clear();
1619 for ( int iP = 0; iP < nbIntPoints; ++iP )
1620 if ( intPoints[ iP ].myEdgeInd[ iFrom ] == iE )
1621 pointsOnEdge.insert( & intPoints[ iP ] );
1623 pointsOnEdge.insert( pointsOnEdge.begin(), & ip0 );
1624 pointsOnEdge.insert( pointsOnEdge.end(), & ip1 );
1626 // add edge parts to faceTo
1628 TIntPointPtrSet::iterator ipIt = pointsOnEdge.begin() + 1;
1629 for ( ; ipIt != pointsOnEdge.end(); ++ipIt )
1631 const IntPoint2D* p1 = *(ipIt-1);
1632 const IntPoint2D* p2 = *ipIt;
1633 gp_XYZ middle = 0.5 * ( p1->myNode + p2->myNode );
1634 if ( isPointInTriangle( middle, nodesTo ))
1636 p1->InitLink( link1, iTo, ( p1 != & ip0 ) ? nodesTo : nodesFrom );
1637 p2->InitLink( link2, iTo, ( p2 != & ip1 ) ? nodesTo : nodesFrom );
1638 cutFaceTo->AddEdge( link1, link2, faceFrom, minNbOnPlane );
1640 // if ( cutFaceFrom )
1642 // p1->InitLink( link1, iFrom, nodesFrom );
1643 // p2->InitLink( link2, iFrom, nodesFrom );
1644 // cutFaceTo->AddEdge( link1, link2, faceTo, minNbOnPlane );
1653 } // Intersector::Algo::cutCoplanar()
1655 //================================================================================
1657 * \brief Intersect edges added to myCutFaces
1659 //================================================================================
1661 void Intersector::Algo::IntersectNewEdges( const CutFace& cf )
1663 IntPoint2D intPoint;
1665 if ( cf.NbInternalEdges() < 2 )
1668 if ( myNodes1.empty() )
1674 const gp_XYZ& faceNorm = myNormals[ cf.myInitFace->GetID() ];
1675 setPlaneIndices( faceNorm ); // choose indices on an axis-aligned plane
1677 size_t limit = cf.myLinks.size() * cf.myLinks.size() * 2;
1679 for ( size_t i1 = 3; i1 < cf.myLinks.size(); ++i1 )
1681 if ( !cf.myLinks[i1].IsInternal() )
1684 myIntPointSet.clear();
1685 for ( size_t i2 = i1 + 2; i2 < cf.myLinks.size(); ++i2 )
1687 if ( !cf.myLinks[i2].IsInternal() )
1690 // prepare to intersection
1691 myFace1 = cf.myLinks[i1].myFace;
1692 myNodes1[0] = cf.myLinks[i1].myNode1;
1693 myNodes1[1] = cf.myLinks[i1].myNode2;
1694 myFace2 = cf.myLinks[i2].myFace;
1695 myNodes2[0] = cf.myLinks[i2].myNode1;
1696 myNodes2[1] = cf.myLinks[i2].myNode2;
1699 intPoint.myIsCollinear = true; // to find collinear solutions
1700 if ( intersectEdgeEdge( 0, 0, intPoint ))
1702 if ( cf.myLinks[i1].IsSame( cf.myLinks[i2] )) // remove i2
1704 cf.myLinks[i1].ReplaceCoplanar( cf.myLinks[i2] );
1705 cf.myLinks.erase( cf.myLinks.begin() + i2, cf.myLinks.begin() + i2 + 2 );
1709 if ( !intPoint.myIsCollinear )
1711 intPoint.myEdgeInd[1] = i2;
1712 myIntPointSet.insert( intPoint );
1714 else // if ( intPoint.myIsCollinear ) // overlapping edges
1716 myIntPointSet.clear(); // to recompute
1718 if ( intPoint.myU[0] > intPoint.myU[1] ) // orient in same direction
1720 std::swap( intPoint.myU[0], intPoint.myU[1] );
1721 std::swap( myNodes1[0], myNodes1[1] );
1723 // replace _COPLANAR by _INTERNAL
1724 cf.myLinks[i1].ReplaceCoplanar( cf.myLinks[i1+1] );
1725 cf.myLinks[i2].ReplaceCoplanar( cf.myLinks[i2+1] );
1727 if ( coincide( myNodes1[0], myNodes2[0], myTol ) &&
1728 coincide( myNodes1[1], myNodes2[1], myTol ))
1730 cf.myLinks.erase( cf.myLinks.begin() + i2, cf.myLinks.begin() + i2 + 2 );
1735 EdgePart common = cf.myLinks[i1];
1736 common.ReplaceCoplanar( cf.myLinks[i2] );
1738 const SMDS_MeshNode* n1 = myNodes1[0].Node(); // end nodes of an overlapping part
1739 const SMDS_MeshNode* n2 = myNodes1[1].Node();
1740 size_t i3 = cf.myLinks.size();
1742 if ( myNodes1[0] != myNodes2[0] ) // a part before the overlapping one
1744 if ( intPoint.myU[0] < 0 )
1745 cf.myLinks[i1].Set( myNodes1[0].Node(), myNodes2[0].Node(),
1746 cf.myLinks[i1].myFace, cf.myLinks[i1].myIndex );
1748 cf.myLinks[i1].Set( myNodes2[0].Node(), myNodes1[0].Node(),
1749 cf.myLinks[i2].myFace, cf.myLinks[i2].myIndex );
1751 cf.myLinks[i1+1].Set( cf.myLinks[i1].myNode2,
1752 cf.myLinks[i1].myNode1,
1753 cf.myLinks[i1].myFace,
1754 cf.myLinks[i1].myIndex);
1755 n1 = cf.myLinks[i1].myNode2;
1760 if ( myNodes1[1] != myNodes2[1] ) // a part after the overlapping one
1762 if ( intPoint.myU[1] < 1 )
1763 cf.myLinks[i2].Set( myNodes1[1].Node(), myNodes2[1].Node(),
1764 cf.myLinks[i2].myFace, cf.myLinks[i2].myIndex );
1766 cf.myLinks[i2].Set( myNodes2[1].Node(), myNodes1[1].Node(),
1767 cf.myLinks[i1].myFace, cf.myLinks[i1].myIndex );
1769 cf.myLinks[i2+1].Set( cf.myLinks[i2].myNode2,
1770 cf.myLinks[i2].myNode1,
1771 cf.myLinks[i2].myFace,
1772 cf.myLinks[i2].myIndex);
1773 n2 = cf.myLinks[i2].myNode1;
1778 if ( i3 == cf.myLinks.size() )
1779 cf.myLinks.resize( i3 + 2 );
1781 cf.myLinks[i3].Set ( n1, n2, common.myFace, common.myIndex );
1782 cf.myLinks[i3+1].Set( n2, n1, common.myFace, common.myIndex );
1784 i2 = i1 + 1; // recheck modified i1
1789 // // remember a new node
1790 // CutLink link1( myNodes1[0].Node(), myNodes1[1].Node(), cf.myInitFace );
1791 // CutLink link2( myNodes2[0].Node(), myNodes2[1].Node(), cf.myInitFace );
1792 // link2.myIntNode = link1.myIntNode = intPoint.myNode;
1793 // addLink( link1 );
1794 // addLink( link2 );
1797 // size_t i = cf.myLinks.size();
1798 // if ( intPoint.myNode != cf.myLinks[ i1 ].myNode1 &&
1799 // intPoint.myNode != cf.myLinks[ i1 ].myNode2 )
1801 // cf.myLinks.push_back( cf.myLinks[ i1 ]);
1802 // cf.myLinks.push_back( cf.myLinks[ i1 + 1 ]);
1803 // cf.myLinks[ i1 ].myNode2 = cf.myLinks[ i1 + 1 ].myNode1 = intPoint.Node();
1804 // cf.myLinks[ i ].myNode1 = cf.myLinks[ i + 1 ].myNode2 = intPoint.Node();
1806 // if ( intPoint.myNode != cf.myLinks[ i2 ].myNode1 &&
1807 // intPoint.myNode != cf.myLinks[ i2 ].myNode2 )
1809 // i = cf.myLinks.size();
1810 // cf.myLinks.push_back( cf.myLinks[ i2 ]);
1811 // cf.myLinks.push_back( cf.myLinks[ i2 + 1 ]);
1812 // cf.myLinks[ i2 ].myNode2 = cf.myLinks[ i2 + 1 ].myNode1 = intPoint.Node();
1813 // cf.myLinks[ i ].myNode1 = cf.myLinks[ i + 1 ].myNode2 = intPoint.Node();
1817 } // if ( intersectEdgeEdge( 0, 0, intPoint ))
1823 // split i1 edge and all edges it intersects
1824 // don't do it inside intersection loop in order not to loose direction of i1 edge
1825 if ( !myIntPointSet.empty() )
1827 cf.myLinks.reserve( cf.myLinks.size() + myIntPointSet.size() * 2 + 2 );
1829 EdgePart* edge1 = &cf.myLinks[ i1 ];
1830 EdgePart* twin1 = &cf.myLinks[ i1 + 1 ];
1832 TIntPointSet::iterator ipIt = myIntPointSet.begin();
1833 for ( ; ipIt != myIntPointSet.end(); ++ipIt ) // int points sorted on i1 edge
1835 size_t i = cf.myLinks.size();
1836 if ( ipIt->myNode != edge1->myNode1 &&
1837 ipIt->myNode != edge1->myNode2 )
1839 cf.myLinks.push_back( *edge1 );
1840 cf.myLinks.push_back( *twin1 );
1841 edge1->myNode2 = twin1->myNode1 = ipIt->Node();
1842 cf.myLinks[ i ].myNode1 = cf.myLinks[ i + 1 ].myNode2 = ipIt->Node();
1843 edge1 = & cf.myLinks[ i ];
1844 twin1 = & cf.myLinks[ i + 1 ];
1846 size_t i2 = ipIt->myEdgeInd[1];
1847 if ( ipIt->myNode != cf.myLinks[ i2 ].myNode1 &&
1848 ipIt->myNode != cf.myLinks[ i2 ].myNode2 )
1850 i = cf.myLinks.size();
1851 cf.myLinks.push_back( cf.myLinks[ i2 ]);
1852 cf.myLinks.push_back( cf.myLinks[ i2 + 1 ]);
1853 cf.myLinks[ i2 ].myNode2 = cf.myLinks[ i2 + 1 ].myNode1 = ipIt->Node();
1854 cf.myLinks[ i ].myNode1 = cf.myLinks[ i + 1 ].myNode2 = ipIt->Node();
1857 if ( cf.myLinks.size() >= limit )
1858 throw SALOME_Exception( "Infinite loop in Intersector::Algo::IntersectNewEdges()" );
1860 ++i1; // each internal edge encounters twice
1865 //================================================================================
1867 * \brief Split intersected faces
1869 //================================================================================
1871 void Intersector::Algo::MakeNewFaces( SMESH_MeshAlgos::TElemIntPairVec& theNew2OldFaces,
1872 SMESH_MeshAlgos::TNodeIntPairVec& theNew2OldNodes,
1873 const double theSign,
1874 const bool theOptimize)
1876 // fill theNew2OldFaces if empty
1877 TCutFaceMap::const_iterator cutFacesIt = myCutFaces.cbegin();
1878 if ( theNew2OldFaces.empty() )
1879 for ( ; cutFacesIt != myCutFaces.cend(); ++cutFacesIt )
1881 const CutFace& cf = *cutFacesIt;
1882 int index = cf.myInitFace->GetID(); // index in theNew2OldFaces
1883 if ((int) theNew2OldFaces.size() <= index )
1884 theNew2OldFaces.resize( index + 1 );
1885 theNew2OldFaces[ index ] = std::make_pair( cf.myInitFace, index );
1888 // unmark all nodes except intersection ones
1890 for ( SMDS_NodeIteratorPtr nIt = myMesh->nodesIterator(); nIt->more(); )
1892 const SMDS_MeshNode* n = nIt->next();
1893 if ( n->isMarked() && n->GetID()-1 < (int) theNew2OldNodes.size() )
1894 n->setIsMarked( false );
1896 // SMESH_MeshAlgos::MarkElems( myMesh->nodesIterator(), false );
1898 TCutLinkMap::const_iterator cutLinksIt = myCutLinks.cbegin();
1899 // for ( ; cutLinksIt != myCutLinks.cend(); ++cutLinksIt )
1901 // const CutLink& link = *cutLinksIt;
1902 // if ( link.IntNode() && link.IntNode()->GetID()-1 < (int) theNew2OldNodes.size() )
1903 // link.IntNode()->setIsMarked( true );
1906 // intersect edges added to myCutFaces
1908 for ( cutFacesIt = myCutFaces.cbegin(); cutFacesIt != myCutFaces.cend(); ++cutFacesIt )
1910 const CutFace& cf = *cutFacesIt;
1911 cf.ReplaceNodes( myRemove2KeepNodes );
1912 IntersectNewEdges( cf );
1917 EdgeLoopSet loopSet;
1918 SMESH_MeshAlgos::Triangulate triangulator( theOptimize );
1919 std::vector< EdgePart > cutOffLinks;
1920 TLinkMap cutOffCoplanarLinks;
1921 std::vector< const CutFace* > touchedFaces;
1922 SMESH_MeshAlgos::TElemIntPairVec::value_type new2OldTria;
1924 std::vector< const SMDS_MeshNode* > nodes;
1925 std::vector<const SMDS_MeshElement *> faces;
1927 cutOffLinks.reserve( myCutFaces.Extent() * 2 );
1929 for ( cutFacesIt = myCutFaces.cbegin(); cutFacesIt != myCutFaces.cend(); ++cutFacesIt )
1931 const CutFace& cf = *cutFacesIt;
1934 touchedFaces.push_back( & cf );
1938 const gp_XYZ& normal = myNormals[ cf.myInitFace->GetID() ];
1940 // form loops of new faces
1941 cf.ReplaceNodes( myRemove2KeepNodes );
1942 cf.MakeLoops( loopSet, normal );
1944 // avoid loops that are not connected to boundary edges of cf.myInitFace
1945 if ( cf.RemoveInternalLoops( loopSet ))
1947 IntersectNewEdges( cf );
1948 cf.MakeLoops( loopSet, normal );
1950 // erase loops that are cut off by face intersections
1951 cf.CutOffLoops( loopSet, theSign, myNormals, cutOffLinks, cutOffCoplanarLinks );
1953 int index = cf.myInitFace->GetID(); // index in theNew2OldFaces
1955 const SMDS_MeshElement* tria;
1956 for ( size_t iL = 0; iL < loopSet.myNbLoops; ++iL )
1958 EdgeLoop& loop = loopSet.myLoops[ iL ];
1959 if ( loop.myLinks.size() == 0 )
1962 int nbTria = triangulator.GetTriangles( &loop, nodes );
1963 int nbNodes = 3 * nbTria;
1964 for ( int i = 0; i < nbNodes; i += 3 )
1966 if ( nodes[i] == nodes[i+1] || nodes[i] == nodes[i+2] || nodes[i+1] == nodes[i+2] )
1969 std::cerr << "BAD tria" << std::endl;
1974 if (!( tria = myMesh->FindFace( nodes[i], nodes[i+1], nodes[i+2] )))
1975 tria = myMesh->AddFace( nodes[i], nodes[i+1], nodes[i+2] );
1976 tria->setIsMarked( true ); // not to remove it
1978 new2OldTria = std::make_pair( tria, theNew2OldFaces[ index ].second );
1979 if ( tria->GetID() < (int)theNew2OldFaces.size() )
1980 theNew2OldFaces[ tria->GetID() ] = new2OldTria;
1982 theNew2OldFaces.push_back( new2OldTria );
1984 if ( index == tria->GetID() )
1985 index = 0; // do not remove tria
1988 theNew2OldFaces[ index ].first = 0;
1991 // remove split faces
1992 for ( size_t id = 1; id < theNew2OldFaces.size(); ++id )
1994 if ( theNew2OldFaces[id].first ||
1995 theNew2OldFaces[id].second == 0 )
1997 if ( const SMDS_MeshElement* f = myMesh->FindElement( id ))
1998 myMesh->RemoveFreeElement( f );
2001 // remove faces connected to cut off parts of cf.myInitFace
2004 for ( size_t i = 0; i < cutOffLinks.size(); ++i )
2007 nodes[0] = cutOffLinks[i].myNode1;
2008 nodes[1] = cutOffLinks[i].myNode2;
2010 if ( nodes[0] != nodes[1] &&
2011 myMesh->GetElementsByNodes( nodes, faces ))
2013 if ( cutOffLinks[i].myFace &&
2014 cutOffLinks[i].myIndex != EdgePart::_COPLANAR &&
2017 for ( size_t iF = 0; iF < faces.size(); ++iF )
2019 int index = faces[iF]->GetID();
2020 // if ( //faces[iF]->isMarked() || // kept part of cutFace
2021 // !theNew2OldFaces[ index ].first ) // already removed
2023 cutFace.myInitFace = faces[iF];
2024 // if ( myCutFaces.Contains( cutFace )) // keep cutting faces needed in CutOffLoops()
2026 // if ( !myCutFaces.Added( cutFace ).IsCut() )
2027 // theNew2OldFaces[ index ].first = 0;
2030 cutFace.myLinks.clear();
2031 cutFace.InitLinks();
2032 for ( size_t iL = 0; iL < cutFace.myLinks.size(); ++iL )
2033 if ( !cutOffLinks[i].IsSame( cutFace.myLinks[ iL ]))
2034 cutOffLinks.push_back( cutFace.myLinks[ iL ]);
2036 theNew2OldFaces[ index ].first = 0;
2037 myMesh->RemoveFreeElement( faces[iF] );
2042 // replace nodes in touched faces
2044 // treat touched faces
2045 for ( size_t i = 0; i < touchedFaces.size(); ++i )
2047 const CutFace& cf = *touchedFaces[i];
2049 int index = cf.myInitFace->GetID(); // index in theNew2OldFaces
2050 if ( !theNew2OldFaces[ index ].first )
2051 continue; // already cut off
2053 if ( !cf.ReplaceNodes( myRemove2KeepNodes ))
2054 continue; // just keep as is
2056 if ( cf.myLinks.size() == 3 )
2058 const SMDS_MeshElement* tria = myMesh->AddFace( cf.myLinks[0].myNode1,
2059 cf.myLinks[1].myNode1,
2060 cf.myLinks[2].myNode1 );
2061 new2OldTria = std::make_pair( tria, theNew2OldFaces[ index ].second );
2062 if ( tria->GetID() < (int)theNew2OldFaces.size() )
2063 theNew2OldFaces[ tria->GetID() ] = new2OldTria;
2065 theNew2OldFaces.push_back( new2OldTria );
2067 theNew2OldFaces[ index ].first = 0;
2071 // add used new nodes to theNew2OldNodes
2072 SMESH_MeshAlgos::TNodeIntPairVec::value_type new2OldNode;
2073 new2OldNode.second = 0;
2074 for ( cutLinksIt = myCutLinks.cbegin(); cutLinksIt != myCutLinks.cend(); ++cutLinksIt )
2076 const CutLink& link = *cutLinksIt;
2077 if ( link.IntNode() ) // && link.IntNode()->NbInverseElements() > 0 )
2079 new2OldNode.first = link.IntNode();
2080 theNew2OldNodes.push_back( new2OldNode );
2087 //================================================================================
2088 Intersector::Intersector( SMDS_Mesh* mesh, double tol, const std::vector< gp_XYZ >& normals )
2090 myAlgo = new Algo( mesh, tol, normals );
2092 //================================================================================
2093 Intersector::~Intersector()
2097 //================================================================================
2098 //! compute cut of two faces of the mesh
2099 void Intersector::Cut( const SMDS_MeshElement* face1,
2100 const SMDS_MeshElement* face2,
2101 const int nbCommonNodes )
2103 myAlgo->Cut( face1, face2, nbCommonNodes );
2105 //================================================================================
2106 //! store a face cut by a line given by its ends
2107 // accompanied by indices of intersected face edges.
2108 // Edge index is <0 if a line end is inside the face.
2109 void Intersector::Cut( const SMDS_MeshElement* face,
2110 SMESH_NodeXYZ& lineEnd1,
2112 SMESH_NodeXYZ& lineEnd2,
2115 myAlgo->Cut( face, lineEnd1, edgeIndex1, lineEnd2, edgeIndex2 );
2117 //================================================================================
2118 //! split all face intersected by Cut() methods
2119 void Intersector::MakeNewFaces( SMESH_MeshAlgos::TElemIntPairVec& theNew2OldFaces,
2120 SMESH_MeshAlgos::TNodeIntPairVec& theNew2OldNodes,
2121 const double theSign,
2122 const bool theOptimize )
2124 myAlgo->MakeNewFaces( theNew2OldFaces, theNew2OldNodes, theSign, theOptimize );
2126 //================================================================================
2127 //! Cut a face by planes, whose normals point to parts to keep
2128 bool Intersector::CutByPlanes(const SMDS_MeshElement* theFace,
2129 const std::vector< gp_Ax1 > & thePlanes,
2130 const double theTol,
2131 std::vector< TFace > & theNewFaceConnectivity )
2133 theNewFaceConnectivity.clear();
2135 // check if theFace is wholly cut off
2136 std::vector< SMESH_NodeXYZ > facePoints( theFace->begin_nodes(), theFace->end_nodes() );
2137 facePoints.resize( theFace->NbCornerNodes() );
2138 for ( size_t iP = 0; iP < thePlanes.size(); ++iP )
2141 const gp_Pnt& O = thePlanes[iP].Location();
2142 for ( size_t i = 0; i < facePoints.size(); ++i )
2144 gp_Vec Op( O, facePoints[i] );
2145 nbOut += ( Op * thePlanes[iP].Direction() <= 0 );
2147 if ( nbOut == facePoints.size() )
2151 // copy theFace into a temporary mesh
2154 std::vector< const SMDS_MeshNode* > faceNodes;
2155 faceNodes.resize( facePoints.size() );
2156 for ( size_t i = 0; i < facePoints.size(); ++i )
2158 const SMESH_NodeXYZ& n = facePoints[i];
2159 faceNodes[i] = mesh.AddNode( n.X(), n.Y(), n.Z() );
2162 const SMDS_MeshElement* faceToCut = 0;
2163 switch ( theFace->NbCornerNodes() )
2166 faceToCut = mesh.AddFace( faceNodes[0], faceNodes[1], faceNodes[2] );
2169 faceToCut = mesh.AddFace( faceNodes[0], faceNodes[1], faceNodes[2], faceNodes[3] );
2172 faceToCut = mesh.AddPolygonalFace( faceNodes );
2175 std::vector< gp_XYZ > normals( 2 + thePlanes.size() );
2176 SMESH_MeshAlgos::FaceNormal( faceToCut, normals[ faceToCut->GetID() ]);
2178 // add faces corresponding to thePlanes
2179 std::vector< const SMDS_MeshElement* > planeFaces;
2180 double faceSize = Sqrt( faceBox.SquareExtent() );
2181 gp_XYZ center = 0.5 * ( faceBox.CornerMin() + faceBox.CornerMax() );
2182 for ( size_t i = 0; i < thePlanes.size(); ++i )
2184 gp_Ax2 plnAx( thePlanes[i].Location(), thePlanes[i].Direction() );
2185 gp_XYZ O = plnAx.Location().XYZ();
2186 gp_XYZ X = plnAx.XDirection().XYZ();
2187 gp_XYZ Y = plnAx.YDirection().XYZ();
2188 gp_XYZ Z = plnAx.Direction().XYZ();
2190 double dot = ( O - center ) * Z;
2191 gp_XYZ o = center + Z * dot; // center projected to a plane
2193 gp_XYZ p1 = o + X * faceSize * 2;
2194 gp_XYZ p2 = o + Y * faceSize * 2;
2195 gp_XYZ p3 = o - (X + Y ) * faceSize * 2;
2197 const SMDS_MeshNode* n1 = mesh.AddNode( p1.X(), p1.Y(), p1.Z() );
2198 const SMDS_MeshNode* n2 = mesh.AddNode( p2.X(), p2.Y(), p2.Z() );
2199 const SMDS_MeshNode* n3 = mesh.AddNode( p3.X(), p3.Y(), p3.Z() );
2200 planeFaces.push_back( mesh.AddFace( n1, n2, n3 ));
2202 normals[ planeFaces.back()->GetID() ] = thePlanes[i].Direction().XYZ();
2206 Algo algo ( &mesh, theTol, normals );
2207 for ( size_t i = 0; i < planeFaces.size(); ++i )
2209 algo.Cut( faceToCut, planeFaces[i], 0 );
2212 // retrieve a result
2213 SMESH_MeshAlgos::TElemIntPairVec new2OldFaces;
2214 SMESH_MeshAlgos::TNodeIntPairVec new2OldNodes;
2215 TCutFaceMap::const_iterator cutFacesIt= algo.myCutFaces.cbegin();
2216 for ( ; cutFacesIt != algo.myCutFaces.cend(); ++cutFacesIt )
2218 const CutFace& cf = *cutFacesIt;
2219 if ( cf.myInitFace != faceToCut )
2224 theNewFaceConnectivity.push_back( facePoints );
2228 // intersect cut lines
2229 algo.IntersectNewEdges( cf );
2231 // form loops of new faces
2232 EdgeLoopSet loopSet;
2233 cf.MakeLoops( loopSet, normals[ faceToCut->GetID() ]);
2235 // erase loops that are cut off by thePlanes
2236 const double sign = 1;
2237 std::vector< EdgePart > cutOffLinks;
2238 TLinkMap cutOffCoplanarLinks;
2239 cf.CutOffLoops( loopSet, sign, normals, cutOffLinks, cutOffCoplanarLinks );
2241 for ( size_t iL = 0; iL < loopSet.myNbLoops; ++iL )
2243 EdgeLoop& loop = loopSet.myLoops[ iL ];
2244 if ( loop.myLinks.size() > 0 )
2247 for ( SMDS_NodeIteratorPtr nIt = loop.nodeIterator(); nIt->more(); )
2249 const SMDS_MeshNode* n = nIt->next();
2250 facePoints.push_back( n );
2251 int iN = faceToCut->GetNodeIndex( n );
2253 facePoints.back()._node = 0; // an intersection point
2255 facePoints.back()._node = theFace->GetNode( iN );
2257 theNewFaceConnectivity.push_back( facePoints );
2263 return theNewFaceConnectivity.empty();
2266 } // namespace SMESH_MeshAlgos
2270 //================================================================================
2274 //================================================================================
2276 void CutFace::Dump() const
2278 std::cout << std::endl << "INI F " << myInitFace->GetID() << std::endl;
2279 for ( size_t i = 0; i < myLinks.size(); ++i )
2280 std::cout << "[" << i << "] ("
2281 << char(( myLinks[i].IsInternal() ? 'j' : '0' ) + myLinks[i].myIndex ) << ") "
2282 << myLinks[i].myNode1->GetID() << " - " << myLinks[i].myNode2->GetID()
2283 << " " << ( myLinks[i].myFace ? 'F' : 'C' )
2284 << ( myLinks[i].myFace ? myLinks[i].myFace->GetID() : 0 ) << " " << std::endl;
2287 //================================================================================
2289 * \brief Add an edge cutting this face
2290 * \param [in] p1 - start point of the edge
2291 * \param [in] p2 - end point of the edge
2292 * \param [in] cutter - a face producing the added cut edge.
2293 * \param [in] nbOnPlane - nb of triangle nodes lying on the plane of the cutter face
2295 //================================================================================
2297 void CutFace::AddEdge( const CutLink& p1,
2299 const SMDS_MeshElement* cutterFace,
2300 const int nbOnPlane,
2301 const int iNotOnPlane) const
2303 int iN[2] = { myInitFace->GetNodeIndex( p1.IntNode() ),
2304 myInitFace->GetNodeIndex( p2.IntNode() ) };
2305 if ( iN[0] >= 0 && iN[1] >= 0 )
2307 // the cutting edge is a whole side
2308 if (( cutterFace && nbOnPlane < 3 ) &&
2309 !( cutterFace->GetNodeIndex( p1.IntNode() ) >= 0 &&
2310 cutterFace->GetNodeIndex( p2.IntNode() ) >= 0 ))
2313 myLinks[ Abs( iN[0] - iN[1] ) == 1 ? Min( iN[0], iN[1] ) : 2 ].myFace = cutterFace;
2318 if ( p1.IntNode() == p2.IntNode() )
2320 AddPoint( p1, p2, 1e-10 );
2326 // cut side edges by a new one
2328 int iEOnPlane = ( nbOnPlane == 2 ) ? ( iNotOnPlane + 1 ) % 3 : -1;
2331 for ( int is2nd = 0; is2nd < 2; ++is2nd )
2333 const CutLink& p = is2nd ? p2 : p1;
2335 if ( iN[ is2nd ] >= 0 )
2338 int iE = Max( iEOnPlane, myInitFace->GetNodeIndex( p.Node1() ));
2340 continue; // link of other face
2342 SMESH_NodeXYZ n0 = myLinks[iE].myNode1;
2343 dist[ is2nd ] = ( n0 - p.myIntNode ).SquareModulus();
2345 for ( size_t i = 0; i < myLinks.size(); ++i )
2346 if ( myLinks[i].myIndex == iE )
2348 double d1 = n0.SquareDistance( myLinks[i].myNode1 );
2349 if ( d1 < dist[ is2nd ] )
2351 double d2 = n0.SquareDistance( myLinks[i].myNode2 );
2352 if ( dist[ is2nd ] < d2 )
2354 myLinks.push_back( myLinks[i] );
2355 myLinks.back().myNode1 = myLinks[i].myNode2 = p.IntNode();
2362 int state = nbOnPlane == 3 ? EdgePart::_COPLANAR : EdgePart::_INTERNAL;
2364 // look for an existing equal edge
2365 if ( nbOnPlane == 2 )
2367 SMESH_NodeXYZ n0 = myLinks[ iEOnPlane ].myNode1;
2368 if ( iN[0] >= 0 ) dist[0] = ( n0 - p1.myIntNode ).SquareModulus();
2369 if ( iN[1] >= 0 ) dist[1] = ( n0 - p2.myIntNode ).SquareModulus();
2370 if ( dist[0] > dist[1] )
2371 std::swap( dist[0], dist[1] );
2372 for ( size_t i = 0; i < myLinks.size(); ++i )
2374 if ( myLinks[i].myIndex != iEOnPlane )
2376 gp_XYZ mid = 0.5 * ( SMESH_NodeXYZ( myLinks[i].myNode1 ) +
2377 SMESH_NodeXYZ( myLinks[i].myNode2 ));
2378 double d = ( n0 - mid ).SquareModulus();
2379 if ( dist[0] < d && d < dist[1] )
2380 myLinks[i].myFace = cutterFace;
2386 EdgePart newEdge; newEdge.Set( p1.IntNode(), p2.IntNode(), cutterFace, state );
2387 for ( size_t i = 0; i < myLinks.size(); ++i )
2389 if ( myLinks[i].IsSame( newEdge ))
2391 // if ( !myLinks[i].IsInternal() )
2392 // myLinks[ i ].myFace = cutterFace;
2394 myLinks[ i ].ReplaceCoplanar( newEdge );
2395 if ( myLinks[i].IsInternal() && i+1 < myLinks.size() )
2396 myLinks[ i+1 ].ReplaceCoplanar( newEdge );
2399 i += myLinks[i].IsInternal();
2403 size_t i = myLinks.size();
2404 myLinks.resize( i + 2 );
2405 myLinks[ i ].Set( p1.IntNode(), p2.IntNode(), cutterFace, state );
2406 myLinks[ i+1 ].Set( p2.IntNode(), p1.IntNode(), cutterFace, state );
2409 //================================================================================
2411 * \brief Add a point cutting this face
2413 //================================================================================
2415 void CutFace::AddPoint( const CutLink& p1, const CutLink& p2, double tol ) const
2417 if ( myInitFace->GetNodeIndex( p1.IntNode() ) >= 0 ||
2418 myInitFace->GetNodeIndex( p2.IntNode() ) >= 0 )
2423 const CutLink* link = &p1;
2424 int iE = myInitFace->GetNodeIndex( link->Node1() );
2428 iE = myInitFace->GetNodeIndex( link->Node1() );
2432 // cut an existing edge by the point
2433 SMESH_NodeXYZ n0 = link->Node1();
2434 double d = ( n0 - link->myIntNode ).SquareModulus();
2436 for ( size_t i = 0; i < myLinks.size(); ++i )
2437 if ( myLinks[i].myIndex == iE )
2439 double d1 = n0.SquareDistance( myLinks[i].myNode1 );
2442 double d2 = n0.SquareDistance( myLinks[i].myNode2 );
2445 myLinks.push_back( myLinks[i] );
2446 myLinks.back().myNode1 = myLinks[i].myNode2 = link->IntNode();
2452 else // point is inside the triangle
2454 // // check if a point already added
2455 // for ( size_t i = 3; i < myLinks.size(); ++i )
2456 // if ( myLinks[i].myNode1 == p1.IntNode() )
2459 // // create a link between the point and the closest corner node
2460 // const SMDS_MeshNode* closeNode = myLinks[0].myNode1;
2461 // double minDist = p1.myIntNode.SquareDistance( closeNode );
2462 // for ( int i = 1; i < 3; ++i )
2464 // double dist = p1.myIntNode.SquareDistance( myLinks[i].myNode1 );
2465 // if ( dist < minDist )
2468 // closeNode = myLinks[i].myNode1;
2471 // if ( minDist > tol * tol )
2473 // size_t i = myLinks.size();
2474 // myLinks.resize( i + 2 );
2475 // myLinks[ i ].Set( p1.IntNode(), closeNode );
2476 // myLinks[ i+1 ].Set( closeNode, p1.IntNode() );
2481 //================================================================================
2483 * \brief Perform node replacement
2485 //================================================================================
2487 bool CutFace::ReplaceNodes( const TNNMap& theRm2KeepMap ) const
2489 bool replaced = false;
2490 for ( size_t i = 0; i < myLinks.size(); ++i )
2492 while ( theRm2KeepMap.IsBound( myLinks[i].myNode1 ))
2493 replaced = ( myLinks[i].myNode1 = theRm2KeepMap( myLinks[i].myNode1 ));
2495 while ( theRm2KeepMap.IsBound( myLinks[i].myNode2 ))
2496 replaced = ( myLinks[i].myNode2 = theRm2KeepMap( myLinks[i].myNode2 ));
2499 //if ( replaced ) // remove equal links
2501 for ( size_t i1 = 0; i1 < myLinks.size(); ++i1 )
2503 if ( myLinks[i1].myNode1 == myLinks[i1].myNode2 )
2505 myLinks.erase( myLinks.begin() + i1,
2506 myLinks.begin() + i1 + 1 + myLinks[i1].IsInternal() );
2510 size_t i2 = i1 + 1 + myLinks[i1].IsInternal();
2511 for ( ; i2 < myLinks.size(); ++i2 )
2513 if ( !myLinks[i2].IsInternal() )
2515 if ( myLinks[i1].IsSame( myLinks[i2] ))
2517 myLinks[i1]. ReplaceCoplanar( myLinks[i2] );
2518 if ( myLinks[i1].IsInternal() )
2519 myLinks[i1+1].ReplaceCoplanar( myLinks[i2+1] );
2520 if ( !myLinks[i1].myFace && myLinks[i2].myFace )
2522 myLinks[i1]. myFace = myLinks[i2].myFace;
2523 if ( myLinks[i1].IsInternal() )
2524 myLinks[i1+1].myFace = myLinks[i2+1].myFace;
2526 myLinks.erase( myLinks.begin() + i2,
2527 myLinks.begin() + i2 + 2 );
2533 i1 += myLinks[i1].IsInternal();
2540 //================================================================================
2542 * \brief Initialize myLinks with edges of myInitFace
2544 //================================================================================
2546 void CutFace::InitLinks() const
2548 if ( !myLinks.empty() ) return;
2550 int nbNodes = myInitFace->NbNodes();
2551 myLinks.reserve( nbNodes * 2 );
2552 myLinks.resize( nbNodes );
2554 for ( int i = 0; i < nbNodes; ++i )
2556 const SMDS_MeshNode* n1 = myInitFace->GetNode( i );
2557 const SMDS_MeshNode* n2 = myInitFace->GetNodeWrap( i + 1);
2558 myLinks[i].Set( n1, n2, 0, i );
2562 //================================================================================
2564 * \brief Return number of internal edges
2566 //================================================================================
2568 int CutFace::NbInternalEdges() const
2571 for ( size_t i = 3; i < myLinks.size(); ++i )
2572 nb += myLinks[i].IsInternal();
2574 return nb / 2; // each internal edge encounters twice
2577 //================================================================================
2579 * \brief Remove loops that are not connected to boundary edges of myFace by
2580 * adding edges connecting these loops to the boundary.
2581 * Such loops must be removed as they form polygons with ring topology.
2583 //================================================================================
2585 bool CutFace::RemoveInternalLoops( EdgeLoopSet& theLoops ) const
2587 size_t nbReachedLoops = 0;
2589 // count loops including boundary EdgeParts
2590 for ( size_t iL = 0; iL < theLoops.myNbLoops; ++iL )
2592 EdgeLoop& loop = theLoops.myLoops[ iL ];
2594 for ( size_t iE = 0; iE < loop.myLinks.size(); ++iE )
2595 if ( !loop.myLinks[ iE ]->IsInternal() )
2597 nbReachedLoops += loop.SetConnected();
2601 if ( nbReachedLoops == theLoops.myNbLoops )
2602 return false; // no unreachable loops
2605 // try to reach all loops by propagating via internal edges shared by loops
2606 size_t prevNbReached;
2609 prevNbReached = nbReachedLoops;
2611 for ( size_t iL = 0; iL < theLoops.myNbLoops; ++iL )
2613 EdgeLoop& loop = theLoops.myLoops[ iL ];
2614 if ( !loop.myIsBndConnected )
2617 for ( size_t iE = 0; iE < loop.myLinks.size(); ++iE )
2618 if ( loop.myLinks[ iE ]->IsInternal() )
2620 const EdgePart* twinEdge = getTwin( loop.myLinks[ iE ]);
2621 EdgeLoop* loop2 = theLoops.GetLoopOf( twinEdge );
2622 if ( loop2->SetConnected() && ++nbReachedLoops == theLoops.myNbLoops )
2623 return false; // no unreachable loops
2627 while ( prevNbReached < nbReachedLoops );
2631 for ( size_t iL = 0; iL < theLoops.myNbLoops; ++iL )
2633 EdgeLoop& loop = theLoops.myLoops[ iL ];
2634 if ( loop.myIsBndConnected || loop.myLinks.size() == 0 )
2637 if ( loop.myHasPending )
2639 // try to join the loop to another one, with which it contacts at a node
2641 // look for a node where the loop reverses
2642 const EdgePart* edgePrev = loop.myLinks.back();
2643 for ( size_t iE = 0; iE < loop.myLinks.size(); edgePrev = loop.myLinks[ iE++ ] )
2645 if ( !edgePrev->IsTwin( *loop.myLinks[ iE ]))
2647 const SMDS_MeshNode* reverseNode = edgePrev->myNode2;
2649 // look for a loop including reverseNode
2650 size_t iContactEdge2; // index(+1) of edge starting at reverseNode
2651 for ( size_t iL2 = 0; iL2 < theLoops.myNbLoops; ++iL2 )
2655 EdgeLoop& loop2 = theLoops.myLoops[ iL2 ];
2656 if ( ! ( iContactEdge2 = loop2.Contains( reverseNode )))
2659 // insert loop2 into the loop
2660 theLoops.Join( loop, iE, loop2, iContactEdge2 - 1 );
2663 if ( loop.myIsBndConnected )
2667 if ( loop.myIsBndConnected )
2671 // add links connecting internal loops with the boundary ones
2673 // find a pair of closest nodes
2674 const SMDS_MeshNode *closestNode1, *closestNode2;
2675 double minDist = 1e100;
2676 for ( size_t iE = 0; iE < loop.myLinks.size(); ++iE )
2678 SMESH_NodeXYZ n1 = loop.myLinks[ iE ]->myNode1;
2680 for ( size_t i = 0; i < myLinks.size(); ++i )
2682 if ( !loop.Contains( myLinks[i].myNode1 ))
2684 double dist = n1.SquareDistance( myLinks[i].myNode1 );
2685 if ( dist < minDist )
2688 closestNode1 = loop.myLinks[ iE ]->myNode1;
2689 closestNode2 = myLinks[i].myNode1;
2692 if ( myLinks[i].IsInternal() )
2697 size_t i = myLinks.size();
2698 myLinks.resize( i + 2 );
2699 myLinks[ i ].Set( closestNode1, closestNode2 );
2700 myLinks[ i+1 ].Set( closestNode2, closestNode1 );
2706 //================================================================================
2708 * \brief Return equal reversed edge
2710 //================================================================================
2712 EdgePart* CutFace::getTwin( const EdgePart* edge ) const
2714 size_t i = edge - & myLinks[0];
2716 if ( i > 2 && myLinks[ i-1 ].IsTwin( *edge ))
2717 return & myLinks[ i-1 ];
2719 if ( i+1 < myLinks.size() &&
2720 myLinks[ i+1 ].IsTwin( *edge ))
2721 return & myLinks[ i+1 ];
2726 //================================================================================
2728 * \brief Fill loops of edges
2730 //================================================================================
2732 void CutFace::MakeLoops( EdgeLoopSet& theLoops, const gp_XYZ& theFaceNorm ) const
2734 theLoops.Init( myLinks );
2736 if ( myLinks.size() == 3 )
2738 theLoops.AddNewLoop();
2739 theLoops.AddEdge( myLinks[0] );
2740 theLoops.AddEdge( myLinks[1] );
2741 theLoops.AddEdge( myLinks[2] );
2745 while ( !theLoops.AllEdgesUsed() )
2747 EdgeLoop& loop = theLoops.AddNewLoop();
2749 // add 1st edge to a new loop
2751 for ( i1 = theLoops.myNbLoops - 1; i1 < myLinks.size(); ++i1 )
2752 if ( theLoops.AddEdge( myLinks[i1] ))
2755 EdgePart* lastEdge = & myLinks[ i1 ];
2756 EdgePart* twinEdge = getTwin( lastEdge );
2757 const SMDS_MeshNode* firstNode = lastEdge->myNode1;
2758 const SMDS_MeshNode* lastNode = lastEdge->myNode2;
2760 do // add the rest edges
2762 theLoops.myCandidates.clear(); // edges starting at lastNode
2765 // find candidate edges
2766 for ( size_t i = i1 + 1; i < myLinks.size(); ++i )
2767 if ( myLinks[ i ].myNode1 == lastNode &&
2768 &myLinks[ i ] != twinEdge &&
2769 !theLoops.myIsUsedEdge[ i ])
2771 theLoops.myCandidates.push_back( & myLinks[ i ]);
2772 nbInternal += myLinks[ i ].IsInternal();
2775 // choose among candidates
2776 if ( theLoops.myCandidates.size() == 0 )
2778 loop.myHasPending = bool( twinEdge );
2779 lastEdge = twinEdge;
2781 else if ( theLoops.myCandidates.size() == 1 )
2783 lastEdge = theLoops.myCandidates[0];
2785 else if ( nbInternal == 1 && !lastEdge->IsInternal() )
2787 lastEdge = theLoops.myCandidates[ !theLoops.myCandidates[0]->IsInternal() ];
2791 gp_Vec lastVec = *lastEdge;
2792 double maxAngle = -2 * M_PI;
2793 for ( size_t i = 0; i < theLoops.myCandidates.size(); ++i )
2795 double angle = lastVec.AngleWithRef( *theLoops.myCandidates[i], theFaceNorm );
2796 if ( angle > maxAngle )
2799 lastEdge = theLoops.myCandidates[i];
2803 theLoops.AddEdge( *lastEdge );
2804 lastNode = lastEdge->myNode2;
2805 twinEdge = getTwin( lastEdge );
2807 while ( lastNode != firstNode );
2810 if ( twinEdge == & myLinks[ i1 ])
2811 loop.myHasPending = true;
2813 } // while ( !theLoops.AllEdgesUsed() )
2818 //================================================================================
2820 * \brief Erase loops that are cut off by face intersections
2822 //================================================================================
2824 void CutFace::CutOffLoops( EdgeLoopSet& theLoops,
2825 const double theSign,
2826 const std::vector< gp_XYZ >& theNormals,
2827 std::vector< EdgePart >& theCutOffLinks,
2828 TLinkMap& theCutOffCoplanarLinks) const
2831 for ( size_t i = 0; i < myLinks.size(); ++i )
2833 if ( !myLinks[i].myFace )
2836 EdgeLoop* loop = theLoops.GetLoopOf( & myLinks[i] );
2837 if ( !loop || loop->myLinks.empty() || loop->myHasPending )
2840 bool toErase, isCoplanar = ( myLinks[i].myIndex == EdgePart::_COPLANAR );
2842 gp_Vec iniNorm = theNormals[ myInitFace->GetID() ];
2845 toErase = ( myLinks[i].myFace->GetID() > myInitFace->GetID() );
2847 const EdgePart* twin = getTwin( & myLinks[i] );
2848 if ( !twin || twin->myFace == myLinks[i].myFace )
2850 // only one co-planar face includes myLinks[i]
2851 gp_Vec inFaceDir = iniNorm ^ myLinks[i];
2852 gp_XYZ edgePnt = SMESH_NodeXYZ( myLinks[i].myNode1 );
2853 for ( int iN = 0; iN < 3; ++iN )
2855 gp_Vec inCutFaceDir = ( SMESH_NodeXYZ( myLinks[i].myFace->GetNode( iN )) - edgePnt );
2856 if ( inCutFaceDir * inFaceDir < 0 )
2866 gp_Vec cutNorm = theNormals[ myLinks[i].myFace->GetID() ];
2867 gp_Vec inFaceDir = iniNorm ^ myLinks[i];
2869 toErase = inFaceDir * cutNorm * theSign < 0;
2872 // erase a neighboring loop
2874 if ( const EdgePart* twin = getTwin( & myLinks[i] ))
2875 loop = theLoops.GetLoopOf( twin );
2876 toErase = ( loop && !loop->myLinks.empty() );
2884 // remember whole sides of myInitFace that are cut off
2885 for ( size_t iE = 0; iE < loop->myLinks.size(); ++iE )
2887 if ( !loop->myLinks[ iE ]->myFace &&
2888 !loop->myLinks[ iE ]->IsInternal() )// &&
2889 // !loop->myLinks[ iE ]->myNode1->isMarked() && // cut nodes are marked
2890 // !loop->myLinks[ iE ]->myNode2->isMarked() )
2892 int i = loop->myLinks[ iE ]->myIndex;
2893 sideEdge.Set( myInitFace->GetNode ( i ),
2894 myInitFace->GetNodeWrap( i+1 ));
2895 theCutOffLinks.push_back( sideEdge );
2897 if ( !sideEdge.IsSame( *loop->myLinks[ iE ] )) // nodes replaced
2899 theCutOffLinks.push_back( *loop->myLinks[ iE ] );
2902 else if ( IsCoplanar( loop->myLinks[ iE ]))
2904 // propagate erasure to a co-planar face
2905 theCutOffLinks.push_back( *loop->myLinks[ iE ]);
2907 else if ( loop->myLinks[ iE ]->myFace &&
2908 loop->myLinks[ iE ]->IsInternal() )
2909 theCutOffLinks.push_back( *loop->myLinks[ iE ]);
2913 theLoops.Erase( loop );
2920 //================================================================================
2922 * \brief Check if the face has cut edges
2924 //================================================================================
2926 bool CutFace::IsCut() const
2928 if ( myLinks.size() > 3 )
2931 if ( myLinks.size() == 3 )
2932 for ( size_t i = 0; i < 3; ++i )
2933 if ( myLinks[i].myFace )
2939 //================================================================================
2941 * \brief Check if an edge is produced by a co-planar cut
2943 //================================================================================
2945 bool CutFace::IsCoplanar( const EdgePart* edge ) const
2947 if ( edge->myIndex == EdgePart::_COPLANAR )
2949 const EdgePart* twin = getTwin( edge );
2950 return ( !twin || twin->myIndex == EdgePart::_COPLANAR );
2955 //================================================================================
2957 * \brief Replace _COPLANAR cut edge by _INTERNAL or vice versa
2959 //================================================================================
2961 bool EdgePart::ReplaceCoplanar( const EdgePart& e )
2963 if ( myIndex + e.myIndex == _COPLANAR + _INTERNAL )
2965 //check if the faces are connected
2966 int nbCommonNodes = SMESH_MeshAlgos::GetCommonNodes( e.myFace, myFace ).size();
2967 bool toReplace = (( myIndex == _INTERNAL && nbCommonNodes > 1 ) ||
2968 ( myIndex == _COPLANAR && nbCommonNodes < 2 ));
2971 myIndex = e.myIndex;
2981 //================================================================================
2983 * \brief Create an offsetMesh of given faces
2984 * \param [in] faceIt - the input faces
2985 * \param [out] new2OldFaces - history of faces (new face -> old face ID)
2986 * \param [out] new2OldNodes - history of nodes (new node -> old node ID)
2987 * \return SMDS_Mesh* - the new offset mesh, a caller should delete
2989 //================================================================================
2991 SMDS_Mesh* SMESH_MeshAlgos::MakeOffset( SMDS_ElemIteratorPtr theFaceIt,
2992 SMDS_Mesh& theSrcMesh,
2993 const double theOffset,
2994 const bool theFixIntersections,
2995 TElemIntPairVec& theNew2OldFaces,
2996 TNodeIntPairVec& theNew2OldNodes)
2998 if ( theSrcMesh.GetMeshInfo().NbFaces( ORDER_QUADRATIC ) > 0 )
2999 throw SALOME_Exception( "Offset of quadratic mesh not supported" );
3000 if ( theSrcMesh.GetMeshInfo().NbFaces() > theSrcMesh.GetMeshInfo().NbTriangles() )
3001 throw SALOME_Exception( "Offset of non-triangular mesh not supported" );
3003 SMDS_Mesh* newMesh = new SMDS_Mesh;
3004 theNew2OldFaces.clear();
3005 theNew2OldNodes.clear();
3006 theNew2OldFaces.push_back
3007 ( std::make_pair(( const SMDS_MeshElement*) 0, 0)); // to have index == face->GetID()
3009 // copy input faces to the newMesh keeping IDs of nodes
3011 double minNodeDist = 1e100;
3013 std::vector< const SMDS_MeshNode* > nodes;
3014 while ( theFaceIt->more() )
3016 const SMDS_MeshElement* face = theFaceIt->next();
3017 if ( face->GetType() != SMDSAbs_Face ) continue;
3020 nodes.assign( face->begin_nodes(), face->end_nodes() );
3021 for ( size_t i = 0; i < nodes.size(); ++i )
3023 const SMDS_MeshNode* newNode = newMesh->FindNode( nodes[i]->GetID() );
3026 SMESH_NodeXYZ xyz( nodes[i] );
3027 newNode = newMesh->AddNodeWithID( xyz.X(), xyz.Y(), xyz.Z(), nodes[i]->GetID() );
3028 theNew2OldNodes.push_back( std::make_pair( newNode, nodes[i]->GetID() ));
3032 const SMDS_MeshElement* newFace = 0;
3033 switch ( face->GetEntityType() )
3035 case SMDSEntity_Triangle:
3036 newFace = newMesh->AddFace( nodes[0],nodes[1],nodes[2] );
3038 case SMDSEntity_Quad_Triangle:
3039 newFace = newMesh->AddFace( nodes[0],nodes[1],nodes[2],
3040 nodes[3],nodes[4],nodes[5] );
3042 case SMDSEntity_BiQuad_Triangle:
3043 newFace = newMesh->AddFace( nodes[0],nodes[1],nodes[2],
3044 nodes[3],nodes[4],nodes[5],nodes[6] );
3046 case SMDSEntity_Quadrangle:
3047 newFace = newMesh->AddFace( nodes[0],nodes[1],nodes[2],nodes[3] );
3049 case SMDSEntity_Quad_Quadrangle:
3050 newFace = newMesh->AddFace( nodes[0],nodes[1],nodes[2],nodes[3],
3051 nodes[4],nodes[5],nodes[6],nodes[7] );
3053 case SMDSEntity_BiQuad_Quadrangle:
3054 newFace = newMesh->AddFace( nodes[0],nodes[1],nodes[2],nodes[3],nodes[4],
3055 nodes[5],nodes[6],nodes[7],nodes[8] );
3057 case SMDSEntity_Polygon:
3058 newFace = newMesh->AddPolygonalFace( nodes );
3060 case SMDSEntity_Quad_Polygon:
3061 newFace = newMesh->AddQuadPolygonalFace( nodes );
3066 theNew2OldFaces.push_back( std::make_pair( newFace, face->GetID() ));
3068 SMESH_NodeXYZ pPrev = nodes.back(), p;
3069 for ( size_t i = 0; i < nodes.size(); ++i )
3072 double dist = ( pPrev - p ).SquareModulus();
3073 if ( dist > std::numeric_limits<double>::min() )
3077 } // while ( faceIt->more() )
3080 // compute normals to faces
3081 std::vector< gp_XYZ > normals( theNew2OldFaces.size() );
3082 for ( size_t i = 1; i < normals.size(); ++i )
3084 if ( !SMESH_MeshAlgos::FaceNormal( theNew2OldFaces[i].first, normals[i] ))
3085 normals[i].SetCoord( 0,0,0 ); // TODO find norm by neighbors
3088 const double sign = ( theOffset < 0 ? -1 : +1 );
3089 const double tol = Min( 1e-3 * Sqrt( minNodeDist ),
3090 1e-2 * theOffset * sign );
3092 // translate new nodes by normal to input faces
3094 std::vector< const SMDS_MeshNode* > multiNormalNodes;
3095 for ( size_t i = 0; i < theNew2OldNodes.size(); ++i )
3097 const SMDS_MeshNode* newNode = theNew2OldNodes[i].first;
3099 if ( getTranslatedPosition( newNode, theOffset, tol*10., sign, normals, theSrcMesh, newXYZ ))
3100 newMesh->MoveNode( newNode, newXYZ.X(), newXYZ.Y(), newXYZ.Z() );
3102 multiNormalNodes.push_back( newNode );
3104 // make multi-normal translation
3105 std::vector< SMESH_NodeXYZ > multiPos(10);
3106 for ( size_t i = 0; i < multiNormalNodes.size(); ++i )
3108 const SMDS_MeshNode* newNode = multiNormalNodes[i];
3109 newNode->setIsMarked( true );
3110 SMESH_NodeXYZ oldXYZ = newNode;
3112 for ( SMDS_ElemIteratorPtr fIt = newNode->GetInverseElementIterator(); fIt->more(); )
3114 const SMDS_MeshElement* newFace = fIt->next();
3115 const int faceIndex = newFace->GetID();
3116 const gp_XYZ& oldNorm = normals[ faceIndex ];
3117 const gp_XYZ newXYZ = oldXYZ + oldNorm * theOffset;
3118 if ( multiPos.empty() )
3120 newMesh->MoveNode( newNode, newXYZ.X(), newXYZ.Y(), newXYZ.Z() );
3121 multiPos.emplace_back( newNode );
3126 for ( size_t iP = 0; iP < multiPos.size() && !newNode; ++iP )
3127 if (( multiPos[iP] - newXYZ ).SquareModulus() < tol * tol )
3128 newNode = multiPos[iP].Node();
3131 newNode = newMesh->AddNode( newXYZ.X(), newXYZ.Y(), newXYZ.Z() );
3132 newNode->setIsMarked( true );
3133 theNew2OldNodes.push_back( std::make_pair( newNode, 0 ));
3134 multiPos.emplace_back( newNode );
3137 if ( newNode != oldXYZ.Node() )
3139 nodes.assign( newFace->begin_nodes(), newFace->end_nodes() );
3140 nodes[ newFace->GetNodeIndex( oldXYZ.Node() )] = newNode;
3141 newMesh->ChangeElementNodes( newFace, & nodes[0], nodes.size() );
3146 if ( !theFixIntersections )
3150 // remove new faces around concave nodes (they are marked) if the faces are inverted
3152 for ( size_t i = 0; i < theNew2OldNodes.size(); ++i )
3154 const SMDS_MeshNode* newNode = theNew2OldNodes[i].first;
3155 //const SMDS_MeshNode* oldNode = theNew2OldNodes[i].second;
3156 if ( newNode->isMarked() )
3158 //gp_XYZ moveVec = sign * ( SMESH_NodeXYZ( newNode ) - SMESH_NodeXYZ( oldNode ));
3160 //bool haveInverseFace = false;
3161 for ( SMDS_ElemIteratorPtr fIt = newNode->GetInverseElementIterator(); fIt->more(); )
3163 const SMDS_MeshElement* newFace = fIt->next();
3164 const int faceIndex = newFace->GetID();
3165 const gp_XYZ& oldNorm = normals[ faceIndex ];
3166 if ( !SMESH_MeshAlgos::FaceNormal( newFace, faceNorm, /*normalize=*/false ) ||
3167 //faceNorm * moveVec < 0 )
3168 faceNorm * oldNorm < 0 )
3170 //haveInverseFace = true;
3171 theNew2OldFaces[ faceIndex ].first = 0;
3172 newMesh->RemoveFreeElement( newFace );
3176 // if ( haveInverseFace )
3178 // newMesh->MoveNode( newNode, oldNode->X(), oldNode->Y(), oldNode->Z() );
3180 // for ( SMDS_ElemIteratorPtr fIt = newNode->GetInverseElementIterator(); fIt->more(); )
3182 // const SMDS_MeshElement* newFace = fIt->next();
3183 // if ( !SMESH_MeshAlgos::FaceNormal( newFace, normals[ newFace->GetID() ] ))
3184 // normals[i].SetCoord( 0,0,0 ); // TODO find norm by neighbors
3188 // mark all new nodes located closer than theOffset from theSrcMesh
3191 // ==================================================
3192 // find self-intersections of new faces and fix them
3193 // ==================================================
3195 std::unique_ptr< SMESH_ElementSearcher > fSearcher
3196 ( SMESH_MeshAlgos::GetElementSearcher( *newMesh, tol ));
3198 Intersector intersector( newMesh, tol, normals );
3200 std::vector< const SMDS_MeshElement* > closeFaces;
3201 std::vector< const SMDS_MeshNode* > faceNodes;
3203 for ( size_t iF = 1; iF < theNew2OldFaces.size(); ++iF )
3205 const SMDS_MeshElement* newFace = theNew2OldFaces[iF].first;
3206 if ( !newFace ) continue;
3207 faceNodes.assign( newFace->begin_nodes(), newFace->end_nodes() );
3209 bool isConcaveNode1 = false;
3210 for ( size_t iN = 0; iN < faceNodes.size() && !isConcaveNode1; ++iN )
3211 isConcaveNode1 = faceNodes[iN]->isMarked();
3213 // get faces close to a newFace
3216 for ( size_t i = 0; i < faceNodes.size(); ++i )
3217 faceBox.Add( SMESH_NodeXYZ( faceNodes[i] ));
3218 faceBox.Enlarge( tol );
3220 fSearcher->GetElementsInBox( faceBox, SMDSAbs_Face, closeFaces );
3222 // intersect the newFace with closeFaces
3224 for ( size_t i = 0; i < closeFaces.size(); ++i )
3226 const SMDS_MeshElement* closeFace = closeFaces[i];
3227 if ( closeFace->GetID() <= newFace->GetID() )
3228 continue; // this pair already treated
3230 // do not intersect connected faces if they have no concave nodes
3231 int nbCommonNodes = 0;
3232 for ( size_t iN = 0; iN < faceNodes.size(); ++iN )
3233 nbCommonNodes += ( closeFace->GetNodeIndex( faceNodes[iN] ) >= 0 );
3235 if ( !isConcaveNode1 )
3237 bool isConcaveNode2 = false;
3238 for ( SMDS_ElemIteratorPtr nIt = closeFace->nodesIterator(); nIt->more(); )
3239 if (( isConcaveNode2 = nIt->next()->isMarked() ))
3242 if ( !isConcaveNode2 && nbCommonNodes > 0 )
3246 intersector.Cut( newFace, closeFace, nbCommonNodes );
3249 intersector.MakeNewFaces( theNew2OldFaces, theNew2OldNodes, sign );