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 <Visual3d_ViewManager.ixx>
60 #include <Standard_ErrorHandler.hxx>
63 #include <Aspect_IdentDefinitionError.hxx>
65 #include <Graphic3d_GraphicDriver.hxx>
66 #include <Graphic3d_MapOfStructure.hxx>
67 #include <Graphic3d_MapIteratorOfMapOfStructure.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>
78 // views identifiers : possible range
80 #define View_IDMAX 10000
82 Visual3d_ViewManager::Visual3d_ViewManager (const Handle(Graphic3d_GraphicDriver)& theDriver):
83 Graphic3d_StructureManager (theDriver),
85 MyViewGenId (View_IDMIN+((View_IDMIN+View_IDMAX)/(Visual3d_ViewManager::Limit ()))*(Visual3d_ViewManager::CurrentId ()-1),View_IDMIN+((View_IDMIN+View_IDMAX)/(Visual3d_ViewManager::Limit ()))*Visual3d_ViewManager::CurrentId ()-1),
86 MyZBufferAuto (Standard_False),
87 MyTransparency (Standard_False)
89 // default layer is always presented in display layer sequence
90 // it can not be removed
92 myLayerSeq.Append (0);
94 MyGraphicDriver = theDriver;
95 myMapOfZLayerSettings.Bind (0, Graphic3d_ZLayerSettings());
100 void Visual3d_ViewManager::Destroy () {
103 cout << "Visual3d_ViewManager::Destroy (" << MyId << ")\n" << flush;
111 void Visual3d_ViewManager::Remove () {
114 cout << "Visual3d_ViewManager::Remove (" << MyId << ")\n" << flush;
118 // Destroy all defined views
122 cout << "The Manager " << MyId << " have " << Length << " defined views\n";
126 // clear all structures whilst views are alive for correct GPU memory management
127 MyDisplayedStructure.Clear();
128 MyHighlightedStructure.Clear();
129 MyPickStructure.Clear();
131 // clear list of managed views
132 MyDefinedView.Clear();
135 void Visual3d_ViewManager::ChangeDisplayPriority (const Handle(Graphic3d_Structure)& AStructure, const Standard_Integer OldPriority, const Standard_Integer NewPriority)
139 cout << "Visual3d_ViewManager::ChangeDisplayPriority ("
140 << AStructure->Identification ()
141 << ", " << OldPriority << ", " << NewPriority << ")\n";
146 // Change structure priority in all defined views
148 for(int i=1; i<=MyDefinedView.Length(); i++)
150 (MyDefinedView.Value(i))->ChangeDisplayPriority(AStructure, OldPriority, NewPriority);
154 void Visual3d_ViewManager::ReCompute (const Handle(Graphic3d_Structure)& AStructure) {
156 //Standard_Integer LengthD = MyDisplayedStructure.Extent() ();
158 // Even if physically the structure cannot
159 // be displayed (pb of visualisation type)
160 // it has status Displayed.
162 if (!MyDisplayedStructure.Contains(AStructure))
166 // Recompute structure in all activated views
168 for(int i=1; i<=MyDefinedView.Length(); i++)
170 (MyDefinedView.Value(i))->ReCompute(AStructure);
174 void Visual3d_ViewManager::ReCompute (const Handle(Graphic3d_Structure)& AStructure,
175 const Handle(Graphic3d_DataStructureManager)& AProjector)
178 if (! AProjector->IsKind (STANDARD_TYPE (Visual3d_View))) return;
181 Handle(Visual3d_View) theView = Handle(Visual3d_View)::DownCast (AProjector);
183 Handle(Visual3d_View) theView = *(Handle(Visual3d_View) *) &AProjector;
185 Standard_Integer ViewId = theView->Identification ();
187 // Even if physically the structure cannot
188 // be displayed (pb of visualisation type)
189 // it has status Displayed.
190 if (!MyDisplayedStructure.Contains(AStructure))
194 // Recompute structure in all activated views
196 for(int i=1; i<=MyDefinedView.Length(); i++)
198 if ((MyDefinedView.Value(i))->Identification () == ViewId)
200 theView->ReCompute (AStructure);
205 void Visual3d_ViewManager::Clear (const Handle(Graphic3d_Structure)& AStructure, const Standard_Boolean WithDestruction)
207 for(int i=1; i<=MyDefinedView.Length(); i++)
209 (MyDefinedView.Value(i))->Clear(AStructure, WithDestruction);
213 void Visual3d_ViewManager::Connect (const Handle(Graphic3d_Structure)& AMother, const Handle(Graphic3d_Structure)& ADaughter)
215 for(int i=1; i<=MyDefinedView.Length(); i++)
217 (MyDefinedView.Value(i))->Connect (AMother, ADaughter);
221 void Visual3d_ViewManager::Disconnect (const Handle(Graphic3d_Structure)& AMother, const Handle(Graphic3d_Structure)& ADaughter)
223 for(int i=1; i<=MyDefinedView.Length(); i++)
225 (MyDefinedView.Value(i))->Disconnect (AMother, ADaughter);
229 void Visual3d_ViewManager::Display (const Handle(Graphic3d_Structure)& AStructure)
231 // Even if physically the structure cannot
232 // be displayed (pb of visualisation type)
233 // it has status Displayed.
235 MyDisplayedStructure.Add(AStructure);
237 for(int i=1; i<=MyDefinedView.Length(); i++)
239 (MyDefinedView.Value(i))->Display(AStructure);
243 void Visual3d_ViewManager::Erase (const Handle(Graphic3d_Structure)& AStructure)
245 // Even if physically the structure cannot
246 // be displayed (pb of visualisation type)
247 // it has status Displayed.
249 MyDisplayedStructure.Remove(AStructure);
252 // Erase structure in all defined views
254 for(int i=1; i<=MyDefinedView.Length(); i++)
256 (MyDefinedView.Value(i))->Erase (AStructure);
259 MyHighlightedStructure.Remove (AStructure);
260 MyPickStructure.Remove (AStructure);
263 void Visual3d_ViewManager::Erase () {
265 Graphic3d_MapIteratorOfMapOfStructure it( MyDisplayedStructure);
267 for (; it.More(); it.Next()) {
268 Handle(Graphic3d_Structure) SG = it.Key();
274 void Visual3d_ViewManager::Highlight (const Handle(Graphic3d_Structure)& AStructure, const Aspect_TypeOfHighlightMethod AMethod)
276 MyHighlightedStructure.Add(AStructure);
279 // Highlight in all activated views
282 for(int i=1; i<=MyDefinedView.Length(); i++)
284 (MyDefinedView.Value(i))->Highlight (AStructure, AMethod);
288 void Visual3d_ViewManager::SetTransform (const Handle(Graphic3d_Structure)& AStructure, const TColStd_Array2OfReal& ATrsf)
290 for(int i=1; i<=MyDefinedView.Length(); i++)
292 (MyDefinedView.Value(i))->SetTransform (AStructure, ATrsf);
296 void Visual3d_ViewManager::UnHighlight () {
298 Graphic3d_MapIteratorOfMapOfStructure it(MyHighlightedStructure);
300 for (; it.More(); it.Next()) {
301 Handle(Graphic3d_Structure) SG = it.Key();
308 void Visual3d_ViewManager::UnHighlight (const Handle(Graphic3d_Structure)& AStructure)
310 MyHighlightedStructure.Remove(AStructure);
313 // UnHighlight in all activated views
316 for(int i=1; i<=MyDefinedView.Length(); i++)
318 (MyDefinedView.Value(i))->UnHighlight (AStructure);
322 void Visual3d_ViewManager::Redraw() const
324 // redraw all activated views
325 if (MyDefinedView.Length() == 0)
330 if (!MyUnderLayer.IsNull() || !MyOverLayer.IsNull())
332 Standard_Integer aWidth = 0, aHeight = 0;
333 Standard_Integer aWidthMax = 0;
334 Standard_Integer aHeightMax = 0;
336 for(int i=1; i<=MyDefinedView.Length(); i++)
338 MyDefinedView.Value(i)->Window()->Size (aWidth, aHeight);
339 aWidthMax = Max (aWidthMax, aWidth);
340 aHeightMax = Max (aHeightMax, aHeight);
343 if (!MyUnderLayer.IsNull())
345 MyUnderLayer->SetViewport (aWidthMax, aHeightMax);
347 if (!MyOverLayer.IsNull())
349 MyOverLayer->SetViewport (aWidthMax, aHeightMax);
353 for(int i=1; i<=MyDefinedView.Length(); i++)
355 MyDefinedView.Value(i)->Redraw (MyUnderLayer, MyOverLayer);
359 void Visual3d_ViewManager::Update() const
364 void Visual3d_ViewManager::RedrawImmediate() const
366 if (MyDefinedView.Length() == 0)
371 // update all activated views
372 for(int i=1; i<=MyDefinedView.Length(); i++)
374 MyDefinedView.Value(i)->RedrawImmediate (MyUnderLayer, MyOverLayer);
378 void Visual3d_ViewManager::Invalidate() const
380 if (MyDefinedView.Length() == 0)
385 // update all activated views
386 for(int i=1; i<=MyDefinedView.Length(); i++)
388 MyDefinedView.Value(i)->Invalidate();
392 Handle(Visual3d_HSequenceOfView) Visual3d_ViewManager::ActivatedView () const
395 Handle(Visual3d_HSequenceOfView) SG = new Visual3d_HSequenceOfView();
397 for(int i=1; i<=MyDefinedView.Length(); i++)
399 if ((MyDefinedView.Value(i))->IsActive ())
401 SG->Append(MyDefinedView.Value(i));
409 Standard_Boolean Visual3d_ViewManager::ContainsComputedStructure () const
411 Standard_Boolean Result = Standard_False;
414 // Check all activated views
416 for(int i=1; (!Result) && i<=MyDefinedView.Length(); i++)
418 if ((MyDefinedView.Value(i))->IsActive())
420 Result = (MyDefinedView.Value(i))->ContainsComputedStructure();
428 Handle(Visual3d_HSequenceOfView) Visual3d_ViewManager::DefinedView () const
430 Handle (Visual3d_HSequenceOfView) SG = new Visual3d_HSequenceOfView();
432 for(int i=1; i<=MyDefinedView.Length(); i++)
434 SG->Append(MyDefinedView.Value(i));
440 Standard_Boolean Visual3d_ViewManager::ViewExists (const Handle(Aspect_Window)& AWindow, Graphic3d_CView& TheCView) const
442 Standard_Boolean Exist = Standard_False;
444 // Parse the list of views to find
445 // a view with the specified window
448 const Handle(WNT_Window) THEWindow = Handle(WNT_Window)::DownCast (AWindow);
449 Aspect_Handle TheSpecifiedWindowId = THEWindow->HWindow ();
450 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
451 const Handle(Cocoa_Window) THEWindow = Handle(Cocoa_Window)::DownCast (AWindow);
452 NSView* TheSpecifiedWindowId = THEWindow->HView();
453 #elif defined(__ANDROID__)
454 int TheSpecifiedWindowId = -1;
456 const Handle(Xw_Window) THEWindow = Handle(Xw_Window)::DownCast (AWindow);
457 int TheSpecifiedWindowId = int (THEWindow->XWindow ());
460 for(int i=1; (!Exist) && i<=MyDefinedView.Length(); i++)
462 if ( ((MyDefinedView.Value(i))->IsDefined ()) && ((MyDefinedView.Value(i))->IsActive ()) )
464 const Handle(Aspect_Window) AspectWindow = (MyDefinedView.Value(i))->Window();
467 const Handle(WNT_Window) theWindow = Handle(WNT_Window)::DownCast (AspectWindow);
468 Aspect_Handle TheWindowIdOfView = theWindow->HWindow ();
469 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
470 const Handle(Cocoa_Window) theWindow = Handle(Cocoa_Window)::DownCast (AspectWindow);
471 NSView* TheWindowIdOfView = theWindow->HView();
472 #elif defined(__ANDROID__)
473 int TheWindowIdOfView = 0;
475 const Handle(Xw_Window) theWindow = Handle(Xw_Window)::DownCast (AspectWindow);
476 int TheWindowIdOfView = int (theWindow->XWindow ());
478 // Comparaison on window IDs
479 if (TheWindowIdOfView == TheSpecifiedWindowId)
481 Exist = Standard_True;
482 TheCView = *(Graphic3d_CView* )(MyDefinedView.Value(i))->CView();
490 void Visual3d_ViewManager::Activate ()
493 // Activates all deactivated views
495 for(int i=1; i<=MyDefinedView.Length(); i++)
497 if (! (MyDefinedView.Value(i))->IsActive())
499 (MyDefinedView.Value(i))->Activate();
504 void Visual3d_ViewManager::Deactivate ()
507 // Deactivates all activated views
509 for(int i=1; i<=MyDefinedView.Length(); i++)
511 if ((MyDefinedView.Value(i))->IsActive())
513 (MyDefinedView.Value(i))->Deactivate();
519 Standard_Integer Visual3d_ViewManager::MaxNumOfViews () const {
521 // Retourne the planned of definable views for the current
522 // Visual3d_ViewManager.
524 (Standard_Integer ((View_IDMAX-View_IDMIN+1)/Visual3d_ViewManager::Limit ()));
528 Handle(Graphic3d_Structure) Visual3d_ViewManager::Identification (const Standard_Integer AId) const {
530 return (Graphic3d_StructureManager::Identification (AId));
534 Standard_Integer Visual3d_ViewManager::Identification () const {
536 return (Graphic3d_StructureManager::Identification ());
540 Standard_Integer Visual3d_ViewManager::Identification (const Handle(Visual3d_View)& AView)
542 MyDefinedView.Append(AView);
543 return (MyViewGenId.Next ());
546 void Visual3d_ViewManager::UnIdentification (const Standard_Integer aViewId)
548 for(int i=1; i<=MyDefinedView.Length(); i++)
550 if ((MyDefinedView.Value(i))->Identification() == aViewId)
552 //remove the view from the list
553 MyDefinedView.Remove(i);
558 MyViewGenId.Free(aViewId);
561 void Visual3d_ViewManager::SetTransparency (const Standard_Boolean AFlag)
563 if (MyTransparency && AFlag) return;
564 if (! MyTransparency && ! AFlag) return;
566 for(int i=1; i<=MyDefinedView.Length(); i++)
568 (MyDefinedView.Value(i))->SetTransparency(AFlag);
571 MyTransparency = AFlag;
574 Standard_Boolean Visual3d_ViewManager::Transparency () const
576 return (MyTransparency);
579 void Visual3d_ViewManager::SetZBufferAuto (const Standard_Boolean AFlag)
581 if (MyZBufferAuto && AFlag) return;
582 if (! MyZBufferAuto && ! AFlag) return;
584 // if pass from False to True :
585 // no problem, at the next view update, it
586 // will properly ask questions to answer (SetVisualisation)
587 // if pass from True to False :
588 // it is necessary to modify ZBufferActivity at each view so that
589 // zbuffer could be active only if required by context.
590 // In this case -1 is passed so that the view ask itself the question
591 // Note : 0 forces the desactivation, 1 forces the activation
594 for(int i=1; i<=MyDefinedView.Length(); i++)
596 (MyDefinedView.Value(i))->SetZBufferActivity(-1);
599 MyZBufferAuto = AFlag;
602 Standard_Boolean Visual3d_ViewManager::ZBufferAuto () const
604 return (MyZBufferAuto);
607 void Visual3d_ViewManager::SetLayer (const Handle(Visual3d_Layer)& ALayer) {
610 cout << "Visual3d_ViewManager::SetLayer\n" << flush;
613 if (ALayer->Type () == Aspect_TOL_OVERLAY) {
615 if (MyOverLayer.IsNull ())
616 cout << "MyOverLayer is defined" << endl;
618 cout << "MyOverLayer is redefined" << endl;
620 MyOverLayer = ALayer;
624 if (MyUnderLayer.IsNull ())
625 cout << "MyUnderLayer is defined" << endl;
627 cout << "MyUnderLayer is redefined" << endl;
629 MyUnderLayer = ALayer;
634 const Handle(Visual3d_Layer)& Visual3d_ViewManager::UnderLayer () const {
636 return (MyUnderLayer);
640 const Handle(Visual3d_Layer)& Visual3d_ViewManager::OverLayer () const {
642 return (MyOverLayer);
646 //=======================================================================
647 //function : ChangeZLayer
649 //=======================================================================
651 void Visual3d_ViewManager::ChangeZLayer (const Handle(Graphic3d_Structure)& theStructure,
652 const Standard_Integer theLayerId)
654 if (!myLayerIds.Contains (theLayerId))
657 // change display layer for structure in all views
658 if (MyDisplayedStructure.Contains (theStructure))
660 for(int i=1; i<=MyDefinedView.Length(); i++)
662 (MyDefinedView.Value(i))->ChangeZLayer(theStructure, theLayerId);
666 // tell graphic driver to update the structure's display layer
667 MyGraphicDriver->ChangeZLayer (*(theStructure->CStructure()), theLayerId);
670 //=======================================================================
671 //function : GetZLayer
673 //=======================================================================
675 Standard_Integer Visual3d_ViewManager::GetZLayer (const Handle(Graphic3d_Structure)& theStructure) const
677 return MyGraphicDriver->GetZLayer (*theStructure->CStructure ());
680 //=======================================================================
681 //function : SetZLayerSettings
683 //=======================================================================
684 void Visual3d_ViewManager::SetZLayerSettings (const Standard_Integer theLayerId,
685 const Graphic3d_ZLayerSettings& theSettings)
687 // tell all managed views to set zlayer settings
688 for(int i=1; i<=MyDefinedView.Length(); i++)
690 (MyDefinedView.Value(i))->SetZLayerSettings (theLayerId, theSettings);
693 if (myMapOfZLayerSettings.IsBound (theLayerId))
695 myMapOfZLayerSettings.ChangeFind (theLayerId) = theSettings;
699 myMapOfZLayerSettings.Bind (theLayerId, theSettings);
704 //=======================================================================
705 //function : ZLayerSettings
707 //=======================================================================
708 Graphic3d_ZLayerSettings Visual3d_ViewManager::ZLayerSettings (const Standard_Integer theLayerId)
710 if (!myLayerIds.Contains (theLayerId))
712 return Graphic3d_ZLayerSettings();
715 return myMapOfZLayerSettings.Find (theLayerId);
718 //=======================================================================
719 //function : AddZLayer
721 //=======================================================================
723 Standard_Boolean Visual3d_ViewManager::AddZLayer (Standard_Integer& theLayerId)
728 theLayerId = getZLayerGenId ().Next ();
729 myLayerIds.Add (theLayerId);
730 myLayerSeq.Append (theLayerId);
732 catch (Aspect_IdentDefinitionError)
734 // new index can't be generated
735 return Standard_False;
738 // default z-layer settings
739 myMapOfZLayerSettings.Bind (theLayerId, Graphic3d_ZLayerSettings());
741 // tell all managed views to remove display layers
742 for(int i=1; i<=MyDefinedView.Length(); i++)
744 (MyDefinedView.Value(i))->AddZLayer(theLayerId);
747 return Standard_True;
750 //=======================================================================
751 //function : RemoveZLayer
753 //=======================================================================
755 Standard_Boolean Visual3d_ViewManager::RemoveZLayer (const Standard_Integer theLayerId)
757 if (!myLayerIds.Contains (theLayerId) || theLayerId == 0)
758 return Standard_False;
760 // tell all managed views to remove display layers
761 for(int i=1; i<=MyDefinedView.Length(); i++)
763 (MyDefinedView.Value(i))->RemoveZLayer (theLayerId);
766 MyGraphicDriver->UnsetZLayer (theLayerId);
769 for (int aIdx = 1; aIdx <= myLayerSeq.Length (); aIdx++)
771 if (myLayerSeq (aIdx) == theLayerId)
773 myLayerSeq.Remove (aIdx);
778 myMapOfZLayerSettings.UnBind (theLayerId);
780 myLayerIds.Remove (theLayerId);
781 getZLayerGenId ().Free (theLayerId);
783 return Standard_True;
786 //=======================================================================
787 //function : GetAllZLayers
789 //=======================================================================
791 void Visual3d_ViewManager::GetAllZLayers (TColStd_SequenceOfInteger& theLayerSeq) const
793 theLayerSeq.Assign (myLayerSeq);
796 //=======================================================================
797 //function : getZLayerGenId
799 //=======================================================================
801 Aspect_GenId& Visual3d_ViewManager::getZLayerGenId ()
803 static Aspect_GenId aGenId (1, IntegerLast());
807 //=======================================================================
808 //function : InstallZLayers
810 //=======================================================================
812 void Visual3d_ViewManager::InstallZLayers(const Handle(Visual3d_View)& theView) const
814 Standard_Boolean isContainsView = Standard_False;
815 for(int i=1; i<=MyDefinedView.Length(); i++)
817 if(MyDefinedView.Value(i) == theView)
819 isContainsView = Standard_True;
826 // erase and insert layers iteratively to provide the same layer order as
827 // in the view manager's sequence. This approach bases on the layer insertion
828 // order: the new layers are always appended to the end of the list
829 // inside of view, while layer remove operation doesn't affect the order.
830 // Starting from second layer : no need to change the default z layer.
831 for (Standard_Integer aSeqIdx = 2; aSeqIdx <= myLayerSeq.Length (); aSeqIdx++)
833 Standard_Integer aLayerID = myLayerSeq.Value (aSeqIdx);
834 theView->RemoveZLayer (aLayerID);
835 theView->AddZLayer (aLayerID);