Salome HOME
PAL16202,16203 (Propagation 1D on edges group)
[modules/smesh.git] / src / StdMeshers / StdMeshers_MEFISTO_2D.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_MEFISTO_2D.cxx
25 //           Moved here from SMESH_MEFISTO_2D.cxx
26 //  Author : Paul RASCLE, EDF
27 //  Module : SMESH
28 //  $Header$
29
30 #include "StdMeshers_MEFISTO_2D.hxx"
31
32 #include "SMESH_Gen.hxx"
33 #include "SMESH_Mesh.hxx"
34 #include "SMESH_subMesh.hxx"
35 #include "SMESH_Block.hxx"
36 #include "SMESH_MesherHelper.hxx"
37 #include "SMESH_Comment.hxx"
38
39 #include "StdMeshers_FaceSide.hxx"
40 #include "StdMeshers_MaxElementArea.hxx"
41 #include "StdMeshers_LengthFromEdges.hxx"
42
43 #include "Rn.h"
44 #include "aptrte.h"
45
46 #include "SMDS_MeshElement.hxx"
47 #include "SMDS_MeshNode.hxx"
48 #include "SMDS_EdgePosition.hxx"
49 #include "SMDS_FacePosition.hxx"
50
51 #include "utilities.h"
52
53 #include <BRepTools.hxx>
54 #include <BRep_Tool.hxx>
55 #include <Geom2d_Curve.hxx>
56 #include <Geom_Surface.hxx>
57 #include <TopExp.hxx>
58 #include <TopExp_Explorer.hxx>
59 #include <TopTools_ListIteratorOfListOfShape.hxx>
60 #include <TopTools_ListOfShape.hxx>
61 #include <TopTools_MapOfShape.hxx>
62 #include <TopoDS.hxx>
63 #include <TopoDS_Edge.hxx>
64 #include <TopoDS_Face.hxx>
65 #include <TopoDS_Iterator.hxx>
66 #include <gp_Pnt2d.hxx>
67
68 using namespace std;
69
70 //=============================================================================
71 /*!
72  *  
73  */
74 //=============================================================================
75
76 StdMeshers_MEFISTO_2D::StdMeshers_MEFISTO_2D(int hypId, int studyId, SMESH_Gen * gen):
77   SMESH_2D_Algo(hypId, studyId, gen)
78 {
79   MESSAGE("StdMeshers_MEFISTO_2D::StdMeshers_MEFISTO_2D");
80   _name = "MEFISTO_2D";
81   _shapeType = (1 << TopAbs_FACE);
82   _compatibleHypothesis.push_back("MaxElementArea");
83   _compatibleHypothesis.push_back("LengthFromEdges");
84
85   _edgeLength = 0;
86   _maxElementArea = 0;
87   _hypMaxElementArea = NULL;
88   _hypLengthFromEdges = NULL;
89   myTool = 0;
90 }
91
92 //=============================================================================
93 /*!
94  *  
95  */
96 //=============================================================================
97
98 StdMeshers_MEFISTO_2D::~StdMeshers_MEFISTO_2D()
99 {
100   MESSAGE("StdMeshers_MEFISTO_2D::~StdMeshers_MEFISTO_2D");
101 }
102
103 //=============================================================================
104 /*!
105  *  
106  */
107 //=============================================================================
108
109 bool StdMeshers_MEFISTO_2D::CheckHypothesis
110                          (SMESH_Mesh&                          aMesh,
111                           const TopoDS_Shape&                  aShape,
112                           SMESH_Hypothesis::Hypothesis_Status& aStatus)
113 {
114   _hypMaxElementArea = NULL;
115   _hypLengthFromEdges = NULL;
116
117   list <const SMESHDS_Hypothesis * >::const_iterator itl;
118   const SMESHDS_Hypothesis *theHyp;
119
120   const list <const SMESHDS_Hypothesis * >&hyps = GetUsedHypothesis(aMesh, aShape);
121   int nbHyp = hyps.size();
122   if (!nbHyp)
123   {
124     aStatus = SMESH_Hypothesis::HYP_MISSING;
125     return false;  // can't work with no hypothesis
126   }
127
128   itl = hyps.begin();
129   theHyp = (*itl); // use only the first hypothesis
130
131   string hypName = theHyp->GetName();
132
133   bool isOk = false;
134
135   if (hypName == "MaxElementArea")
136   {
137     _hypMaxElementArea = static_cast<const StdMeshers_MaxElementArea *>(theHyp);
138     ASSERT(_hypMaxElementArea);
139     _maxElementArea = _hypMaxElementArea->GetMaxArea();
140     _edgeLength = 0;
141     isOk = true;
142     aStatus = SMESH_Hypothesis::HYP_OK;
143   }
144
145   else if (hypName == "LengthFromEdges")
146   {
147     _hypLengthFromEdges = static_cast<const StdMeshers_LengthFromEdges *>(theHyp);
148     ASSERT(_hypLengthFromEdges);
149     _edgeLength = 0;
150     _maxElementArea = 0;
151     isOk = true;
152     aStatus = SMESH_Hypothesis::HYP_OK;
153   }
154   else
155     aStatus = SMESH_Hypothesis::HYP_INCOMPATIBLE;
156
157   if (isOk)
158   {
159     isOk = false;
160     if (_maxElementArea > 0)
161     {
162       //_edgeLength = 2 * sqrt(_maxElementArea);        // triangles : minorant
163       _edgeLength = sqrt(2. * _maxElementArea/sqrt(3.0));
164       isOk = true;
165     }
166     else
167       isOk = (_hypLengthFromEdges != NULL);     // **** check mode
168     if (!isOk)
169       aStatus = SMESH_Hypothesis::HYP_BAD_PARAMETER;
170   }
171
172   return isOk;
173 }
174
175 //=============================================================================
176 /*!
177  *  
178  */
179 //=============================================================================
180
181 bool StdMeshers_MEFISTO_2D::Compute(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
182 {
183   MESSAGE("StdMeshers_MEFISTO_2D::Compute");
184
185   TopoDS_Face F = TopoDS::Face(aShape.Oriented(TopAbs_FORWARD));
186
187   // helper builds quadratic mesh if necessary
188   SMESH_MesherHelper helper(aMesh);
189   myTool = &helper;
190   _quadraticMesh = myTool->IsQuadraticSubMesh(aShape);
191   const bool ignoreMediumNodes = _quadraticMesh;
192
193   // get all edges of a face
194   TError problem;
195   TWireVector wires = StdMeshers_FaceSide::GetFaceWires( F, aMesh, ignoreMediumNodes, problem );
196   int nbWires = wires.size();
197   if ( problem && !problem->IsOK() ) return error( problem );
198   if ( nbWires == 0 ) return error( "Problem in StdMeshers_FaceSide::GetFaceWires()");
199   if ( wires[0]->NbSegments() < 3 ) // ex: a circle with 2 segments
200     return error(COMPERR_BAD_INPUT_MESH,
201                  SMESH_Comment("Too few segments: ")<<wires[0]->NbSegments());
202
203   // compute average edge length
204   if (_hypLengthFromEdges)
205   {
206     _edgeLength = 0;
207     int nbSegments = 0;
208     for ( int iW = 0; iW < nbWires; ++iW )
209     {
210       StdMeshers_FaceSidePtr wire = wires[ iW ];
211       _edgeLength += wire->Length();
212       nbSegments  += wire->NbSegments();
213     }
214     if ( nbSegments )
215       _edgeLength /= nbSegments;
216   }
217
218   if (_hypLengthFromEdges && _edgeLength < DBL_MIN )
219     _edgeLength = 100;
220
221   Z nblf;                 //nombre de lignes fermees (enveloppe en tete)
222   Z *nudslf = NULL;       //numero du dernier sommet de chaque ligne fermee
223   R2 *uvslf = NULL;       
224   Z nbpti = 0;            //nombre points internes futurs sommets de la triangulation
225   R2 *uvpti = NULL;
226   
227   Z nbst;
228   R2 *uvst = NULL;
229   Z nbt;
230   Z *nust = NULL;
231   Z ierr = 0;
232
233   Z nutysu = 1;           // 1: il existe un fonction areteideale_()
234   // Z  nutysu=0;         // 0: on utilise aretmx
235   R aretmx = _edgeLength; // longueur max aretes future triangulation
236   
237   nblf = nbWires;
238   
239   nudslf = new Z[1 + nblf];
240   nudslf[0] = 0;
241   int iw = 1;
242   int nbpnt = 0;
243
244   // count nb of input points
245   for ( int iW = 0; iW < nbWires; ++iW )
246   {
247     nbpnt += wires[iW]->NbPoints() - 1;
248     nudslf[iw++] = nbpnt;
249   }
250
251   uvslf = new R2[nudslf[nblf]];
252
253   double scalex, scaley;
254   ComputeScaleOnFace(aMesh, F, scalex, scaley);
255
256   // correspondence mefisto index --> Nodes
257   vector< const SMDS_MeshNode*> mefistoToDS(nbpnt, (const SMDS_MeshNode*)0);
258
259   bool isOk = false;
260
261   // fill input points UV
262   if ( LoadPoints(wires, uvslf, mefistoToDS, scalex, scaley) )
263   {
264     // Compute
265     aptrte(nutysu, aretmx,
266            nblf, nudslf, uvslf, nbpti, uvpti, nbst, uvst, nbt, nust, ierr);
267
268     if (ierr == 0)
269     {
270       MESSAGE("... End Triangulation Generated Triangle Number " << nbt);
271       MESSAGE("                                    Node Number " << nbst);
272       StoreResult(nbst, uvst, nbt, nust, mefistoToDS, scalex, scaley);
273       isOk = true;
274     }
275     else
276     {
277       error(ierr,"Error in Triangulation (aptrte())");
278     }
279   }
280   if (nudslf != NULL) delete[]nudslf;
281   if (uvslf != NULL)  delete[]uvslf;
282   if (uvst != NULL)   delete[]uvst;
283   if (nust != NULL)   delete[]nust;
284
285   return isOk;
286 }
287
288 //=======================================================================
289 //function : fixOverlappedLinkUV
290 //purpose  : prevent failure due to overlapped adjacent links
291 //=======================================================================
292
293 static bool fixOverlappedLinkUV( R2& uv0, const R2& uv1, const R2& uv2 )
294 {
295   gp_XY v1( uv0.x - uv1.x, uv0.y - uv1.y );
296   gp_XY v2( uv2.x - uv1.x, uv2.y - uv1.y );
297
298   double tol2 = DBL_MIN * DBL_MIN;
299   double sqMod1 = v1.SquareModulus();
300   if ( sqMod1 <= tol2 ) return false;
301   double sqMod2 = v2.SquareModulus();
302   if ( sqMod2 <= tol2 ) return false;
303
304   double dot = v1*v2;
305
306   // check sinus >= 1.e-3
307   const double minSin = 1.e-3;
308   if ( dot > 0 && 1 - dot * dot / ( sqMod1 * sqMod2 ) < minSin * minSin ) {
309     MESSAGE(" ___ FIX UV ____" << uv0.x << " " << uv0.y);
310     v1.SetCoord( -v1.Y(), v1.X() );
311     double delta = sqrt( sqMod1 ) * minSin;
312     if ( v1.X() < 0 )
313       uv0.x -= delta;
314     else
315       uv0.x += delta;
316     if ( v1.Y() < 0 )
317       uv0.y -= delta;
318     else
319       uv0.y += delta;
320 // #ifdef _DEBUG_
321 //     MESSAGE(" -> " << uv0.x << " " << uv0.y << " ");
322 //     MESSAGE("v1( " << v1.X() << " " << v1.Y() << " ) " <<
323 //       "v2( " << v2.X() << " " << v2.Y() << " ) ");
324 //    MESSAGE("SIN: " << sqrt(1 - dot * dot / (sqMod1 * sqMod2)));
325 //     v1.SetCoord( uv0.x - uv1.x, uv0.y - uv1.y );
326 //     v2.SetCoord( uv2.x - uv1.x, uv2.y - uv1.y );
327 //     gp_XY v3( uv2.x - uv0.x, uv2.y - uv0.y );
328 //     sqMod1 = v1.SquareModulus();
329 //     sqMod2 = v2.SquareModulus();
330 //     dot = v1*v2;
331 //     double sin = sqrt(1 - dot * dot / (sqMod1 * sqMod2));
332 //     MESSAGE("NEW SIN: " << sin);
333 // #endif
334     return true;
335   }
336   return false;
337 }
338
339 //=======================================================================
340 //function : fixCommonVertexUV
341 //purpose  : 
342 //=======================================================================
343
344 static bool fixCommonVertexUV (R2 &                 theUV,
345                                const TopoDS_Vertex& theV,
346                                const TopoDS_Face&   theF,
347                                const TopTools_IndexedDataMapOfShapeListOfShape & theVWMap,
348                                SMESH_Mesh &         theMesh,
349                                const double         theScaleX,
350                                const double         theScaleY,
351                                const bool           theCreateQuadratic)
352 {
353   if( !theVWMap.Contains( theV )) return false;
354
355   // check if there is another wire sharing theV
356   const TopTools_ListOfShape& WList = theVWMap.FindFromKey( theV );
357   TopTools_ListIteratorOfListOfShape aWIt;
358   TopTools_MapOfShape aWires;
359   for ( aWIt.Initialize( WList ); aWIt.More(); aWIt.Next() )
360     aWires.Add( aWIt.Value() );
361   if ( aWires.Extent() < 2 ) return false;
362
363   TopoDS_Shape anOuterWire = BRepTools::OuterWire(theF);
364   TopoDS_Shape anInnerWire;
365   for ( aWIt.Initialize( WList ); aWIt.More() && anInnerWire.IsNull(); aWIt.Next() )
366     if ( !anOuterWire.IsSame( aWIt.Value() ))
367       anInnerWire = aWIt.Value();
368
369   TopTools_ListOfShape EList;
370   list< double >       UList;
371
372   // find edges of theW sharing theV
373   // and find 2d normal to them at theV
374   gp_Vec2d N(0.,0.);
375   TopoDS_Iterator itE( anInnerWire );
376   for (  ; itE.More(); itE.Next() )
377   {
378     const TopoDS_Edge& E = TopoDS::Edge( itE.Value() );
379     TopoDS_Iterator itV( E );
380     for ( ; itV.More(); itV.Next() )
381     {
382       const TopoDS_Vertex & V = TopoDS::Vertex( itV.Value() );
383       if ( !V.IsSame( theV ))
384         continue;
385       EList.Append( E );
386       Standard_Real u = BRep_Tool::Parameter( V, E );
387       UList.push_back( u );
388       double f, l;
389       Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface(E, theF, f, l);
390       gp_Vec2d d1;
391       gp_Pnt2d p;
392       C2d->D1( u, p, d1 );
393       gp_Vec2d n( d1.Y() * theScaleX, -d1.X() * theScaleY);
394       if ( E.Orientation() == TopAbs_REVERSED )
395         n.Reverse();
396       N += n.Normalized();
397     }
398   }
399
400   // define step size by which to move theUV
401
402   gp_Pnt2d nextUV; // uv of next node on edge, most distant of the four
403   gp_Pnt2d thisUV( theUV.x, theUV.y );
404   double maxDist = -DBL_MAX;
405   TopTools_ListIteratorOfListOfShape aEIt (EList);
406   list< double >::iterator aUIt = UList.begin();
407   for ( ; aEIt.More(); aEIt.Next(), aUIt++ )
408   {
409     const TopoDS_Edge& E = TopoDS::Edge( aEIt.Value() );
410     double f, l;
411     Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface(E, theF, f, l);
412
413     double umin = DBL_MAX, umax = -DBL_MAX;
414     SMDS_NodeIteratorPtr nIt = theMesh.GetSubMesh(E)->GetSubMeshDS()->GetNodes();
415     if ( !nIt->more() ) // no nodes on edge, only on vertices
416     {
417       umin = l;
418       umax = f;
419     }
420     else {
421       while ( nIt->more() ) {
422         const SMDS_MeshNode* node = nIt->next();
423         // check if node is medium
424         if ( theCreateQuadratic && SMESH_MesherHelper::IsMedium( node, SMDSAbs_Edge ))
425           continue;
426         const SMDS_EdgePosition* epos =
427           static_cast<const SMDS_EdgePosition*>(node->GetPosition().get());
428         double u = epos->GetUParameter();
429         if ( u < umin )
430           umin = u;
431         if ( u > umax )
432           umax = u;
433       }
434     }
435     bool isFirstCommon = ( *aUIt == f );
436     gp_Pnt2d uv = C2d->Value( isFirstCommon ? umin : umax );
437     double dist = thisUV.SquareDistance( uv );
438     if ( dist > maxDist ) {
439       maxDist = dist;
440       nextUV  = uv;
441     }
442   }
443   R2 uv0, uv1, uv2;
444   uv0.x = thisUV.X();   uv0.y = thisUV.Y();
445   uv1.x = nextUV.X();   uv1.y = nextUV.Y(); 
446   uv2.x = thisUV.X();   uv2.y = thisUV.Y();
447
448   uv1.x *= theScaleX;   uv1.y *= theScaleY; 
449
450   if ( fixOverlappedLinkUV( uv0, uv1, uv2 ))
451   {
452     double step = thisUV.Distance( gp_Pnt2d( uv0.x, uv0.y ));
453
454     // move theUV along the normal by the step
455
456     N *= step;
457
458     MESSAGE("--fixCommonVertexUV move(" << theUV.x << " " << theUV.x
459             << ") by (" << N.X() << " " << N.Y() << ")" 
460             << endl << "--- MAX DIST " << maxDist);
461
462     theUV.x += N.X();
463     theUV.y += N.Y();
464
465     return true;
466   }
467   return false;
468 }
469
470 //=============================================================================
471 /*!
472  *  
473  */
474 //=============================================================================
475
476 bool StdMeshers_MEFISTO_2D::LoadPoints(TWireVector &                 wires,
477                                        R2 *                          uvslf,
478                                        vector<const SMDS_MeshNode*>& mefistoToDS,
479                                        double                        scalex,
480                                        double                        scaley)
481 {
482   // to avoid passing same uv points for a vertex common to 2 wires
483   TopoDS_Face F;
484   TopTools_IndexedDataMapOfShapeListOfShape VWMap;
485   if ( wires.size() > 1 )
486   {
487     F = TopoDS::Face( myTool->GetSubShape() );
488     TopExp::MapShapesAndAncestors( F, TopAbs_VERTEX, TopAbs_WIRE, VWMap );
489     int nbVertices = 0;
490     for ( int iW = 0; iW < wires.size(); ++iW )
491       nbVertices += wires[ iW ]->NbEdges();
492     if ( nbVertices == VWMap.Extent() )
493       VWMap.Clear(); // wires have no common vertices
494   }
495
496   int m = 0;
497   list< int > mOnVertex;
498
499   for ( int iW = 0; iW < wires.size(); ++iW )
500   {
501     const vector<UVPtStruct>& uvPtVec = wires[ iW ]->GetUVPtStruct();
502     if ( uvPtVec.size() != wires[ iW ]->NbPoints() ) {
503       return error(COMPERR_BAD_INPUT_MESH,SMESH_Comment("Unexpected nb of points on wire ")
504                    << iW << uvPtVec.size()<<" != "<<wires[ iW ]->NbPoints());
505     }
506     if ( m + uvPtVec.size()-1 > mefistoToDS.size() ) {
507       MESSAGE("Wrong mefistoToDS.size: "<<mefistoToDS.size()<<" < "<<m + uvPtVec.size()-1);
508       return error("Internal error");
509     }
510
511     vector<UVPtStruct>::const_iterator uvPt = uvPtVec.begin();
512     for ( ++uvPt; uvPt != uvPtVec.end(); ++uvPt )
513     {
514       // bind mefisto ID to node
515       mefistoToDS[m] = uvPt->node;
516       // set UV
517       uvslf[m].x = uvPt->u * scalex;
518       uvslf[m].y = uvPt->v * scaley;
519       if ( uvPt->node->GetPosition()->GetTypeOfPosition() == SMDS_TOP_VERTEX )
520         mOnVertex.push_back( m );
521       m++;
522     }
523
524     int mFirst = mOnVertex.front(), mLast = m - 1;
525     list< int >::iterator mIt = mOnVertex.begin();
526     for ( ; mIt != mOnVertex.end(); ++mIt)
527     {
528       int m = *mIt;
529       if ( iW && !VWMap.IsEmpty()) { // except outer wire
530         // avoid passing same uv point for a vertex common to 2 wires
531         int vID = mefistoToDS[m]->GetPosition()->GetShapeId();
532         TopoDS_Vertex V = TopoDS::Vertex( myTool->GetMeshDS()->IndexToShape( vID ));
533         if ( fixCommonVertexUV( uvslf[m], V, F, VWMap, *myTool->GetMesh(),
534                                 scalex, scaley, _quadraticMesh )) {
535           myNodesOnCommonV.push_back( mefistoToDS[m] );
536           continue;
537         }
538       }
539       // prevent failure on overlapped adjacent links,
540       // check only links ending in vertex nodes
541       int mB = m - 1, mA = m + 1; // indices Before and After
542       if ( mB < mFirst ) mB = mLast;
543       if ( mA > mLast )  mA = mFirst;
544       fixOverlappedLinkUV (uvslf[ mB ], uvslf[ m ], uvslf[ mA ]);
545     }
546   }
547
548   return true;
549 }
550
551 //=============================================================================
552 /*!
553  *  
554  */
555 //=============================================================================
556
557 void StdMeshers_MEFISTO_2D::ComputeScaleOnFace(SMESH_Mesh &        aMesh,
558                                                const TopoDS_Face & aFace,
559                                                double &            scalex,
560                                                double &            scaley)
561 {
562   TopoDS_Wire W = BRepTools::OuterWire(aFace);
563
564   double xmin = 1.e300;         // min & max of face 2D parametric coord.
565   double xmax = -1.e300;
566   double ymin = 1.e300;
567   double ymax = -1.e300;
568   int nbp = 23;
569   scalex = 1;
570   scaley = 1;
571
572   TopExp_Explorer wexp(W, TopAbs_EDGE);
573   for ( ; wexp.More(); wexp.Next())
574   {
575     const TopoDS_Edge & E = TopoDS::Edge( wexp.Current() );
576     double f, l;
577     Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface(E, aFace, f, l);
578     if ( C2d.IsNull() ) continue;
579     double du = (l - f) / double (nbp);
580     for (int i = 0; i <= nbp; i++)
581     {
582       double param = f + double (i) * du;
583       gp_Pnt2d p = C2d->Value(param);
584       if (p.X() < xmin)
585         xmin = p.X();
586       if (p.X() > xmax)
587         xmax = p.X();
588       if (p.Y() < ymin)
589         ymin = p.Y();
590       if (p.Y() > ymax)
591         ymax = p.Y();
592       //    MESSAGE(" "<< f<<" "<<l<<" "<<param<<" "<<xmin<<" "<<xmax<<" "<<ymin<<" "<<ymax);
593     }
594   }
595   //   SCRUTE(xmin);
596   //   SCRUTE(xmax);
597   //   SCRUTE(ymin);
598   //   SCRUTE(ymax);
599   double xmoy = (xmax + xmin) / 2.;
600   double ymoy = (ymax + ymin) / 2.;
601   double xsize = xmax - xmin;
602   double ysize = ymax - ymin;
603
604   TopLoc_Location L;
605   Handle(Geom_Surface) S = BRep_Tool::Surface(aFace,L);       // 3D surface
606
607   double length_x = 0;
608   double length_y = 0;
609   gp_Pnt PX0 = S->Value(xmin, ymoy);
610   gp_Pnt PY0 = S->Value(xmoy, ymin);
611   double dx = xsize / double (nbp);
612   double dy = ysize / double (nbp);
613   for (int i = 1; i <= nbp; i++)
614   {
615     double x = xmin + double (i) * dx;
616     gp_Pnt PX = S->Value(x, ymoy);
617     double y = ymin + double (i) * dy;
618     gp_Pnt PY = S->Value(xmoy, y);
619     length_x += PX.Distance(PX0);
620     length_y += PY.Distance(PY0);
621     PX0 = PX;
622     PY0 = PY;
623   }
624   scalex = length_x / xsize;
625   scaley = length_y / ysize;
626 //   SCRUTE(xsize);
627 //   SCRUTE(ysize);
628   double xyratio = xsize*scalex/(ysize*scaley);
629   const double maxratio = 1.e2;
630   //SCRUTE(xyratio);
631   if (xyratio > maxratio) {
632     SCRUTE( scaley );
633     scaley *= xyratio / maxratio;
634     SCRUTE( scaley );
635   }
636   else if (xyratio < 1./maxratio) {
637     SCRUTE( scalex );
638     scalex *= 1 / xyratio / maxratio;
639     SCRUTE( scalex );
640   }
641   ASSERT(scalex);
642   ASSERT(scaley);
643 }
644
645 //=============================================================================
646 /*!
647  *  
648  */
649 //=============================================================================
650
651 void StdMeshers_MEFISTO_2D::StoreResult(Z nbst, R2 * uvst, Z nbt, Z * nust,
652                                         vector< const SMDS_MeshNode*>&mefistoToDS,
653                                         double scalex, double scaley)
654 {
655   SMESHDS_Mesh * meshDS = myTool->GetMeshDS();
656   int faceID = myTool->GetSubShapeID();
657
658   TopoDS_Face F = TopoDS::Face( myTool->GetSubShape() );
659   Handle(Geom_Surface) S = BRep_Tool::Surface( F );
660
661   Z n = mefistoToDS.size(); // nb input points
662   mefistoToDS.resize( nbst );
663   for ( ; n < nbst; n++)
664   {
665     if (!mefistoToDS[n])
666     {
667       double u = uvst[n][0] / scalex;
668       double v = uvst[n][1] / scaley;
669       gp_Pnt P = S->Value(u, v);
670
671       SMDS_MeshNode * node = meshDS->AddNode(P.X(), P.Y(), P.Z());
672       meshDS->SetNodeOnFace(node, faceID, u, v);
673
674       //MESSAGE(P.X()<<" "<<P.Y()<<" "<<P.Z());
675       mefistoToDS[n] = node;
676       //MESSAGE("NEW: "<<n<<" "<<mefistoToDS[n+1]);
677     }
678   }
679
680   Z m = 0;
681
682   // triangle points must be in trigonometric order if face is Forward
683   // else they must be put clockwise
684
685   bool triangleIsWellOriented = ( F.Orientation() == TopAbs_FORWARD );
686
687   for (n = 1; n <= nbt; n++)
688   {
689     const SMDS_MeshNode * n1 = mefistoToDS[ nust[m++] - 1 ];
690     const SMDS_MeshNode * n2 = mefistoToDS[ nust[m++] - 1 ];
691     const SMDS_MeshNode * n3 = mefistoToDS[ nust[m++] - 1 ];
692
693     SMDS_MeshElement * elt;
694     if (triangleIsWellOriented)
695       elt = myTool->AddFace(n1, n2, n3);
696     else
697       elt = myTool->AddFace(n1, n3, n2);
698
699     meshDS->SetMeshElementOnShape(elt, faceID);
700     m++;
701   }
702
703   // remove bad elements built on vertices shared by wires
704
705   list<const SMDS_MeshNode*>::iterator itN = myNodesOnCommonV.begin();
706   for ( ; itN != myNodesOnCommonV.end(); itN++ )
707   {
708     const SMDS_MeshNode* node = *itN;
709     SMDS_ElemIteratorPtr invElemIt = node->GetInverseElementIterator();
710     while ( invElemIt->more() )
711     {
712       const SMDS_MeshElement* elem = invElemIt->next();
713       SMDS_ElemIteratorPtr itN = elem->nodesIterator();
714       int nbSame = 0;
715       while ( itN->more() )
716         if ( itN->next() == node)
717           nbSame++;
718       if (nbSame > 1) {
719         MESSAGE( "RM bad element " << elem->GetID());
720         meshDS->RemoveElement( elem );
721       }
722     }
723   }
724 }