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