1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 /***********************************************************************
19 Classe Visual3d_ViewManager.cxx :
21 Declaration of variables specific to visualisers
23 HISTORIQUE DES MODIFICATIONS :
24 --------------------------------
25 Mars 1992 : NW,JPB,CAL ; Creation.
26 19-06-96 : FMN ; Suppression variables inutiles
27 04-02-97 : FMN ; Suppression de PSOutput, XWDOutput ...
28 06-05-97 : CAL ; Ajout du Clear sur les TOS_COMPUTED.
29 19-09-97 : CAL ; Remplacement de Window->Position par Window->Size;
30 24-10-97 : CAL ; Retrait de DownCast.
31 20-11-97 : CAL ; Disparition de la dependance avec math
32 01-12-97 : CAL ; Retrait du test IsActive sur l'Update et le Redraw
33 31-12-97 : CAL ; Disparition de MathGra
34 16-01-98 : CAL ; Ajout du SetTransform sur une TOS_COMPUTED
35 11-03-98 : CAL ; Visual3d_ViewManager::Remove ()
36 20-05-98 : CAL ; Perfs. Connection entre structures COMPUTED.
37 10-06-98 : CAL ; Modification des signatures de xxProjectRaster.
38 10-06-98 : CAL ; Modification de la signature de ViewExists.
39 01-12-98 : CAL ; S4062. Ajout des layers.
40 02-12-98 : CAL ; Remove () ne detruit plus les vues.
42 ************************************************************************/
44 /*----------------------------------------------------------------------*/
52 /*----------------------------------------------------------------------*/
58 #include <Aspect_IdentDefinitionError.hxx>
59 #include <Aspect_Window.hxx>
60 #include <Graphic3d_DataStructureManager.hxx>
61 #include <Graphic3d_GraphicDriver.hxx>
62 #include <Graphic3d_MapIteratorOfMapOfStructure.hxx>
63 #include <Graphic3d_MapOfStructure.hxx>
64 #include <Graphic3d_Structure.hxx>
65 #include <Standard_ErrorHandler.hxx>
66 #include <Standard_Type.hxx>
67 #include <Visual3d_View.hxx>
68 #include <Visual3d_ViewManager.hxx>
70 #if defined (_WIN32) || defined(__WIN32__)
71 # include <WNT_Window.hxx>
72 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
73 # include <Cocoa_Window.hxx>
75 # include <Xw_Window.hxx>
76 #include <Visual3d_View.hxx>
79 // views identifiers : possible range
81 #define View_IDMAX 10000
83 Visual3d_ViewManager::Visual3d_ViewManager (const Handle(Graphic3d_GraphicDriver)& theDriver):
84 Graphic3d_StructureManager (theDriver),
87 MyZBufferAuto (Standard_False),
88 myZLayerGenId (1, IntegerLast())
90 MyGraphicDriver = theDriver;
92 // default layers are always presented in display layer sequence it can not be removed
93 Graphic3d_ZLayerSettings anUnderlaySettings;
94 anUnderlaySettings.Flags = 0;
95 anUnderlaySettings.IsImmediate = false;
96 myLayerIds.Add (Graphic3d_ZLayerId_BotOSD);
97 myLayerSeq.Append (Graphic3d_ZLayerId_BotOSD);
98 myMapOfZLayerSettings.Bind (Graphic3d_ZLayerId_BotOSD, anUnderlaySettings);
100 Graphic3d_ZLayerSettings aDefSettings;
101 aDefSettings.Flags = Graphic3d_ZLayerDepthTest
102 | Graphic3d_ZLayerDepthWrite;
103 aDefSettings.IsImmediate = false;
104 myLayerIds.Add (Graphic3d_ZLayerId_Default);
105 myLayerSeq.Append (Graphic3d_ZLayerId_Default);
106 myMapOfZLayerSettings.Bind (Graphic3d_ZLayerId_Default, aDefSettings);
108 Graphic3d_ZLayerSettings aTopSettings;
109 aTopSettings.Flags = Graphic3d_ZLayerDepthTest
110 | Graphic3d_ZLayerDepthWrite;
111 aTopSettings.IsImmediate = true;
112 myLayerIds.Add (Graphic3d_ZLayerId_Top);
113 myLayerSeq.Append (Graphic3d_ZLayerId_Top);
114 myMapOfZLayerSettings.Bind (Graphic3d_ZLayerId_Top, aTopSettings);
116 Graphic3d_ZLayerSettings aTopmostSettings;
117 aTopmostSettings.Flags = Graphic3d_ZLayerDepthTest
118 | Graphic3d_ZLayerDepthWrite
119 | Graphic3d_ZLayerDepthClear;
120 aTopmostSettings.IsImmediate = true;
121 myLayerIds.Add (Graphic3d_ZLayerId_Topmost);
122 myLayerSeq.Append (Graphic3d_ZLayerId_Topmost);
123 myMapOfZLayerSettings.Bind (Graphic3d_ZLayerId_Topmost, aTopmostSettings);
125 Graphic3d_ZLayerSettings anOsdSettings;
126 anOsdSettings.Flags = 0;
127 anOsdSettings.IsImmediate = true;
128 myLayerIds.Add (Graphic3d_ZLayerId_TopOSD);
129 myLayerSeq.Append (Graphic3d_ZLayerId_TopOSD);
130 myMapOfZLayerSettings.Bind (Graphic3d_ZLayerId_TopOSD, anOsdSettings);
135 void Visual3d_ViewManager::Destroy () {
142 void Visual3d_ViewManager::Remove () {
145 // Destroy all defined views
148 // clear all structures whilst views are alive for correct GPU memory management
149 MyDisplayedStructure.Clear();
150 MyHighlightedStructure.Clear();
152 // clear list of managed views
153 MyDefinedView.Clear();
156 void Visual3d_ViewManager::ChangeDisplayPriority (const Handle(Graphic3d_Structure)& AStructure, const Standard_Integer OldPriority, const Standard_Integer NewPriority)
159 // Change structure priority in all defined views
161 for(int i=1; i<=MyDefinedView.Length(); i++)
163 (MyDefinedView.Value(i))->ChangeDisplayPriority(AStructure, OldPriority, NewPriority);
167 void Visual3d_ViewManager::ReCompute (const Handle(Graphic3d_Structure)& AStructure) {
169 //Standard_Integer LengthD = MyDisplayedStructure.Extent() ();
171 // Even if physically the structure cannot
172 // be displayed (pb of visualisation type)
173 // it has status Displayed.
175 if (!MyDisplayedStructure.Contains(AStructure))
179 // Recompute structure in all activated views
181 for(int i=1; i<=MyDefinedView.Length(); i++)
183 (MyDefinedView.Value(i))->ReCompute(AStructure);
187 void Visual3d_ViewManager::ReCompute (const Handle(Graphic3d_Structure)& AStructure,
188 const Handle(Graphic3d_DataStructureManager)& AProjector)
191 if (! AProjector->IsKind (STANDARD_TYPE (Visual3d_View))) return;
194 Handle(Visual3d_View) theView = Handle(Visual3d_View)::DownCast (AProjector);
196 Handle(Visual3d_View) theView = Handle(Visual3d_View)::DownCast (AProjector);
198 Standard_Integer ViewId = theView->Identification ();
200 // Even if physically the structure cannot
201 // be displayed (pb of visualisation type)
202 // it has status Displayed.
203 if (!MyDisplayedStructure.Contains(AStructure))
207 // Recompute structure in all activated views
209 for(int i=1; i<=MyDefinedView.Length(); i++)
211 if ((MyDefinedView.Value(i))->Identification () == ViewId)
213 theView->ReCompute (AStructure);
218 void Visual3d_ViewManager::Clear (const Handle(Graphic3d_Structure)& AStructure, const Standard_Boolean WithDestruction)
220 for(int i=1; i<=MyDefinedView.Length(); i++)
222 (MyDefinedView.Value(i))->Clear(AStructure, WithDestruction);
226 void Visual3d_ViewManager::Connect (const Handle(Graphic3d_Structure)& AMother, const Handle(Graphic3d_Structure)& ADaughter)
228 for(int i=1; i<=MyDefinedView.Length(); i++)
230 (MyDefinedView.Value(i))->Connect (AMother, ADaughter);
234 void Visual3d_ViewManager::Disconnect (const Handle(Graphic3d_Structure)& AMother, const Handle(Graphic3d_Structure)& ADaughter)
236 for(int i=1; i<=MyDefinedView.Length(); i++)
238 (MyDefinedView.Value(i))->Disconnect (AMother, ADaughter);
242 void Visual3d_ViewManager::Display (const Handle(Graphic3d_Structure)& AStructure)
244 // Even if physically the structure cannot
245 // be displayed (pb of visualisation type)
246 // it has status Displayed.
248 MyDisplayedStructure.Add(AStructure);
250 for(int i=1; i<=MyDefinedView.Length(); i++)
252 (MyDefinedView.Value(i))->Display(AStructure);
256 void Visual3d_ViewManager::Erase (const Handle(Graphic3d_Structure)& AStructure)
258 // Even if physically the structure cannot
259 // be displayed (pb of visualisation type)
260 // it has status Displayed.
262 MyDisplayedStructure.Remove(AStructure);
265 // Erase structure in all defined views
267 for(int i=1; i<=MyDefinedView.Length(); i++)
269 (MyDefinedView.Value(i))->Erase (AStructure);
272 MyHighlightedStructure.Remove (AStructure);
275 void Visual3d_ViewManager::Erase () {
277 Graphic3d_MapIteratorOfMapOfStructure it( MyDisplayedStructure);
279 for (; it.More(); it.Next()) {
280 Handle(Graphic3d_Structure) SG = it.Key();
286 void Visual3d_ViewManager::Highlight (const Handle(Graphic3d_Structure)& AStructure, const Aspect_TypeOfHighlightMethod AMethod)
288 MyHighlightedStructure.Add(AStructure);
291 // Highlight in all activated views
294 for(int i=1; i<=MyDefinedView.Length(); i++)
296 (MyDefinedView.Value(i))->Highlight (AStructure, AMethod);
300 void Visual3d_ViewManager::SetTransform (const Handle(Graphic3d_Structure)& AStructure, const TColStd_Array2OfReal& ATrsf)
302 for(int i=1; i<=MyDefinedView.Length(); i++)
304 (MyDefinedView.Value(i))->SetTransform (AStructure, ATrsf);
308 void Visual3d_ViewManager::UnHighlight () {
310 Graphic3d_MapIteratorOfMapOfStructure it(MyHighlightedStructure);
312 for (; it.More(); it.Next()) {
313 Handle(Graphic3d_Structure) SG = it.Key();
320 void Visual3d_ViewManager::UnHighlight (const Handle(Graphic3d_Structure)& AStructure)
322 MyHighlightedStructure.Remove(AStructure);
325 // UnHighlight in all activated views
328 for(int i=1; i<=MyDefinedView.Length(); i++)
330 (MyDefinedView.Value(i))->UnHighlight (AStructure);
334 void Visual3d_ViewManager::Redraw() const
336 // redraw all activated views
337 if (MyDefinedView.Length() == 0)
342 for(int i=1; i<=MyDefinedView.Length(); i++)
344 MyDefinedView.Value(i)->Redraw ();
348 void Visual3d_ViewManager::Update() const
353 void Visual3d_ViewManager::RedrawImmediate() const
355 if (MyDefinedView.Length() == 0)
360 // update all activated views
361 for(int i=1; i<=MyDefinedView.Length(); i++)
363 MyDefinedView.Value(i)->RedrawImmediate ();
367 void Visual3d_ViewManager::Invalidate() const
369 if (MyDefinedView.Length() == 0)
374 // update all activated views
375 for(int i=1; i<=MyDefinedView.Length(); i++)
377 MyDefinedView.Value(i)->Invalidate();
381 Handle(Visual3d_HSequenceOfView) Visual3d_ViewManager::ActivatedView () const
384 Handle(Visual3d_HSequenceOfView) SG = new Visual3d_HSequenceOfView();
386 for(int i=1; i<=MyDefinedView.Length(); i++)
388 if ((MyDefinedView.Value(i))->IsActive ())
390 SG->Append(MyDefinedView.Value(i));
398 Standard_Boolean Visual3d_ViewManager::ContainsComputedStructure () const
400 Standard_Boolean Result = Standard_False;
403 // Check all activated views
405 for(int i=1; (!Result) && i<=MyDefinedView.Length(); i++)
407 if ((MyDefinedView.Value(i))->IsActive())
409 Result = (MyDefinedView.Value(i))->ContainsComputedStructure();
417 const Visual3d_SequenceOfView& Visual3d_ViewManager::DefinedViews() const
419 return MyDefinedView;
422 Standard_Boolean Visual3d_ViewManager::ViewExists (const Handle(Aspect_Window)& AWindow, Graphic3d_CView& TheCView) const
424 Standard_Boolean Exist = Standard_False;
426 // Parse the list of views to find
427 // a view with the specified window
430 const Handle(WNT_Window) THEWindow = Handle(WNT_Window)::DownCast (AWindow);
431 Aspect_Handle TheSpecifiedWindowId = THEWindow->HWindow ();
432 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
433 const Handle(Cocoa_Window) THEWindow = Handle(Cocoa_Window)::DownCast (AWindow);
434 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
435 UIView* TheSpecifiedWindowId = THEWindow->HView();
437 NSView* TheSpecifiedWindowId = THEWindow->HView();
439 #elif defined(__ANDROID__)
440 int TheSpecifiedWindowId = -1;
442 const Handle(Xw_Window) THEWindow = Handle(Xw_Window)::DownCast (AWindow);
443 int TheSpecifiedWindowId = int (THEWindow->XWindow ());
446 for(int i=1; (!Exist) && i<=MyDefinedView.Length(); i++)
448 if ( ((MyDefinedView.Value(i))->IsDefined ()) && ((MyDefinedView.Value(i))->IsActive ()) )
450 const Handle(Aspect_Window) AspectWindow = (MyDefinedView.Value(i))->Window();
453 const Handle(WNT_Window) theWindow = Handle(WNT_Window)::DownCast (AspectWindow);
454 Aspect_Handle TheWindowIdOfView = theWindow->HWindow ();
455 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
456 const Handle(Cocoa_Window) theWindow = Handle(Cocoa_Window)::DownCast (AspectWindow);
457 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
458 UIView* TheWindowIdOfView = theWindow->HView();
460 NSView* TheWindowIdOfView = theWindow->HView();
462 #elif defined(__ANDROID__)
463 int TheWindowIdOfView = 0;
465 const Handle(Xw_Window) theWindow = Handle(Xw_Window)::DownCast (AspectWindow);
466 int TheWindowIdOfView = int (theWindow->XWindow ());
468 // Comparaison on window IDs
469 if (TheWindowIdOfView == TheSpecifiedWindowId)
471 Exist = Standard_True;
472 TheCView = *(Graphic3d_CView* )(MyDefinedView.Value(i))->CView();
480 void Visual3d_ViewManager::Activate ()
483 // Activates all deactivated views
485 for(int i=1; i<=MyDefinedView.Length(); i++)
487 if (! (MyDefinedView.Value(i))->IsActive())
489 (MyDefinedView.Value(i))->Activate();
494 void Visual3d_ViewManager::Deactivate ()
497 // Deactivates all activated views
499 for(int i=1; i<=MyDefinedView.Length(); i++)
501 if ((MyDefinedView.Value(i))->IsActive())
503 (MyDefinedView.Value(i))->Deactivate();
509 Standard_Integer Visual3d_ViewManager::MaxNumOfViews () const {
511 // Retourne the planned of definable views for the current
512 // Visual3d_ViewManager.
514 (Standard_Integer ((View_IDMAX-View_IDMIN+1)/Visual3d_ViewManager::Limit ()));
518 Handle(Graphic3d_Structure) Visual3d_ViewManager::Identification (const Standard_Integer AId) const {
520 return (Graphic3d_StructureManager::Identification (AId));
524 Standard_Integer Visual3d_ViewManager::Identification () const {
526 return (Graphic3d_StructureManager::Identification ());
530 Standard_Integer Visual3d_ViewManager::Identification (const Handle(Visual3d_View)& AView)
532 MyDefinedView.Append(AView);
533 return (MyViewGenId.Next ());
536 void Visual3d_ViewManager::UnIdentification (const Standard_Integer aViewId)
538 for(int i=1; i<=MyDefinedView.Length(); i++)
540 if ((MyDefinedView.Value(i))->Identification() == aViewId)
542 //remove the view from the list
543 MyDefinedView.Remove(i);
548 MyViewGenId.Free(aViewId);
551 void Visual3d_ViewManager::SetZBufferAuto (const Standard_Boolean AFlag)
553 MyZBufferAuto = AFlag;
556 Standard_Boolean Visual3d_ViewManager::ZBufferAuto () const
558 return (MyZBufferAuto);
561 //=======================================================================
562 //function : ChangeZLayer
564 //=======================================================================
566 void Visual3d_ViewManager::ChangeZLayer (const Handle(Graphic3d_Structure)& theStructure,
567 const Graphic3d_ZLayerId theLayerId)
569 if (!myLayerIds.Contains (theLayerId)
570 || !MyDisplayedStructure.Contains (theStructure))
575 // change display layer for structure in all views
576 for (int aViewIter = 1; aViewIter <= MyDefinedView.Length(); ++aViewIter)
578 MyDefinedView.Value (aViewIter)->ChangeZLayer (theStructure, theLayerId);
582 //=======================================================================
583 //function : SetZLayerSettings
585 //=======================================================================
586 void Visual3d_ViewManager::SetZLayerSettings (const Graphic3d_ZLayerId theLayerId,
587 const Graphic3d_ZLayerSettings& theSettings)
589 // tell all managed views to set zlayer settings
590 for(int i=1; i<=MyDefinedView.Length(); i++)
592 (MyDefinedView.Value(i))->SetZLayerSettings (theLayerId, theSettings);
595 if (myMapOfZLayerSettings.IsBound (theLayerId))
597 myMapOfZLayerSettings.ChangeFind (theLayerId) = theSettings;
601 myMapOfZLayerSettings.Bind (theLayerId, theSettings);
606 //=======================================================================
607 //function : ZLayerSettings
609 //=======================================================================
610 Graphic3d_ZLayerSettings Visual3d_ViewManager::ZLayerSettings (const Graphic3d_ZLayerId theLayerId)
612 if (!myLayerIds.Contains (theLayerId))
614 return Graphic3d_ZLayerSettings();
617 return myMapOfZLayerSettings.Find (theLayerId);
620 //=======================================================================
621 //function : AddZLayer
623 //=======================================================================
625 Standard_Boolean Visual3d_ViewManager::AddZLayer (Graphic3d_ZLayerId& theLayerId)
630 theLayerId = myZLayerGenId.Next();
631 myLayerIds.Add (theLayerId);
632 myLayerSeq.Append (theLayerId);
634 catch (Aspect_IdentDefinitionError)
636 // new index can't be generated
637 return Standard_False;
640 // default z-layer settings
641 myMapOfZLayerSettings.Bind (theLayerId, Graphic3d_ZLayerSettings());
643 // tell all managed views to remove display layers
644 for(int i=1; i<=MyDefinedView.Length(); i++)
646 (MyDefinedView.Value(i))->AddZLayer(theLayerId);
649 return Standard_True;
652 //=======================================================================
653 //function : RemoveZLayer
655 //=======================================================================
657 Standard_Boolean Visual3d_ViewManager::RemoveZLayer (const Graphic3d_ZLayerId theLayerId)
659 if (!myLayerIds.Contains (theLayerId)
660 || theLayerId < myZLayerGenId.Lower()
661 || theLayerId > myZLayerGenId.Upper())
663 return Standard_False;
666 // tell all managed views to remove display layers
667 for(int i=1; i<=MyDefinedView.Length(); i++)
669 (MyDefinedView.Value(i))->RemoveZLayer (theLayerId);
672 MyGraphicDriver->UnsetZLayer (theLayerId);
675 for (int aIdx = 1; aIdx <= myLayerSeq.Length (); aIdx++)
677 if (myLayerSeq (aIdx) == theLayerId)
679 myLayerSeq.Remove (aIdx);
684 myMapOfZLayerSettings.UnBind (theLayerId);
686 myLayerIds.Remove (theLayerId);
687 myZLayerGenId.Free (theLayerId);
689 return Standard_True;
692 //=======================================================================
693 //function : GetAllZLayers
695 //=======================================================================
697 void Visual3d_ViewManager::GetAllZLayers (TColStd_SequenceOfInteger& theLayerSeq) const
699 theLayerSeq.Assign (myLayerSeq);
702 //=======================================================================
703 //function : InstallZLayers
705 //=======================================================================
707 void Visual3d_ViewManager::InstallZLayers(const Handle(Visual3d_View)& theView) const
709 Standard_Boolean isContainsView = Standard_False;
710 for(int i=1; i<=MyDefinedView.Length(); i++)
712 if(MyDefinedView.Value(i) == theView)
714 isContainsView = Standard_True;
721 // erase and insert layers iteratively to provide the same layer order as
722 // in the view manager's sequence. This approach bases on the layer insertion
723 // order: the new layers are always appended to the end of the list
724 // inside of view, while layer remove operation doesn't affect the order.
725 // Starting from second layer : no need to change the default z layer.
726 for (Standard_Integer aSeqIdx = 1; aSeqIdx <= myLayerSeq.Length(); ++aSeqIdx)
728 const Graphic3d_ZLayerId aLayerID = myLayerSeq.Value (aSeqIdx);
729 const Graphic3d_ZLayerSettings& aSettings = myMapOfZLayerSettings.Find (aLayerID);
730 if (aLayerID < myZLayerGenId.Lower()
731 || aLayerID > myZLayerGenId.Upper())
733 theView->SetZLayerSettings (aLayerID, aSettings);
737 theView->RemoveZLayer (aLayerID);
738 theView->AddZLayer (aLayerID);
739 theView->SetZLayerSettings (aLayerID, aSettings);