0031313: Foundation Classes - Dump improvement for classes
[occt.git] / src / Graphic3d / Graphic3d_StructureManager.cxx
1 // Created by: NW,JPB,CAL
2 // Copyright (c) 1991-1999 Matra Datavision
3 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <Graphic3d_StructureManager.hxx>
17
18 #include <Graphic3d_DataStructureManager.hxx>
19 #include <Graphic3d_GraphicDriver.hxx>
20 #include <Graphic3d_Structure.hxx>
21
22 IMPLEMENT_STANDARD_RTTIEXT(Graphic3d_StructureManager,Standard_Transient)
23
24 #include <Graphic3d_MapIteratorOfMapOfStructure.hxx>
25 #include <Graphic3d_CView.hxx>
26
27 // ========================================================================
28 // function : Graphic3d_StructureManager
29 // purpose  :
30 // ========================================================================
31 Graphic3d_StructureManager::Graphic3d_StructureManager (const Handle(Graphic3d_GraphicDriver)& theDriver)
32 : myViewGenId (0, 31),
33   myGraphicDriver (theDriver),
34   myDeviceLostFlag (Standard_False)
35 {
36   //
37 }
38
39 // ========================================================================
40 // function : ~Graphic3d_StructureManager
41 // purpose  :
42 // ========================================================================
43 Graphic3d_StructureManager::~Graphic3d_StructureManager()
44 {
45   for (Graphic3d_MapIteratorOfMapOfStructure anIt (myDisplayedStructure); anIt.More(); anIt.Next())
46   {
47     anIt.Value()->Remove();
48   }
49
50   myDisplayedStructure.Clear();
51   myHighlightedStructure.Clear();
52   myDefinedViews.Clear();
53 }
54
55 // ========================================================================
56 // function : Update
57 // purpose  :
58 // ========================================================================
59 void Graphic3d_StructureManager::Update (const Graphic3d_ZLayerId theLayerId) const
60 {
61   for (Graphic3d_IndexedMapOfView::Iterator aViewIt (myDefinedViews); aViewIt.More(); aViewIt.Next())
62   {
63     aViewIt.Value()->Update (theLayerId);
64   }
65 }
66
67 // ========================================================================
68 // function : Remove
69 // purpose  :
70 // ========================================================================
71 void Graphic3d_StructureManager::Remove()
72 {
73   // clear all structures whilst views are alive for correct GPU memory management
74   for (Graphic3d_MapIteratorOfMapOfStructure anIt (myDisplayedStructure); anIt.More(); anIt.Next())
75   {
76     anIt.Value()->Remove();
77   }
78   myDisplayedStructure.Clear();
79   myHighlightedStructure.Clear();
80
81   // clear list of managed views
82   myDefinedViews.Clear();
83 }
84
85 // ========================================================================
86 // function : Erase
87 // purpose  :
88 // ========================================================================
89 void Graphic3d_StructureManager::Erase()
90 {
91   for (Graphic3d_MapIteratorOfMapOfStructure anIt (myDisplayedStructure); anIt.More(); anIt.Next())
92   {
93     anIt.Key()->Erase();
94   }
95 }
96
97 void Graphic3d_StructureManager::DisplayedStructures (Graphic3d_MapOfStructure& SG) const {
98
99   SG.Assign(myDisplayedStructure);
100
101   //JMBStandard_Integer Length  = myDisplayedStructure.Length ();
102
103   //JMBfor (Standard_Integer i=1; i<=Length; i++)
104   //JMB SG.Add (myDisplayedStructure.Value (i));
105
106 }
107
108 Standard_Integer Graphic3d_StructureManager::NumberOfDisplayedStructures () const {
109
110 Standard_Integer Length = myDisplayedStructure.Extent ();
111
112         return (Length);
113
114 }
115
116 //Handle(Graphic3d_Structure) Graphic3d_StructureManager::DisplayedStructure (const Standard_Integer AnIndex) const {
117
118 //return (myDisplayedStructure.Value (AnIndex));
119
120 //}
121
122 void Graphic3d_StructureManager::HighlightedStructures (Graphic3d_MapOfStructure& SG) const {
123
124   SG.Assign(myHighlightedStructure);
125
126 }
127
128 Handle(Graphic3d_Structure) Graphic3d_StructureManager::Identification (const Standard_Integer AId) const {
129
130 //  Standard_Integer ind=0;
131   Standard_Boolean notfound     = Standard_True;
132
133   Handle(Graphic3d_Structure) StructNull;
134  
135   Graphic3d_MapIteratorOfMapOfStructure it( myDisplayedStructure);
136   
137   Handle(Graphic3d_Structure) SGfound;
138
139   for (; it.More() && notfound; it.Next()) {
140     Handle(Graphic3d_Structure) SG = it.Key();
141     if ( SG->Identification () == AId) {
142       notfound  = Standard_False;
143       SGfound = SG;
144     }
145   }
146
147
148   if (notfound)
149     return (StructNull);
150   else
151     return (SGfound);
152
153 }
154
155 const Handle(Graphic3d_GraphicDriver)& Graphic3d_StructureManager::GraphicDriver () const 
156 {
157   return (myGraphicDriver);
158 }
159
160 void Graphic3d_StructureManager::RecomputeStructures()
161 {
162   myDeviceLostFlag = Standard_False;
163
164   // Go through all unique structures including child (connected) ones and ensure that they are computed.
165   NCollection_Map<Graphic3d_Structure*> aStructNetwork;
166   for (Graphic3d_MapIteratorOfMapOfStructure anIter(myDisplayedStructure); anIter.More(); anIter.Next())
167   {
168     Handle(Graphic3d_Structure) aStructure = anIter.Key();
169     anIter.Key()->Network (anIter.Key().get(), Graphic3d_TOC_DESCENDANT, aStructNetwork);
170   }
171
172   RecomputeStructures (aStructNetwork);
173 }
174
175 void Graphic3d_StructureManager::RecomputeStructures (const NCollection_Map<Graphic3d_Structure*>& theStructures)
176 {
177   for (NCollection_Map<Graphic3d_Structure*>::Iterator anIter (theStructures); anIter.More(); anIter.Next())
178   {
179     Graphic3d_Structure* aStruct = anIter.Key();
180     aStruct->Clear();
181     aStruct->Compute();
182   }
183 }
184
185 Handle(Graphic3d_ViewAffinity) Graphic3d_StructureManager::RegisterObject (const Handle(Standard_Transient)& theObject)
186 {
187   Handle(Graphic3d_ViewAffinity) aResult;
188   if (myRegisteredObjects.Find (theObject.operator->(), aResult))
189   {
190     return aResult;
191   }
192
193   aResult = new Graphic3d_ViewAffinity();
194   myRegisteredObjects.Bind (theObject.operator->(), aResult);
195   return aResult;
196 }
197
198 void Graphic3d_StructureManager::UnregisterObject (const Handle(Standard_Transient)& theObject)
199 {
200   myRegisteredObjects.UnBind (theObject.operator->());
201 }
202
203 Handle(Graphic3d_ViewAffinity) Graphic3d_StructureManager::ObjectAffinity (const Handle(Standard_Transient)& theObject) const
204 {
205   Handle(Graphic3d_ViewAffinity) aResult;
206   myRegisteredObjects.Find (theObject.operator->(), aResult);
207   return aResult;
208 }
209
210 // ========================================================================
211 // function : Identification
212 // purpose  :
213 // ========================================================================
214 Standard_Integer Graphic3d_StructureManager::Identification (Graphic3d_CView* theView)
215 {
216   if (myDefinedViews.Contains (theView))
217   {
218     return theView->Identification();
219   }
220
221   myDefinedViews.Add (theView);
222   return myViewGenId.Next();
223 }
224
225 // ========================================================================
226 // function : UnIdentification
227 // purpose  :
228 // ========================================================================
229 void Graphic3d_StructureManager::UnIdentification (Graphic3d_CView* theView)
230 {
231   if (myDefinedViews.Contains (theView))
232   {
233     myDefinedViews.Swap (myDefinedViews.FindIndex (theView), myDefinedViews.Size());
234     myDefinedViews.RemoveLast();
235     myViewGenId.Free (theView->Identification());
236   }
237 }
238
239 // ========================================================================
240 // function : DefinedViews
241 // purpose  :
242 // ========================================================================
243 const Graphic3d_IndexedMapOfView& Graphic3d_StructureManager::DefinedViews() const
244 {
245   return myDefinedViews;
246 }
247
248 // ========================================================================
249 // function : MaxNumOfViews
250 // purpose  :
251 // ========================================================================
252 Standard_Integer Graphic3d_StructureManager::MaxNumOfViews() const
253 {
254   return myViewGenId.Upper() - myViewGenId.Lower() + 1;
255 }
256
257 // ========================================================================
258 // function : ReCompute
259 // purpose  :
260 // ========================================================================
261 void Graphic3d_StructureManager::ReCompute (const Handle(Graphic3d_Structure)& theStructure)
262 {
263   if (!myDisplayedStructure.Contains (theStructure))
264   {
265     return;
266   }
267
268   // Recompute structure for all defined views
269   for (Standard_Integer aViewIt = 1; aViewIt <= myDefinedViews.Extent(); ++aViewIt)
270   {
271     ReCompute (theStructure, myDefinedViews (aViewIt));
272   }
273 }
274
275 // ========================================================================
276 // function : ReCompute
277 // purpose  :
278 // ========================================================================
279 void Graphic3d_StructureManager::ReCompute (const Handle(Graphic3d_Structure)& theStructure,
280                                             const Handle(Graphic3d_DataStructureManager)& theProjector)
281 {
282   Handle(Graphic3d_CView) aView = Handle(Graphic3d_CView)::DownCast (theProjector);
283
284   if (aView.IsNull()
285    || !myDefinedViews.Contains (aView.operator->())
286    || !myDisplayedStructure.Contains (theStructure))
287   {
288     return;
289   }
290
291   aView->ReCompute (theStructure);
292 }
293
294 // ========================================================================
295 // function : Clear
296 // purpose  :
297 // ========================================================================
298 void Graphic3d_StructureManager::Clear (Graphic3d_Structure* theStructure,
299                                         const Standard_Boolean theWithDestruction)
300 {
301   for (Graphic3d_IndexedMapOfView::Iterator aViewIt (myDefinedViews); aViewIt.More(); aViewIt.Next())
302   {
303     aViewIt.Value()->Clear (theStructure, theWithDestruction);
304   }
305 }
306
307 // ========================================================================
308 // function : Connect
309 // purpose  :
310 // ========================================================================
311 void Graphic3d_StructureManager::Connect (const Graphic3d_Structure* theMother,
312                                           const Graphic3d_Structure* theDaughter)
313 {
314   for (Graphic3d_IndexedMapOfView::Iterator aViewIt (myDefinedViews); aViewIt.More(); aViewIt.Next())
315   {
316     aViewIt.Value()->Connect (theMother, theDaughter);
317   }
318 }
319
320 // ========================================================================
321 // function : Disconnect
322 // purpose  :
323 // ========================================================================
324 void Graphic3d_StructureManager::Disconnect (const Graphic3d_Structure* theMother,
325                                              const Graphic3d_Structure* theDaughter)
326 {
327   for (Graphic3d_IndexedMapOfView::Iterator aViewIt (myDefinedViews); aViewIt.More(); aViewIt.Next())
328   {
329     aViewIt.Value()->Disconnect (theMother, theDaughter);
330   }
331 }
332
333 // ========================================================================
334 // function : Display
335 // purpose  :
336 // ========================================================================
337 void Graphic3d_StructureManager::Display (const Handle(Graphic3d_Structure)& theStructure)
338 {
339   myDisplayedStructure.Add (theStructure);
340
341   for (Graphic3d_IndexedMapOfView::Iterator aViewIt (myDefinedViews); aViewIt.More(); aViewIt.Next())
342   {
343     aViewIt.Value()->Display (theStructure);
344   }
345 }
346
347 // ========================================================================
348 // function : Erase
349 // purpose  :
350 // ========================================================================
351 void Graphic3d_StructureManager::Erase (const Handle(Graphic3d_Structure)& theStructure)
352 {
353   myDisplayedStructure  .Remove(theStructure);
354   myHighlightedStructure.Remove (theStructure);
355
356   // Erase structure in all defined views
357   for (Graphic3d_IndexedMapOfView::Iterator aViewIt (myDefinedViews); aViewIt.More(); aViewIt.Next())
358   {
359     aViewIt.Value()->Erase (theStructure);
360   }
361 }
362
363 // ========================================================================
364 // function : Erase
365 // purpose  :
366 // ========================================================================
367 void Graphic3d_StructureManager::Highlight (const Handle(Graphic3d_Structure)& theStructure)
368 {
369   myHighlightedStructure.Add (theStructure);
370
371   // Highlight in all defined views
372   for (Graphic3d_IndexedMapOfView::Iterator aViewIt (myDefinedViews); aViewIt.More(); aViewIt.Next())
373   {
374     aViewIt.Value()->Highlight (theStructure);
375   }
376 }
377
378 // ========================================================================
379 // function : UnHighlight
380 // purpose  :
381 // ========================================================================
382 void Graphic3d_StructureManager::UnHighlight (const Handle(Graphic3d_Structure)& theStructure)
383 {
384   myHighlightedStructure.Remove (theStructure);
385
386   // UnHighlight in all defined views
387   for (Graphic3d_IndexedMapOfView::Iterator aViewIt (myDefinedViews); aViewIt.More(); aViewIt.Next())
388   {
389     aViewIt.Value()->UnHighlight (theStructure);
390   }
391 }
392
393 // ========================================================================
394 // function : UnHighlight
395 // purpose  :
396 // ========================================================================
397 void Graphic3d_StructureManager::UnHighlight()
398 {
399   for (Graphic3d_MapIteratorOfMapOfStructure anIt (myHighlightedStructure); anIt.More(); anIt.Next())
400   {
401     anIt.Key()->UnHighlight();
402   }
403 }
404
405 // ========================================================================
406 // function : SetTransform
407 // purpose  :
408 // ========================================================================
409 void Graphic3d_StructureManager::SetTransform (const Handle(Graphic3d_Structure)& theStructure,
410                                                const Handle(Geom_Transformation)& theTrsf)
411 {
412   for (Graphic3d_IndexedMapOfView::Iterator aViewIt (myDefinedViews); aViewIt.More(); aViewIt.Next())
413   {
414     aViewIt.Value()->SetTransform (theStructure, theTrsf);
415   }
416 }
417
418 // ========================================================================
419 // function : ChangeDisplayPriority
420 // purpose  :
421 // ========================================================================
422 void Graphic3d_StructureManager::ChangeDisplayPriority (const Handle(Graphic3d_Structure)& theStructure,
423                                                         const Standard_Integer theOldPriority,
424                                                         const Standard_Integer theNewPriority)
425 {
426   for (Graphic3d_IndexedMapOfView::Iterator aViewIt (myDefinedViews); aViewIt.More(); aViewIt.Next())
427   {
428     aViewIt.Value()->ChangePriority (theStructure, theOldPriority, theNewPriority);
429   }
430 }
431
432 //=======================================================================
433 //function : ChangeZLayer
434 //purpose  :
435 //=======================================================================
436 void Graphic3d_StructureManager::ChangeZLayer (const Handle(Graphic3d_Structure)& theStructure,
437                                                const Graphic3d_ZLayerId           theLayerId)
438 {
439   if (!myDisplayedStructure.Contains (theStructure))
440   {
441     return;
442   }
443
444   for (Graphic3d_IndexedMapOfView::Iterator aViewIt (myDefinedViews); aViewIt.More(); aViewIt.Next())
445   {
446     aViewIt.Value()->ChangeZLayer (theStructure, theLayerId);
447   }
448 }