a32ab8f5e0a88da62fcf96f9c40f5afd3535a678
[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 //-Version      
17 //-Design       Declaration of variables specific to managers
18 //-Warning      Manager manages a set of structures
19 //-References   
20 //-Language     C++ 2.0
21 //-Declarations
22 // for the class
23
24 #include <Graphic3d_AspectFillArea3d.hxx>
25 #include <Graphic3d_AspectLine3d.hxx>
26 #include <Graphic3d_AspectMarker3d.hxx>
27 #include <Graphic3d_AspectText3d.hxx>
28 #include <Graphic3d_DataStructureManager.hxx>
29 #include <Graphic3d_GraphicDriver.hxx>
30 #include <Graphic3d_InitialisationError.hxx>
31 #include <Graphic3d_Structure.hxx>
32 #include <Graphic3d_StructureManager.hxx>
33 #include <Standard_Transient.hxx>
34 #include <Standard_Type.hxx>
35
36 IMPLEMENT_STANDARD_RTTIEXT(Graphic3d_StructureManager,MMgt_TShared)
37
38 #include <Graphic3d_MapIteratorOfMapOfStructure.hxx>
39 #include <Graphic3d_CView.hxx>
40
41 Graphic3d_StructureManager::Graphic3d_StructureManager (const Handle(Graphic3d_GraphicDriver)& theDriver)
42 : myViewGenId (0, 31)
43 {
44   myAspectLine3d          = new Graphic3d_AspectLine3d ();
45   myAspectText3d          = new Graphic3d_AspectText3d ();
46   myAspectMarker3d        = new Graphic3d_AspectMarker3d ();
47   myAspectFillArea3d      = new Graphic3d_AspectFillArea3d ();
48
49   myUpdateMode            = Aspect_TOU_WAIT;
50   myGraphicDriver         = theDriver;
51
52 }
53
54 //-Destructors
55
56 Graphic3d_StructureManager::~Graphic3d_StructureManager ()
57 {
58   myDisplayedStructure.Clear ();
59   myHighlightedStructure.Clear ();
60   myDefinedViews.Clear();
61
62 }
63
64 // ========================================================================
65 // function : SetUpdateMode
66 // purpose  :
67 // ========================================================================
68 void Graphic3d_StructureManager::SetUpdateMode (const Aspect_TypeOfUpdate theType)
69 {
70   myUpdateMode = theType;
71 }
72
73 // ========================================================================
74 // function : UpdateMode
75 // purpose  :
76 // ========================================================================
77 Aspect_TypeOfUpdate Graphic3d_StructureManager::UpdateMode() const
78 {
79   return myUpdateMode;
80 }
81
82 // ========================================================================
83 // function : Update
84 // purpose  :
85 // ========================================================================
86 void Graphic3d_StructureManager::Update (const Aspect_TypeOfUpdate theMode) const
87 {
88   for (Graphic3d_IndexedMapOfView::Iterator aViewIt (myDefinedViews); aViewIt.More(); aViewIt.Next())
89   {
90     aViewIt.Value()->Update (theMode);
91   }
92 }
93
94 // ========================================================================
95 // function : Remove
96 // purpose  :
97 // ========================================================================
98 void Graphic3d_StructureManager::Remove()
99 {
100   // clear all structures whilst views are alive for correct GPU memory management
101   myDisplayedStructure.Clear();
102   myHighlightedStructure.Clear();
103
104   // clear list of managed views
105   myDefinedViews.Clear();
106 }
107
108 // ========================================================================
109 // function : Erase
110 // purpose  :
111 // ========================================================================
112 void Graphic3d_StructureManager::Erase()
113 {
114   for (Graphic3d_MapIteratorOfMapOfStructure anIt (myDisplayedStructure); anIt.More(); anIt.Next())
115   {
116     anIt.Key()->Erase();
117   }
118 }
119
120
121
122
123
124 void Graphic3d_StructureManager::SetPrimitivesAspect (const Handle(Graphic3d_AspectLine3d)& CTX) {
125
126         myAspectLine3d          = CTX;
127
128 }
129
130 void Graphic3d_StructureManager::SetPrimitivesAspect (const Handle(Graphic3d_AspectFillArea3d)& CTX) {
131
132         myAspectFillArea3d      = CTX;
133
134 }
135
136 void Graphic3d_StructureManager::SetPrimitivesAspect (const Handle(Graphic3d_AspectText3d)& CTX) {
137
138         myAspectText3d          = CTX;
139
140 }
141
142 void Graphic3d_StructureManager::SetPrimitivesAspect (const Handle(Graphic3d_AspectMarker3d)& CTX) {
143
144         myAspectMarker3d        = CTX;
145
146 }
147
148 void Graphic3d_StructureManager::PrimitivesAspect (Handle(Graphic3d_AspectLine3d)& CTXL, Handle(Graphic3d_AspectText3d)& CTXT, Handle(Graphic3d_AspectMarker3d)& CTXM, Handle(Graphic3d_AspectFillArea3d)& CTXF) const {
149
150         CTXL    = myAspectLine3d;
151         CTXT    = myAspectText3d;
152         CTXM    = myAspectMarker3d;
153         CTXF    = myAspectFillArea3d;
154
155 }
156
157 Handle(Graphic3d_AspectLine3d) Graphic3d_StructureManager::Line3dAspect () const {
158
159         return (myAspectLine3d);
160
161 }
162
163 Handle(Graphic3d_AspectText3d) Graphic3d_StructureManager::Text3dAspect () const {
164
165         return (myAspectText3d);
166
167 }
168
169 Handle(Graphic3d_AspectMarker3d) Graphic3d_StructureManager::Marker3dAspect () const {
170
171         return (myAspectMarker3d);
172
173 }
174
175 Handle(Graphic3d_AspectFillArea3d) Graphic3d_StructureManager::FillArea3dAspect () const {
176
177         return (myAspectFillArea3d);
178
179 }
180
181 void Graphic3d_StructureManager::DisplayedStructures (Graphic3d_MapOfStructure& SG) const {
182
183   SG.Assign(myDisplayedStructure);
184
185   //JMBStandard_Integer Length  = myDisplayedStructure.Length ();
186
187   //JMBfor (Standard_Integer i=1; i<=Length; i++)
188   //JMB SG.Add (myDisplayedStructure.Value (i));
189
190 }
191
192 Standard_Integer Graphic3d_StructureManager::NumberOfDisplayedStructures () const {
193
194 Standard_Integer Length = myDisplayedStructure.Extent ();
195
196         return (Length);
197
198 }
199
200 //Handle(Graphic3d_Structure) Graphic3d_StructureManager::DisplayedStructure (const Standard_Integer AnIndex) const {
201
202 //return (myDisplayedStructure.Value (AnIndex));
203
204 //}
205
206 void Graphic3d_StructureManager::HighlightedStructures (Graphic3d_MapOfStructure& SG) const {
207
208   SG.Assign(myHighlightedStructure);
209
210 }
211
212 Handle(Graphic3d_Structure) Graphic3d_StructureManager::Identification (const Standard_Integer AId) const {
213
214 //  Standard_Integer ind=0;
215   Standard_Boolean notfound     = Standard_True;
216
217   Handle(Graphic3d_Structure) StructNull;
218  
219   Graphic3d_MapIteratorOfMapOfStructure it( myDisplayedStructure);
220   
221   Handle(Graphic3d_Structure) SGfound;
222
223   for (; it.More() && notfound; it.Next()) {
224     Handle(Graphic3d_Structure) SG = it.Key();
225     if ( SG->Identification () == AId) {
226       notfound  = Standard_False;
227       SGfound = SG;
228     }
229   }
230
231
232   if (notfound)
233     return (StructNull);
234   else
235     return (SGfound);
236
237 }
238
239 const Handle(Graphic3d_GraphicDriver)& Graphic3d_StructureManager::GraphicDriver () const 
240 {
241   return (myGraphicDriver);
242 }
243
244 void Graphic3d_StructureManager::RecomputeStructures()
245 {
246   // Go through all unique structures including child (connected) ones and ensure that they are computed.
247   Graphic3d_MapOfStructure aStructNetwork;
248
249   for (Graphic3d_MapIteratorOfMapOfStructure anIter(myDisplayedStructure); anIter.More(); anIter.Next())
250   {
251     Handle(Graphic3d_Structure) aStructure = anIter.Key();
252     anIter.Key()->Network (anIter.Key(), Graphic3d_TOC_DESCENDANT, aStructNetwork);
253   }
254
255   RecomputeStructures (aStructNetwork);
256 }
257
258 void Graphic3d_StructureManager::RecomputeStructures (const Graphic3d_MapOfStructure& theStructures)
259 {
260   for (Graphic3d_MapIteratorOfMapOfStructure anIter (theStructures); anIter.More(); anIter.Next())
261   {
262     Handle(Graphic3d_Structure) aStruct = anIter.Key();
263     aStruct->Clear();
264     aStruct->Compute();
265   }
266 }
267
268 Handle(Graphic3d_ViewAffinity) Graphic3d_StructureManager::RegisterObject (const Handle(Standard_Transient)& theObject)
269 {
270   Handle(Graphic3d_ViewAffinity) aResult;
271   if (myRegisteredObjects.Find (theObject.operator->(), aResult))
272   {
273     return aResult;
274   }
275
276   aResult = new Graphic3d_ViewAffinity();
277   myRegisteredObjects.Bind (theObject.operator->(), aResult);
278   return aResult;
279 }
280
281 void Graphic3d_StructureManager::UnregisterObject (const Handle(Standard_Transient)& theObject)
282 {
283   myRegisteredObjects.UnBind (theObject.operator->());
284 }
285
286 Handle(Graphic3d_ViewAffinity) Graphic3d_StructureManager::ObjectAffinity (const Handle(Standard_Transient)& theObject) const
287 {
288   Handle(Graphic3d_ViewAffinity) aResult;
289   myRegisteredObjects.Find (theObject.operator->(), aResult);
290   return aResult;
291 }
292
293 // ========================================================================
294 // function : Identification
295 // purpose  :
296 // ========================================================================
297 Standard_Integer Graphic3d_StructureManager::Identification (Graphic3d_CView* theView)
298 {
299   if (myDefinedViews.Contains (theView))
300   {
301     return theView->Identification();
302   }
303
304   myDefinedViews.Add (theView);
305   return myViewGenId.Next();
306 }
307
308 // ========================================================================
309 // function : UnIdentification
310 // purpose  :
311 // ========================================================================
312 void Graphic3d_StructureManager::UnIdentification (Graphic3d_CView* theView)
313 {
314   if (myDefinedViews.Contains (theView))
315   {
316     myDefinedViews.Swap (myDefinedViews.FindIndex (theView), myDefinedViews.Size());
317     myDefinedViews.RemoveLast();
318     myViewGenId.Free (theView->Identification());
319   }
320 }
321
322 // ========================================================================
323 // function : DefinedViews
324 // purpose  :
325 // ========================================================================
326 const Graphic3d_IndexedMapOfView& Graphic3d_StructureManager::DefinedViews() const
327 {
328   return myDefinedViews;
329 }
330
331 // ========================================================================
332 // function : MaxNumOfViews
333 // purpose  :
334 // ========================================================================
335 Standard_Integer Graphic3d_StructureManager::MaxNumOfViews() const
336 {
337   return myViewGenId.Upper() - myViewGenId.Lower() + 1;
338 }
339
340 // ========================================================================
341 // function : ReCompute
342 // purpose  :
343 // ========================================================================
344 void Graphic3d_StructureManager::ReCompute (const Handle(Graphic3d_Structure)& theStructure)
345 {
346   if (!myDisplayedStructure.Contains (theStructure))
347   {
348     return;
349   }
350
351   // Recompute structure for all defined views
352   for (Standard_Integer aViewIt = 1; aViewIt <= myDefinedViews.Extent(); ++aViewIt)
353   {
354     ReCompute (theStructure, myDefinedViews (aViewIt));
355   }
356 }
357
358 // ========================================================================
359 // function : ReCompute
360 // purpose  :
361 // ========================================================================
362 void Graphic3d_StructureManager::ReCompute (const Handle(Graphic3d_Structure)& theStructure,
363                                             const Handle(Graphic3d_DataStructureManager)& theProjector)
364 {
365   Handle(Graphic3d_CView) aView = Handle(Graphic3d_CView)::DownCast (theProjector);
366
367   if (aView.IsNull()
368    || !myDefinedViews.Contains (aView.operator->())
369    || !myDisplayedStructure.Contains (theStructure))
370   {
371     return;
372   }
373
374   aView->ReCompute (theStructure);
375 }
376
377 // ========================================================================
378 // function : Clear
379 // purpose  :
380 // ========================================================================
381 void Graphic3d_StructureManager::Clear (const Handle(Graphic3d_Structure)& theStructure,
382                                         const Standard_Boolean theWithDestruction)
383 {
384   for (Graphic3d_IndexedMapOfView::Iterator aViewIt (myDefinedViews); aViewIt.More(); aViewIt.Next())
385   {
386     aViewIt.Value()->Clear (theStructure, theWithDestruction);
387   }
388 }
389
390 // ========================================================================
391 // function : Connect
392 // purpose  :
393 // ========================================================================
394 void Graphic3d_StructureManager::Connect (const Handle(Graphic3d_Structure)& theMother,
395                                           const Handle(Graphic3d_Structure)& theDaughter)
396 {
397   for (Graphic3d_IndexedMapOfView::Iterator aViewIt (myDefinedViews); aViewIt.More(); aViewIt.Next())
398   {
399     aViewIt.Value()->Connect (theMother, theDaughter);
400   }
401 }
402
403 // ========================================================================
404 // function : Disconnect
405 // purpose  :
406 // ========================================================================
407 void Graphic3d_StructureManager::Disconnect (const Handle(Graphic3d_Structure)& theMother,
408                                              const Handle(Graphic3d_Structure)& theDaughter)
409 {
410   for (Graphic3d_IndexedMapOfView::Iterator aViewIt (myDefinedViews); aViewIt.More(); aViewIt.Next())
411   {
412     aViewIt.Value()->Disconnect (theMother, theDaughter);
413   }
414 }
415
416 // ========================================================================
417 // function : Display
418 // purpose  :
419 // ========================================================================
420 void Graphic3d_StructureManager::Display (const Handle(Graphic3d_Structure)& theStructure)
421 {
422   myDisplayedStructure.Add (theStructure);
423
424   for (Graphic3d_IndexedMapOfView::Iterator aViewIt (myDefinedViews); aViewIt.More(); aViewIt.Next())
425   {
426     aViewIt.Value()->Display (theStructure);
427   }
428 }
429
430 // ========================================================================
431 // function : Erase
432 // purpose  :
433 // ========================================================================
434 void Graphic3d_StructureManager::Erase (const Handle(Graphic3d_Structure)& theStructure)
435 {
436   myDisplayedStructure  .Remove(theStructure);
437   myHighlightedStructure.Remove (theStructure);
438
439   // Erase structure in all defined views
440   for (Graphic3d_IndexedMapOfView::Iterator aViewIt (myDefinedViews); aViewIt.More(); aViewIt.Next())
441   {
442     aViewIt.Value()->Erase (theStructure);
443   }
444 }
445
446 // ========================================================================
447 // function : Erase
448 // purpose  :
449 // ========================================================================
450 void Graphic3d_StructureManager::Highlight (const Handle(Graphic3d_Structure)& theStructure,
451                                             const Aspect_TypeOfHighlightMethod theMethod)
452 {
453   myHighlightedStructure.Add (theStructure);
454
455   // Highlight in all defined views
456   for (Graphic3d_IndexedMapOfView::Iterator aViewIt (myDefinedViews); aViewIt.More(); aViewIt.Next())
457   {
458     aViewIt.Value()->Highlight (theStructure, theMethod);
459   }
460 }
461
462 // ========================================================================
463 // function : UnHighlight
464 // purpose  :
465 // ========================================================================
466 void Graphic3d_StructureManager::UnHighlight (const Handle(Graphic3d_Structure)& theStructure)
467 {
468   myHighlightedStructure.Remove (theStructure);
469
470   // UnHighlight in all defined views
471   for (Graphic3d_IndexedMapOfView::Iterator aViewIt (myDefinedViews); aViewIt.More(); aViewIt.Next())
472   {
473     aViewIt.Value()->UnHighlight (theStructure);
474   }
475 }
476
477 // ========================================================================
478 // function : UnHighlight
479 // purpose  :
480 // ========================================================================
481 void Graphic3d_StructureManager::UnHighlight()
482 {
483   for (Graphic3d_MapIteratorOfMapOfStructure anIt (myHighlightedStructure); anIt.More(); anIt.Next())
484   {
485     anIt.Key()->UnHighlight();
486   }
487 }
488
489 // ========================================================================
490 // function : SetTransform
491 // purpose  :
492 // ========================================================================
493 void Graphic3d_StructureManager::SetTransform (const Handle(Graphic3d_Structure)& theStructure,
494                                                const TColStd_Array2OfReal& theTrsf)
495 {
496   for (Graphic3d_IndexedMapOfView::Iterator aViewIt (myDefinedViews); aViewIt.More(); aViewIt.Next())
497   {
498     aViewIt.Value()->SetTransform (theStructure, theTrsf);
499   }
500 }
501
502 // ========================================================================
503 // function : ChangeDisplayPriority
504 // purpose  :
505 // ========================================================================
506 void Graphic3d_StructureManager::ChangeDisplayPriority (const Handle(Graphic3d_Structure)& theStructure,
507                                                         const Standard_Integer theOldPriority,
508                                                         const Standard_Integer theNewPriority)
509 {
510   for (Graphic3d_IndexedMapOfView::Iterator aViewIt (myDefinedViews); aViewIt.More(); aViewIt.Next())
511   {
512     aViewIt.Value()->ChangePriority (theStructure, theOldPriority, theNewPriority);
513   }
514 }
515
516 //=======================================================================
517 //function : ChangeZLayer
518 //purpose  :
519 //=======================================================================
520 void Graphic3d_StructureManager::ChangeZLayer (const Handle(Graphic3d_Structure)& theStructure,
521                                                const Graphic3d_ZLayerId           theLayerId)
522 {
523   if (!myDisplayedStructure.Contains (theStructure))
524   {
525     return;
526   }
527
528   for (Graphic3d_IndexedMapOfView::Iterator aViewIt (myDefinedViews); aViewIt.More(); aViewIt.Next())
529   {
530     aViewIt.Value()->ChangeZLayer (theStructure, theLayerId);
531   }
532 }