1 // Copyright (C) 2007-2015 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"
59 //================================================================================
61 * \brief Constructor of a side of one edge
62 * \param theFace - the face
63 * \param theEdge - the edge
65 //================================================================================
67 StdMeshers_FaceSide::StdMeshers_FaceSide(const TopoDS_Face& theFace,
68 const TopoDS_Edge& theEdge,
70 const bool theIsForward,
71 const bool theIgnoreMediumNodes,
72 SMESH_ProxyMesh::Ptr theProxyMesh)
74 list<TopoDS_Edge> edges(1,theEdge);
75 *this = StdMeshers_FaceSide( theFace, edges, theMesh, theIsForward,
76 theIgnoreMediumNodes, theProxyMesh );
79 //================================================================================
81 * \brief Constructor of a side of several edges
83 //================================================================================
85 StdMeshers_FaceSide::StdMeshers_FaceSide(const TopoDS_Face& theFace,
86 list<TopoDS_Edge>& theEdges,
88 const bool theIsForward,
89 const bool theIgnoreMediumNodes,
90 SMESH_ProxyMesh::Ptr theProxyMesh)
92 int nbEdges = theEdges.size();
93 myEdge.resize ( nbEdges );
94 myEdgeID.resize ( nbEdges );
95 myC2d.resize ( nbEdges );
96 myC3dAdaptor.resize( nbEdges );
97 myFirst.resize ( nbEdges );
98 myLast.resize ( nbEdges );
99 myNormPar.resize ( nbEdges );
100 myEdgeLength.resize( nbEdges );
101 myIsUniform.resize ( nbEdges, true );
104 myNbPonits = myNbSegments = 0;
105 myProxyMesh = theProxyMesh;
106 myMissingVertexNodes = false;
107 myIgnoreMediumNodes = theIgnoreMediumNodes;
108 myDefaultPnt2d = gp_Pnt2d( 1e+100, 1e+100 );
109 if ( !myProxyMesh ) myProxyMesh.reset( new SMESH_ProxyMesh( *theMesh ));
110 if ( nbEdges == 0 ) return;
112 SMESHDS_Mesh* meshDS = myProxyMesh->GetMeshDS();
115 list<TopoDS_Edge>::iterator edge = theEdges.begin();
116 for ( int index = 0; edge != theEdges.end(); ++index, ++edge )
118 int i = theIsForward ? index : nbEdges-index-1;
119 myEdgeLength[i] = SMESH_Algo::EdgeLength( *edge );
120 if ( myEdgeLength[i] < DBL_MIN ) nbDegen++;
121 myLength += myEdgeLength[i];
123 myEdgeID[i] = meshDS->ShapeToIndex( *edge );
124 if ( !theIsForward ) myEdge[i].Reverse();
126 if ( theFace.IsNull() )
127 BRep_Tool::Range( *edge, myFirst[i], myLast[i] );
129 myC2d[i] = BRep_Tool::CurveOnSurface( *edge, theFace, myFirst[i], myLast[i] );
130 if ( myEdge[i].Orientation() == TopAbs_REVERSED )
131 std::swap( myFirst[i], myLast[i] );
133 // check if the edge has a non-uniform parametrization (issue 0020705)
134 if ( !myC2d[i].IsNull() )
136 if ( myEdgeLength[i] > DBL_MIN )
138 Geom2dAdaptor_Curve A2dC( myC2d[i],
139 std::min( myFirst[i], myLast[i] ),
140 std::max( myFirst[i], myLast[i] ));
141 double p2 = myFirst[i]+(myLast[i]-myFirst[i])/2.;
142 double p4 = myFirst[i]+(myLast[i]-myFirst[i])/4.;
143 double d2 = GCPnts_AbscissaPoint::Length( A2dC, myFirst[i], p2 );
144 double d4 = GCPnts_AbscissaPoint::Length( A2dC, myFirst[i], p4 );
145 //cout<<"len = "<<len<<" d2 = "<<d2<<" fabs(2*d2/len-1.0) = "<<fabs(2*d2/len-1.0)<<endl;
146 myIsUniform[i] = !( fabs(2*d2/myEdgeLength[i]-1.0) > 0.01 || fabs(2*d4/d2-1.0) > 0.01 );
147 Handle(Geom_Curve) C3d = BRep_Tool::Curve(myEdge[i],d2,d4);
148 myC3dAdaptor[i].Load( C3d, d2,d4 );
152 const TopoDS_Vertex& V = SMESH_MesherHelper::IthVertex( 0, *edge );
153 Handle(Geom_Curve) C3d = new Geom_Line( BRep_Tool::Pnt( V ), gp::DX() );
154 myC3dAdaptor[i].Load( C3d, 0, 0.5 * BRep_Tool::Tolerance( V ));
157 // reverse a proxy sub-mesh
159 reverseProxySubmesh( myEdge[i] );
163 // count nodes and segments
164 NbPoints( /*update=*/true );
166 if ( nbEdges > 1 && myLength > DBL_MIN ) {
167 const double degenNormLen = 1.e-5;
168 double totLength = myLength;
170 totLength += myLength * degenNormLen * nbDegen;
171 double prevNormPar = 0;
172 for ( int i = 0; i < nbEdges; ++i ) {
173 if ( myEdgeLength[ i ] < DBL_MIN )
174 myEdgeLength[ i ] = myLength * degenNormLen;
175 myNormPar[ i ] = prevNormPar + myEdgeLength[i]/totLength;
176 prevNormPar = myNormPar[ i ];
179 myNormPar[nbEdges-1] = 1.;
183 //================================================================================
185 * \brief Constructor of a side for vertex using data from other FaceSide
187 //================================================================================
189 StdMeshers_FaceSide::StdMeshers_FaceSide(const StdMeshers_FaceSide* theSide,
190 const SMDS_MeshNode* theNode,
191 const gp_Pnt2d* thePnt2d1,
192 const gp_Pnt2d* thePnt2d2,
193 const Handle(Geom2d_Curve)& theC2d,
194 const double theUFirst,
195 const double theULast)
197 myC2d.push_back ( theC2d );
198 myFirst.push_back ( theUFirst );
199 myLast.push_back ( theULast );
200 myNormPar.push_back ( 1. );
201 myIsUniform.push_back( true );
202 myEdgeID.push_back ( 0 );
204 myProxyMesh = theSide->myProxyMesh;
205 myDefaultPnt2d = *thePnt2d1;
206 myPoints = theSide->GetUVPtStruct();
207 myNbPonits = myPoints.size();
208 myNbSegments = theSide->myNbSegments;
210 for ( size_t i = 0; i < myPoints.size(); ++i )
212 double r = i / ( myPoints.size() - 1. );
213 myPoints[i].u = (1-r) * thePnt2d1->X() + r * thePnt2d2->X();
214 myPoints[i].v = (1-r) * thePnt2d1->Y() + r * thePnt2d2->Y();
215 myPoints[i].node = theNode;
218 for ( size_t i = 0; i < myPoints.size(); ++i )
220 myPoints[i].u = thePnt2d1->X();
221 myPoints[i].v = thePnt2d1->Y();
222 myPoints[i].node = theNode;
226 //================================================================================
228 * Create a side from an UVPtStructVec
230 //================================================================================
232 StdMeshers_FaceSide::StdMeshers_FaceSide(UVPtStructVec& theSideNodes,
233 const TopoDS_Face& theFace)
236 myEdgeID.resize( 1, -1 );
238 myC3dAdaptor.resize( 1 );
239 myFirst.resize( 1, 0. );
240 myLast.resize( 1, 1. );
241 myNormPar.resize( 1, 1. );
242 myIsUniform.resize( 1, 1 );
243 myMissingVertexNodes = myIgnoreMediumNodes = false;
244 myDefaultPnt2d.SetCoord( 1e100, 1e100 );
247 myPoints = theSideNodes;
248 myNbPonits = myPoints.size();
249 myNbSegments = myNbPonits + 1;
252 if ( !myPoints.empty() )
254 myPoints[0].normParam = 0;
255 if ( myPoints[0].node &&
256 myPoints.back().node &&
257 myPoints[ myNbPonits/2 ].node )
259 gp_Pnt pPrev = SMESH_TNodeXYZ( myPoints[0].node );
260 for ( size_t i = 1; i < myPoints.size(); ++i )
262 gp_Pnt p = SMESH_TNodeXYZ( myPoints[i].node );
263 myLength += p.Distance( pPrev );
264 myPoints[i].normParam = myLength;
268 else if ( !theFace.IsNull() )
271 Handle(Geom_Surface) surf = BRep_Tool::Surface( theFace, loc );
272 gp_Pnt pPrev = surf->Value( myPoints[0].u, myPoints[0].v );
273 for ( size_t i = 1; i < myPoints.size(); ++i )
275 gp_Pnt p = surf->Value( myPoints[i].u, myPoints[i].v );
276 myLength += p.Distance( pPrev );
277 myPoints[i].normParam = myLength;
283 gp_Pnt2d pPrev = myPoints[0].UV();
284 for ( size_t i = 1; i < myPoints.size(); ++i )
286 gp_Pnt2d p = myPoints[i].UV();
287 myLength += p.Distance( pPrev );
288 myPoints[i].normParam = myLength;
292 if ( myLength > std::numeric_limits<double>::min() )
293 for ( size_t i = 1; i < myPoints.size(); ++i )
294 myPoints[i].normParam /= myLength;
296 myEdgeLength.resize( 1, myLength );
299 //================================================================================
301 * Return info on nodes on the side
303 //================================================================================
305 const vector<UVPtStruct>& StdMeshers_FaceSide::GetUVPtStruct(bool isXConst,
306 double constValue) const
308 if ( myPoints.empty() )
310 if ( NbEdges() == 0 ) return myPoints;
312 StdMeshers_FaceSide* me = const_cast< StdMeshers_FaceSide* >( this );
313 SMESHDS_Mesh* meshDS = myProxyMesh->GetMeshDS();
314 SMESH_MesherHelper eHelper( *myProxyMesh->GetMesh() );
315 SMESH_MesherHelper fHelper( *myProxyMesh->GetMesh() );
316 fHelper.SetSubShape( myFace );
320 // sort nodes of all edges putting them into a map
322 map< double, const SMDS_MeshNode*> u2node;
323 vector< pair< double, const SMDS_MeshNode*> > u2nodeVec;
324 vector<const SMDS_MeshNode*> nodes;
325 set<const SMDS_MeshNode*> vertexNodes;
326 vector< const SMESH_ProxyMesh::SubMesh* > proxySubMesh( myEdge.size() );
327 int nbProxyNodes = 0;
330 for ( iE = 0; iE < myEdge.size(); ++iE )
332 proxySubMesh[iE] = myProxyMesh->GetProxySubMesh( myEdge[iE] );
333 if ( proxySubMesh[iE] )
335 if ( proxySubMesh[iE]->GetUVPtStructVec().empty() ) {
336 proxySubMesh[iE] = 0;
339 nbProxyNodes += proxySubMesh[iE]->GetUVPtStructVec().size() - 1;
340 if ( iE+1 == myEdge.size() )
346 // Add 1st vertex node of a current edge
347 const SMDS_MeshNode* node = VertexNode( iE );
348 const double prevNormPar = ( iE == 0 ? 0 : myNormPar[ iE-1 ]); // normalized param
349 if ( node ) // nodes on internal vertices may be missing
351 if ( vertexNodes.insert( node ).second ||
352 fHelper.IsRealSeam( node->getshapeId() ))
353 u2node.insert( u2node.end(), make_pair( prevNormPar, node ));
357 if ( nodes.empty() ) {
358 for ( ++iE; iE < myEdge.size(); ++iE )
359 if (( node = VertexNode( iE ))) {
360 u2node.insert( make_pair( prevNormPar, node ));
367 vertexNodes.insert( node );
370 // Add internal nodes
372 if ( !GetEdgeNodes( iE, nodes, /*v0=*/false, /*v1=*/false ))
374 if ( !nodes.empty() )
377 double paramSize = myLast[iE] - myFirst[iE];
378 double r = myNormPar[iE] - prevNormPar;
379 eHelper.SetSubShape( myEdge[iE] );
380 eHelper.ToFixNodeParameters( true );
381 if ( !myIsUniform[iE] )
382 for ( size_t i = 0; i < nodes.size(); ++i )
384 double u = eHelper.GetNodeU( myEdge[iE], nodes[i], 0, ¶mOK );
385 double aLenU = GCPnts_AbscissaPoint::Length( me->myC3dAdaptor[iE], myFirst[iE], u );
386 if ( myEdgeLength[iE] < aLenU ) // nonregression test "3D_mesh_NETGEN/G6"
391 double normPar = prevNormPar + r * aLenU / myEdgeLength[iE];
392 u2nodeVec.push_back( make_pair( normPar, nodes[i] ));
394 if ( u2nodeVec.empty() )
395 for ( size_t i = 0; i < nodes.size(); ++i )
397 double u = eHelper.GetNodeU( myEdge[iE], nodes[i], 0, ¶mOK );
398 // paramSize is signed so orientation is taken into account
399 double normPar = prevNormPar + r * ( u - myFirst[iE] ) / paramSize;
400 u2nodeVec.push_back( make_pair( normPar, nodes[i] ));
402 for ( size_t j = 0; j < u2nodeVec.size(); ++j )
403 u2node.insert( u2node.end(), u2nodeVec[j] );
405 } // loop on myEdge's
407 // Add 2nd VERTEX node for a last EDGE
408 if ( !proxySubMesh.back() )
410 if ( u2node.empty() ) return myPoints;
412 const SMDS_MeshNode* node;
414 node = u2node.begin()->second;
417 node = VertexNode( iE );
418 while ( !node && iE > 0 )
419 node = VertexNode( --iE );
423 if ( u2node.rbegin()->second == node )
424 u2node.erase( --u2node.end() );
425 u2node.insert( u2node.end(), make_pair( 1., node ));
428 if ( u2node.size() + nbProxyNodes != myNbPonits &&
429 u2node.size() + nbProxyNodes != NbPoints( /*update=*/true ))
431 MESSAGE("Wrong node parameters on edges, u2node.size():"
432 <<u2node.size()<<" != myNbPonits:"<<myNbPonits);
436 // fill array of UVPtStruct
438 UVPtStructVec& points = me->myPoints;
439 points.resize( myNbPonits );
442 double prevNormPar = 0, paramSize = myNormPar[ 0 ];
443 map< double, const SMDS_MeshNode*>::iterator u_node = u2node.begin();
444 for ( size_t iE = 0; iE < myEdge.size(); ++iE )
446 if ( proxySubMesh[ iE ] ) // copy data from a proxy sub-mesh
448 const UVPtStructVec& edgeUVPtStruct = proxySubMesh[iE]->GetUVPtStructVec();
449 std::copy( edgeUVPtStruct.begin(), edgeUVPtStruct.end(), & points[iPt] );
451 double du1 = edgeUVPtStruct.back().param - edgeUVPtStruct[0].param;
452 double du2 = myLast[iE] - myFirst[iE];
455 std::reverse( & points[iPt], & points[iPt + edgeUVPtStruct.size()]);
456 for ( size_t i = 0; i < edgeUVPtStruct.size(); ++i )
457 points[iPt+i].normParam = 1. - points[iPt+i].normParam;
459 // update normalized params
460 if ( myEdge.size() > 1 ) {
461 for ( size_t i = 0; i < edgeUVPtStruct.size(); ++i, ++iPt )
463 UVPtStruct & uvPt = points[iPt];
464 uvPt.normParam = prevNormPar + uvPt.normParam * paramSize;
465 uvPt.x = uvPt.y = uvPt.normParam;
467 --iPt; // to point to the 1st VERTEX of the next EDGE
472 for ( ; u_node != u2node.end(); ++u_node, ++iPt )
474 if ( myNormPar[ iE ]-eps < u_node->first )
475 break; // u_node is at VERTEX of the next EDGE
477 UVPtStruct & uvPt = points[iPt];
478 uvPt.node = u_node->second;
479 // -- normParam, x, y --------------------------------
480 uvPt.normParam = u_node->first;
481 uvPt.x = uvPt.y = uvPt.normParam;
482 // -- U ----------------------------------------------
483 const SMDS_EdgePosition* epos =
484 dynamic_cast<const SMDS_EdgePosition*>(uvPt.node->GetPosition());
485 if ( epos && uvPt.node->getshapeId() == myEdgeID[iE] ) {
486 uvPt.param = epos->GetUParameter();
489 double r = ( uvPt.normParam - prevNormPar )/ paramSize;
490 uvPt.param = ( r > 0.5 ? myLast[iE] : myFirst[iE] );
492 // -- UV ---------------------------------------------
493 if ( !myC2d[ iE ].IsNull() ) {
494 gp_Pnt2d p = myC2d[ iE ]->Value( uvPt.param );
499 uvPt.u = uvPt.v = 1e+100;
503 // prepare for the next EDGE
504 if ( iE+1 < myEdge.size() )
506 prevNormPar = myNormPar[ iE ];
507 paramSize = myNormPar[ iE+1 ] - prevNormPar;
509 } // loop on myEdge's
513 for ( iPt = 0; iPt < points.size(); ++iPt ) points[ iPt ].x = constValue;
515 for ( iPt = 0; iPt < points.size(); ++iPt ) points[ iPt ].y = constValue;
517 } // if ( myPoints.empty())
522 //================================================================================
524 * \brief Falsificate info on nodes
525 * \param nbSeg - nb of segments on the side
526 * \retval UVPtStruct* - array of data structures
528 //================================================================================
530 const vector<UVPtStruct>& StdMeshers_FaceSide::SimulateUVPtStruct(int nbSeg,
532 double constValue) const
534 if ( myFalsePoints.empty() ) {
536 if ( NbEdges() == 0 ) return myFalsePoints;
538 vector<uvPtStruct>* points = const_cast<vector<uvPtStruct>*>( &myFalsePoints );
539 points->resize( nbSeg+1 );
542 double prevNormPar = 0, paramSize = myNormPar[ EdgeIndex ];
543 for (int i = 0 ; i < myFalsePoints.size(); ++i ) {
544 double normPar = double(i) / double(nbSeg);
545 UVPtStruct & uvPt = (*points)[i];
547 uvPt.x = uvPt.y = uvPt.param = uvPt.normParam = normPar;
548 if ( isXConst ) uvPt.x = constValue;
549 else uvPt.y = constValue;
550 if ( myNormPar[ EdgeIndex ] < normPar ) {
551 prevNormPar = myNormPar[ EdgeIndex ];
553 paramSize = myNormPar[ EdgeIndex ] - prevNormPar;
555 double r = ( normPar - prevNormPar )/ paramSize;
556 uvPt.param = myFirst[EdgeIndex] * ( 1 - r ) + myLast[EdgeIndex] * r;
557 if ( !myC2d[ EdgeIndex ].IsNull() ) {
558 gp_Pnt2d p = myC2d[ EdgeIndex ]->Value( uvPt.param );
563 uvPt.u = uvPt.v = 1e+100;
567 return myFalsePoints;
570 //=======================================================================
571 //function : GetOrderedNodes
572 //purpose : Return nodes in the order they encounter while walking along the side
573 //=======================================================================
575 std::vector<const SMDS_MeshNode*> StdMeshers_FaceSide::GetOrderedNodes(int theEdgeInd) const
577 vector<const SMDS_MeshNode*> resultNodes;
578 if ( myPoints.empty() || ( theEdgeInd >= 0 && NbEdges() > 0 ))
580 if ( NbEdges() == 0 ) return resultNodes;
582 SMESHDS_Mesh* meshDS = myProxyMesh->GetMeshDS();
583 SMESH_MesherHelper eHelper( *myProxyMesh->GetMesh() );
584 SMESH_MesherHelper fHelper( *myProxyMesh->GetMesh() );
585 fHelper.SetSubShape( myFace );
588 // Sort nodes of all edges putting them into a map
590 map< double, const SMDS_MeshNode*> u2node;
591 vector<const SMDS_MeshNode*> nodes;
592 set<const SMDS_MeshNode*> vertexNodes;
593 int iE = 0, iEnd = myEdge.size();
594 if ( theEdgeInd >= 0 )
596 iE = theEdgeInd % NbEdges();
599 for ( iE = 0; iE < iEnd; ++iE )
601 double prevNormPar = ( iE == 0 ? 0 : myNormPar[ iE-1 ]); // normalized param
603 const SMESH_ProxyMesh::SubMesh* proxySM = myProxyMesh->GetProxySubMesh( myEdge[iE] );
606 const UVPtStructVec& points = proxySM->GetUVPtStructVec();
607 for ( size_t i = 0; i < points.size(); ++i )
608 u2node.insert( make_pair( prevNormPar + points[i].normParam, points[i].node ));
612 // Add 1st vertex node of a current EDGE
613 const SMDS_MeshNode* node = VertexNode( iE );
614 if ( node ) { // nodes on internal vertices may be missing
615 if ( vertexNodes.insert( node ).second ||
616 fHelper.IsRealSeam( node->getshapeId() ))
617 u2node.insert( make_pair( prevNormPar, node ));
621 if ( nodes.empty() ) {
622 for ( ++iE; iE < iEnd; ++iE )
623 if (( node = VertexNode( iE ))) {
624 u2node.insert( make_pair( prevNormPar, node ));
631 vertexNodes.insert( node );
634 // Add internal nodes
636 if ( !GetEdgeNodes( iE, nodes, /*v0=*/false, /*v1=*/false ))
638 if ( !nodes.empty() )
640 double paramSize = myLast[iE] - myFirst[iE];
641 double r = myNormPar[iE] - prevNormPar;
642 eHelper.SetSubShape( myEdge[iE] );
643 eHelper.ToFixNodeParameters( true );
644 for ( size_t i = 0; i < nodes.size(); ++i )
646 double u = eHelper.GetNodeU( myEdge[iE], nodes[i], 0, ¶mOK );
647 // paramSize is signed so orientation is taken into account
648 double normPar = prevNormPar + r * ( u - myFirst[iE] ) / paramSize;
649 u2node.insert( u2node.end(), make_pair( normPar, nodes[i] ));
655 if ( u2node.empty() ) return resultNodes;
657 // Add 2nd vertex node for a last EDGE
659 const SMDS_MeshNode* node;
660 if ( IsClosed() && theEdgeInd < 0 )
661 node = u2node.begin()->second;
664 node = VertexNode( iE );
665 while ( !node && iE > 0 )
666 node = VertexNode( --iE );
670 if ( u2node.rbegin()->second == node )
671 u2node.erase( --u2node.end() );
672 u2node.insert( u2node.end(), make_pair( 1., node ));
675 // Fill the result vector
677 if ( u2node.size() == myNbPonits )
679 resultNodes.reserve( u2node.size() );
680 map< double, const SMDS_MeshNode*>::iterator u2n = u2node.begin();
681 for ( ; u2n != u2node.end(); ++u2n )
682 resultNodes.push_back( u2n->second );
687 resultNodes.resize( myPoints.size() );
688 for ( size_t i = 0; i < myPoints.size(); ++i )
689 resultNodes[i] = myPoints[i].node;
695 //================================================================================
697 * \brief Return (unsorted) nodes of the i-th EDGE.
698 * Nodes moved to other geometry by MergeNodes() are also returned.
699 * \retval bool - is OK
701 //================================================================================
703 bool StdMeshers_FaceSide::GetEdgeNodes(size_t i,
704 vector<const SMDS_MeshNode*>& nodes,
705 bool inlude1stVertex,
706 bool inludeLastVertex) const
708 if ( i >= myEdge.size() )
711 SMESH_Mesh* mesh = myProxyMesh->GetMesh();
712 SMESHDS_Mesh* meshDS = mesh->GetMeshDS();
713 SMESHDS_SubMesh* sm = meshDS->MeshElements( myEdge[i] );
715 if ( inlude1stVertex )
717 if ( const SMDS_MeshNode* n0 = VertexNode( i ))
718 nodes.push_back( n0 );
721 if ( sm && ( sm->NbElements() > 0 || sm->NbNodes() > 0 ))
723 if ( mesh->HasModificationsToDiscard() ) // check nb of nodes on the EDGE sub-mesh
725 int iQuad = sm->NbElements() ? sm->GetElements()->next()->IsQuadratic() : 0;
726 int nbExpect = sm->NbElements() - 1 + iQuad * sm->NbElements();
727 if ( nbExpect != sm->NbNodes() ) // some nodes are moved from the EDGE by MergeNodes()
729 // add nodes of all segments
730 typedef set< const SMDS_MeshNode* > TNodeSet;
731 TNodeSet sharedNodes;
732 SMDS_ElemIteratorPtr segIt = sm->GetElements();
733 while ( segIt->more() )
735 const SMDS_MeshElement* seg = segIt->next();
736 if ( seg->GetType() != SMDSAbs_Edge )
738 for ( int i = 0; i < 3-myIgnoreMediumNodes; ++i )
740 const SMDS_MeshNode* n = seg->GetNode( i );
741 if ( i == 2 ) // medium node
743 nodes.push_back( n );
747 pair<TNodeSet::iterator, bool> it2new = sharedNodes.insert( n );
748 if ( !it2new.second ) // n encounters twice == it's on EDGE, not on VERTEX
750 nodes.push_back( n );
751 sharedNodes.erase( it2new.first );
758 if ( nodes.size() < 2 ) // add nodes assigned to the EDGE
760 SMDS_NodeIteratorPtr nItr = sm->GetNodes();
761 while ( nItr->more() )
763 const SMDS_MeshNode* n = nItr->next();
764 if ( myIgnoreMediumNodes && SMESH_MeshEditor::IsMedium( n, SMDSAbs_Edge ))
766 nodes.push_back( n );
769 } // if ( sm && sm->NbElements() > 0 )
771 if ( inludeLastVertex )
773 if ( const SMDS_MeshNode* n1 = VertexNode( i+1 ))
774 nodes.push_back( n1 );
779 //================================================================================
781 * \brief Return a node from the i-th VERTEX (count starts from zero)
782 * Nodes moved to other geometry by MergeNodes() are also returned.
783 * \param [in] i - the VERTEX index
784 * \param [out] isMoved - returns \c true if the found node is moved by MergeNodes()
785 * \return const SMDS_MeshNode* - the found node
787 //================================================================================
789 const SMDS_MeshNode* StdMeshers_FaceSide::VertexNode(std::size_t i, bool* isMoved) const
791 TopoDS_Vertex V = ( i >= myEdge.size() ) ? LastVertex() : FirstVertex(i);
793 const SMDS_MeshNode* n = SMESH_Algo::VertexNode( V, myProxyMesh->GetMeshDS() );
795 if ( !n && !myEdge.empty() && myProxyMesh->GetMesh()->HasModificationsToDiscard() )
797 size_t iE = ( i < myEdge.size() ) ? i : myEdge.size()-1;
798 SMESHDS_SubMesh* sm = myProxyMesh->GetMeshDS()->MeshElements( myEdgeID[ iE ]);
800 n = SMESH_Algo::VertexNode( V, sm, myProxyMesh->GetMesh(), /*checkV=*/false );
803 (( i > 0 && i < NbEdges() ) || IsClosed() ))
805 iE = SMESH_MesherHelper::WrapIndex( int(i)-1, NbEdges() );
806 sm = myProxyMesh->GetMeshDS()->MeshElements( myEdgeID[ iE ]);
807 n = SMESH_Algo::VertexNode( V, sm, myProxyMesh->GetMesh(), /*checkV=*/false );
810 if ( n && n->GetPosition()->GetDim() == 1 ) // check that n does not lie on an EDGE of myFace
812 TopoDS_Shape S = SMESH_MesherHelper::GetSubShapeByNode( n, myProxyMesh->GetMeshDS() );
813 if ( SMESH_MesherHelper::IsSubShape( S, myFace ))
814 n = 0; // VERTEX ignored by e.g. Composite Wire Discretization algo
822 //================================================================================
824 * \brief reverse order of vector elements
825 * \param vec - vector to reverse
827 //================================================================================
829 template <typename T > void reverse(vector<T> & vec)
831 std::reverse( vec.begin(), vec.end() );
834 //================================================================================
836 * \brief Change orientation of side geometry
838 //================================================================================
840 void StdMeshers_FaceSide::Reverse()
842 int nbEdges = myEdge.size();
843 for ( int i = nbEdges-1; i >= 0; --i ) {
844 std::swap( myFirst[i], myLast[i] );
845 if ( !myEdge[i].IsNull() )
847 if ( i > 0 ) // at the first loop 1. is overwritten
848 myNormPar[i] = 1 - myNormPar[i-1];
854 //reverse( myC3dAdaptor );
857 reverse( myNormPar );
858 reverse( myEdgeLength );
859 reverse( myIsUniform );
863 myNormPar[nbEdges-1]=1.;
864 if ( !myEdge[0].IsNull() )
866 for ( size_t i = 0; i < myEdge.size(); ++i )
867 reverseProxySubmesh( myEdge[i] );
869 myFalsePoints.clear();
873 for ( size_t i = 0; i < myPoints.size(); ++i )
875 UVPtStruct & uvPt = myPoints[i];
876 uvPt.normParam = 1 - uvPt.normParam;
882 for ( size_t i = 0; i < myFalsePoints.size(); ++i )
884 UVPtStruct & uvPt = myFalsePoints[i];
885 uvPt.normParam = 1 - uvPt.normParam;
889 reverse( myFalsePoints );
892 for ( size_t i = 0; i < myEdge.size(); ++i )
894 if ( myEdge[i].IsNull() ) continue; // for a side on points only
896 Handle(Geom_Curve) C3d = BRep_Tool::Curve(myEdge[i],fp,lp);
898 myC3dAdaptor[i].Load( C3d, fp,lp );
902 //=======================================================================
903 //function : SetIgnoreMediumNodes
904 //purpose : Make ignore medium nodes
905 //=======================================================================
907 void StdMeshers_FaceSide::SetIgnoreMediumNodes(bool toIgnore)
909 if ( myIgnoreMediumNodes != toIgnore )
911 myIgnoreMediumNodes = toIgnore;
913 if ( !myPoints.empty() )
915 UVPtStructVec newPoints;
916 newPoints.reserve( myPoints.size()/2 + 1 );
917 for ( size_t i = 0; i < myPoints.size(); i += 2 )
918 newPoints.push_back( myPoints[i] );
920 myPoints.swap( newPoints );
924 NbPoints( /*update=*/true );
929 //=======================================================================
930 //function : NbPoints
931 //purpose : Return nb nodes on edges and vertices (+1 to be == GetUVPtStruct().size() )
932 // Call it with update == true if mesh of this side can be recomputed
933 // since creation of this side
934 //=======================================================================
936 int StdMeshers_FaceSide::NbPoints(const bool update) const
938 if ( !myPoints.empty() )
939 return myPoints.size();
941 // if ( !myFalsePoints.empty() )
942 // return myFalsePoints.size();
944 if ( update && myEdge.size() > 0 )
946 StdMeshers_FaceSide* me = (StdMeshers_FaceSide*) this;
948 me->myNbSegments = 0;
949 me->myMissingVertexNodes = false;
951 vector<const SMDS_MeshNode*> nodes;
952 for ( int i = 0; i < NbEdges(); ++i )
954 if ( const SMESHDS_SubMesh* sm = myProxyMesh->GetSubMesh( Edge(i) ))
956 if ( sm->NbNodes() == sm->NbElements() - 1 )
958 me->myNbPonits += sm->NbNodes();
959 if ( myIgnoreMediumNodes && sm->IsQuadratic() )
960 me->myNbPonits -= sm->NbElements();
962 else // nodes can be moved to other shapes by MergeNodes()
965 GetEdgeNodes( i, nodes, /*v1=*/false, /*v2=*/false );
966 me->myNbPonits += nodes.size();
968 me->myNbSegments += sm->NbElements();
972 SMESH_MesherHelper helper( *myProxyMesh->GetMesh() );
973 helper.SetSubShape( myFace );
975 std::set< const SMDS_MeshNode* > vNodes;
976 for ( int i = 0; i <= NbEdges(); ++i ) // nb VERTEXes is more than NbEdges() if !IsClosed()
977 if ( const SMDS_MeshNode* n = VertexNode( i ))
979 if ( !vNodes.insert( n ).second &&
980 helper.IsRealSeam( n->getshapeId() ) &&
986 me->myMissingVertexNodes = true;
988 me->myNbPonits += vNodes.size();
991 me->myNbPonits++; // closing node is repeated
996 //=======================================================================
997 //function : NbSegments
998 //purpose : Return nb edges
999 // Call it with update == true if mesh of this side can be recomputed
1000 // since creation of this side
1001 //=======================================================================
1003 int StdMeshers_FaceSide::NbSegments(const bool update) const
1005 return NbPoints( update ), myNbSegments;
1008 //================================================================================
1010 * \brief Reverse UVPtStructVec if a proxy sub-mesh of E
1012 //================================================================================
1014 void StdMeshers_FaceSide::reverseProxySubmesh( const TopoDS_Edge& E )
1016 if ( !myProxyMesh ) return;
1017 if ( const SMESH_ProxyMesh::SubMesh* sm = myProxyMesh->GetProxySubMesh( E ))
1019 UVPtStructVec& edgeUVPtStruct = (UVPtStructVec& ) sm->GetUVPtStructVec();
1020 for ( size_t i = 0; i < edgeUVPtStruct.size(); ++i )
1022 UVPtStruct & uvPt = edgeUVPtStruct[i];
1023 uvPt.normParam = 1 - uvPt.normParam;
1024 uvPt.x = 1 - uvPt.x;
1025 uvPt.y = 1 - uvPt.y;
1027 reverse( edgeUVPtStruct );
1031 //================================================================================
1033 * \brief Show side features
1035 //================================================================================
1037 void StdMeshers_FaceSide::dump(const char* msg) const
1040 if (msg) MESSAGE ( std::endl << msg );
1041 MESSAGE_BEGIN ("NB EDGES: "<< myEdge.size() );
1042 MESSAGE_ADD ( "nbPoints: "<<myNbPonits<<" vecSize: " << myPoints.size()<<" "<<myFalsePoints.size() );
1043 for ( int i=0; i<myEdge.size(); ++i)
1045 MESSAGE_ADD ( "\t"<<i+1 );
1046 MESSAGE_ADD ( "\tEDGE: " );
1047 if (myEdge[i].IsNull()) {
1048 MESSAGE_ADD ( "NULL" );
1051 TopAbs::Print(myEdge[i].Orientation(),cout)<<" "<<myEdge[i].TShape().operator->()<<endl;
1052 MESSAGE_ADD ( "\tV1: " << TopExp::FirstVertex( myEdge[i], 1).TShape().operator->()
1053 << " V2: " << TopExp::LastVertex( myEdge[i], 1).TShape().operator->() );
1055 MESSAGE_ADD ( "\tC2d: ");
1057 if (myC2d[i].IsNull()) {
1058 MESSAGE_ADD ( "NULL" );
1061 MESSAGE_ADD ( myC2d[i].operator->() );
1064 MESSAGE_ADD ( "\tF: "<<myFirst[i]<< " L: "<< myLast[i] );
1065 MESSAGE_END ( "\tnormPar: "<<myNormPar[i]<<endl );
1070 //================================================================================
1072 * \brief Creates a Adaptor2d_Curve2d to be used in SMESH_Block
1073 * \retval Adaptor2d_Curve2d* -
1075 //================================================================================
1077 struct Adaptor2dCurve2d : public Adaptor2d_Curve2d
1079 const StdMeshers_FaceSide* mySide;
1080 Adaptor2dCurve2d(const StdMeshers_FaceSide* faceSide):mySide(faceSide) {}
1081 gp_Pnt2d Value(const Standard_Real U) const { return mySide->Value2d( U ); }
1082 Standard_Real FirstParameter() const { return 0; }
1083 Standard_Real LastParameter() const { return 1; }
1086 Adaptor2d_Curve2d* StdMeshers_FaceSide::GetCurve2d() const
1088 return new Adaptor2dCurve2d( this );
1091 //================================================================================
1093 * \brief Creates a fully functional Adaptor_Curve
1095 //================================================================================
1097 BRepAdaptor_CompCurve* StdMeshers_FaceSide::GetCurve3d() const
1099 if ( myEdge.empty() )
1103 BRep_Builder aBuilder;
1104 aBuilder.MakeWire(aWire);
1105 for ( int i=0; i<myEdge.size(); ++i )
1106 aBuilder.Add( aWire, myEdge[i] );
1108 if ( myEdge.size() == 2 && IsClosed() )
1109 aWire.Closed(true); // issue 0021141
1111 return new BRepAdaptor_CompCurve( aWire );
1114 //================================================================================
1116 * \brief Return 2D point by normalized parameter
1117 * \param U - normalized parameter value
1118 * \retval gp_Pnt2d - point
1120 //================================================================================
1122 gp_Pnt2d StdMeshers_FaceSide::Value2d(double U) const
1124 if ( !myC2d[0].IsNull() ) {
1125 int i = EdgeIndex( U );
1126 double prevU = i ? myNormPar[ i-1 ] : 0;
1127 double r = ( U - prevU )/ ( myNormPar[ i ] - prevU );
1129 double par = myFirst[i] * ( 1 - r ) + myLast[i] * r;
1131 // check parametrization of curve
1132 if( !myIsUniform[i] )
1134 double aLen3dU = r * myEdgeLength[i] * ( myFirst[i]>myLast[i] ? -1. : 1.);
1135 GCPnts_AbscissaPoint AbPnt
1136 ( const_cast<GeomAdaptor_Curve&>( myC3dAdaptor[i]), aLen3dU, myFirst[i] );
1137 if( AbPnt.IsDone() ) {
1138 par = AbPnt.Parameter();
1141 return myC2d[ i ]->Value(par);
1144 else if ( !myPoints.empty() )
1146 int i = U * double( myPoints.size()-1 );
1147 while ( i > 0 && myPoints[ i ].normParam > U )
1149 while ( i+1 < myPoints.size() && myPoints[ i+1 ].normParam < U )
1151 double r = (( U - myPoints[ i ].normParam ) /
1152 ( myPoints[ i+1 ].normParam - myPoints[ i ].normParam ));
1153 return ( myPoints[ i ].UV() * ( 1 - r ) +
1154 myPoints[ i+1 ].UV() * r );
1156 return myDefaultPnt2d;
1159 //================================================================================
1161 * \brief Return XYZ by normalized parameter
1162 * \param U - normalized parameter value
1163 * \retval gp_Pnt - point
1165 //================================================================================
1167 gp_Pnt StdMeshers_FaceSide::Value3d(double U) const
1169 int i = EdgeIndex( U );
1170 double prevU = i ? myNormPar[ i-1 ] : 0;
1171 double r = ( U - prevU )/ ( myNormPar[ i ] - prevU );
1173 double par = myFirst[i] * ( 1 - r ) + myLast[i] * r;
1175 // check parametrization of curve
1176 if( !myIsUniform[i] )
1178 double aLen3dU = r * myEdgeLength[i] * ( myFirst[i]>myLast[i] ? -1. : 1.);
1179 GCPnts_AbscissaPoint AbPnt
1180 ( const_cast<GeomAdaptor_Curve&>( myC3dAdaptor[i]), aLen3dU, myFirst[i] );
1181 if( AbPnt.IsDone() ) {
1182 par = AbPnt.Parameter();
1185 return myC3dAdaptor[ i ].Value(par);
1188 //================================================================================
1190 * \brief Return wires of a face as StdMeshers_FaceSide's
1192 //================================================================================
1194 TSideVector StdMeshers_FaceSide::GetFaceWires(const TopoDS_Face& theFace,
1195 SMESH_Mesh & theMesh,
1196 const bool theIgnoreMediumNodes,
1198 SMESH_ProxyMesh::Ptr theProxyMesh,
1199 const bool theCheckVertexNodes)
1201 list< TopoDS_Edge > edges, internalEdges;
1202 list< int > nbEdgesInWires;
1203 int nbWires = SMESH_Block::GetOrderedEdges (theFace, edges, nbEdgesInWires);
1205 // split list of all edges into separate wires
1206 TSideVector wires( nbWires );
1207 list< int >::iterator nbE = nbEdgesInWires.begin();
1208 list< TopoDS_Edge >::iterator from = edges.begin(), to = from;
1209 for ( int iW = 0; iW < nbWires; ++iW, ++nbE )
1211 std::advance( to, *nbE );
1212 if ( *nbE == 0 ) // Issue 0020676
1216 wires.resize( nbWires );
1219 list< TopoDS_Edge > wireEdges( from, to );
1220 // assure that there is a node on the first vertex
1221 // as StdMeshers_FaceSide::GetUVPtStruct() requires
1222 if ( wireEdges.front().Orientation() != TopAbs_INTERNAL ) // Issue 0020676
1224 if ( theCheckVertexNodes )
1225 while ( !SMESH_Algo::VertexNode( TopExp::FirstVertex( wireEdges.front(), true),
1226 theMesh.GetMeshDS()))
1228 wireEdges.splice(wireEdges.end(), wireEdges,
1229 wireEdges.begin(), ++wireEdges.begin());
1230 if ( from->IsSame( wireEdges.front() )) {
1232 ( new SMESH_ComputeError(COMPERR_BAD_INPUT_MESH,"No nodes on vertices"));
1233 return TSideVector(0);
1237 else if ( *nbE > 1 ) // Issue 0020676 (Face_pb_netgen.brep) - several internal edges in a wire
1239 internalEdges.splice( internalEdges.end(), wireEdges, ++wireEdges.begin(), wireEdges.end());
1242 StdMeshers_FaceSide* wire = new StdMeshers_FaceSide( theFace, wireEdges, &theMesh,
1243 /*isForward=*/true, theIgnoreMediumNodes,
1245 wires[ iW ] = StdMeshers_FaceSidePtr( wire );
1248 while ( !internalEdges.empty() )
1250 StdMeshers_FaceSide* wire = new StdMeshers_FaceSide( theFace, internalEdges.back(), &theMesh,
1251 /*isForward=*/true, theIgnoreMediumNodes,
1253 wires.push_back( StdMeshers_FaceSidePtr( wire ));
1254 internalEdges.pop_back();
1259 //================================================================================
1261 * \brief Return 1st vertex of the i-the edge
1263 //================================================================================
1265 TopoDS_Vertex StdMeshers_FaceSide::FirstVertex(int i) const
1268 if ( i < NbEdges() )
1270 v = myEdge[i].Orientation() <= TopAbs_REVERSED ? // FORWARD || REVERSED
1271 TopExp::FirstVertex( myEdge[i], 1 ) :
1272 TopoDS::Vertex( TopoDS_Iterator( myEdge[i] ).Value() );
1277 //================================================================================
1279 * \brief Return last vertex of the i-the edge
1281 //================================================================================
1283 TopoDS_Vertex StdMeshers_FaceSide::LastVertex(int i) const
1286 if ( i < NbEdges() )
1288 const TopoDS_Edge& edge = i<0 ? myEdge[ NbEdges() + i ] : myEdge[i];
1289 if ( edge.Orientation() <= TopAbs_REVERSED ) // FORWARD || REVERSED
1290 v = TopExp::LastVertex( edge, 1 );
1292 for ( TopoDS_Iterator vIt( edge ); vIt.More(); vIt.Next() )
1293 v = TopoDS::Vertex( vIt.Value() );
1298 //================================================================================
1300 * \brief Return \c true if the chain of EDGEs is closed
1302 //================================================================================
1304 bool StdMeshers_FaceSide::IsClosed() const
1306 return myEdge.empty() ? false : FirstVertex().IsSame( LastVertex() );