1 // Created by: NW,JPB,CAL
2 // Copyright (c) 1991-1999 Matra Datavision
3 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <Graphic3d_StructureManager.hxx>
18 #include <Graphic3d_DataStructureManager.hxx>
19 #include <Graphic3d_GraphicDriver.hxx>
20 #include <Graphic3d_Structure.hxx>
22 IMPLEMENT_STANDARD_RTTIEXT(Graphic3d_StructureManager,Standard_Transient)
24 #include <Graphic3d_MapIteratorOfMapOfStructure.hxx>
25 #include <Graphic3d_CView.hxx>
27 // ========================================================================
28 // function : Graphic3d_StructureManager
30 // ========================================================================
31 Graphic3d_StructureManager::Graphic3d_StructureManager (const Handle(Graphic3d_GraphicDriver)& theDriver)
32 : myViewGenId (0, 31),
33 myGraphicDriver (theDriver),
34 myDeviceLostFlag (Standard_False)
39 // ========================================================================
40 // function : ~Graphic3d_StructureManager
42 // ========================================================================
43 Graphic3d_StructureManager::~Graphic3d_StructureManager()
45 for (Graphic3d_MapIteratorOfMapOfStructure anIt (myDisplayedStructure); anIt.More(); anIt.Next())
47 anIt.Value()->Remove();
50 myDisplayedStructure.Clear();
51 myHighlightedStructure.Clear();
52 myDefinedViews.Clear();
55 // ========================================================================
58 // ========================================================================
59 void Graphic3d_StructureManager::Update (const Graphic3d_ZLayerId theLayerId) const
61 for (Graphic3d_IndexedMapOfView::Iterator aViewIt (myDefinedViews); aViewIt.More(); aViewIt.Next())
63 aViewIt.Value()->Update (theLayerId);
67 // ========================================================================
70 // ========================================================================
71 void Graphic3d_StructureManager::Remove()
73 // clear all structures whilst views are alive for correct GPU memory management
74 for (Graphic3d_MapIteratorOfMapOfStructure anIt (myDisplayedStructure); anIt.More(); anIt.Next())
76 anIt.Value()->Remove();
78 myDisplayedStructure.Clear();
79 myHighlightedStructure.Clear();
81 // clear list of managed views
82 myDefinedViews.Clear();
85 // ========================================================================
88 // ========================================================================
89 void Graphic3d_StructureManager::Erase()
91 for (Graphic3d_MapIteratorOfMapOfStructure anIt (myDisplayedStructure); anIt.More(); anIt.Next())
97 void Graphic3d_StructureManager::DisplayedStructures (Graphic3d_MapOfStructure& SG) const {
99 SG.Assign(myDisplayedStructure);
101 //JMBStandard_Integer Length = myDisplayedStructure.Length ();
103 //JMBfor (Standard_Integer i=1; i<=Length; i++)
104 //JMB SG.Add (myDisplayedStructure.Value (i));
108 Standard_Integer Graphic3d_StructureManager::NumberOfDisplayedStructures () const {
110 Standard_Integer Length = myDisplayedStructure.Extent ();
116 //Handle(Graphic3d_Structure) Graphic3d_StructureManager::DisplayedStructure (const Standard_Integer AnIndex) const {
118 //return (myDisplayedStructure.Value (AnIndex));
122 void Graphic3d_StructureManager::HighlightedStructures (Graphic3d_MapOfStructure& SG) const {
124 SG.Assign(myHighlightedStructure);
128 Handle(Graphic3d_Structure) Graphic3d_StructureManager::Identification (const Standard_Integer AId) const {
130 // Standard_Integer ind=0;
131 Standard_Boolean notfound = Standard_True;
133 Handle(Graphic3d_Structure) StructNull;
135 Graphic3d_MapIteratorOfMapOfStructure it( myDisplayedStructure);
137 Handle(Graphic3d_Structure) SGfound;
139 for (; it.More() && notfound; it.Next()) {
140 Handle(Graphic3d_Structure) SG = it.Key();
141 if ( SG->Identification () == AId) {
142 notfound = Standard_False;
155 const Handle(Graphic3d_GraphicDriver)& Graphic3d_StructureManager::GraphicDriver () const
157 return (myGraphicDriver);
160 void Graphic3d_StructureManager::RecomputeStructures()
162 myDeviceLostFlag = Standard_False;
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())
168 Handle(Graphic3d_Structure) aStructure = anIter.Key();
169 anIter.Key()->Network (anIter.Key().get(), Graphic3d_TOC_DESCENDANT, aStructNetwork);
172 RecomputeStructures (aStructNetwork);
175 void Graphic3d_StructureManager::RecomputeStructures (const NCollection_Map<Graphic3d_Structure*>& theStructures)
177 for (NCollection_Map<Graphic3d_Structure*>::Iterator anIter (theStructures); anIter.More(); anIter.Next())
179 Graphic3d_Structure* aStruct = anIter.Key();
185 Handle(Graphic3d_ViewAffinity) Graphic3d_StructureManager::RegisterObject (const Handle(Standard_Transient)& theObject)
187 Handle(Graphic3d_ViewAffinity) aResult;
188 if (myRegisteredObjects.Find (theObject.operator->(), aResult))
193 aResult = new Graphic3d_ViewAffinity();
194 myRegisteredObjects.Bind (theObject.operator->(), aResult);
198 void Graphic3d_StructureManager::UnregisterObject (const Handle(Standard_Transient)& theObject)
200 myRegisteredObjects.UnBind (theObject.operator->());
203 Handle(Graphic3d_ViewAffinity) Graphic3d_StructureManager::ObjectAffinity (const Handle(Standard_Transient)& theObject) const
205 Handle(Graphic3d_ViewAffinity) aResult;
206 myRegisteredObjects.Find (theObject.operator->(), aResult);
210 // ========================================================================
211 // function : Identification
213 // ========================================================================
214 Standard_Integer Graphic3d_StructureManager::Identification (Graphic3d_CView* theView)
216 if (myDefinedViews.Contains (theView))
218 return theView->Identification();
221 myDefinedViews.Add (theView);
222 return myViewGenId.Next();
225 // ========================================================================
226 // function : UnIdentification
228 // ========================================================================
229 void Graphic3d_StructureManager::UnIdentification (Graphic3d_CView* theView)
231 if (myDefinedViews.Contains (theView))
233 myDefinedViews.Swap (myDefinedViews.FindIndex (theView), myDefinedViews.Size());
234 myDefinedViews.RemoveLast();
235 myViewGenId.Free (theView->Identification());
239 // ========================================================================
240 // function : DefinedViews
242 // ========================================================================
243 const Graphic3d_IndexedMapOfView& Graphic3d_StructureManager::DefinedViews() const
245 return myDefinedViews;
248 // ========================================================================
249 // function : MaxNumOfViews
251 // ========================================================================
252 Standard_Integer Graphic3d_StructureManager::MaxNumOfViews() const
254 return myViewGenId.Upper() - myViewGenId.Lower() + 1;
257 // ========================================================================
258 // function : ReCompute
260 // ========================================================================
261 void Graphic3d_StructureManager::ReCompute (const Handle(Graphic3d_Structure)& theStructure)
263 if (!myDisplayedStructure.Contains (theStructure))
268 // Recompute structure for all defined views
269 for (Standard_Integer aViewIt = 1; aViewIt <= myDefinedViews.Extent(); ++aViewIt)
271 ReCompute (theStructure, myDefinedViews (aViewIt));
275 // ========================================================================
276 // function : ReCompute
278 // ========================================================================
279 void Graphic3d_StructureManager::ReCompute (const Handle(Graphic3d_Structure)& theStructure,
280 const Handle(Graphic3d_DataStructureManager)& theProjector)
282 Handle(Graphic3d_CView) aView = Handle(Graphic3d_CView)::DownCast (theProjector);
285 || !myDefinedViews.Contains (aView.operator->())
286 || !myDisplayedStructure.Contains (theStructure))
291 aView->ReCompute (theStructure);
294 // ========================================================================
297 // ========================================================================
298 void Graphic3d_StructureManager::Clear (Graphic3d_Structure* theStructure,
299 const Standard_Boolean theWithDestruction)
301 for (Graphic3d_IndexedMapOfView::Iterator aViewIt (myDefinedViews); aViewIt.More(); aViewIt.Next())
303 aViewIt.Value()->Clear (theStructure, theWithDestruction);
307 // ========================================================================
308 // function : Connect
310 // ========================================================================
311 void Graphic3d_StructureManager::Connect (const Graphic3d_Structure* theMother,
312 const Graphic3d_Structure* theDaughter)
314 for (Graphic3d_IndexedMapOfView::Iterator aViewIt (myDefinedViews); aViewIt.More(); aViewIt.Next())
316 aViewIt.Value()->Connect (theMother, theDaughter);
320 // ========================================================================
321 // function : Disconnect
323 // ========================================================================
324 void Graphic3d_StructureManager::Disconnect (const Graphic3d_Structure* theMother,
325 const Graphic3d_Structure* theDaughter)
327 for (Graphic3d_IndexedMapOfView::Iterator aViewIt (myDefinedViews); aViewIt.More(); aViewIt.Next())
329 aViewIt.Value()->Disconnect (theMother, theDaughter);
333 // ========================================================================
334 // function : Display
336 // ========================================================================
337 void Graphic3d_StructureManager::Display (const Handle(Graphic3d_Structure)& theStructure)
339 myDisplayedStructure.Add (theStructure);
341 for (Graphic3d_IndexedMapOfView::Iterator aViewIt (myDefinedViews); aViewIt.More(); aViewIt.Next())
343 aViewIt.Value()->Display (theStructure);
347 // ========================================================================
350 // ========================================================================
351 void Graphic3d_StructureManager::Erase (const Handle(Graphic3d_Structure)& theStructure)
353 myDisplayedStructure .Remove(theStructure);
354 myHighlightedStructure.Remove (theStructure);
356 // Erase structure in all defined views
357 for (Graphic3d_IndexedMapOfView::Iterator aViewIt (myDefinedViews); aViewIt.More(); aViewIt.Next())
359 aViewIt.Value()->Erase (theStructure);
363 // ========================================================================
366 // ========================================================================
367 void Graphic3d_StructureManager::Highlight (const Handle(Graphic3d_Structure)& theStructure)
369 myHighlightedStructure.Add (theStructure);
371 // Highlight in all defined views
372 for (Graphic3d_IndexedMapOfView::Iterator aViewIt (myDefinedViews); aViewIt.More(); aViewIt.Next())
374 aViewIt.Value()->Highlight (theStructure);
378 // ========================================================================
379 // function : UnHighlight
381 // ========================================================================
382 void Graphic3d_StructureManager::UnHighlight (const Handle(Graphic3d_Structure)& theStructure)
384 myHighlightedStructure.Remove (theStructure);
386 // UnHighlight in all defined views
387 for (Graphic3d_IndexedMapOfView::Iterator aViewIt (myDefinedViews); aViewIt.More(); aViewIt.Next())
389 aViewIt.Value()->UnHighlight (theStructure);
393 // ========================================================================
394 // function : UnHighlight
396 // ========================================================================
397 void Graphic3d_StructureManager::UnHighlight()
399 for (Graphic3d_MapIteratorOfMapOfStructure anIt (myHighlightedStructure); anIt.More(); anIt.Next())
401 anIt.Key()->UnHighlight();
405 // ========================================================================
406 // function : SetTransform
408 // ========================================================================
409 void Graphic3d_StructureManager::SetTransform (const Handle(Graphic3d_Structure)& theStructure,
410 const Handle(Geom_Transformation)& theTrsf)
412 for (Graphic3d_IndexedMapOfView::Iterator aViewIt (myDefinedViews); aViewIt.More(); aViewIt.Next())
414 aViewIt.Value()->SetTransform (theStructure, theTrsf);
418 // ========================================================================
419 // function : ChangeDisplayPriority
421 // ========================================================================
422 void Graphic3d_StructureManager::ChangeDisplayPriority (const Handle(Graphic3d_Structure)& theStructure,
423 const Standard_Integer theOldPriority,
424 const Standard_Integer theNewPriority)
426 for (Graphic3d_IndexedMapOfView::Iterator aViewIt (myDefinedViews); aViewIt.More(); aViewIt.Next())
428 aViewIt.Value()->ChangePriority (theStructure, theOldPriority, theNewPriority);
432 //=======================================================================
433 //function : ChangeZLayer
435 //=======================================================================
436 void Graphic3d_StructureManager::ChangeZLayer (const Handle(Graphic3d_Structure)& theStructure,
437 const Graphic3d_ZLayerId theLayerId)
439 if (!myDisplayedStructure.Contains (theStructure))
444 for (Graphic3d_IndexedMapOfView::Iterator aViewIt (myDefinedViews); aViewIt.More(); aViewIt.Next())
446 aViewIt.Value()->ChangeZLayer (theStructure, theLayerId);