1 // Copyright (C) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // SMESH SMESH : implementaion of SMESH idl descriptions
24 // File : StdMeshers_MEFISTO_2D.cxx
25 // Moved here from SMESH_MEFISTO_2D.cxx
26 // Author : Paul RASCLE, EDF
29 #include "StdMeshers_MEFISTO_2D.hxx"
31 #include "SMESH_Gen.hxx"
32 #include "SMESH_Mesh.hxx"
33 #include "SMESH_subMesh.hxx"
34 #include "SMESH_Block.hxx"
35 #include "SMESH_MesherHelper.hxx"
36 #include "SMESH_Comment.hxx"
38 #include "StdMeshers_FaceSide.hxx"
39 #include "StdMeshers_MaxElementArea.hxx"
40 #include "StdMeshers_LengthFromEdges.hxx"
45 #include "SMDS_MeshElement.hxx"
46 #include "SMDS_MeshNode.hxx"
47 #include "SMDS_EdgePosition.hxx"
48 #include "SMDS_FacePosition.hxx"
50 #include "utilities.h"
52 #include <BRepTools.hxx>
53 #include <BRep_Tool.hxx>
54 #include <Geom_Curve.hxx>
55 #include <Geom2d_Curve.hxx>
56 #include <Geom_Surface.hxx>
57 #include <Precision.hxx>
59 #include <TopExp_Explorer.hxx>
60 #include <TopTools_ListIteratorOfListOfShape.hxx>
61 #include <TopTools_ListOfShape.hxx>
62 #include <TopTools_MapOfShape.hxx>
64 #include <TopoDS_Edge.hxx>
65 #include <TopoDS_Face.hxx>
66 #include <TopoDS_Iterator.hxx>
67 #include <gp_Pnt2d.hxx>
69 #include <BRep_Tool.hxx>
70 #include <GProp_GProps.hxx>
71 #include <BRepGProp.hxx>
75 //=============================================================================
79 //=============================================================================
81 StdMeshers_MEFISTO_2D::StdMeshers_MEFISTO_2D(int hypId, int studyId, SMESH_Gen * gen):
82 SMESH_2D_Algo(hypId, studyId, gen)
84 MESSAGE("StdMeshers_MEFISTO_2D::StdMeshers_MEFISTO_2D");
86 _shapeType = (1 << TopAbs_FACE);
87 _compatibleHypothesis.push_back("MaxElementArea");
88 _compatibleHypothesis.push_back("LengthFromEdges");
92 _hypMaxElementArea = NULL;
93 _hypLengthFromEdges = NULL;
97 //=============================================================================
101 //=============================================================================
103 StdMeshers_MEFISTO_2D::~StdMeshers_MEFISTO_2D()
105 MESSAGE("StdMeshers_MEFISTO_2D::~StdMeshers_MEFISTO_2D");
108 //=============================================================================
112 //=============================================================================
114 bool StdMeshers_MEFISTO_2D::CheckHypothesis
116 const TopoDS_Shape& aShape,
117 SMESH_Hypothesis::Hypothesis_Status& aStatus)
119 _hypMaxElementArea = NULL;
120 _hypLengthFromEdges = NULL;
124 list <const SMESHDS_Hypothesis * >::const_iterator itl;
125 const SMESHDS_Hypothesis *theHyp;
127 const list <const SMESHDS_Hypothesis * >&hyps = GetUsedHypothesis(aMesh, aShape);
128 int nbHyp = hyps.size();
131 aStatus = SMESH_Hypothesis::HYP_OK; //SMESH_Hypothesis::HYP_MISSING;
132 return true; // (PAL13464) can work with no hypothesis, LengthFromEdges is default one
136 theHyp = (*itl); // use only the first hypothesis
138 string hypName = theHyp->GetName();
142 if (hypName == "MaxElementArea")
144 _hypMaxElementArea = static_cast<const StdMeshers_MaxElementArea *>(theHyp);
145 ASSERT(_hypMaxElementArea);
146 _maxElementArea = _hypMaxElementArea->GetMaxArea();
148 aStatus = SMESH_Hypothesis::HYP_OK;
151 else if (hypName == "LengthFromEdges")
153 _hypLengthFromEdges = static_cast<const StdMeshers_LengthFromEdges *>(theHyp);
154 ASSERT(_hypLengthFromEdges);
156 aStatus = SMESH_Hypothesis::HYP_OK;
159 aStatus = SMESH_Hypothesis::HYP_INCOMPATIBLE;
164 if (_maxElementArea > 0)
166 //_edgeLength = 2 * sqrt(_maxElementArea); // triangles : minorant
167 _edgeLength = sqrt(2. * _maxElementArea/sqrt(3.0));
171 isOk = (_hypLengthFromEdges != NULL); // **** check mode
173 aStatus = SMESH_Hypothesis::HYP_BAD_PARAMETER;
179 //=============================================================================
183 //=============================================================================
185 bool StdMeshers_MEFISTO_2D::Compute(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
187 MESSAGE("StdMeshers_MEFISTO_2D::Compute");
189 TopoDS_Face F = TopoDS::Face(aShape.Oriented(TopAbs_FORWARD));
191 // helper builds quadratic mesh if necessary
192 SMESH_MesherHelper helper(aMesh);
194 _quadraticMesh = myTool->IsQuadraticSubMesh(aShape);
195 const bool ignoreMediumNodes = _quadraticMesh;
197 // get all edges of a face
199 TWireVector wires = StdMeshers_FaceSide::GetFaceWires( F, aMesh, ignoreMediumNodes, problem );
200 int nbWires = wires.size();
201 if ( problem && !problem->IsOK() ) return error( problem );
202 if ( nbWires == 0 ) return error( "Problem in StdMeshers_FaceSide::GetFaceWires()");
203 if ( wires[0]->NbSegments() < 3 ) // ex: a circle with 2 segments
204 return error(COMPERR_BAD_INPUT_MESH,
205 SMESH_Comment("Too few segments: ")<<wires[0]->NbSegments());
207 // compute average edge length
208 if (!_hypMaxElementArea)
212 for ( int iW = 0; iW < nbWires; ++iW )
214 StdMeshers_FaceSidePtr wire = wires[ iW ];
215 _edgeLength += wire->Length();
216 nbSegments += wire->NbSegments();
219 _edgeLength /= nbSegments;
222 if (/*_hypLengthFromEdges &&*/ _edgeLength < DBL_MIN )
225 Z nblf; //nombre de lignes fermees (enveloppe en tete)
226 Z *nudslf = NULL; //numero du dernier sommet de chaque ligne fermee
228 Z nbpti = 0; //nombre points internes futurs sommets de la triangulation
237 Z nutysu = 1; // 1: il existe un fonction areteideale_()
238 // Z nutysu=0; // 0: on utilise aretmx
239 R aretmx = _edgeLength; // longueur max aretes future triangulation
243 nudslf = new Z[1 + nblf];
248 // count nb of input points
249 for ( int iW = 0; iW < nbWires; ++iW )
251 nbpnt += wires[iW]->NbPoints() - 1;
252 nudslf[iw++] = nbpnt;
255 uvslf = new R2[nudslf[nblf]];
257 double scalex, scaley;
258 ComputeScaleOnFace(aMesh, F, scalex, scaley);
260 // correspondence mefisto index --> Nodes
261 vector< const SMDS_MeshNode*> mefistoToDS(nbpnt, (const SMDS_MeshNode*)0);
265 // fill input points UV
266 if ( LoadPoints(wires, uvslf, mefistoToDS, scalex, scaley) )
269 aptrte(nutysu, aretmx,
270 nblf, nudslf, uvslf, nbpti, uvpti, nbst, uvst, nbt, nust, ierr);
274 MESSAGE("... End Triangulation Generated Triangle Number " << nbt);
275 MESSAGE(" Node Number " << nbst);
276 StoreResult(nbst, uvst, nbt, nust, mefistoToDS, scalex, scaley);
281 error(ierr,"Error in Triangulation (aptrte())");
284 if (nudslf != NULL) delete[]nudslf;
285 if (uvslf != NULL) delete[]uvslf;
286 if (uvst != NULL) delete[]uvst;
287 if (nust != NULL) delete[]nust;
293 //=============================================================================
297 //=============================================================================
299 bool StdMeshers_MEFISTO_2D::Evaluate(SMESH_Mesh & aMesh,
300 const TopoDS_Shape & aShape,
301 MapShapeNbElems& aResMap)
303 MESSAGE("StdMeshers_MEFISTO_2D::Evaluate");
305 TopoDS_Face F = TopoDS::Face(aShape.Oriented(TopAbs_FORWARD));
309 bool IsQuadratic = false;
311 TopExp_Explorer exp(F,TopAbs_EDGE);
312 for(; exp.More(); exp.Next()) {
313 TopoDS_Edge E = TopoDS::Edge(exp.Current());
314 MapShapeNbElemsItr anIt = aResMap.find( aMesh.GetSubMesh(E) );
315 if( anIt == aResMap.end() ) continue;
316 std::vector<int> aVec = (*anIt).second;
317 int nbe = Max(aVec[SMDSEntity_Edge],aVec[SMDSEntity_Quad_Edge]);
320 IsQuadratic = ( aVec[SMDSEntity_Quad_Edge] > aVec[SMDSEntity_Edge] );
325 Handle(Geom_Curve) C = BRep_Tool::Curve(E,L,a,b);
328 double dp = (b-a)/nbe;
329 for(int i=1; i<=nbe; i++) {
332 aLen += P1.Distance(P2);
337 std::vector<int> aResVec(SMDSEntity_Last);
338 for(int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aResVec[i] = 0;
339 SMESH_subMesh * sm = aMesh.GetSubMesh(aShape);
340 aResMap.insert(std::make_pair(sm,aResVec));
341 SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
342 smError.reset( new SMESH_ComputeError(COMPERR_ALGO_FAILED,
343 "Submesh can not be evaluated",this));
346 aLen = aLen/NbSeg; // middle length
348 _edgeLength = Precision::Infinite();
349 double tmpLength = Min( _edgeLength, aLen );
352 BRepGProp::SurfaceProperties(aShape,G);
353 double anArea = G.Mass();
355 int nbFaces = Precision::IsInfinite( tmpLength ) ? 0 :
356 (int)( anArea/(tmpLength*tmpLength*sqrt(3.)/4) );
357 int nbNodes = (int) ( nbFaces*3 - (NbSeg-1)*2 ) / 6;
359 std::vector<int> aVec(SMDSEntity_Last);
360 for(int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aVec[i] = 0;
362 aVec[SMDSEntity_Quad_Triangle] = nbFaces;
363 aVec[SMDSEntity_Node] = (int)( nbNodes + nbFaces*3 - (NbSeg-1) );
366 aVec[SMDSEntity_Node] = nbNodes;
367 aVec[SMDSEntity_Triangle] = nbFaces;
369 SMESH_subMesh * sm = aMesh.GetSubMesh(aShape);
370 aResMap.insert(std::make_pair(sm,aVec));
376 //=======================================================================
377 //function : fixOverlappedLinkUV
378 //purpose : prevent failure due to overlapped adjacent links
379 //=======================================================================
381 static bool fixOverlappedLinkUV( R2& uv0, const R2& uv1, const R2& uv2 )
383 gp_XY v1( uv0.x - uv1.x, uv0.y - uv1.y );
384 gp_XY v2( uv2.x - uv1.x, uv2.y - uv1.y );
386 double tol2 = DBL_MIN * DBL_MIN;
387 double sqMod1 = v1.SquareModulus();
388 if ( sqMod1 <= tol2 ) return false;
389 double sqMod2 = v2.SquareModulus();
390 if ( sqMod2 <= tol2 ) return false;
394 // check sinus >= 1.e-3
395 const double minSin = 1.e-3;
396 if ( dot > 0 && 1 - dot * dot / ( sqMod1 * sqMod2 ) < minSin * minSin ) {
397 MESSAGE(" ___ FIX UV ____" << uv0.x << " " << uv0.y);
398 v1.SetCoord( -v1.Y(), v1.X() );
399 double delta = sqrt( sqMod1 ) * minSin;
409 // MESSAGE(" -> " << uv0.x << " " << uv0.y << " ");
410 // MESSAGE("v1( " << v1.X() << " " << v1.Y() << " ) " <<
411 // "v2( " << v2.X() << " " << v2.Y() << " ) ");
412 // MESSAGE("SIN: " << sqrt(1 - dot * dot / (sqMod1 * sqMod2)));
413 // v1.SetCoord( uv0.x - uv1.x, uv0.y - uv1.y );
414 // v2.SetCoord( uv2.x - uv1.x, uv2.y - uv1.y );
415 // gp_XY v3( uv2.x - uv0.x, uv2.y - uv0.y );
416 // sqMod1 = v1.SquareModulus();
417 // sqMod2 = v2.SquareModulus();
419 // double sin = sqrt(1 - dot * dot / (sqMod1 * sqMod2));
420 // MESSAGE("NEW SIN: " << sin);
427 //=======================================================================
428 //function : fixCommonVertexUV
430 //=======================================================================
432 static bool fixCommonVertexUV (R2 & theUV,
433 const TopoDS_Vertex& theV,
434 const TopoDS_Face& theF,
435 const TopTools_IndexedDataMapOfShapeListOfShape & theVWMap,
436 SMESH_Mesh & theMesh,
437 const double theScaleX,
438 const double theScaleY,
439 const bool theCreateQuadratic)
441 if( !theVWMap.Contains( theV )) return false;
443 // check if there is another wire sharing theV
444 const TopTools_ListOfShape& WList = theVWMap.FindFromKey( theV );
445 TopTools_ListIteratorOfListOfShape aWIt;
446 TopTools_MapOfShape aWires;
447 for ( aWIt.Initialize( WList ); aWIt.More(); aWIt.Next() )
448 aWires.Add( aWIt.Value() );
449 if ( aWires.Extent() < 2 ) return false;
451 TopoDS_Shape anOuterWire = BRepTools::OuterWire(theF);
452 TopoDS_Shape anInnerWire;
453 for ( aWIt.Initialize( WList ); aWIt.More() && anInnerWire.IsNull(); aWIt.Next() )
454 if ( !anOuterWire.IsSame( aWIt.Value() ))
455 anInnerWire = aWIt.Value();
457 TopTools_ListOfShape EList;
458 list< double > UList;
460 // find edges of theW sharing theV
461 // and find 2d normal to them at theV
463 TopoDS_Iterator itE( anInnerWire );
464 for ( ; itE.More(); itE.Next() )
466 const TopoDS_Edge& E = TopoDS::Edge( itE.Value() );
467 TopoDS_Iterator itV( E );
468 for ( ; itV.More(); itV.Next() )
470 const TopoDS_Vertex & V = TopoDS::Vertex( itV.Value() );
471 if ( !V.IsSame( theV ))
474 Standard_Real u = BRep_Tool::Parameter( V, E );
475 UList.push_back( u );
477 Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface(E, theF, f, l);
481 gp_Vec2d n( d1.Y() * theScaleX, -d1.X() * theScaleY);
482 if ( E.Orientation() == TopAbs_REVERSED )
488 // define step size by which to move theUV
490 gp_Pnt2d nextUV; // uv of next node on edge, most distant of the four
491 gp_Pnt2d thisUV( theUV.x, theUV.y );
492 double maxDist = -DBL_MAX;
493 TopTools_ListIteratorOfListOfShape aEIt (EList);
494 list< double >::iterator aUIt = UList.begin();
495 for ( ; aEIt.More(); aEIt.Next(), aUIt++ )
497 const TopoDS_Edge& E = TopoDS::Edge( aEIt.Value() );
499 Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface(E, theF, f, l);
501 double umin = DBL_MAX, umax = -DBL_MAX;
502 SMDS_NodeIteratorPtr nIt = theMesh.GetSubMesh(E)->GetSubMeshDS()->GetNodes();
503 if ( !nIt->more() ) // no nodes on edge, only on vertices
509 while ( nIt->more() ) {
510 const SMDS_MeshNode* node = nIt->next();
511 // check if node is medium
512 if ( theCreateQuadratic && SMESH_MesherHelper::IsMedium( node, SMDSAbs_Edge ))
514 const SMDS_EdgePosition* epos =
515 static_cast<const SMDS_EdgePosition*>(node->GetPosition().get());
516 double u = epos->GetUParameter();
523 bool isFirstCommon = ( *aUIt == f );
524 gp_Pnt2d uv = C2d->Value( isFirstCommon ? umin : umax );
525 double dist = thisUV.SquareDistance( uv );
526 if ( dist > maxDist ) {
532 uv0.x = thisUV.X(); uv0.y = thisUV.Y();
533 uv1.x = nextUV.X(); uv1.y = nextUV.Y();
534 uv2.x = thisUV.X(); uv2.y = thisUV.Y();
536 uv1.x *= theScaleX; uv1.y *= theScaleY;
538 if ( fixOverlappedLinkUV( uv0, uv1, uv2 ))
540 double step = thisUV.Distance( gp_Pnt2d( uv0.x, uv0.y ));
542 // move theUV along the normal by the step
546 MESSAGE("--fixCommonVertexUV move(" << theUV.x << " " << theUV.x
547 << ") by (" << N.X() << " " << N.Y() << ")"
548 << endl << "--- MAX DIST " << maxDist);
558 //=============================================================================
562 //=============================================================================
564 bool StdMeshers_MEFISTO_2D::LoadPoints(TWireVector & wires,
566 vector<const SMDS_MeshNode*>& mefistoToDS,
570 // to avoid passing same uv points for a vertex common to 2 wires
572 TopTools_IndexedDataMapOfShapeListOfShape VWMap;
573 if ( wires.size() > 1 )
575 F = TopoDS::Face( myTool->GetSubShape() );
576 TopExp::MapShapesAndAncestors( F, TopAbs_VERTEX, TopAbs_WIRE, VWMap );
578 for ( int iW = 0; iW < wires.size(); ++iW )
579 nbVertices += wires[ iW ]->NbEdges();
580 if ( nbVertices == VWMap.Extent() )
581 VWMap.Clear(); // wires have no common vertices
586 for ( int iW = 0; iW < wires.size(); ++iW )
588 const vector<UVPtStruct>& uvPtVec = wires[ iW ]->GetUVPtStruct();
589 if ( uvPtVec.size() != wires[ iW ]->NbPoints() ) {
590 return error(COMPERR_BAD_INPUT_MESH,SMESH_Comment("Unexpected nb of points on wire ")
591 << iW << ": " << uvPtVec.size()<<" != "<<wires[ iW ]->NbPoints()
592 << ", probably because of invalid node parameters on geom edges");
594 if ( m + uvPtVec.size()-1 > mefistoToDS.size() ) {
595 MESSAGE("Wrong mefistoToDS.size: "<<mefistoToDS.size()<<" < "<<m + uvPtVec.size()-1);
596 return error("Internal error");
599 list< int > mOnVertex;
600 vector<UVPtStruct>::const_iterator uvPt = uvPtVec.begin();
601 for ( ++uvPt; uvPt != uvPtVec.end(); ++uvPt )
603 // bind mefisto ID to node
604 mefistoToDS[m] = uvPt->node;
606 uvslf[m].x = uvPt->u * scalex;
607 uvslf[m].y = uvPt->v * scaley;
608 switch ( uvPt->node->GetPosition()->GetTypeOfPosition())
610 case SMDS_TOP_VERTEX:
611 mOnVertex.push_back( m );
614 // In order to detect degenerated faces easily, we replace
615 // nodes on a degenerated edge by node on the vertex of that edge
616 if ( myTool->IsDegenShape( uvPt->node->GetPosition()->GetShapeId() ))
618 int edgeID = uvPt->node->GetPosition()->GetShapeId();
619 SMESH_subMesh* edgeSM = myTool->GetMesh()->GetSubMeshContaining( edgeID );
620 SMESH_subMeshIteratorPtr smIt = edgeSM->getDependsOnIterator( /*includeSelf=*/0,
621 /*complexShapeFirst=*/0);
624 SMESH_subMesh* vertexSM = smIt->next();
625 SMDS_NodeIteratorPtr nIt = vertexSM->GetSubMeshDS()->GetNodes();
627 mefistoToDS[m] = nIt->next();
636 int mFirst = mOnVertex.front(), mLast = m - 1;
637 list< int >::iterator mIt = mOnVertex.begin();
638 for ( ; mIt != mOnVertex.end(); ++mIt)
641 if ( iW && !VWMap.IsEmpty()) { // except outer wire
642 // avoid passing same uv point for a vertex common to 2 wires
643 int vID = mefistoToDS[m]->GetPosition()->GetShapeId();
644 TopoDS_Vertex V = TopoDS::Vertex( myTool->GetMeshDS()->IndexToShape( vID ));
645 if ( fixCommonVertexUV( uvslf[m], V, F, VWMap, *myTool->GetMesh(),
646 scalex, scaley, _quadraticMesh )) {
647 myNodesOnCommonV.push_back( mefistoToDS[m] );
651 // prevent failure on overlapped adjacent links,
652 // check only links ending in vertex nodes
653 int mB = m - 1, mA = m + 1; // indices Before and After
654 if ( mB < mFirst ) mB = mLast;
655 if ( mA > mLast ) mA = mFirst;
656 fixOverlappedLinkUV (uvslf[ mB ], uvslf[ m ], uvslf[ mA ]);
659 // cout << "MEFISTO INPUT************" << endl;
660 // for ( int i =0; i < m; ++i )
661 // cout << i << ": \t" << uvslf[i].x << ", " << uvslf[i].y << " Node " << mefistoToDS[i]->GetID()<< endl;
666 //=============================================================================
670 //=============================================================================
672 void StdMeshers_MEFISTO_2D::ComputeScaleOnFace(SMESH_Mesh & aMesh,
673 const TopoDS_Face & aFace,
677 TopoDS_Wire W = BRepTools::OuterWire(aFace);
679 double xmin = 1.e300; // min & max of face 2D parametric coord.
680 double xmax = -1.e300;
681 double ymin = 1.e300;
682 double ymax = -1.e300;
687 TopExp_Explorer wexp(W, TopAbs_EDGE);
688 for ( ; wexp.More(); wexp.Next())
690 const TopoDS_Edge & E = TopoDS::Edge( wexp.Current() );
692 Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface(E, aFace, f, l);
693 if ( C2d.IsNull() ) continue;
694 double du = (l - f) / double (nbp);
695 for (int i = 0; i <= nbp; i++)
697 double param = f + double (i) * du;
698 gp_Pnt2d p = C2d->Value(param);
707 // MESSAGE(" "<< f<<" "<<l<<" "<<param<<" "<<xmin<<" "<<xmax<<" "<<ymin<<" "<<ymax);
714 double xmoy = (xmax + xmin) / 2.;
715 double ymoy = (ymax + ymin) / 2.;
716 double xsize = xmax - xmin;
717 double ysize = ymax - ymin;
720 Handle(Geom_Surface) S = BRep_Tool::Surface(aFace,L); // 3D surface
724 gp_Pnt PX0 = S->Value(xmin, ymoy);
725 gp_Pnt PY0 = S->Value(xmoy, ymin);
726 double dx = xsize / double (nbp);
727 double dy = ysize / double (nbp);
728 for (int i = 1; i <= nbp; i++)
730 double x = xmin + double (i) * dx;
731 gp_Pnt PX = S->Value(x, ymoy);
732 double y = ymin + double (i) * dy;
733 gp_Pnt PY = S->Value(xmoy, y);
734 length_x += PX.Distance(PX0);
735 length_y += PY.Distance(PY0);
739 scalex = length_x / xsize;
740 scaley = length_y / ysize;
743 double xyratio = xsize*scalex/(ysize*scaley);
744 const double maxratio = 1.e2;
746 if (xyratio > maxratio) {
748 scaley *= xyratio / maxratio;
751 else if (xyratio < 1./maxratio) {
753 scalex *= 1 / xyratio / maxratio;
760 //=============================================================================
764 //=============================================================================
766 void StdMeshers_MEFISTO_2D::StoreResult(Z nbst, R2 * uvst, Z nbt, Z * nust,
767 vector< const SMDS_MeshNode*>&mefistoToDS,
768 double scalex, double scaley)
770 SMESHDS_Mesh * meshDS = myTool->GetMeshDS();
771 int faceID = myTool->GetSubShapeID();
773 TopoDS_Face F = TopoDS::Face( myTool->GetSubShape() );
774 Handle(Geom_Surface) S = BRep_Tool::Surface( F );
776 Z n = mefistoToDS.size(); // nb input points
777 mefistoToDS.resize( nbst );
778 for ( ; n < nbst; n++)
782 double u = uvst[n][0] / scalex;
783 double v = uvst[n][1] / scaley;
784 gp_Pnt P = S->Value(u, v);
786 SMDS_MeshNode * node = meshDS->AddNode(P.X(), P.Y(), P.Z());
787 meshDS->SetNodeOnFace(node, faceID, u, v);
789 //MESSAGE(P.X()<<" "<<P.Y()<<" "<<P.Z());
790 mefistoToDS[n] = node;
791 //MESSAGE("NEW: "<<n<<" "<<mefistoToDS[n+1]);
797 // triangle points must be in trigonometric order if face is Forward
798 // else they must be put clockwise
800 bool triangleIsWellOriented = ( F.Orientation() == TopAbs_FORWARD );
802 for (n = 1; n <= nbt; n++)
804 const SMDS_MeshNode * n1 = mefistoToDS[ nust[m++] - 1 ];
805 const SMDS_MeshNode * n2 = mefistoToDS[ nust[m++] - 1 ];
806 const SMDS_MeshNode * n3 = mefistoToDS[ nust[m++] - 1 ];
808 // avoid creating degenetrated faces
809 bool isDegen = ( myTool->HasDegeneratedEdges() && ( n1 == n2 || n1 == n3 || n2 == n3 ));
812 SMDS_MeshElement * elt;
813 if (triangleIsWellOriented)
814 elt = myTool->AddFace(n1, n2, n3);
816 elt = myTool->AddFace(n1, n3, n2);
817 meshDS->SetMeshElementOnShape(elt, faceID);
822 // remove bad elements built on vertices shared by wires
824 list<const SMDS_MeshNode*>::iterator itN = myNodesOnCommonV.begin();
825 for ( ; itN != myNodesOnCommonV.end(); itN++ )
827 const SMDS_MeshNode* node = *itN;
828 SMDS_ElemIteratorPtr invElemIt = node->GetInverseElementIterator();
829 while ( invElemIt->more() )
831 const SMDS_MeshElement* elem = invElemIt->next();
832 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
834 while ( itN->more() )
835 if ( itN->next() == node)
838 MESSAGE( "RM bad element " << elem->GetID());
839 meshDS->RemoveElement( elem );