Salome HOME
Add new example python script (from CEA)
[modules/smesh.git] / src / StdMeshers / StdMeshers_FaceSide.cxx
1 //  SMESH SMESH : implementaion of SMESH idl descriptions
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
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. 
10 // 
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. 
15 // 
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 
19 // 
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 //
24 // File      : StdMeshers_FaceSide.hxx
25 // Created   : Wed Jan 31 18:41:25 2007
26 // Author    : Edward AGAPOV (eap)
27 // Module    : SMESH
28
29 #include "StdMeshers_FaceSide.hxx"
30
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_MeshEditor.hxx"
38 #include "SMESH_ComputeError.hxx"
39 #include "SMESH_Block.hxx"
40
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>
46 #include <TopExp.hxx>
47 #include <TopExp_Explorer.hxx>
48 #include <TopoDS_Face.hxx>
49 #include <TopoDS_Vertex.hxx>
50 #include <TopoDS_Wire.hxx>
51
52 #include <map>
53
54 #include "utilities.h"
55
56 using namespace std;
57
58 //================================================================================
59 /*!
60  * \brief Constructor of a side of one edge
61   * \param theFace - the face
62   * \param theEdge - the edge
63  */
64 //================================================================================
65
66 StdMeshers_FaceSide::StdMeshers_FaceSide(const TopoDS_Face& theFace,
67                                          const TopoDS_Edge& theEdge,
68                                          SMESH_Mesh*        theMesh,
69                                          const bool         theIsForward,
70                                          const bool         theIgnoreMediumNodes)
71 {
72   list<TopoDS_Edge> edges(1,theEdge);
73   *this = StdMeshers_FaceSide( theFace, edges, theMesh, theIsForward, theIgnoreMediumNodes );
74 }
75
76 //================================================================================
77 /*!
78  * \brief Constructor of a side of several edges
79   * \param theFace - the face
80   * \param theEdge - the edge
81  */
82 //================================================================================
83
84 StdMeshers_FaceSide::StdMeshers_FaceSide(const TopoDS_Face& theFace,
85                                          list<TopoDS_Edge>& theEdges,
86                                          SMESH_Mesh*        theMesh,
87                                          const bool         theIsForward,
88                                          const bool         theIgnoreMediumNodes)
89 {
90   int nbEdges = theEdges.size();
91   myEdge.resize( nbEdges );
92   myC2d.resize( nbEdges );
93   myFirst.resize( nbEdges );
94   myLast.resize( nbEdges );
95   myNormPar.resize( nbEdges );
96   myLength = 0;
97   myNbPonits = myNbSegments = 0;
98   myMesh = theMesh;
99   myMissingVertexNodes = false;
100   myIgnoreMediumNodes = theIgnoreMediumNodes;
101   if ( nbEdges == 0 ) return;
102
103   SMESHDS_Mesh* meshDS = theMesh->GetMeshDS();
104   vector<double> len( nbEdges );
105
106   int nbDegen = 0;
107   list<TopoDS_Edge>::iterator edge = theEdges.begin();
108   for ( int index = 0; edge != theEdges.end(); ++index, ++edge )
109   {
110     int i = theIsForward ? index : nbEdges - index - 1;
111     len[i] = SMESH_Algo::EdgeLength( *edge );
112     if ( len[i] < DBL_MIN ) nbDegen++;
113     myLength += len[i];
114     myEdge[i] = *edge;
115     if ( !theIsForward ) myEdge[i].Reverse();
116
117     if ( theFace.IsNull() )
118       BRep_Tool::Range( *edge, myFirst[i], myLast[i] );
119     else
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] );
123
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();
130       }
131       myNbPonits += nbN;
132       myNbSegments += sm->NbElements();
133     }
134     if ( SMESH_Algo::VertexNode( TopExp::FirstVertex( *edge, 1), meshDS ))
135       myNbPonits += 1; // for the first end
136     else
137       myMissingVertexNodes = true;
138   }
139   if ( SMESH_Algo::VertexNode( TopExp::LastVertex( theEdges.back(), 1), meshDS ))
140     myNbPonits++; // for the last end
141   else
142     myMissingVertexNodes = true;
143
144   if ( nbEdges > 1 && myLength > DBL_MIN ) {
145     const double degenNormLen = 1.e-5;
146     double totLength = myLength;
147     if ( nbDegen )
148       totLength += myLength * degenNormLen * nbDegen;
149     double prevNormPar = 0;
150     for ( int i = 0; i < nbEdges; ++i ) {
151       if ( len[ i ] < DBL_MIN )
152         len[ i ] = myLength * degenNormLen;
153       myNormPar[ i ] = prevNormPar + len[i]/totLength;
154       prevNormPar = myNormPar[ i ];
155     }
156   }
157   myNormPar[nbEdges-1] = 1.;
158   //dump();
159 }
160
161 //================================================================================
162 /*!
163  * \brief Return info on nodes on the side
164   * \retval UVPtStruct* - array of data structures
165  */
166 //================================================================================
167
168 const vector<UVPtStruct>& StdMeshers_FaceSide::GetUVPtStruct(bool   isXConst,
169                                                              double constValue) const
170 {
171   if ( myPoints.empty() ) {
172
173     if ( NbEdges() == 0 ) return myPoints;
174
175     SMESHDS_Mesh* meshDS = myMesh->GetMeshDS();
176
177     // sort nodes of all edges putting them into a map
178
179     map< double, const SMDS_MeshNode*> u2node;
180     //int nbOnDegen = 0;
181     for ( int i = 0; i < myEdge.size(); ++i )
182     {
183       // put 1st vertex node
184       TopoDS_Vertex VFirst, VLast;
185       TopExp::Vertices( myEdge[i], VFirst, VLast, true);
186       const SMDS_MeshNode* node = SMESH_Algo::VertexNode( VFirst, meshDS );
187       double prevNormPar = ( i == 0 ? 0 : myNormPar[ i-1 ]); // normalized param
188       if ( node ) { // internal nodes may be missing
189         u2node.insert( make_pair( prevNormPar, node ));
190       } else if ( i == 0 ) {
191         MESSAGE(" NO NODE on VERTEX" );
192         return myPoints;
193       }
194
195       // put 2nd vertex node for a last edge
196       if ( i+1 == myEdge.size() ) {
197         node = SMESH_Algo::VertexNode( VLast, meshDS );
198         if ( !node ) {
199           MESSAGE(" NO NODE on VERTEX" );
200           return myPoints;
201         }
202         u2node.insert( make_pair( 1., node ));
203       }
204
205       // put internal nodes
206       SMESHDS_SubMesh* sm = meshDS->MeshElements( myEdge[i] );
207       if ( !sm ) continue;
208       SMDS_NodeIteratorPtr nItr = sm->GetNodes();
209       double paramSize = myLast[i] - myFirst[i], r = myNormPar[i] - prevNormPar;
210       while ( nItr->more() ) {
211         const SMDS_MeshNode* node = nItr->next();
212         if ( myIgnoreMediumNodes && SMESH_MeshEditor::IsMedium( node, SMDSAbs_Edge ))
213           continue;
214         const SMDS_EdgePosition* epos =
215           static_cast<const SMDS_EdgePosition*>(node->GetPosition().get());
216         double u = epos->GetUParameter();
217         // paramSize is signed so orientation is taken into account
218         double normPar = prevNormPar + r * ( u - myFirst[i] ) / paramSize;
219 #ifdef _DEBUG_
220         if ( normPar > 1 || normPar < 0) {
221           dump("DEBUG");
222           MESSAGE ( "WRONG normPar: "<<normPar<< " prevNormPar="<<prevNormPar
223                     << " u="<<u << " myFirst[i]="<<myFirst[i]<< " myLast[i]="<<myLast[i]
224                     << " paramSize="<<paramSize );
225         }
226 #endif
227         u2node.insert( make_pair( normPar, node ));
228       }
229     }
230     if ( u2node.size() != myNbPonits ) {
231       MESSAGE("Wrong node parameters on edges, u2node.size():"
232               <<u2node.size()<<" !=  myNbPonits:"<<myNbPonits);
233       return myPoints;
234     }
235
236     // fill array of UVPtStruct
237
238     vector<uvPtStruct>* points = const_cast<vector<uvPtStruct>*>( &myPoints );
239     points->resize( myNbPonits );
240
241     int EdgeIndex = 0;
242     double prevNormPar = 0, paramSize = myNormPar[ EdgeIndex ];
243     map< double, const SMDS_MeshNode*>::iterator u_node = u2node.begin();
244     for (int i = 0 ; u_node != u2node.end(); ++u_node, ++i ) {
245       UVPtStruct & uvPt = (*points)[i];
246       uvPt.node = u_node->second;
247       uvPt.x = uvPt.y = uvPt.normParam = u_node->first;
248       if ( isXConst ) uvPt.x = constValue;
249       else            uvPt.y = constValue;
250       if ( myNormPar[ EdgeIndex ] < uvPt.normParam ) {
251         prevNormPar = myNormPar[ EdgeIndex ];
252         ++EdgeIndex;
253 #ifdef _DEBUG_
254         if ( EdgeIndex >= myEdge.size() ) {
255           dump("DEBUG");
256           MESSAGE ( "WRONg EdgeIndex " << 1+EdgeIndex
257                     << " myNormPar.size()="<<myNormPar.size()
258                     << " myNormPar["<< EdgeIndex<<"]="<< myNormPar[ EdgeIndex ]
259                     << " uvPt.normParam="<<uvPt.normParam );
260         }
261 #endif
262         paramSize = myNormPar[ EdgeIndex ] - prevNormPar;
263       }
264       const SMDS_EdgePosition* epos =
265         dynamic_cast<const SMDS_EdgePosition*>(uvPt.node->GetPosition().get());
266       if ( epos ) {
267         uvPt.param = epos->GetUParameter();
268       }
269       else {
270         double r = ( uvPt.normParam - prevNormPar )/ paramSize;
271 //         uvPt.param = myFirst[EdgeIndex] * ( 1 - r ) + myLast[EdgeIndex] * r;
272         uvPt.param = ( r > 0.5 ? myLast[EdgeIndex] : myFirst[EdgeIndex] );
273       }
274       if ( !myC2d[ EdgeIndex ].IsNull() ) {
275         gp_Pnt2d p = myC2d[ EdgeIndex ]->Value( uvPt.param );
276         uvPt.u = p.X();
277         uvPt.v = p.Y();
278       }
279       else {
280         uvPt.u = uvPt.v = 1e+100;
281       }
282     }
283   }
284   return myPoints;
285 }
286
287 //================================================================================
288 /*!
289  * \brief Falsificate info on nodes
290   * \param nbSeg - nb of segments on the side
291   * \retval UVPtStruct* - array of data structures
292  */
293 //================================================================================
294
295 const vector<UVPtStruct>& StdMeshers_FaceSide::SimulateUVPtStruct(int    nbSeg,
296                                                                   bool   isXConst,
297                                                                   double constValue) const
298 {
299   if ( myFalsePoints.empty() ) {
300
301     if ( NbEdges() == 0 ) return myFalsePoints;
302
303     vector<uvPtStruct>* points = const_cast<vector<uvPtStruct>*>( &myFalsePoints );
304     points->resize( nbSeg+1 );
305
306     int EdgeIndex = 0;
307     double prevNormPar = 0, paramSize = myNormPar[ EdgeIndex ];
308     for (int i = 0 ; i < myFalsePoints.size(); ++i ) {
309       double normPar = double(i) / double(nbSeg);
310       UVPtStruct & uvPt = (*points)[i];
311       uvPt.node = 0;
312       uvPt.x = uvPt.y = uvPt.param = uvPt.normParam = normPar;
313       if ( isXConst ) uvPt.x = constValue;
314       else            uvPt.y = constValue;
315       if ( myNormPar[ EdgeIndex ] < normPar ) {
316         prevNormPar = myNormPar[ EdgeIndex ];
317         ++EdgeIndex;
318         paramSize = myNormPar[ EdgeIndex ] - prevNormPar;
319       }
320       double r = ( normPar - prevNormPar )/ paramSize;
321       uvPt.param = myFirst[EdgeIndex] * ( 1 - r ) + myLast[EdgeIndex] * r;
322       if ( !myC2d[ EdgeIndex ].IsNull() ) {
323         gp_Pnt2d p = myC2d[ EdgeIndex ]->Value( uvPt.param );
324         uvPt.u = p.X();
325         uvPt.v = p.Y();
326       }
327       else {
328         uvPt.u = uvPt.v = 1e+100;
329       }
330     }
331   }
332   return myFalsePoints;
333 }
334 // gp_Pnt StdMeshers_FaceSide::Value(double U) const
335 // {
336 // }
337
338 //================================================================================
339 /*!
340  * \brief reverse order of vector elements
341   * \param vec - vector to reverse
342  */
343 //================================================================================
344
345 template <typename T > void reverse(vector<T> & vec)
346 {
347   for ( int f=0, r=vec.size()-1; f < r; ++f, --r )
348     std::swap( vec[f], vec[r] );
349 }
350
351 //================================================================================
352 /*!
353  * \brief Change orientation of side geometry
354  */
355 //================================================================================
356
357 void StdMeshers_FaceSide::Reverse()
358 {
359   int nbEdges = myEdge.size();
360   for ( int i = nbEdges-1; i >= 0; --i ) {
361     std::swap( myFirst[i], myLast[i] );
362     myEdge[i].Reverse();
363     if ( i > 0 ) // at the first loop 1. is overwritten
364       myNormPar[i] = 1 - myNormPar[i-1];
365   }
366   if ( nbEdges > 1 ) {
367     reverse( myEdge );
368     reverse( myC2d );
369     reverse( myFirst );
370     reverse( myLast );
371     reverse( myNormPar );
372   }
373   myNormPar[nbEdges-1]=1.;
374   myPoints.clear();
375   myFalsePoints.clear();
376 }
377
378 //================================================================================
379 /*!
380  * \brief Show side features
381  */
382 //================================================================================
383
384 void StdMeshers_FaceSide::dump(const char* msg) const
385 {
386 #ifdef _DEBUG_
387   if (msg) MESSAGE ( std::endl << msg );
388   MESSAGE_BEGIN ("NB EDGES: "<< myEdge.size() );
389   MESSAGE_ADD ( "nbPoints: "<<myNbPonits<<" vecSize: " << myPoints.size()<<" "<<myFalsePoints.size() );
390   for ( int i=0; i<myEdge.size(); ++i)
391   {
392     MESSAGE_ADD ( "\t"<<i+1 );
393     MESSAGE_ADD ( "\tEDGE: " );
394     if (myEdge[i].IsNull()) {
395       MESSAGE_ADD ( "NULL" );
396     }
397     else {
398       TopAbs::Print(myEdge[i].Orientation(),cout)<<" "<<myEdge[i].TShape().operator->()<<endl;
399       MESSAGE_ADD ( "\tV1: " << TopExp::FirstVertex( myEdge[i], 1).TShape().operator->()
400                  << "  V2: " << TopExp::LastVertex( myEdge[i], 1).TShape().operator->() );
401     }
402     MESSAGE_ADD ( "\tC2d: ");
403     
404     if (myC2d[i].IsNull()) {
405       MESSAGE_ADD ( "NULL" );
406     }
407     else {
408       MESSAGE_ADD ( myC2d[i].operator->() );
409     }
410       
411     MESSAGE_ADD ( "\tF: "<<myFirst[i]<< " L: "<< myLast[i] );
412     MESSAGE_END ( "\tnormPar: "<<myNormPar[i]<<endl );
413   }
414 #endif
415 }
416
417 //================================================================================
418 /*!
419  * \brief Creates a Adaptor2d_Curve2d to be used in SMESH_Block
420   * \retval Adaptor2d_Curve2d* - 
421  */
422 //================================================================================
423
424 struct Adaptor2dCurve2d : public Adaptor2d_Curve2d
425 {
426   const StdMeshers_FaceSide* mySide;
427   Adaptor2dCurve2d(const StdMeshers_FaceSide* faceSide):mySide(faceSide) {}
428   gp_Pnt2d Value(const Standard_Real U) const { return mySide->Value2d( U ); }
429   Standard_Real FirstParameter() const { return 0; }
430   Standard_Real LastParameter() const { return 1; }
431 };
432
433 Adaptor2d_Curve2d* StdMeshers_FaceSide::GetCurve2d() const
434 {
435   return new Adaptor2dCurve2d( this );
436 }
437
438 //================================================================================
439 /*!
440  * \brief Creates a fully functional Adaptor_Curve
441  */
442 //================================================================================
443
444 BRepAdaptor_CompCurve* StdMeshers_FaceSide::GetCurve3d() const
445 {
446   if ( myEdge.empty() )
447     return 0;
448
449 //   if ( myEdge.size() == 1 )
450 //     return new BRepAdaptor_Curve( myEdge[0] );
451
452   TopoDS_Wire aWire;
453   BRep_Builder aBuilder;
454   aBuilder.MakeWire(aWire);
455   for ( int i=0; i<myEdge.size(); ++i )
456     aBuilder.Add( aWire, myEdge[i] );
457   return new BRepAdaptor_CompCurve( aWire );
458 }
459
460 //================================================================================
461 /*!
462  * \brief Return 2D point by normalized parameter
463   * \param U - normalized parameter value
464   * \retval gp_Pnt2d - point
465  */
466 //================================================================================
467
468 gp_Pnt2d StdMeshers_FaceSide::Value2d(double U) const
469 {
470   if ( !myC2d[0].IsNull() ) {
471     int i = EdgeIndex( U );
472     double prevU = i ? myNormPar[ i-1 ] : 0;
473     double r = ( U - prevU )/ ( myNormPar[ i ] - prevU );
474     return myC2d[ i ]->Value( myFirst[i] * ( 1 - r ) + myLast[i] * r );
475   }
476   return gp_Pnt2d( 1e+100, 1e+100 );
477 }
478
479 //================================================================================
480 /*!
481  * \brief Return wires of a face as StdMeshers_FaceSide's
482  */
483 //================================================================================
484
485 TSideVector StdMeshers_FaceSide::GetFaceWires(const TopoDS_Face& theFace,
486                                               SMESH_Mesh &       theMesh,
487                                               const bool         theIgnoreMediumNodes,
488                                               TError &           theError)
489 {
490   TopoDS_Vertex V1;
491   list< TopoDS_Edge > edges;
492   list< int > nbEdgesInWires;
493   int nbWires = SMESH_Block::GetOrderedEdges (theFace, V1, edges, nbEdgesInWires);
494
495   // split list of all edges into separate wires
496   TSideVector wires( nbWires );
497   list< int >::iterator nbE = nbEdgesInWires.begin();
498   list< TopoDS_Edge >::iterator from, to;
499   from = to = edges.begin();
500   for ( int iW = 0; iW < nbWires; ++iW )
501   {
502     std::advance( to, *nbE++ );
503     list< TopoDS_Edge > wireEdges( from, to );
504     // assure that there is a node on the first vertex
505     // as StdMeshers_FaceSide::GetUVPtStruct() requires
506     while ( !SMESH_Algo::VertexNode( TopExp::FirstVertex( wireEdges.front(), true),
507                                      theMesh.GetMeshDS()))
508     {
509       wireEdges.splice(wireEdges.end(), wireEdges,
510                        wireEdges.begin(), ++wireEdges.begin());
511       if ( from->IsSame( wireEdges.front() )) {
512         theError = TError
513           ( new SMESH_ComputeError(COMPERR_BAD_INPUT_MESH,"No nodes on vertices"));
514         return TSideVector(0);
515       }
516     }
517     // find out side orientation, which is important if there are several wires (PAL19080) 
518     bool isForward = true;
519     if ( nbWires > 1 ) {
520       TopExp_Explorer e( theFace, TopAbs_EDGE );
521       while ( ! e.Current().IsSame( wireEdges.back() ))
522         e.Next();
523       isForward = ( e.Current().Orientation() == wireEdges.back().Orientation() );
524     }
525
526     StdMeshers_FaceSide* wire = new StdMeshers_FaceSide( theFace, wireEdges, &theMesh,
527                                                          isForward, theIgnoreMediumNodes);
528     wires[ iW ] = StdMeshers_FaceSidePtr( wire );
529     from = to;
530   }
531   return wires;
532 }
533