0025695: Visualization, AIS_InteractiveContext - define default HilightMode
[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 <Graphic3d_Structure.hxx>
20 #include <Precision.hxx>
21 #include <Prs3d_Drawer.hxx>
22 #include <Prs3d_Presentation.hxx>
23 #include <Prs3d_Projector.hxx>
24 #include <PrsMgr_ModedPresentation.hxx>
25 #include <PrsMgr_PresentableObject.hxx>
26 #include <PrsMgr_PresentationManager.hxx>
27 #include <PrsMgr_Prs.hxx>
28 #include <Quantity_Color.hxx>
29 #include <Standard_Type.hxx>
30 #include <Graphic3d_CView.hxx>
31
32 IMPLEMENT_STANDARD_RTTIEXT(PrsMgr_Presentation, Standard_Transient)
33
34 namespace
35 {
36   enum BeforeHighlightState
37   {
38     State_Empty,
39     State_Hidden,
40     State_Visible
41   };
42
43   static BeforeHighlightState StructureState(const Handle(Prs3d_Presentation)& theStructure)
44   {
45     return !theStructure->IsDisplayed() ?
46       State_Empty : !theStructure->IsVisible() ?
47         State_Hidden : State_Visible;
48   }
49 }
50
51 //=======================================================================
52 //function : PrsMgr_Presentation
53 //purpose  :
54 //=======================================================================
55 PrsMgr_Presentation::PrsMgr_Presentation (const Handle(PrsMgr_PresentationManager3d)& thePrsMgr,
56                                           const Handle(PrsMgr_PresentableObject)&     thePrsObject)
57 : myPresentationManager  (thePrsMgr),
58   myPresentableObject    (thePrsObject.operator->()),
59   myMustBeUpdated        (Standard_False),
60   myBeforeHighlightState (State_Empty)
61 {
62   myStructure = new PrsMgr_Prs (thePrsMgr->StructureManager(),
63                                 this, thePrsObject->TypeOfPresentation3d());
64   myStructure->SetOwner (myPresentableObject);
65   myStructure->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 (!myStructure->IsDisplayed())
85   {
86     myStructure->SetIsForHighlight (theIsHighlight);
87     myStructure->Display();
88   }
89   else if (!myStructure->IsVisible())
90   {
91     SetVisible (Standard_True);
92     myStructure->SetIsForHighlight (theIsHighlight);
93   }
94 }
95
96 //=======================================================================
97 //function : Erase
98 //purpose  :
99 //=======================================================================
100 void PrsMgr_Presentation::Erase()
101 {
102   if (myStructure.IsNull())
103   {
104     return;
105   }
106
107   // Erase structure from structure manager
108   myStructure->Erase();
109   myStructure->Clear();
110   // Disconnect other structures
111   myStructure->DisconnectAll (Graphic3d_TOC_DESCENDANT);
112   // Clear groups and remove graphic structure
113   myStructure.Nullify();
114 }
115
116 //=======================================================================
117 //function : SetVisible
118 //purpose  :
119 //=======================================================================
120 void PrsMgr_Presentation::SetVisible (const Standard_Boolean theValue)
121 {
122   myStructure->SetVisible (theValue);
123 }
124
125 //=======================================================================
126 //function : Highlight
127 //purpose  :
128 //=======================================================================
129 void PrsMgr_Presentation::Highlight (const Handle(Prs3d_Drawer)& theStyle)
130 {
131   if (!IsHighlighted())
132   {
133     myBeforeHighlightState = StructureState (myStructure);
134   }
135
136   display (Standard_True);
137   myStructure->Highlight (theStyle);
138 }
139
140 //=======================================================================
141 //function : Unhighlight
142 //purpose  :
143 //=======================================================================
144 void PrsMgr_Presentation::Unhighlight() const
145 {
146   myStructure->UnHighlight();
147   switch (myBeforeHighlightState)
148   {
149  case State_Visible:
150     return;
151  case State_Hidden:
152     myStructure->SetVisible (Standard_False);
153     break;
154  case State_Empty:
155     myStructure->Erase();
156     break;
157   }
158 }
159
160 //=======================================================================
161 //function : Clear
162 //purpose  :
163 //=======================================================================
164 void PrsMgr_Presentation::Clear()
165 {
166   // This modification remove the contain of the structure:
167   // Consequence:
168   //    1. The memory zone of the group is reused
169   //    2. The speed for animation is constant
170   //myPresentableObject = NULL;
171   SetUpdateStatus (Standard_True);
172   if (myStructure.IsNull())
173   {
174     return;
175   }
176
177   myStructure->Clear (Standard_True);
178   //  myStructure->Clear(Standard_False);
179   myStructure->RemoveAll();
180 }
181
182 //=======================================================================
183 //function : IsDisplayed
184 //purpose  :
185 //=======================================================================
186 Standard_Boolean PrsMgr_Presentation::IsDisplayed() const
187 {
188   return  myStructure->IsDisplayed()
189       &&  myStructure->IsVisible();
190 }
191
192 //=======================================================================
193 //function : IsHighlighted
194 //purpose  :
195 //=======================================================================
196 Standard_Boolean PrsMgr_Presentation::IsHighlighted() const
197 {
198   return myStructure->IsHighlighted();
199 }
200
201 //=======================================================================
202 //function : DisplayPriority
203 //purpose  :
204 //=======================================================================
205 Standard_Integer PrsMgr_Presentation::DisplayPriority() const
206 {
207   return myStructure->DisplayPriority();
208 }
209
210 //=======================================================================
211 //function : SetDisplayPriority
212 //purpose  :
213 //=======================================================================
214 void PrsMgr_Presentation::SetDisplayPriority (const Standard_Integer theNewPrior)
215 {
216   myStructure->SetDisplayPriority (theNewPrior);
217 }
218
219 //=======================================================================
220 //function : Connect
221 //purpose  :
222 //=======================================================================
223 void PrsMgr_Presentation::Connect (const Handle(PrsMgr_Presentation)& theOther) const
224 {
225   myStructure->Connect (theOther->Presentation());
226 }
227
228 //=======================================================================
229 //function : SetTransformation
230 //purpose  :
231 //=======================================================================
232 void PrsMgr_Presentation::SetTransformation (const Handle(Geom_Transformation)& theTrsf) const
233 {
234   myStructure->SetTransformation (theTrsf);
235 }
236
237 //=======================================================================
238 //function : Compute
239 //purpose  : Methods for hidden parts...
240 //=======================================================================
241 Handle(Graphic3d_Structure) PrsMgr_Presentation::Compute (const Handle(Graphic3d_DataStructureManager)& theProjector)
242 {
243   Handle(Prs3d_Presentation) aPrs = new Prs3d_Presentation (myPresentationManager->StructureManager());
244   myPresentableObject->Compute (Projector (theProjector), aPrs);
245   return aPrs;
246 }
247
248 //=======================================================================
249 //function : Compute
250 //purpose  :
251 //=======================================================================
252 void PrsMgr_Presentation::Compute (const Handle(Graphic3d_Structure)& theStructure)
253 {
254   Standard_Integer aDispMode = 0;
255   Standard_Integer aPresentationsNumber = myPresentableObject->myPresentations.Length();
256   for (Standard_Integer anIter = 1; anIter <= aPresentationsNumber; ++anIter)
257   {
258     const PrsMgr_ModedPresentation& aModedPresentation = myPresentableObject->myPresentations.Value (anIter);
259     if (aModedPresentation.Presentation().operator->() == this)
260     {
261       aDispMode = aModedPresentation.Mode();
262       break;
263     }
264   }
265
266   Handle(Prs3d_Presentation) aPrs3d = Handle(Prs3d_Presentation)::DownCast (theStructure);
267   myPresentableObject->Compute (myPresentationManager, aPrs3d, aDispMode);
268 }
269
270 //=======================================================================
271 //function : Compute
272 //purpose  :
273 //=======================================================================
274 void PrsMgr_Presentation::Compute (const Handle(Graphic3d_DataStructureManager)& theProjector,
275                                    const Handle(Graphic3d_Structure)&            theStructToFill)
276 {
277   theStructToFill->Clear();
278   Handle(Prs3d_Presentation) aPrs (Handle(Prs3d_Presentation)::DownCast (theStructToFill));
279   myPresentableObject->Compute (Projector (theProjector), aPrs);
280 }
281
282 //=======================================================================
283 //function : Compute
284 //purpose  :
285 //=======================================================================
286 Handle(Graphic3d_Structure) PrsMgr_Presentation::Compute (const Handle(Graphic3d_DataStructureManager)& theProjector,
287                                                           const Handle(Geom_Transformation)&            theTrsf)
288 {
289   Handle(Prs3d_Presentation) aPrs3d = new Prs3d_Presentation (myPresentationManager->StructureManager());
290   if (theTrsf->Form() == gp_Translation)
291   {
292     myPresentableObject->Compute (Projector (theProjector), aPrs3d);
293     aPrs3d->SetTransformation (theTrsf);
294     return aPrs3d;
295   }
296
297   // waiting that something is done in gp_Trsf...rob
298   for (Standard_Integer i = 1; i <= 3; ++i)
299   {
300     for (Standard_Integer j = 1; j <= 3; ++j)
301     {
302       if (i != j)
303       {
304         if (Abs (theTrsf->Value (i, j)) > Precision::Confusion())
305         {
306           myPresentableObject->Compute (Projector (theProjector), theTrsf, aPrs3d);
307           return aPrs3d;
308         }
309       }
310     }
311   }
312
313   myPresentableObject->Compute (Projector (theProjector), aPrs3d);
314   aPrs3d->SetTransformation (theTrsf);
315   return aPrs3d;
316 }
317
318 //=======================================================================
319 //function : Compute
320 //purpose  :
321 //=======================================================================
322 void PrsMgr_Presentation::Compute (const Handle(Graphic3d_DataStructureManager)& theProjector,
323                                    const Handle(Geom_Transformation)&            theTrsf,
324                                    const Handle(Graphic3d_Structure)&            theStructToFill)
325 {
326   // recompute HLR after transformation in all the case
327   Handle(Prs3d_Presentation) aPrs = Handle(Prs3d_Presentation)::DownCast (theStructToFill);
328   theStructToFill->Clear();
329   myPresentableObject->Compute (Projector (theProjector), theTrsf, aPrs);
330 }
331
332 //=======================================================================
333 //function : Projector
334 //purpose  :
335 //=======================================================================
336 Handle(Prs3d_Projector) PrsMgr_Presentation::Projector (const Handle(Graphic3d_DataStructureManager)& theProjector)
337 {
338   Handle(Graphic3d_Camera) aCamera = Handle(Graphic3d_CView)::DownCast (theProjector)->Camera();
339   const gp_Dir aDir = aCamera->Direction().Reversed();
340   const gp_Pnt anAt = aCamera->Center();
341   const gp_Dir anUp = aCamera->Up();
342   Handle(Prs3d_Projector) aProj = new Prs3d_Projector (!aCamera->IsOrthographic(),
343                                                        aCamera->Scale(),
344                                                        aDir.X(), aDir.Y(), aDir.Z(),
345                                                        anAt.X(), anAt.Y(), anAt.Z(),
346                                                        anUp.X(), anUp.Y(), anUp.Z());
347   return aProj;
348 }
349
350 //=======================================================================
351 //function : ~PrsMgr_Presentation
352 //purpose  :
353 //=======================================================================
354 PrsMgr_Presentation::~PrsMgr_Presentation()
355 {
356   Erase();
357 }
358
359 //=======================================================================
360 //function : SetZLayer
361 //purpose  :
362 //=======================================================================
363 void PrsMgr_Presentation::SetZLayer (Standard_Integer theLayerId)
364 {
365   myStructure->SetZLayer (theLayerId);
366 }
367
368 //=======================================================================
369 //function : GetZLayer
370 //purpose  :
371 //=======================================================================
372 Standard_Integer PrsMgr_Presentation::GetZLayer() const
373 {
374   return myStructure->GetZLayer();
375 }