401c74e0915a732a293ec256a4362ae320b0a622
[occt.git] / src / PrsMgr / PrsMgr_Presentation3d.cxx
1 // Created on: 1993-10-21
2 // Created by: Jean-Louis FRENKEL
3 // Copyright (c) 1993-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 // Modified by  rob 09-oct-96
18
19
20 #define IMP040200       //GG Recompute HLR after transformation
21 //                      in all the case.
22
23 #include <PrsMgr_Presentation3d.ixx>
24 #include <PrsMgr_PresentationManager.hxx>
25 #include <PrsMgr_Prs.hxx>
26 #include <PrsMgr_ModedPresentation.hxx>
27 #include <Visual3d_View.hxx>
28 #include <Graphic3d_Structure.hxx>
29 #include <Precision.hxx>
30
31 PrsMgr_Presentation3d::PrsMgr_Presentation3d (const Handle(PrsMgr_PresentationManager3d)& aPresentationManager, 
32                                               const Handle(PrsMgr_PresentableObject)& aPresentableObject)
33 : PrsMgr_Presentation(aPresentationManager), 
34   myDisplayReason(Standard_False),
35   myPresentableObject (aPresentableObject.operator->())
36 {
37   myStructure = new PrsMgr_Prs(aPresentationManager->StructureManager(),
38                                this, aPresentableObject->TypeOfPresentation3d());
39   myStructure->SetOwner(myPresentableObject);
40 }
41
42 PrsMgr_KindOfPrs PrsMgr_Presentation3d::KindOfPresentation() const
43 {return PrsMgr_KOP_3D;}
44
45
46 void PrsMgr_Presentation3d::Display()
47 {
48   Display (Standard_False);
49   myDisplayReason = Standard_False;
50 }
51
52 void PrsMgr_Presentation3d::Display(const Standard_Boolean theIsHighlight)
53 {
54   if (!myStructure->IsDisplayed())
55   {
56     myStructure->Display();
57     myDisplayReason = theIsHighlight;
58   }
59   else if (!myStructure->IsVisible())
60   {
61     myStructure->SetVisible (Standard_True);
62     myDisplayReason = theIsHighlight;
63   }
64 }
65
66 void PrsMgr_Presentation3d::Erase ()
67 {
68   if (myStructure.IsNull())
69   {
70     return;
71   }
72
73   // Erase structure from structure manager
74   myStructure->Erase();
75   myStructure->Clear();
76   // Disconnect other structures
77   myStructure->DisconnectAll (Graphic3d_TOC_DESCENDANT);
78   // Clear groups and remove graphic structure
79   myStructure.Nullify();
80 }
81
82 void PrsMgr_Presentation3d::SetVisible (const Standard_Boolean theValue)
83 {
84   myStructure->SetVisible (theValue);
85 }
86
87 void PrsMgr_Presentation3d::Highlight () {
88   Display (Standard_True);
89   myStructure->Highlight();}
90
91 void PrsMgr_Presentation3d::Unhighlight () const {
92   myStructure->UnHighlight();
93   if (myDisplayReason) myStructure->SetVisible (Standard_False);
94 }
95
96 void PrsMgr_Presentation3d::Clear() {
97   // This modification remove the contain of the structure:
98   // Consequence: 
99   //    1. The memory zone of the group is reused
100   //    2. The speed for animation is constant
101   //myPresentableObject = NULL;
102   SetUpdateStatus(Standard_True);
103   if (myStructure.IsNull())
104   {
105     return;
106   }
107
108   myStructure->Clear(Standard_True);
109   //  myStructure->Clear(Standard_False);
110   myStructure->RemoveAll();
111 }
112
113 void PrsMgr_Presentation3d::Color(const Quantity_NameOfColor aColor){
114   Display (Standard_True);
115   myStructure->Color(aColor);
116 }
117
118 void PrsMgr_Presentation3d::BoundBox() const {
119    myStructure->BoundBox();
120 }
121
122 Standard_Boolean PrsMgr_Presentation3d::IsDisplayed () const {
123   return myStructure->IsDisplayed() && myStructure->IsVisible() && !myDisplayReason;
124 }        
125
126 Standard_Boolean PrsMgr_Presentation3d::IsHighlighted () const {
127   return myStructure->IsHighlighted();
128 }
129
130
131 Standard_Integer PrsMgr_Presentation3d::DisplayPriority() const {
132 return myStructure->DisplayPriority();
133 }
134
135 void PrsMgr_Presentation3d::SetDisplayPriority(const Standard_Integer TheNewPrior)
136 {
137   myStructure->SetDisplayPriority(TheNewPrior);
138 }
139
140 Handle(Prs3d_Presentation) PrsMgr_Presentation3d::Presentation() const {
141   return myStructure;
142 }
143 void PrsMgr_Presentation3d::Connect(const Handle(PrsMgr_Presentation3d)& anOtherPresentation) const {
144   myStructure->Connect(anOtherPresentation->Presentation());
145 }
146
147 void PrsMgr_Presentation3d::Transform (const Handle(Geom_Transformation)& aTransformation) const {
148   myStructure->Transform(aTransformation);
149 }
150
151 void PrsMgr_Presentation3d::Place (const Quantity_Length X,
152                               const Quantity_Length Y,
153                               const Quantity_Length Z) const {
154   myStructure->Place(X,Y,Z);
155 }
156
157 void PrsMgr_Presentation3d::Multiply (const Handle(Geom_Transformation)& aTransformation) const {
158   myStructure->Multiply(aTransformation);
159 }
160
161 void PrsMgr_Presentation3d::Move (const Quantity_Length X,
162                               const Quantity_Length Y,
163                               const Quantity_Length Z) const {
164   myStructure->Move(X,Y,Z);
165 }
166
167 void PrsMgr_Presentation3d::SetShadingAspect(const Handle(Prs3d_ShadingAspect)& aShadingAspect) const {
168   myStructure->SetShadingAspect(aShadingAspect);
169 }
170
171
172
173 //=======================================================================
174 //function : Compute
175 //purpose  : Methods for hidden parts...
176 //=======================================================================
177
178 Handle(Graphic3d_Structure) PrsMgr_Presentation3d::
179 Compute(const Handle(Graphic3d_DataStructureManager)& aProjector)
180 {
181 #ifdef DEB
182   cout<<"passage in g = Compute(P) "<<endl;
183 #endif
184   Handle(Prs3d_Presentation) g = new Prs3d_Presentation(Handle(PrsMgr_PresentationManager3d)::DownCast(PresentationManager())->StructureManager());
185   myPresentableObject->Compute(Projector(aProjector),g);
186   return g;
187 }
188
189 //=======================================================================
190 //function : Compute
191 //purpose  : 
192 //=======================================================================
193
194 void PrsMgr_Presentation3d::Compute (const Handle(Graphic3d_Structure)& theStructure)
195 {
196   Standard_Integer aDispMode = 0;
197
198   Standard_Integer aPresentationsNumber = myPresentableObject->myPresentations.Length();
199   for (Standard_Integer anIter = 1; anIter <= aPresentationsNumber; anIter++)
200   {
201     const PrsMgr_ModedPresentation& aModedPresentation = myPresentableObject->myPresentations.Value (anIter);
202     Handle(PrsMgr_Presentation) aPresentation = aModedPresentation.Presentation();
203     PrsMgr_Presentation3d* aPresentation3d = (PrsMgr_Presentation3d* )aPresentation.operator->();
204     if (aPresentation3d == this)
205     {
206       aDispMode = aModedPresentation.Mode();
207       break;
208     }
209   }
210
211   Handle(Prs3d_Presentation) aPrs3d = Handle(Prs3d_Presentation)::DownCast (theStructure);
212
213   myPresentableObject->Compute(
214     Handle(PrsMgr_PresentationManager3d)::DownCast (PresentationManager()),
215     aPrs3d,
216     aDispMode);
217 }
218
219 //=======================================================================
220 //function : Compute
221 //purpose  : 
222 //=======================================================================
223
224 void PrsMgr_Presentation3d::Compute(const Handle(Graphic3d_DataStructureManager)& aProjector,
225                                     const Handle(Graphic3d_Structure)& TheStructToFill)
226 {
227 #ifdef DEB
228   cout<<"passage in Compute(P,Str)"<<endl;
229 #endif
230   TheStructToFill->Clear();
231   const Handle(Prs3d_Presentation)& P = *((Handle(Prs3d_Presentation)*) &TheStructToFill);
232   myPresentableObject->Compute(Projector(aProjector),P);
233 }
234
235 //=======================================================================
236 //function : Compute
237 //purpose  : 
238 //=======================================================================
239 Handle(Graphic3d_Structure) PrsMgr_Presentation3d::
240 Compute(const Handle(Graphic3d_DataStructureManager)& aProjector,
241         const Handle(Geom_Transformation)& TheTrsf)
242 {
243 #ifdef DEB
244   cout<<"passage in G =  Compute(P,Trsf)"<<endl;
245 #endif
246   Handle(Prs3d_Presentation) g = new Prs3d_Presentation(Handle(PrsMgr_PresentationManager3d)::DownCast(PresentationManager())->StructureManager());
247
248
249   if(TheTrsf->Form()== gp_Translation){
250 #ifdef DEB
251     cout<<"\tla Trsf est une translation"<<endl;
252 #endif
253    
254     myPresentableObject->Compute(Projector(aProjector),g);
255     g->Transform(TheTrsf);
256   }
257   else{
258     // waiting that something is done in gp_Trsf...rob
259     Standard_Boolean good (Standard_True);
260     for (Standard_Integer i=1;i<=3 && good ;i++){
261       for (Standard_Integer j=1;j<=3 && good ;j++){
262         if(i!=j){
263           if(Abs(TheTrsf->Value(i,j)) > Precision::Confusion())
264             good = Standard_False;
265         }
266       }
267     }
268     
269     if(good){
270 #ifdef DEB
271       cout<<"\t it is checked if Trsf is a Translation"<<endl;
272 #endif
273     myPresentableObject->Compute(Projector(aProjector),g);
274     g->Transform(TheTrsf);
275       
276     }
277     else{
278 #ifdef DEB
279       cout<<"\t Trsf is not only translation..."<<endl;
280 #endif
281       myPresentableObject->Compute(Projector(aProjector),TheTrsf,g);
282     }
283   }
284   return g;
285 }
286
287 //=======================================================================
288 //function : Compute
289 //purpose  : 
290 //=======================================================================
291 void PrsMgr_Presentation3d::Compute(const Handle(Graphic3d_DataStructureManager)& aProjector,
292                                     const Handle(Geom_Transformation)& TheTrsf,
293                                     const Handle(Graphic3d_Structure)& TheStructToFill)
294 {
295   
296 #ifdef DEB
297   cout<<"passage in Compute(P,Trsf,Str)"<<endl;
298 #endif
299
300   Handle(Prs3d_Presentation) P = *((Handle(Prs3d_Presentation)*)&TheStructToFill);
301  
302 #ifdef IMP040200        
303   TheStructToFill->Clear();
304   myPresentableObject->Compute(Projector(aProjector),TheTrsf,P);
305 #else   //Does not work properly, HLR seems deactivated for view rotation 
306   if(TheTrsf->Form()== gp_Translation){
307 #ifdef DEB
308     cout<<"\t Trsf is a translation"<<endl;
309 #endif
310 //    myPresentableObject->Compute(Projector(aProjector),P);
311     P->Transform(TheTrsf);
312   }
313   else{
314     //  waiting that something is done in gp_Trsf...rob
315     Standard_Boolean good (Standard_True);
316     for (Standard_Integer i=1;i<=3 && good ;i++){
317       for (Standard_Integer j=1;j<=3 && good ;j++){
318         if(i!=j){
319           if(Abs(TheTrsf->Value(i,j)) > Precision::Confusion())
320             good = Standard_False;
321         }
322       }
323     }
324     if(good && !TheStructToFill->IsEmpty()){
325 #ifdef DEB
326       cout<<"\t it is checked if Trsf is a Translation"<<endl;
327 #endif
328       
329       P->Transform(TheTrsf);
330     }
331     else{
332       TheStructToFill->Clear();
333       
334 #ifdef DEB
335       cout<<"\t Trsf is not only translation..."<<endl;
336 #endif
337       
338       myPresentableObject->Compute(Projector(aProjector),TheTrsf,P);
339     }
340   }
341 #endif
342 }
343
344
345
346
347
348
349 //=======================================================================
350 //function : Projector
351 //purpose  : 
352 //=======================================================================
353
354 Handle(Prs3d_Projector) PrsMgr_Presentation3d::Projector(const Handle(Graphic3d_DataStructureManager)& aProjector)
355 {
356   const Handle(Graphic3d_Camera)& aCamera = Handle(Visual3d_View)::DownCast (aProjector)->Camera();
357
358   Standard_Real DX, DY, DZ,XAt, YAt , ZAt,XUp, YUp, ZUp;
359   gp_Dir aDir = aCamera->Direction().Reversed();
360   DX = aDir.X(); DY = aDir.Y(); DZ = aDir.Z();
361
362   gp_Pnt anAt = aCamera->Center();
363   XAt = anAt.X(); YAt = anAt.Y(); ZAt = anAt.Z();
364
365   gp_Dir anUp = aCamera->Up();
366   XUp = anUp.X(); YUp = anUp.Y(); ZUp = anUp.Z();
367
368   Standard_Boolean pers = !aCamera->IsOrthographic();
369   Standard_Real focale  =  aCamera->Scale();
370
371   Handle(Prs3d_Projector) aProj = 
372     new Prs3d_Projector(pers, focale, DX, DY, DZ, XAt, YAt, ZAt, XUp, YUp, ZUp);
373   return aProj;
374
375 }
376
377 void PrsMgr_Presentation3d::Destroy () {
378   if (!myStructure.IsNull())
379     myStructure->Clear();
380   myStructure.Nullify();
381 }
382
383 //=======================================================================
384 //function : SetZLayer
385 //purpose  :
386 //=======================================================================
387
388 void PrsMgr_Presentation3d::SetZLayer (Standard_Integer theLayerId)
389 {
390   myStructure->SetZLayer (theLayerId);
391 }
392
393 //=======================================================================
394 //function : GetZLayer
395 //purpose  :  
396 //=======================================================================
397
398 Standard_Integer PrsMgr_Presentation3d::GetZLayer () const
399 {
400   return myStructure->GetZLayer ();
401 }