1 // Copyright (C) 2007-2020 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
23 // File : StdMeshers_FaceSide.hxx
24 // Created : Wed Jan 31 18:41:25 2007
25 // Author : Edward AGAPOV (eap)
28 #include "StdMeshers_FaceSide.hxx"
30 #include "SMDS_EdgePosition.hxx"
31 #include "SMDS_MeshNode.hxx"
32 #include "SMESHDS_Mesh.hxx"
33 #include "SMESHDS_SubMesh.hxx"
34 #include "SMESH_Algo.hxx"
35 #include "SMESH_Block.hxx"
36 #include "SMESH_ComputeError.hxx"
37 #include "SMESH_Mesh.hxx"
38 #include "SMESH_MeshEditor.hxx"
39 #include "SMESH_MesherHelper.hxx"
41 #include <Adaptor2d_Curve2d.hxx>
42 #include <BRepAdaptor_CompCurve.hxx>
43 #include <BRep_Builder.hxx>
44 #include <BRep_Tool.hxx>
45 #include <GCPnts_AbscissaPoint.hxx>
46 #include <Geom2dAdaptor_Curve.hxx>
47 #include <Geom_Line.hxx>
49 #include <TopExp_Explorer.hxx>
51 #include <TopoDS_Face.hxx>
52 #include <TopoDS_Vertex.hxx>
53 #include <TopoDS_Wire.hxx>
58 #include "utilities.h"
62 //================================================================================
64 * \brief Constructor of a side of one edge
65 * \param theFace - the face
66 * \param theEdge - the edge
68 //================================================================================
70 StdMeshers_FaceSide::StdMeshers_FaceSide(const TopoDS_Face& theFace,
71 const TopoDS_Edge& theEdge,
73 const bool theIsForward,
74 const bool theIgnoreMediumNodes,
75 SMESH_MesherHelper* theFaceHelper,
76 SMESH_ProxyMesh::Ptr theProxyMesh)
78 std::list<TopoDS_Edge> edges(1,theEdge);
79 StdMeshers_FaceSide tmp( theFace, edges, theMesh, theIsForward,
80 theIgnoreMediumNodes, theFaceHelper, theProxyMesh );
86 //================================================================================
88 * \brief Constructor of a side of several edges
90 //================================================================================
92 StdMeshers_FaceSide::StdMeshers_FaceSide(const TopoDS_Face& theFace,
93 const std::list<TopoDS_Edge>& theEdges,
95 const bool theIsForward,
96 const bool theIgnoreMediumNodes,
97 SMESH_MesherHelper* theFaceHelper,
98 SMESH_ProxyMesh::Ptr theProxyMesh)
100 int nbEdges = theEdges.size();
101 myEdge.resize ( nbEdges );
102 myEdgeID.resize ( nbEdges );
103 myC2d.resize ( nbEdges );
104 myC3dAdaptor.resize( nbEdges );
105 myFirst.resize ( nbEdges );
106 myLast.resize ( nbEdges );
107 myNormPar.resize ( nbEdges );
108 myEdgeLength.resize( nbEdges );
109 myIsUniform.resize ( nbEdges, true );
112 myNbPonits = myNbSegments = 0;
113 myProxyMesh = theProxyMesh;
114 myMissingVertexNodes = false;
115 myIgnoreMediumNodes = theIgnoreMediumNodes;
116 myDefaultPnt2d = gp_Pnt2d( 1e+100, 1e+100 );
118 if ( !myProxyMesh ) myProxyMesh.reset( new SMESH_ProxyMesh( *theMesh ));
119 if ( theFaceHelper && theFaceHelper->GetSubShape() == myFace )
121 myHelper = new SMESH_MesherHelper( * myProxyMesh->GetMesh() );
122 myHelper->CopySubShapeInfo( *theFaceHelper );
124 if ( nbEdges == 0 ) return;
126 SMESHDS_Mesh* meshDS = myProxyMesh->GetMeshDS();
129 std::list<TopoDS_Edge>::const_iterator edge = theEdges.begin();
130 for ( int index = 0; edge != theEdges.end(); ++index, ++edge )
132 int i = theIsForward ? index : nbEdges-index-1;
133 myEdgeLength[i] = SMESH_Algo::EdgeLength( *edge );
134 if ( myEdgeLength[i] < DBL_MIN ) nbDegen++;
135 myLength += myEdgeLength[i];
137 myEdgeID[i] = meshDS->ShapeToIndex( *edge );
138 if ( !theIsForward ) myEdge[i].Reverse();
140 if ( theFace.IsNull() )
141 BRep_Tool::Range( *edge, myFirst[i], myLast[i] );
143 myC2d[i] = BRep_Tool::CurveOnSurface( *edge, theFace, myFirst[i], myLast[i] );
144 if ( myEdge[i].Orientation() == TopAbs_REVERSED )
145 std::swap( myFirst[i], myLast[i] );
147 // check if the edge has a non-uniform parametrization (issue 0020705)
148 if ( !myC2d[i].IsNull() )
150 if ( myEdgeLength[i] > DBL_MIN )
152 Geom2dAdaptor_Curve A2dC( myC2d[i],
153 std::min( myFirst[i], myLast[i] ),
154 std::max( myFirst[i], myLast[i] ));
155 double p2 = myFirst[i]+(myLast[i]-myFirst[i])/2.;
156 double p4 = myFirst[i]+(myLast[i]-myFirst[i])/4.;
157 double d2 = GCPnts_AbscissaPoint::Length( A2dC, myFirst[i], p2 );
158 double d4 = GCPnts_AbscissaPoint::Length( A2dC, myFirst[i], p4 );
159 myIsUniform[i] = !( fabs(2*d2/myEdgeLength[i]-1.0) > 0.01 || fabs(2*d4/d2-1.0) > 0.01 );
160 Handle(Geom_Curve) C3d = BRep_Tool::Curve(myEdge[i],d2,d4);
161 myC3dAdaptor[i].Load( C3d, d2,d4 );
165 const TopoDS_Vertex& V = SMESH_MesherHelper::IthVertex( 0, *edge );
166 Handle(Geom_Curve) C3d = new Geom_Line( BRep_Tool::Pnt( V ), gp::DX() );
167 myC3dAdaptor[i].Load( C3d, 0, 0.5 * BRep_Tool::Tolerance( V ));
170 else if ( myEdgeLength[i] > DBL_MIN )
172 Handle(Geom_Curve) C3d = BRep_Tool::Curve(myEdge[i],myFirst[i], myLast[i] );
173 myC3dAdaptor[i].Load( C3d, myFirst[i], myLast[i] );
174 if ( myEdge[i].Orientation() == TopAbs_REVERSED )
175 std::swap( myFirst[i], myLast[i] );
178 // reverse a proxy sub-mesh
180 reverseProxySubmesh( myEdge[i] );
184 // count nodes and segments
185 NbPoints( /*update=*/true );
187 if ( nbEdges > 1 && myLength > DBL_MIN ) {
188 const double degenNormLen = 1.e-5;
189 double totLength = myLength;
191 totLength += myLength * degenNormLen * nbDegen;
192 double prevNormPar = 0;
193 for ( int i = 0; i < nbEdges; ++i ) {
194 if ( myEdgeLength[ i ] < DBL_MIN )
195 myEdgeLength[ i ] = myLength * degenNormLen;
196 myNormPar[ i ] = prevNormPar + myEdgeLength[i]/totLength;
197 prevNormPar = myNormPar[ i ];
200 myNormPar[nbEdges-1] = 1.;
204 //================================================================================
206 * \brief Constructor of a side for vertex using data from other FaceSide
208 //================================================================================
210 StdMeshers_FaceSide::StdMeshers_FaceSide(const StdMeshers_FaceSide* theSide,
211 const SMDS_MeshNode* theNode,
212 const gp_Pnt2d* thePnt2d1,
213 const gp_Pnt2d* thePnt2d2,
214 const Handle(Geom2d_Curve)& theC2d,
215 const double theUFirst,
216 const double theULast)
219 myEdgeID.resize ( 1, 0 );
220 myC2d.push_back ( theC2d );
221 myC3dAdaptor.resize ( 1 );
222 myFirst.push_back ( theUFirst );
223 myLast.push_back ( theULast );
224 myNormPar.push_back ( 1. );
225 myIsUniform.push_back( true );
228 myProxyMesh = theSide->myProxyMesh;
229 myDefaultPnt2d = *thePnt2d1;
230 myPoints = theSide->GetUVPtStruct();
231 myNbPonits = myPoints.size();
232 myNbSegments = theSide->myNbSegments;
234 for ( size_t i = 0; i < myPoints.size(); ++i )
236 double r = i / ( myPoints.size() - 1. );
237 myPoints[i].u = (1-r) * thePnt2d1->X() + r * thePnt2d2->X();
238 myPoints[i].v = (1-r) * thePnt2d1->Y() + r * thePnt2d2->Y();
239 myPoints[i].node = theNode;
242 for ( size_t i = 0; i < myPoints.size(); ++i )
244 myPoints[i].u = thePnt2d1->X();
245 myPoints[i].v = thePnt2d1->Y();
246 myPoints[i].node = theNode;
250 //================================================================================
252 * Create a side from an UVPtStructVec
254 //================================================================================
256 StdMeshers_FaceSide::StdMeshers_FaceSide(UVPtStructVec& theSideNodes,
257 const TopoDS_Face& theFace,
258 const TopoDS_Edge& theEdge,
261 myEdge.resize( 1, theEdge );
262 myEdgeID.resize( 1, -1 );
264 myC3dAdaptor.resize( 1 );
265 myFirst.resize( 1, 0. );
266 myLast.resize( 1, 1. );
267 myNormPar.resize( 1, 1. );
268 myIsUniform.resize( 1, 1 );
269 myMissingVertexNodes = myIgnoreMediumNodes = false;
270 myDefaultPnt2d.SetCoord( 1e100, 1e100 );
271 if ( theMesh ) myProxyMesh.reset( new SMESH_ProxyMesh( *theMesh ));
272 if ( !theEdge.IsNull() )
274 if ( theMesh ) myEdgeID[0] = theMesh->GetMeshDS()->ShapeToIndex( theEdge );
275 if ( theFace.IsNull() )
276 BRep_Tool::Range( theEdge, myFirst[0], myLast[0] );
278 myC2d[0] = BRep_Tool::CurveOnSurface( theEdge, theFace, myFirst[0], myLast[0] );
279 if ( theEdge.Orientation() == TopAbs_REVERSED )
280 std::swap( myFirst[0], myLast[0] );
285 myPoints = theSideNodes;
286 myNbPonits = myPoints.size();
287 myNbSegments = myNbPonits + 1;
290 if ( !myPoints.empty() )
292 myPoints[0].normParam = 0;
293 if ( myPoints[0].node &&
294 myPoints.back().node &&
295 myPoints[ myNbPonits/2 ].node )
297 gp_Pnt pPrev = SMESH_TNodeXYZ( myPoints[0].node );
298 for ( size_t i = 1; i < myPoints.size(); ++i )
300 gp_Pnt p = SMESH_TNodeXYZ( myPoints[i].node );
301 myLength += p.Distance( pPrev );
302 myPoints[i].normParam = myLength;
306 else if ( !theFace.IsNull() )
309 Handle(Geom_Surface) surf = BRep_Tool::Surface( theFace, loc );
310 gp_Pnt pPrev = surf->Value( myPoints[0].u, myPoints[0].v );
311 for ( size_t i = 1; i < myPoints.size(); ++i )
313 gp_Pnt p = surf->Value( myPoints[i].u, myPoints[i].v );
314 myLength += p.Distance( pPrev );
315 myPoints[i].normParam = myLength;
321 gp_Pnt2d pPrev = myPoints[0].UV();
322 for ( size_t i = 1; i < myPoints.size(); ++i )
324 gp_Pnt2d p = myPoints[i].UV();
325 myLength += p.Distance( pPrev );
326 myPoints[i].normParam = myLength;
330 if ( myLength > std::numeric_limits<double>::min() )
331 for ( size_t i = 1; i < myPoints.size(); ++i )
332 myPoints[i].normParam /= myLength;
334 myEdgeLength.resize( 1, myLength );
337 //================================================================================
341 //================================================================================
343 StdMeshers_FaceSide::~StdMeshers_FaceSide()
345 delete myHelper; myHelper = NULL;
348 //================================================================================
350 * Return info on nodes on the side
352 //================================================================================
354 const std::vector<UVPtStruct>& StdMeshers_FaceSide::GetUVPtStruct(bool isXConst,
355 double constValue) const
357 if ( myPoints.empty() )
359 if ( NbEdges() == 0 ) return myPoints;
361 StdMeshers_FaceSide* me = const_cast< StdMeshers_FaceSide* >( this );
365 SMESH_MesherHelper eHelper( *myProxyMesh->GetMesh() );
366 SMESH_MesherHelper& fHelper = *FaceHelper();
368 // sort nodes of all edges by putting them into a map
370 map< double, const SMDS_MeshNode*> u2node;
371 vector< pair< double, const SMDS_MeshNode*> > u2nodeVec;
372 vector<const SMDS_MeshNode*> nodes;
373 set<const SMDS_MeshNode*> vertexNodes;
374 vector< const SMESH_ProxyMesh::SubMesh* > proxySubMesh( myEdge.size() );
375 int nbProxyNodes = 0;
378 for ( iE = 0; iE < myEdge.size(); ++iE )
380 proxySubMesh[iE] = myProxyMesh->GetProxySubMesh( myEdge[iE] );
381 if ( proxySubMesh[iE] )
383 if ( proxySubMesh[iE]->GetUVPtStructVec().empty() ) {
384 proxySubMesh[iE] = 0;
387 nbProxyNodes += proxySubMesh[iE]->GetUVPtStructVec().size() - 1;
388 if ( iE+1 == myEdge.size() )
394 // Add 1st vertex node of a current edge
395 const SMDS_MeshNode* node = VertexNode( iE );
396 const double prevNormPar = ( iE == 0 ? 0 : myNormPar[ iE-1 ]); // normalized param
397 if ( node ) // nodes on internal vertices may be missing
399 if ( vertexNodes.insert( node ).second ||
400 fHelper.IsRealSeam ( node->getshapeId() ) ||
401 fHelper.IsDegenShape( node->getshapeId() ))
402 u2node.insert( u2node.end(), make_pair( prevNormPar, node ));
406 for ( ++iE; iE < myEdge.size(); ++iE )
407 if (( node = VertexNode( iE ))) {
408 u2node.insert( make_pair( prevNormPar, node ));
415 vertexNodes.insert( node );
418 // Add internal nodes
420 if ( !GetEdgeNodes( iE, nodes, /*v0=*/false, /*v1=*/false ))
422 if ( !nodes.empty() )
425 double paramSize = myLast[iE] - myFirst[iE];
426 double r = myNormPar[iE] - prevNormPar;
427 eHelper.SetSubShape( myEdge[iE] );
428 eHelper.ToFixNodeParameters( true );
429 if ( !myIsUniform[iE] )
430 for ( size_t i = 0; i < nodes.size(); ++i )
432 double u = eHelper.GetNodeU( myEdge[iE], nodes[i], 0, ¶mOK );
433 double aLenU = GCPnts_AbscissaPoint::Length( me->myC3dAdaptor[iE], myFirst[iE], u );
434 if ( myEdgeLength[iE] < aLenU ) // nonregression test "3D_mesh_NETGEN/G6"
439 double normPar = prevNormPar + r * aLenU / myEdgeLength[iE];
440 u2nodeVec.push_back( make_pair( normPar, nodes[i] ));
442 if ( u2nodeVec.empty() )
443 for ( size_t i = 0; i < nodes.size(); ++i )
445 double u = eHelper.GetNodeU( myEdge[iE], nodes[i], 0, ¶mOK );
446 // paramSize is signed so orientation is taken into account
447 double normPar = prevNormPar + r * ( u - myFirst[iE] ) / paramSize;
448 u2nodeVec.push_back( make_pair( normPar, nodes[i] ));
450 for ( size_t j = 0; j < u2nodeVec.size(); ++j )
451 u2node.insert( u2node.end(), u2nodeVec[j] );
454 } // loop on myEdge's
456 // Add 2nd VERTEX node for a last EDGE
457 if ( !proxySubMesh.back() )
459 if ( u2node.empty() ) return myPoints;
461 const SMDS_MeshNode* node;
462 if ( IsClosed() && !proxySubMesh[0] )
463 node = u2node.begin()->second;
466 node = VertexNode( iE );
467 if ( myProxyMesh->GetMesh()->HasModificationsToDiscard() )
468 while ( !node && iE > 1 ) // check intermediate VERTEXes
469 node = VertexNode( --iE );
473 if ( u2node.rbegin()->second == node &&
474 !fHelper.IsRealSeam ( node->getshapeId() ) &&
475 !fHelper.IsDegenShape( node->getshapeId() ))
476 u2node.erase( --u2node.end() );
478 u2node.insert( u2node.end(), make_pair( 1., node ));
482 if ((int) u2node.size() + nbProxyNodes != myNbPonits &&
483 (int) u2node.size() + nbProxyNodes != NbPoints( /*update=*/true ))
487 if (( u2node.size() > 0 ) &&
488 ( u2node.begin()->first < 0 || u2node.rbegin()->first > 1 ))
493 // fill array of UVPtStruct
495 UVPtStructVec& points = me->myPoints;
496 points.resize( myNbPonits );
499 double prevNormPar = 0, paramSize = myNormPar[ 0 ];
500 map< double, const SMDS_MeshNode*>::iterator u_node = u2node.begin();
501 for ( size_t iE = 0; iE < myEdge.size(); ++iE )
503 if ( proxySubMesh[ iE ] ) // copy data from a proxy sub-mesh
505 const UVPtStructVec& edgeUVPtStruct = proxySubMesh[iE]->GetUVPtStructVec();
506 UVPtStruct* pointsPtr = & points[iPt];
507 std::copy( edgeUVPtStruct.begin(), edgeUVPtStruct.end(), pointsPtr );
509 double du1 = edgeUVPtStruct.back().param - edgeUVPtStruct[0].param;
510 double du2 = myLast[iE] - myFirst[iE];
513 std::reverse( pointsPtr, pointsPtr + edgeUVPtStruct.size());
514 for ( size_t i = 0; i < edgeUVPtStruct.size(); ++i )
515 pointsPtr[i].normParam = 1. - pointsPtr[i].normParam;
517 // update normalized params
518 if ( myEdge.size() > 1 ) {
519 for ( size_t i = 0; i < edgeUVPtStruct.size(); ++i )
521 UVPtStruct & uvPt = pointsPtr[i];
522 uvPt.normParam = prevNormPar + uvPt.normParam * paramSize;
523 uvPt.x = uvPt.y = uvPt.normParam;
525 iPt += edgeUVPtStruct.size() - 1; // to point to the 1st VERTEX of the next EDGE
527 // update UV on a seam EDGE
528 if ( fHelper.IsRealSeam( myEdgeID[ iE ]))
530 // check if points lye on the EDGE
531 const UVPtStruct& pm = edgeUVPtStruct[ edgeUVPtStruct.size()/2 ];
532 gp_Pnt pNode = SMESH_TNodeXYZ( pm.node );
533 gp_Pnt pCurv = myC3dAdaptor[ iE ].Value( pm.param );
534 double tol = BRep_Tool::Tolerance( myEdge[ iE ]) * 10;
535 bool isPointOnEdge = ( pNode.SquareDistance( pCurv ) < tol * tol );
537 for ( size_t i = 0; i < edgeUVPtStruct.size(); ++i )
538 pointsPtr[i].SetUV( myC2d[ iE ]->Value( pointsPtr[i].param ).XY() );
543 for ( ; u_node != u2node.end(); ++u_node, ++iPt )
545 if ( myNormPar[ iE ]-eps < u_node->first )
546 break; // u_node is at VERTEX of the next EDGE
548 UVPtStruct & uvPt = points[iPt];
549 uvPt.node = u_node->second;
550 // -- normParam, x, y --------------------------------
551 uvPt.normParam = u_node->first;
552 uvPt.x = uvPt.y = uvPt.normParam;
553 // -- U ----------------------------------------------
554 SMDS_EdgePositionPtr epos = uvPt.node->GetPosition();
555 if ( epos && uvPt.node->getshapeId() == myEdgeID[iE] ) {
556 uvPt.param = epos->GetUParameter();
559 double r = ( uvPt.normParam - prevNormPar )/ paramSize;
560 uvPt.param = ( r > 0.5 ? myLast[iE] : myFirst[iE] );
562 // -- UV ---------------------------------------------
563 if ( !myC2d[ iE ].IsNull() ) {
564 gp_Pnt2d p = myC2d[ iE ]->Value( uvPt.param );
569 uvPt.u = uvPt.v = 1e+100;
573 // prepare for the next EDGE
574 if ( iE+1 < myEdge.size() )
576 prevNormPar = myNormPar[ iE ];
577 paramSize = myNormPar[ iE+1 ] - prevNormPar;
579 } // loop on myEdge's
583 for ( iPt = 0; iPt < (int)points.size(); ++iPt ) points[ iPt ].x = constValue;
585 for ( iPt = 0; iPt < (int)points.size(); ++iPt ) points[ iPt ].y = constValue;
587 } // if ( myPoints.empty())
592 //================================================================================
594 * \brief Falsificate info on nodes
595 * \param nbSeg - nb of segments on the side
596 * \retval UVPtStruct* - array of data structures
598 //================================================================================
600 const vector<UVPtStruct>& StdMeshers_FaceSide::SimulateUVPtStruct(int nbSeg,
602 double constValue) const
604 if ( myFalsePoints.empty() )
606 if ( NbEdges() == 0 ) return myFalsePoints;
608 vector<uvPtStruct>* points = const_cast<vector<uvPtStruct>*>( &myFalsePoints );
609 points->resize( nbSeg+1 );
612 double prevNormPar = 0, paramSize = myNormPar[ EdgeIndex ];
614 for ( size_t i = 0 ; i < myFalsePoints.size(); ++i )
616 double normPar = double(i) / double(nbSeg);
617 UVPtStruct & uvPt = (*points)[i];
619 uvPt.x = uvPt.y = uvPt.param = uvPt.normParam = normPar;
620 if ( isXConst ) uvPt.x = constValue;
621 else uvPt.y = constValue;
622 if ( myNormPar[ EdgeIndex ] < normPar )
624 prevNormPar = myNormPar[ EdgeIndex ];
626 paramSize = myNormPar[ EdgeIndex ] - prevNormPar;
628 double r = ( normPar - prevNormPar )/ paramSize;
629 uvPt.param = myFirst[EdgeIndex] * ( 1 - r ) + myLast[EdgeIndex] * r;
630 if ( !myC2d[ EdgeIndex ].IsNull() )
631 p = myC2d[ EdgeIndex ]->Value( uvPt.param );
633 p = Value2d( normPar );
638 return myFalsePoints;
641 //=======================================================================
642 //function : GetOrderedNodes
643 //purpose : Return nodes in the order they encounter while walking along the side
644 //=======================================================================
646 std::vector<const SMDS_MeshNode*> StdMeshers_FaceSide::GetOrderedNodes(int theEdgeInd) const
648 vector<const SMDS_MeshNode*> resultNodes;
649 if ( myPoints.empty() || ( theEdgeInd >= 0 && NbEdges() > 0 ))
651 if ( NbEdges() == 0 ) return resultNodes;
653 //SMESHDS_Mesh* meshDS = myProxyMesh->GetMeshDS();
654 SMESH_MesherHelper eHelper( *myProxyMesh->GetMesh() );
655 SMESH_MesherHelper& fHelper = * FaceHelper();
658 // Sort nodes of all edges putting them into a map
660 map< double, const SMDS_MeshNode*> u2node;
661 vector<const SMDS_MeshNode*> nodes;
662 set<const SMDS_MeshNode*> vertexNodes;
663 int iE = 0, iEnd = myEdge.size();
664 if ( theEdgeInd >= 0 )
666 iE = theEdgeInd % NbEdges();
669 for ( ; iE < iEnd; ++iE )
671 double prevNormPar = ( iE == 0 ? 0 : myNormPar[ iE-1 ]); // normalized param
673 const SMESH_ProxyMesh::SubMesh* proxySM = myProxyMesh->GetProxySubMesh( myEdge[iE] );
676 const UVPtStructVec& points = proxySM->GetUVPtStructVec();
677 for ( size_t i = 0; i < points.size(); ++i )
678 u2node.insert( make_pair( prevNormPar + points[i].normParam, points[i].node ));
682 // Add 1st vertex node of a current EDGE
683 const SMDS_MeshNode* node = VertexNode( iE );
684 if ( node ) { // nodes on internal vertices may be missing
685 if ( vertexNodes.insert( node ).second ||
686 fHelper.IsRealSeam ( node->getshapeId() ) ||
687 fHelper.IsDegenShape( node->getshapeId() ))
688 u2node.insert( make_pair( prevNormPar, node ));
692 if ( nodes.empty() ) {
693 for ( ++iE; iE < iEnd; ++iE )
694 if (( node = VertexNode( iE ))) {
695 u2node.insert( make_pair( prevNormPar, node ));
702 vertexNodes.insert( node );
705 // Add internal nodes
707 if ( !GetEdgeNodes( iE, nodes, /*v0=*/false, /*v1=*/false ))
709 if ( !nodes.empty() )
711 double paramSize = myLast[iE] - myFirst[iE];
712 double r = myNormPar[iE] - prevNormPar;
713 eHelper.SetSubShape( myEdge[iE] );
714 eHelper.ToFixNodeParameters( true );
715 for ( size_t i = 0; i < nodes.size(); ++i )
717 double u = eHelper.GetNodeU( myEdge[iE], nodes[i], 0, ¶mOK );
718 // paramSize is signed so orientation is taken into account
719 double normPar = prevNormPar + r * ( u - myFirst[iE] ) / paramSize;
720 u2node.insert( u2node.end(), make_pair( normPar, nodes[i] ));
726 if ( u2node.empty() ) return resultNodes;
728 // Add 2nd vertex node for a last EDGE
730 const SMDS_MeshNode* node;
731 if ( IsClosed() && theEdgeInd < 0 )
732 node = u2node.begin()->second;
735 node = VertexNode( iE );
736 while ( !node && iE > 0 )
737 node = VertexNode( --iE );
741 if ( u2node.rbegin()->second == node &&
742 !fHelper.IsRealSeam ( node->getshapeId() ) &&
743 !fHelper.IsDegenShape( node->getshapeId() ))
744 u2node.erase( --u2node.end() );
746 u2node.insert( u2node.end(), make_pair( 1., node ));
749 // Fill the result vector
751 if ( theEdgeInd < 0 &&
752 (int) u2node.size() != myNbPonits &&
753 (int) u2node.size() != NbPoints( /*update=*/true ))
757 resultNodes.reserve( u2node.size() );
758 map< double, const SMDS_MeshNode*>::iterator u2n = u2node.begin();
759 for ( ; u2n != u2node.end(); ++u2n )
760 resultNodes.push_back( u2n->second );
764 resultNodes.resize( myPoints.size() );
765 for ( size_t i = 0; i < myPoints.size(); ++i )
766 resultNodes[i] = myPoints[i].node;
772 //================================================================================
774 * \brief Return (unsorted) nodes of the i-th EDGE.
775 * Nodes moved to other geometry by MergeNodes() are also returned.
776 * \retval bool - is OK
778 //================================================================================
780 bool StdMeshers_FaceSide::GetEdgeNodes(size_t i,
781 vector<const SMDS_MeshNode*>& nodes,
782 bool inlude1stVertex,
783 bool inludeLastVertex) const
785 if ( i >= myEdge.size() )
788 SMESH_Mesh* mesh = myProxyMesh->GetMesh();
789 SMESHDS_Mesh* meshDS = mesh->GetMeshDS();
790 SMESHDS_SubMesh* sm = meshDS->MeshElements( myEdge[i] );
792 if ( inlude1stVertex )
794 if ( const SMDS_MeshNode* n0 = VertexNode( i ))
795 nodes.push_back( n0 );
798 if ( sm && ( sm->NbElements() > 0 || sm->NbNodes() > 0 ))
800 if ( mesh->HasModificationsToDiscard() ) // check nb of nodes on the EDGE sub-mesh
802 int iQuad = sm->NbElements() ? sm->GetElements()->next()->IsQuadratic() : 0;
803 int nbExpect = sm->NbElements() - 1 + iQuad * sm->NbElements();
804 if ( nbExpect != sm->NbNodes() ) // some nodes are moved from the EDGE by MergeNodes()
806 // add nodes of all segments
807 typedef set< const SMDS_MeshNode* > TNodeSet;
808 TNodeSet sharedNodes;
809 SMDS_ElemIteratorPtr segIt = sm->GetElements();
810 while ( segIt->more() )
812 const SMDS_MeshElement* seg = segIt->next();
813 if ( seg->GetType() != SMDSAbs_Edge )
815 for ( int i = 0; i < 3-myIgnoreMediumNodes; ++i )
817 const SMDS_MeshNode* n = seg->GetNode( i );
818 if ( i == 2 ) // medium node
820 nodes.push_back( n );
824 pair<TNodeSet::iterator, bool> it2new = sharedNodes.insert( n );
825 if ( !it2new.second ) // n encounters twice == it's on EDGE, not on VERTEX
827 nodes.push_back( n );
828 sharedNodes.erase( it2new.first );
835 if ( nodes.size() < 2 ) // add nodes assigned to the EDGE
837 SMDS_NodeIteratorPtr nItr = sm->GetNodes();
838 while ( nItr->more() )
840 const SMDS_MeshNode* n = nItr->next();
841 if ( myIgnoreMediumNodes && SMESH_MeshEditor::IsMedium( n, SMDSAbs_Edge ))
843 nodes.push_back( n );
846 } // if ( sm && sm->NbElements() > 0 )
848 if ( inludeLastVertex )
850 if ( const SMDS_MeshNode* n1 = VertexNode( i+1 ))
851 nodes.push_back( n1 );
856 //================================================================================
858 * \brief Return a node from the i-th VERTEX (count starts from zero)
859 * Nodes moved to other geometry by MergeNodes() are also returned.
860 * \param [in] i - the VERTEX index
861 * \param [out] isMoved - returns \c true if the found node is moved by MergeNodes()
862 * \return const SMDS_MeshNode* - the found node
864 //================================================================================
866 const SMDS_MeshNode* StdMeshers_FaceSide::VertexNode(std::size_t i, bool* isMoved) const
868 TopoDS_Vertex V = ( i >= myEdge.size() ) ? LastVertex() : FirstVertex(i);
870 const SMDS_MeshNode* n = SMESH_Algo::VertexNode( V, myProxyMesh->GetMeshDS() );
872 if ( !n && !myEdge.empty() && myProxyMesh->GetMesh()->HasModificationsToDiscard() )
874 size_t iE = ( i < myEdge.size() ) ? i : myEdge.size()-1;
875 SMESHDS_SubMesh* sm = myProxyMesh->GetMeshDS()->MeshElements( myEdgeID[ iE ]);
877 n = SMESH_Algo::VertexNode( V, sm, myProxyMesh->GetMesh(), /*checkV=*/false );
880 (( i > 0 && (int) i < NbEdges() ) || IsClosed() ))
882 iE = SMESH_MesherHelper::WrapIndex( int(i)-1, NbEdges() );
883 sm = myProxyMesh->GetMeshDS()->MeshElements( myEdgeID[ iE ]);
884 n = SMESH_Algo::VertexNode( V, sm, myProxyMesh->GetMesh(), /*checkV=*/false );
887 if ( n && n->GetPosition()->GetDim() == 1 ) // check that n does not lie on an EDGE of myFace
889 TopoDS_Shape S = SMESH_MesherHelper::GetSubShapeByNode( n, myProxyMesh->GetMeshDS() );
890 if ( SMESH_MesherHelper::IsSubShape( S, myFace ))
891 n = 0; // VERTEX ignored by e.g. Composite Wire Discretization algo
899 //================================================================================
901 * \brief reverse order of vector elements
902 * \param vec - vector to reverse
904 //================================================================================
906 template <typename T > void reverse(vector<T> & vec)
908 std::reverse( vec.begin(), vec.end() );
911 //================================================================================
913 * \brief Change orientation of side geometry
915 //================================================================================
917 void StdMeshers_FaceSide::Reverse()
919 int nbEdges = myEdge.size();
920 for ( int i = nbEdges-1; i >= 0; --i ) {
921 std::swap( myFirst[i], myLast[i] );
922 if ( !myEdge[i].IsNull() )
924 if ( i > 0 ) // at the first loop 1. is overwritten
925 myNormPar[i] = 1 - myNormPar[i-1];
931 //reverse( myC3dAdaptor );
934 reverse( myNormPar );
935 reverse( myEdgeLength );
936 reverse( myIsUniform );
940 myNormPar[nbEdges-1]=1.;
941 if ( !myEdge[0].IsNull() )
943 for ( size_t i = 0; i < myEdge.size(); ++i )
944 reverseProxySubmesh( myEdge[i] );
946 myFalsePoints.clear();
950 for ( size_t i = 0; i < myPoints.size(); ++i )
952 UVPtStruct & uvPt = myPoints[i];
953 uvPt.normParam = 1 - uvPt.normParam;
959 for ( size_t i = 0; i < myFalsePoints.size(); ++i )
961 UVPtStruct & uvPt = myFalsePoints[i];
962 uvPt.normParam = 1 - uvPt.normParam;
966 reverse( myFalsePoints );
969 for ( size_t i = 0; i < myEdge.size(); ++i )
971 if ( myEdge[i].IsNull() ) continue; // for a side on points only
973 Handle(Geom_Curve) C3d = BRep_Tool::Curve(myEdge[i],fp,lp);
975 myC3dAdaptor[i].Load( C3d, fp,lp );
979 //=======================================================================
980 //function : SetIgnoreMediumNodes
981 //purpose : Make ignore medium nodes
982 //=======================================================================
984 void StdMeshers_FaceSide::SetIgnoreMediumNodes(bool toIgnore)
986 if ( myIgnoreMediumNodes != toIgnore )
988 myIgnoreMediumNodes = toIgnore;
990 if ( !myPoints.empty() )
992 UVPtStructVec newPoints;
993 newPoints.reserve( myPoints.size()/2 + 1 );
994 for ( size_t i = 0; i < myPoints.size(); i += 2 )
995 newPoints.push_back( myPoints[i] );
997 myPoints.swap( newPoints );
1001 NbPoints( /*update=*/true );
1006 //=======================================================================
1007 //function : NbPoints
1008 //purpose : Return nb nodes on edges and vertices (+1 to be == GetUVPtStruct().size() )
1009 // Call it with update == true if mesh of this side can be recomputed
1010 // since creation of this side
1011 //=======================================================================
1013 int StdMeshers_FaceSide::NbPoints(const bool update) const
1015 if ( !myPoints.empty() )
1016 return myPoints.size();
1018 // if ( !myFalsePoints.empty() )
1019 // return myFalsePoints.size();
1021 if ( update && myEdge.size() > 0 )
1023 StdMeshers_FaceSide* me = (StdMeshers_FaceSide*) this;
1025 me->myNbSegments = 0;
1026 me->myMissingVertexNodes = false;
1028 vector<const SMDS_MeshNode*> nodes;
1029 for ( int i = 0; i < NbEdges(); ++i )
1031 if ( const SMESHDS_SubMesh* sm = myProxyMesh->GetSubMesh( Edge(i) ))
1033 if ( sm->NbNodes() == sm->NbElements()-1 || sm->NbElements() == 0 )
1035 me->myNbPonits += sm->NbNodes();
1036 if ( myIgnoreMediumNodes && sm->IsQuadratic() )
1037 me->myNbPonits -= sm->NbElements();
1039 else // nodes can be moved to other shapes by MergeNodes()
1042 GetEdgeNodes( i, nodes, /*v1=*/false, /*v2=*/false );
1043 me->myNbPonits += nodes.size();
1045 me->myNbSegments += sm->NbElements();
1049 SMESH_MesherHelper* helper = FaceHelper();
1051 std::set< const SMDS_MeshNode* > vNodes;
1052 const int nbV = NbEdges() + !IsClosed();
1053 for ( int i = 0; i < nbV; ++i )
1054 if ( const SMDS_MeshNode* n = VertexNode( i ))
1056 if ( !vNodes.insert( n ).second &&
1057 ( helper->IsRealSeam ( n->getshapeId() ) ||
1058 helper->IsDegenShape( n->getshapeId() )))
1063 me->myMissingVertexNodes = true;
1065 me->myNbPonits += vNodes.size();
1068 me->myNbPonits++; // closing node is repeated
1073 //=======================================================================
1074 //function : NbSegments
1075 //purpose : Return nb edges
1076 // Call it with update == true if mesh of this side can be recomputed
1077 // since creation of this side
1078 //=======================================================================
1080 int StdMeshers_FaceSide::NbSegments(const bool update) const
1082 return NbPoints( update ), myNbSegments;
1085 //================================================================================
1087 * \brief Reverse UVPtStructVec if a proxy sub-mesh of E
1089 //================================================================================
1091 void StdMeshers_FaceSide::reverseProxySubmesh( const TopoDS_Edge& E )
1093 if ( !myProxyMesh ) return;
1094 if ( const SMESH_ProxyMesh::SubMesh* sm = myProxyMesh->GetProxySubMesh( E ))
1096 UVPtStructVec& edgeUVPtStruct = (UVPtStructVec& ) sm->GetUVPtStructVec();
1097 for ( size_t i = 0; i < edgeUVPtStruct.size(); ++i )
1099 UVPtStruct & uvPt = edgeUVPtStruct[i];
1100 uvPt.normParam = 1 - uvPt.normParam;
1101 uvPt.x = 1 - uvPt.x;
1102 uvPt.y = 1 - uvPt.y;
1104 reverse( edgeUVPtStruct );
1108 //================================================================================
1110 * \brief Show side features
1112 //================================================================================
1114 void StdMeshers_FaceSide::dump(const char* msg) const
1117 if (msg) MESSAGE ( std::endl << msg );
1118 MESSAGE_BEGIN ("NB EDGES: "<< myEdge.size() );
1119 MESSAGE_ADD ( "nbPoints: "<<myNbPonits<<" vecSize: " << myPoints.size()<<" "<<myFalsePoints.size() );
1120 for ( size_t i = 0; i < myEdge.size(); ++i )
1122 MESSAGE_ADD ( "\t"<<i+1 );
1123 MESSAGE_ADD ( "\tEDGE: " );
1124 if (myEdge[i].IsNull()) {
1125 MESSAGE_ADD ( "NULL" );
1128 TopAbs::Print(myEdge[i].Orientation(),cout)<<" "<<myEdge[i].TShape().operator->()<<endl;
1129 MESSAGE_ADD ( "\tV1: " << TopExp::FirstVertex( myEdge[i], 1).TShape().operator->()
1130 << " V2: " << TopExp::LastVertex( myEdge[i], 1).TShape().operator->() );
1132 MESSAGE_ADD ( "\tC2d: ");
1134 if (myC2d[i].IsNull()) {
1135 MESSAGE_ADD ( "NULL" );
1138 MESSAGE_ADD ( myC2d[i].operator->() );
1141 MESSAGE_ADD ( "\tF: "<<myFirst[i]<< " L: "<< myLast[i] );
1142 MESSAGE_END ( "\tnormPar: "<<myNormPar[i]<<endl );
1147 //================================================================================
1149 * \brief Creates a Adaptor2d_Curve2d to be used in SMESH_Block
1150 * \retval Adaptor2d_Curve2d* -
1152 //================================================================================
1154 struct Adaptor2dCurve2d : public Adaptor2d_Curve2d
1156 const StdMeshers_FaceSide* mySide;
1157 Adaptor2dCurve2d(const StdMeshers_FaceSide* faceSide):mySide(faceSide) {}
1158 gp_Pnt2d Value(const Standard_Real U) const { return mySide->Value2d( U ); }
1159 Standard_Real FirstParameter() const { return 0; }
1160 Standard_Real LastParameter() const { return 1; }
1163 Adaptor2d_Curve2d* StdMeshers_FaceSide::GetCurve2d() const
1165 return new Adaptor2dCurve2d( this );
1168 //================================================================================
1170 * \brief Creates a fully functional Adaptor_Curve
1172 //================================================================================
1174 BRepAdaptor_CompCurve* StdMeshers_FaceSide::GetCurve3d() const
1176 if ( myEdge.empty() )
1180 BRep_Builder aBuilder;
1181 aBuilder.MakeWire(aWire);
1182 for ( size_t i = 0; i < myEdge.size(); ++i )
1183 aBuilder.Add( aWire, myEdge[i] );
1185 if ( myEdge.size() == 2 && IsClosed() )
1186 aWire.Closed(true); // issue 0021141
1188 return new BRepAdaptor_CompCurve( aWire );
1191 //================================================================================
1193 * \brief Return 2D point by normalized parameter
1194 * \param U - normalized parameter value
1195 * \retval gp_Pnt2d - point
1197 //================================================================================
1199 gp_Pnt2d StdMeshers_FaceSide::Value2d(double U) const
1201 if ( !myC2d[0].IsNull() ) {
1202 int i = EdgeIndex( U );
1203 double prevU = i ? myNormPar[ i-1 ] : 0;
1204 double r = ( U - prevU )/ ( myNormPar[ i ] - prevU );
1206 double par = myFirst[i] * ( 1 - r ) + myLast[i] * r;
1208 // check parametrization of curve
1209 if( !myIsUniform[i] )
1211 double aLen3dU = r * myEdgeLength[i] * ( myFirst[i]>myLast[i] ? -1. : 1.);
1212 GCPnts_AbscissaPoint AbPnt
1213 ( const_cast<GeomAdaptor_Curve&>( myC3dAdaptor[i]), aLen3dU, myFirst[i] );
1214 if( AbPnt.IsDone() ) {
1215 par = AbPnt.Parameter();
1218 return myC2d[ i ]->Value(par);
1221 else if ( !myPoints.empty() )
1223 int i = U * double( myPoints.size()-1 );
1224 while ( i > 0 && myPoints[ i ].normParam > U )
1226 while ( i+1 < (int)myPoints.size() && myPoints[ i+1 ].normParam < U )
1228 double r = (( U - myPoints[ i ].normParam ) /
1229 ( myPoints[ i+1 ].normParam - myPoints[ i ].normParam ));
1230 return ( myPoints[ i ].UV() * ( 1 - r ) +
1231 myPoints[ i+1 ].UV() * r );
1233 return myDefaultPnt2d;
1236 //================================================================================
1238 * \brief Return XYZ by normalized parameter
1239 * \param U - normalized parameter value
1240 * \retval gp_Pnt - point
1242 //================================================================================
1244 gp_Pnt StdMeshers_FaceSide::Value3d(double U) const
1246 int i = EdgeIndex( U );
1247 double prevU = i ? myNormPar[ i-1 ] : 0;
1248 double r = ( U - prevU )/ ( myNormPar[ i ] - prevU );
1250 double par = myFirst[i] * ( 1 - r ) + myLast[i] * r;
1252 // check parametrization of curve
1253 if( !myIsUniform[i] )
1255 double aLen3dU = r * myEdgeLength[i] * ( myFirst[i] > myLast[i] ? -1. : 1. );
1256 GCPnts_AbscissaPoint AbPnt
1257 ( const_cast<GeomAdaptor_Curve&>( myC3dAdaptor[i]), aLen3dU, myFirst[i] );
1258 if( AbPnt.IsDone() ) {
1259 par = AbPnt.Parameter();
1262 return myC3dAdaptor[ i ].Value(par);
1265 //================================================================================
1267 * \brief Return wires of a face as StdMeshers_FaceSide's
1269 //================================================================================
1271 TSideVector StdMeshers_FaceSide::GetFaceWires(const TopoDS_Face& theFace,
1272 SMESH_Mesh & theMesh,
1273 const bool theIgnoreMediumNodes,
1275 SMESH_MesherHelper* theFaceHelper,
1276 SMESH_ProxyMesh::Ptr theProxyMesh,
1277 const bool theCheckVertexNodes)
1279 SMESH_MesherHelper helper( theMesh );
1280 if ( theFaceHelper && theFaceHelper->GetSubShape() == theFace )
1281 helper.CopySubShapeInfo( *theFaceHelper );
1283 list< TopoDS_Edge > edges, internalEdges;
1284 list< int > nbEdgesInWires;
1285 int nbWires = SMESH_Block::GetOrderedEdges (theFace, edges, nbEdgesInWires);
1287 // split list of all edges into separate wires
1288 TSideVector wires( nbWires );
1289 list< int >::iterator nbE = nbEdgesInWires.begin();
1290 list< TopoDS_Edge >::iterator from = edges.begin(), to = from;
1291 for ( int iW = 0; iW < nbWires; ++iW, ++nbE )
1293 std::advance( to, *nbE );
1294 if ( *nbE == 0 ) // Issue 0020676
1298 wires.resize( nbWires );
1301 list< TopoDS_Edge > wireEdges( from, to );
1302 // assure that there is a node on the first vertex
1303 // as StdMeshers_FaceSide::GetUVPtStruct() requires
1304 if ( wireEdges.front().Orientation() != TopAbs_INTERNAL ) // Issue 0020676
1306 if ( theCheckVertexNodes )
1307 while ( !SMESH_Algo::VertexNode( TopExp::FirstVertex( wireEdges.front(), true),
1308 theMesh.GetMeshDS()))
1310 wireEdges.splice(wireEdges.end(), wireEdges,
1311 wireEdges.begin(), ++wireEdges.begin());
1312 if ( from->IsSame( wireEdges.front() )) {
1314 ( new SMESH_ComputeError(COMPERR_BAD_INPUT_MESH,"No nodes on vertices"));
1315 return TSideVector(0);
1319 else if ( *nbE > 1 ) // Issue 0020676 (Face_pb_netgen.brep) - several internal edges in a wire
1321 internalEdges.splice( internalEdges.end(), wireEdges, ++wireEdges.begin(), wireEdges.end());
1324 StdMeshers_FaceSide* wire = new StdMeshers_FaceSide( theFace, wireEdges, &theMesh,
1325 /*isForward=*/true, theIgnoreMediumNodes,
1326 &helper, theProxyMesh );
1327 wires[ iW ] = StdMeshers_FaceSidePtr( wire );
1330 while ( !internalEdges.empty() )
1332 StdMeshers_FaceSide* wire = new StdMeshers_FaceSide( theFace, internalEdges.back(), &theMesh,
1333 /*isForward=*/true, theIgnoreMediumNodes,
1334 &helper, theProxyMesh );
1335 wires.push_back( StdMeshers_FaceSidePtr( wire ));
1336 internalEdges.pop_back();
1341 //================================================================================
1343 * \brief Return 1st vertex of the i-the edge
1345 //================================================================================
1347 TopoDS_Vertex StdMeshers_FaceSide::FirstVertex(int i) const
1350 if ( i < NbEdges() )
1352 v = myEdge[i].Orientation() <= TopAbs_REVERSED ? // FORWARD || REVERSED
1353 TopExp::FirstVertex( myEdge[i], 1 ) :
1354 TopoDS::Vertex( TopoDS_Iterator( myEdge[i] ).Value() );
1359 //================================================================================
1361 * \brief Return last vertex of the i-the edge
1363 //================================================================================
1365 TopoDS_Vertex StdMeshers_FaceSide::LastVertex(int i) const
1368 if ( i < NbEdges() )
1370 const TopoDS_Edge& edge = i<0 ? myEdge[ NbEdges() + i ] : myEdge[i];
1371 if ( edge.Orientation() <= TopAbs_REVERSED ) // FORWARD || REVERSED
1372 v = TopExp::LastVertex( edge, 1 );
1374 for ( TopoDS_Iterator vIt( edge ); vIt.More(); vIt.Next() )
1375 v = TopoDS::Vertex( vIt.Value() );
1380 //================================================================================
1382 * \brief Return \c true if the chain of EDGEs is closed
1384 //================================================================================
1386 bool StdMeshers_FaceSide::IsClosed() const
1388 return myEdge.empty() ? false : FirstVertex().IsSame( LastVertex() );
1391 //================================================================================
1393 * \brief Return a helper initialized with the FACE
1395 //================================================================================
1397 SMESH_MesherHelper* StdMeshers_FaceSide::FaceHelper() const
1399 StdMeshers_FaceSide* me = const_cast< StdMeshers_FaceSide* >( this );
1400 if ( !myHelper && myProxyMesh )
1402 me->myHelper = new SMESH_MesherHelper( *myProxyMesh->GetMesh() );
1403 me->myHelper->SetSubShape( myFace );
1405 return me->myHelper;