1 // Copyright (C) 2007-2016 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_Mesh.hxx"
36 #include "SMESH_MesherHelper.hxx"
37 #include "SMESH_ComputeError.hxx"
38 #include "SMESH_Block.hxx"
40 #include <Adaptor2d_Curve2d.hxx>
41 #include <BRepAdaptor_CompCurve.hxx>
42 #include <BRep_Builder.hxx>
43 #include <BRep_Tool.hxx>
44 #include <GCPnts_AbscissaPoint.hxx>
45 #include <Geom2dAdaptor_Curve.hxx>
46 #include <Geom_Line.hxx>
48 #include <TopExp_Explorer.hxx>
50 #include <TopoDS_Face.hxx>
51 #include <TopoDS_Vertex.hxx>
52 #include <TopoDS_Wire.hxx>
57 #include "utilities.h"
61 //================================================================================
63 * \brief Constructor of a side of one edge
64 * \param theFace - the face
65 * \param theEdge - the edge
67 //================================================================================
69 StdMeshers_FaceSide::StdMeshers_FaceSide(const TopoDS_Face& theFace,
70 const TopoDS_Edge& theEdge,
72 const bool theIsForward,
73 const bool theIgnoreMediumNodes,
74 SMESH_ProxyMesh::Ptr theProxyMesh)
76 std::list<TopoDS_Edge> edges(1,theEdge);
77 *this = StdMeshers_FaceSide( theFace, edges, theMesh, theIsForward,
78 theIgnoreMediumNodes, theProxyMesh );
81 //================================================================================
83 * \brief Constructor of a side of several edges
85 //================================================================================
87 StdMeshers_FaceSide::StdMeshers_FaceSide(const TopoDS_Face& theFace,
88 std::list<TopoDS_Edge>& theEdges,
90 const bool theIsForward,
91 const bool theIgnoreMediumNodes,
92 SMESH_ProxyMesh::Ptr theProxyMesh)
94 int nbEdges = theEdges.size();
95 myEdge.resize ( nbEdges );
96 myEdgeID.resize ( nbEdges );
97 myC2d.resize ( nbEdges );
98 myC3dAdaptor.resize( nbEdges );
99 myFirst.resize ( nbEdges );
100 myLast.resize ( nbEdges );
101 myNormPar.resize ( nbEdges );
102 myEdgeLength.resize( nbEdges );
103 myIsUniform.resize ( nbEdges, true );
106 myNbPonits = myNbSegments = 0;
107 myProxyMesh = theProxyMesh;
108 myMissingVertexNodes = false;
109 myIgnoreMediumNodes = theIgnoreMediumNodes;
110 myDefaultPnt2d = gp_Pnt2d( 1e+100, 1e+100 );
111 if ( !myProxyMesh ) myProxyMesh.reset( new SMESH_ProxyMesh( *theMesh ));
112 if ( nbEdges == 0 ) return;
114 SMESHDS_Mesh* meshDS = myProxyMesh->GetMeshDS();
117 std::list<TopoDS_Edge>::iterator edge = theEdges.begin();
118 for ( int index = 0; edge != theEdges.end(); ++index, ++edge )
120 int i = theIsForward ? index : nbEdges-index-1;
121 myEdgeLength[i] = SMESH_Algo::EdgeLength( *edge );
122 if ( myEdgeLength[i] < DBL_MIN ) nbDegen++;
123 myLength += myEdgeLength[i];
125 myEdgeID[i] = meshDS->ShapeToIndex( *edge );
126 if ( !theIsForward ) myEdge[i].Reverse();
128 if ( theFace.IsNull() )
129 BRep_Tool::Range( *edge, myFirst[i], myLast[i] );
131 myC2d[i] = BRep_Tool::CurveOnSurface( *edge, theFace, myFirst[i], myLast[i] );
132 if ( myEdge[i].Orientation() == TopAbs_REVERSED )
133 std::swap( myFirst[i], myLast[i] );
135 // check if the edge has a non-uniform parametrization (issue 0020705)
136 if ( !myC2d[i].IsNull() )
138 if ( myEdgeLength[i] > DBL_MIN )
140 Geom2dAdaptor_Curve A2dC( myC2d[i],
141 std::min( myFirst[i], myLast[i] ),
142 std::max( myFirst[i], myLast[i] ));
143 double p2 = myFirst[i]+(myLast[i]-myFirst[i])/2.;
144 double p4 = myFirst[i]+(myLast[i]-myFirst[i])/4.;
145 double d2 = GCPnts_AbscissaPoint::Length( A2dC, myFirst[i], p2 );
146 double d4 = GCPnts_AbscissaPoint::Length( A2dC, myFirst[i], p4 );
147 myIsUniform[i] = !( fabs(2*d2/myEdgeLength[i]-1.0) > 0.01 || fabs(2*d4/d2-1.0) > 0.01 );
148 Handle(Geom_Curve) C3d = BRep_Tool::Curve(myEdge[i],d2,d4);
149 myC3dAdaptor[i].Load( C3d, d2,d4 );
153 const TopoDS_Vertex& V = SMESH_MesherHelper::IthVertex( 0, *edge );
154 Handle(Geom_Curve) C3d = new Geom_Line( BRep_Tool::Pnt( V ), gp::DX() );
155 myC3dAdaptor[i].Load( C3d, 0, 0.5 * BRep_Tool::Tolerance( V ));
158 // reverse a proxy sub-mesh
160 reverseProxySubmesh( myEdge[i] );
164 // count nodes and segments
165 NbPoints( /*update=*/true );
167 if ( nbEdges > 1 && myLength > DBL_MIN ) {
168 const double degenNormLen = 1.e-5;
169 double totLength = myLength;
171 totLength += myLength * degenNormLen * nbDegen;
172 double prevNormPar = 0;
173 for ( int i = 0; i < nbEdges; ++i ) {
174 if ( myEdgeLength[ i ] < DBL_MIN )
175 myEdgeLength[ i ] = myLength * degenNormLen;
176 myNormPar[ i ] = prevNormPar + myEdgeLength[i]/totLength;
177 prevNormPar = myNormPar[ i ];
180 myNormPar[nbEdges-1] = 1.;
184 //================================================================================
186 * \brief Constructor of a side for vertex using data from other FaceSide
188 //================================================================================
190 StdMeshers_FaceSide::StdMeshers_FaceSide(const StdMeshers_FaceSide* theSide,
191 const SMDS_MeshNode* theNode,
192 const gp_Pnt2d* thePnt2d1,
193 const gp_Pnt2d* thePnt2d2,
194 const Handle(Geom2d_Curve)& theC2d,
195 const double theUFirst,
196 const double theULast)
199 myEdgeID.resize ( 1, 0 );
200 myC2d.push_back ( theC2d );
201 myC3dAdaptor.resize ( 1 );
202 myFirst.push_back ( theUFirst );
203 myLast.push_back ( theULast );
204 myNormPar.push_back ( 1. );
205 myIsUniform.push_back( true );
207 myProxyMesh = theSide->myProxyMesh;
208 myDefaultPnt2d = *thePnt2d1;
209 myPoints = theSide->GetUVPtStruct();
210 myNbPonits = myPoints.size();
211 myNbSegments = theSide->myNbSegments;
213 for ( size_t i = 0; i < myPoints.size(); ++i )
215 double r = i / ( myPoints.size() - 1. );
216 myPoints[i].u = (1-r) * thePnt2d1->X() + r * thePnt2d2->X();
217 myPoints[i].v = (1-r) * thePnt2d1->Y() + r * thePnt2d2->Y();
218 myPoints[i].node = theNode;
221 for ( size_t i = 0; i < myPoints.size(); ++i )
223 myPoints[i].u = thePnt2d1->X();
224 myPoints[i].v = thePnt2d1->Y();
225 myPoints[i].node = theNode;
229 //================================================================================
231 * Create a side from an UVPtStructVec
233 //================================================================================
235 StdMeshers_FaceSide::StdMeshers_FaceSide(UVPtStructVec& theSideNodes,
236 const TopoDS_Face& theFace,
237 const TopoDS_Edge& theEdge,
240 myEdge.resize( 1, theEdge );
241 myEdgeID.resize( 1, -1 );
243 myC3dAdaptor.resize( 1 );
244 myFirst.resize( 1, 0. );
245 myLast.resize( 1, 1. );
246 myNormPar.resize( 1, 1. );
247 myIsUniform.resize( 1, 1 );
248 myMissingVertexNodes = myIgnoreMediumNodes = false;
249 myDefaultPnt2d.SetCoord( 1e100, 1e100 );
250 if ( theMesh ) myProxyMesh.reset( new SMESH_ProxyMesh( *theMesh ));
251 if ( !theEdge.IsNull() )
253 if ( theMesh ) myEdgeID[0] = theMesh->GetMeshDS()->ShapeToIndex( theEdge );
254 if ( theFace.IsNull() )
255 BRep_Tool::Range( theEdge, myFirst[0], myLast[0] );
257 myC2d[0] = BRep_Tool::CurveOnSurface( theEdge, theFace, myFirst[0], myLast[0] );
258 if ( theEdge.Orientation() == TopAbs_REVERSED )
259 std::swap( myFirst[0], myLast[0] );
263 myPoints = theSideNodes;
264 myNbPonits = myPoints.size();
265 myNbSegments = myNbPonits + 1;
268 if ( !myPoints.empty() )
270 myPoints[0].normParam = 0;
271 if ( myPoints[0].node &&
272 myPoints.back().node &&
273 myPoints[ myNbPonits/2 ].node )
275 gp_Pnt pPrev = SMESH_TNodeXYZ( myPoints[0].node );
276 for ( size_t i = 1; i < myPoints.size(); ++i )
278 gp_Pnt p = SMESH_TNodeXYZ( myPoints[i].node );
279 myLength += p.Distance( pPrev );
280 myPoints[i].normParam = myLength;
284 else if ( !theFace.IsNull() )
287 Handle(Geom_Surface) surf = BRep_Tool::Surface( theFace, loc );
288 gp_Pnt pPrev = surf->Value( myPoints[0].u, myPoints[0].v );
289 for ( size_t i = 1; i < myPoints.size(); ++i )
291 gp_Pnt p = surf->Value( myPoints[i].u, myPoints[i].v );
292 myLength += p.Distance( pPrev );
293 myPoints[i].normParam = myLength;
299 gp_Pnt2d pPrev = myPoints[0].UV();
300 for ( size_t i = 1; i < myPoints.size(); ++i )
302 gp_Pnt2d p = myPoints[i].UV();
303 myLength += p.Distance( pPrev );
304 myPoints[i].normParam = myLength;
308 if ( myLength > std::numeric_limits<double>::min() )
309 for ( size_t i = 1; i < myPoints.size(); ++i )
310 myPoints[i].normParam /= myLength;
312 myEdgeLength.resize( 1, myLength );
315 //================================================================================
317 * Return info on nodes on the side
319 //================================================================================
321 const std::vector<UVPtStruct>& StdMeshers_FaceSide::GetUVPtStruct(bool isXConst,
322 double constValue) const
324 if ( myPoints.empty() )
326 if ( NbEdges() == 0 ) return myPoints;
328 StdMeshers_FaceSide* me = const_cast< StdMeshers_FaceSide* >( this );
329 SMESH_MesherHelper eHelper( *myProxyMesh->GetMesh() );
330 SMESH_MesherHelper fHelper( *myProxyMesh->GetMesh() );
331 fHelper.SetSubShape( myFace );
335 // sort nodes of all edges by putting them into a map
337 map< double, const SMDS_MeshNode*> u2node;
338 vector< pair< double, const SMDS_MeshNode*> > u2nodeVec;
339 vector<const SMDS_MeshNode*> nodes;
340 set<const SMDS_MeshNode*> vertexNodes;
341 vector< const SMESH_ProxyMesh::SubMesh* > proxySubMesh( myEdge.size() );
342 int nbProxyNodes = 0;
345 for ( iE = 0; iE < myEdge.size(); ++iE )
347 proxySubMesh[iE] = myProxyMesh->GetProxySubMesh( myEdge[iE] );
348 if ( proxySubMesh[iE] )
350 if ( proxySubMesh[iE]->GetUVPtStructVec().empty() ) {
351 proxySubMesh[iE] = 0;
354 nbProxyNodes += proxySubMesh[iE]->GetUVPtStructVec().size() - 1;
355 if ( iE+1 == myEdge.size() )
361 // Add 1st vertex node of a current edge
362 const SMDS_MeshNode* node = VertexNode( iE );
363 const double prevNormPar = ( iE == 0 ? 0 : myNormPar[ iE-1 ]); // normalized param
364 if ( node ) // nodes on internal vertices may be missing
366 if ( vertexNodes.insert( node ).second ||
367 fHelper.IsRealSeam ( node->getshapeId() ) ||
368 fHelper.IsDegenShape( node->getshapeId() ))
369 u2node.insert( u2node.end(), make_pair( prevNormPar, node ));
373 for ( ++iE; iE < myEdge.size(); ++iE )
374 if (( node = VertexNode( iE ))) {
375 u2node.insert( make_pair( prevNormPar, node ));
382 vertexNodes.insert( node );
385 // Add internal nodes
387 if ( !GetEdgeNodes( iE, nodes, /*v0=*/false, /*v1=*/false ))
389 if ( !nodes.empty() )
392 double paramSize = myLast[iE] - myFirst[iE];
393 double r = myNormPar[iE] - prevNormPar;
394 eHelper.SetSubShape( myEdge[iE] );
395 eHelper.ToFixNodeParameters( true );
396 if ( !myIsUniform[iE] )
397 for ( size_t i = 0; i < nodes.size(); ++i )
399 double u = eHelper.GetNodeU( myEdge[iE], nodes[i], 0, ¶mOK );
400 double aLenU = GCPnts_AbscissaPoint::Length( me->myC3dAdaptor[iE], myFirst[iE], u );
401 if ( myEdgeLength[iE] < aLenU ) // nonregression test "3D_mesh_NETGEN/G6"
406 double normPar = prevNormPar + r * aLenU / myEdgeLength[iE];
407 u2nodeVec.push_back( make_pair( normPar, nodes[i] ));
409 if ( u2nodeVec.empty() )
410 for ( size_t i = 0; i < nodes.size(); ++i )
412 double u = eHelper.GetNodeU( myEdge[iE], nodes[i], 0, ¶mOK );
413 // paramSize is signed so orientation is taken into account
414 double normPar = prevNormPar + r * ( u - myFirst[iE] ) / paramSize;
415 u2nodeVec.push_back( make_pair( normPar, nodes[i] ));
417 for ( size_t j = 0; j < u2nodeVec.size(); ++j )
418 u2node.insert( u2node.end(), u2nodeVec[j] );
420 } // loop on myEdge's
422 // Add 2nd VERTEX node for a last EDGE
423 if ( !proxySubMesh.back() )
425 if ( u2node.empty() ) return myPoints;
427 const SMDS_MeshNode* node;
428 if ( IsClosed() && !proxySubMesh[0] )
429 node = u2node.begin()->second;
432 node = VertexNode( iE );
433 if ( myProxyMesh->GetMesh()->HasModificationsToDiscard() )
434 while ( !node && iE > 1 ) // check intermediate VERTEXes
435 node = VertexNode( --iE );
439 if ( u2node.rbegin()->second == node &&
440 !fHelper.IsRealSeam ( node->getshapeId() ) &&
441 !fHelper.IsDegenShape( node->getshapeId() ))
442 u2node.erase( --u2node.end() );
444 u2node.insert( u2node.end(), make_pair( 1., node ));
448 if ((int) u2node.size() + nbProxyNodes != myNbPonits &&
449 (int) u2node.size() + nbProxyNodes != NbPoints( /*update=*/true ))
453 if (( u2node.size() > 0 ) &&
454 ( u2node.begin()->first < 0 || u2node.rbegin()->first > 1 ))
459 // fill array of UVPtStruct
461 UVPtStructVec& points = me->myPoints;
462 points.resize( myNbPonits );
465 double prevNormPar = 0, paramSize = myNormPar[ 0 ];
466 map< double, const SMDS_MeshNode*>::iterator u_node = u2node.begin();
467 for ( size_t iE = 0; iE < myEdge.size(); ++iE )
469 if ( proxySubMesh[ iE ] ) // copy data from a proxy sub-mesh
471 const UVPtStructVec& edgeUVPtStruct = proxySubMesh[iE]->GetUVPtStructVec();
472 UVPtStruct* pointsPtr = & points[iPt];
473 std::copy( edgeUVPtStruct.begin(), edgeUVPtStruct.end(), pointsPtr );
475 double du1 = edgeUVPtStruct.back().param - edgeUVPtStruct[0].param;
476 double du2 = myLast[iE] - myFirst[iE];
479 std::reverse( pointsPtr, pointsPtr + edgeUVPtStruct.size());
480 for ( size_t i = 0; i < edgeUVPtStruct.size(); ++i )
481 pointsPtr[i].normParam = 1. - pointsPtr[i].normParam;
483 // update normalized params
484 if ( myEdge.size() > 1 ) {
485 for ( size_t i = 0; i < edgeUVPtStruct.size(); ++i )
487 UVPtStruct & uvPt = pointsPtr[i];
488 uvPt.normParam = prevNormPar + uvPt.normParam * paramSize;
489 uvPt.x = uvPt.y = uvPt.normParam;
491 iPt += edgeUVPtStruct.size() - 1; // to point to the 1st VERTEX of the next EDGE
493 // update UV on a seam EDGE
494 if ( fHelper.IsRealSeam( myEdgeID[ iE ]))
496 // check if points lye on the EDGE
497 const UVPtStruct& pm = edgeUVPtStruct[ edgeUVPtStruct.size()/2 ];
498 gp_Pnt pNode = SMESH_TNodeXYZ( pm.node );
499 gp_Pnt pCurv = myC3dAdaptor[ iE ].Value( pm.param );
500 double tol = BRep_Tool::Tolerance( myEdge[ iE ]) * 10;
501 bool isPointOnEdge = ( pNode.SquareDistance( pCurv ) < tol * tol );
503 for ( size_t i = 0; i < edgeUVPtStruct.size(); ++i )
504 pointsPtr[i].SetUV( myC2d[ iE ]->Value( pointsPtr[i].param ).XY() );
509 for ( ; u_node != u2node.end(); ++u_node, ++iPt )
511 if ( myNormPar[ iE ]-eps < u_node->first )
512 break; // u_node is at VERTEX of the next EDGE
514 UVPtStruct & uvPt = points[iPt];
515 uvPt.node = u_node->second;
516 // -- normParam, x, y --------------------------------
517 uvPt.normParam = u_node->first;
518 uvPt.x = uvPt.y = uvPt.normParam;
519 // -- U ----------------------------------------------
520 const SMDS_EdgePosition* epos =
521 dynamic_cast<const SMDS_EdgePosition*>(uvPt.node->GetPosition());
522 if ( epos && uvPt.node->getshapeId() == myEdgeID[iE] ) {
523 uvPt.param = epos->GetUParameter();
526 double r = ( uvPt.normParam - prevNormPar )/ paramSize;
527 uvPt.param = ( r > 0.5 ? myLast[iE] : myFirst[iE] );
529 // -- UV ---------------------------------------------
530 if ( !myC2d[ iE ].IsNull() ) {
531 gp_Pnt2d p = myC2d[ iE ]->Value( uvPt.param );
536 uvPt.u = uvPt.v = 1e+100;
540 // prepare for the next EDGE
541 if ( iE+1 < myEdge.size() )
543 prevNormPar = myNormPar[ iE ];
544 paramSize = myNormPar[ iE+1 ] - prevNormPar;
546 } // loop on myEdge's
550 for ( iPt = 0; iPt < (int)points.size(); ++iPt ) points[ iPt ].x = constValue;
552 for ( iPt = 0; iPt < (int)points.size(); ++iPt ) points[ iPt ].y = constValue;
554 } // if ( myPoints.empty())
559 //================================================================================
561 * \brief Falsificate info on nodes
562 * \param nbSeg - nb of segments on the side
563 * \retval UVPtStruct* - array of data structures
565 //================================================================================
567 const vector<UVPtStruct>& StdMeshers_FaceSide::SimulateUVPtStruct(int nbSeg,
569 double constValue) const
571 if ( myFalsePoints.empty() )
573 if ( NbEdges() == 0 ) return myFalsePoints;
575 vector<uvPtStruct>* points = const_cast<vector<uvPtStruct>*>( &myFalsePoints );
576 points->resize( nbSeg+1 );
579 double prevNormPar = 0, paramSize = myNormPar[ EdgeIndex ];
581 for ( size_t i = 0 ; i < myFalsePoints.size(); ++i )
583 double normPar = double(i) / double(nbSeg);
584 UVPtStruct & uvPt = (*points)[i];
586 uvPt.x = uvPt.y = uvPt.param = uvPt.normParam = normPar;
587 if ( isXConst ) uvPt.x = constValue;
588 else uvPt.y = constValue;
589 if ( myNormPar[ EdgeIndex ] < normPar )
591 prevNormPar = myNormPar[ EdgeIndex ];
593 paramSize = myNormPar[ EdgeIndex ] - prevNormPar;
595 double r = ( normPar - prevNormPar )/ paramSize;
596 uvPt.param = myFirst[EdgeIndex] * ( 1 - r ) + myLast[EdgeIndex] * r;
597 if ( !myC2d[ EdgeIndex ].IsNull() )
598 p = myC2d[ EdgeIndex ]->Value( uvPt.param );
600 p = Value2d( normPar );
605 return myFalsePoints;
608 //=======================================================================
609 //function : GetOrderedNodes
610 //purpose : Return nodes in the order they encounter while walking along the side
611 //=======================================================================
613 std::vector<const SMDS_MeshNode*> StdMeshers_FaceSide::GetOrderedNodes(int theEdgeInd) const
615 vector<const SMDS_MeshNode*> resultNodes;
616 if ( myPoints.empty() || ( theEdgeInd >= 0 && NbEdges() > 0 ))
618 if ( NbEdges() == 0 ) return resultNodes;
620 //SMESHDS_Mesh* meshDS = myProxyMesh->GetMeshDS();
621 SMESH_MesherHelper eHelper( *myProxyMesh->GetMesh() );
622 SMESH_MesherHelper fHelper( *myProxyMesh->GetMesh() );
623 fHelper.SetSubShape( myFace );
626 // Sort nodes of all edges putting them into a map
628 map< double, const SMDS_MeshNode*> u2node;
629 vector<const SMDS_MeshNode*> nodes;
630 set<const SMDS_MeshNode*> vertexNodes;
631 int iE = 0, iEnd = myEdge.size();
632 if ( theEdgeInd >= 0 )
634 iE = theEdgeInd % NbEdges();
637 for ( iE = 0; iE < iEnd; ++iE )
639 double prevNormPar = ( iE == 0 ? 0 : myNormPar[ iE-1 ]); // normalized param
641 const SMESH_ProxyMesh::SubMesh* proxySM = myProxyMesh->GetProxySubMesh( myEdge[iE] );
644 const UVPtStructVec& points = proxySM->GetUVPtStructVec();
645 for ( size_t i = 0; i < points.size(); ++i )
646 u2node.insert( make_pair( prevNormPar + points[i].normParam, points[i].node ));
650 // Add 1st vertex node of a current EDGE
651 const SMDS_MeshNode* node = VertexNode( iE );
652 if ( node ) { // nodes on internal vertices may be missing
653 if ( vertexNodes.insert( node ).second ||
654 fHelper.IsRealSeam ( node->getshapeId() ) ||
655 fHelper.IsDegenShape( node->getshapeId() ))
656 u2node.insert( make_pair( prevNormPar, node ));
660 if ( nodes.empty() ) {
661 for ( ++iE; iE < iEnd; ++iE )
662 if (( node = VertexNode( iE ))) {
663 u2node.insert( make_pair( prevNormPar, node ));
670 vertexNodes.insert( node );
673 // Add internal nodes
675 if ( !GetEdgeNodes( iE, nodes, /*v0=*/false, /*v1=*/false ))
677 if ( !nodes.empty() )
679 double paramSize = myLast[iE] - myFirst[iE];
680 double r = myNormPar[iE] - prevNormPar;
681 eHelper.SetSubShape( myEdge[iE] );
682 eHelper.ToFixNodeParameters( true );
683 for ( size_t i = 0; i < nodes.size(); ++i )
685 double u = eHelper.GetNodeU( myEdge[iE], nodes[i], 0, ¶mOK );
686 // paramSize is signed so orientation is taken into account
687 double normPar = prevNormPar + r * ( u - myFirst[iE] ) / paramSize;
688 u2node.insert( u2node.end(), make_pair( normPar, nodes[i] ));
694 if ( u2node.empty() ) return resultNodes;
696 // Add 2nd vertex node for a last EDGE
698 const SMDS_MeshNode* node;
699 if ( IsClosed() && theEdgeInd < 0 )
700 node = u2node.begin()->second;
703 node = VertexNode( iE );
704 while ( !node && iE > 0 )
705 node = VertexNode( --iE );
709 if ( u2node.rbegin()->second == node &&
710 !fHelper.IsRealSeam ( node->getshapeId() ) &&
711 !fHelper.IsDegenShape( node->getshapeId() ))
712 u2node.erase( --u2node.end() );
714 u2node.insert( u2node.end(), make_pair( 1., node ));
717 // Fill the result vector
719 if ( theEdgeInd < 0 &&
720 (int) u2node.size() != myNbPonits &&
721 (int) u2node.size() != NbPoints( /*update=*/true ))
725 resultNodes.reserve( u2node.size() );
726 map< double, const SMDS_MeshNode*>::iterator u2n = u2node.begin();
727 for ( ; u2n != u2node.end(); ++u2n )
728 resultNodes.push_back( u2n->second );
732 resultNodes.resize( myPoints.size() );
733 for ( size_t i = 0; i < myPoints.size(); ++i )
734 resultNodes[i] = myPoints[i].node;
740 //================================================================================
742 * \brief Return (unsorted) nodes of the i-th EDGE.
743 * Nodes moved to other geometry by MergeNodes() are also returned.
744 * \retval bool - is OK
746 //================================================================================
748 bool StdMeshers_FaceSide::GetEdgeNodes(size_t i,
749 vector<const SMDS_MeshNode*>& nodes,
750 bool inlude1stVertex,
751 bool inludeLastVertex) const
753 if ( i >= myEdge.size() )
756 SMESH_Mesh* mesh = myProxyMesh->GetMesh();
757 SMESHDS_Mesh* meshDS = mesh->GetMeshDS();
758 SMESHDS_SubMesh* sm = meshDS->MeshElements( myEdge[i] );
760 if ( inlude1stVertex )
762 if ( const SMDS_MeshNode* n0 = VertexNode( i ))
763 nodes.push_back( n0 );
766 if ( sm && ( sm->NbElements() > 0 || sm->NbNodes() > 0 ))
768 if ( mesh->HasModificationsToDiscard() ) // check nb of nodes on the EDGE sub-mesh
770 int iQuad = sm->NbElements() ? sm->GetElements()->next()->IsQuadratic() : 0;
771 int nbExpect = sm->NbElements() - 1 + iQuad * sm->NbElements();
772 if ( nbExpect != sm->NbNodes() ) // some nodes are moved from the EDGE by MergeNodes()
774 // add nodes of all segments
775 typedef set< const SMDS_MeshNode* > TNodeSet;
776 TNodeSet sharedNodes;
777 SMDS_ElemIteratorPtr segIt = sm->GetElements();
778 while ( segIt->more() )
780 const SMDS_MeshElement* seg = segIt->next();
781 if ( seg->GetType() != SMDSAbs_Edge )
783 for ( int i = 0; i < 3-myIgnoreMediumNodes; ++i )
785 const SMDS_MeshNode* n = seg->GetNode( i );
786 if ( i == 2 ) // medium node
788 nodes.push_back( n );
792 pair<TNodeSet::iterator, bool> it2new = sharedNodes.insert( n );
793 if ( !it2new.second ) // n encounters twice == it's on EDGE, not on VERTEX
795 nodes.push_back( n );
796 sharedNodes.erase( it2new.first );
803 if ( nodes.size() < 2 ) // add nodes assigned to the EDGE
805 SMDS_NodeIteratorPtr nItr = sm->GetNodes();
806 while ( nItr->more() )
808 const SMDS_MeshNode* n = nItr->next();
809 if ( myIgnoreMediumNodes && SMESH_MeshEditor::IsMedium( n, SMDSAbs_Edge ))
811 nodes.push_back( n );
814 } // if ( sm && sm->NbElements() > 0 )
816 if ( inludeLastVertex )
818 if ( const SMDS_MeshNode* n1 = VertexNode( i+1 ))
819 nodes.push_back( n1 );
824 //================================================================================
826 * \brief Return a node from the i-th VERTEX (count starts from zero)
827 * Nodes moved to other geometry by MergeNodes() are also returned.
828 * \param [in] i - the VERTEX index
829 * \param [out] isMoved - returns \c true if the found node is moved by MergeNodes()
830 * \return const SMDS_MeshNode* - the found node
832 //================================================================================
834 const SMDS_MeshNode* StdMeshers_FaceSide::VertexNode(std::size_t i, bool* isMoved) const
836 TopoDS_Vertex V = ( i >= myEdge.size() ) ? LastVertex() : FirstVertex(i);
838 const SMDS_MeshNode* n = SMESH_Algo::VertexNode( V, myProxyMesh->GetMeshDS() );
840 if ( !n && !myEdge.empty() && myProxyMesh->GetMesh()->HasModificationsToDiscard() )
842 size_t iE = ( i < myEdge.size() ) ? i : myEdge.size()-1;
843 SMESHDS_SubMesh* sm = myProxyMesh->GetMeshDS()->MeshElements( myEdgeID[ iE ]);
845 n = SMESH_Algo::VertexNode( V, sm, myProxyMesh->GetMesh(), /*checkV=*/false );
848 (( i > 0 && (int) i < NbEdges() ) || IsClosed() ))
850 iE = SMESH_MesherHelper::WrapIndex( int(i)-1, NbEdges() );
851 sm = myProxyMesh->GetMeshDS()->MeshElements( myEdgeID[ iE ]);
852 n = SMESH_Algo::VertexNode( V, sm, myProxyMesh->GetMesh(), /*checkV=*/false );
855 if ( n && n->GetPosition()->GetDim() == 1 ) // check that n does not lie on an EDGE of myFace
857 TopoDS_Shape S = SMESH_MesherHelper::GetSubShapeByNode( n, myProxyMesh->GetMeshDS() );
858 if ( SMESH_MesherHelper::IsSubShape( S, myFace ))
859 n = 0; // VERTEX ignored by e.g. Composite Wire Discretization algo
867 //================================================================================
869 * \brief reverse order of vector elements
870 * \param vec - vector to reverse
872 //================================================================================
874 template <typename T > void reverse(vector<T> & vec)
876 std::reverse( vec.begin(), vec.end() );
879 //================================================================================
881 * \brief Change orientation of side geometry
883 //================================================================================
885 void StdMeshers_FaceSide::Reverse()
887 int nbEdges = myEdge.size();
888 for ( int i = nbEdges-1; i >= 0; --i ) {
889 std::swap( myFirst[i], myLast[i] );
890 if ( !myEdge[i].IsNull() )
892 if ( i > 0 ) // at the first loop 1. is overwritten
893 myNormPar[i] = 1 - myNormPar[i-1];
899 //reverse( myC3dAdaptor );
902 reverse( myNormPar );
903 reverse( myEdgeLength );
904 reverse( myIsUniform );
908 myNormPar[nbEdges-1]=1.;
909 if ( !myEdge[0].IsNull() )
911 for ( size_t i = 0; i < myEdge.size(); ++i )
912 reverseProxySubmesh( myEdge[i] );
914 myFalsePoints.clear();
918 for ( size_t i = 0; i < myPoints.size(); ++i )
920 UVPtStruct & uvPt = myPoints[i];
921 uvPt.normParam = 1 - uvPt.normParam;
927 for ( size_t i = 0; i < myFalsePoints.size(); ++i )
929 UVPtStruct & uvPt = myFalsePoints[i];
930 uvPt.normParam = 1 - uvPt.normParam;
934 reverse( myFalsePoints );
937 for ( size_t i = 0; i < myEdge.size(); ++i )
939 if ( myEdge[i].IsNull() ) continue; // for a side on points only
941 Handle(Geom_Curve) C3d = BRep_Tool::Curve(myEdge[i],fp,lp);
943 myC3dAdaptor[i].Load( C3d, fp,lp );
947 //=======================================================================
948 //function : SetIgnoreMediumNodes
949 //purpose : Make ignore medium nodes
950 //=======================================================================
952 void StdMeshers_FaceSide::SetIgnoreMediumNodes(bool toIgnore)
954 if ( myIgnoreMediumNodes != toIgnore )
956 myIgnoreMediumNodes = toIgnore;
958 if ( !myPoints.empty() )
960 UVPtStructVec newPoints;
961 newPoints.reserve( myPoints.size()/2 + 1 );
962 for ( size_t i = 0; i < myPoints.size(); i += 2 )
963 newPoints.push_back( myPoints[i] );
965 myPoints.swap( newPoints );
969 NbPoints( /*update=*/true );
974 //=======================================================================
975 //function : NbPoints
976 //purpose : Return nb nodes on edges and vertices (+1 to be == GetUVPtStruct().size() )
977 // Call it with update == true if mesh of this side can be recomputed
978 // since creation of this side
979 //=======================================================================
981 int StdMeshers_FaceSide::NbPoints(const bool update) const
983 if ( !myPoints.empty() )
984 return myPoints.size();
986 // if ( !myFalsePoints.empty() )
987 // return myFalsePoints.size();
989 if ( update && myEdge.size() > 0 )
991 StdMeshers_FaceSide* me = (StdMeshers_FaceSide*) this;
993 me->myNbSegments = 0;
994 me->myMissingVertexNodes = false;
996 vector<const SMDS_MeshNode*> nodes;
997 for ( int i = 0; i < NbEdges(); ++i )
999 if ( const SMESHDS_SubMesh* sm = myProxyMesh->GetSubMesh( Edge(i) ))
1001 if ( sm->NbNodes() == sm->NbElements()-1 || sm->NbElements() == 0 )
1003 me->myNbPonits += sm->NbNodes();
1004 if ( myIgnoreMediumNodes && sm->IsQuadratic() )
1005 me->myNbPonits -= sm->NbElements();
1007 else // nodes can be moved to other shapes by MergeNodes()
1010 GetEdgeNodes( i, nodes, /*v1=*/false, /*v2=*/false );
1011 me->myNbPonits += nodes.size();
1013 me->myNbSegments += sm->NbElements();
1017 SMESH_MesherHelper helper( *myProxyMesh->GetMesh() );
1018 helper.SetSubShape( myFace );
1020 std::set< const SMDS_MeshNode* > vNodes;
1021 const int nbV = NbEdges() + !IsClosed();
1022 for ( int i = 0; i < nbV; ++i )
1023 if ( const SMDS_MeshNode* n = VertexNode( i ))
1025 if ( !vNodes.insert( n ).second &&
1026 ( helper.IsRealSeam ( n->getshapeId() ) ||
1027 helper.IsDegenShape( n->getshapeId() )))
1032 me->myMissingVertexNodes = true;
1034 me->myNbPonits += vNodes.size();
1037 me->myNbPonits++; // closing node is repeated
1042 //=======================================================================
1043 //function : NbSegments
1044 //purpose : Return nb edges
1045 // Call it with update == true if mesh of this side can be recomputed
1046 // since creation of this side
1047 //=======================================================================
1049 int StdMeshers_FaceSide::NbSegments(const bool update) const
1051 return NbPoints( update ), myNbSegments;
1054 //================================================================================
1056 * \brief Reverse UVPtStructVec if a proxy sub-mesh of E
1058 //================================================================================
1060 void StdMeshers_FaceSide::reverseProxySubmesh( const TopoDS_Edge& E )
1062 if ( !myProxyMesh ) return;
1063 if ( const SMESH_ProxyMesh::SubMesh* sm = myProxyMesh->GetProxySubMesh( E ))
1065 UVPtStructVec& edgeUVPtStruct = (UVPtStructVec& ) sm->GetUVPtStructVec();
1066 for ( size_t i = 0; i < edgeUVPtStruct.size(); ++i )
1068 UVPtStruct & uvPt = edgeUVPtStruct[i];
1069 uvPt.normParam = 1 - uvPt.normParam;
1070 uvPt.x = 1 - uvPt.x;
1071 uvPt.y = 1 - uvPt.y;
1073 reverse( edgeUVPtStruct );
1077 //================================================================================
1079 * \brief Show side features
1081 //================================================================================
1083 void StdMeshers_FaceSide::dump(const char* msg) const
1086 if (msg) MESSAGE ( std::endl << msg );
1087 MESSAGE_BEGIN ("NB EDGES: "<< myEdge.size() );
1088 MESSAGE_ADD ( "nbPoints: "<<myNbPonits<<" vecSize: " << myPoints.size()<<" "<<myFalsePoints.size() );
1089 for ( size_t i = 0; i < myEdge.size(); ++i )
1091 MESSAGE_ADD ( "\t"<<i+1 );
1092 MESSAGE_ADD ( "\tEDGE: " );
1093 if (myEdge[i].IsNull()) {
1094 MESSAGE_ADD ( "NULL" );
1097 TopAbs::Print(myEdge[i].Orientation(),cout)<<" "<<myEdge[i].TShape().operator->()<<endl;
1098 MESSAGE_ADD ( "\tV1: " << TopExp::FirstVertex( myEdge[i], 1).TShape().operator->()
1099 << " V2: " << TopExp::LastVertex( myEdge[i], 1).TShape().operator->() );
1101 MESSAGE_ADD ( "\tC2d: ");
1103 if (myC2d[i].IsNull()) {
1104 MESSAGE_ADD ( "NULL" );
1107 MESSAGE_ADD ( myC2d[i].operator->() );
1110 MESSAGE_ADD ( "\tF: "<<myFirst[i]<< " L: "<< myLast[i] );
1111 MESSAGE_END ( "\tnormPar: "<<myNormPar[i]<<endl );
1116 //================================================================================
1118 * \brief Creates a Adaptor2d_Curve2d to be used in SMESH_Block
1119 * \retval Adaptor2d_Curve2d* -
1121 //================================================================================
1123 struct Adaptor2dCurve2d : public Adaptor2d_Curve2d
1125 const StdMeshers_FaceSide* mySide;
1126 Adaptor2dCurve2d(const StdMeshers_FaceSide* faceSide):mySide(faceSide) {}
1127 gp_Pnt2d Value(const Standard_Real U) const { return mySide->Value2d( U ); }
1128 Standard_Real FirstParameter() const { return 0; }
1129 Standard_Real LastParameter() const { return 1; }
1132 Adaptor2d_Curve2d* StdMeshers_FaceSide::GetCurve2d() const
1134 return new Adaptor2dCurve2d( this );
1137 //================================================================================
1139 * \brief Creates a fully functional Adaptor_Curve
1141 //================================================================================
1143 BRepAdaptor_CompCurve* StdMeshers_FaceSide::GetCurve3d() const
1145 if ( myEdge.empty() )
1149 BRep_Builder aBuilder;
1150 aBuilder.MakeWire(aWire);
1151 for ( size_t i = 0; i < myEdge.size(); ++i )
1152 aBuilder.Add( aWire, myEdge[i] );
1154 if ( myEdge.size() == 2 && IsClosed() )
1155 aWire.Closed(true); // issue 0021141
1157 return new BRepAdaptor_CompCurve( aWire );
1160 //================================================================================
1162 * \brief Return 2D point by normalized parameter
1163 * \param U - normalized parameter value
1164 * \retval gp_Pnt2d - point
1166 //================================================================================
1168 gp_Pnt2d StdMeshers_FaceSide::Value2d(double U) const
1170 if ( !myC2d[0].IsNull() ) {
1171 int i = EdgeIndex( U );
1172 double prevU = i ? myNormPar[ i-1 ] : 0;
1173 double r = ( U - prevU )/ ( myNormPar[ i ] - prevU );
1175 double par = myFirst[i] * ( 1 - r ) + myLast[i] * r;
1177 // check parametrization of curve
1178 if( !myIsUniform[i] )
1180 double aLen3dU = r * myEdgeLength[i] * ( myFirst[i]>myLast[i] ? -1. : 1.);
1181 GCPnts_AbscissaPoint AbPnt
1182 ( const_cast<GeomAdaptor_Curve&>( myC3dAdaptor[i]), aLen3dU, myFirst[i] );
1183 if( AbPnt.IsDone() ) {
1184 par = AbPnt.Parameter();
1187 return myC2d[ i ]->Value(par);
1190 else if ( !myPoints.empty() )
1192 int i = U * double( myPoints.size()-1 );
1193 while ( i > 0 && myPoints[ i ].normParam > U )
1195 while ( i+1 < (int)myPoints.size() && myPoints[ i+1 ].normParam < U )
1197 double r = (( U - myPoints[ i ].normParam ) /
1198 ( myPoints[ i+1 ].normParam - myPoints[ i ].normParam ));
1199 return ( myPoints[ i ].UV() * ( 1 - r ) +
1200 myPoints[ i+1 ].UV() * r );
1202 return myDefaultPnt2d;
1205 //================================================================================
1207 * \brief Return XYZ by normalized parameter
1208 * \param U - normalized parameter value
1209 * \retval gp_Pnt - point
1211 //================================================================================
1213 gp_Pnt StdMeshers_FaceSide::Value3d(double U) const
1215 int i = EdgeIndex( U );
1216 double prevU = i ? myNormPar[ i-1 ] : 0;
1217 double r = ( U - prevU )/ ( myNormPar[ i ] - prevU );
1219 double par = myFirst[i] * ( 1 - r ) + myLast[i] * r;
1221 // check parametrization of curve
1222 if( !myIsUniform[i] )
1224 double aLen3dU = r * myEdgeLength[i] * ( myFirst[i]>myLast[i] ? -1. : 1.);
1225 GCPnts_AbscissaPoint AbPnt
1226 ( const_cast<GeomAdaptor_Curve&>( myC3dAdaptor[i]), aLen3dU, myFirst[i] );
1227 if( AbPnt.IsDone() ) {
1228 par = AbPnt.Parameter();
1231 return myC3dAdaptor[ i ].Value(par);
1234 //================================================================================
1236 * \brief Return wires of a face as StdMeshers_FaceSide's
1238 //================================================================================
1240 TSideVector StdMeshers_FaceSide::GetFaceWires(const TopoDS_Face& theFace,
1241 SMESH_Mesh & theMesh,
1242 const bool theIgnoreMediumNodes,
1244 SMESH_ProxyMesh::Ptr theProxyMesh,
1245 const bool theCheckVertexNodes)
1247 list< TopoDS_Edge > edges, internalEdges;
1248 list< int > nbEdgesInWires;
1249 int nbWires = SMESH_Block::GetOrderedEdges (theFace, edges, nbEdgesInWires);
1251 // split list of all edges into separate wires
1252 TSideVector wires( nbWires );
1253 list< int >::iterator nbE = nbEdgesInWires.begin();
1254 list< TopoDS_Edge >::iterator from = edges.begin(), to = from;
1255 for ( int iW = 0; iW < nbWires; ++iW, ++nbE )
1257 std::advance( to, *nbE );
1258 if ( *nbE == 0 ) // Issue 0020676
1262 wires.resize( nbWires );
1265 list< TopoDS_Edge > wireEdges( from, to );
1266 // assure that there is a node on the first vertex
1267 // as StdMeshers_FaceSide::GetUVPtStruct() requires
1268 if ( wireEdges.front().Orientation() != TopAbs_INTERNAL ) // Issue 0020676
1270 if ( theCheckVertexNodes )
1271 while ( !SMESH_Algo::VertexNode( TopExp::FirstVertex( wireEdges.front(), true),
1272 theMesh.GetMeshDS()))
1274 wireEdges.splice(wireEdges.end(), wireEdges,
1275 wireEdges.begin(), ++wireEdges.begin());
1276 if ( from->IsSame( wireEdges.front() )) {
1278 ( new SMESH_ComputeError(COMPERR_BAD_INPUT_MESH,"No nodes on vertices"));
1279 return TSideVector(0);
1283 else if ( *nbE > 1 ) // Issue 0020676 (Face_pb_netgen.brep) - several internal edges in a wire
1285 internalEdges.splice( internalEdges.end(), wireEdges, ++wireEdges.begin(), wireEdges.end());
1288 StdMeshers_FaceSide* wire = new StdMeshers_FaceSide( theFace, wireEdges, &theMesh,
1289 /*isForward=*/true, theIgnoreMediumNodes,
1291 wires[ iW ] = StdMeshers_FaceSidePtr( wire );
1294 while ( !internalEdges.empty() )
1296 StdMeshers_FaceSide* wire = new StdMeshers_FaceSide( theFace, internalEdges.back(), &theMesh,
1297 /*isForward=*/true, theIgnoreMediumNodes,
1299 wires.push_back( StdMeshers_FaceSidePtr( wire ));
1300 internalEdges.pop_back();
1305 //================================================================================
1307 * \brief Return 1st vertex of the i-the edge
1309 //================================================================================
1311 TopoDS_Vertex StdMeshers_FaceSide::FirstVertex(int i) const
1314 if ( i < NbEdges() )
1316 v = myEdge[i].Orientation() <= TopAbs_REVERSED ? // FORWARD || REVERSED
1317 TopExp::FirstVertex( myEdge[i], 1 ) :
1318 TopoDS::Vertex( TopoDS_Iterator( myEdge[i] ).Value() );
1323 //================================================================================
1325 * \brief Return last vertex of the i-the edge
1327 //================================================================================
1329 TopoDS_Vertex StdMeshers_FaceSide::LastVertex(int i) const
1332 if ( i < NbEdges() )
1334 const TopoDS_Edge& edge = i<0 ? myEdge[ NbEdges() + i ] : myEdge[i];
1335 if ( edge.Orientation() <= TopAbs_REVERSED ) // FORWARD || REVERSED
1336 v = TopExp::LastVertex( edge, 1 );
1338 for ( TopoDS_Iterator vIt( edge ); vIt.More(); vIt.Next() )
1339 v = TopoDS::Vertex( vIt.Value() );
1344 //================================================================================
1346 * \brief Return \c true if the chain of EDGEs is closed
1348 //================================================================================
1350 bool StdMeshers_FaceSide::IsClosed() const
1352 return myEdge.empty() ? false : FirstVertex().IsSame( LastVertex() );