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