1 // Copyright (C) 2007-2010 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.
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 // SMESH SMESH : implementaion of SMESH idl descriptions
24 // File : StdMeshers_FaceSide.hxx
25 // Created : Wed Jan 31 18:41:25 2007
26 // Author : Edward AGAPOV (eap)
29 #include "StdMeshers_FaceSide.hxx"
31 #include "SMDS_EdgePosition.hxx"
32 #include "SMDS_MeshNode.hxx"
33 #include "SMESHDS_Mesh.hxx"
34 #include "SMESHDS_SubMesh.hxx"
35 #include "SMESH_Algo.hxx"
36 #include "SMESH_Mesh.hxx"
37 #include "SMESH_MesherHelper.hxx"
38 #include "SMESH_ComputeError.hxx"
39 #include "SMESH_Block.hxx"
41 #include <Adaptor2d_Curve2d.hxx>
42 #include <BRepAdaptor_CompCurve.hxx>
43 #include <BRepAdaptor_Curve.hxx>
44 #include <BRep_Builder.hxx>
45 #include <BRep_Tool.hxx>
47 #include <TopExp_Explorer.hxx>
49 #include <TopoDS_Face.hxx>
50 #include <TopoDS_Vertex.hxx>
51 #include <TopoDS_Wire.hxx>
53 #include <GCPnts_AbscissaPoint.hxx>
54 #include <Geom2dAdaptor_Curve.hxx>
58 #include "utilities.h"
60 //================================================================================
62 * \brief Constructor of a side of one edge
63 * \param theFace - the face
64 * \param theEdge - the edge
66 //================================================================================
68 StdMeshers_FaceSide::StdMeshers_FaceSide(const TopoDS_Face& theFace,
69 const TopoDS_Edge& theEdge,
71 const bool theIsForward,
72 const bool theIgnoreMediumNodes)
74 list<TopoDS_Edge> edges(1,theEdge);
75 *this = StdMeshers_FaceSide( theFace, edges, theMesh, theIsForward, theIgnoreMediumNodes );
78 //================================================================================
80 * \brief Constructor of a side of several edges
81 * \param theFace - the face
82 * \param theEdge - the edge
84 //================================================================================
86 StdMeshers_FaceSide::StdMeshers_FaceSide(const TopoDS_Face& theFace,
87 list<TopoDS_Edge>& theEdges,
89 const bool theIsForward,
90 const bool theIgnoreMediumNodes)
92 int nbEdges = theEdges.size();
93 myEdge.resize( nbEdges );
94 myC2d.resize( nbEdges );
95 myC3dAdaptor.resize( nbEdges );
96 myFirst.resize( nbEdges );
97 myLast.resize( nbEdges );
98 myNormPar.resize( nbEdges );
99 myEdgeLength.resize( nbEdges );
100 myIsUniform.resize( nbEdges );
102 myNbPonits = myNbSegments = 0;
104 myMissingVertexNodes = false;
105 myIgnoreMediumNodes = theIgnoreMediumNodes;
106 myDefaultPnt2d = gp_Pnt2d( 1e+100, 1e+100 );
107 if ( nbEdges == 0 ) return;
109 SMESHDS_Mesh* meshDS = theMesh->GetMeshDS();
112 list<TopoDS_Edge>::iterator edge = theEdges.begin();
113 TopoDS_Iterator vExp;
114 for ( int index = 0; edge != theEdges.end(); ++index, ++edge )
116 int i = theIsForward ? index : nbEdges - index - 1;
117 myEdgeLength[i] = SMESH_Algo::EdgeLength( *edge );
118 if ( myEdgeLength[i] < DBL_MIN ) nbDegen++;
119 myLength += myEdgeLength[i];
121 if ( !theIsForward ) myEdge[i].Reverse();
123 if ( theFace.IsNull() )
124 BRep_Tool::Range( *edge, myFirst[i], myLast[i] );
126 myC2d[i] = BRep_Tool::CurveOnSurface( *edge, theFace, myFirst[i], myLast[i] );
127 if ( myEdge[i].Orientation() == TopAbs_REVERSED )
128 std::swap( myFirst[i], myLast[i] );
130 if ( SMESHDS_SubMesh* sm = meshDS->MeshElements( *edge )) {
131 int nbN = sm->NbNodes();
132 if ( theIgnoreMediumNodes ) {
133 SMDS_ElemIteratorPtr elemIt = sm->GetElements();
134 if ( elemIt->more() && elemIt->next()->IsQuadratic() )
135 nbN -= sm->NbElements();
138 myNbSegments += sm->NbElements();
140 // TopExp::FirstVertex() and TopExp::LastVertex() return NULL from INTERNAL edge
141 vExp.Initialize( *edge );
142 if ( vExp.Value().Orientation() == TopAbs_REVERSED ) vExp.Next();
143 if ( SMESH_Algo::VertexNode( TopoDS::Vertex( vExp.Value()), meshDS ))
144 myNbPonits += 1; // for the first end
146 myMissingVertexNodes = true;
148 // check if edge has non-uniform parametrization (issue 0020705)
149 if ( !myC2d[i].IsNull() )
151 Geom2dAdaptor_Curve A2dC( myC2d[i] );
152 double p2 = myFirst[i]+(myLast[i]-myFirst[i])/2., p4 = myFirst[i]+(myLast[i]-myFirst[i])/4.;
153 double d2 = GCPnts_AbscissaPoint::Length( A2dC, myFirst[i], p2 );
154 double d4 = GCPnts_AbscissaPoint::Length( A2dC, myFirst[i], p4 );
155 //cout<<"len = "<<len<<" d2 = "<<d2<<" fabs(2*d2/len-1.0) = "<<fabs(2*d2/len-1.0)<<endl;
156 myIsUniform[i] = !( fabs(2*d2/myEdgeLength[i]-1.0) > 0.01 || fabs(2*d4/d2-1.0) > 0.01 );
157 if ( !myIsUniform[i] )
161 Handle(Geom_Curve) C3d = BRep_Tool::Curve(myEdge[i],L,fp,lp);
162 myC3dAdaptor[i].Load( C3d, fp,lp );
166 vExp.Initialize( theEdges.back() );
167 if ( vExp.Value().Orientation() != TopAbs_REVERSED ) vExp.Next();
170 if ( SMESH_Algo::VertexNode( TopoDS::Vertex( vExp.Value()), meshDS ))
171 myNbPonits++; // for the last end
173 myMissingVertexNodes = true;
175 if ( nbEdges > 1 && myLength > DBL_MIN ) {
176 const double degenNormLen = 1.e-5;
177 double totLength = myLength;
179 totLength += myLength * degenNormLen * nbDegen;
180 double prevNormPar = 0;
181 for ( int i = 0; i < nbEdges; ++i ) {
182 if ( myEdgeLength[ i ] < DBL_MIN )
183 myEdgeLength[ i ] = myLength * degenNormLen;
184 myNormPar[ i ] = prevNormPar + myEdgeLength[i]/totLength;
185 prevNormPar = myNormPar[ i ];
188 myNormPar[nbEdges-1] = 1.;
192 //================================================================================
194 * \brief Constructor of a side for vertex using data from other FaceSide
195 * \param theVertex - the vertex
196 * \param theSide - the side
198 //================================================================================
200 StdMeshers_FaceSide::StdMeshers_FaceSide(const SMDS_MeshNode* theNode,
201 const gp_Pnt2d thePnt2d,
202 const StdMeshers_FaceSide* theSide)
206 myMesh = theSide->GetMesh();
207 myDefaultPnt2d = thePnt2d;
209 myPoints = theSide->GetUVPtStruct();
210 myNbPonits = myNbSegments = myPoints.size();
211 std::vector<uvPtStruct>::iterator it = myPoints.begin();
212 for(; it!=myPoints.end(); it++) {
213 (*it).u = thePnt2d.X();
214 (*it).v = thePnt2d.Y();
216 (*it).node = theNode;
220 //================================================================================
222 * \brief Return info on nodes on the side
223 * \retval UVPtStruct* - array of data structures
225 //================================================================================
227 const vector<UVPtStruct>& StdMeshers_FaceSide::GetUVPtStruct(bool isXConst,
228 double constValue) const
230 if ( myPoints.empty() ) {
232 if ( NbEdges() == 0 ) return myPoints;
234 SMESHDS_Mesh* meshDS = myMesh->GetMeshDS();
235 SMESH_MesherHelper helper(*myMesh);
238 // sort nodes of all edges putting them into a map
240 map< double, const SMDS_MeshNode*> u2node;
242 for ( int i = 0; i < myEdge.size(); ++i ) {
243 // put 1st vertex node
244 TopoDS_Vertex VV[2]; // TopExp::FirstVertex() returns NULL for INTERNAL edge
245 for ( TopoDS_Iterator vIt(myEdge[i]); vIt.More(); vIt.Next() )
246 VV[ VV[0].IsNull() ? 0 : 1 ] = TopoDS::Vertex(vIt.Value());
247 if ( VV[0].Orientation() == TopAbs_REVERSED ) std::swap ( VV[0], VV[1] );
248 const SMDS_MeshNode* node = SMESH_Algo::VertexNode( VV[0], meshDS );
249 double prevNormPar = ( i == 0 ? 0 : myNormPar[ i-1 ]); // normalized param
250 if ( node ) { // internal nodes may be missing
251 u2node.insert( make_pair( prevNormPar, node ));
254 MESSAGE(" NO NODE on VERTEX" );
258 // put 2nd vertex node for a last edge
259 if ( i+1 == myEdge.size() ) {
260 node = SMESH_Algo::VertexNode( VV[1], meshDS );
262 MESSAGE(" NO NODE on VERTEX" );
265 u2node.insert( make_pair( 1., node ));
268 // put internal nodes
269 SMESHDS_SubMesh* sm = meshDS->MeshElements( myEdge[i] );
271 SMDS_NodeIteratorPtr nItr = sm->GetNodes();
272 double paramSize = myLast[i] - myFirst[i];
273 double r = myNormPar[i] - prevNormPar;
274 while ( nItr->more() )
276 const SMDS_MeshNode* node = nItr->next();
277 if ( myIgnoreMediumNodes && SMESH_MeshEditor::IsMedium( node, SMDSAbs_Edge ))
279 double u = helper.GetNodeU( myEdge[i], node, ¶mOK );
281 // paramSize is signed so orientation is taken into account
282 double normPar = prevNormPar + r * ( u - myFirst[i] ) / paramSize;
285 double aLenU = GCPnts_AbscissaPoint::Length
286 ( const_cast<GeomAdaptor_Curve&>( myC3dAdaptor[i]), myFirst[i], u );
287 if ( myEdgeLength[i] > aLenU ) // nonregression test "3D_mesh_NETGEN/G6"
288 normPar = prevNormPar + r*aLenU/myEdgeLength[i];
291 if ( normPar > 1 || normPar < 0) {
293 MESSAGE ( "WRONG normPar: "<<normPar<< " prevNormPar="<<prevNormPar
294 << " u="<<u << " myFirst[i]="<<myFirst[i]<< " myLast[i]="<<myLast[i]
295 << " paramSize="<<paramSize );
298 u2node.insert( make_pair( normPar, node ));
301 if ( u2node.size() != myNbPonits ) {
302 MESSAGE("Wrong node parameters on edges, u2node.size():"
303 <<u2node.size()<<" != myNbPonits:"<<myNbPonits);
307 // fill array of UVPtStruct
309 vector<uvPtStruct>* points = const_cast<vector<uvPtStruct>*>( &myPoints );
310 points->resize( myNbPonits );
313 double prevNormPar = 0, paramSize = myNormPar[ EdgeIndex ];
314 map< double, const SMDS_MeshNode*>::iterator u_node = u2node.begin();
315 for (int i = 0 ; u_node != u2node.end(); ++u_node, ++i ) {
316 UVPtStruct & uvPt = (*points)[i];
317 uvPt.node = u_node->second;
318 uvPt.x = uvPt.y = uvPt.normParam = u_node->first;
319 if ( isXConst ) uvPt.x = constValue;
320 else uvPt.y = constValue;
321 if ( myNormPar[ EdgeIndex ] < uvPt.normParam ) {
322 prevNormPar = myNormPar[ EdgeIndex ];
325 if ( EdgeIndex >= myEdge.size() ) {
327 MESSAGE ( "WRONg EdgeIndex " << 1+EdgeIndex
328 << " myNormPar.size()="<<myNormPar.size()
329 << " myNormPar["<< EdgeIndex<<"]="<< myNormPar[ EdgeIndex ]
330 << " uvPt.normParam="<<uvPt.normParam );
333 paramSize = myNormPar[ EdgeIndex ] - prevNormPar;
335 const SMDS_EdgePosition* epos =
336 dynamic_cast<const SMDS_EdgePosition*>(uvPt.node->GetPosition().get());
338 uvPt.param = epos->GetUParameter();
341 double r = ( uvPt.normParam - prevNormPar )/ paramSize;
342 // uvPt.param = myFirst[EdgeIndex] * ( 1 - r ) + myLast[EdgeIndex] * r;
343 uvPt.param = ( r > 0.5 ? myLast[EdgeIndex] : myFirst[EdgeIndex] );
345 if ( !myC2d[ EdgeIndex ].IsNull() ) {
346 gp_Pnt2d p = myC2d[ EdgeIndex ]->Value( uvPt.param );
351 uvPt.u = uvPt.v = 1e+100;
358 //================================================================================
360 * \brief Falsificate info on nodes
361 * \param nbSeg - nb of segments on the side
362 * \retval UVPtStruct* - array of data structures
364 //================================================================================
366 const vector<UVPtStruct>& StdMeshers_FaceSide::SimulateUVPtStruct(int nbSeg,
368 double constValue) const
370 if ( myFalsePoints.empty() ) {
372 if ( NbEdges() == 0 ) return myFalsePoints;
374 vector<uvPtStruct>* points = const_cast<vector<uvPtStruct>*>( &myFalsePoints );
375 points->resize( nbSeg+1 );
378 double prevNormPar = 0, paramSize = myNormPar[ EdgeIndex ];
379 for (int i = 0 ; i < myFalsePoints.size(); ++i ) {
380 double normPar = double(i) / double(nbSeg);
381 UVPtStruct & uvPt = (*points)[i];
383 uvPt.x = uvPt.y = uvPt.param = uvPt.normParam = normPar;
384 if ( isXConst ) uvPt.x = constValue;
385 else uvPt.y = constValue;
386 if ( myNormPar[ EdgeIndex ] < normPar ) {
387 prevNormPar = myNormPar[ EdgeIndex ];
389 paramSize = myNormPar[ EdgeIndex ] - prevNormPar;
391 double r = ( normPar - prevNormPar )/ paramSize;
392 uvPt.param = myFirst[EdgeIndex] * ( 1 - r ) + myLast[EdgeIndex] * r;
393 if ( !myC2d[ EdgeIndex ].IsNull() ) {
394 gp_Pnt2d p = myC2d[ EdgeIndex ]->Value( uvPt.param );
399 uvPt.u = uvPt.v = 1e+100;
403 return myFalsePoints;
405 // gp_Pnt StdMeshers_FaceSide::Value(double U) const
409 //================================================================================
411 * \brief reverse order of vector elements
412 * \param vec - vector to reverse
414 //================================================================================
416 template <typename T > void reverse(vector<T> & vec)
418 for ( int f=0, r=vec.size()-1; f < r; ++f, --r )
419 std::swap( vec[f], vec[r] );
422 //================================================================================
424 * \brief Change orientation of side geometry
426 //================================================================================
428 void StdMeshers_FaceSide::Reverse()
430 int nbEdges = myEdge.size();
431 for ( int i = nbEdges-1; i >= 0; --i ) {
432 std::swap( myFirst[i], myLast[i] );
434 if ( i > 0 ) // at the first loop 1. is overwritten
435 myNormPar[i] = 1 - myNormPar[i-1];
442 reverse( myNormPar );
444 myNormPar[nbEdges-1]=1.;
446 myFalsePoints.clear();
449 //================================================================================
451 * \brief Show side features
453 //================================================================================
455 void StdMeshers_FaceSide::dump(const char* msg) const
458 if (msg) MESSAGE ( std::endl << msg );
459 MESSAGE_BEGIN ("NB EDGES: "<< myEdge.size() );
460 MESSAGE_ADD ( "nbPoints: "<<myNbPonits<<" vecSize: " << myPoints.size()<<" "<<myFalsePoints.size() );
461 for ( int i=0; i<myEdge.size(); ++i)
463 MESSAGE_ADD ( "\t"<<i+1 );
464 MESSAGE_ADD ( "\tEDGE: " );
465 if (myEdge[i].IsNull()) {
466 MESSAGE_ADD ( "NULL" );
469 TopAbs::Print(myEdge[i].Orientation(),cout)<<" "<<myEdge[i].TShape().operator->()<<endl;
470 MESSAGE_ADD ( "\tV1: " << TopExp::FirstVertex( myEdge[i], 1).TShape().operator->()
471 << " V2: " << TopExp::LastVertex( myEdge[i], 1).TShape().operator->() );
473 MESSAGE_ADD ( "\tC2d: ");
475 if (myC2d[i].IsNull()) {
476 MESSAGE_ADD ( "NULL" );
479 MESSAGE_ADD ( myC2d[i].operator->() );
482 MESSAGE_ADD ( "\tF: "<<myFirst[i]<< " L: "<< myLast[i] );
483 MESSAGE_END ( "\tnormPar: "<<myNormPar[i]<<endl );
488 //================================================================================
490 * \brief Creates a Adaptor2d_Curve2d to be used in SMESH_Block
491 * \retval Adaptor2d_Curve2d* -
493 //================================================================================
495 struct Adaptor2dCurve2d : public Adaptor2d_Curve2d
497 const StdMeshers_FaceSide* mySide;
498 Adaptor2dCurve2d(const StdMeshers_FaceSide* faceSide):mySide(faceSide) {}
499 gp_Pnt2d Value(const Standard_Real U) const { return mySide->Value2d( U ); }
500 Standard_Real FirstParameter() const { return 0; }
501 Standard_Real LastParameter() const { return 1; }
504 Adaptor2d_Curve2d* StdMeshers_FaceSide::GetCurve2d() const
506 return new Adaptor2dCurve2d( this );
509 //================================================================================
511 * \brief Creates a fully functional Adaptor_Curve
513 //================================================================================
515 BRepAdaptor_CompCurve* StdMeshers_FaceSide::GetCurve3d() const
517 if ( myEdge.empty() )
520 // if ( myEdge.size() == 1 )
521 // return new BRepAdaptor_Curve( myEdge[0] );
524 BRep_Builder aBuilder;
525 aBuilder.MakeWire(aWire);
526 for ( int i=0; i<myEdge.size(); ++i )
527 aBuilder.Add( aWire, myEdge[i] );
528 return new BRepAdaptor_CompCurve( aWire );
532 //================================================================================
534 * \brief Return 2D point by normalized parameter
535 * \param U - normalized parameter value
536 * \retval gp_Pnt2d - point
538 //================================================================================
540 gp_Pnt2d StdMeshers_FaceSide::Value2d(double U) const
542 if ( !myC2d[0].IsNull() ) {
543 int i = EdgeIndex( U );
544 double prevU = i ? myNormPar[ i-1 ] : 0;
545 double r = ( U - prevU )/ ( myNormPar[ i ] - prevU );
547 double par = myFirst[i] * ( 1 - r ) + myLast[i] * r;
549 // check parametrization of curve
550 if( !myIsUniform[i] )
552 double aLen3dU = r * myEdgeLength[i] * ( myFirst[i]>myLast[i] ? -1. : 1.);
553 GCPnts_AbscissaPoint AbPnt
554 ( const_cast<GeomAdaptor_Curve&>( myC3dAdaptor[i]), aLen3dU, myFirst[i] );
555 if( AbPnt.IsDone() ) {
556 par = AbPnt.Parameter();
559 return myC2d[ i ]->Value(par);
562 return myDefaultPnt2d;
565 //================================================================================
567 * \brief Return wires of a face as StdMeshers_FaceSide's
569 //================================================================================
571 TSideVector StdMeshers_FaceSide::GetFaceWires(const TopoDS_Face& theFace,
572 SMESH_Mesh & theMesh,
573 const bool theIgnoreMediumNodes,
577 list< TopoDS_Edge > edges;
578 list< int > nbEdgesInWires;
579 int nbWires = SMESH_Block::GetOrderedEdges (theFace, V1, edges, nbEdgesInWires);
581 // split list of all edges into separate wires
582 TSideVector wires( nbWires );
583 list< int >::iterator nbE = nbEdgesInWires.begin();
584 list< TopoDS_Edge >::iterator from, to;
585 from = to = edges.begin();
586 for ( int iW = 0; iW < nbWires; ++iW )
588 std::advance( to, *nbE++ );
589 if ( *nbE == 0 ) // Issue 0020676
593 wires.resize( nbWires );
596 list< TopoDS_Edge > wireEdges( from, to );
597 // assure that there is a node on the first vertex
598 // as StdMeshers_FaceSide::GetUVPtStruct() requires
599 if ( wireEdges.front().Orientation() != TopAbs_INTERNAL ) // Issue 0020676
600 while ( !SMESH_Algo::VertexNode( TopExp::FirstVertex( wireEdges.front(), true),
601 theMesh.GetMeshDS()))
603 wireEdges.splice(wireEdges.end(), wireEdges,
604 wireEdges.begin(), ++wireEdges.begin());
605 if ( from->IsSame( wireEdges.front() )) {
607 ( new SMESH_ComputeError(COMPERR_BAD_INPUT_MESH,"No nodes on vertices"));
608 return TSideVector(0);
611 const bool isForward = true;
612 StdMeshers_FaceSide* wire = new StdMeshers_FaceSide( theFace, wireEdges, &theMesh,
613 isForward, theIgnoreMediumNodes);
614 wires[ iW ] = StdMeshers_FaceSidePtr( wire );
620 //================================================================================
622 * \brief Return 1st vertex of the i-the edge
624 //================================================================================
626 TopoDS_Vertex StdMeshers_FaceSide::FirstVertex(int i) const
631 v = myEdge[i].Orientation() <= TopAbs_REVERSED ? // FORWARD || REVERSED
632 TopExp::FirstVertex( myEdge[i], 1 ) :
633 TopoDS::Vertex( TopoDS_Iterator( myEdge[i] ).Value() );
638 //================================================================================
640 * \brief Return last vertex of the i-the edge
642 //================================================================================
644 TopoDS_Vertex StdMeshers_FaceSide::LastVertex(int i) const
649 const TopoDS_Edge& edge = i<0 ? myEdge[ NbEdges() + i ] : myEdge[i];
650 if ( edge.Orientation() <= TopAbs_REVERSED ) // FORWARD || REVERSED
651 v = TopExp::LastVertex( edge, 1 );
653 for ( TopoDS_Iterator vIt( edge ); vIt.More(); vIt.Next() )
654 v = TopoDS::Vertex( vIt.Value() );