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 TopoDS_Iterator vExp;
117 for ( int index = 0; edge != theEdges.end(); ++index, ++edge )
119 int i = theIsForward ? index : nbEdges-index-1;
120 myEdgeLength[i] = SMESH_Algo::EdgeLength( *edge );
121 if ( myEdgeLength[i] < DBL_MIN ) nbDegen++;
122 myLength += myEdgeLength[i];
124 myEdgeID[i] = meshDS->ShapeToIndex( *edge );
125 if ( !theIsForward ) myEdge[i].Reverse();
127 if ( theFace.IsNull() )
128 BRep_Tool::Range( *edge, myFirst[i], myLast[i] );
130 myC2d[i] = BRep_Tool::CurveOnSurface( *edge, theFace, myFirst[i], myLast[i] );
131 if ( myEdge[i].Orientation() == TopAbs_REVERSED )
132 std::swap( myFirst[i], myLast[i] );
134 // check if the edge has a non-uniform parametrization (issue 0020705)
135 if ( !myC2d[i].IsNull() )
137 if ( myEdgeLength[i] > DBL_MIN )
139 Geom2dAdaptor_Curve A2dC( myC2d[i],
140 std::min( myFirst[i], myLast[i] ),
141 std::max( myFirst[i], myLast[i] ));
142 double p2 = myFirst[i]+(myLast[i]-myFirst[i])/2.;
143 double p4 = myFirst[i]+(myLast[i]-myFirst[i])/4.;
144 double d2 = GCPnts_AbscissaPoint::Length( A2dC, myFirst[i], p2 );
145 double d4 = GCPnts_AbscissaPoint::Length( A2dC, myFirst[i], p4 );
146 //cout<<"len = "<<len<<" d2 = "<<d2<<" fabs(2*d2/len-1.0) = "<<fabs(2*d2/len-1.0)<<endl;
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 = TopoDS::Vertex( vExp.Value() );
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)
198 myC2d.push_back ( theC2d );
199 myFirst.push_back ( theUFirst );
200 myLast.push_back ( theULast );
201 myNormPar.push_back ( 1. );
202 myIsUniform.push_back( true );
203 myEdgeID.push_back ( 0 );
205 myProxyMesh = theSide->myProxyMesh;
206 myDefaultPnt2d = *thePnt2d1;
207 myPoints = theSide->GetUVPtStruct();
208 myNbPonits = myPoints.size();
209 myNbSegments = theSide->myNbSegments;
211 for ( size_t i = 0; i < myPoints.size(); ++i )
213 double r = i / ( myPoints.size() - 1. );
214 myPoints[i].u = (1-r) * thePnt2d1->X() + r * thePnt2d2->X();
215 myPoints[i].v = (1-r) * thePnt2d1->Y() + r * thePnt2d2->Y();
216 myPoints[i].node = theNode;
219 for ( size_t i = 0; i < myPoints.size(); ++i )
221 myPoints[i].u = thePnt2d1->X();
222 myPoints[i].v = thePnt2d1->Y();
223 myPoints[i].node = theNode;
227 //================================================================================
229 * Create a side from an UVPtStructVec
231 //================================================================================
233 StdMeshers_FaceSide::StdMeshers_FaceSide(UVPtStructVec& theSideNodes,
234 const TopoDS_Face& theFace)
237 myEdgeID.resize( 1, -1 );
239 myC3dAdaptor.resize( 1 );
240 myFirst.resize( 1, 0. );
241 myLast.resize( 1, 1. );
242 myNormPar.resize( 1, 1. );
243 myIsUniform.resize( 1, 1 );
244 myMissingVertexNodes = myIgnoreMediumNodes = false;
245 myDefaultPnt2d.SetCoord( 1e100, 1e100 );
248 myPoints = theSideNodes;
249 myNbPonits = myPoints.size();
250 myNbSegments = myNbPonits + 1;
253 if ( !myPoints.empty() )
255 myPoints[0].normParam = 0;
256 if ( myPoints[0].node &&
257 myPoints.back().node &&
258 myPoints[ myNbPonits/2 ].node )
260 gp_Pnt pPrev = SMESH_TNodeXYZ( myPoints[0].node );
261 for ( size_t i = 1; i < myPoints.size(); ++i )
263 gp_Pnt p = SMESH_TNodeXYZ( myPoints[i].node );
264 myLength += p.Distance( pPrev );
265 myPoints[i].normParam = myLength;
269 else if ( !theFace.IsNull() )
272 Handle(Geom_Surface) surf = BRep_Tool::Surface( theFace, loc );
273 gp_Pnt pPrev = surf->Value( myPoints[0].u, myPoints[0].v );
274 for ( size_t i = 1; i < myPoints.size(); ++i )
276 gp_Pnt p = surf->Value( myPoints[i].u, myPoints[i].v );
277 myLength += p.Distance( pPrev );
278 myPoints[i].normParam = myLength;
284 gp_Pnt2d pPrev = myPoints[0].UV();
285 for ( size_t i = 1; i < myPoints.size(); ++i )
287 gp_Pnt2d p = myPoints[i].UV();
288 myLength += p.Distance( pPrev );
289 myPoints[i].normParam = myLength;
293 if ( myLength > std::numeric_limits<double>::min() )
294 for ( size_t i = 1; i < myPoints.size(); ++i )
295 myPoints[i].normParam /= myLength;
297 myEdgeLength.resize( 1, myLength );
300 //================================================================================
302 * Return info on nodes on the side
304 //================================================================================
306 const vector<UVPtStruct>& StdMeshers_FaceSide::GetUVPtStruct(bool isXConst,
307 double constValue) const
309 if ( myPoints.empty() )
311 if ( NbEdges() == 0 ) return myPoints;
313 StdMeshers_FaceSide* me = const_cast< StdMeshers_FaceSide* >( this );
314 SMESHDS_Mesh* meshDS = myProxyMesh->GetMeshDS();
315 SMESH_MesherHelper helper( *myProxyMesh->GetMesh() );
319 // sort nodes of all edges putting them into a map
321 map< double, const SMDS_MeshNode*> u2node;
322 vector< pair< double, const SMDS_MeshNode*> > u2nodeVec;
323 vector<const SMDS_MeshNode*> nodes;
324 set<const SMDS_MeshNode*> vertexNodes;
325 vector< const SMESH_ProxyMesh::SubMesh* > proxySubMesh( myEdge.size() );
326 int nbProxyNodes = 0;
329 for ( iE = 0; iE < myEdge.size(); ++iE )
331 proxySubMesh[iE] = myProxyMesh->GetProxySubMesh( myEdge[iE] );
332 if ( proxySubMesh[iE] )
334 if ( proxySubMesh[iE]->GetUVPtStructVec().empty() ) {
335 proxySubMesh[iE] = 0;
338 nbProxyNodes += proxySubMesh[iE]->GetUVPtStructVec().size() - 1;
339 if ( iE+1 == myEdge.size() )
345 // Add 1st vertex node of a current edge
346 const SMDS_MeshNode* node = VertexNode( iE );
347 const double prevNormPar = ( iE == 0 ? 0 : myNormPar[ iE-1 ]); // normalized param
348 if ( node ) // nodes on internal vertices may be missing
350 if ( vertexNodes.insert( node ).second )
351 u2node.insert( u2node.end(), make_pair( prevNormPar, node ));
355 if ( nodes.empty() ) {
356 for ( ++iE; iE < myEdge.size(); ++iE )
357 if (( node = VertexNode( iE ))) {
358 u2node.insert( make_pair( prevNormPar, node ));
365 vertexNodes.insert( node );
368 // Add internal nodes
370 if ( !GetEdgeNodes( iE, nodes, /*v0=*/false, /*v1=*/false ))
372 if ( !nodes.empty() )
375 double paramSize = myLast[iE] - myFirst[iE];
376 double r = myNormPar[iE] - prevNormPar;
377 helper.SetSubShape( myEdge[iE] );
378 helper.ToFixNodeParameters( true );
379 if ( !myIsUniform[iE] )
380 for ( size_t i = 0; i < nodes.size(); ++i )
382 double u = helper.GetNodeU( myEdge[iE], nodes[i], 0, ¶mOK );
383 double aLenU = GCPnts_AbscissaPoint::Length( me->myC3dAdaptor[iE], myFirst[iE], u );
384 if ( myEdgeLength[iE] < aLenU ) // nonregression test "3D_mesh_NETGEN/G6"
389 double normPar = prevNormPar + r * aLenU / myEdgeLength[iE];
390 u2nodeVec.push_back( make_pair( normPar, nodes[i] ));
392 if ( u2nodeVec.empty() )
393 for ( size_t i = 0; i < nodes.size(); ++i )
395 double u = helper.GetNodeU( myEdge[iE], nodes[i], 0, ¶mOK );
396 // paramSize is signed so orientation is taken into account
397 double normPar = prevNormPar + r * ( u - myFirst[iE] ) / paramSize;
398 u2nodeVec.push_back( make_pair( normPar, nodes[i] ));
400 for ( size_t j = 0; j < u2nodeVec.size(); ++j )
401 u2node.insert( u2node.end(), u2nodeVec[j] );
403 } // loop on myEdge's
405 if ( u2node.empty() ) return myPoints;
407 // Add 2nd VERTEX node for a last EDGE
409 const SMDS_MeshNode* node;
411 node = u2node.begin()->second;
414 node = VertexNode( iE );
415 while ( !node && iE > 0 )
416 node = VertexNode( --iE );
420 if ( u2node.rbegin()->second == node )
421 u2node.erase( --u2node.end() );
422 u2node.insert( u2node.end(), make_pair( 1., node ));
425 if ( u2node.size() + nbProxyNodes != myNbPonits &&
426 u2node.size() + nbProxyNodes != NbPoints( /*update=*/true ))
428 MESSAGE("Wrong node parameters on edges, u2node.size():"
429 <<u2node.size()<<" != myNbPonits:"<<myNbPonits);
433 // fill array of UVPtStruct
435 UVPtStructVec& points = me->myPoints;
436 points.resize( myNbPonits );
439 double prevNormPar = 0, paramSize = myNormPar[ 0 ];
440 map< double, const SMDS_MeshNode*>::iterator u_node = u2node.begin();
441 for ( size_t iE = 0; iE < myEdge.size(); ++iE )
443 if ( proxySubMesh[ iE ] ) // copy data from a proxy sub-mesh
445 const UVPtStructVec& edgeUVPtStruct = proxySubMesh[iE]->GetUVPtStructVec();
446 std::copy( edgeUVPtStruct.begin(), edgeUVPtStruct.end(), & points[iPt] );
448 double du1 = edgeUVPtStruct.back().param - edgeUVPtStruct[0].param;
449 double du2 = myLast[iE] - myFirst[iE];
452 std::reverse( & points[iPt], & points[iPt + edgeUVPtStruct.size()]);
453 for ( size_t i = 0; i < edgeUVPtStruct.size(); ++i )
454 points[iPt+i].normParam = 1. - points[iPt+i].normParam;
456 // update normalized params
457 if ( myEdge.size() > 1 ) {
458 for ( size_t i = 0; i < edgeUVPtStruct.size(); ++i, ++iPt )
460 UVPtStruct & uvPt = points[iPt];
461 uvPt.normParam = prevNormPar + uvPt.normParam * paramSize;
462 uvPt.x = uvPt.y = uvPt.normParam;
464 --iPt; // to point to the 1st VERTEX of the next EDGE
469 for ( ; u_node != u2node.end(); ++u_node, ++iPt )
471 if ( myNormPar[ iE ]-eps < u_node->first )
472 break; // u_node is at VERTEX of the next EDGE
474 UVPtStruct & uvPt = points[iPt];
475 uvPt.node = u_node->second;
476 // -- normParam, x, y --------------------------------
477 uvPt.normParam = u_node->first;
478 uvPt.x = uvPt.y = uvPt.normParam;
479 // -- U ----------------------------------------------
480 const SMDS_EdgePosition* epos =
481 dynamic_cast<const SMDS_EdgePosition*>(uvPt.node->GetPosition());
482 if ( epos && uvPt.node->getshapeId() == myEdgeID[iE] ) {
483 uvPt.param = epos->GetUParameter();
486 double r = ( uvPt.normParam - prevNormPar )/ paramSize;
487 uvPt.param = ( r > 0.5 ? myLast[iE] : myFirst[iE] );
489 // -- UV ---------------------------------------------
490 if ( !myC2d[ iE ].IsNull() ) {
491 gp_Pnt2d p = myC2d[ iE ]->Value( uvPt.param );
496 uvPt.u = uvPt.v = 1e+100;
500 // prepare for the next EDGE
501 if ( iE+1 < myEdge.size() )
503 prevNormPar = myNormPar[ iE ];
504 paramSize = myNormPar[ iE+1 ] - prevNormPar;
506 } // loop on myEdge's
510 for ( iPt = 0; iPt < points.size(); ++iPt ) points[ iPt ].x = constValue;
512 for ( iPt = 0; iPt < points.size(); ++iPt ) points[ iPt ].y = constValue;
514 } // if ( myPoints.empty())
519 //================================================================================
521 * \brief Falsificate info on nodes
522 * \param nbSeg - nb of segments on the side
523 * \retval UVPtStruct* - array of data structures
525 //================================================================================
527 const vector<UVPtStruct>& StdMeshers_FaceSide::SimulateUVPtStruct(int nbSeg,
529 double constValue) const
531 if ( myFalsePoints.empty() ) {
533 if ( NbEdges() == 0 ) return myFalsePoints;
535 vector<uvPtStruct>* points = const_cast<vector<uvPtStruct>*>( &myFalsePoints );
536 points->resize( nbSeg+1 );
539 double prevNormPar = 0, paramSize = myNormPar[ EdgeIndex ];
540 for (int i = 0 ; i < myFalsePoints.size(); ++i ) {
541 double normPar = double(i) / double(nbSeg);
542 UVPtStruct & uvPt = (*points)[i];
544 uvPt.x = uvPt.y = uvPt.param = uvPt.normParam = normPar;
545 if ( isXConst ) uvPt.x = constValue;
546 else uvPt.y = constValue;
547 if ( myNormPar[ EdgeIndex ] < normPar ) {
548 prevNormPar = myNormPar[ EdgeIndex ];
550 paramSize = myNormPar[ EdgeIndex ] - prevNormPar;
552 double r = ( normPar - prevNormPar )/ paramSize;
553 uvPt.param = myFirst[EdgeIndex] * ( 1 - r ) + myLast[EdgeIndex] * r;
554 if ( !myC2d[ EdgeIndex ].IsNull() ) {
555 gp_Pnt2d p = myC2d[ EdgeIndex ]->Value( uvPt.param );
560 uvPt.u = uvPt.v = 1e+100;
564 return myFalsePoints;
567 //=======================================================================
568 //function : GetOrderedNodes
569 //purpose : Return nodes in the order they encounter while walking along the side
570 //=======================================================================
572 std::vector<const SMDS_MeshNode*> StdMeshers_FaceSide::GetOrderedNodes(int theEdgeInd) const
574 vector<const SMDS_MeshNode*> resultNodes;
575 if ( myPoints.empty() || ( theEdgeInd >= 0 && NbEdges() > 0 ))
577 if ( NbEdges() == 0 ) return resultNodes;
579 SMESHDS_Mesh* meshDS = myProxyMesh->GetMeshDS();
580 SMESH_MesherHelper helper(*myProxyMesh->GetMesh());
583 // Sort nodes of all edges putting them into a map
585 map< double, const SMDS_MeshNode*> u2node;
586 vector<const SMDS_MeshNode*> nodes;
587 set<const SMDS_MeshNode*> vertexNodes;
588 int iE = 0, iEnd = myEdge.size();
589 if ( theEdgeInd >= 0 )
591 iE = theEdgeInd % NbEdges();
594 for ( iE = 0; iE < iEnd; ++iE )
596 double prevNormPar = ( iE == 0 ? 0 : myNormPar[ iE-1 ]); // normalized param
598 const SMESH_ProxyMesh::SubMesh* proxySM = myProxyMesh->GetProxySubMesh( myEdge[iE] );
601 const UVPtStructVec& points = proxySM->GetUVPtStructVec();
602 for ( size_t i = 0; i < points.size(); ++i )
603 u2node.insert( make_pair( prevNormPar + points[i].normParam, points[i].node ));
607 // Add 1st vertex node of a current EDGE
608 const SMDS_MeshNode* node = VertexNode( iE );
609 if ( node ) { // nodes on internal vertices may be missing
610 if ( vertexNodes.insert( node ).second )
611 u2node.insert( make_pair( prevNormPar, node ));
615 if ( nodes.empty() ) {
616 for ( ++iE; iE < iEnd; ++iE )
617 if (( node = VertexNode( iE ))) {
618 u2node.insert( make_pair( prevNormPar, node ));
625 vertexNodes.insert( node );
628 // Add internal nodes
630 if ( !GetEdgeNodes( iE, nodes, /*v0=*/false, /*v1=*/false ))
632 if ( !nodes.empty() )
634 double paramSize = myLast[iE] - myFirst[iE];
635 double r = myNormPar[iE] - prevNormPar;
636 helper.SetSubShape( myEdge[iE] );
637 helper.ToFixNodeParameters( true );
638 for ( size_t i = 0; i < nodes.size(); ++i )
640 double u = helper.GetNodeU( myEdge[iE], nodes[i], 0, ¶mOK );
641 // paramSize is signed so orientation is taken into account
642 double normPar = prevNormPar + r * ( u - myFirst[iE] ) / paramSize;
643 u2node.insert( u2node.end(), make_pair( normPar, nodes[i] ));
649 if ( u2node.empty() ) return resultNodes;
651 // Add 2nd vertex node for a last EDGE
653 const SMDS_MeshNode* node;
654 if ( IsClosed() && theEdgeInd < 0 )
655 node = u2node.begin()->second;
658 node = VertexNode( iE );
659 while ( !node && iE > 0 )
660 node = VertexNode( --iE );
664 if ( u2node.rbegin()->second == node )
665 u2node.erase( --u2node.end() );
666 u2node.insert( u2node.end(), make_pair( 1., node ));
669 // Fill the result vector
671 if ( u2node.size() == myNbPonits )
673 resultNodes.reserve( u2node.size() );
674 map< double, const SMDS_MeshNode*>::iterator u2n = u2node.begin();
675 for ( ; u2n != u2node.end(); ++u2n )
676 resultNodes.push_back( u2n->second );
681 resultNodes.resize( myPoints.size() );
682 for ( size_t i = 0; i < myPoints.size(); ++i )
683 resultNodes[i] = myPoints[i].node;
689 //================================================================================
691 * \brief Return (unsorted) nodes of the i-th EDGE.
692 * Nodes moved to other geometry by MergeNodes() are also returned.
693 * \retval bool - is OK
695 //================================================================================
697 bool StdMeshers_FaceSide::GetEdgeNodes(size_t i,
698 vector<const SMDS_MeshNode*>& nodes,
699 bool inlude1stVertex,
700 bool inludeLastVertex) const
702 if ( i >= myEdge.size() )
705 SMESH_Mesh* mesh = myProxyMesh->GetMesh();
706 SMESHDS_Mesh* meshDS = mesh->GetMeshDS();
707 SMESHDS_SubMesh* sm = meshDS->MeshElements( myEdge[i] );
709 if ( inlude1stVertex )
711 const SMDS_MeshNode* n0 = VertexNode( i );
712 if ( !n0 ) return false;
713 nodes.push_back( n0 );
716 if ( sm && ( sm->NbElements() > 0 || sm->NbNodes() > 0 ))
718 if ( mesh->HasModificationsToDiscard() ) // check nb of nodes on the EDGE sub-mesh
720 int iQuad = sm->GetElements()->next()->IsQuadratic();
721 int nbExpect = sm->NbElements() - 1 + iQuad * sm->NbElements();
722 if ( nbExpect != sm->NbNodes() ) // some nodes are moved from the EDGE by MergeNodes()
724 // add nodes of all segments
725 typedef set< const SMDS_MeshNode* > TNodeSet;
726 TNodeSet sharedNodes;
727 SMDS_ElemIteratorPtr segIt = sm->GetElements();
728 while ( segIt->more() )
730 const SMDS_MeshElement* seg = segIt->next();
731 if ( seg->GetType() != SMDSAbs_Edge )
733 for ( int i = 0; i < 3-myIgnoreMediumNodes; ++i )
735 const SMDS_MeshNode* n = seg->GetNode( i );
736 if ( i == 2 ) // medium node
738 nodes.push_back( n );
742 pair<TNodeSet::iterator, bool> it2new = sharedNodes.insert( n );
743 if ( !it2new.second ) // n encounters twice == it's on EDGE, not on VERTEX
745 nodes.push_back( n );
746 sharedNodes.erase( it2new.first );
753 if ( nodes.size() < 2 ) // add nodes assigned to the EDGE
755 SMDS_NodeIteratorPtr nItr = sm->GetNodes();
756 while ( nItr->more() )
758 const SMDS_MeshNode* n = nItr->next();
759 if ( myIgnoreMediumNodes && SMESH_MeshEditor::IsMedium( n, SMDSAbs_Edge ))
761 nodes.push_back( n );
764 } // if ( sm && sm->NbElements() > 0 )
766 if ( inludeLastVertex )
768 const SMDS_MeshNode* n1 = VertexNode( i+1 );
769 if ( !n1 ) return false;
770 nodes.push_back( n1 );
775 //================================================================================
777 * \brief Return a node from the i-th VERTEX (count starts from zero)
778 * Nodes moved to other geometry by MergeNodes() are also returned.
779 * \param [in] i - the VERTEX index
780 * \param [out] isMoved - returns \c true if the found node is moved by MergeNodes()
781 * \return const SMDS_MeshNode* - the found node
783 //================================================================================
785 const SMDS_MeshNode* StdMeshers_FaceSide::VertexNode(std::size_t i, bool* isMoved) const
787 TopoDS_Vertex V = ( i >= myEdge.size() ) ? LastVertex() : FirstVertex(i);
789 const SMDS_MeshNode* n = SMESH_Algo::VertexNode( V, myProxyMesh->GetMeshDS() );
791 if ( !n && !myEdge.empty() && myProxyMesh->GetMesh()->HasModificationsToDiscard() )
793 size_t iE = ( i < myEdge.size() ) ? i : myEdge.size()-1;
794 SMESHDS_SubMesh* sm = myProxyMesh->GetMeshDS()->MeshElements( myEdgeID[ iE ]);
796 n = SMESH_Algo::VertexNode( V, sm, myProxyMesh->GetMesh(), /*checkV=*/false );
799 (( i > 0 && i < NbEdges() ) || IsClosed() ))
801 iE = SMESH_MesherHelper::WrapIndex( int(i)-1, NbEdges() );
802 sm = myProxyMesh->GetMeshDS()->MeshElements( myEdgeID[ iE ]);
803 n = SMESH_Algo::VertexNode( V, sm, myProxyMesh->GetMesh(), /*checkV=*/false );
806 if ( n && n->GetPosition()->GetDim() == 1 ) // check that n does not lie on an EDGE of myFace
808 TopoDS_Shape S = SMESH_MesherHelper::GetSubShapeByNode( n, myProxyMesh->GetMeshDS() );
809 if ( SMESH_MesherHelper::IsSubShape( S, myFace ))
810 n = 0; // VERTEX ignored by e.g. Composite Wire Discretization algo
818 //================================================================================
820 * \brief reverse order of vector elements
821 * \param vec - vector to reverse
823 //================================================================================
825 template <typename T > void reverse(vector<T> & vec)
827 std::reverse( vec.begin(), vec.end() );
830 //================================================================================
832 * \brief Change orientation of side geometry
834 //================================================================================
836 void StdMeshers_FaceSide::Reverse()
838 int nbEdges = myEdge.size();
839 for ( int i = nbEdges-1; i >= 0; --i ) {
840 std::swap( myFirst[i], myLast[i] );
841 if ( !myEdge[i].IsNull() )
843 if ( i > 0 ) // at the first loop 1. is overwritten
844 myNormPar[i] = 1 - myNormPar[i-1];
850 //reverse( myC3dAdaptor );
853 reverse( myNormPar );
854 reverse( myEdgeLength );
855 reverse( myIsUniform );
859 myNormPar[nbEdges-1]=1.;
860 if ( !myEdge[0].IsNull() )
862 for ( size_t i = 0; i < myEdge.size(); ++i )
863 reverseProxySubmesh( myEdge[i] );
865 myFalsePoints.clear();
869 for ( size_t i = 0; i < myPoints.size(); ++i )
871 UVPtStruct & uvPt = myPoints[i];
872 uvPt.normParam = 1 - uvPt.normParam;
878 for ( size_t i = 0; i < myFalsePoints.size(); ++i )
880 UVPtStruct & uvPt = myFalsePoints[i];
881 uvPt.normParam = 1 - uvPt.normParam;
885 reverse( myFalsePoints );
888 for ( size_t i = 0; i < myEdge.size(); ++i )
890 if ( myEdge[i].IsNull() ) continue; // for a side on points only
892 Handle(Geom_Curve) C3d = BRep_Tool::Curve(myEdge[i],fp,lp);
894 myC3dAdaptor[i].Load( C3d, fp,lp );
898 //=======================================================================
899 //function : SetIgnoreMediumNodes
900 //purpose : Make ignore medium nodes
901 //=======================================================================
903 void StdMeshers_FaceSide::SetIgnoreMediumNodes(bool toIgnore)
905 if ( myIgnoreMediumNodes != toIgnore )
907 myIgnoreMediumNodes = toIgnore;
909 if ( !myPoints.empty() )
911 UVPtStructVec newPoints;
912 newPoints.reserve( myPoints.size()/2 + 1 );
913 for ( size_t i = 0; i < myPoints.size(); i += 2 )
914 newPoints.push_back( myPoints[i] );
916 myPoints.swap( newPoints );
920 NbPoints( /*update=*/true );
925 //=======================================================================
926 //function : NbPoints
927 //purpose : Return nb nodes on edges and vertices (+1 to be == GetUVPtStruct().size() )
928 // Call it with update == true if mesh of this side can be recomputed
929 // since creation of this side
930 //=======================================================================
932 int StdMeshers_FaceSide::NbPoints(const bool update) const
934 if ( !myPoints.empty() )
935 return myPoints.size();
937 // if ( !myFalsePoints.empty() )
938 // return myFalsePoints.size();
940 if ( update && myEdge.size() > 0 )
942 StdMeshers_FaceSide* me = (StdMeshers_FaceSide*) this;
944 me->myNbSegments = 0;
945 me->myMissingVertexNodes = false;
947 for ( int i = 0; i < NbEdges(); ++i )
949 if ( const SMESHDS_SubMesh* sm = myProxyMesh->GetSubMesh( Edge(i) )) {
950 int nbN = sm->NbNodes();
951 if ( sm->NbElements() > 0 ) {
952 nbN = sm->NbElements() - 1; // nodes can be moved to other shapes by MergeNodes()
953 if ( !myIgnoreMediumNodes &&
954 sm->GetElements()->next()->IsQuadratic() )
955 nbN += sm->NbElements();
957 me->myNbPonits += nbN;
958 me->myNbSegments += sm->NbElements();
962 std::set< const SMDS_MeshNode* > vNodes;
963 for ( int i = 0; i <= NbEdges(); ++i )
964 if ( const SMDS_MeshNode* n = VertexNode( i ))
967 me->myMissingVertexNodes = true;
968 me->myNbPonits += vNodes.size();
971 me->myNbPonits++; // closing node is repeated
976 //=======================================================================
977 //function : NbSegments
978 //purpose : Return nb edges
979 // Call it with update == true if mesh of this side can be recomputed
980 // since creation of this side
981 //=======================================================================
983 int StdMeshers_FaceSide::NbSegments(const bool update) const
985 return NbPoints( update ), myNbSegments;
988 //================================================================================
990 * \brief Reverse UVPtStructVec if a proxy sub-mesh of E
992 //================================================================================
994 void StdMeshers_FaceSide::reverseProxySubmesh( const TopoDS_Edge& E )
996 if ( !myProxyMesh ) return;
997 if ( const SMESH_ProxyMesh::SubMesh* sm = myProxyMesh->GetProxySubMesh( E ))
999 UVPtStructVec& edgeUVPtStruct = (UVPtStructVec& ) sm->GetUVPtStructVec();
1000 for ( size_t i = 0; i < edgeUVPtStruct.size(); ++i )
1002 UVPtStruct & uvPt = edgeUVPtStruct[i];
1003 uvPt.normParam = 1 - uvPt.normParam;
1004 uvPt.x = 1 - uvPt.x;
1005 uvPt.y = 1 - uvPt.y;
1007 reverse( edgeUVPtStruct );
1011 //================================================================================
1013 * \brief Show side features
1015 //================================================================================
1017 void StdMeshers_FaceSide::dump(const char* msg) const
1020 if (msg) MESSAGE ( std::endl << msg );
1021 MESSAGE_BEGIN ("NB EDGES: "<< myEdge.size() );
1022 MESSAGE_ADD ( "nbPoints: "<<myNbPonits<<" vecSize: " << myPoints.size()<<" "<<myFalsePoints.size() );
1023 for ( int i=0; i<myEdge.size(); ++i)
1025 MESSAGE_ADD ( "\t"<<i+1 );
1026 MESSAGE_ADD ( "\tEDGE: " );
1027 if (myEdge[i].IsNull()) {
1028 MESSAGE_ADD ( "NULL" );
1031 TopAbs::Print(myEdge[i].Orientation(),cout)<<" "<<myEdge[i].TShape().operator->()<<endl;
1032 MESSAGE_ADD ( "\tV1: " << TopExp::FirstVertex( myEdge[i], 1).TShape().operator->()
1033 << " V2: " << TopExp::LastVertex( myEdge[i], 1).TShape().operator->() );
1035 MESSAGE_ADD ( "\tC2d: ");
1037 if (myC2d[i].IsNull()) {
1038 MESSAGE_ADD ( "NULL" );
1041 MESSAGE_ADD ( myC2d[i].operator->() );
1044 MESSAGE_ADD ( "\tF: "<<myFirst[i]<< " L: "<< myLast[i] );
1045 MESSAGE_END ( "\tnormPar: "<<myNormPar[i]<<endl );
1050 //================================================================================
1052 * \brief Creates a Adaptor2d_Curve2d to be used in SMESH_Block
1053 * \retval Adaptor2d_Curve2d* -
1055 //================================================================================
1057 struct Adaptor2dCurve2d : public Adaptor2d_Curve2d
1059 const StdMeshers_FaceSide* mySide;
1060 Adaptor2dCurve2d(const StdMeshers_FaceSide* faceSide):mySide(faceSide) {}
1061 gp_Pnt2d Value(const Standard_Real U) const { return mySide->Value2d( U ); }
1062 Standard_Real FirstParameter() const { return 0; }
1063 Standard_Real LastParameter() const { return 1; }
1066 Adaptor2d_Curve2d* StdMeshers_FaceSide::GetCurve2d() const
1068 return new Adaptor2dCurve2d( this );
1071 //================================================================================
1073 * \brief Creates a fully functional Adaptor_Curve
1075 //================================================================================
1077 BRepAdaptor_CompCurve* StdMeshers_FaceSide::GetCurve3d() const
1079 if ( myEdge.empty() )
1083 BRep_Builder aBuilder;
1084 aBuilder.MakeWire(aWire);
1085 for ( int i=0; i<myEdge.size(); ++i )
1086 aBuilder.Add( aWire, myEdge[i] );
1088 if ( myEdge.size() == 2 && IsClosed() )
1089 aWire.Closed(true); // issue 0021141
1091 return new BRepAdaptor_CompCurve( aWire );
1094 //================================================================================
1096 * \brief Return 2D point by normalized parameter
1097 * \param U - normalized parameter value
1098 * \retval gp_Pnt2d - point
1100 //================================================================================
1102 gp_Pnt2d StdMeshers_FaceSide::Value2d(double U) const
1104 if ( !myC2d[0].IsNull() ) {
1105 int i = EdgeIndex( U );
1106 double prevU = i ? myNormPar[ i-1 ] : 0;
1107 double r = ( U - prevU )/ ( myNormPar[ i ] - prevU );
1109 double par = myFirst[i] * ( 1 - r ) + myLast[i] * r;
1111 // check parametrization of curve
1112 if( !myIsUniform[i] )
1114 double aLen3dU = r * myEdgeLength[i] * ( myFirst[i]>myLast[i] ? -1. : 1.);
1115 GCPnts_AbscissaPoint AbPnt
1116 ( const_cast<GeomAdaptor_Curve&>( myC3dAdaptor[i]), aLen3dU, myFirst[i] );
1117 if( AbPnt.IsDone() ) {
1118 par = AbPnt.Parameter();
1121 return myC2d[ i ]->Value(par);
1124 else if ( !myPoints.empty() )
1126 int i = U * double( myPoints.size()-1 );
1127 while ( i > 0 && myPoints[ i ].normParam > U )
1129 while ( i+1 < myPoints.size() && myPoints[ i+1 ].normParam < U )
1131 double r = (( U - myPoints[ i ].normParam ) /
1132 ( myPoints[ i+1 ].normParam - myPoints[ i ].normParam ));
1133 return ( myPoints[ i ].UV() * ( 1 - r ) +
1134 myPoints[ i+1 ].UV() * r );
1136 return myDefaultPnt2d;
1139 //================================================================================
1141 * \brief Return XYZ by normalized parameter
1142 * \param U - normalized parameter value
1143 * \retval gp_Pnt - point
1145 //================================================================================
1147 gp_Pnt StdMeshers_FaceSide::Value3d(double U) const
1149 int i = EdgeIndex( U );
1150 double prevU = i ? myNormPar[ i-1 ] : 0;
1151 double r = ( U - prevU )/ ( myNormPar[ i ] - prevU );
1153 double par = myFirst[i] * ( 1 - r ) + myLast[i] * r;
1155 // check parametrization of curve
1156 if( !myIsUniform[i] )
1158 double aLen3dU = r * myEdgeLength[i] * ( myFirst[i]>myLast[i] ? -1. : 1.);
1159 GCPnts_AbscissaPoint AbPnt
1160 ( const_cast<GeomAdaptor_Curve&>( myC3dAdaptor[i]), aLen3dU, myFirst[i] );
1161 if( AbPnt.IsDone() ) {
1162 par = AbPnt.Parameter();
1165 return myC3dAdaptor[ i ].Value(par);
1168 //================================================================================
1170 * \brief Return wires of a face as StdMeshers_FaceSide's
1172 //================================================================================
1174 TSideVector StdMeshers_FaceSide::GetFaceWires(const TopoDS_Face& theFace,
1175 SMESH_Mesh & theMesh,
1176 const bool theIgnoreMediumNodes,
1178 SMESH_ProxyMesh::Ptr theProxyMesh,
1179 const bool theCheckVertexNodes)
1181 list< TopoDS_Edge > edges, internalEdges;
1182 list< int > nbEdgesInWires;
1183 int nbWires = SMESH_Block::GetOrderedEdges (theFace, edges, nbEdgesInWires);
1185 // split list of all edges into separate wires
1186 TSideVector wires( nbWires );
1187 list< int >::iterator nbE = nbEdgesInWires.begin();
1188 list< TopoDS_Edge >::iterator from = edges.begin(), to = from;
1189 for ( int iW = 0; iW < nbWires; ++iW, ++nbE )
1191 std::advance( to, *nbE );
1192 if ( *nbE == 0 ) // Issue 0020676
1196 wires.resize( nbWires );
1199 list< TopoDS_Edge > wireEdges( from, to );
1200 // assure that there is a node on the first vertex
1201 // as StdMeshers_FaceSide::GetUVPtStruct() requires
1202 if ( wireEdges.front().Orientation() != TopAbs_INTERNAL ) // Issue 0020676
1204 if ( theCheckVertexNodes )
1205 while ( !SMESH_Algo::VertexNode( TopExp::FirstVertex( wireEdges.front(), true),
1206 theMesh.GetMeshDS()))
1208 wireEdges.splice(wireEdges.end(), wireEdges,
1209 wireEdges.begin(), ++wireEdges.begin());
1210 if ( from->IsSame( wireEdges.front() )) {
1212 ( new SMESH_ComputeError(COMPERR_BAD_INPUT_MESH,"No nodes on vertices"));
1213 return TSideVector(0);
1217 else if ( *nbE > 1 ) // Issue 0020676 (Face_pb_netgen.brep) - several internal edges in a wire
1219 internalEdges.splice( internalEdges.end(), wireEdges, ++wireEdges.begin(), wireEdges.end());
1222 StdMeshers_FaceSide* wire = new StdMeshers_FaceSide( theFace, wireEdges, &theMesh,
1223 /*isForward=*/true, theIgnoreMediumNodes,
1225 wires[ iW ] = StdMeshers_FaceSidePtr( wire );
1228 while ( !internalEdges.empty() )
1230 StdMeshers_FaceSide* wire = new StdMeshers_FaceSide( theFace, internalEdges.back(), &theMesh,
1231 /*isForward=*/true, theIgnoreMediumNodes,
1233 wires.push_back( StdMeshers_FaceSidePtr( wire ));
1234 internalEdges.pop_back();
1239 //================================================================================
1241 * \brief Return 1st vertex of the i-the edge
1243 //================================================================================
1245 TopoDS_Vertex StdMeshers_FaceSide::FirstVertex(int i) const
1248 if ( i < NbEdges() )
1250 v = myEdge[i].Orientation() <= TopAbs_REVERSED ? // FORWARD || REVERSED
1251 TopExp::FirstVertex( myEdge[i], 1 ) :
1252 TopoDS::Vertex( TopoDS_Iterator( myEdge[i] ).Value() );
1257 //================================================================================
1259 * \brief Return last vertex of the i-the edge
1261 //================================================================================
1263 TopoDS_Vertex StdMeshers_FaceSide::LastVertex(int i) const
1266 if ( i < NbEdges() )
1268 const TopoDS_Edge& edge = i<0 ? myEdge[ NbEdges() + i ] : myEdge[i];
1269 if ( edge.Orientation() <= TopAbs_REVERSED ) // FORWARD || REVERSED
1270 v = TopExp::LastVertex( edge, 1 );
1272 for ( TopoDS_Iterator vIt( edge ); vIt.More(); vIt.Next() )
1273 v = TopoDS::Vertex( vIt.Value() );
1278 //================================================================================
1280 * \brief Return \c true if the chain of EDGEs is closed
1282 //================================================================================
1284 bool StdMeshers_FaceSide::IsClosed() const
1286 return myEdge.empty() ? false : FirstVertex().IsSame( LastVertex() );