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_MesherHelper* theFaceHelper,
75 SMESH_ProxyMesh::Ptr theProxyMesh)
77 std::list<TopoDS_Edge> edges(1,theEdge);
78 StdMeshers_FaceSide tmp( theFace, edges, theMesh, theIsForward,
79 theIgnoreMediumNodes, theFaceHelper, theProxyMesh );
85 //================================================================================
87 * \brief Constructor of a side of several edges
89 //================================================================================
91 StdMeshers_FaceSide::StdMeshers_FaceSide(const TopoDS_Face& theFace,
92 const std::list<TopoDS_Edge>& theEdges,
94 const bool theIsForward,
95 const bool theIgnoreMediumNodes,
96 SMESH_MesherHelper* theFaceHelper,
97 SMESH_ProxyMesh::Ptr theProxyMesh)
99 int nbEdges = theEdges.size();
100 myEdge.resize ( nbEdges );
101 myEdgeID.resize ( nbEdges );
102 myC2d.resize ( nbEdges );
103 myC3dAdaptor.resize( nbEdges );
104 myFirst.resize ( nbEdges );
105 myLast.resize ( nbEdges );
106 myNormPar.resize ( nbEdges );
107 myEdgeLength.resize( nbEdges );
108 myIsUniform.resize ( nbEdges, true );
111 myNbPonits = myNbSegments = 0;
112 myProxyMesh = theProxyMesh;
113 myMissingVertexNodes = false;
114 myIgnoreMediumNodes = theIgnoreMediumNodes;
115 myDefaultPnt2d = gp_Pnt2d( 1e+100, 1e+100 );
117 if ( !myProxyMesh ) myProxyMesh.reset( new SMESH_ProxyMesh( *theMesh ));
118 if ( theFaceHelper && theFaceHelper->GetSubShape() == myFace )
120 myHelper = new SMESH_MesherHelper( * myProxyMesh->GetMesh() );
121 myHelper->CopySubShapeInfo( *theFaceHelper );
123 if ( nbEdges == 0 ) return;
125 SMESHDS_Mesh* meshDS = myProxyMesh->GetMeshDS();
128 std::list<TopoDS_Edge>::const_iterator edge = theEdges.begin();
129 for ( int index = 0; edge != theEdges.end(); ++index, ++edge )
131 int i = theIsForward ? index : nbEdges-index-1;
132 myEdgeLength[i] = SMESH_Algo::EdgeLength( *edge );
133 if ( myEdgeLength[i] < DBL_MIN ) nbDegen++;
134 myLength += myEdgeLength[i];
136 myEdgeID[i] = meshDS->ShapeToIndex( *edge );
137 if ( !theIsForward ) myEdge[i].Reverse();
139 if ( theFace.IsNull() )
140 BRep_Tool::Range( *edge, myFirst[i], myLast[i] );
142 myC2d[i] = BRep_Tool::CurveOnSurface( *edge, theFace, myFirst[i], myLast[i] );
143 if ( myEdge[i].Orientation() == TopAbs_REVERSED )
144 std::swap( myFirst[i], myLast[i] );
146 // check if the edge has a non-uniform parametrization (issue 0020705)
147 if ( !myC2d[i].IsNull() )
149 if ( myEdgeLength[i] > DBL_MIN )
151 Geom2dAdaptor_Curve A2dC( myC2d[i],
152 std::min( myFirst[i], myLast[i] ),
153 std::max( myFirst[i], myLast[i] ));
154 double p2 = myFirst[i]+(myLast[i]-myFirst[i])/2.;
155 double p4 = myFirst[i]+(myLast[i]-myFirst[i])/4.;
156 double d2 = GCPnts_AbscissaPoint::Length( A2dC, myFirst[i], p2 );
157 double d4 = GCPnts_AbscissaPoint::Length( A2dC, myFirst[i], p4 );
158 myIsUniform[i] = !( fabs(2*d2/myEdgeLength[i]-1.0) > 0.01 || fabs(2*d4/d2-1.0) > 0.01 );
159 Handle(Geom_Curve) C3d = BRep_Tool::Curve(myEdge[i],d2,d4);
160 myC3dAdaptor[i].Load( C3d, d2,d4 );
164 const TopoDS_Vertex& V = SMESH_MesherHelper::IthVertex( 0, *edge );
165 Handle(Geom_Curve) C3d = new Geom_Line( BRep_Tool::Pnt( V ), gp::DX() );
166 myC3dAdaptor[i].Load( C3d, 0, 0.5 * BRep_Tool::Tolerance( V ));
169 else if ( myEdgeLength[i] > DBL_MIN )
171 Handle(Geom_Curve) C3d = BRep_Tool::Curve(myEdge[i],myFirst[i], myLast[i] );
172 myC3dAdaptor[i].Load( C3d, myFirst[i], myLast[i] );
173 if ( myEdge[i].Orientation() == TopAbs_REVERSED )
174 std::swap( myFirst[i], myLast[i] );
177 // reverse a proxy sub-mesh
179 reverseProxySubmesh( myEdge[i] );
183 // count nodes and segments
184 NbPoints( /*update=*/true );
186 if ( nbEdges > 1 && myLength > DBL_MIN ) {
187 const double degenNormLen = 1.e-5;
188 double totLength = myLength;
190 totLength += myLength * degenNormLen * nbDegen;
191 double prevNormPar = 0;
192 for ( int i = 0; i < nbEdges; ++i ) {
193 if ( myEdgeLength[ i ] < DBL_MIN )
194 myEdgeLength[ i ] = myLength * degenNormLen;
195 myNormPar[ i ] = prevNormPar + myEdgeLength[i]/totLength;
196 prevNormPar = myNormPar[ i ];
199 myNormPar[nbEdges-1] = 1.;
203 //================================================================================
205 * \brief Constructor of a side for vertex using data from other FaceSide
207 //================================================================================
209 StdMeshers_FaceSide::StdMeshers_FaceSide(const StdMeshers_FaceSide* theSide,
210 const SMDS_MeshNode* theNode,
211 const gp_Pnt2d* thePnt2d1,
212 const gp_Pnt2d* thePnt2d2,
213 const Handle(Geom2d_Curve)& theC2d,
214 const double theUFirst,
215 const double theULast)
218 myEdgeID.resize ( 1, 0 );
219 myC2d.push_back ( theC2d );
220 myC3dAdaptor.resize ( 1 );
221 myFirst.push_back ( theUFirst );
222 myLast.push_back ( theULast );
223 myNormPar.push_back ( 1. );
224 myIsUniform.push_back( true );
227 myProxyMesh = theSide->myProxyMesh;
228 myDefaultPnt2d = *thePnt2d1;
229 myPoints = theSide->GetUVPtStruct();
230 myNbPonits = myPoints.size();
231 myNbSegments = theSide->myNbSegments;
233 for ( size_t i = 0; i < myPoints.size(); ++i )
235 double r = i / ( myPoints.size() - 1. );
236 myPoints[i].u = (1-r) * thePnt2d1->X() + r * thePnt2d2->X();
237 myPoints[i].v = (1-r) * thePnt2d1->Y() + r * thePnt2d2->Y();
238 myPoints[i].node = theNode;
241 for ( size_t i = 0; i < myPoints.size(); ++i )
243 myPoints[i].u = thePnt2d1->X();
244 myPoints[i].v = thePnt2d1->Y();
245 myPoints[i].node = theNode;
249 //================================================================================
251 * Create a side from an UVPtStructVec
253 //================================================================================
255 StdMeshers_FaceSide::StdMeshers_FaceSide(UVPtStructVec& theSideNodes,
256 const TopoDS_Face& theFace,
257 const TopoDS_Edge& theEdge,
260 myEdge.resize( 1, theEdge );
261 myEdgeID.resize( 1, -1 );
263 myC3dAdaptor.resize( 1 );
264 myFirst.resize( 1, 0. );
265 myLast.resize( 1, 1. );
266 myNormPar.resize( 1, 1. );
267 myIsUniform.resize( 1, 1 );
268 myMissingVertexNodes = myIgnoreMediumNodes = false;
269 myDefaultPnt2d.SetCoord( 1e100, 1e100 );
270 if ( theMesh ) myProxyMesh.reset( new SMESH_ProxyMesh( *theMesh ));
271 if ( !theEdge.IsNull() )
273 if ( theMesh ) myEdgeID[0] = theMesh->GetMeshDS()->ShapeToIndex( theEdge );
274 if ( theFace.IsNull() )
275 BRep_Tool::Range( theEdge, myFirst[0], myLast[0] );
277 myC2d[0] = BRep_Tool::CurveOnSurface( theEdge, theFace, myFirst[0], myLast[0] );
278 if ( theEdge.Orientation() == TopAbs_REVERSED )
279 std::swap( myFirst[0], myLast[0] );
284 myPoints = theSideNodes;
285 myNbPonits = myPoints.size();
286 myNbSegments = myNbPonits + 1;
289 if ( !myPoints.empty() )
291 myPoints[0].normParam = 0;
292 if ( myPoints[0].node &&
293 myPoints.back().node &&
294 myPoints[ myNbPonits/2 ].node )
296 gp_Pnt pPrev = SMESH_TNodeXYZ( myPoints[0].node );
297 for ( size_t i = 1; i < myPoints.size(); ++i )
299 gp_Pnt p = SMESH_TNodeXYZ( myPoints[i].node );
300 myLength += p.Distance( pPrev );
301 myPoints[i].normParam = myLength;
305 else if ( !theFace.IsNull() )
308 Handle(Geom_Surface) surf = BRep_Tool::Surface( theFace, loc );
309 gp_Pnt pPrev = surf->Value( myPoints[0].u, myPoints[0].v );
310 for ( size_t i = 1; i < myPoints.size(); ++i )
312 gp_Pnt p = surf->Value( myPoints[i].u, myPoints[i].v );
313 myLength += p.Distance( pPrev );
314 myPoints[i].normParam = myLength;
320 gp_Pnt2d pPrev = myPoints[0].UV();
321 for ( size_t i = 1; i < myPoints.size(); ++i )
323 gp_Pnt2d p = myPoints[i].UV();
324 myLength += p.Distance( pPrev );
325 myPoints[i].normParam = myLength;
329 if ( myLength > std::numeric_limits<double>::min() )
330 for ( size_t i = 1; i < myPoints.size(); ++i )
331 myPoints[i].normParam /= myLength;
333 myEdgeLength.resize( 1, myLength );
336 //================================================================================
340 //================================================================================
342 StdMeshers_FaceSide::~StdMeshers_FaceSide()
344 delete myHelper; myHelper = NULL;
347 //================================================================================
349 * Return info on nodes on the side
351 //================================================================================
353 const std::vector<UVPtStruct>& StdMeshers_FaceSide::GetUVPtStruct(bool isXConst,
354 double constValue) const
356 if ( myPoints.empty() )
358 if ( NbEdges() == 0 ) return myPoints;
360 StdMeshers_FaceSide* me = const_cast< StdMeshers_FaceSide* >( this );
364 SMESH_MesherHelper eHelper( *myProxyMesh->GetMesh() );
365 SMESH_MesherHelper& fHelper = *FaceHelper();
367 // sort nodes of all edges by putting them into a map
369 map< double, const SMDS_MeshNode*> u2node;
370 vector< pair< double, const SMDS_MeshNode*> > u2nodeVec;
371 vector<const SMDS_MeshNode*> nodes;
372 set<const SMDS_MeshNode*> vertexNodes;
373 vector< const SMESH_ProxyMesh::SubMesh* > proxySubMesh( myEdge.size() );
374 int nbProxyNodes = 0;
377 for ( iE = 0; iE < myEdge.size(); ++iE )
379 proxySubMesh[iE] = myProxyMesh->GetProxySubMesh( myEdge[iE] );
380 if ( proxySubMesh[iE] )
382 if ( proxySubMesh[iE]->GetUVPtStructVec().empty() ) {
383 proxySubMesh[iE] = 0;
386 nbProxyNodes += proxySubMesh[iE]->GetUVPtStructVec().size() - 1;
387 if ( iE+1 == myEdge.size() )
393 // Add 1st vertex node of a current edge
394 const SMDS_MeshNode* node = VertexNode( iE );
395 const double prevNormPar = ( iE == 0 ? 0 : myNormPar[ iE-1 ]); // normalized param
396 if ( node ) // nodes on internal vertices may be missing
398 if ( vertexNodes.insert( node ).second ||
399 fHelper.IsRealSeam ( node->getshapeId() ) ||
400 fHelper.IsDegenShape( node->getshapeId() ))
401 u2node.insert( u2node.end(), make_pair( prevNormPar, node ));
405 for ( ++iE; iE < myEdge.size(); ++iE )
406 if (( node = VertexNode( iE ))) {
407 u2node.insert( make_pair( prevNormPar, node ));
414 vertexNodes.insert( node );
417 // Add internal nodes
419 if ( !GetEdgeNodes( iE, nodes, /*v0=*/false, /*v1=*/false ))
421 if ( !nodes.empty() )
424 double paramSize = myLast[iE] - myFirst[iE];
425 double r = myNormPar[iE] - prevNormPar;
426 eHelper.SetSubShape( myEdge[iE] );
427 eHelper.ToFixNodeParameters( true );
428 if ( !myIsUniform[iE] )
429 for ( size_t i = 0; i < nodes.size(); ++i )
431 double u = eHelper.GetNodeU( myEdge[iE], nodes[i], 0, ¶mOK );
432 double aLenU = GCPnts_AbscissaPoint::Length( me->myC3dAdaptor[iE], myFirst[iE], u );
433 if ( myEdgeLength[iE] < aLenU ) // nonregression test "3D_mesh_NETGEN/G6"
438 double normPar = prevNormPar + r * aLenU / myEdgeLength[iE];
439 u2nodeVec.push_back( make_pair( normPar, nodes[i] ));
441 if ( u2nodeVec.empty() )
442 for ( size_t i = 0; i < nodes.size(); ++i )
444 double u = eHelper.GetNodeU( myEdge[iE], nodes[i], 0, ¶mOK );
445 // paramSize is signed so orientation is taken into account
446 double normPar = prevNormPar + r * ( u - myFirst[iE] ) / paramSize;
447 u2nodeVec.push_back( make_pair( normPar, nodes[i] ));
449 for ( size_t j = 0; j < u2nodeVec.size(); ++j )
450 u2node.insert( u2node.end(), u2nodeVec[j] );
452 } // loop on myEdge's
454 // Add 2nd VERTEX node for a last EDGE
455 if ( !proxySubMesh.back() )
457 if ( u2node.empty() ) return myPoints;
459 const SMDS_MeshNode* node;
460 if ( IsClosed() && !proxySubMesh[0] )
461 node = u2node.begin()->second;
464 node = VertexNode( iE );
465 if ( myProxyMesh->GetMesh()->HasModificationsToDiscard() )
466 while ( !node && iE > 1 ) // check intermediate VERTEXes
467 node = VertexNode( --iE );
471 if ( u2node.rbegin()->second == node &&
472 !fHelper.IsRealSeam ( node->getshapeId() ) &&
473 !fHelper.IsDegenShape( node->getshapeId() ))
474 u2node.erase( --u2node.end() );
476 u2node.insert( u2node.end(), make_pair( 1., node ));
480 if ((int) u2node.size() + nbProxyNodes != myNbPonits &&
481 (int) u2node.size() + nbProxyNodes != NbPoints( /*update=*/true ))
485 if (( u2node.size() > 0 ) &&
486 ( u2node.begin()->first < 0 || u2node.rbegin()->first > 1 ))
491 // fill array of UVPtStruct
493 UVPtStructVec& points = me->myPoints;
494 points.resize( myNbPonits );
497 double prevNormPar = 0, paramSize = myNormPar[ 0 ];
498 map< double, const SMDS_MeshNode*>::iterator u_node = u2node.begin();
499 for ( size_t iE = 0; iE < myEdge.size(); ++iE )
501 if ( proxySubMesh[ iE ] ) // copy data from a proxy sub-mesh
503 const UVPtStructVec& edgeUVPtStruct = proxySubMesh[iE]->GetUVPtStructVec();
504 UVPtStruct* pointsPtr = & points[iPt];
505 std::copy( edgeUVPtStruct.begin(), edgeUVPtStruct.end(), pointsPtr );
507 double du1 = edgeUVPtStruct.back().param - edgeUVPtStruct[0].param;
508 double du2 = myLast[iE] - myFirst[iE];
511 std::reverse( pointsPtr, pointsPtr + edgeUVPtStruct.size());
512 for ( size_t i = 0; i < edgeUVPtStruct.size(); ++i )
513 pointsPtr[i].normParam = 1. - pointsPtr[i].normParam;
515 // update normalized params
516 if ( myEdge.size() > 1 ) {
517 for ( size_t i = 0; i < edgeUVPtStruct.size(); ++i )
519 UVPtStruct & uvPt = pointsPtr[i];
520 uvPt.normParam = prevNormPar + uvPt.normParam * paramSize;
521 uvPt.x = uvPt.y = uvPt.normParam;
523 iPt += edgeUVPtStruct.size() - 1; // to point to the 1st VERTEX of the next EDGE
525 // update UV on a seam EDGE
526 if ( fHelper.IsRealSeam( myEdgeID[ iE ]))
528 // check if points lye on the EDGE
529 const UVPtStruct& pm = edgeUVPtStruct[ edgeUVPtStruct.size()/2 ];
530 gp_Pnt pNode = SMESH_TNodeXYZ( pm.node );
531 gp_Pnt pCurv = myC3dAdaptor[ iE ].Value( pm.param );
532 double tol = BRep_Tool::Tolerance( myEdge[ iE ]) * 10;
533 bool isPointOnEdge = ( pNode.SquareDistance( pCurv ) < tol * tol );
535 for ( size_t i = 0; i < edgeUVPtStruct.size(); ++i )
536 pointsPtr[i].SetUV( myC2d[ iE ]->Value( pointsPtr[i].param ).XY() );
541 for ( ; u_node != u2node.end(); ++u_node, ++iPt )
543 if ( myNormPar[ iE ]-eps < u_node->first )
544 break; // u_node is at VERTEX of the next EDGE
546 UVPtStruct & uvPt = points[iPt];
547 uvPt.node = u_node->second;
548 // -- normParam, x, y --------------------------------
549 uvPt.normParam = u_node->first;
550 uvPt.x = uvPt.y = uvPt.normParam;
551 // -- U ----------------------------------------------
552 SMDS_EdgePositionPtr epos = uvPt.node->GetPosition();
553 if ( epos && uvPt.node->getshapeId() == myEdgeID[iE] ) {
554 uvPt.param = epos->GetUParameter();
557 double r = ( uvPt.normParam - prevNormPar )/ paramSize;
558 uvPt.param = ( r > 0.5 ? myLast[iE] : myFirst[iE] );
560 // -- UV ---------------------------------------------
561 if ( !myC2d[ iE ].IsNull() ) {
562 gp_Pnt2d p = myC2d[ iE ]->Value( uvPt.param );
567 uvPt.u = uvPt.v = 1e+100;
571 // prepare for the next EDGE
572 if ( iE+1 < myEdge.size() )
574 prevNormPar = myNormPar[ iE ];
575 paramSize = myNormPar[ iE+1 ] - prevNormPar;
577 } // loop on myEdge's
581 for ( iPt = 0; iPt < (int)points.size(); ++iPt ) points[ iPt ].x = constValue;
583 for ( iPt = 0; iPt < (int)points.size(); ++iPt ) points[ iPt ].y = constValue;
585 } // if ( myPoints.empty())
590 //================================================================================
592 * \brief Falsificate info on nodes
593 * \param nbSeg - nb of segments on the side
594 * \retval UVPtStruct* - array of data structures
596 //================================================================================
598 const vector<UVPtStruct>& StdMeshers_FaceSide::SimulateUVPtStruct(int nbSeg,
600 double constValue) const
602 if ( myFalsePoints.empty() )
604 if ( NbEdges() == 0 ) return myFalsePoints;
606 vector<uvPtStruct>* points = const_cast<vector<uvPtStruct>*>( &myFalsePoints );
607 points->resize( nbSeg+1 );
610 double prevNormPar = 0, paramSize = myNormPar[ EdgeIndex ];
612 for ( size_t i = 0 ; i < myFalsePoints.size(); ++i )
614 double normPar = double(i) / double(nbSeg);
615 UVPtStruct & uvPt = (*points)[i];
617 uvPt.x = uvPt.y = uvPt.param = uvPt.normParam = normPar;
618 if ( isXConst ) uvPt.x = constValue;
619 else uvPt.y = constValue;
620 if ( myNormPar[ EdgeIndex ] < normPar )
622 prevNormPar = myNormPar[ EdgeIndex ];
624 paramSize = myNormPar[ EdgeIndex ] - prevNormPar;
626 double r = ( normPar - prevNormPar )/ paramSize;
627 uvPt.param = myFirst[EdgeIndex] * ( 1 - r ) + myLast[EdgeIndex] * r;
628 if ( !myC2d[ EdgeIndex ].IsNull() )
629 p = myC2d[ EdgeIndex ]->Value( uvPt.param );
631 p = Value2d( normPar );
636 return myFalsePoints;
639 //=======================================================================
640 //function : GetOrderedNodes
641 //purpose : Return nodes in the order they encounter while walking along the side
642 //=======================================================================
644 std::vector<const SMDS_MeshNode*> StdMeshers_FaceSide::GetOrderedNodes(int theEdgeInd) const
646 vector<const SMDS_MeshNode*> resultNodes;
647 if ( myPoints.empty() || ( theEdgeInd >= 0 && NbEdges() > 0 ))
649 if ( NbEdges() == 0 ) return resultNodes;
651 //SMESHDS_Mesh* meshDS = myProxyMesh->GetMeshDS();
652 SMESH_MesherHelper eHelper( *myProxyMesh->GetMesh() );
653 SMESH_MesherHelper& fHelper = * FaceHelper();
656 // Sort nodes of all edges putting them into a map
658 map< double, const SMDS_MeshNode*> u2node;
659 vector<const SMDS_MeshNode*> nodes;
660 set<const SMDS_MeshNode*> vertexNodes;
661 int iE = 0, iEnd = myEdge.size();
662 if ( theEdgeInd >= 0 )
664 iE = theEdgeInd % NbEdges();
667 for ( iE = 0; iE < iEnd; ++iE )
669 double prevNormPar = ( iE == 0 ? 0 : myNormPar[ iE-1 ]); // normalized param
671 const SMESH_ProxyMesh::SubMesh* proxySM = myProxyMesh->GetProxySubMesh( myEdge[iE] );
674 const UVPtStructVec& points = proxySM->GetUVPtStructVec();
675 for ( size_t i = 0; i < points.size(); ++i )
676 u2node.insert( make_pair( prevNormPar + points[i].normParam, points[i].node ));
680 // Add 1st vertex node of a current EDGE
681 const SMDS_MeshNode* node = VertexNode( iE );
682 if ( node ) { // nodes on internal vertices may be missing
683 if ( vertexNodes.insert( node ).second ||
684 fHelper.IsRealSeam ( node->getshapeId() ) ||
685 fHelper.IsDegenShape( node->getshapeId() ))
686 u2node.insert( make_pair( prevNormPar, node ));
690 if ( nodes.empty() ) {
691 for ( ++iE; iE < iEnd; ++iE )
692 if (( node = VertexNode( iE ))) {
693 u2node.insert( make_pair( prevNormPar, node ));
700 vertexNodes.insert( node );
703 // Add internal nodes
705 if ( !GetEdgeNodes( iE, nodes, /*v0=*/false, /*v1=*/false ))
707 if ( !nodes.empty() )
709 double paramSize = myLast[iE] - myFirst[iE];
710 double r = myNormPar[iE] - prevNormPar;
711 eHelper.SetSubShape( myEdge[iE] );
712 eHelper.ToFixNodeParameters( true );
713 for ( size_t i = 0; i < nodes.size(); ++i )
715 double u = eHelper.GetNodeU( myEdge[iE], nodes[i], 0, ¶mOK );
716 // paramSize is signed so orientation is taken into account
717 double normPar = prevNormPar + r * ( u - myFirst[iE] ) / paramSize;
718 u2node.insert( u2node.end(), make_pair( normPar, nodes[i] ));
724 if ( u2node.empty() ) return resultNodes;
726 // Add 2nd vertex node for a last EDGE
728 const SMDS_MeshNode* node;
729 if ( IsClosed() && theEdgeInd < 0 )
730 node = u2node.begin()->second;
733 node = VertexNode( iE );
734 while ( !node && iE > 0 )
735 node = VertexNode( --iE );
739 if ( u2node.rbegin()->second == node &&
740 !fHelper.IsRealSeam ( node->getshapeId() ) &&
741 !fHelper.IsDegenShape( node->getshapeId() ))
742 u2node.erase( --u2node.end() );
744 u2node.insert( u2node.end(), make_pair( 1., node ));
747 // Fill the result vector
749 if ( theEdgeInd < 0 &&
750 (int) u2node.size() != myNbPonits &&
751 (int) u2node.size() != NbPoints( /*update=*/true ))
755 resultNodes.reserve( u2node.size() );
756 map< double, const SMDS_MeshNode*>::iterator u2n = u2node.begin();
757 for ( ; u2n != u2node.end(); ++u2n )
758 resultNodes.push_back( u2n->second );
762 resultNodes.resize( myPoints.size() );
763 for ( size_t i = 0; i < myPoints.size(); ++i )
764 resultNodes[i] = myPoints[i].node;
770 //================================================================================
772 * \brief Return (unsorted) nodes of the i-th EDGE.
773 * Nodes moved to other geometry by MergeNodes() are also returned.
774 * \retval bool - is OK
776 //================================================================================
778 bool StdMeshers_FaceSide::GetEdgeNodes(size_t i,
779 vector<const SMDS_MeshNode*>& nodes,
780 bool inlude1stVertex,
781 bool inludeLastVertex) const
783 if ( i >= myEdge.size() )
786 SMESH_Mesh* mesh = myProxyMesh->GetMesh();
787 SMESHDS_Mesh* meshDS = mesh->GetMeshDS();
788 SMESHDS_SubMesh* sm = meshDS->MeshElements( myEdge[i] );
790 if ( inlude1stVertex )
792 if ( const SMDS_MeshNode* n0 = VertexNode( i ))
793 nodes.push_back( n0 );
796 if ( sm && ( sm->NbElements() > 0 || sm->NbNodes() > 0 ))
798 if ( mesh->HasModificationsToDiscard() ) // check nb of nodes on the EDGE sub-mesh
800 int iQuad = sm->NbElements() ? sm->GetElements()->next()->IsQuadratic() : 0;
801 int nbExpect = sm->NbElements() - 1 + iQuad * sm->NbElements();
802 if ( nbExpect != sm->NbNodes() ) // some nodes are moved from the EDGE by MergeNodes()
804 // add nodes of all segments
805 typedef set< const SMDS_MeshNode* > TNodeSet;
806 TNodeSet sharedNodes;
807 SMDS_ElemIteratorPtr segIt = sm->GetElements();
808 while ( segIt->more() )
810 const SMDS_MeshElement* seg = segIt->next();
811 if ( seg->GetType() != SMDSAbs_Edge )
813 for ( int i = 0; i < 3-myIgnoreMediumNodes; ++i )
815 const SMDS_MeshNode* n = seg->GetNode( i );
816 if ( i == 2 ) // medium node
818 nodes.push_back( n );
822 pair<TNodeSet::iterator, bool> it2new = sharedNodes.insert( n );
823 if ( !it2new.second ) // n encounters twice == it's on EDGE, not on VERTEX
825 nodes.push_back( n );
826 sharedNodes.erase( it2new.first );
833 if ( nodes.size() < 2 ) // add nodes assigned to the EDGE
835 SMDS_NodeIteratorPtr nItr = sm->GetNodes();
836 while ( nItr->more() )
838 const SMDS_MeshNode* n = nItr->next();
839 if ( myIgnoreMediumNodes && SMESH_MeshEditor::IsMedium( n, SMDSAbs_Edge ))
841 nodes.push_back( n );
844 } // if ( sm && sm->NbElements() > 0 )
846 if ( inludeLastVertex )
848 if ( const SMDS_MeshNode* n1 = VertexNode( i+1 ))
849 nodes.push_back( n1 );
854 //================================================================================
856 * \brief Return a node from the i-th VERTEX (count starts from zero)
857 * Nodes moved to other geometry by MergeNodes() are also returned.
858 * \param [in] i - the VERTEX index
859 * \param [out] isMoved - returns \c true if the found node is moved by MergeNodes()
860 * \return const SMDS_MeshNode* - the found node
862 //================================================================================
864 const SMDS_MeshNode* StdMeshers_FaceSide::VertexNode(std::size_t i, bool* isMoved) const
866 TopoDS_Vertex V = ( i >= myEdge.size() ) ? LastVertex() : FirstVertex(i);
868 const SMDS_MeshNode* n = SMESH_Algo::VertexNode( V, myProxyMesh->GetMeshDS() );
870 if ( !n && !myEdge.empty() && myProxyMesh->GetMesh()->HasModificationsToDiscard() )
872 size_t iE = ( i < myEdge.size() ) ? i : myEdge.size()-1;
873 SMESHDS_SubMesh* sm = myProxyMesh->GetMeshDS()->MeshElements( myEdgeID[ iE ]);
875 n = SMESH_Algo::VertexNode( V, sm, myProxyMesh->GetMesh(), /*checkV=*/false );
878 (( i > 0 && (int) i < NbEdges() ) || IsClosed() ))
880 iE = SMESH_MesherHelper::WrapIndex( int(i)-1, NbEdges() );
881 sm = myProxyMesh->GetMeshDS()->MeshElements( myEdgeID[ iE ]);
882 n = SMESH_Algo::VertexNode( V, sm, myProxyMesh->GetMesh(), /*checkV=*/false );
885 if ( n && n->GetPosition()->GetDim() == 1 ) // check that n does not lie on an EDGE of myFace
887 TopoDS_Shape S = SMESH_MesherHelper::GetSubShapeByNode( n, myProxyMesh->GetMeshDS() );
888 if ( SMESH_MesherHelper::IsSubShape( S, myFace ))
889 n = 0; // VERTEX ignored by e.g. Composite Wire Discretization algo
897 //================================================================================
899 * \brief reverse order of vector elements
900 * \param vec - vector to reverse
902 //================================================================================
904 template <typename T > void reverse(vector<T> & vec)
906 std::reverse( vec.begin(), vec.end() );
909 //================================================================================
911 * \brief Change orientation of side geometry
913 //================================================================================
915 void StdMeshers_FaceSide::Reverse()
917 int nbEdges = myEdge.size();
918 for ( int i = nbEdges-1; i >= 0; --i ) {
919 std::swap( myFirst[i], myLast[i] );
920 if ( !myEdge[i].IsNull() )
922 if ( i > 0 ) // at the first loop 1. is overwritten
923 myNormPar[i] = 1 - myNormPar[i-1];
929 //reverse( myC3dAdaptor );
932 reverse( myNormPar );
933 reverse( myEdgeLength );
934 reverse( myIsUniform );
938 myNormPar[nbEdges-1]=1.;
939 if ( !myEdge[0].IsNull() )
941 for ( size_t i = 0; i < myEdge.size(); ++i )
942 reverseProxySubmesh( myEdge[i] );
944 myFalsePoints.clear();
948 for ( size_t i = 0; i < myPoints.size(); ++i )
950 UVPtStruct & uvPt = myPoints[i];
951 uvPt.normParam = 1 - uvPt.normParam;
957 for ( size_t i = 0; i < myFalsePoints.size(); ++i )
959 UVPtStruct & uvPt = myFalsePoints[i];
960 uvPt.normParam = 1 - uvPt.normParam;
964 reverse( myFalsePoints );
967 for ( size_t i = 0; i < myEdge.size(); ++i )
969 if ( myEdge[i].IsNull() ) continue; // for a side on points only
971 Handle(Geom_Curve) C3d = BRep_Tool::Curve(myEdge[i],fp,lp);
973 myC3dAdaptor[i].Load( C3d, fp,lp );
977 //=======================================================================
978 //function : SetIgnoreMediumNodes
979 //purpose : Make ignore medium nodes
980 //=======================================================================
982 void StdMeshers_FaceSide::SetIgnoreMediumNodes(bool toIgnore)
984 if ( myIgnoreMediumNodes != toIgnore )
986 myIgnoreMediumNodes = toIgnore;
988 if ( !myPoints.empty() )
990 UVPtStructVec newPoints;
991 newPoints.reserve( myPoints.size()/2 + 1 );
992 for ( size_t i = 0; i < myPoints.size(); i += 2 )
993 newPoints.push_back( myPoints[i] );
995 myPoints.swap( newPoints );
999 NbPoints( /*update=*/true );
1004 //=======================================================================
1005 //function : NbPoints
1006 //purpose : Return nb nodes on edges and vertices (+1 to be == GetUVPtStruct().size() )
1007 // Call it with update == true if mesh of this side can be recomputed
1008 // since creation of this side
1009 //=======================================================================
1011 int StdMeshers_FaceSide::NbPoints(const bool update) const
1013 if ( !myPoints.empty() )
1014 return myPoints.size();
1016 // if ( !myFalsePoints.empty() )
1017 // return myFalsePoints.size();
1019 if ( update && myEdge.size() > 0 )
1021 StdMeshers_FaceSide* me = (StdMeshers_FaceSide*) this;
1023 me->myNbSegments = 0;
1024 me->myMissingVertexNodes = false;
1026 vector<const SMDS_MeshNode*> nodes;
1027 for ( int i = 0; i < NbEdges(); ++i )
1029 if ( const SMESHDS_SubMesh* sm = myProxyMesh->GetSubMesh( Edge(i) ))
1031 if ( sm->NbNodes() == sm->NbElements()-1 || sm->NbElements() == 0 )
1033 me->myNbPonits += sm->NbNodes();
1034 if ( myIgnoreMediumNodes && sm->IsQuadratic() )
1035 me->myNbPonits -= sm->NbElements();
1037 else // nodes can be moved to other shapes by MergeNodes()
1040 GetEdgeNodes( i, nodes, /*v1=*/false, /*v2=*/false );
1041 me->myNbPonits += nodes.size();
1043 me->myNbSegments += sm->NbElements();
1047 SMESH_MesherHelper* helper = FaceHelper();
1049 std::set< const SMDS_MeshNode* > vNodes;
1050 const int nbV = NbEdges() + !IsClosed();
1051 for ( int i = 0; i < nbV; ++i )
1052 if ( const SMDS_MeshNode* n = VertexNode( i ))
1054 if ( !vNodes.insert( n ).second &&
1055 ( helper->IsRealSeam ( n->getshapeId() ) ||
1056 helper->IsDegenShape( n->getshapeId() )))
1061 me->myMissingVertexNodes = true;
1063 me->myNbPonits += vNodes.size();
1066 me->myNbPonits++; // closing node is repeated
1071 //=======================================================================
1072 //function : NbSegments
1073 //purpose : Return nb edges
1074 // Call it with update == true if mesh of this side can be recomputed
1075 // since creation of this side
1076 //=======================================================================
1078 int StdMeshers_FaceSide::NbSegments(const bool update) const
1080 return NbPoints( update ), myNbSegments;
1083 //================================================================================
1085 * \brief Reverse UVPtStructVec if a proxy sub-mesh of E
1087 //================================================================================
1089 void StdMeshers_FaceSide::reverseProxySubmesh( const TopoDS_Edge& E )
1091 if ( !myProxyMesh ) return;
1092 if ( const SMESH_ProxyMesh::SubMesh* sm = myProxyMesh->GetProxySubMesh( E ))
1094 UVPtStructVec& edgeUVPtStruct = (UVPtStructVec& ) sm->GetUVPtStructVec();
1095 for ( size_t i = 0; i < edgeUVPtStruct.size(); ++i )
1097 UVPtStruct & uvPt = edgeUVPtStruct[i];
1098 uvPt.normParam = 1 - uvPt.normParam;
1099 uvPt.x = 1 - uvPt.x;
1100 uvPt.y = 1 - uvPt.y;
1102 reverse( edgeUVPtStruct );
1106 //================================================================================
1108 * \brief Show side features
1110 //================================================================================
1112 void StdMeshers_FaceSide::dump(const char* msg) const
1115 if (msg) MESSAGE ( std::endl << msg );
1116 MESSAGE_BEGIN ("NB EDGES: "<< myEdge.size() );
1117 MESSAGE_ADD ( "nbPoints: "<<myNbPonits<<" vecSize: " << myPoints.size()<<" "<<myFalsePoints.size() );
1118 for ( size_t i = 0; i < myEdge.size(); ++i )
1120 MESSAGE_ADD ( "\t"<<i+1 );
1121 MESSAGE_ADD ( "\tEDGE: " );
1122 if (myEdge[i].IsNull()) {
1123 MESSAGE_ADD ( "NULL" );
1126 TopAbs::Print(myEdge[i].Orientation(),cout)<<" "<<myEdge[i].TShape().operator->()<<endl;
1127 MESSAGE_ADD ( "\tV1: " << TopExp::FirstVertex( myEdge[i], 1).TShape().operator->()
1128 << " V2: " << TopExp::LastVertex( myEdge[i], 1).TShape().operator->() );
1130 MESSAGE_ADD ( "\tC2d: ");
1132 if (myC2d[i].IsNull()) {
1133 MESSAGE_ADD ( "NULL" );
1136 MESSAGE_ADD ( myC2d[i].operator->() );
1139 MESSAGE_ADD ( "\tF: "<<myFirst[i]<< " L: "<< myLast[i] );
1140 MESSAGE_END ( "\tnormPar: "<<myNormPar[i]<<endl );
1145 //================================================================================
1147 * \brief Creates a Adaptor2d_Curve2d to be used in SMESH_Block
1148 * \retval Adaptor2d_Curve2d* -
1150 //================================================================================
1152 struct Adaptor2dCurve2d : public Adaptor2d_Curve2d
1154 const StdMeshers_FaceSide* mySide;
1155 Adaptor2dCurve2d(const StdMeshers_FaceSide* faceSide):mySide(faceSide) {}
1156 gp_Pnt2d Value(const Standard_Real U) const { return mySide->Value2d( U ); }
1157 Standard_Real FirstParameter() const { return 0; }
1158 Standard_Real LastParameter() const { return 1; }
1161 Adaptor2d_Curve2d* StdMeshers_FaceSide::GetCurve2d() const
1163 return new Adaptor2dCurve2d( this );
1166 //================================================================================
1168 * \brief Creates a fully functional Adaptor_Curve
1170 //================================================================================
1172 BRepAdaptor_CompCurve* StdMeshers_FaceSide::GetCurve3d() const
1174 if ( myEdge.empty() )
1178 BRep_Builder aBuilder;
1179 aBuilder.MakeWire(aWire);
1180 for ( size_t i = 0; i < myEdge.size(); ++i )
1181 aBuilder.Add( aWire, myEdge[i] );
1183 if ( myEdge.size() == 2 && IsClosed() )
1184 aWire.Closed(true); // issue 0021141
1186 return new BRepAdaptor_CompCurve( aWire );
1189 //================================================================================
1191 * \brief Return 2D point by normalized parameter
1192 * \param U - normalized parameter value
1193 * \retval gp_Pnt2d - point
1195 //================================================================================
1197 gp_Pnt2d StdMeshers_FaceSide::Value2d(double U) const
1199 if ( !myC2d[0].IsNull() ) {
1200 int i = EdgeIndex( U );
1201 double prevU = i ? myNormPar[ i-1 ] : 0;
1202 double r = ( U - prevU )/ ( myNormPar[ i ] - prevU );
1204 double par = myFirst[i] * ( 1 - r ) + myLast[i] * r;
1206 // check parametrization of curve
1207 if( !myIsUniform[i] )
1209 double aLen3dU = r * myEdgeLength[i] * ( myFirst[i]>myLast[i] ? -1. : 1.);
1210 GCPnts_AbscissaPoint AbPnt
1211 ( const_cast<GeomAdaptor_Curve&>( myC3dAdaptor[i]), aLen3dU, myFirst[i] );
1212 if( AbPnt.IsDone() ) {
1213 par = AbPnt.Parameter();
1216 return myC2d[ i ]->Value(par);
1219 else if ( !myPoints.empty() )
1221 int i = U * double( myPoints.size()-1 );
1222 while ( i > 0 && myPoints[ i ].normParam > U )
1224 while ( i+1 < (int)myPoints.size() && myPoints[ i+1 ].normParam < U )
1226 double r = (( U - myPoints[ i ].normParam ) /
1227 ( myPoints[ i+1 ].normParam - myPoints[ i ].normParam ));
1228 return ( myPoints[ i ].UV() * ( 1 - r ) +
1229 myPoints[ i+1 ].UV() * r );
1231 return myDefaultPnt2d;
1234 //================================================================================
1236 * \brief Return XYZ by normalized parameter
1237 * \param U - normalized parameter value
1238 * \retval gp_Pnt - point
1240 //================================================================================
1242 gp_Pnt StdMeshers_FaceSide::Value3d(double U) const
1244 int i = EdgeIndex( U );
1245 double prevU = i ? myNormPar[ i-1 ] : 0;
1246 double r = ( U - prevU )/ ( myNormPar[ i ] - prevU );
1248 double par = myFirst[i] * ( 1 - r ) + myLast[i] * r;
1250 // check parametrization of curve
1251 if( !myIsUniform[i] )
1253 double aLen3dU = r * myEdgeLength[i] * ( myFirst[i] > myLast[i] ? -1. : 1. );
1254 GCPnts_AbscissaPoint AbPnt
1255 ( const_cast<GeomAdaptor_Curve&>( myC3dAdaptor[i]), aLen3dU, myFirst[i] );
1256 if( AbPnt.IsDone() ) {
1257 par = AbPnt.Parameter();
1260 return myC3dAdaptor[ i ].Value(par);
1263 //================================================================================
1265 * \brief Return wires of a face as StdMeshers_FaceSide's
1267 //================================================================================
1269 TSideVector StdMeshers_FaceSide::GetFaceWires(const TopoDS_Face& theFace,
1270 SMESH_Mesh & theMesh,
1271 const bool theIgnoreMediumNodes,
1273 SMESH_MesherHelper* theFaceHelper,
1274 SMESH_ProxyMesh::Ptr theProxyMesh,
1275 const bool theCheckVertexNodes)
1277 SMESH_MesherHelper helper( theMesh );
1278 if ( theFaceHelper && theFaceHelper->GetSubShape() == theFace )
1279 helper.CopySubShapeInfo( *theFaceHelper );
1281 list< TopoDS_Edge > edges, internalEdges;
1282 list< int > nbEdgesInWires;
1283 int nbWires = SMESH_Block::GetOrderedEdges (theFace, edges, nbEdgesInWires);
1285 // split list of all edges into separate wires
1286 TSideVector wires( nbWires );
1287 list< int >::iterator nbE = nbEdgesInWires.begin();
1288 list< TopoDS_Edge >::iterator from = edges.begin(), to = from;
1289 for ( int iW = 0; iW < nbWires; ++iW, ++nbE )
1291 std::advance( to, *nbE );
1292 if ( *nbE == 0 ) // Issue 0020676
1296 wires.resize( nbWires );
1299 list< TopoDS_Edge > wireEdges( from, to );
1300 // assure that there is a node on the first vertex
1301 // as StdMeshers_FaceSide::GetUVPtStruct() requires
1302 if ( wireEdges.front().Orientation() != TopAbs_INTERNAL ) // Issue 0020676
1304 if ( theCheckVertexNodes )
1305 while ( !SMESH_Algo::VertexNode( TopExp::FirstVertex( wireEdges.front(), true),
1306 theMesh.GetMeshDS()))
1308 wireEdges.splice(wireEdges.end(), wireEdges,
1309 wireEdges.begin(), ++wireEdges.begin());
1310 if ( from->IsSame( wireEdges.front() )) {
1312 ( new SMESH_ComputeError(COMPERR_BAD_INPUT_MESH,"No nodes on vertices"));
1313 return TSideVector(0);
1317 else if ( *nbE > 1 ) // Issue 0020676 (Face_pb_netgen.brep) - several internal edges in a wire
1319 internalEdges.splice( internalEdges.end(), wireEdges, ++wireEdges.begin(), wireEdges.end());
1322 StdMeshers_FaceSide* wire = new StdMeshers_FaceSide( theFace, wireEdges, &theMesh,
1323 /*isForward=*/true, theIgnoreMediumNodes,
1324 &helper, theProxyMesh );
1325 wires[ iW ] = StdMeshers_FaceSidePtr( wire );
1328 while ( !internalEdges.empty() )
1330 StdMeshers_FaceSide* wire = new StdMeshers_FaceSide( theFace, internalEdges.back(), &theMesh,
1331 /*isForward=*/true, theIgnoreMediumNodes,
1332 &helper, theProxyMesh );
1333 wires.push_back( StdMeshers_FaceSidePtr( wire ));
1334 internalEdges.pop_back();
1339 //================================================================================
1341 * \brief Return 1st vertex of the i-the edge
1343 //================================================================================
1345 TopoDS_Vertex StdMeshers_FaceSide::FirstVertex(int i) const
1348 if ( i < NbEdges() )
1350 v = myEdge[i].Orientation() <= TopAbs_REVERSED ? // FORWARD || REVERSED
1351 TopExp::FirstVertex( myEdge[i], 1 ) :
1352 TopoDS::Vertex( TopoDS_Iterator( myEdge[i] ).Value() );
1357 //================================================================================
1359 * \brief Return last vertex of the i-the edge
1361 //================================================================================
1363 TopoDS_Vertex StdMeshers_FaceSide::LastVertex(int i) const
1366 if ( i < NbEdges() )
1368 const TopoDS_Edge& edge = i<0 ? myEdge[ NbEdges() + i ] : myEdge[i];
1369 if ( edge.Orientation() <= TopAbs_REVERSED ) // FORWARD || REVERSED
1370 v = TopExp::LastVertex( edge, 1 );
1372 for ( TopoDS_Iterator vIt( edge ); vIt.More(); vIt.Next() )
1373 v = TopoDS::Vertex( vIt.Value() );
1378 //================================================================================
1380 * \brief Return \c true if the chain of EDGEs is closed
1382 //================================================================================
1384 bool StdMeshers_FaceSide::IsClosed() const
1386 return myEdge.empty() ? false : FirstVertex().IsSame( LastVertex() );
1389 //================================================================================
1391 * \brief Return a helper initialized with the FACE
1393 //================================================================================
1395 SMESH_MesherHelper* StdMeshers_FaceSide::FaceHelper() const
1397 StdMeshers_FaceSide* me = const_cast< StdMeshers_FaceSide* >( this );
1398 if ( !myHelper && myProxyMesh )
1400 me->myHelper = new SMESH_MesherHelper( *myProxyMesh->GetMesh() );
1401 me->myHelper->SetSubShape( myFace );
1403 return me->myHelper;