1 // Created by: Ekaterina SMIRNOVA
2 // Copyright (c) 2008-2012 OPEN CASCADE SAS
4 // The content of this file is subject to the Open CASCADE Technology Public
5 // License Version 6.5 (the "License"). You may not use the content of this file
6 // except in compliance with the License. Please obtain a copy of the License
7 // at http://www.opencascade.org and read it completely before using this file.
9 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
10 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 // The Original Code and all software distributed under the License is
13 // distributed on an "AS IS" basis, without warranty of any kind, and the
14 // Initial Developer hereby disclaims all such warranties, including without
15 // limitation, any warranties of merchantability, fitness for a particular
16 // purpose or non-infringement. Please see the License for the specific terms
17 // and conditions governing the rights and limitations under the License.
20 #include <BRepMesh_FastDiscretFace.ixx>
22 #include <Adaptor3d_IsoCurve.hxx>
23 #include <BRepAdaptor_Curve.hxx>
24 #include <BRepMesh_GeomTool.hxx>
25 #include <BRepMesh_ListOfXY.hxx>
26 #include <BRepMesh_Array1OfVertexOfDelaun.hxx>
27 #include <BRepMesh_ListIteratorOfListOfVertex.hxx>
28 #include <BRepMesh_ListIteratorOfListOfXY.hxx>
29 #include <BRepMesh_PairOfPolygon.hxx>
30 #include <BRepMesh_DataMapIteratorOfDataMapOfShapePairOfPolygon.hxx>
31 #include <BRep_ListIteratorOfListOfPointRepresentation.hxx>
32 #include <BRepMesh_ClassifierPtr.hxx>
33 #include <BRep_Builder.hxx>
34 #include <BRep_PointRepresentation.hxx>
35 #include <BRep_TVertex.hxx>
36 #include <BRep_Tool.hxx>
37 #include <Geom_Surface.hxx>
39 #include <Standard_ErrorHandler.hxx>
40 #include <Standard_Failure.hxx>
41 #include <SortTools_ShellSortOfReal.hxx>
42 #include <Poly_PolygonOnTriangulation.hxx>
43 #include <Poly_Triangulation.hxx>
44 #include <TCollection_CompareOfReal.hxx>
45 #include <TColStd_Array1OfReal.hxx>
46 #include <TColStd_SequenceOfReal.hxx>
47 #include <TColStd_Array1OfInteger.hxx>
48 #include <TColStd_HArray1OfReal.hxx>
49 #include <TopExp_Explorer.hxx>
52 #include <TColgp_Array1OfPnt2d.hxx>
53 #include <NCollection_Map.hxx>
54 #include <Geom_BSplineSurface.hxx>
55 #include <GeomLib.hxx>
56 #include <Bnd_Box2d.hxx>
58 #define UVDEFLECTION 1.e-05
60 static Standard_Real FUN_CalcAverageDUV(TColStd_Array1OfReal& P, const Standard_Integer PLen)
62 Standard_Integer i, j, n = 0;
63 Standard_Real p, result = 0.;
65 for(i = 1; i <= PLen; i++)
68 for(j = i + 1; j <= PLen; j++)
80 p = Abs(P(i) - P(i-1));
88 return (n? (result / (Standard_Real) n) : -1.);
91 //=======================================================================
92 //function : BRepMesh_FastDiscretFace
94 //=======================================================================
95 BRepMesh_FastDiscretFace::BRepMesh_FastDiscretFace
96 (const Standard_Real theAngle,
97 const Standard_Boolean theWithShare) :
98 myAngle(theAngle), myWithShare(theWithShare), myNbLocat(0),
99 myInternalVerticesMode(Standard_True)
101 myAllocator = new NCollection_IncAllocator(64000);
104 //=======================================================================
105 //function : Add(face)
107 //=======================================================================
109 void BRepMesh_FastDiscretFace::Add(const TopoDS_Face& theFace,
110 const Handle(BRepMesh_FaceAttribute)& theAttrib,
111 const TopTools_DataMapOfShapeReal& theMapDefle)
118 TopoDS_Face face = theFace;
121 const Handle(Poly_Triangulation)& aFaceTrigu = BRep_Tool::Triangulation(face, loc);
122 if ( aFaceTrigu.IsNull() )
125 myAttrib = theAttrib;
126 face.Orientation(TopAbs_FORWARD);
127 myStructure.Nullify();
128 Handle(NCollection_IncAllocator) anAlloc = Handle(NCollection_IncAllocator)::DownCast(myAllocator);
129 anAlloc->Reset(Standard_False);
130 myStructure=new BRepMesh_DataStructureOfDelaun(anAlloc);
132 Standard_Real umax = myAttrib->GetUMax();
133 Standard_Real umin = myAttrib->GetUMin();
134 Standard_Real vmax = myAttrib->GetVMax();
135 Standard_Real vmin = myAttrib->GetVMin();
137 Standard_Real aTolU = (umax - umin) * UVDEFLECTION;
138 Standard_Real aTolV = (vmax - vmin) * UVDEFLECTION;
139 Standard_Real uCellSize = 14 * aTolU;
140 Standard_Real vCellSize = 14 * aTolV;
142 myStructure->Data().SetCellSize ( uCellSize, vCellSize );
143 myStructure->Data().SetTolerance( aTolU, aTolV );
146 BRepAdaptor_Surface BS(face, Standard_False);
147 Handle(BRepAdaptor_HSurface) gFace = new BRepAdaptor_HSurface(BS);
149 GeomAbs_SurfaceType thetype;
150 thetype = BS.GetType();
152 TopAbs_Orientation orFace = face.Orientation();
159 myLocation2d.Clear();
160 myInternaledges.Clear();
162 Standard_Integer i = 1;
163 Standard_Integer ipn = 0;
165 TopoDS_Iterator exW(face);
166 for (; exW.More(); exW.Next()) {
167 const TopoDS_Shape& aWire = exW.Value();
168 if (aWire.ShapeType() != TopAbs_WIRE)
170 TopoDS_Iterator ex(aWire);
171 for(; ex.More(); ex.Next()) {
172 const TopoDS_Edge& edge = TopoDS::Edge(ex.Value());
176 RestoreStructureFromTriangulation(edge, face, gFace, aFaceTrigu, theMapDefle(edge), loc);
180 ////////////////////////////////////////////////////////////
181 //add internal vertices after self-intersection check
182 Standard_Integer nbVertices = 0;
183 if(myInternalVerticesMode) {
184 for(TopExp_Explorer ex(face,TopAbs_VERTEX ,TopAbs_EDGE); ex.More(); ex.Next()) {
185 const TopoDS_Vertex& aVert = TopoDS::Vertex(ex.Current());
186 Add(aVert,face,gFace);
188 nbVertices = myVemap.Extent();
191 // essai de determination de la longueur vraie:
192 // akm (bug OCC16) : We must calculate these measures in non-singular
193 // parts of face. Let`s try to compute average value of three
194 // (umin, (umin+umax)/2, umax), and respectively for v.
196 //Standard_Real longu = 0.0, longv = 0.0; //, last , first;
197 //gp_Pnt P11, P12, P21, P22, P31, P32;
199 Standard_Real deltaX = myAttrib->GetDeltaX();
200 Standard_Real deltaY = myAttrib->GetDeltaY();
202 TColStd_Array1OfInteger tabvert_corr(1, nbVertices);
205 // Check the necessity to fill the map of parameters
206 const Standard_Boolean useUVParam = (thetype == GeomAbs_Torus ||
207 thetype == GeomAbs_BezierSurface ||
208 thetype == GeomAbs_BSplineSurface);
212 BRepMesh_VertexTool aMoveNodes(myVemap.Extent(), myAllocator);
214 aMoveNodes.SetCellSize ( uCellSize / deltaX, vCellSize / deltaY);
215 aMoveNodes.SetTolerance( aTolU / deltaX, aTolV / deltaY);
217 for (i = 1; i <= myStructure->NbNodes(); i++)
219 const BRepMesh_Vertex& v = myStructure->GetNode(i);
222 myUParam.Add(p2d.X());
223 myVParam.Add(p2d.Y());
226 res.SetCoord((p2d.X() - umin ) / deltaX,
227 (p2d.Y() - vmin ) / deltaY);
228 BRepMesh_Vertex v_new(res,v.Location3d(),v.Movability());
229 const BRepMesh_ListOfInteger& alist = myStructure->GetNodeList(i);
230 aMoveNodes.Add(v_new, alist);
233 myStructure->ReplaceNodes(aMoveNodes);
235 Standard_Boolean rajout;
237 BRepMesh_ClassifierPtr& classifier = theAttrib->GetClassifier();
243 rajout = Standard_True;
246 rajout = Standard_False;
249 BRepMesh_Delaun trigu(myStructure, tabvert_corr, orFace==TopAbs_FORWARD);
251 //removed all free edges from triangulation
252 Standard_Integer nbLinks = myStructure->NbNodes();
253 for(i = 1; i <= nbLinks; i++)
255 if(myStructure->ElemConnectedTo(i).Extent() < 1)
257 BRepMesh_Edge& anEdge = (BRepMesh_Edge&)trigu.GetEdge(i);
258 if(anEdge.Movability()==BRepMesh_Deleted)
260 anEdge.SetMovability(BRepMesh_Free);
261 myStructure->RemoveLink(i);
265 Standard_Boolean isaline;
266 isaline = ((umax-umin) < UVDEFLECTION) || ((vmax-vmin) < UVDEFLECTION);
268 Standard_Real aDef = -1;
269 if (!isaline && myStructure->ElemOfDomain().Extent() > 0) {
270 TColStd_ListOfInteger badTri, nulTri;
274 aDef = Control(gFace, theAttrib->GetDefFace(), myListver, badTri, nulTri, trigu, Standard_True);
275 if( aDef > theAttrib->GetDefFace() || aDef < 0.)
276 rajout = Standard_True;
282 if(myVParam.Extent() > 2) {
283 rajout = Standard_True;
287 if(myUParam.Extent() > 2) {
288 rajout = Standard_True;
295 InternalVertices(gFace, myListver, theAttrib->GetDefFace(), classifier);
297 if (myListver.Extent() > 0) {
298 BRepMesh_Array1OfVertexOfDelaun verttab(1, myListver.Extent());
299 BRepMesh_ListIteratorOfListOfVertex itVer(myListver);
301 for (; itVer.More(); itVer.Next())
302 verttab(ipn++) = itVer.Value();
303 trigu.AddVertices(verttab);
305 //control internal points
306 BRepMesh_ListOfVertex vvlist;
307 aDef = Control(gFace, theAttrib->GetDefFace(), vvlist, badTri, nulTri, trigu, Standard_False);
308 myListver.Append(vvlist);
312 //modify myStructure back
313 aMoveNodes.SetCellSize ( uCellSize, vCellSize );
314 aMoveNodes.SetTolerance( aTolU , aTolV );
315 for (i = 1; i <= myStructure->NbNodes(); i++)
317 const BRepMesh_Vertex& v = myStructure->GetNode(i);
320 res.SetCoord(p2d.X() * deltaX + umin, p2d.Y() * deltaY + vmin);
321 BRepMesh_Vertex v_new(res,v.Location3d(),v.Movability());
322 const BRepMesh_ListOfInteger& alist = myStructure->GetNodeList(i);
323 aMoveNodes.Add(v_new, alist);
325 myStructure->ReplaceNodes(aMoveNodes);
327 AddInShape(face, (aDef < 0.0)? theAttrib->GetDefFace() : aDef);
330 catch(Standard_Failure)
333 Handle(Poly_Triangulation) TNull;
334 B.UpdateFace(theFace,TNull);
337 myStructure.Nullify();
341 //=======================================================================
342 //function : RestoreStructureFromTriangulation(edge)
343 //purpose : Restore structure of Delaun from triangulation on face
344 //=======================================================================
345 Standard_Boolean BRepMesh_FastDiscretFace::RestoreStructureFromTriangulation
346 (const TopoDS_Edge& theEdge,
347 const TopoDS_Face& theFace,
348 const Handle(BRepAdaptor_HSurface)& theSurf,
349 const Handle(Poly_Triangulation)& theTrigu,
350 const Standard_Real theDefEdge,
351 const TopLoc_Location& theLoc)
353 // oan: changes for right restoring of triangulation data from face & edges
354 Handle(Poly_PolygonOnTriangulation) Poly;
355 Poly = BRep_Tool::PolygonOnTriangulation(theEdge, theTrigu, theLoc);
357 if (Poly.IsNull() || !Poly->HasParameters())
359 return Standard_False;
363 TopAbs_Orientation orEdge = theEdge.Orientation();
364 // Get end points on 2d curve
365 gp_Pnt2d uvFirst, uvLast;
366 BRep_Tool::UVPoints(theEdge, theFace, uvFirst, uvLast);
369 TopoDS_Vertex pBegin, pEnd;
370 TopExp::Vertices(theEdge,pBegin,pEnd);
372 const Standard_Boolean sameUV =
373 uvFirst.IsEqual(uvLast, Precision::PConfusion());
375 const TColgp_Array1OfPnt2d& UVNodes = theTrigu->UVNodes();
376 const TColgp_Array1OfPnt& Nodes = theTrigu->Nodes();
377 const TColStd_Array1OfInteger& Indices = Poly->Nodes();
379 const Standard_Integer nbnodes = Indices.Length();
380 TColStd_Array1OfInteger NewNodes(1, nbnodes);
385 // Process first vertex
386 Standard_Integer ipf;
387 if (myVertices.IsBound(pBegin))
389 ipf = myVertices.Find(pBegin);
393 if (sameUV && myVertices.IsBound(pEnd))
395 ipf = myVertices.Find(pEnd);
399 P3d = Nodes(Indices(1));
400 if (!theLoc.IsIdentity())
401 P3d.Transform(theLoc.Transformation());
403 myLocation3d.Bind(myNbLocat, P3d);
406 myVertices.Bind(pBegin,ipf);
409 //Controle vertice tolerances
410 gp_Pnt pFirst = theSurf->Value(uvFirst.X(), uvFirst.Y());
411 gp_Pnt pLast = theSurf->Value(uvLast.X(), uvLast.Y());
413 Standard_Real mindist = 10. * Max(pFirst.Distance(BRep_Tool::Pnt(pBegin)),
414 pLast.Distance(BRep_Tool::Pnt(pEnd)));
416 if (mindist < BRep_Tool::Tolerance(pBegin) ||
417 mindist < BRep_Tool::Tolerance(pEnd) ) mindist = theDefEdge;
419 anUV = FindUV(pBegin, uvFirst, ipf, theSurf, mindist, myLocation2d);
420 Standard_Integer iv1, isv1;
421 BRepMesh_Vertex vf(anUV, ipf, BRepMesh_Frontier);
422 iv1 = myStructure->AddNode(vf);
423 isv1 = myVemap.FindIndex(iv1);
425 isv1 = myVemap.Add(iv1);
428 // Process last vertex
429 Standard_Integer ipl, ivl;
430 if (pEnd.IsSame(pBegin))
436 if (myVertices.IsBound(pEnd))
438 ipl = myVertices.Find(pEnd);
449 P3d = Nodes(Indices(nbnodes));
450 if (!theLoc.IsIdentity())
451 P3d.Transform(theLoc.Transformation());
453 myLocation3d.Bind(myNbLocat, P3d);
456 myVertices.Bind(pEnd,ipl);
460 anUV = FindUV(pEnd, uvLast, ipl, theSurf, mindist, myLocation2d);
461 BRepMesh_Vertex vl(anUV, ipl, BRepMesh_Frontier);
463 Standard_Integer isvl;
464 ivl = myStructure->AddNode(vl);
465 isvl = myVemap.FindIndex(ivl);
467 isvl = myVemap.Add(ivl);
468 NewNodes(nbnodes) = isvl;
473 for (i = 2; i < nbnodes; i++)
476 P3d = Nodes(Indices(i));
477 if (!theLoc.IsIdentity())
478 P3d.Transform(theLoc.Transformation());
480 myLocation3d.Bind(myNbLocat, P3d);
483 anUV = UVNodes(Indices(i)).Coord();
485 Standard_Integer iv2, isv;
486 v.Initialize(anUV, myNbLocat, BRepMesh_Frontier);
487 iv2 = myStructure->AddNode(v);
488 isv = myVemap.FindIndex(iv2);
490 isv = myVemap.Add(iv2);
494 if (orEdge == TopAbs_FORWARD)
495 myStructure->AddLink(BRepMesh_Edge(iv1,iv2,BRepMesh_Frontier));
496 else if (orEdge == TopAbs_REVERSED)
497 myStructure->AddLink(BRepMesh_Edge(iv2,iv1,BRepMesh_Frontier));
498 else if (orEdge == TopAbs_INTERNAL)
499 myStructure->AddLink(BRepMesh_Edge(iv1,iv2,BRepMesh_Fixed));
505 if (orEdge == TopAbs_FORWARD)
506 myStructure->AddLink(BRepMesh_Edge(iv1,ivl,BRepMesh_Frontier));
507 else if (orEdge == TopAbs_REVERSED)
508 myStructure->AddLink(BRepMesh_Edge(ivl,iv1,BRepMesh_Frontier));
509 else if (orEdge == TopAbs_INTERNAL)
510 myStructure->AddLink(BRepMesh_Edge(iv1,ivl,BRepMesh_Fixed));
513 Handle(Poly_PolygonOnTriangulation) P1 =
514 new Poly_PolygonOnTriangulation(NewNodes, Poly->Parameters()->Array1());
515 P1->Deflection(theDefEdge);
516 if (myInternaledges.IsBound(theEdge))
518 BRepMesh_PairOfPolygon& pair = myInternaledges.ChangeFind(theEdge);
519 if (theEdge.Orientation() == TopAbs_REVERSED)
526 BRepMesh_PairOfPolygon pair1;
528 myInternaledges.Bind(theEdge, pair1);
531 return Standard_True;
534 //=======================================================================
535 //function : InternalVertices
537 //=======================================================================
539 static void filterParameters(const TColStd_IndexedMapOfReal& theParams,
540 const Standard_Real theMinDist,
541 const Standard_Real theFilterDist,
542 TColStd_SequenceOfReal& theResult)
544 // Sort sequence of parameters
545 TColStd_SequenceOfReal aParamTmp;
546 Standard_Integer aParamLength = 1;
547 const Standard_Integer anInitLen = theParams.Extent();
549 TColStd_Array1OfReal aParamArray(1, anInitLen);
551 for (j = 1; j <= anInitLen; j++)
552 aParamArray(j) = theParams(j);
554 TCollection_CompareOfReal aCompare;
555 SortTools_ShellSortOfReal::Sort(aParamArray, aCompare);
557 // mandadory pre-filtering using the first (minimal) filter value
558 Standard_Real aP1, aP2;
559 aP1 = aParamArray(1);
560 aParamTmp.Append(aP1);
561 for (j = 2; j <= anInitLen; j++)
563 aP2 = aParamArray(j);
564 if ((aP2-aP1) > theMinDist)
566 aParamTmp.Append(aP2);
572 //add last point if required
573 if(aParamArray(anInitLen)-theParams(aParamLength) > theMinDist)
575 aParamTmp.Append(aParamArray(anInitLen));
579 //perform filtering on series
580 Standard_Real aLastAdded, aLastCandidate;
581 Standard_Boolean isCandidateDefined = Standard_False;
582 aLastAdded = aParamTmp.First();
583 aLastCandidate = aLastAdded;
584 theResult.Append(aParamTmp.First());
586 for(j=2;j<aParamTmp.Length();j++)
588 Standard_Real aVal = aParamTmp.Value(j);
589 if(aVal-aLastAdded > theFilterDist)
592 if(isCandidateDefined) {
593 aLastAdded = aLastCandidate;
594 isCandidateDefined = Standard_False;
601 theResult.Append(aLastAdded);
605 aLastCandidate = aVal;
606 isCandidateDefined = Standard_True;
608 theResult.Append(aParamTmp.Last());
610 if( theResult.Length() == 2 )
612 Standard_Real dist = theResult.Last() - theResult.First();
613 Standard_Integer nbint = (Standard_Integer)((dist / theFilterDist) + 0.5);
617 //Five points more is maximum
624 Standard_Real dU = dist / nbint;
625 for( i = 1; i < nbint; i++ )
627 theResult.InsertAfter(i, theResult.First()+i*dU);
633 void BRepMesh_FastDiscretFace::InternalVertices(const Handle(BRepAdaptor_HSurface)& theCaro,
634 BRepMesh_ListOfVertex& theInternalV,
635 const Standard_Real theDefFace,
636 const BRepMesh_ClassifierPtr& theClassifier)
638 BRepMesh_Vertex newV;
642 // work following the type of surface
643 const BRepAdaptor_Surface& BS = *(BRepAdaptor_Surface*)&(theCaro->Surface());
644 GeomAbs_SurfaceType thetype = theCaro->GetType();
646 Standard_Real umax = myAttrib->GetUMax();
647 Standard_Real umin = myAttrib->GetUMin();
648 Standard_Real vmax = myAttrib->GetVMax();
649 Standard_Real vmin = myAttrib->GetVMin();
650 Standard_Real deltaX = myAttrib->GetDeltaX();
651 Standard_Real deltaY = myAttrib->GetDeltaY();
653 if (thetype == GeomAbs_Sphere)
655 gp_Sphere S = BS.Sphere();
656 const Standard_Real R = S.Radius();
658 // Calculate parameters for iteration in V direction
659 Standard_Real Dv = 1.0 - (theDefFace/R);
660 if (Dv < 0.0) Dv = 0.0;
661 Standard_Real oldDv = 2.0 * ACos (Dv);
662 Dv = .7 * oldDv; //.7 ~= sqrt(2.) - Dv is hypotenuse of triangle when oldDv is legs
663 const Standard_Real sv = vmax - vmin;
664 Dv = sv/((Standard_Integer)(sv/Dv) + 1);
665 const Standard_Real pasvmax = vmax-Dv*0.5;
667 //Du can be defined from relation: 2*r*Sin(Du/2) = 2*R*Sin(Dv/2), r = R*Cos(v)
668 //here approximate relation r*Du = R*Dv is used
670 Standard_Real Du, pasu, pasv; //, ru;
671 const Standard_Real su = umax-umin;
672 Standard_Boolean Shift = Standard_False;
673 for (pasv = vmin + Dv; pasv < pasvmax; pasv += Dv)
675 // Calculate parameters for iteration in U direction
676 // 1.-.365*pasv*pasv is simple approximation of Cos(pasv)
677 // with condition that it gives ~.1 when pasv = pi/2
678 Du = Dv/(1.-.365*pasv*pasv);
679 Du = su/((Standard_Integer)(su/Du) + 1);
681 const Standard_Real d = (Shift)? Du*.5 : 0.;
682 const Standard_Real pasumax = umax-Du*0.5 + d;
683 for (pasu = umin + Du - d; pasu < pasumax; pasu += Du)
685 if (theClassifier->Perform(gp_Pnt2d(pasu, pasv)) == TopAbs_IN)
688 #ifdef DEB_MESH_CHRONO
691 ElSLib::D0(pasu, pasv, S, p3d);
693 myLocation3d.Bind(myNbLocat, p3d);
695 p2d.SetCoord((pasu-umin)/deltaX, (pasv-vmin)/deltaY);
696 newV.Initialize(p2d.XY(), myNbLocat, BRepMesh_Free);
697 theInternalV.Append(newV);
702 else if (thetype == GeomAbs_Cylinder)
704 gp_Cylinder S = BS.Cylinder();
705 const Standard_Real R = S.Radius();
707 // Calculate parameters for iteration in U direction
708 Standard_Real Du = 1.0 - (theDefFace/R);
709 if (Du < 0.0) Du = 0.0;
710 Du = 2.0 * ACos (Du);
711 if (Du > myAngle) Du = myAngle;
712 const Standard_Real su = umax - umin;
713 const Standard_Integer nbU = (Standard_Integer)(su/Du);
716 // Calculate parameters for iteration in V direction
717 const Standard_Real sv = vmax - vmin;
718 Standard_Integer nbV = (Standard_Integer)( nbU*sv/(su*R) );
719 nbV = Min(nbV, 100*nbU);
720 Standard_Real Dv = sv/(nbV+1);
722 Standard_Real pasu, pasv, pasvmax = vmax-Dv*0.5, pasumax = umax-Du*0.5;
723 for (pasv = vmin + Dv; pasv < pasvmax; pasv += Dv) {
724 for (pasu = umin + Du; pasu < pasumax; pasu += Du) {
725 if (theClassifier->Perform(gp_Pnt2d(pasu, pasv)) == TopAbs_IN)
728 ElSLib::D0(pasu, pasv, S, p3d);
730 myLocation3d.Bind(myNbLocat, p3d);
732 p2d.SetCoord((pasu-umin)/deltaX, (pasv-vmin)/deltaY);
733 newV.Initialize(p2d.XY(), myNbLocat, BRepMesh_Free);
734 theInternalV.Append(newV);
739 else if (thetype == GeomAbs_Cone)
741 Standard_Real R, RefR, SAng;
742 gp_Cone C = BS.Cone();
743 RefR = C.RefRadius();
744 SAng = C.SemiAngle();
745 R = Max(Abs(RefR+vmin*Sin(SAng)), Abs(RefR+vmax*Sin(SAng)));
746 Standard_Real Du, Dv, pasu, pasv;
747 Du = Max(1.0e0 - (theDefFace/R),0.0e0);
748 Du = (2.0 * ACos (Du));
749 Standard_Integer nbU = (Standard_Integer) ( (umax-umin)/Du );
750 Standard_Integer nbV = (Standard_Integer) ( nbU*(vmax-vmin)/((umax-umin)*R) );
751 Du = (umax-umin)/(nbU+1);
752 Dv = (vmax-vmin)/(nbV+1);
754 Standard_Real pasvmax = vmax-Dv*0.5, pasumax = umax-Du*0.5;
755 for (pasv = vmin + Dv; pasv < pasvmax; pasv += Dv) {
756 for (pasu = umin + Du; pasu < pasumax; pasu += Du) {
757 if (theClassifier->Perform(gp_Pnt2d(pasu, pasv)) == TopAbs_IN)
760 ElSLib::D0(pasu, pasv, C, p3d);
762 myLocation3d.Bind(myNbLocat, p3d);
764 p2d.SetCoord((pasu-umin)/deltaX, (pasv-vmin)/deltaY);
765 newV.Initialize(p2d.XY(), myNbLocat, BRepMesh_Free);
766 theInternalV.Append(newV);
771 else if (thetype == GeomAbs_Torus)
773 gp_Torus T = BS.Torus();
775 Standard_Boolean insert;
776 Standard_Integer i, j, ParamULength, ParamVLength;
777 Standard_Real pp, pasu, pasv;
778 Standard_Real r = T.MinorRadius(), R = T.MajorRadius();
780 TColStd_SequenceOfReal ParamU, ParamV;
782 Standard_Real Du, Dv;//, pasu, pasv;
783 Dv = Max(1.0e0 - (theDefFace/r),0.0e0) ;
784 Standard_Real oldDv = 2.0 * ACos (Dv);
785 oldDv = Min(oldDv, myAngle);
786 Dv = 0.9*oldDv; //TWOTHIRD * oldDv;
789 Standard_Integer nbV = Max((Standard_Integer)((vmax-vmin)/Dv), 2);
790 Dv = (vmax-vmin)/(nbV+1);
791 Standard_Real ru = R + r;
794 Du = 2.0 * ACos(Max(1.0 - (theDefFace/ru),0.0));
795 if (myAngle < Du) Du = myAngle;
796 Standard_Real aa = sqrt(Du*Du + oldDv*oldDv);
797 if(aa < gp::Resolution())
799 Du *= Min(oldDv, Du) / aa;
803 Standard_Integer nbU = Max((Standard_Integer)((umax-umin)/Du), 2);
804 nbU = Max(nbU , (int)(nbV*(umax-umin)*R/((vmax-vmin)*r)/5.));
805 Du = (umax-umin)/(nbU+1);
809 // As the points of edges are returned.
810 // in this case, the points are not representative.
812 //-- Choose DeltaX and DeltaY so that to avoid skipping points on the grid
813 for (i = 0; i <= nbU; i++) ParamU.Append(umin + i* Du);
818 // Number of mapped U parameters
819 const Standard_Integer LenU = myUParam.Extent();
820 // Fill array of U parameters
821 TColStd_Array1OfReal Up(1,LenU);
822 for (j = 1; j <= LenU; j++) Up(j) = myUParam(j);
824 // Calculate DU, leave array of parameters
825 Standard_Real aDU = FUN_CalcAverageDUV(Up,LenU);
826 aDU = Max(aDU, Abs(umax - umin) / (Standard_Real) nbU / 2.);
827 Standard_Real dUstd = Abs(umax - umin) / (Standard_Real) LenU;
828 if (aDU > dUstd) dUstd = aDU;
830 for (j = 1; j <= LenU; j++)
833 insert = Standard_True;
834 ParamULength = ParamU.Length();
835 for (i = 1; i <= ParamULength && insert; i++)
837 insert = (Abs(ParamU.Value(i)-pp) > (0.5*dUstd));
839 if (insert) ParamU.Append(pp);
844 // Number of mapped V parameters
845 const Standard_Integer LenV = myVParam.Extent();
846 // Fill array of V parameters
847 TColStd_Array1OfReal Vp(1,LenV);
848 for (j = 1; j <= LenV; j++) Vp(j) = myVParam(j);
849 // Calculate DV, sort array of parameters
850 Standard_Real aDV = FUN_CalcAverageDUV(Vp,LenV);
851 aDV = Max(aDV, Abs(vmax - vmin) / (Standard_Real) nbV / 2.);
853 Standard_Real dVstd = Abs(vmax - vmin) / (Standard_Real) LenV;
854 if (aDV > dVstd) dVstd = aDV;
856 for (j = 1; j <= LenV; j++)
860 insert = Standard_True;
861 ParamVLength = ParamV.Length();
862 for (i = 1; i <= ParamVLength && insert; i++)
864 insert = (Abs(ParamV.Value(i)-pp) > (dVstd*2./3.));
866 if (insert) ParamV.Append(pp);
869 Standard_Integer Lu = ParamU.Length(), Lv = ParamV.Length();
870 Standard_Real uminnew = umin+deltaY*0.1;
871 Standard_Real vminnew = vmin+deltaX*0.1;
872 Standard_Real umaxnew = umax-deltaY*0.1;
873 Standard_Real vmaxnew = vmax-deltaX*0.1;
875 for (i = 1; i <= Lu; i++)
877 pasu = ParamU.Value(i);
878 if (pasu >= uminnew && pasu < umaxnew)
880 for (j = 1; j <= Lv; j++)
882 pasv = ParamV.Value(j);
883 if (pasv >= vminnew && pasv < vmaxnew)
885 if (theClassifier->Perform(gp_Pnt2d(pasu, pasv)) == TopAbs_IN)
888 ElSLib::D0(pasu, pasv, T, p3d);
890 myLocation3d.Bind(myNbLocat, p3d);
892 p2d.SetCoord((pasu-umin)/deltaX, (pasv-vmin)/deltaY);
893 newV.Initialize(p2d.XY(), myNbLocat, BRepMesh_Free);
894 theInternalV.Append(newV);
901 else if (thetype == GeomAbs_BezierSurface || thetype == GeomAbs_BSplineSurface)
904 Standard_Real uRes = BS.UResolution(theDefFace);
905 Standard_Real vRes = BS.VResolution(theDefFace);
907 // Sort and filter sequence of parameters
908 Standard_Real aMinDu = Precision::PConfusion();
912 Standard_Real aDuMaxLim = 0.1*(umax-umin);
913 Standard_Real ddu = Min(aDuMaxLim,Max(0.005*(umax-umin),2.*uRes));
914 TColStd_SequenceOfReal ParamU;
915 filterParameters(myUParam,aMinDu,ddu,ParamU);
916 Standard_Integer ParamULength = ParamU.Length();
918 Standard_Real aMinDv = Precision::PConfusion();
922 Standard_Real aDvMaxLim = 0.1*(vmax-vmin);
923 Standard_Real ddv = Min(aDvMaxLim,Max(0.005*(vmax-vmin),2.*vRes));
924 TColStd_SequenceOfReal ParamV;
925 filterParameters(myVParam,aMinDv,ddv,ParamV);
926 Standard_Integer ParamVLength = ParamV.Length();
928 // check intermediate isolines
929 Handle(Geom_Surface) B;
930 if (thetype == GeomAbs_BezierSurface) {
937 gp_Pnt P1, P2, PControl;
938 Standard_Real u, v, dist;
940 // precision for compare square distances
941 double dPreci = Precision::Confusion()*Precision::Confusion();
943 // Insert V parameters by deflection criterion
944 Standard_Integer i,j;
945 Standard_Real V1, V2, U1, U2;
946 for (i = 1; i <= ParamULength; i++) {
947 Handle(Geom_Curve) IsoU = B->UIso(ParamU.Value(i));
948 V1 = ParamV.Value(1);
949 P1 = IsoU->Value(V1);
950 for (j = 2; j <= ParamVLength;) {
951 V2 = ParamV.Value(j);
952 P2 = IsoU->Value(V2);
954 PControl = IsoU->Value(v);
955 // 23.03.2010 skl for OCC21645 - change precision for comparison
956 if( P1.SquareDistance(P2) > dPreci ) {
957 gp_Lin L (P1, gp_Dir(gp_Vec(P1, P2)));
958 dist = L.Distance(PControl);
961 dist = P1.Distance(PControl);
963 if (dist > theDefFace) {
965 ParamV.InsertBefore(j, v);
969 //put regular grig for normals
970 gp_Dir N1(0,0,1),N2(0,0,1);
971 Standard_Boolean aSt1 = GeomLib::NormEstim(B, gp_Pnt2d(ParamU.Value(i),V1), Precision::Confusion(), N1);
972 Standard_Boolean aSt2 = GeomLib::NormEstim(B, gp_Pnt2d(ParamU.Value(i),v), Precision::Confusion(), N2);
974 Standard_Real anAngle1 = N2.Angle(N1);
975 if(aSt1 < 1 && aSt2 < 1 && anAngle1 > myAngle ) {
977 ParamV.InsertBefore(j, v);
989 for (i = 2; i < ParamVLength; i++) {
991 Handle(Geom_Curve) IsoV = B->VIso(v);
992 U1 = ParamU.Value(1);
993 P1 = IsoV->Value(U1);
994 for (j = 2; j <= ParamULength;) {
995 U2 = ParamU.Value(j);
996 P2 = IsoV->Value(U2);
998 PControl = IsoV->Value(u);
999 // 23.03.2010 skl for OCC21645 - change precision for comparison
1000 if( P1.SquareDistance(P2) > dPreci ) {
1001 gp_Lin L (P1, gp_Dir(gp_Vec(P1, P2)));
1002 dist = L.Distance(PControl);
1005 dist = P1.Distance(PControl);
1007 if (dist > theDefFace) {
1009 ParamU.InsertBefore(j, u);
1014 //put regular grig for normals
1015 gp_Dir N1(0,0,1),N2(0,0,1);
1016 Standard_Boolean aSt1 = GeomLib::NormEstim(B, gp_Pnt2d(U1,v), Precision::Confusion(), N1);
1017 Standard_Boolean aSt2 = GeomLib::NormEstim(B, gp_Pnt2d(u,v), Precision::Confusion(), N2);
1019 Standard_Real anAngle1 = N2.Angle(N1);
1020 if(aSt1 < 1 && aSt2 < 1 && anAngle1 > myAngle) {
1022 ParamU.InsertBefore(j, u);
1028 if (j < ParamULength) {
1029 // Classify intersection point
1030 if (theClassifier->Perform(gp_Pnt2d(U1, v)) == TopAbs_IN)
1034 myLocation3d.Bind(myNbLocat, P1);
1036 p2d.SetCoord((U1-umin)/deltaX, (v-vmin)/deltaY);
1037 newV.Initialize(p2d.XY(), myNbLocat, BRepMesh_Free);
1038 theInternalV.Append(newV);
1048 const Standard_Real anAngle = 0.35;
1050 Standard_Integer i, j, nbpointsU = 10, nbpointsV = 10;
1051 Adaptor3d_IsoCurve tabu[11], tabv[11];
1053 TColStd_SequenceOfReal ParamU, ParamV;
1054 Standard_Real u, v, du, dv;
1055 Standard_Integer iu, iv;
1058 du = (umax-umin) / (nbpointsU+1); dv = (vmax-vmin) / (nbpointsV+1);
1060 for (iu = 0; iu <= nbpointsU; iu++) {
1062 tabu[iu].Load(theCaro);
1063 tabu[iu].Load(GeomAbs_IsoU, u);
1066 for (iv = 0; iv <= nbpointsV; iv++) {
1068 tabv[iv].Load(theCaro);
1069 tabv[iv].Load(GeomAbs_IsoV, v);
1072 Standard_Integer imax = 1, MaxV = 0;
1074 GCPnts_TangentialDeflection* tabGU = new GCPnts_TangentialDeflection[nbpointsU+1];
1076 for (i = 0; i <= nbpointsU; i++) {
1077 f = Max(vmin, tabu[i].FirstParameter());
1078 l = Min(vmax, tabu[i].LastParameter());
1079 GCPnts_TangentialDeflection theDeflection(tabu[i], f, l, anAngle, 0.7*theDefFace, 2);
1080 tabGU[i] = theDeflection;
1081 if (tabGU[i].NbPoints() > MaxV) {
1082 MaxV = tabGU[i].NbPoints();
1087 // return table of parameters V:
1088 Standard_Integer NV = tabGU[imax].NbPoints();
1089 for (i = 1; i <= NV; i++) {
1090 ParamV.Append(tabGU[imax].Parameter(i));
1095 Standard_Integer MaxU = 0;
1097 GCPnts_TangentialDeflection* tabGV = new GCPnts_TangentialDeflection[nbpointsV+1];
1099 for (i = 0; i <= nbpointsV; i++) {
1100 f = Max(umin, tabv[i].FirstParameter());
1101 l = Min(umax, tabv[i].LastParameter());
1102 GCPnts_TangentialDeflection thedeflection2(tabv[i], f, l, anAngle, 0.7*theDefFace, 2);
1103 tabGV[i] = thedeflection2;
1104 if (tabGV[i].NbPoints() > MaxU) {
1105 MaxU = tabGV[i].NbPoints();
1110 // return table of parameters U:
1111 Standard_Integer NU = tabGV[imax].NbPoints();
1112 for (i = 1; i <= NU; i++) {
1113 ParamU.Append(tabGV[imax].Parameter(i));
1117 if (ParamU.Length() == 2) {
1118 ParamU.InsertAfter(1, (umax+umin)*0.5);
1120 if (ParamV.Length() == 2) {
1121 ParamV.InsertAfter(1, (vmax+vmin)*0.5);
1124 TColStd_SequenceOfReal InsertV, InsertU;
1127 Adaptor3d_IsoCurve IsoV;
1130 Standard_Integer Lu = ParamU.Length(), Lv = ParamV.Length();
1132 for (i = 2; i < Lv; i++) {
1133 v = ParamV.Value(i);
1134 IsoV.Load(GeomAbs_IsoV, v);
1135 for (j = 2; j < Lu; j++) {
1136 u = ParamU.Value(j);
1137 if (theClassifier->Perform(gp_Pnt2d(u, v)) == TopAbs_IN)
1142 myLocation3d.Bind(myNbLocat, P1);
1144 p2d.SetCoord((u-umin)/deltaX, (v-vmin)/deltaY);
1145 newV.Initialize(p2d.XY(), myNbLocat, BRepMesh_Free);
1146 theInternalV.Append(newV);
1151 #ifdef DEB_MESH_CHRONO
1157 * Internal class Couple, moved from MeshData package
1160 class BRepMesh_Couple
1163 BRepMesh_Couple() { myI1 = myI2 = 0; }
1164 BRepMesh_Couple(const Standard_Integer I1,
1165 const Standard_Integer I2)
1166 { myI1 = I1; myI2 = I2; }
1168 Standard_Integer myI1;
1169 Standard_Integer myI2;
1172 inline Standard_Boolean IsEqual(const BRepMesh_Couple& one,
1173 const BRepMesh_Couple& other)
1175 if (one.myI1 == other.myI1 &&
1176 one.myI2 == other.myI2) return Standard_True;
1177 else return Standard_False;
1180 inline Standard_Integer HashCode(const BRepMesh_Couple& one,
1181 const Standard_Integer Upper)
1183 return ::HashCode((one.myI1+one.myI2), Upper);
1186 typedef NCollection_Map<BRepMesh_Couple> BRepMesh_MapOfCouple;
1188 //=======================================================================
1189 //function : Control
1191 //=======================================================================
1192 Standard_Real BRepMesh_FastDiscretFace::Control(const Handle(BRepAdaptor_HSurface)& theCaro,
1193 const Standard_Real theDefFace,
1194 BRepMesh_ListOfVertex& theInternalV,
1195 TColStd_ListOfInteger& theBadTriangles,
1196 TColStd_ListOfInteger& theNulTriangles,
1197 BRepMesh_Delaun& theTrigu,
1198 const Standard_Boolean theIsFirst)
1200 //IMPORTANT: Constants used in calculations
1201 const Standard_Real MinimalArea2d = 1.e-9;
1202 const Standard_Real MinimalSqLength3d = 1.e-12;
1204 // Define the number of iterations
1205 Standard_Integer myNbIterations = 11;
1206 const Standard_Integer nbPasses = (theIsFirst? 1 : myNbIterations);
1208 // Initialize stop condition
1209 Standard_Boolean allDegenerated = Standard_False;
1210 Standard_Integer nbInserted = 1;
1212 // Create map of links to skip already processed
1213 Standard_Integer nbtriangles;
1215 nbtriangles = myStructure->ElemOfDomain().Extent();
1216 if (nbtriangles <= 0) return -1.0;
1217 BRepMesh_MapOfCouple theCouples(3*nbtriangles);
1220 gp_XYZ vecEd1, vecEd2, vecEd3;
1222 Standard_Real dv = 0, defl = 0, maxdef = -1;
1223 Standard_Integer pass = 1, nf = 0, nl = 0;
1224 BRepMesh_Vertex InsVertex;
1225 Standard_Boolean caninsert;
1227 Standard_Real sqdefface = theDefFace * theDefFace;
1229 GeomAbs_SurfaceType thetype = theCaro->GetType();
1230 Handle(Geom_Surface) BSpl;
1231 Standard_Boolean isSpline = Standard_False;
1232 if (thetype == GeomAbs_BezierSurface || thetype == GeomAbs_BSplineSurface)
1234 isSpline = Standard_True;
1235 if (thetype == GeomAbs_BezierSurface)
1236 BSpl = theCaro->Bezier();
1238 BSpl = theCaro->BSpline();
1241 NCollection_DataMap<Standard_Integer,gp_Dir> aNorMap;
1242 NCollection_DataMap<Standard_Integer,Standard_Integer> aStatMap;
1244 // Perform refinement passes
1245 for (; pass <= nbPasses && nbInserted && !allDegenerated; pass++)
1247 theInternalV.Clear();
1248 theBadTriangles.Clear();
1250 // Reset stop condition
1251 allDegenerated = Standard_True;
1255 // Do not insert nodes in last pass in non-SharedMode
1256 caninsert = (myWithShare || pass < nbPasses);
1259 nbtriangles = myStructure->ElemOfDomain().Extent();
1260 if (nbtriangles <= 0) break;
1262 // Iterate on current triangles
1263 BRepMesh_MapOfInteger::Iterator triDom;
1264 const BRepMesh_MapOfInteger& TriMap = myStructure->ElemOfDomain();
1265 triDom.Initialize(TriMap);
1266 Standard_Integer aNbPnt = 0;
1267 Standard_Real umin = myAttrib->GetUMin();
1268 Standard_Real vmin = myAttrib->GetVMin();
1269 Standard_Real deltaX = myAttrib->GetDeltaX();
1270 Standard_Real deltaY = myAttrib->GetDeltaY();
1271 for (; triDom.More(); triDom.Next())
1273 Standard_Integer TriId = triDom.Key();
1274 const BRepMesh_Triangle& curTri=Triangle(TriId);
1275 if (curTri.Movability()==BRepMesh_Deleted) continue;
1277 Standard_Boolean o1, o2, o3;
1278 Standard_Integer v1 = 0, v2 = 0, v3 = 0, e1 = 0, e2 = 0, e3 = 0;
1279 curTri.Edges(e1, e2, e3, o1, o2, o3);
1281 const BRepMesh_Edge& edg1=Edge(e1);
1282 const BRepMesh_Edge& edg2=Edge(e2);
1283 const BRepMesh_Edge& edg3=Edge(e3);
1285 Standard_Boolean m1 = (edg1.Movability() == BRepMesh_Frontier);
1286 Standard_Boolean m2 = (edg2.Movability() == BRepMesh_Frontier);
1287 Standard_Boolean m3 = (edg3.Movability() == BRepMesh_Frontier);
1289 v1=edg1.FirstNode();
1294 v2=edg1.FirstNode();
1299 v3=edg2.FirstNode();
1301 const BRepMesh_Vertex& vert1=Vertex(v1);
1302 const BRepMesh_Vertex& vert2=Vertex(v2);
1303 const BRepMesh_Vertex& vert3=Vertex(v3);
1305 const gp_XYZ& p1=myLocation3d(vert1.Location3d()).Coord();
1306 const gp_XYZ& p2=myLocation3d(vert2.Location3d()).Coord();
1307 const gp_XYZ& p3=myLocation3d(vert3.Location3d()).Coord();
1313 // Check for degenerated triangle
1314 if (vecEd1.SquareModulus() < MinimalSqLength3d ||
1315 vecEd2.SquareModulus() < MinimalSqLength3d ||
1316 vecEd3.SquareModulus() < MinimalSqLength3d)
1318 theNulTriangles.Append(TriId);
1322 allDegenerated = Standard_False;
1324 gp_XY xy1(vert1.Coord().X()*deltaX+umin,vert1.Coord().Y()*deltaY+vmin);
1325 gp_XY xy2(vert2.Coord().X()*deltaX+umin,vert2.Coord().Y()*deltaY+vmin);
1326 gp_XY xy3(vert3.Coord().X()*deltaX+umin,vert3.Coord().Y()*deltaY+vmin);
1328 // Check triangle area in 2d
1329 if (Abs((xy2-xy1)^(xy3-xy1)) < MinimalArea2d)
1331 theNulTriangles.Append(TriId);
1335 // Check triangle normal
1336 gp_XYZ normal(vecEd1^vecEd2);
1337 dv = normal.Modulus();
1338 if (dv < Precision::Confusion())
1340 theNulTriangles.Append(TriId);
1345 // Check deflection on triangle
1346 mi2d = (xy1+xy2+xy3)/3.0;
1347 theCaro->D0(mi2d.X(), mi2d.Y(), pDef);
1348 defl = Abs(normal*(pDef.XYZ()-p1));
1350 /*mi2d = (xy1+xy2+xy3)/3.0;
1351 theCaro->D0(mi2d.X(), mi2d.Y(), pDef);
1352 defl = pDef.SquareDistance((p1+p2+p3)/3.);*/
1353 if (defl > maxdef) maxdef = defl;
1354 if (defl > sqdefface)
1356 if (theIsFirst) break;
1359 // Record new vertex
1362 myLocation3d.Bind(myNbLocat,pDef);
1363 mi2d.SetCoord((mi2d.X()-umin)/deltaX,(mi2d.Y()-vmin)/deltaY);
1364 InsVertex.Initialize(mi2d,myNbLocat,BRepMesh_Free);
1365 theInternalV.Append(InsVertex);
1367 theBadTriangles.Append(TriId);
1370 if (!m2) // Not a boundary
1372 // Check if this link was already processed
1373 if (v2 < v3) { nf = v2; nl = v3; } else { nf = v3; nl = v2; }
1374 if (theCouples.Add(BRepMesh_Couple(nf,nl)))
1376 // Check deflection on edge 1
1377 mi2d = (xy2+xy3)*0.5;
1378 theCaro->D0(mi2d.X(), mi2d.Y(), pDef);
1379 gp_Lin L (p2, gp_Vec(p2, p3));
1380 defl = L.SquareDistance(pDef);
1381 if (defl > maxdef) maxdef = defl;
1382 if (defl > sqdefface)
1384 if (theIsFirst) break;
1387 // Record new vertex
1390 myLocation3d.Bind(myNbLocat,pDef);
1391 mi2d.SetCoord((mi2d.X()-umin)/deltaX,(mi2d.Y()-vmin)/deltaY);
1392 InsVertex.Initialize(mi2d,myNbLocat,BRepMesh_Free);
1393 theInternalV.Append(InsVertex);
1395 theBadTriangles.Append(TriId);
1400 if (!m3) // Not a boundary
1402 // Check if this link was already processed
1403 if (v1 < v3) { nf = v1; nl = v3; } else { nf = v3; nl = v1; }
1404 if (theCouples.Add(BRepMesh_Couple(nf,nl)))
1406 // Check deflection on edge 2
1407 mi2d = (xy3+xy1)*0.5;
1408 theCaro->D0(mi2d.X(), mi2d.Y(), pDef);
1409 gp_Lin L (p1, gp_Vec(p1, p3));
1410 defl = L.SquareDistance(pDef);
1411 if (defl > maxdef) maxdef = defl;
1412 if (defl > sqdefface)
1414 if (theIsFirst) break;
1417 // Record new vertex
1420 myLocation3d.Bind(myNbLocat,pDef);
1421 mi2d.SetCoord((mi2d.X()-umin)/deltaX,(mi2d.Y()-vmin)/deltaY);
1422 InsVertex.Initialize(mi2d,myNbLocat,BRepMesh_Free);
1423 theInternalV.Append(InsVertex);
1425 theBadTriangles.Append(TriId);
1430 if (!m1) // Not a boundary
1432 // Check if this link was already processed
1433 if (v1 < v2) { nf = v1; nl = v2; } else { nf = v2; nl = v1; }
1434 if (theCouples.Add(BRepMesh_Couple(nf,nl)))
1436 // Check deflection on edge 3
1437 mi2d = (xy1+xy2)*0.5;
1438 theCaro->D0(mi2d.X(), mi2d.Y(), pDef);
1439 gp_Lin L (p1, gp_Vec(p1, p2));
1440 defl = L.SquareDistance(pDef);
1441 if (defl > maxdef) maxdef = defl;
1442 if (defl > sqdefface)
1444 if (theIsFirst) break;
1447 // Record new vertex
1450 myLocation3d.Bind(myNbLocat,pDef);
1451 mi2d.SetCoord((mi2d.X()-umin)/deltaX,(mi2d.Y()-vmin)/deltaY);
1452 InsVertex.Initialize(mi2d,myNbLocat,BRepMesh_Free);
1453 theInternalV.Append(InsVertex);
1455 theBadTriangles.Append(TriId);
1460 //check normal on bsplines
1461 if(theIsFirst && isSpline && !BSpl.IsNull() )
1463 gp_Dir N1(0,0,1), N2(0,0,1), N3(0,0,1);
1464 Standard_Integer aSt1, aSt2, aSt3;
1465 if(aNorMap.IsBound(v1)) {
1466 aSt1 = aStatMap.Find(v1);
1467 N1 =aNorMap.Find(v1);
1470 aSt1 = GeomLib::NormEstim(BSpl, gp_Pnt2d(xy1), Precision::Confusion(), N1);
1471 aStatMap.Bind(v1,aSt1);
1472 aNorMap.Bind(v1,N1);
1475 if(aNorMap.IsBound(v2)) {
1476 aSt2 = aStatMap.Find(v2);
1477 N2 = aNorMap.Find(v2);
1480 aSt2 = GeomLib::NormEstim(BSpl, gp_Pnt2d(xy2), Precision::Confusion(), N2);
1481 aStatMap.Bind(v2,aSt2);
1482 aNorMap.Bind(v2,N2);
1485 if(aNorMap.IsBound(v3)) {
1486 aSt3 = aStatMap.Find(v3);
1487 N3 = aNorMap.Find(v3);
1490 aSt3 = GeomLib::NormEstim(BSpl, gp_Pnt2d(xy3), Precision::Confusion(), N3);
1491 aStatMap.Bind(v3,aSt3);
1492 aNorMap.Bind(v3,N3.XYZ());
1495 Standard_Real anAngle1 = N2.Angle(N1);
1496 Standard_Real anAngle2 = N3.Angle(N2);
1497 Standard_Real anAngle3 = N1.Angle(N3);
1498 if(aSt1 < 1 && aSt2 < 1 && aSt3 < 1 &&
1499 (anAngle1 > myAngle || anAngle2 > myAngle || anAngle3 > myAngle)) {
1507 if (!theIsFirst && theInternalV.Extent() > 0)
1509 BRepMesh_Array1OfVertexOfDelaun verttab(1, theInternalV.Extent());
1510 BRepMesh_ListIteratorOfListOfVertex itVer(theInternalV);
1511 Standard_Integer ipn = 1;
1512 for (; itVer.More(); itVer.Next())
1513 verttab(ipn++) = itVer.Value();
1515 theTrigu.AddVertices(verttab);
1522 return Sqrt(maxdef);
1525 //=======================================================================
1526 //function : AddInShape
1528 //=======================================================================
1529 void BRepMesh_FastDiscretFace::AddInShape(const TopoDS_Face& theFace,
1530 const Standard_Real theDefFace)
1534 TopLoc_Location loc = theFace.Location();
1535 Handle(Poly_Triangulation) TOld = BRep_Tool::Triangulation(theFace, loc);
1536 Handle(Poly_Triangulation) TNull;
1537 Handle(Poly_PolygonOnTriangulation) NullPoly;
1538 B.UpdateFace(theFace,TNull);
1541 BRepMesh_MapOfInteger::Iterator it;
1543 Standard_Integer e1, e2, e3, nTri;
1544 Standard_Integer v1, v2, v3, iv1, iv2, iv3;
1545 Standard_Integer i, index;
1546 Standard_Boolean o1, o2, o3;
1547 TopAbs_Orientation orFace = theFace.Orientation();
1549 const BRepMesh_MapOfInteger& TriMap = myStructure->ElemOfDomain();
1550 it.Initialize(TriMap);
1552 nTri = TriMap.Extent();
1556 Poly_Array1OfTriangle Tri(1, nTri);
1560 for (; it.More(); it.Next()) {
1561 myStructure->GetElement(it.Key()).Edges(e1, e2, e3, o1, o2, o3);
1563 const BRepMesh_Edge& ve1=myStructure->GetLink(e1);
1564 const BRepMesh_Edge& ve2=myStructure->GetLink(e2);
1583 iv1 = myVemap.FindIndex(v1);
1584 if (iv1 == 0) iv1 = myVemap.Add(v1);
1585 iv2 = myVemap.FindIndex(v2);
1586 if (iv2 == 0) iv2 = myVemap.Add(v2);
1587 iv3 = myVemap.FindIndex(v3);
1588 if (iv3 == 0) iv3 = myVemap.Add(v3);
1590 if (orFace == TopAbs_REVERSED) Tri(i++).Set(iv1, iv3, iv2);
1591 else Tri(i++).Set(iv1, iv2, iv3);
1594 Standard_Integer nbVertices = myVemap.Extent();
1595 Handle(Poly_Triangulation) T = new Poly_Triangulation(nbVertices, nTri, Standard_True);
1596 Poly_Array1OfTriangle& Trian = T->ChangeTriangles();
1598 TColgp_Array1OfPnt& Nodes = T->ChangeNodes();
1599 TColgp_Array1OfPnt2d& Nodes2d = T->ChangeUVNodes();
1601 for (i = 1; i <= nbVertices; i++) {
1602 index = myVemap.FindKey(i);
1603 Nodes(i) = Pnt(index);
1604 Nodes2d(i).SetXY(Vertex(index).Coord());
1607 T->Deflection(theDefFace);
1609 // storage of triangulation in BRep.
1611 //TopLoc_Location loc = theFace.Location();
1612 if (!loc.IsIdentity()) {
1613 gp_Trsf tr = loc.Transformation();
1615 for (i = Nodes.Lower(); i <= Nodes.Upper(); i++)
1616 Nodes(i).Transform(tr);
1618 B1.UpdateFace(theFace, T);
1620 // implement polygons on triangulation in the face:
1621 BRepMesh_DataMapIteratorOfDataMapOfShapePairOfPolygon It(myInternaledges);
1623 for (; It.More(); It.Next()) {
1624 const BRepMesh_PairOfPolygon& pair = It.Value();
1625 const Handle(Poly_PolygonOnTriangulation)& NOD1 = pair.First();
1626 const Handle(Poly_PolygonOnTriangulation)& NOD2 = pair.Last();
1627 if ( NOD1 == NOD2 ) {
1628 B.UpdateEdge(TopoDS::Edge(It.Key()), NullPoly, TOld,loc);
1629 B.UpdateEdge(TopoDS::Edge(It.Key()), NOD1, T, loc);
1632 B.UpdateEdge(TopoDS::Edge(It.Key()), NullPoly, TOld,loc);
1633 B.UpdateEdge(TopoDS::Edge(It.Key()), NOD1, NOD2, T, loc);
1639 catch(Standard_Failure)
1641 // MESH_FAILURE(theFace);
1646 //=======================================================================
1647 //function : Triangle
1649 //=======================================================================
1651 const BRepMesh_Triangle& BRepMesh_FastDiscretFace::Triangle(const Standard_Integer Index) const
1653 return myStructure->GetElement(Index);
1656 //=======================================================================
1657 //function : NbEdges
1659 //=======================================================================
1661 /*Standard_Integer BRepMesh_FastDiscretFace::NbEdges() const
1663 return myStructure->NbLinks();
1666 //=======================================================================
1669 //=======================================================================
1671 const BRepMesh_Edge& BRepMesh_FastDiscretFace::Edge(const Standard_Integer Index) const
1673 return myStructure->GetLink(Index);
1677 //=======================================================================
1680 //=======================================================================
1682 const BRepMesh_Vertex& BRepMesh_FastDiscretFace::Vertex(const Standard_Integer Index) const
1684 return myStructure->GetNode(Index);
1687 //=======================================================================
1690 //=======================================================================
1692 const gp_Pnt& BRepMesh_FastDiscretFace::Pnt(const Standard_Integer Index) const
1694 return myLocation3d(myStructure->GetNode(Index).Location3d());
1697 //=======================================================================
1700 //=======================================================================
1702 gp_XY BRepMesh_FastDiscretFace::FindUV(const TopoDS_Vertex& theV,
1703 const gp_Pnt2d& theXY,
1704 const Standard_Integer theIp,
1705 const Handle(BRepAdaptor_HSurface)& theSFace,
1706 const Standard_Real theMinDist,
1707 BRepMesh_DataMapOfIntegerListOfXY& theLocation2dMap)
1710 if (theLocation2dMap.IsBound(theIp))
1712 BRepMesh_ListOfXY& L = theLocation2dMap.ChangeFind(theIp);
1714 if (L.Extent() != 1)
1716 BRepMesh_ListIteratorOfListOfXY it(L);
1718 Standard_Real dd, dmin = theXY.Distance(gp_Pnt2d(anUV));
1719 for (; it.More(); it.Next())
1721 dd = theXY.Distance(gp_Pnt2d(it.Value()));
1730 const Standard_Real tol = Min(2. * BRep_Tool::Tolerance(theV), theMinDist);
1732 const Standard_Real Utol2d = .5 * (theSFace->LastUParameter() - theSFace->FirstUParameter());
1733 const Standard_Real Vtol2d = .5 * (theSFace->LastVParameter() - theSFace->FirstVParameter());
1735 const gp_Pnt p1 = theSFace->Value(anUV.X(), anUV.Y());
1736 const gp_Pnt p2 = theSFace->Value(theXY.X(), theXY.Y());
1738 if (Abs(anUV.X() - theXY.X()) > Utol2d ||
1739 Abs(anUV.Y() - theXY.Y()) > Vtol2d ||
1740 !p1.IsEqual(p2, tol))
1742 anUV = theXY.Coord();
1748 anUV = theXY.Coord();
1749 BRepMesh_ListOfXY L;
1751 theLocation2dMap.Bind(theIp, L);
1757 static Standard_Boolean GetVertexParameters(const TopoDS_Vertex& theVert,
1758 const TopoDS_Face& theFace,
1762 const Handle(Geom_Surface)& S = BRep_Tool::Surface(theFace,L);
1763 L = L.Predivided(theVert.Location());
1764 BRep_ListIteratorOfListOfPointRepresentation itpr =
1765 ((*((Handle(BRep_TVertex)*) &theVert.TShape()))->Points());
1766 // Check first if there are PointRepresentation (case non Manifold)
1768 while (itpr.More()) {
1769 if (itpr.Value()->IsPointOnSurface(S,L)) {
1770 thePoint.SetCoord(itpr.Value()->Parameter(),
1771 itpr.Value()->Parameter2());
1772 return Standard_True;
1776 return Standard_False;
1779 //=======================================================================
1781 //purpose : method intended to add internal myVertices in triangulation.
1782 //=======================================================================
1783 void BRepMesh_FastDiscretFace::Add(const TopoDS_Vertex& theVert,
1784 const TopoDS_Face& theFace,
1785 const Handle(BRepAdaptor_HSurface)& thegFace)
1787 const TopAbs_Orientation anOrient = theVert.Orientation();
1789 if( anOrient != TopAbs_INTERNAL || !GetVertexParameters(theVert,theFace,uvXY))
1791 Standard_Integer indVert =0;
1792 if (myVertices.IsBound(theVert))
1793 indVert = myVertices.Find(theVert);
1797 myLocation3d.Bind(myNbLocat, BRep_Tool::Pnt(theVert));
1798 indVert = myNbLocat;
1799 myVertices.Bind(theVert, indVert);
1801 Standard_Real mindist = BRep_Tool::Tolerance(theVert);
1802 // gp_Pnt2d uvXY = BRep_Tool::Parameters(theVert,theFace);
1803 gp_XY anUV = FindUV(theVert, uvXY, indVert, thegFace, mindist, myLocation2d);
1804 BRepMesh_Vertex vf(anUV, indVert, BRepMesh_Fixed);
1805 Standard_Integer ivff = myStructure->AddNode(vf);
1806 Standard_Integer isvf = myVemap.FindIndex(ivff);
1807 if (isvf == 0) isvf = myVemap.Add(ivff);