Adding of testing cases from subgroups 937 940 and 941 of CHL group
[occt.git] / src / BooleanOperations / BooleanOperations_Explorer.cxx
1 // Created on: 2000-09-04
2 // Created by: Vincent DELOS
3 // Copyright (c) 2000-2012 OPEN CASCADE SAS
4 //
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
9 //
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 //
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
19
20
21
22 #include <BooleanOperations_Explorer.ixx>
23
24 const static Standard_Integer theStackSize=20;
25 //
26 //===========================================================================
27 //function : BooleanOperations_Explorer
28 //purpose  : 
29 //===========================================================================
30   BooleanOperations_Explorer::BooleanOperations_Explorer
31     (const BooleanOperations_ShapesDataStructure& SDS):
32   myStack(0L),
33   myTopOfStack(-1),
34   mySizeOfStack(-1),
35   myTargetToFind(TopAbs_SHAPE),
36   myTargetToAvoid(TopAbs_SHAPE)
37 {
38   myShapesDataStructure = (BooleanOperations_PShapesDataStructure)&SDS;
39 }
40 //===========================================================================
41 //function : Delete
42 //purpose  : ~BooleanOperations_Explorer
43 //===========================================================================
44   void BooleanOperations_Explorer::Delete()
45 {
46   if(myStack != 0L) {
47     
48     Standard::Free((Standard_Address&)myStack);
49   }
50   myStack = 0;
51 }
52 //===========================================================================
53 //function : Init
54 //purpose  : 
55 //===========================================================================
56   void BooleanOperations_Explorer::Init(const Standard_Integer aShapeNumber,
57                                         const TopAbs_ShapeEnum TargetToFind,
58                                         const TopAbs_ShapeEnum  TargetToAvoid)
59 {
60   Standard_Integer i,j,k,theNumberOfTheShapeOnTop;
61   TopAbs_ShapeEnum theTypeOfShapeOnTop,successorType;
62
63   myTargetToFind = TargetToFind;
64   myTargetToAvoid = TargetToAvoid;
65
66   if (myStack != 0L) {
67     Standard::Free((Standard_Address&)myStack);
68   }
69
70   mySizeOfStack = theStackSize;
71   myStack = (Standard_Integer*)Standard::Allocate(theStackSize*sizeof(Standard_Integer));
72
73   ((Standard_Integer*)myStack)[0] = aShapeNumber;
74   myTopOfStack = 0;
75
76   theNumberOfTheShapeOnTop = ((Standard_Integer*)myStack)[myTopOfStack];
77   theTypeOfShapeOnTop = (*myShapesDataStructure).GetShapeType(theNumberOfTheShapeOnTop);
78   if (theTypeOfShapeOnTop == myTargetToFind)
79     {
80       hasMore = Standard_True;
81       return;
82     }
83   while (theTypeOfShapeOnTop != myTargetToFind)
84     {
85       Standard_Address theSuccessors;
86       Standard_Integer theNumberOfSuccessors;
87       // We get the successors of the shape on top of the stack.
88       (*myShapesDataStructure).GetSuccessors(theNumberOfTheShapeOnTop,theSuccessors,theNumberOfSuccessors);
89       // Do we have enough place to store our new successors ?
90       if ((myTopOfStack+theNumberOfSuccessors > mySizeOfStack) && (theSuccessors != 0L))
91         {
92           // We don't have enough place so we reallocate.
93           Standard_Address theNewStack = (Standard_Integer*)Standard::Allocate
94             ((mySizeOfStack+theStackSize+theNumberOfSuccessors)*sizeof(Standard_Integer));
95           // We copy the old array in the one.
96           for (j=0;j<myTopOfStack;j++)
97             ((Standard_Integer*)theNewStack)[j] = ((Standard_Integer*)myStack)[j];
98
99           Standard::Free((Standard_Address&)myStack);
100           myStack = theNewStack;
101           mySizeOfStack = mySizeOfStack+theStackSize+theNumberOfSuccessors;
102         }
103       // We remove the shape on top and replace it by its own successors.
104       // We must skip the shape of type TargetToAvoid.
105       k = 0;
106       for (i=0;i<theNumberOfSuccessors;i++)
107         {
108           successorType = (*myShapesDataStructure).GetShapeType(((Standard_Integer*)theSuccessors)[i]);
109           if (successorType == myTargetToAvoid)
110             k++;
111           else
112             ((Standard_Integer*)myStack)[i+myTopOfStack-k] = ((Standard_Integer*)theSuccessors)[i];
113         }
114       if (theNumberOfSuccessors-k == 0)
115         {
116           // No valid successor...
117           myTopOfStack--;
118           if (myTopOfStack < 0)
119             {
120               // Empty stack.
121               hasMore = Standard_False;
122               return;
123             }
124         }
125       else
126         myTopOfStack = myTopOfStack+theNumberOfSuccessors-k-1;
127       theNumberOfTheShapeOnTop = ((Standard_Integer*)myStack)[myTopOfStack];
128       theTypeOfShapeOnTop = (*myShapesDataStructure).GetShapeType(theNumberOfTheShapeOnTop);
129       if (theTypeOfShapeOnTop == myTargetToFind)
130         {
131           hasMore = Standard_True;
132           return;
133         }
134     }
135 }
136
137 //===========================================================================
138 //function : Current
139 //purpose  : 
140 //===========================================================================
141   Standard_Integer BooleanOperations_Explorer::Current()
142 {
143   myTopOfStack--;
144   if (myTopOfStack < 0)
145     hasMore = Standard_False;
146   return ((Standard_Integer*)myStack)[myTopOfStack+1];
147 }
148
149 //===========================================================================
150 //function : Next
151 //purpose  : 
152 //===========================================================================
153   void BooleanOperations_Explorer::Next()
154 {
155   TopAbs_ShapeEnum theTypeOfShapeOnTop,successorType;
156   Standard_Integer j,k,theNumberOfTheShapeOnTop,successorNumber;
157   
158   if (myTopOfStack < 0)
159     {
160       hasMore = Standard_False;
161       return;
162     }
163   theNumberOfTheShapeOnTop = ((Standard_Integer*)myStack)[myTopOfStack];
164   theTypeOfShapeOnTop = (*myShapesDataStructure).GetShapeType(theNumberOfTheShapeOnTop);
165   if (theTypeOfShapeOnTop == myTargetToFind)
166     {
167       hasMore = Standard_True;
168       return;
169     }
170   while (theTypeOfShapeOnTop != myTargetToFind)
171     {
172       Standard_Address theSuccessors = 0L;
173       Standard_Integer theNumberOfSuccessors;
174       (*myShapesDataStructure).GetSuccessors(theNumberOfTheShapeOnTop,(Standard_Address&)theSuccessors,theNumberOfSuccessors);
175       // Do we have enough place to store our new successors ?
176       if ((myTopOfStack+theNumberOfSuccessors > mySizeOfStack) && (theSuccessors != 0L))
177         {
178           Standard_Address theNewStack;
179           theNewStack = (Standard_Integer*)Standard::Allocate
180             ((mySizeOfStack+theNumberOfSuccessors+theStackSize)*sizeof(Standard_Integer));
181           for (j=0;j<myTopOfStack;j++)
182             ((Standard_Integer*)theNewStack)[j] = ((Standard_Integer*)myStack)[j];
183           
184           Standard::Free((Standard_Address&)myStack);
185           myStack = theNewStack;
186           mySizeOfStack = mySizeOfStack+theNumberOfSuccessors+theStackSize;
187         }
188       // We remove the shape on top and replace it by its own successors.
189       // We must skip the shape of type TargetToAvoid.
190       k = 0;
191       for (j=0;j<theNumberOfSuccessors;j++)
192         {
193           successorNumber = ((Standard_Integer*)theSuccessors)[j];
194           successorType = (*myShapesDataStructure).GetShapeType(successorNumber);
195           if (successorType == myTargetToAvoid)
196             k++;
197           else
198             ((Standard_Integer*)myStack)[j+myTopOfStack-k] = ((Standard_Integer*)theSuccessors)[j];
199         }
200       if (theNumberOfSuccessors-k == 0)
201         {
202           myTopOfStack--;
203           if (myTopOfStack < 0)
204             {
205               hasMore = Standard_False;
206               return;
207             }
208         }
209       else
210         myTopOfStack = myTopOfStack+theNumberOfSuccessors-k-1;
211       theNumberOfTheShapeOnTop = ((Standard_Integer*)myStack)[myTopOfStack];
212       theTypeOfShapeOnTop = (*myShapesDataStructure).GetShapeType(theNumberOfTheShapeOnTop);
213       if (theTypeOfShapeOnTop == myTargetToFind)
214         {
215           hasMore = Standard_True;
216           return;
217         }
218     }
219 }
220
221 //===========================================================================
222 //function : More
223 //purpose  : 
224 //===========================================================================
225   Standard_Boolean BooleanOperations_Explorer::More() const
226 {
227   return hasMore;
228 }
229
230 //===========================================================================
231 //function : Dump
232 //purpose  : 
233 //===========================================================================
234   void BooleanOperations_Explorer::Dump(Standard_OStream& S) const
235 {
236   Standard_Integer i;
237   Standard_Integer* theSuccessors;
238
239   theSuccessors = ((Standard_Integer*)myStack);
240   S  << "\n" << "Dump of BooleanOperations_Explorer:" << "\n";
241   S << "mySizeOfStack   = " << mySizeOfStack << "\n";
242   S << "myTopOfStack    = " << myTopOfStack << "\n";
243   S << "myTargetToFind  = " << myTargetToFind << "\n";
244   S << "myTargetToAvoid = " << myTargetToAvoid << "\n";
245   S << "hasMore         = " << hasMore << "\n";
246   for (i=0;i<=myTopOfStack;i++)
247     {
248       S << " " << *theSuccessors;
249       theSuccessors++;
250     }
251   S  << "\n";
252 }