1 // Copyright (C) 2007-2012 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 <Geom2d_Curve.hxx>
55 #include <Geom_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 <TopoDS_Wire.hxx>
68 #include <gp_Pnt2d.hxx>
70 #include <BRep_Tool.hxx>
71 #include <GProp_GProps.hxx>
72 #include <BRepGProp.hxx>
76 //=============================================================================
80 //=============================================================================
82 StdMeshers_MEFISTO_2D::StdMeshers_MEFISTO_2D(int hypId, int studyId, SMESH_Gen * gen):
83 SMESH_2D_Algo(hypId, studyId, gen)
85 MESSAGE("StdMeshers_MEFISTO_2D::StdMeshers_MEFISTO_2D");
87 _shapeType = (1 << TopAbs_FACE);
88 _compatibleHypothesis.push_back("MaxElementArea");
89 _compatibleHypothesis.push_back("LengthFromEdges");
93 _hypMaxElementArea = NULL;
94 _hypLengthFromEdges = NULL;
98 //=============================================================================
102 //=============================================================================
104 StdMeshers_MEFISTO_2D::~StdMeshers_MEFISTO_2D()
106 MESSAGE("StdMeshers_MEFISTO_2D::~StdMeshers_MEFISTO_2D");
109 //=============================================================================
113 //=============================================================================
115 bool StdMeshers_MEFISTO_2D::CheckHypothesis
117 const TopoDS_Shape& aShape,
118 SMESH_Hypothesis::Hypothesis_Status& aStatus)
120 _hypMaxElementArea = NULL;
121 _hypLengthFromEdges = NULL;
125 list <const SMESHDS_Hypothesis * >::const_iterator itl;
126 const SMESHDS_Hypothesis *theHyp;
128 const list <const SMESHDS_Hypothesis * >&hyps = GetUsedHypothesis(aMesh, aShape);
129 int nbHyp = hyps.size();
132 aStatus = SMESH_Hypothesis::HYP_OK; //SMESH_Hypothesis::HYP_MISSING;
133 return true; // (PAL13464) can work with no hypothesis, LengthFromEdges is default one
137 theHyp = (*itl); // use only the first hypothesis
139 string hypName = theHyp->GetName();
143 if (hypName == "MaxElementArea")
145 _hypMaxElementArea = static_cast<const StdMeshers_MaxElementArea *>(theHyp);
146 ASSERT(_hypMaxElementArea);
147 _maxElementArea = _hypMaxElementArea->GetMaxArea();
149 aStatus = SMESH_Hypothesis::HYP_OK;
152 else if (hypName == "LengthFromEdges")
154 _hypLengthFromEdges = static_cast<const StdMeshers_LengthFromEdges *>(theHyp);
155 ASSERT(_hypLengthFromEdges);
157 aStatus = SMESH_Hypothesis::HYP_OK;
160 aStatus = SMESH_Hypothesis::HYP_INCOMPATIBLE;
165 if (_maxElementArea > 0)
167 //_edgeLength = 2 * sqrt(_maxElementArea); // triangles : minorant
168 _edgeLength = sqrt(2. * _maxElementArea/sqrt(3.0));
172 isOk = (_hypLengthFromEdges != NULL); // **** check mode
174 aStatus = SMESH_Hypothesis::HYP_BAD_PARAMETER;
180 //=============================================================================
184 //=============================================================================
186 bool StdMeshers_MEFISTO_2D::Compute(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
188 MESSAGE("StdMeshers_MEFISTO_2D::Compute");
190 TopoDS_Face F = TopoDS::Face(aShape.Oriented(TopAbs_FORWARD));
192 // helper builds quadratic mesh if necessary
193 SMESH_MesherHelper helper(aMesh);
195 _quadraticMesh = myTool->IsQuadraticSubMesh(aShape);
196 const bool ignoreMediumNodes = _quadraticMesh;
198 // get all edges of a face
200 TWireVector wires = StdMeshers_FaceSide::GetFaceWires( F, aMesh, ignoreMediumNodes, problem );
201 int nbWires = wires.size();
202 if ( problem && !problem->IsOK() ) return error( problem );
203 if ( nbWires == 0 ) return error( "Problem in StdMeshers_FaceSide::GetFaceWires()");
204 if ( wires[0]->NbSegments() < 3 ) // ex: a circle with 2 segments
205 return error(COMPERR_BAD_INPUT_MESH,
206 SMESH_Comment("Too few segments: ")<<wires[0]->NbSegments());
208 // compute average edge length
209 if (!_hypMaxElementArea)
213 for ( int iW = 0; iW < nbWires; ++iW )
215 StdMeshers_FaceSidePtr wire = wires[ iW ];
216 _edgeLength += wire->Length();
217 nbSegments += wire->NbSegments();
220 _edgeLength /= nbSegments;
223 if (/*_hypLengthFromEdges &&*/ _edgeLength < DBL_MIN )
226 Z nblf; //nombre de lignes fermees (enveloppe en tete)
227 Z *nudslf = NULL; //numero du dernier sommet de chaque ligne fermee
229 Z nbpti = 0; //nombre points internes futurs sommets de la triangulation
238 Z nutysu = 1; // 1: il existe un fonction areteideale_()
239 // Z nutysu=0; // 0: on utilise aretmx
240 R aretmx = _edgeLength; // longueur max aretes future triangulation
241 if ( _hypMaxElementArea )
246 nudslf = new Z[1 + nblf];
251 // count nb of input points
252 for ( int iW = 0; iW < nbWires; ++iW )
254 nbpnt += wires[iW]->NbPoints() - 1;
255 nudslf[iw++] = nbpnt;
258 uvslf = new R2[nudslf[nblf]];
260 double scalex, scaley;
261 ComputeScaleOnFace(aMesh, F, scalex, scaley);
263 // correspondence mefisto index --> Nodes
264 vector< const SMDS_MeshNode*> mefistoToDS(nbpnt, (const SMDS_MeshNode*)0);
268 // fill input points UV
269 if ( LoadPoints(wires, uvslf, mefistoToDS, scalex, scaley) )
272 aptrte(nutysu, aretmx,
273 nblf, nudslf, uvslf, nbpti, uvpti, nbst, uvst, nbt, nust, ierr);
277 MESSAGE("... End Triangulation Generated Triangle Number " << nbt);
278 MESSAGE(" Node Number " << nbst);
279 StoreResult(nbst, uvst, nbt, nust, mefistoToDS, scalex, scaley);
284 error(ierr,"Error in Triangulation (aptrte())");
287 if (nudslf != NULL) delete[]nudslf;
288 if (uvslf != NULL) delete[]uvslf;
289 if (uvst != NULL) delete[]uvst;
290 if (nust != NULL) delete[]nust;
296 //=============================================================================
300 //=============================================================================
302 bool StdMeshers_MEFISTO_2D::Evaluate(SMESH_Mesh & aMesh,
303 const TopoDS_Shape & aShape,
304 MapShapeNbElems& aResMap)
306 MESSAGE("StdMeshers_MEFISTO_2D::Evaluate");
308 TopoDS_Face F = TopoDS::Face(aShape.Oriented(TopAbs_FORWARD));
312 bool IsQuadratic = false;
314 TopExp_Explorer exp(F,TopAbs_EDGE);
315 for(; exp.More(); exp.Next()) {
316 TopoDS_Edge E = TopoDS::Edge(exp.Current());
317 MapShapeNbElemsItr anIt = aResMap.find( aMesh.GetSubMesh(E) );
318 if( anIt == aResMap.end() ) continue;
319 std::vector<int> aVec = (*anIt).second;
320 int nbe = Max(aVec[SMDSEntity_Edge],aVec[SMDSEntity_Quad_Edge]);
323 IsQuadratic = ( aVec[SMDSEntity_Quad_Edge] > aVec[SMDSEntity_Edge] );
328 Handle(Geom_Curve) C = BRep_Tool::Curve(E,L,a,b);
331 double dp = (b-a)/nbe;
332 for(int i=1; i<=nbe; i++) {
335 aLen += P1.Distance(P2);
340 std::vector<int> aResVec(SMDSEntity_Last);
341 for(int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aResVec[i] = 0;
342 SMESH_subMesh * sm = aMesh.GetSubMesh(aShape);
343 aResMap.insert(std::make_pair(sm,aResVec));
344 SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
345 smError.reset( new SMESH_ComputeError(COMPERR_ALGO_FAILED,
346 "Submesh can not be evaluated",this));
349 aLen = aLen/NbSeg; // middle length
351 _edgeLength = Precision::Infinite();
352 double tmpLength = Min( _edgeLength, aLen );
355 BRepGProp::SurfaceProperties(aShape,G);
356 double anArea = G.Mass();
358 int nbFaces = Precision::IsInfinite( tmpLength ) ? 0 :
359 (int)( anArea/(tmpLength*tmpLength*sqrt(3.)/4) );
360 int nbNodes = (int) ( nbFaces*3 - (NbSeg-1)*2 ) / 6;
362 std::vector<int> aVec(SMDSEntity_Last);
363 for(int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aVec[i] = 0;
365 aVec[SMDSEntity_Quad_Triangle] = nbFaces;
366 aVec[SMDSEntity_Node] = (int)( nbNodes + nbFaces*3 - (NbSeg-1) );
369 aVec[SMDSEntity_Node] = nbNodes;
370 aVec[SMDSEntity_Triangle] = nbFaces;
372 SMESH_subMesh * sm = aMesh.GetSubMesh(aShape);
373 aResMap.insert(std::make_pair(sm,aVec));
379 //=======================================================================
380 //function : fixOverlappedLinkUV
381 //purpose : prevent failure due to overlapped adjacent links
382 //=======================================================================
384 static bool fixOverlappedLinkUV( R2& uv0, const R2& uv1, const R2& uv2 )
386 gp_XY v1( uv0.x - uv1.x, uv0.y - uv1.y );
387 gp_XY v2( uv2.x - uv1.x, uv2.y - uv1.y );
389 double tol2 = DBL_MIN * DBL_MIN;
390 double sqMod1 = v1.SquareModulus();
391 if ( sqMod1 <= tol2 ) return false;
392 double sqMod2 = v2.SquareModulus();
393 if ( sqMod2 <= tol2 ) return false;
397 // check sinus >= 1.e-3
398 const double minSin = 1.e-3;
399 if ( dot > 0 && 1 - dot * dot / ( sqMod1 * sqMod2 ) < minSin * minSin ) {
400 MESSAGE(" ___ FIX UV ____" << uv0.x << " " << uv0.y);
401 v1.SetCoord( -v1.Y(), v1.X() );
402 double delta = sqrt( sqMod1 ) * minSin;
412 // MESSAGE(" -> " << uv0.x << " " << uv0.y << " ");
413 // MESSAGE("v1( " << v1.X() << " " << v1.Y() << " ) " <<
414 // "v2( " << v2.X() << " " << v2.Y() << " ) ");
415 // MESSAGE("SIN: " << sqrt(1 - dot * dot / (sqMod1 * sqMod2)));
416 // v1.SetCoord( uv0.x - uv1.x, uv0.y - uv1.y );
417 // v2.SetCoord( uv2.x - uv1.x, uv2.y - uv1.y );
418 // gp_XY v3( uv2.x - uv0.x, uv2.y - uv0.y );
419 // sqMod1 = v1.SquareModulus();
420 // sqMod2 = v2.SquareModulus();
422 // double sin = sqrt(1 - dot * dot / (sqMod1 * sqMod2));
423 // MESSAGE("NEW SIN: " << sin);
430 //=======================================================================
431 //function : fixCommonVertexUV
433 //=======================================================================
435 static bool fixCommonVertexUV (R2 & theUV,
436 const TopoDS_Vertex& theV,
437 const TopoDS_Face& theF,
438 const TopTools_IndexedDataMapOfShapeListOfShape & theVWMap,
439 SMESH_Mesh & theMesh,
440 const double theScaleX,
441 const double theScaleY,
442 const bool theCreateQuadratic)
444 if( !theVWMap.Contains( theV )) return false;
446 // check if there is another wire sharing theV
447 const TopTools_ListOfShape& WList = theVWMap.FindFromKey( theV );
448 TopTools_ListIteratorOfListOfShape aWIt;
449 TopTools_MapOfShape aWires;
450 for ( aWIt.Initialize( WList ); aWIt.More(); aWIt.Next() )
451 aWires.Add( aWIt.Value() );
452 if ( aWires.Extent() < 2 ) return false;
454 TopoDS_Shape anOuterWire = BRepTools::OuterWire(theF);
455 TopoDS_Shape anInnerWire;
456 for ( aWIt.Initialize( WList ); aWIt.More() && anInnerWire.IsNull(); aWIt.Next() )
457 if ( !anOuterWire.IsSame( aWIt.Value() ))
458 anInnerWire = aWIt.Value();
460 TopTools_ListOfShape EList;
461 list< double > UList;
463 // find edges of theW sharing theV
464 // and find 2d normal to them at theV
466 TopoDS_Iterator itE( anInnerWire );
467 for ( ; itE.More(); itE.Next() )
469 const TopoDS_Edge& E = TopoDS::Edge( itE.Value() );
470 TopoDS_Iterator itV( E );
471 for ( ; itV.More(); itV.Next() )
473 const TopoDS_Vertex & V = TopoDS::Vertex( itV.Value() );
474 if ( !V.IsSame( theV ))
477 Standard_Real u = BRep_Tool::Parameter( V, E );
478 UList.push_back( u );
480 Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface(E, theF, f, l);
484 gp_Vec2d n( d1.Y() * theScaleX, -d1.X() * theScaleY);
485 if ( E.Orientation() == TopAbs_REVERSED )
491 // define step size by which to move theUV
493 gp_Pnt2d nextUV; // uv of next node on edge, most distant of the four
494 gp_Pnt2d thisUV( theUV.x, theUV.y );
495 double maxDist = -DBL_MAX;
496 TopTools_ListIteratorOfListOfShape aEIt (EList);
497 list< double >::iterator aUIt = UList.begin();
498 for ( ; aEIt.More(); aEIt.Next(), aUIt++ )
500 const TopoDS_Edge& E = TopoDS::Edge( aEIt.Value() );
502 Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface(E, theF, f, l);
504 double umin = DBL_MAX, umax = -DBL_MAX;
505 SMDS_NodeIteratorPtr nIt = theMesh.GetSubMesh(E)->GetSubMeshDS()->GetNodes();
506 if ( !nIt->more() ) // no nodes on edge, only on vertices
512 while ( nIt->more() ) {
513 const SMDS_MeshNode* node = nIt->next();
514 // check if node is medium
515 if ( theCreateQuadratic && SMESH_MesherHelper::IsMedium( node, SMDSAbs_Edge ))
517 const SMDS_EdgePosition* epos =
518 static_cast<const SMDS_EdgePosition*>(node->GetPosition());
519 double u = epos->GetUParameter();
526 bool isFirstCommon = ( *aUIt == f );
527 gp_Pnt2d uv = C2d->Value( isFirstCommon ? umin : umax );
528 double dist = thisUV.SquareDistance( uv );
529 if ( dist > maxDist ) {
535 uv0.x = thisUV.X(); uv0.y = thisUV.Y();
536 uv1.x = nextUV.X(); uv1.y = nextUV.Y();
537 uv2.x = thisUV.X(); uv2.y = thisUV.Y();
539 uv1.x *= theScaleX; uv1.y *= theScaleY;
541 if ( fixOverlappedLinkUV( uv0, uv1, uv2 ))
543 double step = thisUV.Distance( gp_Pnt2d( uv0.x, uv0.y ));
545 // move theUV along the normal by the step
549 MESSAGE("--fixCommonVertexUV move(" << theUV.x << " " << theUV.x
550 << ") by (" << N.X() << " " << N.Y() << ")"
551 << endl << "--- MAX DIST " << maxDist);
561 //=============================================================================
565 //=============================================================================
567 bool StdMeshers_MEFISTO_2D::LoadPoints(TWireVector & wires,
569 vector<const SMDS_MeshNode*>& mefistoToDS,
573 // to avoid passing same uv points for a vertex common to 2 wires
575 TopTools_IndexedDataMapOfShapeListOfShape VWMap;
576 if ( wires.size() > 1 )
578 F = TopoDS::Face( myTool->GetSubShape() );
579 TopExp::MapShapesAndAncestors( F, TopAbs_VERTEX, TopAbs_WIRE, VWMap );
581 for ( int iW = 0; iW < wires.size(); ++iW )
582 nbVertices += wires[ iW ]->NbEdges();
583 if ( nbVertices == VWMap.Extent() )
584 VWMap.Clear(); // wires have no common vertices
589 for ( int iW = 0; iW < wires.size(); ++iW )
591 const vector<UVPtStruct>& uvPtVec = wires[ iW ]->GetUVPtStruct();
592 if ( uvPtVec.size() != wires[ iW ]->NbPoints() ) {
593 return error(COMPERR_BAD_INPUT_MESH,SMESH_Comment("Unexpected nb of points on wire ")
594 << iW << ": " << uvPtVec.size()<<" != "<<wires[ iW ]->NbPoints()
595 << ", probably because of invalid node parameters on geom edges");
597 if ( m + uvPtVec.size()-1 > mefistoToDS.size() ) {
598 MESSAGE("Wrong mefistoToDS.size: "<<mefistoToDS.size()<<" < "<<m + uvPtVec.size()-1);
599 return error("Internal error");
602 list< int > mOnVertex;
603 vector<UVPtStruct>::const_iterator uvPt = uvPtVec.begin();
604 for ( ++uvPt; uvPt != uvPtVec.end(); ++uvPt )
606 // bind mefisto ID to node
607 mefistoToDS[m] = uvPt->node;
609 uvslf[m].x = uvPt->u * scalex;
610 uvslf[m].y = uvPt->v * scaley;
611 switch ( uvPt->node->GetPosition()->GetTypeOfPosition())
613 case SMDS_TOP_VERTEX:
614 mOnVertex.push_back( m );
617 // In order to detect degenerated faces easily, we replace
618 // nodes on a degenerated edge by node on the vertex of that edge
619 if ( myTool->IsDegenShape( uvPt->node->getshapeId() ))
621 int edgeID = uvPt->node->getshapeId();
622 SMESH_subMesh* edgeSM = myTool->GetMesh()->GetSubMeshContaining( edgeID );
623 SMESH_subMeshIteratorPtr smIt = edgeSM->getDependsOnIterator( /*includeSelf=*/0,
624 /*complexShapeFirst=*/0);
627 SMESH_subMesh* vertexSM = smIt->next();
628 SMDS_NodeIteratorPtr nIt = vertexSM->GetSubMeshDS()->GetNodes();
630 mefistoToDS[m] = nIt->next();
639 int mFirst = mOnVertex.front(), mLast = m - 1;
640 list< int >::iterator mIt = mOnVertex.begin();
641 for ( ; mIt != mOnVertex.end(); ++mIt)
644 if ( iW && !VWMap.IsEmpty()) { // except outer wire
645 // avoid passing same uv point for a vertex common to 2 wires
646 int vID = mefistoToDS[m]->getshapeId();
647 TopoDS_Vertex V = TopoDS::Vertex( myTool->GetMeshDS()->IndexToShape( vID ));
648 if ( fixCommonVertexUV( uvslf[m], V, F, VWMap, *myTool->GetMesh(),
649 scalex, scaley, _quadraticMesh )) {
650 myNodesOnCommonV.push_back( mefistoToDS[m] );
654 // prevent failure on overlapped adjacent links,
655 // check only links ending in vertex nodes
656 int mB = m - 1, mA = m + 1; // indices Before and After
657 if ( mB < mFirst ) mB = mLast;
658 if ( mA > mLast ) mA = mFirst;
659 fixOverlappedLinkUV (uvslf[ mB ], uvslf[ m ], uvslf[ mA ]);
662 // cout << "MEFISTO INPUT************" << endl;
663 // for ( int i =0; i < m; ++i )
664 // cout << i << ": \t" << uvslf[i].x << ", " << uvslf[i].y << " Node " << mefistoToDS[i]->GetID()<< endl;
669 //=============================================================================
673 //=============================================================================
675 void StdMeshers_MEFISTO_2D::ComputeScaleOnFace(SMESH_Mesh & aMesh,
676 const TopoDS_Face & aFace,
680 TopoDS_Wire W = BRepTools::OuterWire(aFace);
682 double xmin = 1.e300; // min & max of face 2D parametric coord.
683 double xmax = -1.e300;
684 double ymin = 1.e300;
685 double ymax = -1.e300;
690 TopExp_Explorer wexp(W, TopAbs_EDGE);
691 for ( ; wexp.More(); wexp.Next())
693 const TopoDS_Edge & E = TopoDS::Edge( wexp.Current() );
695 Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface(E, aFace, f, l);
696 if ( C2d.IsNull() ) continue;
697 double du = (l - f) / double (nbp);
698 for (int i = 0; i <= nbp; i++)
700 double param = f + double (i) * du;
701 gp_Pnt2d p = C2d->Value(param);
710 // MESSAGE(" "<< f<<" "<<l<<" "<<param<<" "<<xmin<<" "<<xmax<<" "<<ymin<<" "<<ymax);
717 double xmoy = (xmax + xmin) / 2.;
718 double ymoy = (ymax + ymin) / 2.;
719 double xsize = xmax - xmin;
720 double ysize = ymax - ymin;
723 Handle(Geom_Surface) S = BRep_Tool::Surface(aFace,L); // 3D surface
727 gp_Pnt PX0 = S->Value(xmin, ymoy);
728 gp_Pnt PY0 = S->Value(xmoy, ymin);
729 double dx = xsize / double (nbp);
730 double dy = ysize / double (nbp);
731 for (int i = 1; i <= nbp; i++)
733 double x = xmin + double (i) * dx;
734 gp_Pnt PX = S->Value(x, ymoy);
735 double y = ymin + double (i) * dy;
736 gp_Pnt PY = S->Value(xmoy, y);
737 length_x += PX.Distance(PX0);
738 length_y += PY.Distance(PY0);
742 scalex = length_x / xsize;
743 scaley = length_y / ysize;
746 double xyratio = xsize*scalex/(ysize*scaley);
747 const double maxratio = 1.e2;
749 if (xyratio > maxratio) {
751 scaley *= xyratio / maxratio;
754 else if (xyratio < 1./maxratio) {
756 scalex *= 1 / xyratio / maxratio;
763 //=============================================================================
767 //=============================================================================
769 void StdMeshers_MEFISTO_2D::StoreResult(Z nbst, R2 * uvst, Z nbt, Z * nust,
770 vector< const SMDS_MeshNode*>&mefistoToDS,
771 double scalex, double scaley)
773 SMESHDS_Mesh * meshDS = myTool->GetMeshDS();
774 int faceID = myTool->GetSubShapeID();
776 TopoDS_Face F = TopoDS::Face( myTool->GetSubShape() );
777 Handle(Geom_Surface) S = BRep_Tool::Surface( F );
779 Z n = mefistoToDS.size(); // nb input points
780 mefistoToDS.resize( nbst );
781 for ( ; n < nbst; n++)
785 double u = uvst[n][0] / scalex;
786 double v = uvst[n][1] / scaley;
787 gp_Pnt P = S->Value(u, v);
789 SMDS_MeshNode * node = meshDS->AddNode(P.X(), P.Y(), P.Z());
790 meshDS->SetNodeOnFace(node, faceID, u, v);
792 //MESSAGE(P.X()<<" "<<P.Y()<<" "<<P.Z());
793 mefistoToDS[n] = node;
794 //MESSAGE("NEW: "<<n<<" "<<mefistoToDS[n+1]);
800 // triangle points must be in trigonometric order if face is Forward
801 // else they must be put clockwise
803 bool triangleIsWellOriented = ( F.Orientation() == TopAbs_FORWARD );
805 for (n = 1; n <= nbt; n++)
807 const SMDS_MeshNode * n1 = mefistoToDS[ nust[m++] - 1 ];
808 const SMDS_MeshNode * n2 = mefistoToDS[ nust[m++] - 1 ];
809 const SMDS_MeshNode * n3 = mefistoToDS[ nust[m++] - 1 ];
811 // avoid creating degenetrated faces
812 bool isDegen = ( myTool->HasDegeneratedEdges() && ( n1 == n2 || n1 == n3 || n2 == n3 ));
815 SMDS_MeshElement * elt;
816 if (triangleIsWellOriented)
817 elt = myTool->AddFace(n1, n2, n3);
819 elt = myTool->AddFace(n1, n3, n2);
820 meshDS->SetMeshElementOnShape(elt, faceID);
825 // remove bad elements built on vertices shared by wires
827 list<const SMDS_MeshNode*>::iterator itN = myNodesOnCommonV.begin();
828 for ( ; itN != myNodesOnCommonV.end(); itN++ )
830 const SMDS_MeshNode* node = *itN;
831 SMDS_ElemIteratorPtr invElemIt = node->GetInverseElementIterator();
832 while ( invElemIt->more() )
834 const SMDS_MeshElement* elem = invElemIt->next();
835 SMDS_ElemIteratorPtr itN = elem->nodesIterator();
837 while ( itN->more() )
838 if ( itN->next() == node)
841 MESSAGE( "RM bad element " << elem->GetID());
842 meshDS->RemoveElement( elem );