Salome HOME
Implementation of Quadrangle (Mapping) for faces built on 3 edges (0018911 from Mantis).
[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_MesherHelper.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   myDefaultPnt2d = gp_Pnt2d( 1e+100, 1e+100 );
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 Constructor of a side for vertex using data from other FaceSide
162   * \param theVertex - the vertex
163   * \param theSide - the side
164  */
165 //================================================================================
166
167 StdMeshers_FaceSide::StdMeshers_FaceSide(const SMDS_MeshNode* theNode,
168                                          const gp_Pnt2d thePnt2d,
169                                          const StdMeshers_FaceSide* theSide)
170 {
171   myC2d.resize(1);
172   myLength = 0;
173   myMesh = theSide->GetMesh();
174   myDefaultPnt2d = thePnt2d;
175
176   myPoints = theSide->GetUVPtStruct();
177   myNbPonits = myNbSegments = myPoints.size();
178   std::vector<uvPtStruct>::iterator it = myPoints.begin();
179   for(; it!=myPoints.end(); it++) {
180     (*it).u = thePnt2d.X();
181     (*it).v = thePnt2d.Y();
182     (*it).y = 0.0;
183     (*it).node = theNode;
184   }
185 }
186
187 //================================================================================
188 /*!
189  * \brief Return info on nodes on the side
190   * \retval UVPtStruct* - array of data structures
191  */
192 //================================================================================
193
194 const vector<UVPtStruct>& StdMeshers_FaceSide::GetUVPtStruct(bool   isXConst,
195                                                              double constValue) const
196 {
197   if ( myPoints.empty() ) {
198
199     if ( NbEdges() == 0 ) return myPoints;
200
201     SMESHDS_Mesh* meshDS = myMesh->GetMeshDS();
202
203     // sort nodes of all edges putting them into a map
204
205     map< double, const SMDS_MeshNode*> u2node;
206     //int nbOnDegen = 0;
207     for ( int i = 0; i < myEdge.size(); ++i ) {
208       // put 1st vertex node
209       TopoDS_Vertex VFirst, VLast;
210       TopExp::Vertices( myEdge[i], VFirst, VLast, true);
211       const SMDS_MeshNode* node = SMESH_Algo::VertexNode( VFirst, meshDS );
212       double prevNormPar = ( i == 0 ? 0 : myNormPar[ i-1 ]); // normalized param
213       if ( node ) { // internal nodes may be missing
214         u2node.insert( make_pair( prevNormPar, node ));
215       }
216       else if ( i == 0 ) {
217         MESSAGE(" NO NODE on VERTEX" );
218         return myPoints;
219       }
220
221       // put 2nd vertex node for a last edge
222       if ( i+1 == myEdge.size() ) {
223         node = SMESH_Algo::VertexNode( VLast, meshDS );
224         if ( !node ) {
225           MESSAGE(" NO NODE on VERTEX" );
226           return myPoints;
227         }
228         u2node.insert( make_pair( 1., node ));
229       }
230
231       // put internal nodes
232       SMESHDS_SubMesh* sm = meshDS->MeshElements( myEdge[i] );
233       if ( !sm ) continue;
234       SMDS_NodeIteratorPtr nItr = sm->GetNodes();
235       double paramSize = myLast[i] - myFirst[i], r = myNormPar[i] - prevNormPar;
236       while ( nItr->more() ) {
237         const SMDS_MeshNode* node = nItr->next();
238         if ( myIgnoreMediumNodes && SMESH_MeshEditor::IsMedium( node, SMDSAbs_Edge ))
239           continue;
240         const SMDS_EdgePosition* epos =
241           static_cast<const SMDS_EdgePosition*>(node->GetPosition().get());
242         double u = epos->GetUParameter();
243         // paramSize is signed so orientation is taken into account
244         double normPar = prevNormPar + r * ( u - myFirst[i] ) / paramSize;
245 #ifdef _DEBUG_
246         if ( normPar > 1 || normPar < 0) {
247           dump("DEBUG");
248           MESSAGE ( "WRONG normPar: "<<normPar<< " prevNormPar="<<prevNormPar
249                     << " u="<<u << " myFirst[i]="<<myFirst[i]<< " myLast[i]="<<myLast[i]
250                     << " paramSize="<<paramSize );
251         }
252 #endif
253         u2node.insert( make_pair( normPar, node ));
254       }
255     }
256     if ( u2node.size() != myNbPonits ) {
257       MESSAGE("Wrong node parameters on edges, u2node.size():"
258               <<u2node.size()<<" !=  myNbPonits:"<<myNbPonits);
259       return myPoints;
260     }
261
262     // fill array of UVPtStruct
263
264     vector<uvPtStruct>* points = const_cast<vector<uvPtStruct>*>( &myPoints );
265     points->resize( myNbPonits );
266
267     int EdgeIndex = 0;
268     double prevNormPar = 0, paramSize = myNormPar[ EdgeIndex ];
269     map< double, const SMDS_MeshNode*>::iterator u_node = u2node.begin();
270     for (int i = 0 ; u_node != u2node.end(); ++u_node, ++i ) {
271       UVPtStruct & uvPt = (*points)[i];
272       uvPt.node = u_node->second;
273       uvPt.x = uvPt.y = uvPt.normParam = u_node->first;
274       if ( isXConst ) uvPt.x = constValue;
275       else            uvPt.y = constValue;
276       if ( myNormPar[ EdgeIndex ] < uvPt.normParam ) {
277         prevNormPar = myNormPar[ EdgeIndex ];
278         ++EdgeIndex;
279 #ifdef _DEBUG_
280         if ( EdgeIndex >= myEdge.size() ) {
281           dump("DEBUG");
282           MESSAGE ( "WRONg EdgeIndex " << 1+EdgeIndex
283                     << " myNormPar.size()="<<myNormPar.size()
284                     << " myNormPar["<< EdgeIndex<<"]="<< myNormPar[ EdgeIndex ]
285                     << " uvPt.normParam="<<uvPt.normParam );
286         }
287 #endif
288         paramSize = myNormPar[ EdgeIndex ] - prevNormPar;
289       }
290       const SMDS_EdgePosition* epos =
291         dynamic_cast<const SMDS_EdgePosition*>(uvPt.node->GetPosition().get());
292       if ( epos ) {
293         uvPt.param = epos->GetUParameter();
294       }
295       else {
296         double r = ( uvPt.normParam - prevNormPar )/ paramSize;
297 //         uvPt.param = myFirst[EdgeIndex] * ( 1 - r ) + myLast[EdgeIndex] * r;
298         uvPt.param = ( r > 0.5 ? myLast[EdgeIndex] : myFirst[EdgeIndex] );
299       }
300       if ( !myC2d[ EdgeIndex ].IsNull() ) {
301         gp_Pnt2d p = myC2d[ EdgeIndex ]->Value( uvPt.param );
302         uvPt.u = p.X();
303         uvPt.v = p.Y();
304       }
305       else {
306         uvPt.u = uvPt.v = 1e+100;
307       }
308     }
309   }
310   return myPoints;
311 }
312
313 //================================================================================
314 /*!
315  * \brief Falsificate info on nodes
316   * \param nbSeg - nb of segments on the side
317   * \retval UVPtStruct* - array of data structures
318  */
319 //================================================================================
320
321 const vector<UVPtStruct>& StdMeshers_FaceSide::SimulateUVPtStruct(int    nbSeg,
322                                                                   bool   isXConst,
323                                                                   double constValue) const
324 {
325   if ( myFalsePoints.empty() ) {
326
327     if ( NbEdges() == 0 ) return myFalsePoints;
328
329     vector<uvPtStruct>* points = const_cast<vector<uvPtStruct>*>( &myFalsePoints );
330     points->resize( nbSeg+1 );
331
332     int EdgeIndex = 0;
333     double prevNormPar = 0, paramSize = myNormPar[ EdgeIndex ];
334     for (int i = 0 ; i < myFalsePoints.size(); ++i ) {
335       double normPar = double(i) / double(nbSeg);
336       UVPtStruct & uvPt = (*points)[i];
337       uvPt.node = 0;
338       uvPt.x = uvPt.y = uvPt.param = uvPt.normParam = normPar;
339       if ( isXConst ) uvPt.x = constValue;
340       else            uvPt.y = constValue;
341       if ( myNormPar[ EdgeIndex ] < normPar ) {
342         prevNormPar = myNormPar[ EdgeIndex ];
343         ++EdgeIndex;
344         paramSize = myNormPar[ EdgeIndex ] - prevNormPar;
345       }
346       double r = ( normPar - prevNormPar )/ paramSize;
347       uvPt.param = myFirst[EdgeIndex] * ( 1 - r ) + myLast[EdgeIndex] * r;
348       if ( !myC2d[ EdgeIndex ].IsNull() ) {
349         gp_Pnt2d p = myC2d[ EdgeIndex ]->Value( uvPt.param );
350         uvPt.u = p.X();
351         uvPt.v = p.Y();
352       }
353       else {
354         uvPt.u = uvPt.v = 1e+100;
355       }
356     }
357   }
358   return myFalsePoints;
359 }
360 // gp_Pnt StdMeshers_FaceSide::Value(double U) const
361 // {
362 // }
363
364 //================================================================================
365 /*!
366  * \brief reverse order of vector elements
367   * \param vec - vector to reverse
368  */
369 //================================================================================
370
371 template <typename T > void reverse(vector<T> & vec)
372 {
373   for ( int f=0, r=vec.size()-1; f < r; ++f, --r )
374     std::swap( vec[f], vec[r] );
375 }
376
377 //================================================================================
378 /*!
379  * \brief Change orientation of side geometry
380  */
381 //================================================================================
382
383 void StdMeshers_FaceSide::Reverse()
384 {
385   int nbEdges = myEdge.size();
386   for ( int i = nbEdges-1; i >= 0; --i ) {
387     std::swap( myFirst[i], myLast[i] );
388     myEdge[i].Reverse();
389     if ( i > 0 ) // at the first loop 1. is overwritten
390       myNormPar[i] = 1 - myNormPar[i-1];
391   }
392   if ( nbEdges > 1 ) {
393     reverse( myEdge );
394     reverse( myC2d );
395     reverse( myFirst );
396     reverse( myLast );
397     reverse( myNormPar );
398   }
399   myNormPar[nbEdges-1]=1.;
400   myPoints.clear();
401   myFalsePoints.clear();
402 }
403
404 //================================================================================
405 /*!
406  * \brief Show side features
407  */
408 //================================================================================
409
410 void StdMeshers_FaceSide::dump(const char* msg) const
411 {
412 #ifdef _DEBUG_
413   if (msg) MESSAGE ( std::endl << msg );
414   MESSAGE_BEGIN ("NB EDGES: "<< myEdge.size() );
415   MESSAGE_ADD ( "nbPoints: "<<myNbPonits<<" vecSize: " << myPoints.size()<<" "<<myFalsePoints.size() );
416   for ( int i=0; i<myEdge.size(); ++i)
417   {
418     MESSAGE_ADD ( "\t"<<i+1 );
419     MESSAGE_ADD ( "\tEDGE: " );
420     if (myEdge[i].IsNull()) {
421       MESSAGE_ADD ( "NULL" );
422     }
423     else {
424       TopAbs::Print(myEdge[i].Orientation(),cout)<<" "<<myEdge[i].TShape().operator->()<<endl;
425       MESSAGE_ADD ( "\tV1: " << TopExp::FirstVertex( myEdge[i], 1).TShape().operator->()
426                  << "  V2: " << TopExp::LastVertex( myEdge[i], 1).TShape().operator->() );
427     }
428     MESSAGE_ADD ( "\tC2d: ");
429     
430     if (myC2d[i].IsNull()) {
431       MESSAGE_ADD ( "NULL" );
432     }
433     else {
434       MESSAGE_ADD ( myC2d[i].operator->() );
435     }
436       
437     MESSAGE_ADD ( "\tF: "<<myFirst[i]<< " L: "<< myLast[i] );
438     MESSAGE_END ( "\tnormPar: "<<myNormPar[i]<<endl );
439   }
440 #endif
441 }
442
443 //================================================================================
444 /*!
445  * \brief Creates a Adaptor2d_Curve2d to be used in SMESH_Block
446   * \retval Adaptor2d_Curve2d* - 
447  */
448 //================================================================================
449
450 struct Adaptor2dCurve2d : public Adaptor2d_Curve2d
451 {
452   const StdMeshers_FaceSide* mySide;
453   Adaptor2dCurve2d(const StdMeshers_FaceSide* faceSide):mySide(faceSide) {}
454   gp_Pnt2d Value(const Standard_Real U) const { return mySide->Value2d( U ); }
455   Standard_Real FirstParameter() const { return 0; }
456   Standard_Real LastParameter() const { return 1; }
457 };
458
459 Adaptor2d_Curve2d* StdMeshers_FaceSide::GetCurve2d() const
460 {
461   return new Adaptor2dCurve2d( this );
462 }
463
464 //================================================================================
465 /*!
466  * \brief Creates a fully functional Adaptor_Curve
467  */
468 //================================================================================
469
470 BRepAdaptor_CompCurve* StdMeshers_FaceSide::GetCurve3d() const
471 {
472   if ( myEdge.empty() )
473     return 0;
474
475 //   if ( myEdge.size() == 1 )
476 //     return new BRepAdaptor_Curve( myEdge[0] );
477
478   TopoDS_Wire aWire;
479   BRep_Builder aBuilder;
480   aBuilder.MakeWire(aWire);
481   for ( int i=0; i<myEdge.size(); ++i )
482     aBuilder.Add( aWire, myEdge[i] );
483   return new BRepAdaptor_CompCurve( aWire );
484 }
485
486 //================================================================================
487 /*!
488  * \brief Return 2D point by normalized parameter
489   * \param U - normalized parameter value
490   * \retval gp_Pnt2d - point
491  */
492 //================================================================================
493
494 gp_Pnt2d StdMeshers_FaceSide::Value2d(double U) const
495 {
496   if ( !myC2d[0].IsNull() ) {
497     int i = EdgeIndex( U );
498     double prevU = i ? myNormPar[ i-1 ] : 0;
499     double r = ( U - prevU )/ ( myNormPar[ i ] - prevU );
500     return myC2d[ i ]->Value( myFirst[i] * ( 1 - r ) + myLast[i] * r );
501   }
502   //return gp_Pnt2d( 1e+100, 1e+100 );
503   return myDefaultPnt2d;
504 }
505
506 //================================================================================
507 /*!
508  * \brief Return wires of a face as StdMeshers_FaceSide's
509  */
510 //================================================================================
511
512 TSideVector StdMeshers_FaceSide::GetFaceWires(const TopoDS_Face& theFace,
513                                               SMESH_Mesh &       theMesh,
514                                               const bool         theIgnoreMediumNodes,
515                                               TError &           theError)
516 {
517   TopoDS_Vertex V1;
518   list< TopoDS_Edge > edges;
519   list< int > nbEdgesInWires;
520   int nbWires = SMESH_Block::GetOrderedEdges (theFace, V1, edges, nbEdgesInWires);
521
522   // split list of all edges into separate wires
523   TSideVector wires( nbWires );
524   list< int >::iterator nbE = nbEdgesInWires.begin();
525   list< TopoDS_Edge >::iterator from, to;
526   from = to = edges.begin();
527   for ( int iW = 0; iW < nbWires; ++iW )
528   {
529     std::advance( to, *nbE++ );
530     list< TopoDS_Edge > wireEdges( from, to );
531     // assure that there is a node on the first vertex
532     // as StdMeshers_FaceSide::GetUVPtStruct() requires
533     while ( !SMESH_Algo::VertexNode( TopExp::FirstVertex( wireEdges.front(), true),
534                                      theMesh.GetMeshDS()))
535     {
536       wireEdges.splice(wireEdges.end(), wireEdges,
537                        wireEdges.begin(), ++wireEdges.begin());
538       if ( from->IsSame( wireEdges.front() )) {
539         theError = TError
540           ( new SMESH_ComputeError(COMPERR_BAD_INPUT_MESH,"No nodes on vertices"));
541         return TSideVector(0);
542       }
543     }
544     // find out side orientation, which is important if there are several wires (PAL19080) 
545     bool isForward = true;
546     if ( nbWires > 1 ) {
547       TopExp_Explorer e( theFace, TopAbs_EDGE );
548       while ( ! e.Current().IsSame( wireEdges.back() ))
549         e.Next();
550       isForward = ( e.Current().Orientation() == wireEdges.back().Orientation() );
551     }
552
553     StdMeshers_FaceSide* wire = new StdMeshers_FaceSide( theFace, wireEdges, &theMesh,
554                                                          isForward, theIgnoreMediumNodes);
555     wires[ iW ] = StdMeshers_FaceSidePtr( wire );
556     from = to;
557   }
558   return wires;
559 }
560