1 // Copyright (C) 2007-2008 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
22 // SMESH SMESH : implementaion of SMESH idl descriptions
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 <BRepAdaptor_Curve.hxx>
43 #include <BRep_Builder.hxx>
44 #include <BRep_Tool.hxx>
46 #include <TopExp_Explorer.hxx>
48 #include <TopoDS_Face.hxx>
49 #include <TopoDS_Vertex.hxx>
50 #include <TopoDS_Wire.hxx>
52 #include <GCPnts_AbscissaPoint.hxx>
53 #include <Geom2dAdaptor_Curve.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)
73 list<TopoDS_Edge> edges(1,theEdge);
74 *this = StdMeshers_FaceSide( theFace, edges, theMesh, theIsForward, theIgnoreMediumNodes );
77 //================================================================================
79 * \brief Constructor of a side of several edges
80 * \param theFace - the face
81 * \param theEdge - the edge
83 //================================================================================
85 StdMeshers_FaceSide::StdMeshers_FaceSide(const TopoDS_Face& theFace,
86 list<TopoDS_Edge>& theEdges,
88 const bool theIsForward,
89 const bool theIgnoreMediumNodes)
91 int nbEdges = theEdges.size();
92 myEdge.resize( nbEdges );
93 myC2d.resize( nbEdges );
94 myFirst.resize( nbEdges );
95 myLast.resize( nbEdges );
96 myNormPar.resize( nbEdges );
98 myNbPonits = myNbSegments = 0;
100 myMissingVertexNodes = false;
101 myIgnoreMediumNodes = theIgnoreMediumNodes;
102 myDefaultPnt2d = gp_Pnt2d( 1e+100, 1e+100 );
103 if ( nbEdges == 0 ) return;
105 SMESHDS_Mesh* meshDS = theMesh->GetMeshDS();
106 vector<double> len( nbEdges );
109 list<TopoDS_Edge>::iterator edge = theEdges.begin();
110 TopoDS_Iterator vExp;
111 for ( int index = 0; edge != theEdges.end(); ++index, ++edge )
113 int i = theIsForward ? index : nbEdges - index - 1;
114 len[i] = SMESH_Algo::EdgeLength( *edge );
115 if ( len[i] < DBL_MIN ) nbDegen++;
118 if ( !theIsForward ) myEdge[i].Reverse();
120 if ( theFace.IsNull() )
121 BRep_Tool::Range( *edge, myFirst[i], myLast[i] );
123 myC2d[i] = BRep_Tool::CurveOnSurface( *edge, theFace, myFirst[i], myLast[i] );
124 if ( myEdge[i].Orientation() == TopAbs_REVERSED )
125 std::swap( myFirst[i], myLast[i] );
127 if ( SMESHDS_SubMesh* sm = meshDS->MeshElements( *edge )) {
128 int nbN = sm->NbNodes();
129 if ( theIgnoreMediumNodes ) {
130 SMDS_ElemIteratorPtr elemIt = sm->GetElements();
131 if ( elemIt->more() && elemIt->next()->IsQuadratic() )
132 nbN -= sm->NbElements();
135 myNbSegments += sm->NbElements();
137 // TopExp::FirstVertex() and TopExp::LastVertex() return NULL from INTERNAL edge
138 vExp.Initialize( *edge );
139 if ( vExp.Value().Orientation() == TopAbs_REVERSED ) vExp.Next();
140 if ( SMESH_Algo::VertexNode( TopoDS::Vertex( vExp.Value()), meshDS ))
141 myNbPonits += 1; // for the first end
143 myMissingVertexNodes = true;
145 vExp.Initialize( theEdges.back() );
146 if ( vExp.Value().Orientation() != TopAbs_REVERSED ) vExp.Next();
149 if ( SMESH_Algo::VertexNode( TopoDS::Vertex( vExp.Value()), meshDS ))
150 myNbPonits++; // for the last end
152 myMissingVertexNodes = true;
154 if ( nbEdges > 1 && myLength > DBL_MIN ) {
155 const double degenNormLen = 1.e-5;
156 double totLength = myLength;
158 totLength += myLength * degenNormLen * nbDegen;
159 double prevNormPar = 0;
160 for ( int i = 0; i < nbEdges; ++i ) {
161 if ( len[ i ] < DBL_MIN )
162 len[ i ] = myLength * degenNormLen;
163 myNormPar[ i ] = prevNormPar + len[i]/totLength;
164 prevNormPar = myNormPar[ i ];
167 myNormPar[nbEdges-1] = 1.;
171 //================================================================================
173 * \brief Constructor of a side for vertex using data from other FaceSide
174 * \param theVertex - the vertex
175 * \param theSide - the side
177 //================================================================================
179 StdMeshers_FaceSide::StdMeshers_FaceSide(const SMDS_MeshNode* theNode,
180 const gp_Pnt2d thePnt2d,
181 const StdMeshers_FaceSide* theSide)
185 myMesh = theSide->GetMesh();
186 myDefaultPnt2d = thePnt2d;
188 myPoints = theSide->GetUVPtStruct();
189 myNbPonits = myNbSegments = myPoints.size();
190 std::vector<uvPtStruct>::iterator it = myPoints.begin();
191 for(; it!=myPoints.end(); it++) {
192 (*it).u = thePnt2d.X();
193 (*it).v = thePnt2d.Y();
195 (*it).node = theNode;
200 //=======================================================================
201 //function : IsUniform
202 //purpose : auxilary function
203 //=======================================================================
204 bool IsUniform(const Handle(Geom2d_Curve)& C2d, double fp, double lp)
206 //cout<<"IsUniform fp = "<<fp<<" lp = "<<lp<<endl;
209 Geom2dAdaptor_Curve A2dC(C2d);
210 double d1 = GCPnts_AbscissaPoint::Length( A2dC, fp, lp );
211 double d2 = GCPnts_AbscissaPoint::Length( A2dC, fp, fp+(lp-fp)/2. );
212 double d4 = GCPnts_AbscissaPoint::Length( A2dC, fp, fp+(lp-fp)/4. );
213 //cout<<"d1 = "<<d1<<" d2 = "<<d2<<" fabs(2*d2/d1-1.0) = "<<fabs(2*d2/d1-1.0)<<endl;
214 if( fabs(2*d2/d1-1.0) > 0.01 || fabs(2*d4/d2-1.0) > 0.01 )
221 //================================================================================
223 * \brief Return info on nodes on the side
224 * \retval UVPtStruct* - array of data structures
226 //================================================================================
228 const vector<UVPtStruct>& StdMeshers_FaceSide::GetUVPtStruct(bool isXConst,
229 double constValue) const
231 if ( myPoints.empty() ) {
233 if ( NbEdges() == 0 ) return myPoints;
235 SMESHDS_Mesh* meshDS = myMesh->GetMeshDS();
237 // sort nodes of all edges putting them into a map
239 map< double, const SMDS_MeshNode*> u2node;
241 for ( int i = 0; i < myEdge.size(); ++i ) {
242 // put 1st vertex node
243 TopoDS_Vertex VV[2]; // TopExp::FirstVertex() returns NULL for INTERNAL edge
244 for ( TopoDS_Iterator vIt(myEdge[i]); vIt.More(); vIt.Next() )
245 VV[ VV[0].IsNull() ? 0 : 1 ] = TopoDS::Vertex(vIt.Value());
246 if ( VV[0].Orientation() == TopAbs_REVERSED ) std::swap ( VV[0], VV[1] );
247 const SMDS_MeshNode* node = SMESH_Algo::VertexNode( VV[0], meshDS );
248 double prevNormPar = ( i == 0 ? 0 : myNormPar[ i-1 ]); // normalized param
249 if ( node ) { // internal nodes may be missing
250 u2node.insert( make_pair( prevNormPar, node ));
253 MESSAGE(" NO NODE on VERTEX" );
257 // put 2nd vertex node for a last edge
258 if ( i+1 == myEdge.size() ) {
259 node = SMESH_Algo::VertexNode( VV[1], meshDS );
261 MESSAGE(" NO NODE on VERTEX" );
264 u2node.insert( make_pair( 1., node ));
267 bool IsUni = IsUniform( myC2d[i], myFirst[i], myLast[i] );
269 // put internal nodes
270 SMESHDS_SubMesh* sm = meshDS->MeshElements( myEdge[i] );
272 SMDS_NodeIteratorPtr nItr = sm->GetNodes();
273 double paramSize = myLast[i] - myFirst[i];
274 double r = myNormPar[i] - prevNormPar;
275 while ( nItr->more() ) {
276 const SMDS_MeshNode* node = nItr->next();
277 if ( myIgnoreMediumNodes && SMESH_MeshEditor::IsMedium( node, SMDSAbs_Edge ))
279 const SMDS_EdgePosition* epos =
280 static_cast<const SMDS_EdgePosition*>(node->GetPosition().get());
281 double u = epos->GetUParameter();
282 // paramSize is signed so orientation is taken into account
284 double normPar = prevNormPar + r * ( u - myFirst[i] ) / paramSize;
288 Handle(Geom_Curve) C3d = BRep_Tool::Curve(myEdge[i],L,fp,lp);
289 GeomAdaptor_Curve A3dC( C3d );
290 double aLen = GCPnts_AbscissaPoint::Length( A3dC, myFirst[i], myLast[i] );
291 double aLenU = GCPnts_AbscissaPoint::Length( A3dC, myFirst[i], u );
292 normPar = prevNormPar + r*aLenU/aLen;
295 if ( normPar > 1 || normPar < 0) {
297 MESSAGE ( "WRONG normPar: "<<normPar<< " prevNormPar="<<prevNormPar
298 << " u="<<u << " myFirst[i]="<<myFirst[i]<< " myLast[i]="<<myLast[i]
299 << " paramSize="<<paramSize );
302 u2node.insert( make_pair( normPar, node ));
305 if ( u2node.size() != myNbPonits ) {
306 MESSAGE("Wrong node parameters on edges, u2node.size():"
307 <<u2node.size()<<" != myNbPonits:"<<myNbPonits);
311 // fill array of UVPtStruct
313 vector<uvPtStruct>* points = const_cast<vector<uvPtStruct>*>( &myPoints );
314 points->resize( myNbPonits );
317 double prevNormPar = 0, paramSize = myNormPar[ EdgeIndex ];
318 map< double, const SMDS_MeshNode*>::iterator u_node = u2node.begin();
319 for (int i = 0 ; u_node != u2node.end(); ++u_node, ++i ) {
320 UVPtStruct & uvPt = (*points)[i];
321 uvPt.node = u_node->second;
322 uvPt.x = uvPt.y = uvPt.normParam = u_node->first;
323 if ( isXConst ) uvPt.x = constValue;
324 else uvPt.y = constValue;
325 if ( myNormPar[ EdgeIndex ] < uvPt.normParam ) {
326 prevNormPar = myNormPar[ EdgeIndex ];
329 if ( EdgeIndex >= myEdge.size() ) {
331 MESSAGE ( "WRONg EdgeIndex " << 1+EdgeIndex
332 << " myNormPar.size()="<<myNormPar.size()
333 << " myNormPar["<< EdgeIndex<<"]="<< myNormPar[ EdgeIndex ]
334 << " uvPt.normParam="<<uvPt.normParam );
337 paramSize = myNormPar[ EdgeIndex ] - prevNormPar;
339 const SMDS_EdgePosition* epos =
340 dynamic_cast<const SMDS_EdgePosition*>(uvPt.node->GetPosition().get());
342 uvPt.param = epos->GetUParameter();
345 double r = ( uvPt.normParam - prevNormPar )/ paramSize;
346 // uvPt.param = myFirst[EdgeIndex] * ( 1 - r ) + myLast[EdgeIndex] * r;
347 uvPt.param = ( r > 0.5 ? myLast[EdgeIndex] : myFirst[EdgeIndex] );
349 if ( !myC2d[ EdgeIndex ].IsNull() ) {
350 gp_Pnt2d p = myC2d[ EdgeIndex ]->Value( uvPt.param );
355 uvPt.u = uvPt.v = 1e+100;
362 //================================================================================
364 * \brief Falsificate info on nodes
365 * \param nbSeg - nb of segments on the side
366 * \retval UVPtStruct* - array of data structures
368 //================================================================================
370 const vector<UVPtStruct>& StdMeshers_FaceSide::SimulateUVPtStruct(int nbSeg,
372 double constValue) const
374 if ( myFalsePoints.empty() ) {
376 if ( NbEdges() == 0 ) return myFalsePoints;
378 vector<uvPtStruct>* points = const_cast<vector<uvPtStruct>*>( &myFalsePoints );
379 points->resize( nbSeg+1 );
382 double prevNormPar = 0, paramSize = myNormPar[ EdgeIndex ];
383 for (int i = 0 ; i < myFalsePoints.size(); ++i ) {
384 double normPar = double(i) / double(nbSeg);
385 UVPtStruct & uvPt = (*points)[i];
387 uvPt.x = uvPt.y = uvPt.param = uvPt.normParam = normPar;
388 if ( isXConst ) uvPt.x = constValue;
389 else uvPt.y = constValue;
390 if ( myNormPar[ EdgeIndex ] < normPar ) {
391 prevNormPar = myNormPar[ EdgeIndex ];
393 paramSize = myNormPar[ EdgeIndex ] - prevNormPar;
395 double r = ( normPar - prevNormPar )/ paramSize;
396 uvPt.param = myFirst[EdgeIndex] * ( 1 - r ) + myLast[EdgeIndex] * r;
397 if ( !myC2d[ EdgeIndex ].IsNull() ) {
398 gp_Pnt2d p = myC2d[ EdgeIndex ]->Value( uvPt.param );
403 uvPt.u = uvPt.v = 1e+100;
407 return myFalsePoints;
409 // gp_Pnt StdMeshers_FaceSide::Value(double U) const
413 //================================================================================
415 * \brief reverse order of vector elements
416 * \param vec - vector to reverse
418 //================================================================================
420 template <typename T > void reverse(vector<T> & vec)
422 for ( int f=0, r=vec.size()-1; f < r; ++f, --r )
423 std::swap( vec[f], vec[r] );
426 //================================================================================
428 * \brief Change orientation of side geometry
430 //================================================================================
432 void StdMeshers_FaceSide::Reverse()
434 int nbEdges = myEdge.size();
435 for ( int i = nbEdges-1; i >= 0; --i ) {
436 std::swap( myFirst[i], myLast[i] );
438 if ( i > 0 ) // at the first loop 1. is overwritten
439 myNormPar[i] = 1 - myNormPar[i-1];
446 reverse( myNormPar );
448 myNormPar[nbEdges-1]=1.;
450 myFalsePoints.clear();
453 //================================================================================
455 * \brief Show side features
457 //================================================================================
459 void StdMeshers_FaceSide::dump(const char* msg) const
462 if (msg) MESSAGE ( std::endl << msg );
463 MESSAGE_BEGIN ("NB EDGES: "<< myEdge.size() );
464 MESSAGE_ADD ( "nbPoints: "<<myNbPonits<<" vecSize: " << myPoints.size()<<" "<<myFalsePoints.size() );
465 for ( int i=0; i<myEdge.size(); ++i)
467 MESSAGE_ADD ( "\t"<<i+1 );
468 MESSAGE_ADD ( "\tEDGE: " );
469 if (myEdge[i].IsNull()) {
470 MESSAGE_ADD ( "NULL" );
473 TopAbs::Print(myEdge[i].Orientation(),cout)<<" "<<myEdge[i].TShape().operator->()<<endl;
474 MESSAGE_ADD ( "\tV1: " << TopExp::FirstVertex( myEdge[i], 1).TShape().operator->()
475 << " V2: " << TopExp::LastVertex( myEdge[i], 1).TShape().operator->() );
477 MESSAGE_ADD ( "\tC2d: ");
479 if (myC2d[i].IsNull()) {
480 MESSAGE_ADD ( "NULL" );
483 MESSAGE_ADD ( myC2d[i].operator->() );
486 MESSAGE_ADD ( "\tF: "<<myFirst[i]<< " L: "<< myLast[i] );
487 MESSAGE_END ( "\tnormPar: "<<myNormPar[i]<<endl );
492 //================================================================================
494 * \brief Creates a Adaptor2d_Curve2d to be used in SMESH_Block
495 * \retval Adaptor2d_Curve2d* -
497 //================================================================================
499 struct Adaptor2dCurve2d : public Adaptor2d_Curve2d
501 const StdMeshers_FaceSide* mySide;
502 Adaptor2dCurve2d(const StdMeshers_FaceSide* faceSide):mySide(faceSide) {}
503 gp_Pnt2d Value(const Standard_Real U) const { return mySide->Value2d( U ); }
504 Standard_Real FirstParameter() const { return 0; }
505 Standard_Real LastParameter() const { return 1; }
508 Adaptor2d_Curve2d* StdMeshers_FaceSide::GetCurve2d() const
510 return new Adaptor2dCurve2d( this );
513 //================================================================================
515 * \brief Creates a fully functional Adaptor_Curve
517 //================================================================================
519 BRepAdaptor_CompCurve* StdMeshers_FaceSide::GetCurve3d() const
521 if ( myEdge.empty() )
524 // if ( myEdge.size() == 1 )
525 // return new BRepAdaptor_Curve( myEdge[0] );
528 BRep_Builder aBuilder;
529 aBuilder.MakeWire(aWire);
530 for ( int i=0; i<myEdge.size(); ++i )
531 aBuilder.Add( aWire, myEdge[i] );
532 return new BRepAdaptor_CompCurve( aWire );
536 //================================================================================
538 * \brief Return 2D point by normalized parameter
539 * \param U - normalized parameter value
540 * \retval gp_Pnt2d - point
542 //================================================================================
544 gp_Pnt2d StdMeshers_FaceSide::Value2d(double U) const
546 if ( !myC2d[0].IsNull() ) {
547 int i = EdgeIndex( U );
548 double prevU = i ? myNormPar[ i-1 ] : 0;
549 double r = ( U - prevU )/ ( myNormPar[ i ] - prevU );
551 double par = myFirst[i] * ( 1 - r ) + myLast[i] * r;
553 // check parametrization of curve
554 if( !IsUniform( myC2d[i], myFirst[i], myLast[i] ) ) {
557 Handle(Geom_Curve) C3d = BRep_Tool::Curve(myEdge[i],L,fp,lp);
560 GeomAdaptor_Curve A3dC( C3d );
561 double aLen3d = GCPnts_AbscissaPoint::Length( A3dC, fp, lp );
562 double aLen3dU = aLen3d*r;
566 GCPnts_AbscissaPoint AbPnt( A3dC, aLen3dU, fp );
567 if( AbPnt.IsDone() ) {
568 par = AbPnt.Parameter();
571 return myC2d[ i ]->Value(par);
574 //return gp_Pnt2d( 1e+100, 1e+100 );
575 return myDefaultPnt2d;
578 //================================================================================
580 * \brief Return wires of a face as StdMeshers_FaceSide's
582 //================================================================================
584 TSideVector StdMeshers_FaceSide::GetFaceWires(const TopoDS_Face& theFace,
585 SMESH_Mesh & theMesh,
586 const bool theIgnoreMediumNodes,
590 list< TopoDS_Edge > edges;
591 list< int > nbEdgesInWires;
592 int nbWires = SMESH_Block::GetOrderedEdges (theFace, V1, edges, nbEdgesInWires);
594 // split list of all edges into separate wires
595 TSideVector wires( nbWires );
596 list< int >::iterator nbE = nbEdgesInWires.begin();
597 list< TopoDS_Edge >::iterator from, to;
598 from = to = edges.begin();
599 for ( int iW = 0; iW < nbWires; ++iW )
601 std::advance( to, *nbE++ );
602 if ( *nbE == 0 ) // Issue 0020676
606 wires.resize( nbWires );
609 list< TopoDS_Edge > wireEdges( from, to );
610 // assure that there is a node on the first vertex
611 // as StdMeshers_FaceSide::GetUVPtStruct() requires
612 if ( wireEdges.front().Orientation() != TopAbs_INTERNAL ) // Issue 0020676
613 while ( !SMESH_Algo::VertexNode( TopExp::FirstVertex( wireEdges.front(), true),
614 theMesh.GetMeshDS()))
616 wireEdges.splice(wireEdges.end(), wireEdges,
617 wireEdges.begin(), ++wireEdges.begin());
618 if ( from->IsSame( wireEdges.front() )) {
620 ( new SMESH_ComputeError(COMPERR_BAD_INPUT_MESH,"No nodes on vertices"));
621 return TSideVector(0);
624 const bool isForward = true;
625 StdMeshers_FaceSide* wire = new StdMeshers_FaceSide( theFace, wireEdges, &theMesh,
626 isForward, theIgnoreMediumNodes);
627 wires[ iW ] = StdMeshers_FaceSidePtr( wire );
633 //================================================================================
635 * \brief Return 1st vertex of the i-the edge
637 //================================================================================
639 TopoDS_Vertex StdMeshers_FaceSide::FirstVertex(int i) const
644 v = myEdge[i].Orientation() <= TopAbs_REVERSED ? // FORWARD || REVERSED
645 TopExp::FirstVertex( myEdge[i], 1 ) :
646 TopoDS::Vertex( TopoDS_Iterator( myEdge[i] ).Value() );
651 //================================================================================
653 * \brief Return last vertex of the i-the edge
655 //================================================================================
657 TopoDS_Vertex StdMeshers_FaceSide::LastVertex(int i) const
662 const TopoDS_Edge& edge = i<0 ? myEdge[ NbEdges() + i ] : myEdge[i];
663 if ( edge.Orientation() <= TopAbs_REVERSED ) // FORWARD || REVERSED
664 v = TopExp::LastVertex( edge, 1 );
666 for ( TopoDS_Iterator vIt( edge ); vIt.More(); vIt.Next() )
667 v = TopoDS::Vertex( vIt.Value() );