0030675: Visualization - remove redundant proxy classes in hierarchy of PrsMgr_Presen...
[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);
87     base_type::Display();
88   }
89   else if (!base_type::IsVisible())
90   {
91     base_type::SetVisible (Standard_True);
92     base_type::SetIsForHighlight (theIsHighlight);
93   }
94 }
95
96 //=======================================================================
97 //function : Erase
98 //purpose  :
99 //=======================================================================
100 void PrsMgr_Presentation::Erase()
101 {
102   if (IsDeleted())
103   {
104     return;
105   }
106
107   // Erase structure from structure manager
108   base_type::Erase();
109   base_type::Clear();
110   // Disconnect other structures
111   base_type::DisconnectAll (Graphic3d_TOC_DESCENDANT);
112   // Clear groups and remove graphic structure
113   base_type::Remove();
114 }
115
116 //=======================================================================
117 //function : Highlight
118 //purpose  :
119 //=======================================================================
120 void PrsMgr_Presentation::Highlight (const Handle(Prs3d_Drawer)& theStyle)
121 {
122   if (!IsHighlighted())
123   {
124     myBeforeHighlightState = StructureState (this);
125   }
126
127   display (Standard_True);
128   base_type::Highlight (theStyle);
129 }
130
131 //=======================================================================
132 //function : Unhighlight
133 //purpose  :
134 //=======================================================================
135 void PrsMgr_Presentation::Unhighlight()
136 {
137   base_type::UnHighlight();
138   switch (myBeforeHighlightState)
139   {
140     case State_Visible:
141       return;
142     case State_Hidden:
143       base_type::SetVisible (Standard_False);
144       break;
145     case State_Empty:
146       base_type::Erase();
147       break;
148   }
149 }
150
151 //=======================================================================
152 //function : Clear
153 //purpose  :
154 //=======================================================================
155 void PrsMgr_Presentation::Clear (const Standard_Boolean theWithDestruction)
156 {
157   // This modification remove the contain of the structure:
158   // Consequence:
159   //    1. The memory zone of the group is reused
160   //    2. The speed for animation is constant
161   //myPresentableObject = NULL;
162   SetUpdateStatus (Standard_True);
163   if (IsDeleted())
164   {
165     return;
166   }
167
168   base_type::Clear (theWithDestruction);
169   base_type::DisconnectAll (Graphic3d_TOC_DESCENDANT);
170 }
171
172 //=======================================================================
173 //function : Compute
174 //purpose  :
175 //=======================================================================
176 void PrsMgr_Presentation::Compute()
177 {
178   Standard_Integer aDispMode = 0;
179   for (PrsMgr_Presentations::Iterator aPrsIter (myPresentableObject->myPresentations); aPrsIter.More(); aPrsIter.Next())
180   {
181     const Handle(PrsMgr_Presentation)& aModedPresentation = aPrsIter.Value();
182     if (aModedPresentation == this)
183     {
184       aDispMode = aModedPresentation->Mode();
185       break;
186     }
187   }
188
189   myPresentableObject->Compute (myPresentationManager, this, aDispMode);
190 }
191
192 //=======================================================================
193 //function : Compute
194 //purpose  : Methods for hidden parts...
195 //=======================================================================
196 Handle(Graphic3d_Structure) PrsMgr_Presentation::Compute (const Handle(Graphic3d_DataStructureManager)& theProjector)
197 {
198   Handle(Graphic3d_Structure) aPrs = new Graphic3d_Structure (myPresentationManager->StructureManager());
199   myPresentableObject->Compute (Projector (theProjector), aPrs);
200   return aPrs;
201 }
202
203 //=======================================================================
204 //function : Compute
205 //purpose  :
206 //=======================================================================
207 void PrsMgr_Presentation::Compute (const Handle(Graphic3d_DataStructureManager)& theProjector,
208                                    Handle(Graphic3d_Structure)& theStructToFill)
209 {
210   theStructToFill->Clear();
211   Handle(Prs3d_Presentation) aPrs = theStructToFill;
212   myPresentableObject->Compute (Projector (theProjector), aPrs);
213 }
214
215 //=======================================================================
216 //function : Compute
217 //purpose  :
218 //=======================================================================
219 Handle(Graphic3d_Structure) PrsMgr_Presentation::Compute (const Handle(Graphic3d_DataStructureManager)& theProjector,
220                                                           const Handle(Geom_Transformation)&            theTrsf)
221 {
222   Handle(Prs3d_Presentation) aPrs3d = new Prs3d_Presentation (myPresentationManager->StructureManager());
223   myPresentableObject->Compute (Projector (theProjector), theTrsf, aPrs3d);
224   return aPrs3d;
225 }
226
227 //=======================================================================
228 //function : Compute
229 //purpose  :
230 //=======================================================================
231 void PrsMgr_Presentation::Compute (const Handle(Graphic3d_DataStructureManager)& theProjector,
232                                    const Handle(Geom_Transformation)& theTrsf,
233                                    Handle(Graphic3d_Structure)& theStructToFill)
234 {
235   // recompute HLR after transformation in all the case
236   Handle(Graphic3d_Structure) aPrs = theStructToFill;
237   theStructToFill->Clear();
238   myPresentableObject->Compute (Projector (theProjector), theTrsf, aPrs);
239 }
240
241 //=======================================================================
242 //function : Projector
243 //purpose  :
244 //=======================================================================
245 Handle(Prs3d_Projector) PrsMgr_Presentation::Projector (const Handle(Graphic3d_DataStructureManager)& theProjector)
246 {
247   Handle(Graphic3d_Camera) aCamera = Handle(Graphic3d_CView)::DownCast (theProjector)->Camera();
248   const gp_Dir aDir = aCamera->Direction().Reversed();
249   const gp_Pnt anAt = aCamera->Center();
250   const gp_Dir anUp = aCamera->Up();
251   Handle(Prs3d_Projector) aProj = new Prs3d_Projector (!aCamera->IsOrthographic(),
252                                                        aCamera->Scale(),
253                                                        aDir.X(), aDir.Y(), aDir.Z(),
254                                                        anAt.X(), anAt.Y(), anAt.Z(),
255                                                        anUp.X(), anUp.Y(), anUp.Z());
256   return aProj;
257 }
258
259 //=======================================================================
260 //function : ~PrsMgr_Presentation
261 //purpose  :
262 //=======================================================================
263 PrsMgr_Presentation::~PrsMgr_Presentation()
264 {
265   Erase();
266 }