8cd2576fe64fccd3ce4b5cb5bb8b5fc8a36b8ce3
[occt.git] / src / BOPTools / BOPTools_Set.cxx
1 // Created by: Peter KURNEV
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
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
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.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 #include <BOPTools_Set.ixx>
16 #include <TopoDS_Shape.hxx>
17 #include <TopExp_Explorer.hxx>
18 #include <TopoDS_Edge.hxx>
19 #include <BRep_Tool.hxx>
20 #include <BOPCol_MapOfShape.hxx>
21
22 static 
23   Standard_Integer NormalizedIds(const Standard_Integer aId,
24                                  const Standard_Integer aDiv);
25
26 //=======================================================================
27 //function : 
28 //purpose  : 
29 //=======================================================================
30 BOPTools_Set::BOPTools_Set() 
31 :
32   myAllocator(NCollection_BaseAllocator::CommonBaseAllocator()),
33   myShapes(myAllocator)
34 {
35   myNbShapes=0;
36   mySum=0;
37   myUpper=432123;
38 }
39 //=======================================================================
40 //function : 
41 //purpose  : 
42 //=======================================================================
43 BOPTools_Set::BOPTools_Set
44   (const Handle(NCollection_BaseAllocator)& theAllocator) 
45 :
46   myAllocator(theAllocator),
47   myShapes(myAllocator)
48
49   myNbShapes=0;
50   mySum=0;  
51   myUpper=432123;
52 }
53 //=======================================================================
54 //function :~ 
55 //purpose  : 
56 //=======================================================================
57 BOPTools_Set::~BOPTools_Set()
58 {
59   Clear();
60 }
61 //=======================================================================
62 //function : Clear
63 //purpose  : 
64 //=======================================================================
65 void BOPTools_Set::Clear()
66
67   myNbShapes=0;
68   mySum=0;
69   myShapes.Clear();
70 }
71 //=======================================================================
72 //function : NbShapes
73 //purpose  : 
74 //=======================================================================
75 Standard_Integer BOPTools_Set::NbShapes()const
76 {
77   return myNbShapes;
78 }
79 //=======================================================================
80 //function :Assign
81 //purpose  : 
82 //=======================================================================
83 BOPTools_Set& BOPTools_Set::Assign(const BOPTools_Set& theOther)
84
85   BOPCol_ListIteratorOfListOfShape aIt;
86   //
87   myShape=theOther.myShape;
88   myNbShapes=theOther.myNbShapes;
89   mySum=theOther.mySum;
90   myUpper=theOther.myUpper;
91   myAllocator=theOther.myAllocator;
92   //
93   myShapes.Clear();
94   aIt.Initialize(theOther.myShapes);
95   for (; aIt.More(); aIt.Next()) {
96     const TopoDS_Shape& aSx=aIt.Value();
97     myShapes.Append(aSx);
98   }
99   return *this;
100 }
101 //=======================================================================
102 //function : Shape
103 //purpose  : 
104 //=======================================================================
105 const TopoDS_Shape& BOPTools_Set::Shape()const
106 {
107   return myShape;
108 }
109 //=======================================================================
110 //function : HashCode
111 //purpose  : 
112 //=======================================================================
113 Standard_Integer BOPTools_Set::HashCode
114   (const Standard_Integer theUpper)const
115 {
116   return ::HashCode(mySum, theUpper);
117 }
118 //=======================================================================
119 //function : IsEqual
120 //purpose  : 
121 //=======================================================================
122 Standard_Boolean BOPTools_Set::IsEqual
123   (const BOPTools_Set& theOther)const
124 {
125   Standard_Boolean bRet;
126   //
127   bRet=Standard_False;
128   //
129   if (theOther.myNbShapes!=myNbShapes) {
130     return bRet;
131   }
132   //
133   BOPCol_MapOfShape aM1;
134   BOPCol_ListIteratorOfListOfShape aIt;
135   //
136   aIt.Initialize(myShapes);
137   for (; aIt.More(); aIt.Next()) {
138     const TopoDS_Shape& aSx1=aIt.Value();
139     aM1.Add(aSx1);
140   }
141   //
142   aIt.Initialize(theOther.myShapes);
143   for (; aIt.More(); aIt.Next()) {
144     const TopoDS_Shape& aSx2=aIt.Value();
145     if (!aM1.Contains(aSx2)) {
146       return bRet;
147     }
148   }
149   //
150   return !bRet;
151 }
152 //=======================================================================
153 //function : Add
154 //purpose  : 
155 //=======================================================================
156 void BOPTools_Set::Add(const TopoDS_Shape& theS,
157                        const TopAbs_ShapeEnum theType)
158 {
159   Standard_Integer aId, aIdN;
160   TopExp_Explorer aExp;
161   //
162   myShape=theS;
163   myShapes.Clear();
164   myNbShapes=0;
165   mySum=0;
166   //
167   aExp.Init(theS, theType);
168   for (; aExp.More(); aExp.Next()) {
169     const TopoDS_Shape& aSx=aExp.Current();
170     if (theType==TopAbs_EDGE) {
171       const TopoDS_Edge& aEx=*((TopoDS_Edge*)&aSx);
172       if (BRep_Tool::Degenerated(aEx)) {
173         continue;
174       }
175     }
176     myShapes.Append(aSx);
177   }
178   //
179   myNbShapes=myShapes.Extent();
180   if (!myNbShapes) {
181     return;
182   }
183   // 
184   BOPCol_ListIteratorOfListOfShape aIt;
185   //
186   aIt.Initialize(myShapes);
187   for (; aIt.More(); aIt.Next()) {
188     const TopoDS_Shape& aSx=aIt.Value();
189     aId=aSx.HashCode(myUpper);
190     aIdN=NormalizedIds(aId, myNbShapes);
191     mySum+=aIdN;
192   }
193 }
194 //=======================================================================
195 // function: NormalizedIds
196 // purpose : 
197 //=======================================================================
198 Standard_Integer NormalizedIds(const Standard_Integer aId,
199                                const Standard_Integer aDiv)
200 {
201   Standard_Integer aMax, aTresh, aIdRet;
202   //
203   aIdRet=aId;
204   aMax=::IntegerLast();
205   aTresh=aMax/aDiv;
206   if (aId>aTresh) {
207     aIdRet=aId%aTresh;
208   }
209   return aIdRet;
210 }