4e57c75e |
1 | // Created by: Peter KURNEV |
973c2be1 |
2 | // Copyright (c) 1999-2014 OPEN CASCADE SAS |
4e57c75e |
3 | // |
973c2be1 |
4 | // This file is part of Open CASCADE Technology software library. |
4e57c75e |
5 | // |
d5f74e42 |
6 | // This library is free software; you can redistribute it and/or modify it under |
7 | // the terms of the GNU Lesser General Public License version 2.1 as published |
973c2be1 |
8 | // by the Free Software Foundation, with special exception defined in the file |
9 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
10 | // distribution for complete text of the license and disclaimer of any warranty. |
4e57c75e |
11 | // |
973c2be1 |
12 | // Alternatively, this file may be used under the terms of Open CASCADE |
13 | // commercial license or contractual agreement. |
4e57c75e |
14 | |
42cf5bc1 |
15 | |
25dfc507 |
16 | #include <BOPDS_SubIterator.hxx> |
17 | |
4e57c75e |
18 | #include <Bnd_Box.hxx> |
25dfc507 |
19 | |
42cf5bc1 |
20 | #include <BOPDS_DS.hxx> |
25dfc507 |
21 | #include <BOPDS_Pair.hxx> |
22 | #include <BOPDS_MapOfPair.hxx> |
23 | |
1155d05a |
24 | #include <BOPTools_BoxBndTree.hxx> |
25 | |
42cf5bc1 |
26 | #include <NCollection_UBTreeFiller.hxx> |
25dfc507 |
27 | |
42cf5bc1 |
28 | #include <TopoDS_Shape.hxx> |
4e57c75e |
29 | |
25dfc507 |
30 | #include <algorithm> |
31 | |
4e57c75e |
32 | //======================================================================= |
25dfc507 |
33 | //function : BOPDS_SubIterator |
4e57c75e |
34 | //purpose : |
35 | //======================================================================= |
36 | BOPDS_SubIterator::BOPDS_SubIterator() |
37 | : |
38 | myAllocator(NCollection_BaseAllocator::CommonBaseAllocator()), |
25dfc507 |
39 | myList(1, myAllocator) |
4e57c75e |
40 | { |
25dfc507 |
41 | myDS=NULL; |
4e57c75e |
42 | } |
43 | //======================================================================= |
25dfc507 |
44 | //function : BOPDS_SubIterator |
4e57c75e |
45 | //purpose : |
46 | //======================================================================= |
47 | BOPDS_SubIterator::BOPDS_SubIterator(const Handle(NCollection_BaseAllocator)& theAllocator) |
48 | : |
49 | myAllocator(theAllocator), |
25dfc507 |
50 | myList(1, myAllocator) |
4e57c75e |
51 | { |
25dfc507 |
52 | myDS=NULL; |
4e57c75e |
53 | } |
54 | //======================================================================= |
55 | //function : ~ |
56 | //purpose : |
57 | //======================================================================= |
58 | BOPDS_SubIterator::~BOPDS_SubIterator() |
59 | { |
60 | } |
61 | //======================================================================= |
4e57c75e |
62 | // function: Initialize |
63 | // purpose: |
64 | //======================================================================= |
25dfc507 |
65 | void BOPDS_SubIterator::Initialize() |
4e57c75e |
66 | { |
25dfc507 |
67 | // sort interfering pairs for constant order of intersection |
68 | std::stable_sort(myList.begin(), myList.end()); |
69 | // initialize iterator to access the pairs |
70 | myIterator.Init(myList); |
4e57c75e |
71 | } |
72 | //======================================================================= |
73 | // function: Value |
74 | // purpose: |
75 | //======================================================================= |
76 | void BOPDS_SubIterator::Value(Standard_Integer& theI1, |
488e5b9d |
77 | Standard_Integer& theI2) const |
4e57c75e |
78 | { |
79 | Standard_Integer iT1, iT2, n1, n2; |
80 | // |
25dfc507 |
81 | const BOPDS_Pair& aPKB = myIterator.Value(); |
82 | aPKB.Indices(n1, n2); |
4e57c75e |
83 | // |
84 | iT1=(Standard_Integer)(myDS->ShapeInfo(n1).ShapeType()); |
85 | iT2=(Standard_Integer)(myDS->ShapeInfo(n2).ShapeType()); |
86 | // |
87 | theI1=n1; |
88 | theI2=n2; |
89 | if (iT1<iT2) { |
90 | theI1=n2; |
91 | theI2=n1; |
92 | } |
93 | } |
94 | //======================================================================= |
95 | // function: Prepare |
96 | // purpose: |
97 | //======================================================================= |
98 | void BOPDS_SubIterator::Prepare() |
99 | { |
100 | myList.Clear(); |
101 | // |
102 | if (!myDS){ |
103 | return; |
104 | } |
25dfc507 |
105 | // |
4e57c75e |
106 | if (!mySubSet1->Extent() || !mySubSet2->Extent()) { |
107 | return; |
108 | } |
25dfc507 |
109 | // |
110 | myList.SetIncrement(2 * (mySubSet1->Extent() + mySubSet2->Extent())); |
111 | // |
4e57c75e |
112 | Intersect(); |
113 | } |
114 | //======================================================================= |
115 | // function: Intersect |
116 | // purpose: |
117 | //======================================================================= |
118 | void BOPDS_SubIterator::Intersect() |
119 | { |
25dfc507 |
120 | Standard_Integer i, j, iTi, iTj; |
1155d05a |
121 | BOPTools_BoxBndTree aBBTree; |
4e57c75e |
122 | NCollection_UBTreeFiller <Standard_Integer, Bnd_Box> aTreeFiller(aBBTree); |
123 | // |
1155d05a |
124 | TColStd_ListIteratorOfListOfInteger aIt(*mySubSet1); |
25dfc507 |
125 | for (; aIt.More(); aIt.Next()) { |
126 | i = aIt.Value(); |
4e57c75e |
127 | // |
25dfc507 |
128 | const BOPDS_ShapeInfo& aSI = myDS->ShapeInfo(i); |
129 | const Bnd_Box& aBoxEx = aSI.Box(); |
4e57c75e |
130 | // |
25dfc507 |
131 | aTreeFiller.Add(i, aBoxEx); |
4e57c75e |
132 | } |
133 | // |
134 | aTreeFiller.Fill(); |
135 | // |
25dfc507 |
136 | BOPDS_MapOfPair aMPKFence; |
137 | // |
138 | aIt.Initialize(*mySubSet2); |
139 | for (; aIt.More(); aIt.Next()) { |
140 | i = aIt.Value(); |
4e57c75e |
141 | // |
25dfc507 |
142 | const BOPDS_ShapeInfo& aSI = myDS->ShapeInfo(i); |
143 | const Bnd_Box& aBoxEx = aSI.Box(); |
4e57c75e |
144 | // |
1155d05a |
145 | BOPTools_BoxBndTreeSelector aSelector; |
4e57c75e |
146 | aSelector.SetBox(aBoxEx); |
25dfc507 |
147 | Standard_Integer aNbSD = aBBTree.Select(aSelector); |
148 | if (!aNbSD) { |
4e57c75e |
149 | continue; |
150 | } |
4e57c75e |
151 | // |
25dfc507 |
152 | iTi = BOPDS_Tools::TypeToInteger(aSI.ShapeType()); |
153 | // |
1155d05a |
154 | const TColStd_ListOfInteger& aLI = aSelector.Indices(); |
155 | TColStd_ListIteratorOfListOfInteger aItLI(aLI); |
25dfc507 |
156 | for (; aItLI.More(); aItLI.Next()) { |
157 | j = aItLI.Value(); |
158 | // |
159 | const BOPDS_ShapeInfo& aSJ = myDS->ShapeInfo(j); |
160 | iTj = BOPDS_Tools::TypeToInteger(aSJ.ShapeType()); |
4e57c75e |
161 | // |
25dfc507 |
162 | // avoid interfering of the same shapes and shape with its sub-shapes |
163 | if ((i == j) || ((iTi < iTj) && aSI.HasSubShape(j)) || |
164 | ((iTi > iTj) && aSJ.HasSubShape(i))) { |
165 | continue; |
166 | } |
167 | // |
168 | BOPDS_Pair aPair(j, i); |
169 | if (aMPKFence.Add(aPair)) { |
170 | myList.Append(aPair); |
171 | } |
172 | } |
4e57c75e |
173 | } |
4e57c75e |
174 | } |