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