0024023: Revamp the OCCT Handle -- downcast (automatic)
[occt.git] / src / TopOpeBRepDS / TopOpeBRepDS_TKI.cxx
CommitLineData
b311480e 1// Created on: 1997-09-17
2// Created by: Jean Yves LEBEY
3// Copyright (c) 1997-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 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
973c2be1 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.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
7fd59977 16
17#include <TopOpeBRepDS_TKI.ixx>
18#include <TopOpeBRepDS_define.hxx>
19#include <TopOpeBRepDS_EXPORT.hxx>
20#include <TopOpeBRepDS.hxx>
21#include <Standard_ProgramError.hxx>
22
23// extras define
24#include <TopOpeBRepDS_DataMapOfIntegerListOfInterference.hxx>
25#include <TopOpeBRepDS_DataMapIteratorOfDataMapOfIntegerListOfInterference.hxx>
26#define MDSdmoiloi TopOpeBRepDS_DataMapOfIntegerListOfInterference
27#define MDSdmiodmoiloi TopOpeBRepDS_DataMapIteratorOfDataMapOfIntegerListOfInterference
28#define MDShaodmoiloi TopOpeBRepDS_HArray1OfDataMapOfIntegerListOfInterference
29
30//=======================================================================
31//function : TopOpeBRepDS_TKI
32//purpose :
33//=======================================================================
34TopOpeBRepDS_TKI::TopOpeBRepDS_TKI()
35{
36 Reset();
37}
38
39//=======================================================================
40//function : Reset
41//purpose : private
42//=======================================================================
43void TopOpeBRepDS_TKI::Reset()
44{
45 Standard_Integer ip = (Standard_Integer)TopOpeBRepDS_POINT;
46 Standard_Integer is = (Standard_Integer)TopOpeBRepDS_SOLID;
47 if (ip > is ) {
48 Standard_ProgramError::Raise("TopOpeBRepDS_TKI : enumeration badly ordered");
49 return;
50 }
51 Standard_Integer f = 1; // first index of table
52 Standard_Integer l = f + (is-ip); // last index of table
53 mydelta = f - ip;
54 // k + mydelta = i in [f,l]; TopOpeBRepDS_POINT,SOLID + mydelta = f,l
55 if (myT.IsNull()) myT = new MDShaodmoiloi(f,l);
56 Clear();
57 myK = TopOpeBRepDS_UNKNOWN;
58 myG = 0;
59}
60
61//=======================================================================
62//function : Clear
63//purpose :
64//=======================================================================
65void TopOpeBRepDS_TKI::Clear()
66{
67 Standard_Integer i=myT->Lower(), n=myT->Upper();
68 for (; i<=n; i++) myT->ChangeValue(i).Clear();
69}
70
71//=======================================================================
72//function : FillOnGeometry
73//purpose :
74//=======================================================================
75void TopOpeBRepDS_TKI::FillOnGeometry(const TopOpeBRepDS_ListOfInterference& L)
76{
77 for(TopOpeBRepDS_ListIteratorOfListOfInterference it(L);it.More();it.Next()) {
78 const Handle(TopOpeBRepDS_Interference)& I = it.Value();
79 TopOpeBRepDS_Kind GT,ST; Standard_Integer G,S;
80 FDS_data(I,GT,G,ST,S);
81 Add(GT,G,I);
82 }
83}
84
85//=======================================================================
86//function : FillOnSupport
87//purpose :
88//=======================================================================
89void TopOpeBRepDS_TKI::FillOnSupport(const TopOpeBRepDS_ListOfInterference& L)
90{
91 for(TopOpeBRepDS_ListIteratorOfListOfInterference it(L);it.More();it.Next()) {
92 const Handle(TopOpeBRepDS_Interference)& I = it.Value();
93 TopOpeBRepDS_Kind GT,ST; Standard_Integer G,S;
94 FDS_data(I,GT,G,ST,S);
95 Add(ST,S,I);
96 }
97}
98
99//=======================================================================
100//function : IsBound
101//purpose :
102//=======================================================================
103Standard_Boolean TopOpeBRepDS_TKI::IsBound(const TopOpeBRepDS_Kind K,const Standard_Integer G) const
104{
105 if (!IsValidKG(K,G)) return Standard_False;
106 Standard_Integer TI = KindToTableIndex(K);
107 Standard_Boolean in = myT->Value(TI).IsBound(G);
108 return in;
109}
110
111//=======================================================================
112//function : Interferences
113//purpose :
114//=======================================================================
115const TopOpeBRepDS_ListOfInterference& TopOpeBRepDS_TKI::Interferences
116(const TopOpeBRepDS_Kind K,const Standard_Integer G) const
117{
118 Standard_Boolean in = IsBound(K,G);
119 Standard_Integer TI = KindToTableIndex(K);
120 if ( in ) return myT->Value(TI).Find(G);
121 return myEmptyLOI;
122}
123
124//=======================================================================
125//function : ChangeInterferences
126//purpose :
127//=======================================================================
128TopOpeBRepDS_ListOfInterference& TopOpeBRepDS_TKI::ChangeInterferences(const TopOpeBRepDS_Kind K,const Standard_Integer G)
129{
130 Standard_Boolean in = IsBound(K,G);
131 Standard_Integer TI = KindToTableIndex(K);
132 if ( in ) return myT->ChangeValue(TI).ChangeFind(G);
133 return myEmptyLOI;
134}
135
136//=======================================================================
137//function : HasInterferences
138//purpose :
139//=======================================================================
140Standard_Boolean TopOpeBRepDS_TKI::HasInterferences(const TopOpeBRepDS_Kind K,const Standard_Integer G) const
141{
142 Standard_Boolean has = IsBound(K,G);
143 if ( has ) {
144 const TopOpeBRepDS_ListOfInterference& loi = Interferences(K,G);
145 Standard_Integer l = loi.Extent();
146 has = (l != 0 ) ;
147 }
148 return has;
149}
150
151//=======================================================================
152//function : Add
153//purpose :
154//=======================================================================
155void TopOpeBRepDS_TKI::Add(const TopOpeBRepDS_Kind K,const Standard_Integer G)
156{
157 Standard_Boolean ok = IsValidKG(K,G);
158 if (!ok) {
159 Standard_ProgramError::Raise("TopOpeBRepDS_TKI : Add K G");
160 return;
161 }
162
163 Standard_Boolean in = IsBound(K,G);
164 Standard_Integer TI = KindToTableIndex(K);
165 TopOpeBRepDS_ListOfInterference thelist;
166 if ( !in )
167 myT->ChangeValue(TI).Bind(G, thelist);
168}
169
170//=======================================================================
171//function : Add
172//purpose :
173//=======================================================================
174void TopOpeBRepDS_TKI::Add(const TopOpeBRepDS_Kind K,const Standard_Integer G,const Handle(TopOpeBRepDS_Interference)& HI)
175{
176 Standard_Boolean ok = IsValidKG(K,G);
177 if (!ok) Standard_ProgramError::Raise("TopOpeBRepDS_TKI : Add K G HI");
178
179 Add(K,G);
180 ChangeInterferences(K,G).Append(HI);
181}
182
183//=======================================================================
184//function : DumpTKI
185//purpose :
186//=======================================================================
187void TopOpeBRepDS_TKI::DumpTKI
188(const TCollection_AsciiString& s1,const TCollection_AsciiString& s2) const
189{
190 if (myT.IsNull()) return;
191 cout<<s1;
192 for (Standard_Integer i=myT->Lower();i<=myT->Upper();i++) {
193 TopOpeBRepDS_Kind k = TableIndexToKind(i);
194 DumpTKI(k);
195 }
196 cout<<s2;
197 cout.flush();
198}
199
200//=======================================================================
201//function : DumpTKI
202//purpose :
203//=======================================================================
204void TopOpeBRepDS_TKI::DumpTKI
205(const TopOpeBRepDS_Kind K,const TCollection_AsciiString&,const TCollection_AsciiString&) const
206{
207 if (myT.IsNull()) return;
208 Standard_Integer TI = KindToTableIndex(K);
209 const MDSdmoiloi& M = myT->Value(TI);
210 for (MDSdmiodmoiloi it(M);it.More();it.Next()) {
211 Standard_Integer G = it.Key();
212 DumpTKI(K,G,"","\n");
213 }
214}
215
216//=======================================================================
217//function : DumpTKI
218//purpose :
219//=======================================================================
220void TopOpeBRepDS_TKI::DumpTKI
221(const TopOpeBRepDS_Kind K,const Standard_Integer G,const TCollection_AsciiString& s1,const TCollection_AsciiString& s2) const
222{
223 if (!HasInterferences(K,G)) return;
224 const TopOpeBRepDS_ListOfInterference& loi = Interferences(K,G);
225 DumpTKI(K,G,loi,s1,s2);
226}
227
228//=======================================================================
229//function : DumpTKI
230//purpose :
231//=======================================================================
232void TopOpeBRepDS_TKI::DumpTKI
233(const TopOpeBRepDS_Kind K,const Standard_Integer G,const TopOpeBRepDS_ListOfInterference& L,const TCollection_AsciiString& s1,const TCollection_AsciiString& s2) const
234{
235 if (!HasInterferences(K,G)) return;
236 TCollection_AsciiString s;
237 if (s1.Length()) s = s1;
238 else s = TopOpeBRepDS::SPrint(K,G,"at "," : ");
239 TCollection_AsciiString sb(s.Length(),' ');
240 Standard_Integer i=0;
241 for (TopOpeBRepDS_ListIteratorOfListOfInterference it(L);it.More();it.Next(),i++) {
242 if (i) it.Value()->Dump(cout,sb,s2);
243 else it.Value()->Dump(cout,s,s2);
244 }
245}
246
247//=======================================================================
248//function : DumpTKIIterator
249//purpose :
250//=======================================================================
251void TopOpeBRepDS_TKI::DumpTKIIterator(const TCollection_AsciiString& s1,const TCollection_AsciiString& s2)
252{
253 cout<<s1;
254 Init();
255 while (More()) {
256 TopOpeBRepDS_Kind K;Standard_Integer G; const TopOpeBRepDS_ListOfInterference& L = Value(K,G);
257 DumpTKI(K,G,L,"","\n");
258 Next();
259 }
260 cout<<s2;
261 cout.flush();
262}
263
264//=======================================================================
265//function : Init
266//purpose :
267//=======================================================================
268void TopOpeBRepDS_TKI::Init()
269{
270 myK = TopOpeBRepDS_UNKNOWN;
271 myG = 0;
272 if (myT.IsNull()) return;
273 myTI = myT->Lower(); myK = TableIndexToKind(myTI);
274 myITM.Initialize(myT->Value(myTI));
275 Find();
276}
277
278//=======================================================================
279//function : More
280//purpose :
281//=======================================================================
282Standard_Boolean TopOpeBRepDS_TKI::More() const
283{
284 Standard_Boolean b = IsValidKG(myK,myG);
285 return b;
286}
287
288//=======================================================================
289//function : Next
290//purpose :
291//=======================================================================
292void TopOpeBRepDS_TKI::Next()
293{
294 if ( MoreITM() ) {
295 NextITM();
296 Find();
297 }
298 else if ( MoreTI() ) {
299 NextTI();
300 if (MoreTI()) {
301 myITM.Initialize(myT->Value(myTI));
302 }
303 Find();
304 }
305}
306
307//=======================================================================
308//function : Value
309//purpose :
310//=======================================================================
311const TopOpeBRepDS_ListOfInterference& TopOpeBRepDS_TKI::Value(TopOpeBRepDS_Kind& K,Standard_Integer& G) const
312{
313 if ( !More() ) return myEmptyLOI;
314 K = myK; G = myG;
315 return Interferences(K,G);
316}
317
318//=======================================================================
319//function : ChangeValue
320//purpose :
321//=======================================================================
322TopOpeBRepDS_ListOfInterference& TopOpeBRepDS_TKI::ChangeValue(TopOpeBRepDS_Kind& K,Standard_Integer& G)
323{
324 if ( !More() ) return myEmptyLOI;
325 K = myK; G = myG;
326 return ChangeInterferences(K,G);
327}
328
329//=======================================================================
330//function : MoreTI
331//purpose :
332//=======================================================================
333Standard_Boolean TopOpeBRepDS_TKI::MoreTI() const
334{
335 Standard_Boolean b = IsValidTI(myTI);
336 return b;
337}
338
339//=======================================================================
340//function : NextTI
341//purpose :
342//=======================================================================
343void TopOpeBRepDS_TKI::NextTI()
344{
345 myTI = myTI + 1; myK = TableIndexToKind(myTI);
346}
347
348//=======================================================================
349//function : MoreITM
350//purpose :
351//=======================================================================
352Standard_Boolean TopOpeBRepDS_TKI::MoreITM() const
353{
354 Standard_Boolean b = myITM.More();
355 return b;
356}
357
358//=======================================================================
359//function : FindITM
360//purpose :
361//=======================================================================
362void TopOpeBRepDS_TKI::FindITM()
363{
364 Standard_Boolean f = Standard_False;
365 while (MoreITM()) {
366 myG = myITM.Key();
367 f = HasInterferences(myK,myG);
368 if (f) break;
369 else myITM.Next();
370 }
371}
372
373//=======================================================================
374//function : NextITM
375//purpose :
376//=======================================================================
377void TopOpeBRepDS_TKI::NextITM()
378{
379 if ( !MoreITM() ) return;
380 myITM.Next();
381 FindITM();
382}
383
384//=======================================================================
385//function : Find
386//purpose :
387//=======================================================================
388void TopOpeBRepDS_TKI::Find()
389{
390 Standard_Boolean f = Standard_False;
391 while (MoreTI()) {
392 while (MoreITM()) {
393 FindITM();
394 f = HasInterferences(myK,myG);
395 if (f) break;
396 }
397 if (f) break;
398 else {
399 NextTI();
400 if (MoreTI()) {
401 myITM.Initialize(myT->Value(myTI));
402 }
403 }
404 }
405}
406
407//=======================================================================
408//function : KindToTableIndex
409//purpose : private
410//=======================================================================
411Standard_Integer TopOpeBRepDS_TKI::KindToTableIndex(const TopOpeBRepDS_Kind K) const
412{
413 // K(Kind) + mydelta = TI(integer) = index in myT
414 Standard_Integer TI = (Standard_Integer)K + mydelta;
415 return TI;
416}
417
418//=======================================================================
419//function : TableIndexToKind
420//purpose : private
421//=======================================================================
422TopOpeBRepDS_Kind TopOpeBRepDS_TKI::TableIndexToKind(const Standard_Integer TI) const
423{
424 // K(Kind) + mydelta = TI(integer) = index in myT
425 TopOpeBRepDS_Kind K = (TopOpeBRepDS_Kind)(TI - mydelta);
426 return K;
427}
428
429//=======================================================================
430//function : IsValidTI
431//purpose : private
432//=======================================================================
433Standard_Boolean TopOpeBRepDS_TKI::IsValidTI(const Standard_Integer TI) const
434{
435 if ( myT.IsNull() ) return Standard_False;
436 Standard_Boolean nok = ( TI < myT->Lower() || TI > myT->Upper() );
437 return !nok;
438}
439
440//=======================================================================
441//function : IsValidK
442//purpose : private
443//=======================================================================
444Standard_Boolean TopOpeBRepDS_TKI::IsValidK(const TopOpeBRepDS_Kind K) const
445{
446 Standard_Boolean nok = ( K < TopOpeBRepDS_POINT || K > TopOpeBRepDS_SOLID );
447 return !nok;
448}
449
450//=======================================================================
451//function : IsValidG
452//purpose : private
453//=======================================================================
454Standard_Boolean TopOpeBRepDS_TKI::IsValidG(const Standard_Integer G) const
455{
456 Standard_Boolean nok = (G <= 0);
457 return !nok;
458}
459
460//=======================================================================
461//function : IsValidKG
462//purpose : private
463//=======================================================================
464Standard_Boolean TopOpeBRepDS_TKI::IsValidKG(const TopOpeBRepDS_Kind K,const Standard_Integer G) const
465{
466 Standard_Boolean nok = (!IsValidK(K) || !IsValidG(G));
467 return !nok;
468}