0031313: Foundation Classes - Dump improvement for classes
[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 <Geom_Transformation.hxx>
18 #include <Graphic3d_DataStructureManager.hxx>
19 #include <Precision.hxx>
20 #include <Prs3d_Drawer.hxx>
21 #include <Prs3d_Projector.hxx>
22 #include <PrsMgr_PresentableObject.hxx>
23 #include <PrsMgr_PresentationManager.hxx>
24 #include <Quantity_Color.hxx>
25 #include <Graphic3d_CView.hxx>
26
27 IMPLEMENT_STANDARD_RTTIEXT(PrsMgr_Presentation, Graphic3d_Structure)
28
29 namespace
30 {
31   enum BeforeHighlightState
32   {
33     State_Empty,
34     State_Hidden,
35     State_Visible
36   };
37
38   static BeforeHighlightState StructureState (const Graphic3d_Structure* theStructure)
39   {
40     return !theStructure->IsDisplayed() ?
41       State_Empty : !theStructure->IsVisible() ?
42         State_Hidden : State_Visible;
43   }
44 }
45
46 //=======================================================================
47 //function : PrsMgr_Presentation
48 //purpose  :
49 //=======================================================================
50 PrsMgr_Presentation::PrsMgr_Presentation (const Handle(PrsMgr_PresentationManager3d)& thePrsMgr,
51                                           const Handle(PrsMgr_PresentableObject)& thePrsObject,
52                                           const Standard_Integer theMode)
53 : Graphic3d_Structure (thePrsMgr->StructureManager()),
54   myPresentationManager  (thePrsMgr),
55   myPresentableObject    (thePrsObject.get()),
56   myBeforeHighlightState (State_Empty),
57   myMode                 (theMode),
58   myMustBeUpdated        (Standard_False)
59 {
60   if (thePrsObject->TypeOfPresentation3d() == PrsMgr_TOP_ProjectorDependant)
61   {
62     SetVisual (Graphic3d_TOS_COMPUTED);
63   }
64   SetOwner (myPresentableObject);
65   SetMutable (myPresentableObject->IsMutable());
66 }
67
68 //=======================================================================
69 //function : Display
70 //purpose  :
71 //=======================================================================
72 void PrsMgr_Presentation::Display()
73 {
74   display (Standard_False);
75   myBeforeHighlightState = State_Visible;
76 }
77
78 //=======================================================================
79 //function : display
80 //purpose  :
81 //=======================================================================
82 void PrsMgr_Presentation::display (const Standard_Boolean theIsHighlight)
83 {
84   if (!base_type::IsDisplayed())
85   {
86     base_type::SetIsForHighlight (theIsHighlight); // optimization - disable frustum culling for this presentation
87     base_type::Display();
88   }
89   else if (!base_type::IsVisible())
90   {
91     base_type::SetVisible (Standard_True);
92   }
93 }
94
95 //=======================================================================
96 //function : Erase
97 //purpose  :
98 //=======================================================================
99 void PrsMgr_Presentation::Erase()
100 {
101   if (IsDeleted())
102   {
103     return;
104   }
105
106   // Erase structure from structure manager
107   base_type::Erase();
108   base_type::Clear();
109   // Disconnect other structures
110   base_type::DisconnectAll (Graphic3d_TOC_DESCENDANT);
111   // Clear groups and remove graphic structure
112   base_type::Remove();
113 }
114
115 //=======================================================================
116 //function : Highlight
117 //purpose  :
118 //=======================================================================
119 void PrsMgr_Presentation::Highlight (const Handle(Prs3d_Drawer)& theStyle)
120 {
121   if (!IsHighlighted())
122   {
123     myBeforeHighlightState = StructureState (this);
124   }
125
126   display (Standard_True);
127   base_type::Highlight (theStyle);
128 }
129
130 //=======================================================================
131 //function : Unhighlight
132 //purpose  :
133 //=======================================================================
134 void PrsMgr_Presentation::Unhighlight()
135 {
136   base_type::UnHighlight();
137   switch (myBeforeHighlightState)
138   {
139     case State_Visible:
140       return;
141     case State_Hidden:
142       base_type::SetVisible (Standard_False);
143       break;
144     case State_Empty:
145       base_type::Erase();
146       break;
147   }
148 }
149
150 //=======================================================================
151 //function : Clear
152 //purpose  :
153 //=======================================================================
154 void PrsMgr_Presentation::Clear (const Standard_Boolean theWithDestruction)
155 {
156   // This modification remove the contain of the structure:
157   // Consequence:
158   //    1. The memory zone of the group is reused
159   //    2. The speed for animation is constant
160   //myPresentableObject = NULL;
161   SetUpdateStatus (Standard_True);
162   if (IsDeleted())
163   {
164     return;
165   }
166
167   base_type::Clear (theWithDestruction);
168   base_type::DisconnectAll (Graphic3d_TOC_DESCENDANT);
169 }
170
171 //=======================================================================
172 //function : Compute
173 //purpose  :
174 //=======================================================================
175 void PrsMgr_Presentation::Compute()
176 {
177   Standard_Integer aDispMode = 0;
178   for (PrsMgr_Presentations::Iterator aPrsIter (myPresentableObject->myPresentations); aPrsIter.More(); aPrsIter.Next())
179   {
180     const Handle(PrsMgr_Presentation)& aModedPresentation = aPrsIter.Value();
181     if (aModedPresentation == this)
182     {
183       aDispMode = aModedPresentation->Mode();
184       break;
185     }
186   }
187
188   myPresentableObject->Compute (myPresentationManager, this, aDispMode);
189 }
190
191 //=======================================================================
192 //function : Compute
193 //purpose  : Methods for hidden parts...
194 //=======================================================================
195 Handle(Graphic3d_Structure) PrsMgr_Presentation::Compute (const Handle(Graphic3d_DataStructureManager)& theProjector)
196 {
197   Handle(Graphic3d_Structure) aPrs = new Graphic3d_Structure (myPresentationManager->StructureManager());
198   myPresentableObject->Compute (Projector (theProjector), aPrs);
199   return aPrs;
200 }
201
202 //=======================================================================
203 //function : Compute
204 //purpose  :
205 //=======================================================================
206 void PrsMgr_Presentation::Compute (const Handle(Graphic3d_DataStructureManager)& theProjector,
207                                    Handle(Graphic3d_Structure)& theStructToFill)
208 {
209   theStructToFill->Clear();
210   Handle(Prs3d_Presentation) aPrs = theStructToFill;
211   myPresentableObject->Compute (Projector (theProjector), aPrs);
212 }
213
214 //=======================================================================
215 //function : Compute
216 //purpose  :
217 //=======================================================================
218 Handle(Graphic3d_Structure) PrsMgr_Presentation::Compute (const Handle(Graphic3d_DataStructureManager)& theProjector,
219                                                           const Handle(Geom_Transformation)&            theTrsf)
220 {
221   Handle(Prs3d_Presentation) aPrs3d = new Prs3d_Presentation (myPresentationManager->StructureManager());
222   myPresentableObject->Compute (Projector (theProjector), theTrsf, aPrs3d);
223   return aPrs3d;
224 }
225
226 //=======================================================================
227 //function : Compute
228 //purpose  :
229 //=======================================================================
230 void PrsMgr_Presentation::Compute (const Handle(Graphic3d_DataStructureManager)& theProjector,
231                                    const Handle(Geom_Transformation)& theTrsf,
232                                    Handle(Graphic3d_Structure)& theStructToFill)
233 {
234   // recompute HLR after transformation in all the case
235   Handle(Graphic3d_Structure) aPrs = theStructToFill;
236   theStructToFill->Clear();
237   myPresentableObject->Compute (Projector (theProjector), theTrsf, aPrs);
238 }
239
240 //=======================================================================
241 //function : Projector
242 //purpose  :
243 //=======================================================================
244 Handle(Prs3d_Projector) PrsMgr_Presentation::Projector (const Handle(Graphic3d_DataStructureManager)& theProjector)
245 {
246   Handle(Graphic3d_Camera) aCamera = Handle(Graphic3d_CView)::DownCast (theProjector)->Camera();
247   const gp_Dir aDir = aCamera->Direction().Reversed();
248   const gp_Pnt anAt = aCamera->Center();
249   const gp_Dir anUp = aCamera->Up();
250   Handle(Prs3d_Projector) aProj = new Prs3d_Projector (!aCamera->IsOrthographic(),
251                                                        aCamera->Scale(),
252                                                        aDir.X(), aDir.Y(), aDir.Z(),
253                                                        anAt.X(), anAt.Y(), anAt.Z(),
254                                                        anUp.X(), anUp.Y(), anUp.Z());
255   return aProj;
256 }
257
258 //=======================================================================
259 //function : ~PrsMgr_Presentation
260 //purpose  :
261 //=======================================================================
262 PrsMgr_Presentation::~PrsMgr_Presentation()
263 {
264   Erase();
265 }
266
267 // =======================================================================
268 // function : DumpJson
269 // purpose  :
270 // =======================================================================
271 void PrsMgr_Presentation::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
272 {
273   OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
274   OCCT_DUMP_BASE_CLASS (theOStream, theDepth, Graphic3d_Structure)
275
276   OCCT_DUMP_FIELD_VALUE_POINTER (theOStream, myPresentableObject)
277
278   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myBeforeHighlightState)
279   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myMode)
280   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myMustBeUpdated)
281 }