f600387eb3ed0a8076ad16c02089091437a3f47f
[occt.git] / src / BRepBndLib / BRepBndLib.cxx
1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and / or modify it
7 // under the terms of the GNU Lesser General Public version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 #include <BRepBndLib.ixx>
16 #include <TopExp_Explorer.hxx>
17 #include <BRepAdaptor_Surface.hxx>
18 #include <BRepAdaptor_Curve.hxx>
19 #include <BRep_Tool.hxx> 
20 #include <TopoDS.hxx>
21 #include <BndLib_Add3dCurve.hxx>
22 #include <BndLib_AddSurface.hxx>
23 #include <Geom_Surface.hxx>
24 #include <TopLoc_Location.hxx>
25 #include <Poly_Triangulation.hxx>
26 #include <Poly_PolygonOnTriangulation.hxx>
27 #include <Poly_Polygon3D.hxx>
28 #include <BRep_Polygon3D.hxx>
29 #include <TColStd_HArray1OfInteger.hxx>
30 #include <TColStd_Array1OfInteger.hxx>
31 #include <TColgp_Array1OfPnt.hxx>
32 #include <Geom_Curve.hxx>
33 #include <GeomAdaptor_Curve.hxx>
34 #include <BndLib_Add3dCurve.hxx>
35
36
37 //=======================================================================
38 //function : Add
39 //purpose  : Add a shape bounding to a box
40 //=======================================================================
41
42 void BRepBndLib::Add(const TopoDS_Shape& S, Bnd_Box& B, Standard_Boolean useTriangulation)
43 {
44   TopExp_Explorer ex;
45
46   // Add the faces
47   BRepAdaptor_Surface BS;
48   Handle(Geom_Surface) GS;
49   Handle(Poly_Triangulation) T;
50   TopLoc_Location l;
51   Standard_Integer i, nbNodes;
52   BRepAdaptor_Curve BC;
53
54   for (ex.Init(S,TopAbs_FACE); ex.More(); ex.Next()) {
55     const TopoDS_Face& F = TopoDS::Face(ex.Current());
56     T = BRep_Tool::Triangulation(F, l);
57     if (useTriangulation && !T.IsNull())
58     {
59       nbNodes = T->NbNodes();
60       const TColgp_Array1OfPnt& Nodes = T->Nodes();
61       for (i = 1; i <= nbNodes; i++) {
62         if (l.IsIdentity()) B.Add(Nodes(i));
63         else B.Add(Nodes(i).Transformed(l));
64       }
65       //       B.Enlarge(T->Deflection());
66       B.Enlarge(T->Deflection() + BRep_Tool::Tolerance(F));
67     } else
68     {
69       GS = BRep_Tool::Surface(F, l);
70       if (!GS.IsNull()) {
71         BS.Initialize(F, Standard_False);
72         if (BS.GetType() != GeomAbs_Plane) {
73           BS.Initialize(F);
74           BndLib_AddSurface::Add(BS, BRep_Tool::Tolerance(F), B);
75         }
76         else {
77           // on travaille directement sur les courbes 3d.
78           TopExp_Explorer ex2(F, TopAbs_EDGE);
79           if (!ex2.More()) {
80             BS.Initialize(F);
81             BndLib_AddSurface::Add(BS, BRep_Tool::Tolerance(F), B);
82           }
83           else {
84             for (;ex2.More();ex2.Next()) {
85               BC.Initialize(TopoDS::Edge(ex2.Current()));
86               BndLib_Add3dCurve::Add(BC, BRep_Tool::Tolerance(F), B);
87             }
88             B.Enlarge(BRep_Tool::Tolerance(F));
89           }
90         }
91       }
92     }
93   }
94
95   // Add the edges not in faces
96   Handle(TColStd_HArray1OfInteger) HIndices;
97   Handle(Poly_PolygonOnTriangulation) Poly;
98
99   for (ex.Init(S,TopAbs_EDGE,TopAbs_FACE); ex.More(); ex.Next())
100   {
101     const TopoDS_Edge& E = TopoDS::Edge(ex.Current());
102     Handle(Poly_Polygon3D) P3d = BRep_Tool::Polygon3D(E, l);
103     if (!P3d.IsNull())
104     {
105       const TColgp_Array1OfPnt& Nodes = P3d->Nodes();
106       nbNodes = P3d->NbNodes();
107       for (i = 1; i <= nbNodes; i++)
108       {
109         if (l.IsIdentity()) B.Add(Nodes(i));
110         else B.Add(Nodes(i).Transformed(l));
111       }
112       //       B.Enlarge(P3d->Deflection());
113       B.Enlarge(P3d->Deflection() + BRep_Tool::Tolerance(E));
114     }
115     else
116     {
117       BRep_Tool::PolygonOnTriangulation(E, Poly, T, l);
118       if (useTriangulation && !Poly.IsNull())
119       {
120         const TColStd_Array1OfInteger& Indices = Poly->Nodes();
121         const TColgp_Array1OfPnt& Nodes = T->Nodes();
122         nbNodes = Indices.Length();
123         for (i = 1; i <= nbNodes; i++)
124         {
125           if (l.IsIdentity()) B.Add(Nodes(Indices(i)));
126           else B.Add(Nodes(Indices(i)).Transformed(l));
127         }
128         //      B.Enlarge(T->Deflection());
129         B.Enlarge(Poly->Deflection() + BRep_Tool::Tolerance(E));
130       }
131       else {
132         if (BRep_Tool::IsGeometric(E))
133         {
134           BC.Initialize(E);
135           BndLib_Add3dCurve::Add(BC, BRep_Tool::Tolerance(E), B);
136         }
137       }
138     }
139   }
140
141   // Add the vertices not in edges
142
143   for (ex.Init(S,TopAbs_VERTEX,TopAbs_EDGE); ex.More(); ex.Next()) {
144     B.Add(BRep_Tool::Pnt(TopoDS::Vertex(ex.Current())));
145     B.Enlarge(BRep_Tool::Tolerance(TopoDS::Vertex(ex.Current())));
146   }
147 }
148
149
150
151 //=======================================================================
152 //function : AddClose
153 //purpose  : Add a precise shape bounding to a box
154 //=======================================================================
155
156 void BRepBndLib::AddClose(const TopoDS_Shape& S, Bnd_Box& B)
157 {
158   TopExp_Explorer ex;
159
160   // No faces
161
162   // Add the edges
163
164   BRepAdaptor_Curve BC;
165
166   for (ex.Init(S,TopAbs_EDGE); ex.More(); ex.Next()) {
167     BC.Initialize(TopoDS::Edge(ex.Current()));
168     BndLib_Add3dCurve::Add(BC,0.,B);
169   }
170
171   // Add the vertices not in edges
172
173   for (ex.Init(S,TopAbs_VERTEX,TopAbs_EDGE); ex.More(); ex.Next()) {
174     B.Add(BRep_Tool::Pnt(TopoDS::Vertex(ex.Current())));
175   }
176 }
177