0026885: Visualization - drop redundant aspects from structure level
[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_Presentation.hxx>
22 #include <Prs3d_Projector.hxx>
23 #include <PrsMgr_ModedPresentation.hxx>
24 #include <PrsMgr_PresentableObject.hxx>
25 #include <PrsMgr_PresentationManager.hxx>
26 #include <PrsMgr_Prs.hxx>
27 #include <Quantity_Color.hxx>
28 #include <Standard_Type.hxx>
29 #include <Graphic3d_CView.hxx>
30
31 IMPLEMENT_STANDARD_RTTIEXT(PrsMgr_Presentation,MMgt_TShared)
32
33 namespace
34 {
35   enum BeforeHighlightState
36   {
37     State_Empty,
38     State_Hidden,
39     State_Visible
40   };
41
42   static BeforeHighlightState StructureState(const Handle(Prs3d_Presentation)& theStructure)
43   {
44     return !theStructure->IsDisplayed() ?
45       State_Empty : !theStructure->IsVisible() ?
46         State_Hidden : State_Visible;
47   }
48 }
49
50 //=======================================================================
51 //function : PrsMgr_Presentation
52 //purpose  :
53 //=======================================================================
54 PrsMgr_Presentation::PrsMgr_Presentation (const Handle(PrsMgr_PresentationManager3d)& thePrsMgr,
55                                           const Handle(PrsMgr_PresentableObject)&     thePrsObject)
56 : myPresentationManager  (thePrsMgr),
57   myPresentableObject    (thePrsObject.operator->()),
58   myMustBeUpdated        (Standard_False),
59   myBeforeHighlightState (State_Empty)
60 {
61   myStructure = new PrsMgr_Prs (thePrsMgr->StructureManager(),
62                                 this, thePrsObject->TypeOfPresentation3d());
63   myStructure->SetOwner (myPresentableObject);
64   myStructure->SetMutable (myPresentableObject->IsMutable());
65 }
66
67 //=======================================================================
68 //function : Display
69 //purpose  :
70 //=======================================================================
71 void PrsMgr_Presentation::Display()
72 {
73   display (Standard_False);
74   myBeforeHighlightState = State_Visible;
75 }
76
77 //=======================================================================
78 //function : display
79 //purpose  :
80 //=======================================================================
81 void PrsMgr_Presentation::display (const Standard_Boolean theIsHighlight)
82 {
83   if (!myStructure->IsDisplayed())
84   {
85     myStructure->SetIsForHighlight (theIsHighlight);
86     myStructure->Display();
87   }
88   else if (!myStructure->IsVisible())
89   {
90     SetVisible (Standard_True);
91     myStructure->SetIsForHighlight (theIsHighlight);
92   }
93 }
94
95 //=======================================================================
96 //function : Erase
97 //purpose  :
98 //=======================================================================
99 void PrsMgr_Presentation::Erase()
100 {
101   if (myStructure.IsNull())
102   {
103     return;
104   }
105
106   // Erase structure from structure manager
107   myStructure->Erase();
108   myStructure->Clear();
109   // Disconnect other structures
110   myStructure->DisconnectAll (Graphic3d_TOC_DESCENDANT);
111   // Clear groups and remove graphic structure
112   myStructure.Nullify();
113 }
114
115 //=======================================================================
116 //function : SetVisible
117 //purpose  :
118 //=======================================================================
119 void PrsMgr_Presentation::SetVisible (const Standard_Boolean theValue)
120 {
121   myStructure->SetVisible (theValue);
122 }
123
124 //=======================================================================
125 //function : Highlight
126 //purpose  :
127 //=======================================================================
128 void PrsMgr_Presentation::Highlight (const Aspect_TypeOfHighlightMethod theMethod,
129                                      const Quantity_Color&              theColor)
130 {
131   if (!IsHighlighted())
132   {
133     myBeforeHighlightState = StructureState (myStructure);
134   }
135
136   display (Standard_True);
137   myStructure->Highlight (theMethod, theColor);
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 : Transform
230 //purpose  :
231 //=======================================================================
232 void PrsMgr_Presentation::Transform (const Handle(Geom_Transformation)& theTrsf) const
233 {
234   myStructure->Transform (theTrsf);
235 }
236
237 //=======================================================================
238 //function : Place
239 //purpose  :
240 //=======================================================================
241 void PrsMgr_Presentation::Place (const Quantity_Length theX,
242                                  const Quantity_Length theY,
243                                  const Quantity_Length theZ) const
244 {
245   myStructure->Place (theX, theY, theZ);
246 }
247
248 //=======================================================================
249 //function : Multiply
250 //purpose  :
251 //=======================================================================
252 void PrsMgr_Presentation::Multiply (const Handle(Geom_Transformation)& theTrsf) const
253 {
254   myStructure->Multiply (theTrsf);
255 }
256
257 //=======================================================================
258 //function : Move
259 //purpose  :
260 //=======================================================================
261 void PrsMgr_Presentation::Move (const Quantity_Length theX,
262                                 const Quantity_Length theY,
263                                 const Quantity_Length theZ) const
264 {
265   myStructure->Move (theX, theY, theZ);
266 }
267
268 //=======================================================================
269 //function : Compute
270 //purpose  : Methods for hidden parts...
271 //=======================================================================
272 Handle(Graphic3d_Structure) PrsMgr_Presentation::Compute (const Handle(Graphic3d_DataStructureManager)& theProjector)
273 {
274   Handle(Prs3d_Presentation) aPrs = new Prs3d_Presentation (myPresentationManager->StructureManager());
275   myPresentableObject->Compute (Projector (theProjector), aPrs);
276   return aPrs;
277 }
278
279 //=======================================================================
280 //function : Compute
281 //purpose  :
282 //=======================================================================
283 void PrsMgr_Presentation::Compute (const Handle(Graphic3d_Structure)& theStructure)
284 {
285   Standard_Integer aDispMode = 0;
286   Standard_Integer aPresentationsNumber = myPresentableObject->myPresentations.Length();
287   for (Standard_Integer anIter = 1; anIter <= aPresentationsNumber; ++anIter)
288   {
289     const PrsMgr_ModedPresentation& aModedPresentation = myPresentableObject->myPresentations.Value (anIter);
290     if (aModedPresentation.Presentation().operator->() == this)
291     {
292       aDispMode = aModedPresentation.Mode();
293       break;
294     }
295   }
296
297   Handle(Prs3d_Presentation) aPrs3d = Handle(Prs3d_Presentation)::DownCast (theStructure);
298   myPresentableObject->Compute (myPresentationManager, aPrs3d, aDispMode);
299 }
300
301 //=======================================================================
302 //function : Compute
303 //purpose  :
304 //=======================================================================
305 void PrsMgr_Presentation::Compute (const Handle(Graphic3d_DataStructureManager)& theProjector,
306                                    const Handle(Graphic3d_Structure)&            theStructToFill)
307 {
308   theStructToFill->Clear();
309   Handle(Prs3d_Presentation) aPrs (Handle(Prs3d_Presentation)::DownCast (theStructToFill));
310   myPresentableObject->Compute (Projector (theProjector), aPrs);
311 }
312
313 //=======================================================================
314 //function : Compute
315 //purpose  :
316 //=======================================================================
317 Handle(Graphic3d_Structure) PrsMgr_Presentation::Compute (const Handle(Graphic3d_DataStructureManager)& theProjector,
318                                                           const Handle(Geom_Transformation)&            theTrsf)
319 {
320   Handle(Prs3d_Presentation) aPrs3d = new Prs3d_Presentation (myPresentationManager->StructureManager());
321   if (theTrsf->Form() == gp_Translation)
322   {
323     myPresentableObject->Compute (Projector (theProjector), aPrs3d);
324     aPrs3d->Transform (theTrsf);
325     return aPrs3d;
326   }
327
328   // waiting that something is done in gp_Trsf...rob
329   for (Standard_Integer i = 1; i <= 3; ++i)
330   {
331     for (Standard_Integer j = 1; j <= 3; ++j)
332     {
333       if (i != j)
334       {
335         if (Abs (theTrsf->Value (i, j)) > Precision::Confusion())
336         {
337           myPresentableObject->Compute (Projector (theProjector), theTrsf, aPrs3d);
338           return aPrs3d;
339         }
340       }
341     }
342   }
343
344   myPresentableObject->Compute (Projector (theProjector), aPrs3d);
345   aPrs3d->Transform (theTrsf);
346   return aPrs3d;
347 }
348
349 //=======================================================================
350 //function : Compute
351 //purpose  :
352 //=======================================================================
353 void PrsMgr_Presentation::Compute (const Handle(Graphic3d_DataStructureManager)& theProjector,
354                                    const Handle(Geom_Transformation)&            theTrsf,
355                                    const Handle(Graphic3d_Structure)&            theStructToFill)
356 {
357   // recompute HLR after transformation in all the case
358   Handle(Prs3d_Presentation) aPrs = Handle(Prs3d_Presentation)::DownCast (theStructToFill);
359   theStructToFill->Clear();
360   myPresentableObject->Compute (Projector (theProjector), theTrsf, aPrs);
361 }
362
363 //=======================================================================
364 //function : Projector
365 //purpose  :
366 //=======================================================================
367 Handle(Prs3d_Projector) PrsMgr_Presentation::Projector (const Handle(Graphic3d_DataStructureManager)& theProjector)
368 {
369   Handle(Graphic3d_Camera) aCamera = Handle(Graphic3d_CView)::DownCast (theProjector)->Camera();
370   const gp_Dir aDir = aCamera->Direction().Reversed();
371   const gp_Pnt anAt = aCamera->Center();
372   const gp_Dir anUp = aCamera->Up();
373   Handle(Prs3d_Projector) aProj = new Prs3d_Projector (!aCamera->IsOrthographic(),
374                                                        aCamera->Scale(),
375                                                        aDir.X(), aDir.Y(), aDir.Z(),
376                                                        anAt.X(), anAt.Y(), anAt.Z(),
377                                                        anUp.X(), anUp.Y(), anUp.Z());
378   return aProj;
379 }
380
381 //=======================================================================
382 //function : ~PrsMgr_Presentation
383 //purpose  :
384 //=======================================================================
385 PrsMgr_Presentation::~PrsMgr_Presentation()
386 {
387   Erase();
388 }
389
390 //=======================================================================
391 //function : SetZLayer
392 //purpose  :
393 //=======================================================================
394 void PrsMgr_Presentation::SetZLayer (Standard_Integer theLayerId)
395 {
396   myStructure->SetZLayer (theLayerId);
397 }
398
399 //=======================================================================
400 //function : GetZLayer
401 //purpose  :
402 //=======================================================================
403 Standard_Integer PrsMgr_Presentation::GetZLayer() const
404 {
405   return myStructure->GetZLayer();
406 }