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 vExp.Initialize( *edge );
138 if ( SMESH_Algo::VertexNode( TopoDS::Vertex( vExp.Value()), meshDS ))
139 myNbPonits += 1; // for the first end
141 myMissingVertexNodes = true;
146 if ( SMESH_Algo::VertexNode( TopoDS::Vertex( vExp.Value()), meshDS ))
147 myNbPonits++; // for the last end
149 myMissingVertexNodes = true;
151 if ( nbEdges > 1 && myLength > DBL_MIN ) {
152 const double degenNormLen = 1.e-5;
153 double totLength = myLength;
155 totLength += myLength * degenNormLen * nbDegen;
156 double prevNormPar = 0;
157 for ( int i = 0; i < nbEdges; ++i ) {
158 if ( len[ i ] < DBL_MIN )
159 len[ i ] = myLength * degenNormLen;
160 myNormPar[ i ] = prevNormPar + len[i]/totLength;
161 prevNormPar = myNormPar[ i ];
164 myNormPar[nbEdges-1] = 1.;
168 //================================================================================
170 * \brief Constructor of a side for vertex using data from other FaceSide
171 * \param theVertex - the vertex
172 * \param theSide - the side
174 //================================================================================
176 StdMeshers_FaceSide::StdMeshers_FaceSide(const SMDS_MeshNode* theNode,
177 const gp_Pnt2d thePnt2d,
178 const StdMeshers_FaceSide* theSide)
182 myMesh = theSide->GetMesh();
183 myDefaultPnt2d = thePnt2d;
185 myPoints = theSide->GetUVPtStruct();
186 myNbPonits = myNbSegments = myPoints.size();
187 std::vector<uvPtStruct>::iterator it = myPoints.begin();
188 for(; it!=myPoints.end(); it++) {
189 (*it).u = thePnt2d.X();
190 (*it).v = thePnt2d.Y();
192 (*it).node = theNode;
197 //=======================================================================
198 //function : IsUniform
199 //purpose : auxilary function
200 //=======================================================================
201 bool IsUniform(const Handle(Geom2d_Curve)& C2d, double fp, double lp)
203 //cout<<"IsUniform fp = "<<fp<<" lp = "<<lp<<endl;
206 Geom2dAdaptor_Curve A2dC(C2d);
207 double d1 = GCPnts_AbscissaPoint::Length( A2dC, fp, lp );
208 double d2 = GCPnts_AbscissaPoint::Length( A2dC, fp, fp+(lp-fp)/2. );
209 double d4 = GCPnts_AbscissaPoint::Length( A2dC, fp, fp+(lp-fp)/4. );
210 //cout<<"d1 = "<<d1<<" d2 = "<<d2<<" fabs(2*d2/d1-1.0) = "<<fabs(2*d2/d1-1.0)<<endl;
211 if( fabs(2*d2/d1-1.0) > 0.01 || fabs(2*d4/d2-1.0) > 0.01 )
218 //================================================================================
220 * \brief Return info on nodes on the side
221 * \retval UVPtStruct* - array of data structures
223 //================================================================================
225 const vector<UVPtStruct>& StdMeshers_FaceSide::GetUVPtStruct(bool isXConst,
226 double constValue) const
228 if ( myPoints.empty() ) {
230 if ( NbEdges() == 0 ) return myPoints;
232 SMESHDS_Mesh* meshDS = myMesh->GetMeshDS();
234 // sort nodes of all edges putting them into a map
236 map< double, const SMDS_MeshNode*> u2node;
238 for ( int i = 0; i < myEdge.size(); ++i ) {
239 // put 1st vertex node
240 TopoDS_Vertex VV[2]; // TopExp::FirstVertex() returns NULL for INTERNAL edge
241 for ( TopoDS_Iterator vIt(myEdge[i]); vIt.More(); vIt.Next() )
242 VV[ VV[0].IsNull() ? 0 : 1 ] = TopoDS::Vertex(vIt.Value());
243 if ( VV[0].Orientation() == TopAbs_REVERSED ) std::swap ( VV[0], VV[1] );
244 const SMDS_MeshNode* node = SMESH_Algo::VertexNode( VV[0], meshDS );
245 double prevNormPar = ( i == 0 ? 0 : myNormPar[ i-1 ]); // normalized param
246 if ( node ) { // internal nodes may be missing
247 u2node.insert( make_pair( prevNormPar, node ));
250 MESSAGE(" NO NODE on VERTEX" );
254 // put 2nd vertex node for a last edge
255 if ( i+1 == myEdge.size() ) {
256 node = SMESH_Algo::VertexNode( VV[1], meshDS );
258 MESSAGE(" NO NODE on VERTEX" );
261 u2node.insert( make_pair( 1., node ));
264 bool IsUni = IsUniform( myC2d[i], myFirst[i], myLast[i] );
266 // put internal nodes
267 SMESHDS_SubMesh* sm = meshDS->MeshElements( myEdge[i] );
269 SMDS_NodeIteratorPtr nItr = sm->GetNodes();
270 double paramSize = myLast[i] - myFirst[i];
271 double r = myNormPar[i] - prevNormPar;
272 while ( nItr->more() ) {
273 const SMDS_MeshNode* node = nItr->next();
274 if ( myIgnoreMediumNodes && SMESH_MeshEditor::IsMedium( node, SMDSAbs_Edge ))
276 const SMDS_EdgePosition* epos =
277 static_cast<const SMDS_EdgePosition*>(node->GetPosition().get());
278 double u = epos->GetUParameter();
279 // paramSize is signed so orientation is taken into account
281 double normPar = prevNormPar + r * ( u - myFirst[i] ) / paramSize;
285 Handle(Geom_Curve) C3d = BRep_Tool::Curve(myEdge[i],L,fp,lp);
286 GeomAdaptor_Curve A3dC( C3d );
287 double aLen = GCPnts_AbscissaPoint::Length( A3dC, myFirst[i], myLast[i] );
288 double aLenU = GCPnts_AbscissaPoint::Length( A3dC, myFirst[i], u );
289 normPar = prevNormPar + r*aLenU/aLen;
292 if ( normPar > 1 || normPar < 0) {
294 MESSAGE ( "WRONG normPar: "<<normPar<< " prevNormPar="<<prevNormPar
295 << " u="<<u << " myFirst[i]="<<myFirst[i]<< " myLast[i]="<<myLast[i]
296 << " paramSize="<<paramSize );
299 u2node.insert( make_pair( normPar, node ));
302 if ( u2node.size() != myNbPonits ) {
303 MESSAGE("Wrong node parameters on edges, u2node.size():"
304 <<u2node.size()<<" != myNbPonits:"<<myNbPonits);
308 // fill array of UVPtStruct
310 vector<uvPtStruct>* points = const_cast<vector<uvPtStruct>*>( &myPoints );
311 points->resize( myNbPonits );
314 double prevNormPar = 0, paramSize = myNormPar[ EdgeIndex ];
315 map< double, const SMDS_MeshNode*>::iterator u_node = u2node.begin();
316 for (int i = 0 ; u_node != u2node.end(); ++u_node, ++i ) {
317 UVPtStruct & uvPt = (*points)[i];
318 uvPt.node = u_node->second;
319 uvPt.x = uvPt.y = uvPt.normParam = u_node->first;
320 if ( isXConst ) uvPt.x = constValue;
321 else uvPt.y = constValue;
322 if ( myNormPar[ EdgeIndex ] < uvPt.normParam ) {
323 prevNormPar = myNormPar[ EdgeIndex ];
326 if ( EdgeIndex >= myEdge.size() ) {
328 MESSAGE ( "WRONg EdgeIndex " << 1+EdgeIndex
329 << " myNormPar.size()="<<myNormPar.size()
330 << " myNormPar["<< EdgeIndex<<"]="<< myNormPar[ EdgeIndex ]
331 << " uvPt.normParam="<<uvPt.normParam );
334 paramSize = myNormPar[ EdgeIndex ] - prevNormPar;
336 const SMDS_EdgePosition* epos =
337 dynamic_cast<const SMDS_EdgePosition*>(uvPt.node->GetPosition().get());
339 uvPt.param = epos->GetUParameter();
342 double r = ( uvPt.normParam - prevNormPar )/ paramSize;
343 // uvPt.param = myFirst[EdgeIndex] * ( 1 - r ) + myLast[EdgeIndex] * r;
344 uvPt.param = ( r > 0.5 ? myLast[EdgeIndex] : myFirst[EdgeIndex] );
346 if ( !myC2d[ EdgeIndex ].IsNull() ) {
347 gp_Pnt2d p = myC2d[ EdgeIndex ]->Value( uvPt.param );
352 uvPt.u = uvPt.v = 1e+100;
359 //================================================================================
361 * \brief Falsificate info on nodes
362 * \param nbSeg - nb of segments on the side
363 * \retval UVPtStruct* - array of data structures
365 //================================================================================
367 const vector<UVPtStruct>& StdMeshers_FaceSide::SimulateUVPtStruct(int nbSeg,
369 double constValue) const
371 if ( myFalsePoints.empty() ) {
373 if ( NbEdges() == 0 ) return myFalsePoints;
375 vector<uvPtStruct>* points = const_cast<vector<uvPtStruct>*>( &myFalsePoints );
376 points->resize( nbSeg+1 );
379 double prevNormPar = 0, paramSize = myNormPar[ EdgeIndex ];
380 for (int i = 0 ; i < myFalsePoints.size(); ++i ) {
381 double normPar = double(i) / double(nbSeg);
382 UVPtStruct & uvPt = (*points)[i];
384 uvPt.x = uvPt.y = uvPt.param = uvPt.normParam = normPar;
385 if ( isXConst ) uvPt.x = constValue;
386 else uvPt.y = constValue;
387 if ( myNormPar[ EdgeIndex ] < normPar ) {
388 prevNormPar = myNormPar[ EdgeIndex ];
390 paramSize = myNormPar[ EdgeIndex ] - prevNormPar;
392 double r = ( normPar - prevNormPar )/ paramSize;
393 uvPt.param = myFirst[EdgeIndex] * ( 1 - r ) + myLast[EdgeIndex] * r;
394 if ( !myC2d[ EdgeIndex ].IsNull() ) {
395 gp_Pnt2d p = myC2d[ EdgeIndex ]->Value( uvPt.param );
400 uvPt.u = uvPt.v = 1e+100;
404 return myFalsePoints;
406 // gp_Pnt StdMeshers_FaceSide::Value(double U) const
410 //================================================================================
412 * \brief reverse order of vector elements
413 * \param vec - vector to reverse
415 //================================================================================
417 template <typename T > void reverse(vector<T> & vec)
419 for ( int f=0, r=vec.size()-1; f < r; ++f, --r )
420 std::swap( vec[f], vec[r] );
423 //================================================================================
425 * \brief Change orientation of side geometry
427 //================================================================================
429 void StdMeshers_FaceSide::Reverse()
431 int nbEdges = myEdge.size();
432 for ( int i = nbEdges-1; i >= 0; --i ) {
433 std::swap( myFirst[i], myLast[i] );
435 if ( i > 0 ) // at the first loop 1. is overwritten
436 myNormPar[i] = 1 - myNormPar[i-1];
443 reverse( myNormPar );
445 myNormPar[nbEdges-1]=1.;
447 myFalsePoints.clear();
450 //================================================================================
452 * \brief Show side features
454 //================================================================================
456 void StdMeshers_FaceSide::dump(const char* msg) const
459 if (msg) MESSAGE ( std::endl << msg );
460 MESSAGE_BEGIN ("NB EDGES: "<< myEdge.size() );
461 MESSAGE_ADD ( "nbPoints: "<<myNbPonits<<" vecSize: " << myPoints.size()<<" "<<myFalsePoints.size() );
462 for ( int i=0; i<myEdge.size(); ++i)
464 MESSAGE_ADD ( "\t"<<i+1 );
465 MESSAGE_ADD ( "\tEDGE: " );
466 if (myEdge[i].IsNull()) {
467 MESSAGE_ADD ( "NULL" );
470 TopAbs::Print(myEdge[i].Orientation(),cout)<<" "<<myEdge[i].TShape().operator->()<<endl;
471 MESSAGE_ADD ( "\tV1: " << TopExp::FirstVertex( myEdge[i], 1).TShape().operator->()
472 << " V2: " << TopExp::LastVertex( myEdge[i], 1).TShape().operator->() );
474 MESSAGE_ADD ( "\tC2d: ");
476 if (myC2d[i].IsNull()) {
477 MESSAGE_ADD ( "NULL" );
480 MESSAGE_ADD ( myC2d[i].operator->() );
483 MESSAGE_ADD ( "\tF: "<<myFirst[i]<< " L: "<< myLast[i] );
484 MESSAGE_END ( "\tnormPar: "<<myNormPar[i]<<endl );
489 //================================================================================
491 * \brief Creates a Adaptor2d_Curve2d to be used in SMESH_Block
492 * \retval Adaptor2d_Curve2d* -
494 //================================================================================
496 struct Adaptor2dCurve2d : public Adaptor2d_Curve2d
498 const StdMeshers_FaceSide* mySide;
499 Adaptor2dCurve2d(const StdMeshers_FaceSide* faceSide):mySide(faceSide) {}
500 gp_Pnt2d Value(const Standard_Real U) const { return mySide->Value2d( U ); }
501 Standard_Real FirstParameter() const { return 0; }
502 Standard_Real LastParameter() const { return 1; }
505 Adaptor2d_Curve2d* StdMeshers_FaceSide::GetCurve2d() const
507 return new Adaptor2dCurve2d( this );
510 //================================================================================
512 * \brief Creates a fully functional Adaptor_Curve
514 //================================================================================
516 BRepAdaptor_CompCurve* StdMeshers_FaceSide::GetCurve3d() const
518 if ( myEdge.empty() )
521 // if ( myEdge.size() == 1 )
522 // return new BRepAdaptor_Curve( myEdge[0] );
525 BRep_Builder aBuilder;
526 aBuilder.MakeWire(aWire);
527 for ( int i=0; i<myEdge.size(); ++i )
528 aBuilder.Add( aWire, myEdge[i] );
529 return new BRepAdaptor_CompCurve( aWire );
533 //================================================================================
535 * \brief Return 2D point by normalized parameter
536 * \param U - normalized parameter value
537 * \retval gp_Pnt2d - point
539 //================================================================================
541 gp_Pnt2d StdMeshers_FaceSide::Value2d(double U) const
543 if ( !myC2d[0].IsNull() ) {
544 int i = EdgeIndex( U );
545 double prevU = i ? myNormPar[ i-1 ] : 0;
546 double r = ( U - prevU )/ ( myNormPar[ i ] - prevU );
548 double par = myFirst[i] * ( 1 - r ) + myLast[i] * r;
550 // check parametrization of curve
551 if( !IsUniform( myC2d[i], myFirst[i], myLast[i] ) ) {
554 Handle(Geom_Curve) C3d = BRep_Tool::Curve(myEdge[i],L,fp,lp);
557 GeomAdaptor_Curve A3dC( C3d );
558 double aLen3d = GCPnts_AbscissaPoint::Length( A3dC, fp, lp );
559 double aLen3dU = aLen3d*r;
563 GCPnts_AbscissaPoint AbPnt( A3dC, aLen3dU, fp );
564 if( AbPnt.IsDone() ) {
565 par = AbPnt.Parameter();
568 return myC2d[ i ]->Value(par);
571 //return gp_Pnt2d( 1e+100, 1e+100 );
572 return myDefaultPnt2d;
575 //================================================================================
577 * \brief Return wires of a face as StdMeshers_FaceSide's
579 //================================================================================
581 TSideVector StdMeshers_FaceSide::GetFaceWires(const TopoDS_Face& theFace,
582 SMESH_Mesh & theMesh,
583 const bool theIgnoreMediumNodes,
587 list< TopoDS_Edge > edges;
588 list< int > nbEdgesInWires;
589 int nbWires = SMESH_Block::GetOrderedEdges (theFace, V1, edges, nbEdgesInWires);
591 // split list of all edges into separate wires
592 TSideVector wires( nbWires );
593 list< int >::iterator nbE = nbEdgesInWires.begin();
594 list< TopoDS_Edge >::iterator from, to;
595 from = to = edges.begin();
596 for ( int iW = 0; iW < nbWires; ++iW )
598 std::advance( to, *nbE++ );
599 if ( *nbE == 0 ) // Issue 0020676
603 wires.resize( nbWires );
606 list< TopoDS_Edge > wireEdges( from, to );
607 // assure that there is a node on the first vertex
608 // as StdMeshers_FaceSide::GetUVPtStruct() requires
609 if ( wireEdges.front().Orientation() != TopAbs_INTERNAL ) // Issue 0020676
610 while ( !SMESH_Algo::VertexNode( TopExp::FirstVertex( wireEdges.front(), true),
611 theMesh.GetMeshDS()))
613 wireEdges.splice(wireEdges.end(), wireEdges,
614 wireEdges.begin(), ++wireEdges.begin());
615 if ( from->IsSame( wireEdges.front() )) {
617 ( new SMESH_ComputeError(COMPERR_BAD_INPUT_MESH,"No nodes on vertices"));
618 return TSideVector(0);
621 const bool isForward = true;
622 StdMeshers_FaceSide* wire = new StdMeshers_FaceSide( theFace, wireEdges, &theMesh,
623 isForward, theIgnoreMediumNodes);
624 wires[ iW ] = StdMeshers_FaceSidePtr( wire );
630 //================================================================================
632 * \brief Return 1st vertex of the i-the edge
634 //================================================================================
636 TopoDS_Vertex StdMeshers_FaceSide::FirstVertex(int i) const
641 v = myEdge[i].Orientation() <= TopAbs_REVERSED ? // FORWARD || REVERSED
642 TopExp::FirstVertex( myEdge[i], 1 ) :
643 TopoDS::Vertex( TopoDS_Iterator( myEdge[i] ).Value() );
648 //================================================================================
650 * \brief Return last vertex of the i-the edge
652 //================================================================================
654 TopoDS_Vertex StdMeshers_FaceSide::LastVertex(int i) const
659 const TopoDS_Edge& edge = i<0 ? myEdge[ NbEdges() + i ] : myEdge[i];
660 if ( edge.Orientation() <= TopAbs_REVERSED ) // FORWARD || REVERSED
661 v = TopExp::LastVertex( edge, 1 );
663 for ( TopoDS_Iterator vIt( edge ); vIt.More(); vIt.Next() )
664 v = TopoDS::Vertex( vIt.Value() );