0022048: Visualization, AIS_InteractiveContext - single object selection should alway...
[occt.git] / src / TopOpeBRepTool / TopOpeBRepTool_HBoxTool.cxx
1 // Created on: 1993-07-08
2 // Created by: Jean Yves LEBEY
3 // Copyright (c) 1993-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License 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.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17
18 #include <Bnd_Box.hxx>
19 #include <BRep_Tool.hxx>
20 #include <BRepBndLib.hxx>
21 #include <gp_Pnt.hxx>
22 #include <Precision.hxx>
23 #include <Standard_ProgramError.hxx>
24 #include <Standard_Type.hxx>
25 #include <TopAbs.hxx>
26 #include <TopExp_Explorer.hxx>
27 #include <TopoDS.hxx>
28 #include <TopoDS_Shape.hxx>
29 #include <TopOpeBRepTool_box.hxx>
30 #include <TopOpeBRepTool_define.hxx>
31 #include <TopOpeBRepTool_HBoxTool.hxx>
32
33 IMPLEMENT_STANDARD_RTTIEXT(TopOpeBRepTool_HBoxTool,Standard_Transient)
34
35 #ifdef OCCT_DEBUG
36 #define TBOX TopOpeBRepTool_GettraceBOX()
37 #endif
38
39 //=======================================================================
40 //function : TopOpeBRepTool_HBoxTool
41 //purpose  : 
42 //=======================================================================
43
44 TopOpeBRepTool_HBoxTool::TopOpeBRepTool_HBoxTool()
45 {
46 }
47
48 //=======================================================================
49 //function : Clear
50 //purpose  : 
51 //=======================================================================
52 void TopOpeBRepTool_HBoxTool::Clear()
53 {
54   myIMS.Clear();
55 }
56
57 //=======================================================================
58 //function : ComputeBoxes
59 //purpose  : 
60 //=======================================================================
61 void TopOpeBRepTool_HBoxTool::AddBoxes(const TopoDS_Shape& S,const TopAbs_ShapeEnum TS,const TopAbs_ShapeEnum TA)
62 {
63   TopExp_Explorer ex;
64   for (ex.Init(S,TS,TA);ex.More();ex.Next()) {
65 //  for (TopExp_Explorer ex(S,TS,TA);ex.More();ex.Next()) {
66     const TopoDS_Shape& ss = ex.Current();
67     AddBox(ss);
68   }
69 }
70
71 //=======================================================================
72 //function : AddBox
73 //purpose  : 
74 //=======================================================================
75 void TopOpeBRepTool_HBoxTool::AddBox(const TopoDS_Shape& S)
76 {
77 #ifdef OCCT_DEBUG
78   TopAbs_ShapeEnum t = 
79 #endif
80     S.ShapeType();
81   Standard_Boolean hasb = HasBox(S);
82   if (hasb) return;
83
84   Bnd_Box B;
85   ComputeBox(S,B);
86   myIMS.Add(S,B);
87 #ifdef OCCT_DEBUG
88   if (TBOX) {
89     DumpB(Box(S));cout<<"; # HBT::AddBox "<<Index(S)<<" : ";TopAbs::Print(t,cout);cout<<"\n";cout<<endl;
90   }
91 #endif
92 }
93
94 //=======================================================================
95 //function : ComputeBox
96 //purpose  : 
97 //=======================================================================
98 void TopOpeBRepTool_HBoxTool::ComputeBox(const TopoDS_Shape& S,Bnd_Box& B)
99 {
100   TopAbs_ShapeEnum t = S.ShapeType();
101   if      ( t == TopAbs_FACE ) BRepBndLib::Add(S,B);
102   else if ( t == TopAbs_EDGE ) BRepBndLib::Add(S,B);
103   //modified by NIZHNY-MZV  Tue Sep 21 14:04:33 1999
104   else if ( t == TopAbs_SOLID) BRepBndLib::Add(S,B);
105   else if ( t == TopAbs_COMPOUND) BRepBndLib::Add(S,B);
106   //modified by NIZHNY-MZV  Wed Apr  5 10:05:53 2000
107   else if ( t == TopAbs_VERTEX) BRepBndLib::Add(S,B);
108   else {
109     throw Standard_ProgramError("HBT::ComputeBox : invalid type");
110   }
111 }
112
113 //=======================================================================
114 //function : ComputeBoxOnVertices
115 //purpose  : 
116 //=======================================================================
117 void TopOpeBRepTool_HBoxTool::ComputeBoxOnVertices(const TopoDS_Shape& S,Bnd_Box& B)
118 {
119   TopExp_Explorer ex(S,TopAbs_VERTEX);
120   if (!ex.More()) {
121     B.Update(-1.e5,-1.e5,-1.e5,1.e5,1.e5,1.e5);
122     return;
123   }
124   Standard_Real tol = Precision::Confusion();
125   for (; ex.More(); ex.Next() ) {
126     Standard_Real x,y,z;
127     BRep_Tool::Pnt(TopoDS::Vertex(ex.Current())).Coord(x,y,z);
128     B.Update(x,y,z);
129     tol = Max(tol,BRep_Tool::Tolerance(TopoDS::Vertex(ex.Current())));
130   }
131   B.Enlarge(tol);
132 }
133
134 //=======================================================================
135 //function : Box
136 //purpose  : 
137 //=======================================================================
138 const Bnd_Box& TopOpeBRepTool_HBoxTool::Box(const TopoDS_Shape& S)
139 {
140   Standard_Boolean hb = HasBox(S);
141   if (!hb) {
142     throw Standard_ProgramError("HBT::Box1");
143   }
144
145   const Bnd_Box& B = myIMS.FindFromKey(S);
146   return B;
147 }
148
149 //=======================================================================
150 //function : Box
151 //purpose  : 
152 //=======================================================================
153 const Bnd_Box& TopOpeBRepTool_HBoxTool::Box(const Standard_Integer I) const
154 {
155   Standard_Integer iu = Extent();
156   Standard_Integer hb = (I >= 1 && I <= iu);
157   if (!hb) {
158     throw Standard_ProgramError("HBT::Box2");
159   }
160   const Bnd_Box& B = myIMS.FindFromIndex(I);
161   return B;
162 }
163
164 //=======================================================================
165 //function : HasBox
166 //purpose  : 
167 //=======================================================================
168 Standard_Boolean TopOpeBRepTool_HBoxTool::HasBox(const TopoDS_Shape& S) const 
169 {
170   Standard_Boolean hb = myIMS.Contains(S);
171   return hb;
172 }
173
174 //=======================================================================
175 //function : Shape
176 //purpose  : 
177 //=======================================================================
178 const TopoDS_Shape& TopOpeBRepTool_HBoxTool::Shape(const Standard_Integer I) const
179 {
180   Standard_Integer iu = Extent();
181   Standard_Integer hs = (I >= 1 && I <= iu);
182   if (!hs) {
183     throw Standard_ProgramError("HBT::Box4");
184   }
185   const TopoDS_Shape& S = myIMS.FindKey(I);
186   return S;
187 }
188
189 //=======================================================================
190 //function : Index
191 //purpose  : 
192 //=======================================================================
193 Standard_Integer TopOpeBRepTool_HBoxTool::Index(const TopoDS_Shape& S) const 
194 {
195   Standard_Integer i = myIMS.FindIndex(S);
196   return i;
197 }
198
199 //=======================================================================
200 //function : Extent
201 //purpose  : 
202 //=======================================================================
203 Standard_Integer TopOpeBRepTool_HBoxTool::Extent() const 
204 {
205   Standard_Integer n = myIMS.Extent();
206   return n;
207 }
208
209 //=======================================================================
210 //function : ChangeIMS
211 //purpose  : 
212 //=======================================================================
213 TopOpeBRepTool_IndexedDataMapOfShapeBox& TopOpeBRepTool_HBoxTool::ChangeIMS()
214 {
215   return myIMS;
216 }
217
218 //=======================================================================
219 //function : IMS
220 //purpose  : 
221 //=======================================================================
222 const TopOpeBRepTool_IndexedDataMapOfShapeBox& TopOpeBRepTool_HBoxTool::IMS() const
223 {
224   return myIMS;
225 }
226
227 //=======================================================================
228 //function : DumpB
229 //purpose  : 
230 //=======================================================================
231 void TopOpeBRepTool_HBoxTool::DumpB 
232 #ifdef OCCT_DEBUG
233 (const Bnd_Box& B)
234 {
235   if      (B.IsVoid()) cout<<"# IsVoid";
236   else if (B.IsWhole()) cout<<"# IsWhole";
237   else {
238     Standard_Real x,y,z,X,Y,Z; 
239     B.Get(x,y,z,X,Y,Z);
240     cout<<"bounding "<<x<<" "<<y<<" "<<z<<" "<<X<<" "<<Y<<" "<<Z;
241     cout.flush();
242   }
243 }
244 #else
245 (const Bnd_Box& )
246 {
247 }
248 #endif
249