Salome HOME
Merge from BR_V5_DEV 16Feb09
[modules/smesh.git] / src / StdMeshers / StdMeshers_FaceSide.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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 //  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)
26 // Module    : SMESH
27 //
28 #include "StdMeshers_FaceSide.hxx"
29
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_MeshEditor.hxx"
37 #include "SMESH_ComputeError.hxx"
38 #include "SMESH_Block.hxx"
39
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>
45 #include <TopExp.hxx>
46 #include <TopExp_Explorer.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           MESSAGE ( "WRONG normPar: "<<normPar<< " prevNormPar="<<prevNormPar
220                     << " u="<<u << " myFirst[i]="<<myFirst[i]<< " myLast[i]="<<myLast[i]
221                     << " paramSize="<<paramSize );
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           MESSAGE ( "WRONg EdgeIndex " << 1+EdgeIndex
254                     << " myNormPar.size()="<<myNormPar.size()
255                     << " myNormPar["<< EdgeIndex<<"]="<< myNormPar[ EdgeIndex ]
256                     << " uvPt.normParam="<<uvPt.normParam );
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   if (msg) MESSAGE ( std::endl << msg );
385   MESSAGE_BEGIN ("NB EDGES: "<< myEdge.size() );
386   MESSAGE_ADD ( "nbPoints: "<<myNbPonits<<" vecSize: " << myPoints.size()<<" "<<myFalsePoints.size() );
387   for ( int i=0; i<myEdge.size(); ++i)
388   {
389     MESSAGE_ADD ( "\t"<<i+1 );
390     MESSAGE_ADD ( "\tEDGE: " );
391     if (myEdge[i].IsNull()) {
392       MESSAGE_ADD ( "NULL" );
393     }
394     else {
395       TopAbs::Print(myEdge[i].Orientation(),cout)<<" "<<myEdge[i].TShape().operator->()<<endl;
396       MESSAGE_ADD ( "\tV1: " << TopExp::FirstVertex( myEdge[i], 1).TShape().operator->()
397                  << "  V2: " << TopExp::LastVertex( myEdge[i], 1).TShape().operator->() );
398     }
399     MESSAGE_ADD ( "\tC2d: ");
400     
401     if (myC2d[i].IsNull()) {
402       MESSAGE_ADD ( "NULL" );
403     }
404     else {
405       MESSAGE_ADD ( myC2d[i].operator->() );
406     }
407       
408     MESSAGE_ADD ( "\tF: "<<myFirst[i]<< " L: "<< myLast[i] );
409     MESSAGE_END ( "\tnormPar: "<<myNormPar[i]<<endl );
410   }
411 #endif
412 }
413
414 //================================================================================
415 /*!
416  * \brief Creates a Adaptor2d_Curve2d to be used in SMESH_Block
417   * \retval Adaptor2d_Curve2d* - 
418  */
419 //================================================================================
420
421 struct Adaptor2dCurve2d : public Adaptor2d_Curve2d
422 {
423   const StdMeshers_FaceSide* mySide;
424   Adaptor2dCurve2d(const StdMeshers_FaceSide* faceSide):mySide(faceSide) {}
425   gp_Pnt2d Value(const Standard_Real U) const { return mySide->Value2d( U ); }
426   Standard_Real FirstParameter() const { return 0; }
427   Standard_Real LastParameter() const { return 1; }
428 };
429
430 Adaptor2d_Curve2d* StdMeshers_FaceSide::GetCurve2d() const
431 {
432   return new Adaptor2dCurve2d( this );
433 }
434
435 //================================================================================
436 /*!
437  * \brief Creates a fully functional Adaptor_Curve
438  */
439 //================================================================================
440
441 BRepAdaptor_CompCurve* StdMeshers_FaceSide::GetCurve3d() const
442 {
443   if ( myEdge.empty() )
444     return 0;
445
446 //   if ( myEdge.size() == 1 )
447 //     return new BRepAdaptor_Curve( myEdge[0] );
448
449   TopoDS_Wire aWire;
450   BRep_Builder aBuilder;
451   aBuilder.MakeWire(aWire);
452   for ( int i=0; i<myEdge.size(); ++i )
453     aBuilder.Add( aWire, myEdge[i] );
454   return new BRepAdaptor_CompCurve( aWire );
455 }
456
457 //================================================================================
458 /*!
459  * \brief Return 2D point by normalized parameter
460   * \param U - normalized parameter value
461   * \retval gp_Pnt2d - point
462  */
463 //================================================================================
464
465 gp_Pnt2d StdMeshers_FaceSide::Value2d(double U) const
466 {
467   if ( !myC2d[0].IsNull() ) {
468     int i = EdgeIndex( U );
469     double prevU = i ? myNormPar[ i-1 ] : 0;
470     double r = ( U - prevU )/ ( myNormPar[ i ] - prevU );
471     return myC2d[ i ]->Value( myFirst[i] * ( 1 - r ) + myLast[i] * r );
472   }
473   return gp_Pnt2d( 1e+100, 1e+100 );
474 }
475
476 //================================================================================
477 /*!
478  * \brief Return wires of a face as StdMeshers_FaceSide's
479  */
480 //================================================================================
481
482 TSideVector StdMeshers_FaceSide::GetFaceWires(const TopoDS_Face& theFace,
483                                               SMESH_Mesh &       theMesh,
484                                               const bool         theIgnoreMediumNodes,
485                                               TError &           theError)
486 {
487   TopoDS_Vertex V1;
488   list< TopoDS_Edge > edges;
489   list< int > nbEdgesInWires;
490   int nbWires = SMESH_Block::GetOrderedEdges (theFace, V1, edges, nbEdgesInWires);
491
492   // split list of all edges into separate wires
493   TSideVector wires( nbWires );
494   list< int >::iterator nbE = nbEdgesInWires.begin();
495   list< TopoDS_Edge >::iterator from, to;
496   from = to = edges.begin();
497   for ( int iW = 0; iW < nbWires; ++iW )
498   {
499     std::advance( to, *nbE++ );
500     list< TopoDS_Edge > wireEdges( from, to );
501     // assure that there is a node on the first vertex
502     // as StdMeshers_FaceSide::GetUVPtStruct() requires
503     while ( !SMESH_Algo::VertexNode( TopExp::FirstVertex( wireEdges.front(), true),
504                                      theMesh.GetMeshDS()))
505     {
506       wireEdges.splice(wireEdges.end(), wireEdges,
507                        wireEdges.begin(), ++wireEdges.begin());
508       if ( from->IsSame( wireEdges.front() )) {
509         theError = TError
510           ( new SMESH_ComputeError(COMPERR_BAD_INPUT_MESH,"No nodes on vertices"));
511         return TSideVector(0);
512       }
513     }
514     // find out side orientation, which is important if there are several wires (PAL19080) 
515     bool isForward = true;
516     if ( nbWires > 1 ) {
517       TopExp_Explorer e( theFace, TopAbs_EDGE );
518       while ( ! e.Current().IsSame( wireEdges.back() ))
519         e.Next();
520       isForward = ( e.Current().Orientation() == wireEdges.back().Orientation() );
521     }
522
523     StdMeshers_FaceSide* wire = new StdMeshers_FaceSide( theFace, wireEdges, &theMesh,
524                                                          isForward, theIgnoreMediumNodes);
525     wires[ iW ] = StdMeshers_FaceSidePtr( wire );
526     from = to;
527   }
528   return wires;
529 }
530