1 // Created on: 1993-01-21
2 // Created by: Remi LEQUETTE
3 // Copyright (c) 1993-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and / or modify it
9 // under the terms of the GNU Lesser General Public version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 #include <Standard_Stream.hxx>
19 #include <BRepTools.ixx>
20 #include <BRepTools_ShapeSet.hxx>
21 #include <BRep_Tool.hxx>
23 #include <TopExp_Explorer.hxx>
25 #include <TopoDS_Iterator.hxx>
26 #include <BndLib_Add2dCurve.hxx>
27 #include <Geom2dAdaptor_Curve.hxx>
28 #include <Geom_Surface.hxx>
29 #include <Geom_Curve.hxx>
30 #include <Geom2d_Curve.hxx>
31 #include <BRepTools_MapOfVertexPnt2d.hxx>
32 #include <BRep_CurveRepresentation.hxx>
33 #include <BRep_ListIteratorOfListOfCurveRepresentation.hxx>
34 #include <BRep_TEdge.hxx>
35 #include <TColgp_SequenceOfPnt2d.hxx>
36 #include <TColStd_SequenceOfReal.hxx>
37 #include <TColStd_Array1OfReal.hxx>
38 #include <TColGeom2d_SequenceOfCurve.hxx>
39 #include <TopTools_SequenceOfShape.hxx>
40 #include <Precision.hxx>
42 #include <Poly_Triangulation.hxx>
43 #include <Poly_PolygonOnTriangulation.hxx>
44 #include <TColStd_HArray1OfInteger.hxx>
45 #include <TColStd_MapOfTransient.hxx>
47 #include <gp_Lin2d.hxx>
49 #include <gp_Vec2d.hxx>
50 #include <Standard_ErrorHandler.hxx>
51 #include <Standard_Failure.hxx>
55 //=======================================================================
58 //=======================================================================
60 void BRepTools::UVBounds(const TopoDS_Face& F,
61 Standard_Real& UMin, Standard_Real& UMax,
62 Standard_Real& VMin, Standard_Real& VMax)
66 B.Get(UMin,VMin,UMax,VMax);
69 //=======================================================================
72 //=======================================================================
74 void BRepTools::UVBounds(const TopoDS_Face& F,
76 Standard_Real& UMin, Standard_Real& UMax,
77 Standard_Real& VMin, Standard_Real& VMax)
81 B.Get(UMin,VMin,UMax,VMax);
85 //=======================================================================
88 //=======================================================================
90 void BRepTools::UVBounds(const TopoDS_Face& F,
92 Standard_Real& UMin, Standard_Real& UMax,
93 Standard_Real& VMin, Standard_Real& VMax)
97 B.Get(UMin,VMin,UMax,VMax);
100 //=======================================================================
101 //function : AddUVBounds
103 //=======================================================================
105 void BRepTools::AddUVBounds(const TopoDS_Face& FF, Bnd_Box2d& B)
108 F.Orientation(TopAbs_FORWARD);
109 TopExp_Explorer ex(F,TopAbs_EDGE);
111 // fill box for the given face
113 for (;ex.More();ex.Next()) {
114 BRepTools::AddUVBounds(F,TopoDS::Edge(ex.Current()),aBox);
117 // if the box is empty (face without edges or without pcurves),
118 // get natural bounds
120 Standard_Real UMin,UMax,VMin,VMax;
122 BRep_Tool::Surface(F,L)->Bounds(UMin,UMax,VMin,VMax);
123 aBox.Update(UMin,VMin,UMax,VMax);
126 // add face box to result
131 //=======================================================================
132 //function : AddUVBounds
134 //=======================================================================
136 void BRepTools::AddUVBounds(const TopoDS_Face& F,
137 const TopoDS_Wire& W,
141 for (ex.Init(W,TopAbs_EDGE);ex.More();ex.Next()) {
142 BRepTools::AddUVBounds(F,TopoDS::Edge(ex.Current()),B);
147 //=======================================================================
148 //function : AddUVBounds
150 //=======================================================================
152 void BRepTools::AddUVBounds(const TopoDS_Face& F,
153 const TopoDS_Edge& E,
158 const Handle(Geom2d_Curve) C = BRep_Tool::CurveOnSurface(E,F,pf,pl);
159 if (C.IsNull()) return;
160 if (pl < pf) { // Petit Blindage
162 aux = pf; pf = pl; pl = aux;
164 Geom2dAdaptor_Curve PC(C,pf,pl);
165 if (Precision::IsNegativeInfinite(pf) ||
166 Precision::IsPositiveInfinite(pf)) {
167 Geom2dAdaptor_Curve GC(PC);
168 BndLib_Add2dCurve::Add(GC,0.,B);
172 // just compute points to get a close box.
174 Standard_Real Umin,Umax,Vmin,Vmax;
175 const Handle(Geom_Surface)& Surf=BRep_Tool::Surface(F,L);
176 Surf->Bounds(Umin,Umax,Vmin,Vmax);
180 Standard_Integer i, j, k, nbp = 20;
181 if (PC.GetType() == GeomAbs_Line) nbp = 2;
182 Standard_Integer NbIntC1 = PC.NbIntervals(GeomAbs_C1);
185 TColStd_Array1OfReal SharpPoints(1, NbIntC1+1);
186 PC.Intervals(SharpPoints, GeomAbs_C1);
187 TColStd_Array1OfReal Parameters(1, nbp*NbIntC1+1);
189 for (i = 1; i <= NbIntC1; i++)
191 Standard_Real delta = (SharpPoints(i+1) - SharpPoints(i))/nbp;
192 for (j = 0; j < nbp; j++)
193 Parameters(k++) = SharpPoints(i) + j*delta;
195 Parameters(nbp*NbIntC1+1) = SharpPoints(NbIntC1+1);
201 Standard_Real du=0.0;
202 Standard_Real dv=0.0;
205 for (i = 2; i < Parameters.Upper(); i++) {
209 if(i==2) { Pb=Pc; Pc=P; }
211 //-- Calcul de la fleche
213 gp_Vec2d PaPc(Pa,Pc);
214 // gp_Lin2d L2d(Pa,PaPc);
215 // Standard_Real up = ElCLib::Parameter(L2d,Pb);
216 // gp_Pnt2d PProj = ElCLib::Value(up,L2d);
217 gp_Pnt2d PProj(Pa.Coord()+(PaPc.XY()/2.));
218 Standard_Real ddu=Abs(Pb.X()-PProj.X());
219 Standard_Real ddv=Abs(Pb.Y()-PProj.Y());
227 //-- cout<<" du="<<du<<" dv="<<dv<<endl;
228 Standard_Real u0,u1,v0,v1;
229 Baux.Get(u0,v0,u1,v1);
232 u0-=du; v0-=dv; u1+=du; v1+=dv;
233 if(Surf->IsUPeriodic()) { }
235 if(u0<=Umin) { u0=Umin; }
236 if(u1>=Umax) { u1=Umax; }
238 if(Surf->IsVPeriodic()) { }
240 if(v0<=Vmin) { v0=Vmin; }
241 if(v1>=Vmax) { v1=Vmax; }
243 P.SetCoord(u0,v0) ; Baux.Add(P);
244 P.SetCoord(u1,v1) ; Baux.Add(P);
247 Standard_Real aXmin, aYmin, aXmax, aYmax;
248 Baux.Get(aXmin, aYmin, aXmax, aYmax);
249 Standard_Real Tol2d = Precision::PConfusion();
250 if (Abs(aXmin - Umin) <= Tol2d)
252 if (Abs(aYmin - Vmin) <= Tol2d)
254 if (Abs(aXmax - Umax) <= Tol2d)
256 if (Abs(aYmax - Vmax) <= Tol2d)
258 FinalBox.Update(aXmin, aYmin, aXmax, aYmax);
264 //=======================================================================
267 //=======================================================================
269 void BRepTools::Update(const TopoDS_Vertex&)
273 //=======================================================================
276 //=======================================================================
278 void BRepTools::Update(const TopoDS_Edge&)
282 //=======================================================================
285 //=======================================================================
287 void BRepTools::Update(const TopoDS_Wire&)
291 //=======================================================================
294 //=======================================================================
296 void BRepTools::Update(const TopoDS_Face& F)
299 UpdateFaceUVPoints(F);
300 F.TShape()->Checked(Standard_True);
304 //=======================================================================
307 //=======================================================================
309 void BRepTools::Update(const TopoDS_Shell& S)
311 TopExp_Explorer ex(S,TopAbs_FACE);
313 Update(TopoDS::Face(ex.Current()));
318 //=======================================================================
321 //=======================================================================
323 void BRepTools::Update(const TopoDS_Solid& S)
325 TopExp_Explorer ex(S,TopAbs_FACE);
327 Update(TopoDS::Face(ex.Current()));
332 //=======================================================================
335 //=======================================================================
337 void BRepTools::Update(const TopoDS_CompSolid& CS)
339 TopExp_Explorer ex(CS,TopAbs_FACE);
341 Update(TopoDS::Face(ex.Current()));
346 //=======================================================================
349 //=======================================================================
351 void BRepTools::Update(const TopoDS_Compound& C)
353 TopExp_Explorer ex(C,TopAbs_FACE);
355 Update(TopoDS::Face(ex.Current()));
360 //=======================================================================
363 //=======================================================================
365 void BRepTools::Update(const TopoDS_Shape& S)
367 switch (S.ShapeType()) {
370 Update(TopoDS::Vertex(S));
374 Update(TopoDS::Edge(S));
378 Update(TopoDS::Wire(S));
382 Update(TopoDS::Face(S));
386 Update(TopoDS::Shell(S));
390 Update(TopoDS::Solid(S));
393 case TopAbs_COMPSOLID :
394 Update(TopoDS::CompSolid(S));
397 case TopAbs_COMPOUND :
398 Update(TopoDS::Compound(S));
408 //=======================================================================
409 //function : UpdateFaceUVPoints
410 //purpose : reset the UV points of a Face
411 //=======================================================================
413 void BRepTools::UpdateFaceUVPoints(const TopoDS_Face& F)
415 // Recompute for each edge the two UV points in order to have the same
416 // UV point on connected edges.
418 // First edge loop, store the vertices in a Map with their 2d points
420 BRepTools_MapOfVertexPnt2d theVertices;
421 TopoDS_Iterator expE,expV;
422 TopoDS_Iterator EdgeIt,VertIt;
423 TColStd_SequenceOfReal aFSeq, aLSeq;
424 TColGeom2d_SequenceOfCurve aCSeq;
425 TopTools_SequenceOfShape aShSeq;
428 // a 3d tolerance for UV !!
429 Standard_Real tolerance = BRep_Tool::Tolerance(F);
430 TColgp_SequenceOfPnt2d emptySequence;
432 for (expE.Initialize(F); expE.More(); expE.Next()) {
433 if(expE.Value().ShapeType() != TopAbs_WIRE)
436 EdgeIt.Initialize(expE.Value());
437 for( ; EdgeIt.More(); EdgeIt.Next())
439 const TopoDS_Edge& E = TopoDS::Edge(EdgeIt.Value());
441 Handle(Geom2d_Curve) C = BRep_Tool::CurveOnSurface(E,F,f,l);
448 if (C.IsNull()) continue;
450 for (expV.Initialize(E.Oriented(TopAbs_FORWARD));
451 expV.More(); expV.Next()) {
453 const TopoDS_Vertex& V = TopoDS::Vertex(expV.Value());
455 TopAbs_Orientation Vori = V.Orientation();
456 if ( Vori == TopAbs_INTERNAL ) {
460 Standard_Real p = BRep_Tool::Parameter(V,E,F);
462 if (!theVertices.IsBound(V))
463 theVertices.Bind(V,emptySequence);
464 TColgp_SequenceOfPnt2d& S = theVertices(V);
465 for (i = 1; i <= S.Length(); i++) {
466 if (P.Distance(S(i)) < tolerance) break;
474 // second edge loop, update the edges 2d points
478 for(Standard_Integer j = 1; j <= aShSeq.Length(); j++)
480 const TopoDS_Edge& E = TopoDS::Edge(aShSeq.Value(j));
481 const Handle(Geom2d_Curve)& C = aCSeq.Value(j);
482 if (C.IsNull()) continue;
484 TopExp::Vertices(E,Vf,Vl);
486 Pf.SetCoord(RealLast(),RealLast());
489 if ( Vf.Orientation() == TopAbs_INTERNAL ) {
492 const TColgp_SequenceOfPnt2d& seqf = theVertices(Vf);
493 if (seqf.Length() == 1)
496 C->D0(aFSeq.Value(j),Pf);
497 for (i = 1; i <= seqf.Length(); i++) {
498 if (Pf.Distance(seqf(i)) <= tolerance) {
506 Pl.SetCoord(RealLast(),RealLast());
509 if ( Vl.Orientation() == TopAbs_INTERNAL ) {
512 const TColgp_SequenceOfPnt2d& seql = theVertices(Vl);
513 if (seql.Length() == 1)
516 C->D0(aLSeq.Value(j),Pl);
517 for (i = 1; i <= seql.Length(); i++) {
518 if (Pl.Distance(seql(i)) <= tolerance) {
526 // set the correct points
527 BRep_Tool::SetUVPoints(E,F,Pf,Pl);
533 //=======================================================================
536 //=======================================================================
538 Standard_Boolean BRepTools::Compare(const TopoDS_Vertex& V1,
539 const TopoDS_Vertex& V2)
541 if (V1.IsSame(V2)) return Standard_True;
542 gp_Pnt p1 = BRep_Tool::Pnt(V1);
543 gp_Pnt p2 = BRep_Tool::Pnt(V2);
544 Standard_Real l = p1.Distance(p2);
545 if (l <= BRep_Tool::Tolerance(V1)) return Standard_True;
546 if (l <= BRep_Tool::Tolerance(V2)) return Standard_True;
547 return Standard_False;
550 //=======================================================================
553 //=======================================================================
555 Standard_Boolean BRepTools::Compare(const TopoDS_Edge& E1,
556 const TopoDS_Edge& E2)
558 if (E1.IsSame(E2)) return Standard_True;
559 return Standard_False;
562 //=======================================================================
563 //function : OuterWire
565 //=======================================================================
567 TopoDS_Wire BRepTools::OuterWire(const TopoDS_Face& F)
570 TopExp_Explorer expw (F,TopAbs_WIRE);
573 Wres = TopoDS::Wire(expw.Current());
576 Standard_Real UMin, UMax, VMin, VMax;
577 Standard_Real umin, umax, vmin, vmax;
578 BRepTools::UVBounds(F,Wres,UMin,UMax,VMin,VMax);
579 while (expw.More()) {
580 const TopoDS_Wire& W = TopoDS::Wire(expw.Current());
581 BRepTools::UVBounds(F,W,umin, umax, vmin, vmax);
582 if ((umin <= UMin) &&
599 //=======================================================================
600 //function : Map3DEdges
602 //=======================================================================
604 void BRepTools::Map3DEdges(const TopoDS_Shape& S,
605 TopTools_IndexedMapOfShape& M)
608 for (Ex.Init(S,TopAbs_EDGE); Ex.More(); Ex.Next()) {
609 if (!BRep_Tool::Degenerated(TopoDS::Edge(Ex.Current())))
614 //=======================================================================
617 //=======================================================================
619 void BRepTools::Dump(const TopoDS_Shape& Sh, Standard_OStream& S)
621 BRepTools_ShapeSet SS;
628 //=======================================================================
629 //function : BRepTools_Write
631 //=======================================================================
632 void BRepTools_Write (const TopoDS_Shape& S,
633 const Standard_CString File)
635 BRepTools::Write (S,File);
639 //=======================================================================
642 //=======================================================================
644 void BRepTools::Write(const TopoDS_Shape& Sh, Standard_OStream& S,
645 const Handle(Message_ProgressIndicator)& PR)
647 BRepTools_ShapeSet SS;
655 //=======================================================================
658 //=======================================================================
660 void BRepTools::Read(TopoDS_Shape& Sh,
662 const BRep_Builder& B,
663 const Handle(Message_ProgressIndicator)& PR)
665 BRepTools_ShapeSet SS(B);
671 //=======================================================================
674 //=======================================================================
676 Standard_Boolean BRepTools::Write(const TopoDS_Shape& Sh,
677 const Standard_CString File,
678 const Handle(Message_ProgressIndicator)& PR)
681 // if (!fic.open(File,output)) return Standard_False;
682 os.open(File, ios::out);
683 if (!os.rdbuf()->is_open()) return Standard_False;
685 Standard_Boolean isGood = (os.good() && !os.eof());
689 BRepTools_ShapeSet SS;
693 os << "DBRep_DrawableShape\n"; // for easy Draw read
703 isGood = os.good() && isGood && !errno;
708 //=======================================================================
711 //=======================================================================
713 Standard_Boolean BRepTools::Read(TopoDS_Shape& Sh,
714 const Standard_CString File,
715 const BRep_Builder& B,
716 const Handle(Message_ProgressIndicator)& PR)
720 if (!fic.open(File, ios::in)) return Standard_False;
722 BRepTools_ShapeSet SS(B);
725 if(!SS.NbShapes()) return Standard_False;
727 return Standard_True;
731 //=======================================================================
734 //=======================================================================
736 void BRepTools::Clean(const TopoDS_Shape& S)
740 Handle(Poly_Triangulation) TNULL, T;
741 Handle(Poly_PolygonOnTriangulation) PolyNULL, Poly;
745 for (ex.Init(S,TopAbs_FACE);ex.More();ex.Next()) {
746 const TopoDS_Face& F = TopoDS::Face(ex.Current());
747 B.UpdateFace(F, TNULL);
749 for (ex.Init(S, TopAbs_EDGE); ex.More(); ex.Next()) {
750 const TopoDS_Edge& E = TopoDS::Edge(ex.Current());
751 // agv 21.09.01 : Inefficient management of Locations -> improve performance
753 // BRep_Tool::PolygonOnTriangulation(E, Poly, T, L);
754 // B.UpdateEdge(E, PolyNULL, T, L);
755 // } while(!Poly.IsNull());
757 Handle(BRep_CurveRepresentation) cr;
758 const Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*) &E.TShape());
759 BRep_ListOfCurveRepresentation& lcr = TE -> ChangeCurves();
760 BRep_ListIteratorOfListOfCurveRepresentation itcr(lcr);
762 // find and remove all representations
763 while (itcr.More()) {
765 if (cr->IsPolygonOnTriangulation())
770 TE->Modified(Standard_True);
776 //=======================================================================
777 //function : RemoveUnusedPCurves
779 //=======================================================================
781 void BRepTools::RemoveUnusedPCurves(const TopoDS_Shape& S)
783 TColStd_MapOfTransient UsedSurfaces;
785 TopExp_Explorer Explo(S, TopAbs_FACE);
786 for (; Explo.More(); Explo.Next())
788 TopoDS_Face aFace = TopoDS::Face(Explo.Current());
789 TopLoc_Location aLoc;
790 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace, aLoc);
791 UsedSurfaces.Add(aSurf);
794 TopTools_IndexedMapOfShape Emap;
795 TopExp::MapShapes(S, TopAbs_EDGE, Emap);
798 for (i = 1; i <= Emap.Extent(); i++)
800 const Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*) &Emap(i).TShape());
801 BRep_ListOfCurveRepresentation& lcr = TE -> ChangeCurves();
802 BRep_ListIteratorOfListOfCurveRepresentation itrep(lcr );
805 Standard_Boolean ToRemove = Standard_False;
807 Handle(BRep_CurveRepresentation) CurveRep = itrep.Value();
808 if (CurveRep->IsCurveOnSurface())
810 Handle(Geom_Surface) aSurface = CurveRep->Surface();
811 if (!UsedSurfaces.Contains(aSurface))
812 ToRemove = Standard_True;
814 else if (CurveRep->IsRegularity())
816 Handle(Geom_Surface) Surf1 = CurveRep->Surface();
817 Handle(Geom_Surface) Surf2 = CurveRep->Surface2();
818 ToRemove = (!UsedSurfaces.Contains(Surf1) || !UsedSurfaces.Contains(Surf2));
829 //=======================================================================
830 //function : Triangulation
832 //=======================================================================
834 Standard_Boolean BRepTools::Triangulation(const TopoDS_Shape& S,
835 const Standard_Real deflec)
837 TopExp_Explorer exf, exe;
839 Handle(Poly_Triangulation) T;
840 Handle(Poly_PolygonOnTriangulation) Poly;
842 for (exf.Init(S, TopAbs_FACE); exf.More(); exf.Next()) {
843 const TopoDS_Face& F = TopoDS::Face(exf.Current());
844 T = BRep_Tool::Triangulation(F, l);
845 if (T.IsNull() || (T->Deflection() > deflec))
846 return Standard_False;
847 for (exe.Init(F, TopAbs_EDGE); exe.More(); exe.Next()) {
848 const TopoDS_Edge& E = TopoDS::Edge(exe.Current());
849 Poly = BRep_Tool::PolygonOnTriangulation(E, T, l);
850 if (Poly.IsNull()) return Standard_False;
853 return Standard_True;
857 //=======================================================================
858 //function : IsReallyClosed
860 //=======================================================================
862 Standard_Boolean BRepTools::IsReallyClosed(const TopoDS_Edge& E,
863 const TopoDS_Face& F)
865 if (!BRep_Tool::IsClosed(E,F)) {
866 return Standard_False;
868 Standard_Integer nbocc = 0;
870 for (exp.Init(F,TopAbs_EDGE);exp.More();exp.Next()) {
871 if (exp.Current().IsSame(E)) {