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