0023024: Update headers of OCCT files
[occt.git] / src / BOP / BOP_Area2dBuilder.cxx
1 // Created on: 1995-12-21
2 // Created by: Jean Yves LEBEY
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
5 //
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
10 //
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 //
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
20
21 //modified by NIZNHY-PKV Tue Apr  3 15:50:23 2001
22
23 #include <BOP_Area2dBuilder.ixx>
24
25 #include <BOP_Loop.hxx>
26 #include <BOP_ListIteratorOfListOfLoop.hxx>
27
28 //=======================================================================
29 //function : BOP_Area2dBuilder::BOP_Area2dBuilder
30 //purpose  : 
31 //=======================================================================
32 BOP_Area2dBuilder::BOP_Area2dBuilder()
33 {
34 }
35
36 //=======================================================================
37 //function : BOP_Area2dBuilder::BOP_Area2dBuilder
38 //purpose  : 
39 //=======================================================================
40   BOP_Area2dBuilder::BOP_Area2dBuilder(BOP_LoopSet& LS, 
41                                        BOP_LoopClassifier& LC,
42                                        const Standard_Boolean ForceClass)
43 {
44   InitAreaBuilder(LS,LC,ForceClass);
45 }
46
47 //=======================================================================
48 //function : InitAreaBuilder
49 //purpose  : 
50 //=======================================================================
51   void BOP_Area2dBuilder::InitAreaBuilder(BOP_LoopSet&        LS,
52                                           BOP_LoopClassifier& LC,
53                                           const Standard_Boolean ForceClass)
54 {
55   TopAbs_State     state;
56   Standard_Boolean Loopinside;
57   Standard_Boolean loopoutside;
58   
59   BOP_ListIteratorOfListOfListOfLoop AreaIter;
60   BOP_ListIteratorOfListOfLoop       LoopIter;
61   // boundaryloops : list of boundary loops out of the areas.
62   BOP_ListOfLoop                     boundaryloops; 
63   
64   myArea.Clear();          // Clear the list of Area to be built
65   
66   for (LS.InitLoop(); LS.MoreLoop(); LS.NextLoop()) {
67     
68     // process a new loop : L is the new current Loop
69     const Handle(BOP_Loop)& L = LS.Loop();
70     Standard_Boolean boundaryL = L->IsShape();
71     //
72     //const TopoDS_Shape& aSL=L->Shape();
73     //
74     // L = shape et ForceClass  : on traite L comme un block
75     // L = shape et !ForceClass : on traite L comme un pur shape
76     // L = !shape               : on traite L comme un block
77     Standard_Boolean traitercommeblock = (!boundaryL) || ForceClass;
78     if ( !traitercommeblock ) {
79       // the loop L is a boundary loop : 
80       // - try to insert it in an existing area, such as L is inside all 
81       //   the block loops. Only block loops of the area are compared. 
82       // - if L could not be inserted, store it in list of boundary loops.
83       Loopinside = Standard_False; 
84       AreaIter.Initialize(myArea);
85       for (; AreaIter.More(); AreaIter.Next()) {
86         BOP_ListOfLoop& aArea = AreaIter.Value();
87         if ( aArea.IsEmpty() ) {
88           continue;
89         }       
90         state = CompareLoopWithListOfLoop(LC,L,aArea,BOP_BLOCK );
91         if (state == TopAbs_UNKNOWN){
92           Atomize(state,TopAbs_IN);
93         }       
94         Loopinside = ( state == TopAbs_IN);
95         if ( Loopinside ) {
96           break;
97         }
98       } // end of Area scan
99
100       if ( Loopinside ) {
101         BOP_ListOfLoop& aArea = AreaIter.Value();
102         ADD_Loop_TO_LISTOFLoop(L, aArea);
103       }
104       else if ( ! Loopinside ) {
105         ADD_Loop_TO_LISTOFLoop(L, boundaryloops);
106       }
107     } // end of boundary loop
108     
109     else { 
110       // the loop L is a block loop
111       // if L is IN theArea :
112       //   - stop area scan, insert L in theArea.
113       //   - remove from the area all the loops outside L
114       //   - make a new area with them, unless they are all boundary
115       //   - if they are all boundary put them back in boundaryLoops
116       // else :
117       //   - create a new area with L.
118       //   - insert boundary loops that are IN the new area
119       //     (and remove them from 'boundaryloops')
120       //
121       Standard_Integer aNbAreas, aNbLoops;
122       Loopinside = Standard_False;
123       //
124       aNbAreas=myArea.Extent();
125       if (aNbAreas) {
126         AreaIter.Initialize(myArea);
127         for (; AreaIter.More(); AreaIter.Next()) {
128           BOP_ListOfLoop& aArea = AreaIter.Value();
129           aNbLoops=aArea.Extent();
130           if (!aNbLoops){
131             continue;
132           }
133           //
134           state = CompareLoopWithListOfLoop(LC, L, aArea, BOP_ANYLOOP);
135           //
136           if (state == TopAbs_UNKNOWN){
137             Atomize(state,TopAbs_IN);
138           }
139           //
140           Loopinside = (state==TopAbs_IN);
141           if ( Loopinside ){
142             break;
143           }
144         } // end of Area scan
145       }
146       //
147       if ( Loopinside) {
148         BOP_ListOfLoop& aArea = AreaIter.Value();
149         Standard_Boolean allShape = Standard_True;
150         BOP_ListOfLoop removedLoops;
151         LoopIter.Initialize(aArea);
152         while (LoopIter.More()) {
153           const Handle(BOP_Loop)& curL = LoopIter.Value();
154           state = LC.Compare(curL, L);
155           if (state == TopAbs_UNKNOWN) {
156             Atomize(state,TopAbs_IN); // not OUT
157           }       
158           //
159           loopoutside = ( state == TopAbs_OUT );
160           //
161           if ( loopoutside ) {
162             // remove the loop from the area
163             ADD_Loop_TO_LISTOFLoop(curL, removedLoops);
164             
165             allShape = allShape && curL->IsShape();
166             REM_Loop_FROM_LISTOFLoop(LoopIter, aArea);
167           }
168           else {
169             LoopIter.Next();
170           }
171         }// while (LoopIter.More()) {
172         //
173         // insert the loop in the area
174         ADD_Loop_TO_LISTOFLoop(L, aArea);
175         if ( ! removedLoops.IsEmpty() ) {
176           if ( allShape ) {
177             ADD_LISTOFLoop_TO_LISTOFLoop (removedLoops,boundaryloops);
178           }
179           else {
180             // make a new area with the removed loops
181             BOP_ListOfLoop thelistofloop;
182             myArea.Append(thelistofloop);
183             ADD_LISTOFLoop_TO_LISTOFLoop(removedLoops, myArea.Last());
184           }
185         }
186       } // Loopinside == True
187       
188       else {
189         Standard_Integer ashapeinside,ablockinside;
190         BOP_ListOfLoop thelistofloop1;
191         myArea.Append(thelistofloop1);
192         BOP_ListOfLoop& newArea0 = myArea.Last();
193         ADD_Loop_TO_LISTOFLoop(L, newArea0);
194         
195         LoopIter.Initialize(boundaryloops);
196         while ( LoopIter.More() ) {
197           ashapeinside = ablockinside = Standard_False;
198           const Handle(BOP_Loop)& lb = LoopIter.Value();
199           state = LC.Compare(lb,L);
200           if (state == TopAbs_UNKNOWN){
201             Atomize(state,TopAbs_IN);
202           }          
203           ashapeinside = (state == TopAbs_IN);
204           if (ashapeinside) {
205             state = LC.Compare(L,lb);
206             if (state == TopAbs_UNKNOWN){
207               Atomize(state,TopAbs_IN);
208             }       
209             ablockinside = (state == TopAbs_IN);
210           }
211           if ( ashapeinside && ablockinside ) {
212             const Handle(BOP_Loop)& curL = LoopIter.Value();
213             ADD_Loop_TO_LISTOFLoop(curL, newArea0);
214
215             REM_Loop_FROM_LISTOFLoop(LoopIter,boundaryloops);
216           }
217           else { 
218             LoopIter.Next();
219           }
220         } // end of boundaryloops scan
221       } // Loopinside == False
222     } // end of block loop
223   } // end of LoopSet LS scan
224   
225   if ( ! boundaryloops.IsEmpty() ) {
226     if ( myArea.IsEmpty() )  {
227       BOP_ListOfLoop newArea3;
228       newArea3.Append(boundaryloops);
229       myArea.Append(newArea3);
230     }
231   }
232
233   InitArea();
234 }