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>
54 #include "utilities.h"
56 //================================================================================
58 * \brief Constructor of a side of one edge
59 * \param theFace - the face
60 * \param theEdge - the edge
62 //================================================================================
64 StdMeshers_FaceSide::StdMeshers_FaceSide(const TopoDS_Face& theFace,
65 const TopoDS_Edge& theEdge,
67 const bool theIsForward,
68 const bool theIgnoreMediumNodes)
70 list<TopoDS_Edge> edges(1,theEdge);
71 *this = StdMeshers_FaceSide( theFace, edges, theMesh, theIsForward, theIgnoreMediumNodes );
74 //================================================================================
76 * \brief Constructor of a side of several edges
77 * \param theFace - the face
78 * \param theEdge - the edge
80 //================================================================================
82 StdMeshers_FaceSide::StdMeshers_FaceSide(const TopoDS_Face& theFace,
83 list<TopoDS_Edge>& theEdges,
85 const bool theIsForward,
86 const bool theIgnoreMediumNodes)
88 int nbEdges = theEdges.size();
89 myEdge.resize( nbEdges );
90 myC2d.resize( nbEdges );
91 myFirst.resize( nbEdges );
92 myLast.resize( nbEdges );
93 myNormPar.resize( nbEdges );
95 myNbPonits = myNbSegments = 0;
97 myMissingVertexNodes = false;
98 myIgnoreMediumNodes = theIgnoreMediumNodes;
99 myDefaultPnt2d = gp_Pnt2d( 1e+100, 1e+100 );
100 if ( nbEdges == 0 ) return;
102 SMESHDS_Mesh* meshDS = theMesh->GetMeshDS();
103 vector<double> len( nbEdges );
106 list<TopoDS_Edge>::iterator edge = theEdges.begin();
107 TopoDS_Iterator vExp;
108 for ( int index = 0; edge != theEdges.end(); ++index, ++edge )
110 int i = theIsForward ? index : nbEdges - index - 1;
111 len[i] = SMESH_Algo::EdgeLength( *edge );
112 if ( len[i] < DBL_MIN ) nbDegen++;
115 if ( !theIsForward ) myEdge[i].Reverse();
117 if ( theFace.IsNull() )
118 BRep_Tool::Range( *edge, myFirst[i], myLast[i] );
120 myC2d[i] = BRep_Tool::CurveOnSurface( *edge, theFace, myFirst[i], myLast[i] );
121 if ( myEdge[i].Orientation() == TopAbs_REVERSED )
122 std::swap( myFirst[i], myLast[i] );
124 if ( SMESHDS_SubMesh* sm = meshDS->MeshElements( *edge )) {
125 int nbN = sm->NbNodes();
126 if ( theIgnoreMediumNodes ) {
127 SMDS_ElemIteratorPtr elemIt = sm->GetElements();
128 if ( elemIt->more() && elemIt->next()->IsQuadratic() )
129 nbN -= sm->NbElements();
132 myNbSegments += sm->NbElements();
134 vExp.Initialize( *edge );
135 if ( SMESH_Algo::VertexNode( TopoDS::Vertex( vExp.Value()), meshDS ))
136 myNbPonits += 1; // for the first end
138 myMissingVertexNodes = true;
143 if ( SMESH_Algo::VertexNode( TopoDS::Vertex( vExp.Value()), meshDS ))
144 myNbPonits++; // for the last end
146 myMissingVertexNodes = true;
148 if ( nbEdges > 1 && myLength > DBL_MIN ) {
149 const double degenNormLen = 1.e-5;
150 double totLength = myLength;
152 totLength += myLength * degenNormLen * nbDegen;
153 double prevNormPar = 0;
154 for ( int i = 0; i < nbEdges; ++i ) {
155 if ( len[ i ] < DBL_MIN )
156 len[ i ] = myLength * degenNormLen;
157 myNormPar[ i ] = prevNormPar + len[i]/totLength;
158 prevNormPar = myNormPar[ i ];
161 myNormPar[nbEdges-1] = 1.;
165 //================================================================================
167 * \brief Constructor of a side for vertex using data from other FaceSide
168 * \param theVertex - the vertex
169 * \param theSide - the side
171 //================================================================================
173 StdMeshers_FaceSide::StdMeshers_FaceSide(const SMDS_MeshNode* theNode,
174 const gp_Pnt2d thePnt2d,
175 const StdMeshers_FaceSide* theSide)
179 myMesh = theSide->GetMesh();
180 myDefaultPnt2d = thePnt2d;
182 myPoints = theSide->GetUVPtStruct();
183 myNbPonits = myNbSegments = myPoints.size();
184 std::vector<uvPtStruct>::iterator it = myPoints.begin();
185 for(; it!=myPoints.end(); it++) {
186 (*it).u = thePnt2d.X();
187 (*it).v = thePnt2d.Y();
189 (*it).node = theNode;
193 //================================================================================
195 * \brief Return info on nodes on the side
196 * \retval UVPtStruct* - array of data structures
198 //================================================================================
200 const vector<UVPtStruct>& StdMeshers_FaceSide::GetUVPtStruct(bool isXConst,
201 double constValue) const
203 if ( myPoints.empty() ) {
205 if ( NbEdges() == 0 ) return myPoints;
207 SMESHDS_Mesh* meshDS = myMesh->GetMeshDS();
209 // sort nodes of all edges putting them into a map
211 map< double, const SMDS_MeshNode*> u2node;
213 for ( int i = 0; i < myEdge.size(); ++i ) {
214 // put 1st vertex node
215 TopoDS_Vertex VV[2]; // TopExp::FirstVertex() returns NULL for INTERNAL edge
216 for ( TopoDS_Iterator vIt(myEdge[i]); vIt.More(); vIt.Next() )
217 VV[ VV[0].IsNull() ? 0 : 1 ] = TopoDS::Vertex(vIt.Value());
218 if ( VV[0].Orientation() == TopAbs_REVERSED ) std::swap ( VV[0], VV[1] );
219 const SMDS_MeshNode* node = SMESH_Algo::VertexNode( VV[0], meshDS );
220 double prevNormPar = ( i == 0 ? 0 : myNormPar[ i-1 ]); // normalized param
221 if ( node ) { // internal nodes may be missing
222 u2node.insert( make_pair( prevNormPar, node ));
225 MESSAGE(" NO NODE on VERTEX" );
229 // put 2nd vertex node for a last edge
230 if ( i+1 == myEdge.size() ) {
231 node = SMESH_Algo::VertexNode( VV[1], meshDS );
233 MESSAGE(" NO NODE on VERTEX" );
236 u2node.insert( make_pair( 1., node ));
239 // put internal nodes
240 SMESHDS_SubMesh* sm = meshDS->MeshElements( myEdge[i] );
242 SMDS_NodeIteratorPtr nItr = sm->GetNodes();
243 double paramSize = myLast[i] - myFirst[i], r = myNormPar[i] - prevNormPar;
244 while ( nItr->more() ) {
245 const SMDS_MeshNode* node = nItr->next();
246 if ( myIgnoreMediumNodes && SMESH_MeshEditor::IsMedium( node, SMDSAbs_Edge ))
248 const SMDS_EdgePosition* epos =
249 static_cast<const SMDS_EdgePosition*>(node->GetPosition().get());
250 double u = epos->GetUParameter();
251 // paramSize is signed so orientation is taken into account
252 double normPar = prevNormPar + r * ( u - myFirst[i] ) / paramSize;
254 if ( normPar > 1 || normPar < 0) {
256 MESSAGE ( "WRONG normPar: "<<normPar<< " prevNormPar="<<prevNormPar
257 << " u="<<u << " myFirst[i]="<<myFirst[i]<< " myLast[i]="<<myLast[i]
258 << " paramSize="<<paramSize );
261 u2node.insert( make_pair( normPar, node ));
264 if ( u2node.size() != myNbPonits ) {
265 MESSAGE("Wrong node parameters on edges, u2node.size():"
266 <<u2node.size()<<" != myNbPonits:"<<myNbPonits);
270 // fill array of UVPtStruct
272 vector<uvPtStruct>* points = const_cast<vector<uvPtStruct>*>( &myPoints );
273 points->resize( myNbPonits );
276 double prevNormPar = 0, paramSize = myNormPar[ EdgeIndex ];
277 map< double, const SMDS_MeshNode*>::iterator u_node = u2node.begin();
278 for (int i = 0 ; u_node != u2node.end(); ++u_node, ++i ) {
279 UVPtStruct & uvPt = (*points)[i];
280 uvPt.node = u_node->second;
281 uvPt.x = uvPt.y = uvPt.normParam = u_node->first;
282 if ( isXConst ) uvPt.x = constValue;
283 else uvPt.y = constValue;
284 if ( myNormPar[ EdgeIndex ] < uvPt.normParam ) {
285 prevNormPar = myNormPar[ EdgeIndex ];
288 if ( EdgeIndex >= myEdge.size() ) {
290 MESSAGE ( "WRONg EdgeIndex " << 1+EdgeIndex
291 << " myNormPar.size()="<<myNormPar.size()
292 << " myNormPar["<< EdgeIndex<<"]="<< myNormPar[ EdgeIndex ]
293 << " uvPt.normParam="<<uvPt.normParam );
296 paramSize = myNormPar[ EdgeIndex ] - prevNormPar;
298 const SMDS_EdgePosition* epos =
299 dynamic_cast<const SMDS_EdgePosition*>(uvPt.node->GetPosition().get());
301 uvPt.param = epos->GetUParameter();
304 double r = ( uvPt.normParam - prevNormPar )/ paramSize;
305 // uvPt.param = myFirst[EdgeIndex] * ( 1 - r ) + myLast[EdgeIndex] * r;
306 uvPt.param = ( r > 0.5 ? myLast[EdgeIndex] : myFirst[EdgeIndex] );
308 if ( !myC2d[ EdgeIndex ].IsNull() ) {
309 gp_Pnt2d p = myC2d[ EdgeIndex ]->Value( uvPt.param );
314 uvPt.u = uvPt.v = 1e+100;
321 //================================================================================
323 * \brief Falsificate info on nodes
324 * \param nbSeg - nb of segments on the side
325 * \retval UVPtStruct* - array of data structures
327 //================================================================================
329 const vector<UVPtStruct>& StdMeshers_FaceSide::SimulateUVPtStruct(int nbSeg,
331 double constValue) const
333 if ( myFalsePoints.empty() ) {
335 if ( NbEdges() == 0 ) return myFalsePoints;
337 vector<uvPtStruct>* points = const_cast<vector<uvPtStruct>*>( &myFalsePoints );
338 points->resize( nbSeg+1 );
341 double prevNormPar = 0, paramSize = myNormPar[ EdgeIndex ];
342 for (int i = 0 ; i < myFalsePoints.size(); ++i ) {
343 double normPar = double(i) / double(nbSeg);
344 UVPtStruct & uvPt = (*points)[i];
346 uvPt.x = uvPt.y = uvPt.param = uvPt.normParam = normPar;
347 if ( isXConst ) uvPt.x = constValue;
348 else uvPt.y = constValue;
349 if ( myNormPar[ EdgeIndex ] < normPar ) {
350 prevNormPar = myNormPar[ EdgeIndex ];
352 paramSize = myNormPar[ EdgeIndex ] - prevNormPar;
354 double r = ( normPar - prevNormPar )/ paramSize;
355 uvPt.param = myFirst[EdgeIndex] * ( 1 - r ) + myLast[EdgeIndex] * r;
356 if ( !myC2d[ EdgeIndex ].IsNull() ) {
357 gp_Pnt2d p = myC2d[ EdgeIndex ]->Value( uvPt.param );
362 uvPt.u = uvPt.v = 1e+100;
366 return myFalsePoints;
368 // gp_Pnt StdMeshers_FaceSide::Value(double U) const
372 //================================================================================
374 * \brief reverse order of vector elements
375 * \param vec - vector to reverse
377 //================================================================================
379 template <typename T > void reverse(vector<T> & vec)
381 for ( int f=0, r=vec.size()-1; f < r; ++f, --r )
382 std::swap( vec[f], vec[r] );
385 //================================================================================
387 * \brief Change orientation of side geometry
389 //================================================================================
391 void StdMeshers_FaceSide::Reverse()
393 int nbEdges = myEdge.size();
394 for ( int i = nbEdges-1; i >= 0; --i ) {
395 std::swap( myFirst[i], myLast[i] );
397 if ( i > 0 ) // at the first loop 1. is overwritten
398 myNormPar[i] = 1 - myNormPar[i-1];
405 reverse( myNormPar );
407 myNormPar[nbEdges-1]=1.;
409 myFalsePoints.clear();
412 //================================================================================
414 * \brief Show side features
416 //================================================================================
418 void StdMeshers_FaceSide::dump(const char* msg) const
421 if (msg) MESSAGE ( std::endl << msg );
422 MESSAGE_BEGIN ("NB EDGES: "<< myEdge.size() );
423 MESSAGE_ADD ( "nbPoints: "<<myNbPonits<<" vecSize: " << myPoints.size()<<" "<<myFalsePoints.size() );
424 for ( int i=0; i<myEdge.size(); ++i)
426 MESSAGE_ADD ( "\t"<<i+1 );
427 MESSAGE_ADD ( "\tEDGE: " );
428 if (myEdge[i].IsNull()) {
429 MESSAGE_ADD ( "NULL" );
432 TopAbs::Print(myEdge[i].Orientation(),cout)<<" "<<myEdge[i].TShape().operator->()<<endl;
433 MESSAGE_ADD ( "\tV1: " << TopExp::FirstVertex( myEdge[i], 1).TShape().operator->()
434 << " V2: " << TopExp::LastVertex( myEdge[i], 1).TShape().operator->() );
436 MESSAGE_ADD ( "\tC2d: ");
438 if (myC2d[i].IsNull()) {
439 MESSAGE_ADD ( "NULL" );
442 MESSAGE_ADD ( myC2d[i].operator->() );
445 MESSAGE_ADD ( "\tF: "<<myFirst[i]<< " L: "<< myLast[i] );
446 MESSAGE_END ( "\tnormPar: "<<myNormPar[i]<<endl );
451 //================================================================================
453 * \brief Creates a Adaptor2d_Curve2d to be used in SMESH_Block
454 * \retval Adaptor2d_Curve2d* -
456 //================================================================================
458 struct Adaptor2dCurve2d : public Adaptor2d_Curve2d
460 const StdMeshers_FaceSide* mySide;
461 Adaptor2dCurve2d(const StdMeshers_FaceSide* faceSide):mySide(faceSide) {}
462 gp_Pnt2d Value(const Standard_Real U) const { return mySide->Value2d( U ); }
463 Standard_Real FirstParameter() const { return 0; }
464 Standard_Real LastParameter() const { return 1; }
467 Adaptor2d_Curve2d* StdMeshers_FaceSide::GetCurve2d() const
469 return new Adaptor2dCurve2d( this );
472 //================================================================================
474 * \brief Creates a fully functional Adaptor_Curve
476 //================================================================================
478 BRepAdaptor_CompCurve* StdMeshers_FaceSide::GetCurve3d() const
480 if ( myEdge.empty() )
483 // if ( myEdge.size() == 1 )
484 // return new BRepAdaptor_Curve( myEdge[0] );
487 BRep_Builder aBuilder;
488 aBuilder.MakeWire(aWire);
489 for ( int i=0; i<myEdge.size(); ++i )
490 aBuilder.Add( aWire, myEdge[i] );
491 return new BRepAdaptor_CompCurve( aWire );
494 //================================================================================
496 * \brief Return 2D point by normalized parameter
497 * \param U - normalized parameter value
498 * \retval gp_Pnt2d - point
500 //================================================================================
502 gp_Pnt2d StdMeshers_FaceSide::Value2d(double U) const
504 if ( !myC2d[0].IsNull() ) {
505 int i = EdgeIndex( U );
506 double prevU = i ? myNormPar[ i-1 ] : 0;
507 double r = ( U - prevU )/ ( myNormPar[ i ] - prevU );
508 return myC2d[ i ]->Value( myFirst[i] * ( 1 - r ) + myLast[i] * r );
510 //return gp_Pnt2d( 1e+100, 1e+100 );
511 return myDefaultPnt2d;
514 //================================================================================
516 * \brief Return wires of a face as StdMeshers_FaceSide's
518 //================================================================================
520 TSideVector StdMeshers_FaceSide::GetFaceWires(const TopoDS_Face& theFace,
521 SMESH_Mesh & theMesh,
522 const bool theIgnoreMediumNodes,
526 list< TopoDS_Edge > edges;
527 list< int > nbEdgesInWires;
528 int nbWires = SMESH_Block::GetOrderedEdges (theFace, V1, edges, nbEdgesInWires);
530 // split list of all edges into separate wires
531 TSideVector wires( nbWires );
532 list< int >::iterator nbE = nbEdgesInWires.begin();
533 list< TopoDS_Edge >::iterator from, to;
534 from = to = edges.begin();
535 for ( int iW = 0; iW < nbWires; ++iW )
537 std::advance( to, *nbE++ );
538 if ( *nbE == 0 ) // Issue 0020676
542 wires.resize( nbWires );
545 list< TopoDS_Edge > wireEdges( from, to );
546 // assure that there is a node on the first vertex
547 // as StdMeshers_FaceSide::GetUVPtStruct() requires
548 if ( wireEdges.front().Orientation() != TopAbs_INTERNAL ) // Issue 0020676
549 while ( !SMESH_Algo::VertexNode( TopExp::FirstVertex( wireEdges.front(), true),
550 theMesh.GetMeshDS()))
552 wireEdges.splice(wireEdges.end(), wireEdges,
553 wireEdges.begin(), ++wireEdges.begin());
554 if ( from->IsSame( wireEdges.front() )) {
556 ( new SMESH_ComputeError(COMPERR_BAD_INPUT_MESH,"No nodes on vertices"));
557 return TSideVector(0);
560 const bool isForward = true;
561 StdMeshers_FaceSide* wire = new StdMeshers_FaceSide( theFace, wireEdges, &theMesh,
562 isForward, theIgnoreMediumNodes);
563 wires[ iW ] = StdMeshers_FaceSidePtr( wire );
569 //================================================================================
571 * \brief Return 1st vertex of the i-the edge
573 //================================================================================
575 TopoDS_Vertex StdMeshers_FaceSide::FirstVertex(int i) const
580 v = myEdge[i].Orientation() <= TopAbs_REVERSED ? // FORWARD || REVERSED
581 TopExp::FirstVertex( myEdge[i], 1 ) :
582 TopoDS::Vertex( TopoDS_Iterator( myEdge[i] ).Value() );
587 //================================================================================
589 * \brief Return last vertex of the i-the edge
591 //================================================================================
593 TopoDS_Vertex StdMeshers_FaceSide::LastVertex(int i) const
598 const TopoDS_Edge& edge = i<0 ? myEdge[ NbEdges() + i ] : myEdge[i];
599 if ( edge.Orientation() <= TopAbs_REVERSED ) // FORWARD || REVERSED
600 v = TopExp::LastVertex( edge, 1 );
602 for ( TopoDS_Iterator vIt( edge ); vIt.More(); vIt.Next() )
603 v = TopoDS::Vertex( vIt.Value() );