0028726: Quantity_NameOfColor should be replaced by Quantity_Color in function input...
[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 
146 {
147   TopLoc_Location l;
148   const TopoDS_Face& F = TopoDS::Face(myFaceExplorer.Current());
149   const Handle(Geom_Surface)& S = BRep_Tool::Surface(F, l);
150   if (S.IsNull())
151   {
152     return Standard_False;
153   }
154
155   Handle(Standard_Type) TheType = S->DynamicType();
156
157   if (TheType == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
158     Handle(Geom_RectangularTrimmedSurface) 
159         RTS = Handle(Geom_RectangularTrimmedSurface)::DownCast (S);
160     TheType = RTS->BasisSurface()->DynamicType();
161   }
162   return (TheType == STANDARD_TYPE(Geom_Plane));
163 }
164
165
166
167 //=======================================================================
168 //function : InitCurve
169 //purpose  : 
170 //=======================================================================
171
172 void Prs3d_ShapeTool::InitCurve() 
173 {
174   myEdge = 1;
175 }
176
177 //=======================================================================
178 //function : MoreCurve
179 //purpose  : 
180 //=======================================================================
181
182 Standard_Boolean Prs3d_ShapeTool::MoreCurve() const 
183 {
184   return myEdge <= myEdgeMap.Extent();
185 }
186
187 //=======================================================================
188 //function : NextCurve
189 //purpose  : 
190 //=======================================================================
191
192 void Prs3d_ShapeTool::NextCurve() 
193 {
194   myEdge++;
195 }
196
197 //=======================================================================
198 //function : GetCurve
199 //purpose  : 
200 //=======================================================================
201
202 const TopoDS_Edge& Prs3d_ShapeTool::GetCurve () const 
203 {
204   return  TopoDS::Edge(myEdgeMap.FindKey(myEdge));
205 }
206
207 //=======================================================================
208 //function : CurveBound
209 //purpose  : 
210 //=======================================================================
211
212 Bnd_Box Prs3d_ShapeTool::CurveBound () const 
213 {
214   const TopoDS_Edge& E = TopoDS::Edge(myEdgeMap.FindKey(myEdge));
215   Bnd_Box B;
216   BRepBndLib::Add(E, B);
217   return B;
218 }
219
220 //=======================================================================
221 //function : Neighbours
222 //purpose  : 
223 //=======================================================================
224
225 Standard_Integer Prs3d_ShapeTool::Neighbours () const 
226 {
227   const TopTools_ListOfShape& L = myEdgeMap.FindFromIndex(myEdge);
228   return L.Extent();
229 }
230
231 //=======================================================================
232 //function : FacesOfEdge
233 //purpose  : 
234 //=======================================================================
235
236 Handle(TopTools_HSequenceOfShape) Prs3d_ShapeTool::FacesOfEdge () const 
237 {
238   Handle(TopTools_HSequenceOfShape) H = new TopTools_HSequenceOfShape;
239
240   const TopTools_ListOfShape& L = myEdgeMap.FindFromIndex(myEdge);
241   TopTools_ListIteratorOfListOfShape LI;
242
243   for (LI.Initialize(L); LI.More(); LI.Next()) H->Append(LI.Value());
244   return H;
245 }
246
247
248 //=======================================================================
249 //function : InitVertex
250 //purpose  : 
251 //=======================================================================
252
253 void Prs3d_ShapeTool::InitVertex() 
254 {
255   myVertex = 1;
256 }
257
258 //=======================================================================
259 //function : MoreVertex
260 //purpose  : 
261 //=======================================================================
262
263 Standard_Boolean Prs3d_ShapeTool::MoreVertex() const 
264 {
265   return myVertex <= myVertexMap.Extent();
266 }
267
268 //=======================================================================
269 //function : NextVertex
270 //purpose  : 
271 //=======================================================================
272
273 void Prs3d_ShapeTool::NextVertex() 
274 {
275   myVertex++;
276 }
277
278 //=======================================================================
279 //function : GetVertex
280 //purpose  : 
281 //=======================================================================
282
283 const TopoDS_Vertex& Prs3d_ShapeTool::GetVertex () const 
284 {
285   return  TopoDS::Vertex(myVertexMap.FindKey(myVertex));
286 }
287
288
289 //=======================================================================
290 //function : HasSurface
291 //purpose  : 
292 //=======================================================================
293
294 Standard_Boolean Prs3d_ShapeTool::HasSurface() const
295 {
296   TopLoc_Location l;
297   const Handle(Geom_Surface)& S = BRep_Tool::Surface(GetFace(), l);
298   return (!S.IsNull());
299 }
300
301
302
303 //=======================================================================
304 //function : CurrentTriangulation
305 //purpose  : 
306 //=======================================================================
307
308 Handle(Poly_Triangulation) Prs3d_ShapeTool::CurrentTriangulation(TopLoc_Location& l) const
309 {
310   return BRep_Tool::Triangulation(GetFace(), l);
311 }
312
313
314 //=======================================================================
315 //function : HasCurve
316 //purpose  : 
317 //=======================================================================
318
319 Standard_Boolean Prs3d_ShapeTool::HasCurve() const
320 {
321   return (BRep_Tool::IsGeometric(GetCurve()));
322 }
323
324
325
326
327 //=======================================================================
328 //function : PolygonOnTriangulation
329 //purpose  : 
330 //=======================================================================
331
332 void Prs3d_ShapeTool::PolygonOnTriangulation
333 (Handle(Poly_PolygonOnTriangulation)& Indices,
334  Handle(Poly_Triangulation)&          T,
335  TopLoc_Location&                     l) const
336 {
337   BRep_Tool::PolygonOnTriangulation(GetCurve(), Indices, T, l);
338 }
339
340
341
342 //=======================================================================
343 //function : Polygon3D
344 //purpose  : 
345 //=======================================================================
346
347 Handle(Poly_Polygon3D) Prs3d_ShapeTool::Polygon3D(TopLoc_Location& l) const
348 {
349   return BRep_Tool::Polygon3D(GetCurve(), l);
350 }