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