0031668: Visualization - WebGL sample doesn't work on Emscripten 1.39
[occt.git] / src / BRepBuilderAPI / BRepBuilderAPI_Collect.cxx
1 // Created on: 1996-04-09
2 // Created by: Yves FRICAUD
3 // Copyright (c) 1996-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17
18 #include <BRepBuilderAPI_Collect.hxx>
19 #include <BRepBuilderAPI_MakeShape.hxx>
20 #include <TopExp_Explorer.hxx>
21 #include <TopoDS.hxx>
22 #include <TopoDS_Shape.hxx>
23 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
24 #include <TopTools_DataMapOfShapeShape.hxx>
25 #include <TopTools_ListIteratorOfListOfShape.hxx>
26 #include <TopTools_ListOfShape.hxx>
27 #include <TopTools_MapOfShape.hxx>
28
29 #ifdef OCCT_DEBUG
30 #include <stdio.h>
31 Standard_Boolean Affich;
32 #endif
33
34 #ifdef DRAW
35 #include <DBRep.hxx>
36 #endif
37 //=======================================================================
38 //function : BuilBack
39 //purpose  : 
40 //=======================================================================
41
42 static void BuildBack (const TopTools_DataMapOfShapeListOfShape& M1,
43                              TopTools_DataMapOfShapeShape&       BM1)
44 {
45   TopTools_DataMapIteratorOfDataMapOfShapeListOfShape it(M1);
46   for (; it.More(); it.Next()) {
47     const TopoDS_Shape& KS = it.Key();
48     TopTools_ListIteratorOfListOfShape itl(it.Value());
49     for ( ; itl.More(); itl.Next()) {
50       const TopoDS_Shape& VS = itl.Value();
51       BM1.Bind(VS,KS);
52     }
53   }
54 }
55
56 //=======================================================================
57 //function : Replace
58 //purpose  : 
59 //=======================================================================
60
61 static void  Replace (      TopTools_ListOfShape& L,
62                       const TopoDS_Shape          Old,
63                       const TopTools_ListOfShape& New)
64 {
65   //-----------------------------------
66   // Suppression de Old dans la liste.
67   //-----------------------------------
68   TopTools_ListIteratorOfListOfShape it(L);
69   while (it.More()) {
70     if (it.Value().IsSame(Old)) {
71       L.Remove(it);
72       break;
73     }
74     if (it.More()) it.Next();
75   }
76   //---------------------------
77   // Ajout de New a L.
78   //---------------------------
79   TopTools_ListOfShape copNew;
80   copNew = New;
81   L.Append(copNew);
82 }
83
84
85 //=======================================================================
86 //function : StoreImage
87 //purpose  : 
88 //=======================================================================
89
90 static void StoreImage (      TopTools_DataMapOfShapeListOfShape& MG,
91                         const TopoDS_Shape&                       S,
92                         const TopTools_DataMapOfShapeShape&       MGBack,
93                         const TopTools_ListOfShape&               LI)
94 {    
95   if (!LI.IsEmpty()) {
96     if (MGBack.IsBound(S)) {
97
98       Replace (MG.ChangeFind(MGBack(S)),S,LI);
99     }
100     else {
101       if (!MG.IsBound(S)) {
102         TopTools_ListOfShape empty;
103         MG.Bind(S,empty);
104       }
105       // Dans tous les cas on copie la liste pour eviter les pb de
106       // const& dans BRepBuilderAPI.
107       TopTools_ListIteratorOfListOfShape it;
108       for (it.Initialize(LI); it.More(); it.Next()) {
109         const TopoDS_Shape& SS = it.Value();
110         MG(S).Append(SS);
111       }
112     }
113   }
114 }
115
116 //=======================================================================
117 //function : UpdateGen
118 //purpose  : 
119 //=======================================================================
120
121 static void Update (   TopTools_DataMapOfShapeListOfShape& Mod,
122                        TopTools_DataMapOfShapeListOfShape& Gen,
123                        const TopTools_DataMapOfShapeShape& ModBack,
124                        const TopTools_DataMapOfShapeShape& GenBack,
125                        const TopoDS_Shape&                 SI,
126                        BRepBuilderAPI_MakeShape&                  MKS,
127                        const TopAbs_ShapeEnum              ShapeType)
128 {  
129   
130   TopTools_MapOfShape DejaVu;
131   TopExp_Explorer     exp;
132   
133   for (exp.Init(SI,ShapeType); exp.More(); exp.Next()) {
134     const TopoDS_Shape& S = exp.Current();
135     if (!DejaVu.Add(S))  continue;
136
137     //---------------------------------------
138     // Recuperation de l image de S par MKS.
139     //---------------------------------------
140     const TopTools_ListOfShape& LIM = MKS.Modified(S);
141     if (!LIM.IsEmpty()) {
142       if (GenBack.IsBound(S)) {
143         // Modif de generation => generation du shape initial
144         StoreImage (Gen,S,GenBack,LIM);
145       }
146       else {
147         StoreImage (Mod,S,ModBack,LIM);
148       }
149     }
150     const TopTools_ListOfShape& LIG = MKS.Generated(S);
151     if (!LIG.IsEmpty()) {
152       if (ModBack.IsBound(S)) {
153         // Generation de modif  => generation du shape initial
154         TopoDS_Shape IS = ModBack(S);
155         StoreImage (Gen,IS,GenBack,LIG);
156       }
157       else {
158         StoreImage (Gen,S,GenBack,LIG);
159       }
160     }
161   }
162 }
163 #ifdef OCCT_DEBUG
164 //=======================================================================
165 //function : DEBControl
166 //purpose  : 
167 //=======================================================================
168
169 static void DEBControl (const TopTools_DataMapOfShapeListOfShape& MG)
170 {
171   char name[100];
172   Standard_Integer IK = 0;
173   
174   TopTools_DataMapIteratorOfDataMapOfShapeListOfShape it(MG);
175   for (; it.More(); it.Next()) {
176     const TopoDS_Shape& OS = it.Key();
177     sprintf(name, "SK_%d",++IK);
178 #ifdef DRAW
179     DBRep::Set(name,OS);
180 #endif
181     TopTools_ListIteratorOfListOfShape itl(MG(OS));
182     Standard_Integer IV = 1;
183     for (; itl.More(); itl.Next()) {
184       sprintf(name, "SV_%d_%d",IK,IV++);
185 #ifdef DRAW
186       DBRep::Set(name,NS);
187 #endif
188     }
189   }
190 }
191 #endif
192 //=======================================================================
193 //function : BRepBuilderAPI_Collect
194 //purpose  : 
195 //=======================================================================
196
197 BRepBuilderAPI_Collect::BRepBuilderAPI_Collect()
198 {}
199
200 //=======================================================================
201 //function : Add
202 //purpose  : 
203 //=======================================================================
204
205 void BRepBuilderAPI_Collect::Add (const TopoDS_Shape& SI, 
206                            BRepBuilderAPI_MakeShape&  MKS)
207
208 {
209   TopTools_DataMapOfShapeShape GenBack;
210   TopTools_DataMapOfShapeShape ModBack;
211   BuildBack (myGen, GenBack);   // Vraiment pas optimum a Revoir
212   BuildBack (myMod, ModBack);
213
214   Update (myMod,myGen,ModBack,GenBack,SI,MKS,TopAbs_COMPOUND);
215   Update (myMod,myGen,ModBack,GenBack,SI,MKS,TopAbs_COMPSOLID);
216   Update (myMod,myGen,ModBack,GenBack,SI,MKS,TopAbs_SOLID);
217   Update (myMod,myGen,ModBack,GenBack,SI,MKS,TopAbs_SHELL);
218   Update (myMod,myGen,ModBack,GenBack,SI,MKS,TopAbs_FACE);
219   Update (myMod,myGen,ModBack,GenBack,SI,MKS,TopAbs_WIRE);
220   Update (myMod,myGen,ModBack,GenBack,SI,MKS,TopAbs_EDGE);
221   Update (myMod,myGen,ModBack,GenBack,SI,MKS,TopAbs_VERTEX);
222
223 #ifdef OCCT_DEBUG
224   if (Affich) {
225     DEBControl (myGen);
226     DEBControl (myMod);
227   }
228 #endif
229 }
230 //=======================================================================
231 //function : Add
232 //purpose  : 
233 //=======================================================================
234
235 void BRepBuilderAPI_Collect::AddGenerated (const TopoDS_Shape& S,
236                                     const TopoDS_Shape& NS) 
237 {  
238   TopTools_DataMapOfShapeShape GenBack;
239   TopTools_DataMapOfShapeShape ModBack;
240   BuildBack (myGen, GenBack);
241   BuildBack (myMod, ModBack);
242
243   TopTools_ListOfShape LIG;
244   LIG.Append(NS);
245   if (ModBack.IsBound(S)) {
246     // Generation de modif  => generation du shape initial
247     TopoDS_Shape IS = ModBack(S);
248     StoreImage (myGen,IS,GenBack,LIG);
249   }
250   else {
251     StoreImage (myGen,S,GenBack,LIG);
252   }
253 }
254
255 //=======================================================================
256 //function : Add
257 //purpose  : 
258 //=======================================================================
259
260 void BRepBuilderAPI_Collect::AddModif  (const TopoDS_Shape& S, 
261                                  const TopoDS_Shape& NS)
262
263 {  
264   TopTools_DataMapOfShapeShape GenBack;
265   TopTools_DataMapOfShapeShape ModBack;
266   BuildBack (myGen, GenBack);
267   BuildBack (myMod, ModBack);
268   
269   TopTools_ListOfShape LIG;
270   LIG.Append(NS);
271   if (GenBack.IsBound(S)) {
272     // Modif de generation => generation du shape initial
273     StoreImage (myGen,S,GenBack,LIG);
274   }
275   else {
276     StoreImage (myMod,S,ModBack,LIG);
277   }
278 }
279
280
281 //=======================================================================
282 //function : Filter
283 //purpose  : 
284 //=======================================================================
285
286 static void FilterByShape(TopTools_DataMapOfShapeListOfShape& MG,
287                           const TopoDS_Shape&                 SF)
288 {
289   TopTools_MapOfShape MSF;
290   TopExp_Explorer     exp;
291   Standard_Boolean    YaEdge   = Standard_False;
292   Standard_Boolean    YaVertex = Standard_False;
293   for (exp.Init(SF,TopAbs_FACE)  ; exp.More(); exp.Next()) MSF.Add(exp.Current());
294
295   //-------------------------------------------------------------
296   // Suppression de toutes les images qui ne sont pas dans MSF.
297   //-------------------------------------------------------------
298   TopTools_DataMapIteratorOfDataMapOfShapeListOfShape it(MG);
299   for (; it.More(); it.Next()) {
300     const TopoDS_Shape&   OS  = it.Key();
301     TopTools_ListOfShape& LNS = MG.ChangeFind(OS); 
302     TopTools_ListIteratorOfListOfShape itl(LNS);
303     while (itl.More()) {
304       const TopoDS_Shape& NS = itl.Value();
305       //-------------------------------------------------------------------
306       // Images contiennet des edges => ajout des edges resultat dans MSF.
307       //-------------------------------------------------------------------
308       if (!YaEdge && NS.ShapeType() == TopAbs_EDGE) {  
309         for (exp.Init(SF,TopAbs_EDGE)  ; exp.More(); exp.Next()) { 
310           MSF.Add(exp.Current());
311         }
312         YaEdge = Standard_True;
313       }
314       //-------------------------------------------------------------------
315       // Images contiennet des vertex => ajout des vertex resultat dans MSF.
316       //-------------------------------------------------------------------
317       if (!YaVertex && NS.ShapeType() == TopAbs_VERTEX) {  
318         for (exp.Init(SF,TopAbs_VERTEX)  ; exp.More(); exp.Next()) { 
319           MSF.Add(exp.Current());
320         }
321         YaVertex = Standard_True;
322       }
323       //---------------------------------------
324       // Si pas dans MSF suprresion de l image.
325       //---------------------------------------
326       if (!MSF.Contains(NS)) {
327         LNS.Remove(itl);
328       }
329       else if (itl.More()) itl.Next();
330     }
331   }
332 #ifdef OCCT_DEBUG
333   if (Affich) {
334     DEBControl (MG);
335   }
336 #endif
337
338 }
339
340 //=======================================================================
341 //function : Modification
342 //purpose  : 
343 //=======================================================================
344
345 const TopTools_DataMapOfShapeListOfShape&   BRepBuilderAPI_Collect::Modification() const
346 {
347   return myMod;
348 }
349
350 //=======================================================================
351 //function : Generation
352 //purpose  : 
353 //=======================================================================
354
355 const TopTools_DataMapOfShapeListOfShape&   BRepBuilderAPI_Collect::Generated() const
356 {
357   return myGen;
358 }
359
360 //=======================================================================
361 //function : Filter
362 //purpose  : 
363 //=======================================================================
364
365 void BRepBuilderAPI_Collect::Filter(const TopoDS_Shape& SF)
366 {
367   FilterByShape (myGen,SF);
368   FilterByShape (myMod,SF);
369 }
370
371
372
373
374
375