0023959: Getting rid of generic classes in Visualization: generic classes were remove...
[occt.git] / src / Prs3d / Prs3d_ShapeTool.cxx
1 // Created on: 1995-08-07
2 // Created by: Modelistation
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
5 //
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
10 //
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 //
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
20
21
22 #define OCC215           //SAV: 01/04/02 vertex exploring is done for all types of shape.
23 #define OCC598           //SAV: 22/10/02 searching for internal vertices. 
24
25 #include <Prs3d_ShapeTool.ixx>
26
27 #include <BRepTools.hxx>
28 #include <TopoDS.hxx>
29 #include <TopExp.hxx>
30 #include <TopTools_ListOfShape.hxx>
31 #include <TopTools_MapOfShape.hxx>
32 #include <TopTools_ListIteratorOfListOfShape.hxx>
33
34 #include <BRepBndLib.hxx>
35 #include <BRepAdaptor_Surface.hxx>
36 #include <BRepAdaptor_Curve.hxx>
37 #include <Bnd_Box.hxx>
38 #include <BRep_Tool.hxx>
39 #include <Geom_Surface.hxx>
40 #include <Geom_RectangularTrimmedSurface.hxx>
41 #include <Geom_Plane.hxx>
42
43
44 //=======================================================================
45 //function : Prs3d_ShapeTool
46 //purpose  : 
47 //=======================================================================
48
49 Prs3d_ShapeTool::Prs3d_ShapeTool(const TopoDS_Shape& TheShape):
50                                  myShape (TheShape) 
51 {
52   myEdgeMap.Clear();
53   myVertexMap.Clear();
54   TopExp::MapShapesAndAncestors(TheShape,TopAbs_EDGE,TopAbs_FACE,myEdgeMap);
55
56 #ifndef OCC215
57   // find vertices not under ancestors.
58   TopAbs_ShapeEnum E = TheShape.ShapeType();
59
60
61   // this check were done to reduce the number of selectable objects
62   // in a local context. By now, there's no noticeable performance improvement.
63   if (E != TopAbs_SOLID && E != TopAbs_SHELL)
64 #endif
65     {
66       TopExp_Explorer ex(TheShape,TopAbs_VERTEX, TopAbs_EDGE);
67       while (ex.More()) {
68         const TopoDS_Shape& aV=ex.Current();
69         myVertexMap.Add(aV);
70         ex.Next();
71       }
72     }
73 #ifdef OCC598
74   TopExp_Explorer edges( TheShape, TopAbs_EDGE );
75   while( edges.More() ) {
76     //xf
77     const TopoDS_Shape& aE= edges.Current();
78     TopoDS_Iterator aIt(aE, Standard_False, Standard_True);
79     while( aIt.More() ) {
80       const TopoDS_Shape& aV=aIt.Value();
81       if (aV.Orientation()==TopAbs_INTERNAL) {
82         myVertexMap.Add(aV);
83       }
84       aIt.Next();
85     }
86     /*
87     TopExp_Explorer vertices( edges.Current(), TopAbs_VERTEX );
88     while( vertices.More() ) {
89       TopoDS_Vertex current = TopoDS::Vertex( vertices.Current() );
90       if ( current.Orientation() == TopAbs_INTERNAL )
91         myVertexMap.Add( current );
92       vertices.Next();
93     }
94     */
95     //xt
96     edges.Next();
97   }
98 #endif
99 }
100
101 //=======================================================================
102 //function : InitFace
103 //purpose  : 
104 //=======================================================================
105
106 void Prs3d_ShapeTool::InitFace() 
107 {
108   myFaceExplorer.Init(myShape,TopAbs_FACE);
109 }
110
111 //=======================================================================
112 //function : MoreFace
113 //purpose  : 
114 //=======================================================================
115
116 Standard_Boolean Prs3d_ShapeTool::MoreFace() const 
117 {
118   return myFaceExplorer.More();
119 }
120
121 //=======================================================================
122 //function : NextFace
123 //purpose  : 
124 //=======================================================================
125
126 void Prs3d_ShapeTool::NextFace() 
127 {
128   myFaceExplorer.Next();
129 }
130
131 //=======================================================================
132 //function : GetFace
133 //purpose  : 
134 //=======================================================================
135
136 const TopoDS_Face& Prs3d_ShapeTool::GetFace () const 
137 {
138   return TopoDS::Face(myFaceExplorer.Current());
139 }
140
141
142 //=======================================================================
143 //function : FaceBound
144 //purpose  : 
145 //=======================================================================
146
147 Bnd_Box Prs3d_ShapeTool::FaceBound() const 
148 {
149   const TopoDS_Face& F = TopoDS::Face(myFaceExplorer.Current());
150   Bnd_Box B;
151   BRepBndLib::Add(F, B);
152   return B;
153 }
154
155 //=======================================================================
156 //function : IsPlanarFace
157 //purpose  : 
158 //=======================================================================
159
160 Standard_Boolean Prs3d_ShapeTool::IsPlanarFace() const 
161 {
162   TopLoc_Location l;
163   const TopoDS_Face& F = TopoDS::Face(myFaceExplorer.Current());
164   const Handle(Geom_Surface)& S = BRep_Tool::Surface(F, l);
165   Handle(Standard_Type) TheType = S->DynamicType();
166
167   if (TheType == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
168     Handle(Geom_RectangularTrimmedSurface) 
169         RTS = *((Handle(Geom_RectangularTrimmedSurface)*)&S);
170     TheType = RTS->BasisSurface()->DynamicType();
171   }
172   return (TheType == STANDARD_TYPE(Geom_Plane));
173 }
174
175
176
177 //=======================================================================
178 //function : InitCurve
179 //purpose  : 
180 //=======================================================================
181
182 void Prs3d_ShapeTool::InitCurve() 
183 {
184   myEdge = 1;
185 }
186
187 //=======================================================================
188 //function : MoreCurve
189 //purpose  : 
190 //=======================================================================
191
192 Standard_Boolean Prs3d_ShapeTool::MoreCurve() const 
193 {
194   return myEdge <= myEdgeMap.Extent();
195 }
196
197 //=======================================================================
198 //function : NextCurve
199 //purpose  : 
200 //=======================================================================
201
202 void Prs3d_ShapeTool::NextCurve() 
203 {
204   myEdge++;
205 }
206
207 //=======================================================================
208 //function : GetCurve
209 //purpose  : 
210 //=======================================================================
211
212 const TopoDS_Edge& Prs3d_ShapeTool::GetCurve () const 
213 {
214   return  TopoDS::Edge(myEdgeMap.FindKey(myEdge));
215 }
216
217 //=======================================================================
218 //function : CurveBound
219 //purpose  : 
220 //=======================================================================
221
222 Bnd_Box Prs3d_ShapeTool::CurveBound () const 
223 {
224   const TopoDS_Edge& E = TopoDS::Edge(myEdgeMap.FindKey(myEdge));
225   Bnd_Box B;
226   BRepBndLib::Add(E, B);
227   return B;
228 }
229
230 //=======================================================================
231 //function : Neighbours
232 //purpose  : 
233 //=======================================================================
234
235 Standard_Integer Prs3d_ShapeTool::Neighbours () const 
236 {
237   const TopTools_ListOfShape& L = myEdgeMap.FindFromIndex(myEdge);
238   return L.Extent();
239 }
240
241 //=======================================================================
242 //function : FacesOfEdge
243 //purpose  : 
244 //=======================================================================
245
246 Handle(TopTools_HSequenceOfShape) Prs3d_ShapeTool::FacesOfEdge () const 
247 {
248   Handle(TopTools_HSequenceOfShape) H = new TopTools_HSequenceOfShape;
249
250   const TopTools_ListOfShape& L = myEdgeMap.FindFromIndex(myEdge);
251   TopTools_ListIteratorOfListOfShape LI;
252
253   for (LI.Initialize(L); LI.More(); LI.Next()) H->Append(LI.Value());
254   return H;
255 }
256
257
258 //=======================================================================
259 //function : InitVertex
260 //purpose  : 
261 //=======================================================================
262
263 void Prs3d_ShapeTool::InitVertex() 
264 {
265   myVertex = 1;
266 }
267
268 //=======================================================================
269 //function : MoreVertex
270 //purpose  : 
271 //=======================================================================
272
273 Standard_Boolean Prs3d_ShapeTool::MoreVertex() const 
274 {
275   return myVertex <= myVertexMap.Extent();
276 }
277
278 //=======================================================================
279 //function : NextVertex
280 //purpose  : 
281 //=======================================================================
282
283 void Prs3d_ShapeTool::NextVertex() 
284 {
285   myVertex++;
286 }
287
288 //=======================================================================
289 //function : GetVertex
290 //purpose  : 
291 //=======================================================================
292
293 const TopoDS_Vertex& Prs3d_ShapeTool::GetVertex () const 
294 {
295   return  TopoDS::Vertex(myVertexMap.FindKey(myVertex));
296 }
297
298
299 //=======================================================================
300 //function : HasSurface
301 //purpose  : 
302 //=======================================================================
303
304 Standard_Boolean Prs3d_ShapeTool::HasSurface() const
305 {
306   TopLoc_Location l;
307   const Handle(Geom_Surface)& S = BRep_Tool::Surface(GetFace(), l);
308   return (!S.IsNull());
309 }
310
311
312
313 //=======================================================================
314 //function : CurrentTriangulation
315 //purpose  : 
316 //=======================================================================
317
318 Handle(Poly_Triangulation) Prs3d_ShapeTool::CurrentTriangulation(TopLoc_Location& l) const
319 {
320   return BRep_Tool::Triangulation(GetFace(), l);
321 }
322
323
324 //=======================================================================
325 //function : HasCurve
326 //purpose  : 
327 //=======================================================================
328
329 Standard_Boolean Prs3d_ShapeTool::HasCurve() const
330 {
331   return (BRep_Tool::IsGeometric(GetCurve()));
332 }
333
334
335
336
337 //=======================================================================
338 //function : PolygonOnTriangulation
339 //purpose  : 
340 //=======================================================================
341
342 void Prs3d_ShapeTool::PolygonOnTriangulation
343 (Handle(Poly_PolygonOnTriangulation)& Indices,
344  Handle(Poly_Triangulation)&          T,
345  TopLoc_Location&                     l) const
346 {
347   BRep_Tool::PolygonOnTriangulation(GetCurve(), Indices, T, l);
348 }
349
350
351
352 //=======================================================================
353 //function : Polygon3D
354 //purpose  : 
355 //=======================================================================
356
357 Handle(Poly_Polygon3D) Prs3d_ShapeTool::Polygon3D(TopLoc_Location& l) const
358 {
359   return BRep_Tool::Polygon3D(GetCurve(), l);
360 }