075eb7d02f6c2daff11845b8bdcff12e606b74ab
[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   // Erase structure from structure manager
69   myStructure->Erase();
70   myStructure->Clear();
71   // Disconnect other structures
72   myStructure->DisconnectAll (Graphic3d_TOC_DESCENDANT);
73   // Clear groups and remove graphic structure
74   myStructure.Nullify();
75 }
76
77 void PrsMgr_Presentation3d::SetVisible (const Standard_Boolean theValue)
78 {
79   myStructure->SetVisible (theValue);
80 }
81
82 void PrsMgr_Presentation3d::Highlight () {
83   Display (Standard_True);
84   myStructure->Highlight();}
85
86 void PrsMgr_Presentation3d::Unhighlight () const {
87   myStructure->UnHighlight();
88   if (myDisplayReason) myStructure->SetVisible (Standard_False);
89 }
90
91 void PrsMgr_Presentation3d::Clear() {
92   // This modification remove the contain of the structure:
93   // Consequence: 
94   //    1. The memory zone of the group is reused
95   //    2. The speed for animation is constant
96   //myPresentableObject = NULL;
97   SetUpdateStatus(Standard_True);
98   myStructure->Clear(Standard_True);
99   //  myStructure->Clear(Standard_False);
100
101   myStructure->RemoveAll();
102 }
103
104 void PrsMgr_Presentation3d::Color(const Quantity_NameOfColor aColor){
105   Display (Standard_True);
106   myStructure->Color(aColor);
107 }
108
109 void PrsMgr_Presentation3d::BoundBox() const {
110    myStructure->BoundBox();
111 }
112
113 Standard_Boolean PrsMgr_Presentation3d::IsDisplayed () const {
114   return myStructure->IsDisplayed() && myStructure->IsVisible() && !myDisplayReason;
115 }        
116
117 Standard_Boolean PrsMgr_Presentation3d::IsHighlighted () const {
118   return myStructure->IsHighlighted();
119 }
120
121
122 Standard_Integer PrsMgr_Presentation3d::DisplayPriority() const {
123 return myStructure->DisplayPriority();
124 }
125
126 void PrsMgr_Presentation3d::SetDisplayPriority(const Standard_Integer TheNewPrior)
127 {
128   myStructure->SetDisplayPriority(TheNewPrior);
129 }
130
131 Handle(Prs3d_Presentation) PrsMgr_Presentation3d::Presentation() const {
132   return myStructure;
133 }
134 void PrsMgr_Presentation3d::Connect(const Handle(PrsMgr_Presentation3d)& anOtherPresentation) const {
135   myStructure->Connect(anOtherPresentation->Presentation());
136 }
137
138 void PrsMgr_Presentation3d::Transform (const Handle(Geom_Transformation)& aTransformation) const {
139   myStructure->Transform(aTransformation);
140 }
141
142 void PrsMgr_Presentation3d::Place (const Quantity_Length X,
143                               const Quantity_Length Y,
144                               const Quantity_Length Z) const {
145   myStructure->Place(X,Y,Z);
146 }
147
148 void PrsMgr_Presentation3d::Multiply (const Handle(Geom_Transformation)& aTransformation) const {
149   myStructure->Multiply(aTransformation);
150 }
151
152 void PrsMgr_Presentation3d::Move (const Quantity_Length X,
153                               const Quantity_Length Y,
154                               const Quantity_Length Z) const {
155   myStructure->Move(X,Y,Z);
156 }
157
158 void PrsMgr_Presentation3d::SetShadingAspect(const Handle(Prs3d_ShadingAspect)& aShadingAspect) const {
159   myStructure->SetShadingAspect(aShadingAspect);
160 }
161
162
163
164 //=======================================================================
165 //function : Compute
166 //purpose  : Methods for hidden parts...
167 //=======================================================================
168
169 Handle(Graphic3d_Structure) PrsMgr_Presentation3d::
170 Compute(const Handle(Graphic3d_DataStructureManager)& aProjector)
171 {
172 #ifdef DEB
173   cout<<"passage in g = Compute(P) "<<endl;
174 #endif
175   Handle(Prs3d_Presentation) g = new Prs3d_Presentation(Handle(PrsMgr_PresentationManager3d)::DownCast(PresentationManager())->StructureManager());
176   myPresentableObject->Compute(Projector(aProjector),g);
177   return g;
178 }
179
180 //=======================================================================
181 //function : Compute
182 //purpose  : 
183 //=======================================================================
184
185 void PrsMgr_Presentation3d::Compute (const Handle(Graphic3d_Structure)& theStructure)
186 {
187   Standard_Integer aDispMode = 0;
188
189   Standard_Integer aPresentationsNumber = myPresentableObject->myPresentations.Length();
190   for (Standard_Integer anIter = 1; anIter <= aPresentationsNumber; anIter++)
191   {
192     const PrsMgr_ModedPresentation& aModedPresentation = myPresentableObject->myPresentations.Value (anIter);
193     Handle(PrsMgr_Presentation) aPresentation = aModedPresentation.Presentation();
194     PrsMgr_Presentation3d* aPresentation3d = (PrsMgr_Presentation3d* )aPresentation.operator->();
195     if (aPresentation3d == this)
196     {
197       aDispMode = aModedPresentation.Mode();
198       break;
199     }
200   }
201
202   Handle(Prs3d_Presentation) aPrs3d = Handle(Prs3d_Presentation)::DownCast (theStructure);
203
204   myPresentableObject->Compute(
205     Handle(PrsMgr_PresentationManager3d)::DownCast (PresentationManager()),
206     aPrs3d,
207     aDispMode);
208 }
209
210 //=======================================================================
211 //function : Compute
212 //purpose  : 
213 //=======================================================================
214
215 void PrsMgr_Presentation3d::Compute(const Handle(Graphic3d_DataStructureManager)& aProjector,
216                                     const Handle(Graphic3d_Structure)& TheStructToFill)
217 {
218 #ifdef DEB
219   cout<<"passage in Compute(P,Str)"<<endl;
220 #endif
221   TheStructToFill->Clear();
222   const Handle(Prs3d_Presentation)& P = *((Handle(Prs3d_Presentation)*) &TheStructToFill);
223   myPresentableObject->Compute(Projector(aProjector),P);
224 }
225
226 //=======================================================================
227 //function : Compute
228 //purpose  : 
229 //=======================================================================
230 Handle(Graphic3d_Structure) PrsMgr_Presentation3d::
231 Compute(const Handle(Graphic3d_DataStructureManager)& aProjector,
232         const Handle(Geom_Transformation)& TheTrsf)
233 {
234 #ifdef DEB
235   cout<<"passage in G =  Compute(P,Trsf)"<<endl;
236 #endif
237   Handle(Prs3d_Presentation) g = new Prs3d_Presentation(Handle(PrsMgr_PresentationManager3d)::DownCast(PresentationManager())->StructureManager());
238
239
240   if(TheTrsf->Form()== gp_Translation){
241 #ifdef DEB
242     cout<<"\tla Trsf est une translation"<<endl;
243 #endif
244    
245     myPresentableObject->Compute(Projector(aProjector),g);
246     g->Transform(TheTrsf);
247   }
248   else{
249     // waiting that something is done in gp_Trsf...rob
250     Standard_Boolean good (Standard_True);
251     for (Standard_Integer i=1;i<=3 && good ;i++){
252       for (Standard_Integer j=1;j<=3 && good ;j++){
253         if(i!=j){
254           if(Abs(TheTrsf->Value(i,j)) > Precision::Confusion())
255             good = Standard_False;
256         }
257       }
258     }
259     
260     if(good){
261 #ifdef DEB
262       cout<<"\t it is checked if Trsf is a Translation"<<endl;
263 #endif
264     myPresentableObject->Compute(Projector(aProjector),g);
265     g->Transform(TheTrsf);
266       
267     }
268     else{
269 #ifdef DEB
270       cout<<"\t Trsf is not only translation..."<<endl;
271 #endif
272       myPresentableObject->Compute(Projector(aProjector),TheTrsf,g);
273     }
274   }
275   return g;
276 }
277
278 //=======================================================================
279 //function : Compute
280 //purpose  : 
281 //=======================================================================
282 void PrsMgr_Presentation3d::Compute(const Handle(Graphic3d_DataStructureManager)& aProjector,
283                                     const Handle(Geom_Transformation)& TheTrsf,
284                                     const Handle(Graphic3d_Structure)& TheStructToFill)
285 {
286   
287 #ifdef DEB
288   cout<<"passage in Compute(P,Trsf,Str)"<<endl;
289 #endif
290
291   Handle(Prs3d_Presentation) P = *((Handle(Prs3d_Presentation)*)&TheStructToFill);
292  
293 #ifdef IMP040200        
294   TheStructToFill->Clear();
295   myPresentableObject->Compute(Projector(aProjector),TheTrsf,P);
296 #else   //Does not work properly, HLR seems deactivated for view rotation 
297   if(TheTrsf->Form()== gp_Translation){
298 #ifdef DEB
299     cout<<"\t Trsf is a translation"<<endl;
300 #endif
301 //    myPresentableObject->Compute(Projector(aProjector),P);
302     P->Transform(TheTrsf);
303   }
304   else{
305     //  waiting that something is done in gp_Trsf...rob
306     Standard_Boolean good (Standard_True);
307     for (Standard_Integer i=1;i<=3 && good ;i++){
308       for (Standard_Integer j=1;j<=3 && good ;j++){
309         if(i!=j){
310           if(Abs(TheTrsf->Value(i,j)) > Precision::Confusion())
311             good = Standard_False;
312         }
313       }
314     }
315     if(good && !TheStructToFill->IsEmpty()){
316 #ifdef DEB
317       cout<<"\t it is checked if Trsf is a Translation"<<endl;
318 #endif
319       
320       P->Transform(TheTrsf);
321     }
322     else{
323       TheStructToFill->Clear();
324       
325 #ifdef DEB
326       cout<<"\t Trsf is not only translation..."<<endl;
327 #endif
328       
329       myPresentableObject->Compute(Projector(aProjector),TheTrsf,P);
330     }
331   }
332 #endif
333 }
334
335
336
337
338
339
340 //=======================================================================
341 //function : Projector
342 //purpose  : 
343 //=======================================================================
344
345 Handle(Prs3d_Projector) PrsMgr_Presentation3d::Projector(const Handle(Graphic3d_DataStructureManager)& aProjector)
346 {
347   const Handle(Graphic3d_Camera)& aCamera = Handle(Visual3d_View)::DownCast (aProjector)->Camera();
348
349   Standard_Real DX, DY, DZ,XAt, YAt , ZAt,XUp, YUp, ZUp;
350   gp_Dir aDir = aCamera->Direction().Reversed();
351   DX = aDir.X(); DY = aDir.Y(); DZ = aDir.Z();
352
353   gp_Pnt anAt = aCamera->Center();
354   XAt = anAt.X(); YAt = anAt.Y(); ZAt = anAt.Z();
355
356   gp_Dir anUp = aCamera->Up();
357   XUp = anUp.X(); YUp = anUp.Y(); ZUp = anUp.Z();
358
359   Standard_Boolean pers = !aCamera->IsOrthographic();
360   Standard_Real focale  =  aCamera->Scale();
361
362   Handle(Prs3d_Projector) aProj = 
363     new Prs3d_Projector(pers, focale, DX, DY, DZ, XAt, YAt, ZAt, XUp, YUp, ZUp);
364   return aProj;
365
366 }
367
368 void PrsMgr_Presentation3d::Destroy () {
369   if (!myStructure.IsNull())
370     myStructure->Clear();
371   myStructure.Nullify();
372 }
373
374 //=======================================================================
375 //function : SetZLayer
376 //purpose  :
377 //=======================================================================
378
379 void PrsMgr_Presentation3d::SetZLayer (Standard_Integer theLayerId)
380 {
381   myStructure->SetZLayer (theLayerId);
382 }
383
384 //=======================================================================
385 //function : GetZLayer
386 //purpose  :  
387 //=======================================================================
388
389 Standard_Integer PrsMgr_Presentation3d::GetZLayer () const
390 {
391   return myStructure->GetZLayer ();
392 }