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