732f3205a71ab72f133338a0b485093686b5399d
[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-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 <BRepAdaptor_Curve.hxx>
21 #include <BRepAdaptor_Surface.hxx>
22 #include <BRepBndLib.hxx>
23 #include <BRepTools.hxx>
24 #include <Geom_Plane.hxx>
25 #include <Geom_RectangularTrimmedSurface.hxx>
26 #include <Geom_Surface.hxx>
27 #include <Poly_Polygon3D.hxx>
28 #include <Poly_PolygonOnTriangulation.hxx>
29 #include <Poly_Triangulation.hxx>
30 #include <Prs3d_ShapeTool.hxx>
31 #include <TopExp.hxx>
32 #include <TopLoc_Location.hxx>
33 #include <TopoDS.hxx>
34 #include <TopoDS_Edge.hxx>
35 #include <TopoDS_Face.hxx>
36 #include <TopoDS_Shape.hxx>
37 #include <TopoDS_Vertex.hxx>
38 #include <TopTools_ListIteratorOfListOfShape.hxx>
39 #include <TopTools_ListOfShape.hxx>
40 #include <TopTools_MapOfShape.hxx>
41
42 //=======================================================================
43 //function : Prs3d_ShapeTool
44 //purpose  : 
45 //=======================================================================
46 Prs3d_ShapeTool::Prs3d_ShapeTool (const TopoDS_Shape& theShape,
47                                   const Standard_Boolean theAllVertices)
48 : myShape (theShape)
49 {
50   myEdgeMap.Clear();
51   myVertexMap.Clear();
52   TopExp::MapShapesAndAncestors (theShape,TopAbs_EDGE,TopAbs_FACE, myEdgeMap);
53
54   TopExp_Explorer anExpl;
55   if (theAllVertices)
56   {
57     for (anExpl.Init (theShape, TopAbs_VERTEX); anExpl.More(); anExpl.Next())
58     {
59       myVertexMap.Add (anExpl.Current());
60     }
61   }
62   else
63   {
64     // Extracting isolated vertices
65     for (anExpl.Init (theShape, TopAbs_VERTEX, TopAbs_EDGE); anExpl.More(); anExpl.Next())
66     {
67       myVertexMap.Add (anExpl.Current());
68     }
69
70     // Extracting internal vertices
71     for (anExpl.Init (theShape, TopAbs_EDGE); anExpl.More(); anExpl.Next())
72     {
73       TopoDS_Iterator aIt (anExpl.Current(), Standard_False, Standard_True);
74       for (; aIt.More(); aIt.Next())
75       {
76         const TopoDS_Shape& aV = aIt.Value();
77         if (aV.Orientation() == TopAbs_INTERNAL)
78         {
79           myVertexMap.Add (aV);
80         }
81       }
82     }
83   }
84 }
85
86 //=======================================================================
87 //function : InitFace
88 //purpose  : 
89 //=======================================================================
90
91 void Prs3d_ShapeTool::InitFace() 
92 {
93   myFaceExplorer.Init(myShape,TopAbs_FACE);
94 }
95
96 //=======================================================================
97 //function : MoreFace
98 //purpose  : 
99 //=======================================================================
100
101 Standard_Boolean Prs3d_ShapeTool::MoreFace() const 
102 {
103   return myFaceExplorer.More();
104 }
105
106 //=======================================================================
107 //function : NextFace
108 //purpose  : 
109 //=======================================================================
110
111 void Prs3d_ShapeTool::NextFace() 
112 {
113   myFaceExplorer.Next();
114 }
115
116 //=======================================================================
117 //function : GetFace
118 //purpose  : 
119 //=======================================================================
120
121 const TopoDS_Face& Prs3d_ShapeTool::GetFace () const 
122 {
123   return TopoDS::Face(myFaceExplorer.Current());
124 }
125
126
127 //=======================================================================
128 //function : FaceBound
129 //purpose  : 
130 //=======================================================================
131
132 Bnd_Box Prs3d_ShapeTool::FaceBound() const 
133 {
134   const TopoDS_Face& F = TopoDS::Face(myFaceExplorer.Current());
135   Bnd_Box B;
136   BRepBndLib::Add(F, B);
137   return B;
138 }
139
140 //=======================================================================
141 //function : IsPlanarFace
142 //purpose  : 
143 //=======================================================================
144
145 Standard_Boolean Prs3d_ShapeTool::IsPlanarFace (const TopoDS_Face& theFace)
146 {
147   TopLoc_Location l;
148   const Handle(Geom_Surface)& S = BRep_Tool::Surface(theFace, l);
149   if (S.IsNull())
150   {
151     return Standard_False;
152   }
153
154   Handle(Standard_Type) TheType = S->DynamicType();
155
156   if (TheType == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
157     Handle(Geom_RectangularTrimmedSurface) 
158         RTS = Handle(Geom_RectangularTrimmedSurface)::DownCast (S);
159     TheType = RTS->BasisSurface()->DynamicType();
160   }
161   return (TheType == STANDARD_TYPE(Geom_Plane));
162 }
163
164
165
166 //=======================================================================
167 //function : InitCurve
168 //purpose  : 
169 //=======================================================================
170
171 void Prs3d_ShapeTool::InitCurve() 
172 {
173   myEdge = 1;
174 }
175
176 //=======================================================================
177 //function : MoreCurve
178 //purpose  : 
179 //=======================================================================
180
181 Standard_Boolean Prs3d_ShapeTool::MoreCurve() const 
182 {
183   return myEdge <= myEdgeMap.Extent();
184 }
185
186 //=======================================================================
187 //function : NextCurve
188 //purpose  : 
189 //=======================================================================
190
191 void Prs3d_ShapeTool::NextCurve() 
192 {
193   myEdge++;
194 }
195
196 //=======================================================================
197 //function : GetCurve
198 //purpose  : 
199 //=======================================================================
200
201 const TopoDS_Edge& Prs3d_ShapeTool::GetCurve () const 
202 {
203   return  TopoDS::Edge(myEdgeMap.FindKey(myEdge));
204 }
205
206 //=======================================================================
207 //function : CurveBound
208 //purpose  : 
209 //=======================================================================
210
211 Bnd_Box Prs3d_ShapeTool::CurveBound () const 
212 {
213   const TopoDS_Edge& E = TopoDS::Edge(myEdgeMap.FindKey(myEdge));
214   Bnd_Box B;
215   BRepBndLib::Add(E, B);
216   return B;
217 }
218
219 //=======================================================================
220 //function : Neighbours
221 //purpose  : 
222 //=======================================================================
223
224 Standard_Integer Prs3d_ShapeTool::Neighbours () const 
225 {
226   const TopTools_ListOfShape& L = myEdgeMap.FindFromIndex(myEdge);
227   return L.Extent();
228 }
229
230 //=======================================================================
231 //function : FacesOfEdge
232 //purpose  : 
233 //=======================================================================
234
235 Handle(TopTools_HSequenceOfShape) Prs3d_ShapeTool::FacesOfEdge () const 
236 {
237   Handle(TopTools_HSequenceOfShape) H = new TopTools_HSequenceOfShape;
238
239   const TopTools_ListOfShape& L = myEdgeMap.FindFromIndex(myEdge);
240   TopTools_ListIteratorOfListOfShape LI;
241
242   for (LI.Initialize(L); LI.More(); LI.Next()) H->Append(LI.Value());
243   return H;
244 }
245
246
247 //=======================================================================
248 //function : InitVertex
249 //purpose  : 
250 //=======================================================================
251
252 void Prs3d_ShapeTool::InitVertex() 
253 {
254   myVertex = 1;
255 }
256
257 //=======================================================================
258 //function : MoreVertex
259 //purpose  : 
260 //=======================================================================
261
262 Standard_Boolean Prs3d_ShapeTool::MoreVertex() const 
263 {
264   return myVertex <= myVertexMap.Extent();
265 }
266
267 //=======================================================================
268 //function : NextVertex
269 //purpose  : 
270 //=======================================================================
271
272 void Prs3d_ShapeTool::NextVertex() 
273 {
274   myVertex++;
275 }
276
277 //=======================================================================
278 //function : GetVertex
279 //purpose  : 
280 //=======================================================================
281
282 const TopoDS_Vertex& Prs3d_ShapeTool::GetVertex () const 
283 {
284   return  TopoDS::Vertex(myVertexMap.FindKey(myVertex));
285 }
286
287
288 //=======================================================================
289 //function : HasSurface
290 //purpose  : 
291 //=======================================================================
292
293 Standard_Boolean Prs3d_ShapeTool::HasSurface() const
294 {
295   TopLoc_Location l;
296   const Handle(Geom_Surface)& S = BRep_Tool::Surface(GetFace(), l);
297   return (!S.IsNull());
298 }
299
300
301
302 //=======================================================================
303 //function : CurrentTriangulation
304 //purpose  : 
305 //=======================================================================
306
307 Handle(Poly_Triangulation) Prs3d_ShapeTool::CurrentTriangulation(TopLoc_Location& l) const
308 {
309   return BRep_Tool::Triangulation(GetFace(), l);
310 }
311
312
313 //=======================================================================
314 //function : HasCurve
315 //purpose  : 
316 //=======================================================================
317
318 Standard_Boolean Prs3d_ShapeTool::HasCurve() const
319 {
320   return (BRep_Tool::IsGeometric(GetCurve()));
321 }
322
323
324
325
326 //=======================================================================
327 //function : PolygonOnTriangulation
328 //purpose  : 
329 //=======================================================================
330
331 void Prs3d_ShapeTool::PolygonOnTriangulation
332 (Handle(Poly_PolygonOnTriangulation)& Indices,
333  Handle(Poly_Triangulation)&          T,
334  TopLoc_Location&                     l) const
335 {
336   BRep_Tool::PolygonOnTriangulation(GetCurve(), Indices, T, l);
337 }
338
339
340
341 //=======================================================================
342 //function : Polygon3D
343 //purpose  : 
344 //=======================================================================
345
346 Handle(Poly_Polygon3D) Prs3d_ShapeTool::Polygon3D(TopLoc_Location& l) const
347 {
348   return BRep_Tool::Polygon3D(GetCurve(), l);
349 }