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