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