0031642: Visualization - crash in Graphic3d_Structure::SetVisual() on redisplaying...
[occt.git] / src / PrsMgr / PrsMgr_Presentation.cxx
1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
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.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 #include <PrsMgr_Presentation.hxx>
16
17 #include <Graphic3d_DataStructureManager.hxx>
18 #include <Precision.hxx>
19 #include <Prs3d_Drawer.hxx>
20 #include <PrsMgr_PresentableObject.hxx>
21 #include <PrsMgr_PresentationManager.hxx>
22 #include <Quantity_Color.hxx>
23 #include <Graphic3d_CView.hxx>
24
25 IMPLEMENT_STANDARD_RTTIEXT(PrsMgr_Presentation, Graphic3d_Structure)
26
27 namespace
28 {
29   enum BeforeHighlightState
30   {
31     State_Empty,
32     State_Hidden,
33     State_Visible
34   };
35
36   static BeforeHighlightState StructureState (const Graphic3d_Structure* theStructure)
37   {
38     return !theStructure->IsDisplayed() ?
39       State_Empty : !theStructure->IsVisible() ?
40         State_Hidden : State_Visible;
41   }
42 }
43
44 //=======================================================================
45 //function : PrsMgr_Presentation
46 //purpose  :
47 //=======================================================================
48 PrsMgr_Presentation::PrsMgr_Presentation (const Handle(PrsMgr_PresentationManager3d)& thePrsMgr,
49                                           const Handle(PrsMgr_PresentableObject)& thePrsObject,
50                                           const Standard_Integer theMode)
51 : Graphic3d_Structure (thePrsMgr->StructureManager()),
52   myPresentationManager  (thePrsMgr),
53   myPresentableObject    (thePrsObject.get()),
54   myBeforeHighlightState (State_Empty),
55   myMode                 (theMode),
56   myMustBeUpdated        (Standard_False)
57 {
58   if (thePrsObject->TypeOfPresentation3d() == PrsMgr_TOP_ProjectorDependant)
59   {
60     SetVisual (Graphic3d_TOS_COMPUTED);
61   }
62   SetOwner (myPresentableObject);
63   SetMutable (myPresentableObject->IsMutable());
64 }
65
66 //=======================================================================
67 //function : Display
68 //purpose  :
69 //=======================================================================
70 void PrsMgr_Presentation::Display()
71 {
72   display (Standard_False);
73   myBeforeHighlightState = State_Visible;
74 }
75
76 //=======================================================================
77 //function : display
78 //purpose  :
79 //=======================================================================
80 void PrsMgr_Presentation::display (const Standard_Boolean theIsHighlight)
81 {
82   if (!base_type::IsDisplayed())
83   {
84     base_type::SetIsForHighlight (theIsHighlight); // optimization - disable frustum culling for this presentation
85     base_type::Display();
86   }
87   else if (!base_type::IsVisible())
88   {
89     base_type::SetVisible (Standard_True);
90   }
91 }
92
93 //=======================================================================
94 //function : Erase
95 //purpose  :
96 //=======================================================================
97 void PrsMgr_Presentation::Erase()
98 {
99   if (IsDeleted())
100   {
101     return;
102   }
103
104   // Erase structure from structure manager
105   erase();
106   clear (true);
107   // Disconnect other structures
108   DisconnectAll (Graphic3d_TOC_DESCENDANT);
109   // Clear groups and remove graphic structure
110   Remove();
111 }
112
113 //=======================================================================
114 //function : Highlight
115 //purpose  :
116 //=======================================================================
117 void PrsMgr_Presentation::Highlight (const Handle(Prs3d_Drawer)& theStyle)
118 {
119   if (!IsHighlighted())
120   {
121     myBeforeHighlightState = StructureState (this);
122   }
123
124   display (Standard_True);
125   base_type::Highlight (theStyle);
126 }
127
128 //=======================================================================
129 //function : Unhighlight
130 //purpose  :
131 //=======================================================================
132 void PrsMgr_Presentation::Unhighlight()
133 {
134   base_type::UnHighlight();
135   switch (myBeforeHighlightState)
136   {
137     case State_Visible:
138       return;
139     case State_Hidden:
140       base_type::SetVisible (Standard_False);
141       break;
142     case State_Empty:
143       base_type::erase();
144       break;
145   }
146 }
147
148 //=======================================================================
149 //function : Clear
150 //purpose  :
151 //=======================================================================
152 void PrsMgr_Presentation::Clear (const Standard_Boolean theWithDestruction)
153 {
154   // This modification remove the contain of the structure:
155   // Consequence:
156   //    1. The memory zone of the group is reused
157   //    2. The speed for animation is constant
158   //myPresentableObject = NULL;
159   SetUpdateStatus (Standard_True);
160   if (IsDeleted())
161   {
162     return;
163   }
164
165   clear (theWithDestruction);
166   DisconnectAll (Graphic3d_TOC_DESCENDANT);
167 }
168
169 //=======================================================================
170 //function : Compute
171 //purpose  :
172 //=======================================================================
173 void PrsMgr_Presentation::Compute()
174 {
175   Standard_Integer aDispMode = 0;
176   for (PrsMgr_Presentations::Iterator aPrsIter (myPresentableObject->myPresentations); aPrsIter.More(); aPrsIter.Next())
177   {
178     const Handle(PrsMgr_Presentation)& aModedPresentation = aPrsIter.Value();
179     if (aModedPresentation == this)
180     {
181       aDispMode = aModedPresentation->Mode();
182       break;
183     }
184   }
185
186   myPresentableObject->Compute (myPresentationManager, this, aDispMode);
187 }
188
189 //=======================================================================
190 //function : Compute
191 //purpose  :
192 //=======================================================================
193 void PrsMgr_Presentation::computeHLR (const Handle(Graphic3d_Camera)& theProjector,
194                                       Handle(Graphic3d_Structure)& theStructToFill)
195 {
196   if (theStructToFill.IsNull())
197   {
198     theStructToFill = new Prs3d_Presentation (myPresentationManager->StructureManager());
199   }
200   Handle(Graphic3d_Structure) aPrs = theStructToFill;
201   theStructToFill->Clear();
202   myPresentableObject->computeHLR (theProjector, Transformation(), aPrs);
203 }
204
205 //=======================================================================
206 //function : ~PrsMgr_Presentation
207 //purpose  :
208 //=======================================================================
209 PrsMgr_Presentation::~PrsMgr_Presentation()
210 {
211   Erase();
212 }
213
214 // =======================================================================
215 // function : DumpJson
216 // purpose  :
217 // =======================================================================
218 void PrsMgr_Presentation::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
219 {
220   OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
221   OCCT_DUMP_BASE_CLASS (theOStream, theDepth, Graphic3d_Structure)
222
223   OCCT_DUMP_FIELD_VALUE_POINTER (theOStream, myPresentableObject)
224
225   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myBeforeHighlightState)
226   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myMode)
227   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myMustBeUpdated)
228 }