1 // Copyright (C) 2007-2011 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
240 if ( _hypMaxElementArea )
245 nudslf = new Z[1 + nblf];
250 // count nb of input points
251 for ( int iW = 0; iW < nbWires; ++iW )
253 nbpnt += wires[iW]->NbPoints() - 1;
254 nudslf[iw++] = nbpnt;
257 uvslf = new R2[nudslf[nblf]];
259 double scalex, scaley;
260 ComputeScaleOnFace(aMesh, F, scalex, scaley);
262 // correspondence mefisto index --> Nodes
263 vector< const SMDS_MeshNode*> mefistoToDS(nbpnt, (const SMDS_MeshNode*)0);
267 // fill input points UV
268 if ( LoadPoints(wires, uvslf, mefistoToDS, scalex, scaley) )
271 aptrte(nutysu, aretmx,
272 nblf, nudslf, uvslf, nbpti, uvpti, nbst, uvst, nbt, nust, ierr);
276 MESSAGE("... End Triangulation Generated Triangle Number " << nbt);
277 MESSAGE(" Node Number " << nbst);
278 StoreResult(nbst, uvst, nbt, nust, mefistoToDS, scalex, scaley);
283 error(ierr,"Error in Triangulation (aptrte())");
286 if (nudslf != NULL) delete[]nudslf;
287 if (uvslf != NULL) delete[]uvslf;
288 if (uvst != NULL) delete[]uvst;
289 if (nust != NULL) delete[]nust;
295 //=============================================================================
299 //=============================================================================
301 bool StdMeshers_MEFISTO_2D::Evaluate(SMESH_Mesh & aMesh,
302 const TopoDS_Shape & aShape,
303 MapShapeNbElems& aResMap)
305 MESSAGE("StdMeshers_MEFISTO_2D::Evaluate");
307 TopoDS_Face F = TopoDS::Face(aShape.Oriented(TopAbs_FORWARD));
311 bool IsQuadratic = false;
313 TopExp_Explorer exp(F,TopAbs_EDGE);
314 for(; exp.More(); exp.Next()) {
315 TopoDS_Edge E = TopoDS::Edge(exp.Current());
316 MapShapeNbElemsItr anIt = aResMap.find( aMesh.GetSubMesh(E) );
317 if( anIt == aResMap.end() ) continue;
318 std::vector<int> aVec = (*anIt).second;
319 int nbe = Max(aVec[SMDSEntity_Edge],aVec[SMDSEntity_Quad_Edge]);
322 IsQuadratic = ( aVec[SMDSEntity_Quad_Edge] > aVec[SMDSEntity_Edge] );
327 Handle(Geom_Curve) C = BRep_Tool::Curve(E,L,a,b);
330 double dp = (b-a)/nbe;
331 for(int i=1; i<=nbe; i++) {
334 aLen += P1.Distance(P2);
339 std::vector<int> aResVec(SMDSEntity_Last);
340 for(int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aResVec[i] = 0;
341 SMESH_subMesh * sm = aMesh.GetSubMesh(aShape);
342 aResMap.insert(std::make_pair(sm,aResVec));
343 SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
344 smError.reset( new SMESH_ComputeError(COMPERR_ALGO_FAILED,
345 "Submesh can not be evaluated",this));
348 aLen = aLen/NbSeg; // middle length
350 _edgeLength = Precision::Infinite();
351 double tmpLength = Min( _edgeLength, aLen );
354 BRepGProp::SurfaceProperties(aShape,G);
355 double anArea = G.Mass();
357 int nbFaces = Precision::IsInfinite( tmpLength ) ? 0 :
358 (int)( anArea/(tmpLength*tmpLength*sqrt(3.)/4) );
359 int nbNodes = (int) ( nbFaces*3 - (NbSeg-1)*2 ) / 6;
361 std::vector<int> aVec(SMDSEntity_Last);
362 for(int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aVec[i] = 0;
364 aVec[SMDSEntity_Quad_Triangle] = nbFaces;
365 aVec[SMDSEntity_Node] = (int)( nbNodes + nbFaces*3 - (NbSeg-1) );
368 aVec[SMDSEntity_Node] = nbNodes;
369 aVec[SMDSEntity_Triangle] = nbFaces;
371 SMESH_subMesh * sm = aMesh.GetSubMesh(aShape);
372 aResMap.insert(std::make_pair(sm,aVec));
378 //=======================================================================
379 //function : fixOverlappedLinkUV
380 //purpose : prevent failure due to overlapped adjacent links
381 //=======================================================================
383 static bool fixOverlappedLinkUV( R2& uv0, const R2& uv1, const R2& uv2 )
385 gp_XY v1( uv0.x - uv1.x, uv0.y - uv1.y );
386 gp_XY v2( uv2.x - uv1.x, uv2.y - uv1.y );
388 double tol2 = DBL_MIN * DBL_MIN;
389 double sqMod1 = v1.SquareModulus();
390 if ( sqMod1 <= tol2 ) return false;
391 double sqMod2 = v2.SquareModulus();
392 if ( sqMod2 <= tol2 ) return false;
396 // check sinus >= 1.e-3
397 const double minSin = 1.e-3;
398 if ( dot > 0 && 1 - dot * dot / ( sqMod1 * sqMod2 ) < minSin * minSin ) {
399 MESSAGE(" ___ FIX UV ____" << uv0.x << " " << uv0.y);
400 v1.SetCoord( -v1.Y(), v1.X() );
401 double delta = sqrt( sqMod1 ) * minSin;
411 // MESSAGE(" -> " << uv0.x << " " << uv0.y << " ");
412 // MESSAGE("v1( " << v1.X() << " " << v1.Y() << " ) " <<
413 // "v2( " << v2.X() << " " << v2.Y() << " ) ");
414 // MESSAGE("SIN: " << sqrt(1 - dot * dot / (sqMod1 * sqMod2)));
415 // v1.SetCoord( uv0.x - uv1.x, uv0.y - uv1.y );
416 // v2.SetCoord( uv2.x - uv1.x, uv2.y - uv1.y );
417 // gp_XY v3( uv2.x - uv0.x, uv2.y - uv0.y );
418 // sqMod1 = v1.SquareModulus();
419 // sqMod2 = v2.SquareModulus();
421 // double sin = sqrt(1 - dot * dot / (sqMod1 * sqMod2));
422 // MESSAGE("NEW SIN: " << sin);
429 //=======================================================================
430 //function : fixCommonVertexUV
432 //=======================================================================
434 static bool fixCommonVertexUV (R2 & theUV,
435 const TopoDS_Vertex& theV,
436 const TopoDS_Face& theF,
437 const TopTools_IndexedDataMapOfShapeListOfShape & theVWMap,
438 SMESH_Mesh & theMesh,
439 const double theScaleX,
440 const double theScaleY,
441 const bool theCreateQuadratic)
443 if( !theVWMap.Contains( theV )) return false;
445 // check if there is another wire sharing theV
446 const TopTools_ListOfShape& WList = theVWMap.FindFromKey( theV );
447 TopTools_ListIteratorOfListOfShape aWIt;
448 TopTools_MapOfShape aWires;
449 for ( aWIt.Initialize( WList ); aWIt.More(); aWIt.Next() )
450 aWires.Add( aWIt.Value() );
451 if ( aWires.Extent() < 2 ) return false;
453 TopoDS_Shape anOuterWire = BRepTools::OuterWire(theF);
454 TopoDS_Shape anInnerWire;
455 for ( aWIt.Initialize( WList ); aWIt.More() && anInnerWire.IsNull(); aWIt.Next() )
456 if ( !anOuterWire.IsSame( aWIt.Value() ))
457 anInnerWire = aWIt.Value();
459 TopTools_ListOfShape EList;
460 list< double > UList;
462 // find edges of theW sharing theV
463 // and find 2d normal to them at theV
465 TopoDS_Iterator itE( anInnerWire );
466 for ( ; itE.More(); itE.Next() )
468 const TopoDS_Edge& E = TopoDS::Edge( itE.Value() );
469 TopoDS_Iterator itV( E );
470 for ( ; itV.More(); itV.Next() )
472 const TopoDS_Vertex & V = TopoDS::Vertex( itV.Value() );
473 if ( !V.IsSame( theV ))
476 Standard_Real u = BRep_Tool::Parameter( V, E );
477 UList.push_back( u );
479 Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface(E, theF, f, l);
483 gp_Vec2d n( d1.Y() * theScaleX, -d1.X() * theScaleY);
484 if ( E.Orientation() == TopAbs_REVERSED )
490 // define step size by which to move theUV
492 gp_Pnt2d nextUV; // uv of next node on edge, most distant of the four
493 gp_Pnt2d thisUV( theUV.x, theUV.y );
494 double maxDist = -DBL_MAX;
495 TopTools_ListIteratorOfListOfShape aEIt (EList);
496 list< double >::iterator aUIt = UList.begin();
497 for ( ; aEIt.More(); aEIt.Next(), aUIt++ )
499 const TopoDS_Edge& E = TopoDS::Edge( aEIt.Value() );
501 Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface(E, theF, f, l);
503 double umin = DBL_MAX, umax = -DBL_MAX;
504 SMDS_NodeIteratorPtr nIt = theMesh.GetSubMesh(E)->GetSubMeshDS()->GetNodes();
505 if ( !nIt->more() ) // no nodes on edge, only on vertices
511 while ( nIt->more() ) {
512 const SMDS_MeshNode* node = nIt->next();
513 // check if node is medium
514 if ( theCreateQuadratic && SMESH_MesherHelper::IsMedium( node, SMDSAbs_Edge ))
516 const SMDS_EdgePosition* epos =
517 static_cast<const SMDS_EdgePosition*>(node->GetPosition());
518 double u = epos->GetUParameter();
525 bool isFirstCommon = ( *aUIt == f );
526 gp_Pnt2d uv = C2d->Value( isFirstCommon ? umin : umax );
527 double dist = thisUV.SquareDistance( uv );
528 if ( dist > maxDist ) {
534 uv0.x = thisUV.X(); uv0.y = thisUV.Y();
535 uv1.x = nextUV.X(); uv1.y = nextUV.Y();
536 uv2.x = thisUV.X(); uv2.y = thisUV.Y();
538 uv1.x *= theScaleX; uv1.y *= theScaleY;
540 if ( fixOverlappedLinkUV( uv0, uv1, uv2 ))
542 double step = thisUV.Distance( gp_Pnt2d( uv0.x, uv0.y ));
544 // move theUV along the normal by the step
548 MESSAGE("--fixCommonVertexUV move(" << theUV.x << " " << theUV.x
549 << ") by (" << N.X() << " " << N.Y() << ")"
550 << endl << "--- MAX DIST " << maxDist);
560 //=============================================================================
564 //=============================================================================
566 bool StdMeshers_MEFISTO_2D::LoadPoints(TWireVector & wires,
568 vector<const SMDS_MeshNode*>& mefistoToDS,
572 // to avoid passing same uv points for a vertex common to 2 wires
574 TopTools_IndexedDataMapOfShapeListOfShape VWMap;
575 if ( wires.size() > 1 )
577 F = TopoDS::Face( myTool->GetSubShape() );
578 TopExp::MapShapesAndAncestors( F, TopAbs_VERTEX, TopAbs_WIRE, VWMap );
580 for ( int iW = 0; iW < wires.size(); ++iW )
581 nbVertices += wires[ iW ]->NbEdges();
582 if ( nbVertices == VWMap.Extent() )
583 VWMap.Clear(); // wires have no common vertices
588 for ( int iW = 0; iW < wires.size(); ++iW )
590 const vector<UVPtStruct>& uvPtVec = wires[ iW ]->GetUVPtStruct();
591 if ( uvPtVec.size() != wires[ iW ]->NbPoints() ) {
592 return error(COMPERR_BAD_INPUT_MESH,SMESH_Comment("Unexpected nb of points on wire ")
593 << iW << ": " << uvPtVec.size()<<" != "<<wires[ iW ]->NbPoints()
594 << ", probably because of invalid node parameters on geom edges");
596 if ( m + uvPtVec.size()-1 > mefistoToDS.size() ) {
597 MESSAGE("Wrong mefistoToDS.size: "<<mefistoToDS.size()<<" < "<<m + uvPtVec.size()-1);
598 return error("Internal error");
601 list< int > mOnVertex;
602 vector<UVPtStruct>::const_iterator uvPt = uvPtVec.begin();
603 for ( ++uvPt; uvPt != uvPtVec.end(); ++uvPt )
605 // bind mefisto ID to node
606 mefistoToDS[m] = uvPt->node;
608 uvslf[m].x = uvPt->u * scalex;
609 uvslf[m].y = uvPt->v * scaley;
610 switch ( uvPt->node->GetPosition()->GetTypeOfPosition())
612 case SMDS_TOP_VERTEX:
613 mOnVertex.push_back( m );
616 // In order to detect degenerated faces easily, we replace
617 // nodes on a degenerated edge by node on the vertex of that edge
618 if ( myTool->IsDegenShape( uvPt->node->getshapeId() ))
620 int edgeID = uvPt->node->getshapeId();
621 SMESH_subMesh* edgeSM = myTool->GetMesh()->GetSubMeshContaining( edgeID );
622 SMESH_subMeshIteratorPtr smIt = edgeSM->getDependsOnIterator( /*includeSelf=*/0,
623 /*complexShapeFirst=*/0);
626 SMESH_subMesh* vertexSM = smIt->next();
627 SMDS_NodeIteratorPtr nIt = vertexSM->GetSubMeshDS()->GetNodes();
629 mefistoToDS[m] = nIt->next();
638 int mFirst = mOnVertex.front(), mLast = m - 1;
639 list< int >::iterator mIt = mOnVertex.begin();
640 for ( ; mIt != mOnVertex.end(); ++mIt)
643 if ( iW && !VWMap.IsEmpty()) { // except outer wire
644 // avoid passing same uv point for a vertex common to 2 wires
645 int vID = mefistoToDS[m]->getshapeId();
646 TopoDS_Vertex V = TopoDS::Vertex( myTool->GetMeshDS()->IndexToShape( vID ));
647 if ( fixCommonVertexUV( uvslf[m], V, F, VWMap, *myTool->GetMesh(),
648 scalex, scaley, _quadraticMesh )) {
649 myNodesOnCommonV.push_back( mefistoToDS[m] );
653 // prevent failure on overlapped adjacent links,
654 // check only links ending in vertex nodes
655 int mB = m - 1, mA = m + 1; // indices Before and After
656 if ( mB < mFirst ) mB = mLast;
657 if ( mA > mLast ) mA = mFirst;
658 fixOverlappedLinkUV (uvslf[ mB ], uvslf[ m ], uvslf[ mA ]);
661 // cout << "MEFISTO INPUT************" << endl;
662 // for ( int i =0; i < m; ++i )
663 // cout << i << ": \t" << uvslf[i].x << ", " << uvslf[i].y << " Node " << mefistoToDS[i]->GetID()<< endl;
668 //=============================================================================
672 //=============================================================================
674 void StdMeshers_MEFISTO_2D::ComputeScaleOnFace(SMESH_Mesh & aMesh,
675 const TopoDS_Face & aFace,
679 TopoDS_Wire W = BRepTools::OuterWire(aFace);
681 double xmin = 1.e300; // min & max of face 2D parametric coord.
682 double xmax = -1.e300;
683 double ymin = 1.e300;
684 double ymax = -1.e300;
689 TopExp_Explorer wexp(W, TopAbs_EDGE);
690 for ( ; wexp.More(); wexp.Next())
692 const TopoDS_Edge & E = TopoDS::Edge( wexp.Current() );
694 Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface(E, aFace, f, l);
695 if ( C2d.IsNull() ) continue;
696 double du = (l - f) / double (nbp);
697 for (int i = 0; i <= nbp; i++)
699 double param = f + double (i) * du;
700 gp_Pnt2d p = C2d->Value(param);
709 // MESSAGE(" "<< f<<" "<<l<<" "<<param<<" "<<xmin<<" "<<xmax<<" "<<ymin<<" "<<ymax);
716 double xmoy = (xmax + xmin) / 2.;
717 double ymoy = (ymax + ymin) / 2.;
718 double xsize = xmax - xmin;
719 double ysize = ymax - ymin;
722 Handle(Geom_Surface) S = BRep_Tool::Surface(aFace,L); // 3D surface
726 gp_Pnt PX0 = S->Value(xmin, ymoy);
727 gp_Pnt PY0 = S->Value(xmoy, ymin);
728 double dx = xsize / double (nbp);
729 double dy = ysize / double (nbp);
730 for (int i = 1; i <= nbp; i++)
732 double x = xmin + double (i) * dx;
733 gp_Pnt PX = S->Value(x, ymoy);
734 double y = ymin + double (i) * dy;
735 gp_Pnt PY = S->Value(xmoy, y);
736 length_x += PX.Distance(PX0);
737 length_y += PY.Distance(PY0);
741 scalex = length_x / xsize;
742 scaley = length_y / ysize;
745 double xyratio = xsize*scalex/(ysize*scaley);
746 const double maxratio = 1.e2;
748 if (xyratio > maxratio) {
750 scaley *= xyratio / maxratio;
753 else if (xyratio < 1./maxratio) {
755 scalex *= 1 / xyratio / maxratio;
762 //=============================================================================
766 //=============================================================================
768 void StdMeshers_MEFISTO_2D::StoreResult(Z nbst, R2 * uvst, Z nbt, Z * nust,
769 vector< const SMDS_MeshNode*>&mefistoToDS,
770 double scalex, double scaley)
772 SMESHDS_Mesh * meshDS = myTool->GetMeshDS();
773 int faceID = myTool->GetSubShapeID();
775 TopoDS_Face F = TopoDS::Face( myTool->GetSubShape() );
776 Handle(Geom_Surface) S = BRep_Tool::Surface( F );
778 Z n = mefistoToDS.size(); // nb input points
779 mefistoToDS.resize( nbst );
780 for ( ; n < nbst; n++)
784 double u = uvst[n][0] / scalex;
785 double v = uvst[n][1] / scaley;
786 gp_Pnt P = S->Value(u, v);
788 SMDS_MeshNode * node = meshDS->AddNode(P.X(), P.Y(), P.Z());
789 meshDS->SetNodeOnFace(node, faceID, u, v);
791 //MESSAGE(P.X()<<" "<<P.Y()<<" "<<P.Z());
792 mefistoToDS[n] = node;
793 //MESSAGE("NEW: "<<n<<" "<<mefistoToDS[n+1]);
799 // triangle points must be in trigonometric order if face is Forward
800 // else they must be put clockwise
802 bool triangleIsWellOriented = ( F.Orientation() == TopAbs_FORWARD );
804 for (n = 1; n <= nbt; n++)
806 const SMDS_MeshNode * n1 = mefistoToDS[ nust[m++] - 1 ];
807 const SMDS_MeshNode * n2 = mefistoToDS[ nust[m++] - 1 ];
808 const SMDS_MeshNode * n3 = mefistoToDS[ nust[m++] - 1 ];
810 // avoid creating degenetrated faces
811 bool isDegen = ( myTool->HasDegeneratedEdges() && ( n1 == n2 || n1 == n3 || n2 == n3 ));
814 SMDS_MeshElement * elt;
815 if (triangleIsWellOriented)
816 elt = myTool->AddFace(n1, n2, n3);
818 elt = myTool->AddFace(n1, n3, n2);
819 meshDS->SetMeshElementOnShape(elt, faceID);
824 // remove bad elements built on vertices shared by wires
826 list<const SMDS_MeshNode*>::iterator itN = myNodesOnCommonV.begin();
827 for ( ; itN != myNodesOnCommonV.end(); itN++ )
829 const SMDS_MeshNode* node = *itN;
830 SMDS_ElemIteratorPtr invElemIt = node->GetInverseElementIterator();
831 while ( invElemIt->more() )
833 const SMDS_MeshElement* elem = invElemIt->next();
834 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
836 while ( itN->more() )
837 if ( itN->next() == node)
840 MESSAGE( "RM bad element " << elem->GetID());
841 meshDS->RemoveElement( elem );