1 // Created on: 1993-01-19
2 // Created by: Remi LEQUETTE
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 #define No_Standard_NoMoreObject
18 #define No_Standard_NoSuchObject
19 #define No_Standard_TypeMismatch
20
21
22 #include <TopExp.hxx>
23 #include <TopExp_Explorer.hxx>
24 #include <TopoDS.hxx>
25 #include <TopoDS_Edge.hxx>
26 #include <TopoDS_Iterator.hxx>
27 #include <TopoDS_Shape.hxx>
28 #include <TopoDS_Vertex.hxx>
29 #include <TopoDS_Wire.hxx>
30 #include <TopTools_ListOfShape.hxx>
31 #include <TopTools_MapIteratorOfMapOfShape.hxx>
32 #include <TopTools_MapOfShape.hxx>
33
34 //=======================================================================
35 //function : MapShapes
36 //purpose :
37 //=======================================================================
MapShapes(const TopoDS_Shape & S,const TopAbs_ShapeEnum T,TopTools_IndexedMapOfShape & M)38 void TopExp::MapShapes(const TopoDS_Shape& S,
39 const TopAbs_ShapeEnum T,
40 TopTools_IndexedMapOfShape& M)
41 {
42 TopExp_Explorer Ex(S,T);
43 while (Ex.More()) {
44 M.Add(Ex.Current());
45 Ex.Next();
46 }
47 }
48
49 //=======================================================================
50 //function : MapShapes
51 //purpose :
52 //=======================================================================
53
MapShapes(const TopoDS_Shape & S,TopTools_IndexedMapOfShape & M,const Standard_Boolean cumOri,const Standard_Boolean cumLoc)54 void TopExp::MapShapes(const TopoDS_Shape& S,
55 TopTools_IndexedMapOfShape& M,
56 const Standard_Boolean cumOri, const Standard_Boolean cumLoc)
57 {
58 M.Add(S);
59 TopoDS_Iterator It(S, cumOri, cumLoc);
60 while (It.More()) {
61 MapShapes(It.Value(),M);
62 It.Next();
63 }
64 }
65
66 //=======================================================================
67 //function : MapShapes
68 //purpose :
69 //=======================================================================
MapShapes(const TopoDS_Shape & S,TopTools_MapOfShape & M,const Standard_Boolean cumOri,const Standard_Boolean cumLoc)70 void TopExp::MapShapes(const TopoDS_Shape& S,
71 TopTools_MapOfShape& M,
72 const Standard_Boolean cumOri, const Standard_Boolean cumLoc)
73 {
74 M.Add(S);
75 TopoDS_Iterator It(S, cumOri, cumLoc);
76 for (; It.More(); It.Next())
77 MapShapes(It.Value(), M);
78 }
79
80 //=======================================================================
81 //function : MapShapesAndAncestors
82 //purpose :
83 //=======================================================================
84
MapShapesAndAncestors(const TopoDS_Shape & S,const TopAbs_ShapeEnum TS,const TopAbs_ShapeEnum TA,TopTools_IndexedDataMapOfShapeListOfShape & M)85 void TopExp::MapShapesAndAncestors
86 (const TopoDS_Shape& S,
87 const TopAbs_ShapeEnum TS,
88 const TopAbs_ShapeEnum TA,
89 TopTools_IndexedDataMapOfShapeListOfShape& M)
90 {
91 TopTools_ListOfShape empty;
92
93 // visit ancestors
94 TopExp_Explorer exa(S,TA);
95 while (exa.More()) {
96 // visit shapes
97 const TopoDS_Shape& anc = exa.Current();
98 TopExp_Explorer exs(anc,TS);
99 while (exs.More()) {
100 Standard_Integer index = M.FindIndex(exs.Current());
101 if (index == 0) index = M.Add(exs.Current(),empty);
102 M(index).Append(anc);
103 exs.Next();
104 }
105 exa.Next();
106 }
107
108 // visit shapes not under ancestors
109 TopExp_Explorer ex(S,TS,TA);
110 while (ex.More()) {
111 Standard_Integer index = M.FindIndex(ex.Current());
112 if (index == 0) index = M.Add(ex.Current(),empty);
113 ex.Next();
114 }
115 }
116
117 //=======================================================================
118 //function : MapShapesAndUniqueAncestors
119 //purpose :
120 //=======================================================================
121
MapShapesAndUniqueAncestors(const TopoDS_Shape & S,const TopAbs_ShapeEnum TS,const TopAbs_ShapeEnum TA,TopTools_IndexedDataMapOfShapeListOfShape & M,const Standard_Boolean useOrientation)122 void TopExp::MapShapesAndUniqueAncestors
123 (const TopoDS_Shape& S,
124 const TopAbs_ShapeEnum TS,
125 const TopAbs_ShapeEnum TA,
126 TopTools_IndexedDataMapOfShapeListOfShape& M,
127 const Standard_Boolean useOrientation)
128 {
129 TopTools_ListOfShape empty;
130
131 // visit ancestors
132 TopExp_Explorer exa(S,TA);
133 while (exa.More())
134 {
135 // visit shapes
136 const TopoDS_Shape& anc = exa.Current();
137 TopExp_Explorer exs(anc,TS);
138 while (exs.More())
139 {
140 Standard_Integer index = M.FindIndex(exs.Current());
141 if (index == 0)
142 index = M.Add(exs.Current(),empty);
143 TopTools_ListOfShape& aList = M(index);
144 // check if anc already exists in a list
145 TopTools_ListIteratorOfListOfShape it(aList);
146 for (; it.More(); it.Next())
147 if (useOrientation? anc.IsEqual(it.Value()) : anc.IsSame(it.Value()))
148 break;
149 if (!it.More())
150 aList.Append(anc);
151 exs.Next();
152 }
153 exa.Next();
154 }
155
156 // visit shapes not under ancestors
157 TopExp_Explorer ex(S,TS,TA);
158 while (ex.More())
159 {
160 Standard_Integer index = M.FindIndex(ex.Current());
161 if (index == 0)
162 M.Add(ex.Current(),empty);
163 ex.Next();
164 }
165 }
166
167 //=======================================================================
168 //function : FirstVertex
169 //purpose :
170 //=======================================================================
171
FirstVertex(const TopoDS_Edge & E,const Standard_Boolean CumOri)172 TopoDS_Vertex TopExp::FirstVertex(const TopoDS_Edge& E,
173 const Standard_Boolean CumOri)
174 {
175 TopoDS_Iterator ite(E,CumOri);
176 while (ite.More()) {
177 if (ite.Value().Orientation() == TopAbs_FORWARD)
178 return TopoDS::Vertex(ite.Value());
179 ite.Next();
180 }
181 return TopoDS_Vertex();
182 }
183
184
185 //=======================================================================
186 //function : LastVertex
187 //purpose :
188 //=======================================================================
189
LastVertex(const TopoDS_Edge & E,const Standard_Boolean CumOri)190 TopoDS_Vertex TopExp::LastVertex(const TopoDS_Edge& E,
191 const Standard_Boolean CumOri)
192 {
193 TopoDS_Iterator ite(E,CumOri);
194 while (ite.More()) {
195 if (ite.Value().Orientation() == TopAbs_REVERSED)
196 return TopoDS::Vertex(ite.Value());
197 ite.Next();
198 }
199 return TopoDS_Vertex();
200 }
201
202
203 //=======================================================================
204 //function : Vertices
205 //purpose :
206 //=======================================================================
207
Vertices(const TopoDS_Edge & E,TopoDS_Vertex & Vfirst,TopoDS_Vertex & Vlast,const Standard_Boolean CumOri)208 void TopExp::Vertices(const TopoDS_Edge& E,
209 TopoDS_Vertex& Vfirst,
210 TopoDS_Vertex& Vlast,
211 const Standard_Boolean CumOri)
212 {
213 // minor optimization for case when Vfirst and Vlast are non-null:
214 // at least for VC++ 10, it is faster if we use boolean flags than
215 // if we nullify vertices at that point (see #27021)
216 Standard_Boolean isFirstDefined = Standard_False;
217 Standard_Boolean isLastDefined = Standard_False;
218
219 TopoDS_Iterator ite(E, CumOri);
220 while (ite.More()) {
221 const TopoDS_Shape& aV = ite.Value();
222 if (aV.Orientation() == TopAbs_FORWARD)
223 {
224 Vfirst = TopoDS::Vertex (aV);
225 isFirstDefined = Standard_True;
226 }
227 else if (aV.Orientation() == TopAbs_REVERSED)
228 {
229 Vlast = TopoDS::Vertex (aV);
230 isLastDefined = Standard_True;
231 }
232 ite.Next();
233 }
234
235 if (!isFirstDefined)
236 Vfirst.Nullify();
237
238 if (!isLastDefined)
239 Vlast.Nullify();
240 }
241
242
243 //=======================================================================
244 //function : Vertices
245 //purpose :
246 //=======================================================================
247
Vertices(const TopoDS_Wire & W,TopoDS_Vertex & Vfirst,TopoDS_Vertex & Vlast)248 void TopExp::Vertices(const TopoDS_Wire& W,
249 TopoDS_Vertex& Vfirst,
250 TopoDS_Vertex& Vlast)
251 {
252 Vfirst = Vlast = TopoDS_Vertex(); // nullify
253
254 TopTools_MapOfShape vmap;
255 TopoDS_Iterator it(W);
256 TopoDS_Vertex V1,V2;
257
258 while (it.More()) {
259 const TopoDS_Edge& E = TopoDS::Edge(it.Value());
260 if (E.Orientation() == TopAbs_REVERSED)
261 TopExp::Vertices(E,V2,V1);
262 else
263 TopExp::Vertices(E,V1,V2);
264 // add or remove in the vertex map
265 V1.Orientation(TopAbs_FORWARD);
266 V2.Orientation(TopAbs_REVERSED);
267 if (!vmap.Add(V1)) vmap.Remove(V1);
268 if (!vmap.Add(V2)) vmap.Remove(V2);
269
270 it.Next();
271 }
272 if (vmap.IsEmpty()) { // closed
273 TopoDS_Shape aLocalShape = V2.Oriented(TopAbs_FORWARD);
274 Vfirst = TopoDS::Vertex(aLocalShape);
275 aLocalShape = V2.Oriented(TopAbs_REVERSED);
276 Vlast = TopoDS::Vertex(aLocalShape);
277 // Vfirst = TopoDS::Vertex(V2.Oriented(TopAbs_FORWARD));
278 // Vlast = TopoDS::Vertex(V2.Oriented(TopAbs_REVERSED));
279 }
280 else if (vmap.Extent() == 2) { //open
281 TopTools_MapIteratorOfMapOfShape ite(vmap);
282
283 while (ite.More() && ite.Key().Orientation() != TopAbs_FORWARD)
284 ite.Next();
285 if (ite.More()) Vfirst = TopoDS::Vertex(ite.Key());
286 ite.Initialize(vmap);
287 while (ite.More() && ite.Key().Orientation() != TopAbs_REVERSED)
288 ite.Next();
289 if (ite.More()) Vlast = TopoDS::Vertex(ite.Key());
290 }
291 }
292
293
294 //=======================================================================
295 //function : CommonVertex
296 //purpose :
297 //=======================================================================
CommonVertex(const TopoDS_Edge & E1,const TopoDS_Edge & E2,TopoDS_Vertex & V)298 Standard_Boolean TopExp::CommonVertex(const TopoDS_Edge& E1,
299 const TopoDS_Edge& E2,
300 TopoDS_Vertex& V)
301 {
302 TopoDS_Vertex firstVertex1, lastVertex1, firstVertex2, lastVertex2;
303 TopExp::Vertices(E1, firstVertex1, lastVertex1);
304 TopExp::Vertices(E2, firstVertex2, lastVertex2);
305
306 if (firstVertex1.IsSame(firstVertex2) ||
307 firstVertex1.IsSame(lastVertex2)) {
308 V = firstVertex1;
309 return Standard_True;
310 }
311 if (lastVertex1.IsSame(firstVertex2) ||
312 lastVertex1.IsSame(lastVertex2)) {
313 V = lastVertex1;
314 return Standard_True;
315 }
316 return Standard_False;
317 } // CommonVertex
318
319