0030675: Visualization - remove redundant proxy classes in hierarchy of PrsMgr_Presen...
[occt.git] / src / Graphic3d / Graphic3d_StructureManager.cxx
CommitLineData
b311480e 1// Created by: NW,JPB,CAL
2// Copyright (c) 1991-1999 Matra Datavision
973c2be1 3// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 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
973c2be1 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
7fd59977 15
59ec40f8 16#include <Graphic3d_StructureManager.hxx>
17
42cf5bc1 18#include <Graphic3d_DataStructureManager.hxx>
19#include <Graphic3d_GraphicDriver.hxx>
42cf5bc1 20#include <Graphic3d_Structure.hxx>
42cf5bc1 21
25e59720 22IMPLEMENT_STANDARD_RTTIEXT(Graphic3d_StructureManager,Standard_Transient)
92efcf78 23
7fd59977 24#include <Graphic3d_MapIteratorOfMapOfStructure.hxx>
92efcf78 25#include <Graphic3d_CView.hxx>
7fd59977 26
c357e426 27// ========================================================================
59ec40f8 28// function : Graphic3d_StructureManager
c357e426 29// purpose :
30// ========================================================================
59ec40f8 31Graphic3d_StructureManager::Graphic3d_StructureManager (const Handle(Graphic3d_GraphicDriver)& theDriver)
32: myViewGenId (0, 31),
851dacdb 33 myGraphicDriver (theDriver),
34 myDeviceLostFlag (Standard_False)
c357e426 35{
59ec40f8 36 //
c357e426 37}
7fd59977 38
c357e426 39// ========================================================================
59ec40f8 40// function : ~Graphic3d_StructureManager
c357e426 41// purpose :
42// ========================================================================
59ec40f8 43Graphic3d_StructureManager::~Graphic3d_StructureManager()
c357e426 44{
b4fd9d00 45 for (Graphic3d_MapIteratorOfMapOfStructure anIt (myDisplayedStructure); anIt.More(); anIt.Next())
46 {
47 anIt.Value()->Remove();
48 }
49
59ec40f8 50 myDisplayedStructure.Clear();
51 myHighlightedStructure.Clear();
52 myDefinedViews.Clear();
c357e426 53}
7fd59977 54
c357e426 55// ========================================================================
56// function : Update
57// purpose :
58// ========================================================================
cfece3ef 59void Graphic3d_StructureManager::Update (const Graphic3d_ZLayerId theLayerId) const
c357e426 60{
61 for (Graphic3d_IndexedMapOfView::Iterator aViewIt (myDefinedViews); aViewIt.More(); aViewIt.Next())
62 {
cfece3ef 63 aViewIt.Value()->Update (theLayerId);
c357e426 64 }
7fd59977 65}
66
c357e426 67// ========================================================================
68// function : Remove
69// purpose :
70// ========================================================================
71void Graphic3d_StructureManager::Remove()
72{
73 // clear all structures whilst views are alive for correct GPU memory management
b4fd9d00 74 for (Graphic3d_MapIteratorOfMapOfStructure anIt (myDisplayedStructure); anIt.More(); anIt.Next())
75 {
76 anIt.Value()->Remove();
77 }
c357e426 78 myDisplayedStructure.Clear();
79 myHighlightedStructure.Clear();
7fd59977 80
c357e426 81 // clear list of managed views
82 myDefinedViews.Clear();
83}
7fd59977 84
c357e426 85// ========================================================================
86// function : Erase
87// purpose :
88// ========================================================================
89void Graphic3d_StructureManager::Erase()
90{
91 for (Graphic3d_MapIteratorOfMapOfStructure anIt (myDisplayedStructure); anIt.More(); anIt.Next())
92 {
93 anIt.Key()->Erase();
94 }
7fd59977 95}
96
7fd59977 97void Graphic3d_StructureManager::DisplayedStructures (Graphic3d_MapOfStructure& SG) const {
98
c357e426 99 SG.Assign(myDisplayedStructure);
7fd59977 100
c357e426 101 //JMBStandard_Integer Length = myDisplayedStructure.Length ();
7fd59977 102
103 //JMBfor (Standard_Integer i=1; i<=Length; i++)
c357e426 104 //JMB SG.Add (myDisplayedStructure.Value (i));
7fd59977 105
106}
107
108Standard_Integer Graphic3d_StructureManager::NumberOfDisplayedStructures () const {
109
c357e426 110Standard_Integer Length = myDisplayedStructure.Extent ();
7fd59977 111
112 return (Length);
113
114}
115
116//Handle(Graphic3d_Structure) Graphic3d_StructureManager::DisplayedStructure (const Standard_Integer AnIndex) const {
117
c357e426 118//return (myDisplayedStructure.Value (AnIndex));
7fd59977 119
120//}
121
122void Graphic3d_StructureManager::HighlightedStructures (Graphic3d_MapOfStructure& SG) const {
123
c357e426 124 SG.Assign(myHighlightedStructure);
7fd59977 125
126}
127
7fd59977 128Handle(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
c357e426 135 Graphic3d_MapIteratorOfMapOfStructure it( myDisplayedStructure);
7fd59977 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
016e5959 155const Handle(Graphic3d_GraphicDriver)& Graphic3d_StructureManager::GraphicDriver () const
156{
157 return (myGraphicDriver);
7fd59977 158}
eb4320f2 159
8bb96a97 160void Graphic3d_StructureManager::RecomputeStructures()
eb4320f2 161{
851dacdb 162 myDeviceLostFlag = Standard_False;
163
8bb96a97 164 // Go through all unique structures including child (connected) ones and ensure that they are computed.
7dd7c146 165 NCollection_Map<Graphic3d_Structure*> aStructNetwork;
c357e426 166 for (Graphic3d_MapIteratorOfMapOfStructure anIter(myDisplayedStructure); anIter.More(); anIter.Next())
eb4320f2 167 {
168 Handle(Graphic3d_Structure) aStructure = anIter.Key();
7dd7c146 169 anIter.Key()->Network (anIter.Key().get(), Graphic3d_TOC_DESCENDANT, aStructNetwork);
8bb96a97 170 }
171
172 RecomputeStructures (aStructNetwork);
173}
eb4320f2 174
7dd7c146 175void Graphic3d_StructureManager::RecomputeStructures (const NCollection_Map<Graphic3d_Structure*>& theStructures)
8bb96a97 176{
7dd7c146 177 for (NCollection_Map<Graphic3d_Structure*>::Iterator anIter (theStructures); anIter.More(); anIter.Next())
8bb96a97 178 {
7dd7c146 179 Graphic3d_Structure* aStruct = anIter.Key();
8bb96a97 180 aStruct->Clear();
181 aStruct->Compute();
eb4320f2 182 }
183}
a272ed94 184
185Handle(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
198void Graphic3d_StructureManager::UnregisterObject (const Handle(Standard_Transient)& theObject)
199{
200 myRegisteredObjects.UnBind (theObject.operator->());
201}
202
203Handle(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}
c357e426 209
210// ========================================================================
211// function : Identification
212// purpose :
213// ========================================================================
214Standard_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// ========================================================================
229void 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// ========================================================================
243const Graphic3d_IndexedMapOfView& Graphic3d_StructureManager::DefinedViews() const
244{
245 return myDefinedViews;
246}
247
248// ========================================================================
249// function : MaxNumOfViews
250// purpose :
251// ========================================================================
252Standard_Integer Graphic3d_StructureManager::MaxNumOfViews() const
253{
254 return myViewGenId.Upper() - myViewGenId.Lower() + 1;
255}
256
257// ========================================================================
258// function : ReCompute
259// purpose :
260// ========================================================================
261void 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// ========================================================================
279void 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// ========================================================================
7dd7c146 298void Graphic3d_StructureManager::Clear (Graphic3d_Structure* theStructure,
c357e426 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// ========================================================================
7dd7c146 311void Graphic3d_StructureManager::Connect (const Graphic3d_Structure* theMother,
312 const Graphic3d_Structure* theDaughter)
c357e426 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// ========================================================================
7dd7c146 324void Graphic3d_StructureManager::Disconnect (const Graphic3d_Structure* theMother,
325 const Graphic3d_Structure* theDaughter)
c357e426 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// ========================================================================
337void 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// ========================================================================
351void 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// ========================================================================
8e5fb5ea 367void Graphic3d_StructureManager::Highlight (const Handle(Graphic3d_Structure)& theStructure)
c357e426 368{
369 myHighlightedStructure.Add (theStructure);
370
371 // Highlight in all defined views
372 for (Graphic3d_IndexedMapOfView::Iterator aViewIt (myDefinedViews); aViewIt.More(); aViewIt.Next())
373 {
8e5fb5ea 374 aViewIt.Value()->Highlight (theStructure);
c357e426 375 }
376}
377
378// ========================================================================
379// function : UnHighlight
380// purpose :
381// ========================================================================
382void 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// ========================================================================
397void 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// ========================================================================
409void Graphic3d_StructureManager::SetTransform (const Handle(Graphic3d_Structure)& theStructure,
1f7f5a90 410 const Handle(Geom_Transformation)& theTrsf)
c357e426 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// ========================================================================
422void 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//=======================================================================
436void 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}