1 // Created on: 2000-09-04
2 // Created by: Vincent DELOS
3 // Copyright (c) 2000-2012 OPEN CASCADE SAS
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.
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.
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.
22 #include <BooleanOperations_Explorer.ixx>
24 const static Standard_Integer theStackSize=20;
26 //===========================================================================
27 //function : BooleanOperations_Explorer
29 //===========================================================================
30 BooleanOperations_Explorer::BooleanOperations_Explorer
31 (const BooleanOperations_ShapesDataStructure& SDS):
35 myTargetToFind(TopAbs_SHAPE),
36 myTargetToAvoid(TopAbs_SHAPE)
38 myShapesDataStructure = (BooleanOperations_PShapesDataStructure)&SDS;
40 //===========================================================================
42 //purpose : ~BooleanOperations_Explorer
43 //===========================================================================
44 void BooleanOperations_Explorer::Delete()
48 Standard::Free((Standard_Address&)myStack);
52 //===========================================================================
55 //===========================================================================
56 void BooleanOperations_Explorer::Init(const Standard_Integer aShapeNumber,
57 const TopAbs_ShapeEnum TargetToFind,
58 const TopAbs_ShapeEnum TargetToAvoid)
60 Standard_Integer i,j,k,theNumberOfTheShapeOnTop;
61 TopAbs_ShapeEnum theTypeOfShapeOnTop,successorType;
63 myTargetToFind = TargetToFind;
64 myTargetToAvoid = TargetToAvoid;
67 Standard::Free((Standard_Address&)myStack);
70 mySizeOfStack = theStackSize;
71 myStack = (Standard_Integer*)Standard::Allocate(theStackSize*sizeof(Standard_Integer));
73 ((Standard_Integer*)myStack)[0] = aShapeNumber;
76 theNumberOfTheShapeOnTop = ((Standard_Integer*)myStack)[myTopOfStack];
77 theTypeOfShapeOnTop = (*myShapesDataStructure).GetShapeType(theNumberOfTheShapeOnTop);
78 if (theTypeOfShapeOnTop == myTargetToFind)
80 hasMore = Standard_True;
83 while (theTypeOfShapeOnTop != myTargetToFind)
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))
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];
99 Standard::Free((Standard_Address&)myStack);
100 myStack = theNewStack;
101 mySizeOfStack = mySizeOfStack+theStackSize+theNumberOfSuccessors;
103 // We remove the shape on top and replace it by its own successors.
104 // We must skip the shape of type TargetToAvoid.
106 for (i=0;i<theNumberOfSuccessors;i++)
108 successorType = (*myShapesDataStructure).GetShapeType(((Standard_Integer*)theSuccessors)[i]);
109 if (successorType == myTargetToAvoid)
112 ((Standard_Integer*)myStack)[i+myTopOfStack-k] = ((Standard_Integer*)theSuccessors)[i];
114 if (theNumberOfSuccessors-k == 0)
116 // No valid successor...
118 if (myTopOfStack < 0)
121 hasMore = Standard_False;
126 myTopOfStack = myTopOfStack+theNumberOfSuccessors-k-1;
127 theNumberOfTheShapeOnTop = ((Standard_Integer*)myStack)[myTopOfStack];
128 theTypeOfShapeOnTop = (*myShapesDataStructure).GetShapeType(theNumberOfTheShapeOnTop);
129 if (theTypeOfShapeOnTop == myTargetToFind)
131 hasMore = Standard_True;
137 //===========================================================================
140 //===========================================================================
141 Standard_Integer BooleanOperations_Explorer::Current()
144 if (myTopOfStack < 0)
145 hasMore = Standard_False;
146 return ((Standard_Integer*)myStack)[myTopOfStack+1];
149 //===========================================================================
152 //===========================================================================
153 void BooleanOperations_Explorer::Next()
155 TopAbs_ShapeEnum theTypeOfShapeOnTop,successorType;
156 Standard_Integer j,k,theNumberOfTheShapeOnTop,successorNumber;
158 if (myTopOfStack < 0)
160 hasMore = Standard_False;
163 theNumberOfTheShapeOnTop = ((Standard_Integer*)myStack)[myTopOfStack];
164 theTypeOfShapeOnTop = (*myShapesDataStructure).GetShapeType(theNumberOfTheShapeOnTop);
165 if (theTypeOfShapeOnTop == myTargetToFind)
167 hasMore = Standard_True;
170 while (theTypeOfShapeOnTop != myTargetToFind)
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))
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];
184 Standard::Free((Standard_Address&)myStack);
185 myStack = theNewStack;
186 mySizeOfStack = mySizeOfStack+theNumberOfSuccessors+theStackSize;
188 // We remove the shape on top and replace it by its own successors.
189 // We must skip the shape of type TargetToAvoid.
191 for (j=0;j<theNumberOfSuccessors;j++)
193 successorNumber = ((Standard_Integer*)theSuccessors)[j];
194 successorType = (*myShapesDataStructure).GetShapeType(successorNumber);
195 if (successorType == myTargetToAvoid)
198 ((Standard_Integer*)myStack)[j+myTopOfStack-k] = ((Standard_Integer*)theSuccessors)[j];
200 if (theNumberOfSuccessors-k == 0)
203 if (myTopOfStack < 0)
205 hasMore = Standard_False;
210 myTopOfStack = myTopOfStack+theNumberOfSuccessors-k-1;
211 theNumberOfTheShapeOnTop = ((Standard_Integer*)myStack)[myTopOfStack];
212 theTypeOfShapeOnTop = (*myShapesDataStructure).GetShapeType(theNumberOfTheShapeOnTop);
213 if (theTypeOfShapeOnTop == myTargetToFind)
215 hasMore = Standard_True;
221 //===========================================================================
224 //===========================================================================
225 Standard_Boolean BooleanOperations_Explorer::More() const
230 //===========================================================================
233 //===========================================================================
234 void BooleanOperations_Explorer::Dump(Standard_OStream& S) const
237 Standard_Integer* theSuccessors;
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++)
248 S << " " << *theSuccessors;