0024428: Implementation of LGPL license
[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
9 // under the terms of the GNU Lesser General Public 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 <Visual3d_ViewOrientation.hxx>
29 #include <Graphic3d_Structure.hxx>
30 #include <Precision.hxx>
31
32 PrsMgr_Presentation3d::PrsMgr_Presentation3d (const Handle(PrsMgr_PresentationManager3d)& aPresentationManager, 
33                                               const Handle(PrsMgr_PresentableObject)& aPresentableObject)
34 : PrsMgr_Presentation(aPresentationManager), 
35   myDisplayReason(Standard_False),
36   myPresentableObject (aPresentableObject.operator->())
37 {
38   myStructure = new PrsMgr_Prs(aPresentationManager->StructureManager(),
39                                this, aPresentableObject->TypeOfPresentation3d());
40   myStructure->SetOwner(myPresentableObject);
41 }
42
43 PrsMgr_KindOfPrs PrsMgr_Presentation3d::KindOfPresentation() const
44 {return PrsMgr_KOP_3D;}
45
46
47 void PrsMgr_Presentation3d::Display()
48 {
49   Display (Standard_False);
50   myDisplayReason = Standard_False;
51 }
52
53 void PrsMgr_Presentation3d::Display(const Standard_Boolean theIsHighlight)
54 {
55   if (!myStructure->IsDisplayed())
56   {
57     myStructure->Display();
58     myDisplayReason = theIsHighlight;
59   }
60   else if (!myStructure->IsVisible())
61   {
62     myStructure->SetVisible (Standard_True);
63     myDisplayReason = theIsHighlight;
64   }
65 }
66
67 void PrsMgr_Presentation3d::Erase ()
68 {
69   // Erase structure from structure manager
70   myStructure->Erase();
71   myStructure->Clear();
72   // Disconnect other structures
73   myStructure->DisconnectAll (Graphic3d_TOC_DESCENDANT);
74   // Clear groups and remove graphic structure
75   myStructure.Nullify();
76 }
77
78 void PrsMgr_Presentation3d::SetVisible (const Standard_Boolean theValue)
79 {
80   myStructure->SetVisible (theValue);
81 }
82
83 void PrsMgr_Presentation3d::Highlight () {
84   Display (Standard_True);
85   myStructure->Highlight();}
86
87 void PrsMgr_Presentation3d::Unhighlight () const {
88   myStructure->UnHighlight();
89   if (myDisplayReason) myStructure->SetVisible (Standard_False);
90 }
91
92 void PrsMgr_Presentation3d::Clear() {
93   // This modification remove the contain of the structure:
94   // Consequence: 
95   //    1. The memory zone of the group is reused
96   //    2. The speed for animation is constant
97   //myPresentableObject = NULL;
98   SetUpdateStatus(Standard_True);
99   myStructure->Clear(Standard_True);
100   //  myStructure->Clear(Standard_False);
101
102   myStructure->RemoveAll();
103 }
104
105 void PrsMgr_Presentation3d::Color(const Quantity_NameOfColor aColor){
106   Display (Standard_True);
107   myStructure->Color(aColor);
108 }
109
110 void PrsMgr_Presentation3d::BoundBox() const {
111    myStructure->BoundBox();
112 }
113
114 Standard_Boolean PrsMgr_Presentation3d::IsDisplayed () const {
115   return myStructure->IsDisplayed() && myStructure->IsVisible() && !myDisplayReason;
116 }        
117
118 Standard_Boolean PrsMgr_Presentation3d::IsHighlighted () const {
119   return myStructure->IsHighlighted();
120 }
121
122
123 Standard_Integer PrsMgr_Presentation3d::DisplayPriority() const {
124 return myStructure->DisplayPriority();
125 }
126
127 void PrsMgr_Presentation3d::SetDisplayPriority(const Standard_Integer TheNewPrior)
128 {
129   myStructure->SetDisplayPriority(TheNewPrior);
130 }
131
132 Handle(Prs3d_Presentation) PrsMgr_Presentation3d::Presentation() const {
133   return myStructure;
134 }
135 void PrsMgr_Presentation3d::Connect(const Handle(PrsMgr_Presentation3d)& anOtherPresentation) const {
136   myStructure->Connect(anOtherPresentation->Presentation());
137 }
138
139 void PrsMgr_Presentation3d::Transform (const Handle(Geom_Transformation)& aTransformation) const {
140   myStructure->Transform(aTransformation);
141 }
142
143 void PrsMgr_Presentation3d::Place (const Quantity_Length X,
144                               const Quantity_Length Y,
145                               const Quantity_Length Z) const {
146   myStructure->Place(X,Y,Z);
147 }
148
149 void PrsMgr_Presentation3d::Multiply (const Handle(Geom_Transformation)& aTransformation) const {
150   myStructure->Multiply(aTransformation);
151 }
152
153 void PrsMgr_Presentation3d::Move (const Quantity_Length X,
154                               const Quantity_Length Y,
155                               const Quantity_Length Z) const {
156   myStructure->Move(X,Y,Z);
157 }
158
159 void PrsMgr_Presentation3d::SetShadingAspect(const Handle(Prs3d_ShadingAspect)& aShadingAspect) const {
160   myStructure->SetShadingAspect(aShadingAspect);
161 }
162
163
164
165 //=======================================================================
166 //function : Compute
167 //purpose  : Methods for hidden parts...
168 //=======================================================================
169
170 Handle(Graphic3d_Structure) PrsMgr_Presentation3d::
171 Compute(const Handle(Graphic3d_DataStructureManager)& aProjector)
172 {
173 #ifdef DEB
174   cout<<"passage in g = Compute(P) "<<endl;
175 #endif
176   Handle(Prs3d_Presentation) g = new Prs3d_Presentation(Handle(PrsMgr_PresentationManager3d)::DownCast(PresentationManager())->StructureManager());
177   myPresentableObject->Compute(Projector(aProjector),g);
178   return g;
179 }
180
181 //=======================================================================
182 //function : Compute
183 //purpose  : 
184 //=======================================================================
185
186 void PrsMgr_Presentation3d::Compute (const Handle(Graphic3d_Structure)& theStructure)
187 {
188   Standard_Integer aDispMode = 0;
189
190   Standard_Integer aPresentationsNumber = myPresentableObject->myPresentations.Length();
191   for (Standard_Integer anIter = 1; anIter <= aPresentationsNumber; anIter++)
192   {
193     const PrsMgr_ModedPresentation& aModedPresentation = myPresentableObject->myPresentations.Value (anIter);
194     Handle(PrsMgr_Presentation) aPresentation = aModedPresentation.Presentation();
195     PrsMgr_Presentation3d* aPresentation3d = (PrsMgr_Presentation3d* )aPresentation.operator->();
196     if (aPresentation3d == this)
197     {
198       aDispMode = aModedPresentation.Mode();
199       break;
200     }
201   }
202
203   Handle(Prs3d_Presentation) aPrs3d = Handle(Prs3d_Presentation)::DownCast (theStructure);
204
205   myPresentableObject->Compute(
206     Handle(PrsMgr_PresentationManager3d)::DownCast (PresentationManager()),
207     aPrs3d,
208     aDispMode);
209 }
210
211 //=======================================================================
212 //function : Compute
213 //purpose  : 
214 //=======================================================================
215
216 void PrsMgr_Presentation3d::Compute(const Handle(Graphic3d_DataStructureManager)& aProjector,
217                                     const Handle(Graphic3d_Structure)& TheStructToFill)
218 {
219 #ifdef DEB
220   cout<<"passage in Compute(P,Str)"<<endl;
221 #endif
222   TheStructToFill->Clear();
223   const Handle(Prs3d_Presentation)& P = *((Handle(Prs3d_Presentation)*) &TheStructToFill);
224   myPresentableObject->Compute(Projector(aProjector),P);
225 }
226
227 //=======================================================================
228 //function : Compute
229 //purpose  : 
230 //=======================================================================
231 Handle(Graphic3d_Structure) PrsMgr_Presentation3d::
232 Compute(const Handle(Graphic3d_DataStructureManager)& aProjector,
233         const Handle(Geom_Transformation)& TheTrsf)
234 {
235 #ifdef DEB
236   cout<<"passage in G =  Compute(P,Trsf)"<<endl;
237 #endif
238   Handle(Prs3d_Presentation) g = new Prs3d_Presentation(Handle(PrsMgr_PresentationManager3d)::DownCast(PresentationManager())->StructureManager());
239
240
241   if(TheTrsf->Form()== gp_Translation){
242 #ifdef DEB
243     cout<<"\tla Trsf est une translation"<<endl;
244 #endif
245    
246     myPresentableObject->Compute(Projector(aProjector),g);
247     g->Transform(TheTrsf);
248   }
249   else{
250     // waiting that something is done in gp_Trsf...rob
251     Standard_Boolean good (Standard_True);
252     for (Standard_Integer i=1;i<=3 && good ;i++){
253       for (Standard_Integer j=1;j<=3 && good ;j++){
254         if(i!=j){
255           if(Abs(TheTrsf->Value(i,j)) > Precision::Confusion())
256             good = Standard_False;
257         }
258       }
259     }
260     
261     if(good){
262 #ifdef DEB
263       cout<<"\t it is checked if Trsf is a Translation"<<endl;
264 #endif
265     myPresentableObject->Compute(Projector(aProjector),g);
266     g->Transform(TheTrsf);
267       
268     }
269     else{
270 #ifdef DEB
271       cout<<"\t Trsf is not only translation..."<<endl;
272 #endif
273       myPresentableObject->Compute(Projector(aProjector),TheTrsf,g);
274     }
275   }
276   return g;
277 }
278
279 //=======================================================================
280 //function : Compute
281 //purpose  : 
282 //=======================================================================
283 void PrsMgr_Presentation3d::Compute(const Handle(Graphic3d_DataStructureManager)& aProjector,
284                                     const Handle(Geom_Transformation)& TheTrsf,
285                                     const Handle(Graphic3d_Structure)& TheStructToFill)
286 {
287   
288 #ifdef DEB
289   cout<<"passage in Compute(P,Trsf,Str)"<<endl;
290 #endif
291
292   Handle(Prs3d_Presentation) P = *((Handle(Prs3d_Presentation)*)&TheStructToFill);
293  
294 #ifdef IMP040200        
295   TheStructToFill->Clear();
296   myPresentableObject->Compute(Projector(aProjector),TheTrsf,P);
297 #else   //Does not work properly, HLR seems deactivated for view rotation 
298   if(TheTrsf->Form()== gp_Translation){
299 #ifdef DEB
300     cout<<"\t Trsf is a translation"<<endl;
301 #endif
302 //    myPresentableObject->Compute(Projector(aProjector),P);
303     P->Transform(TheTrsf);
304   }
305   else{
306     //  waiting that something is done in gp_Trsf...rob
307     Standard_Boolean good (Standard_True);
308     for (Standard_Integer i=1;i<=3 && good ;i++){
309       for (Standard_Integer j=1;j<=3 && good ;j++){
310         if(i!=j){
311           if(Abs(TheTrsf->Value(i,j)) > Precision::Confusion())
312             good = Standard_False;
313         }
314       }
315     }
316     if(good && !TheStructToFill->IsEmpty()){
317 #ifdef DEB
318       cout<<"\t it is checked if Trsf is a Translation"<<endl;
319 #endif
320       
321       P->Transform(TheTrsf);
322     }
323     else{
324       TheStructToFill->Clear();
325       
326 #ifdef DEB
327       cout<<"\t Trsf is not only translation..."<<endl;
328 #endif
329       
330       myPresentableObject->Compute(Projector(aProjector),TheTrsf,P);
331     }
332   }
333 #endif
334 }
335
336
337
338
339
340
341 //=======================================================================
342 //function : Projector
343 //purpose  : 
344 //=======================================================================
345
346 Handle(Prs3d_Projector) PrsMgr_Presentation3d::Projector(const Handle(Graphic3d_DataStructureManager)& aProjector) {
347   Visual3d_ViewOrientation VO = Handle(Visual3d_View)::DownCast(aProjector)->ViewOrientation();
348   Standard_Real DX, DY, DZ,XAt, YAt , ZAt,XUp, YUp, ZUp;
349   VO.ViewReferencePlane().Coord(DX, DY, DZ);
350   VO.ViewReferencePoint().Coord(XAt,YAt,ZAt);
351   VO.ViewReferenceUp().Coord(XUp, YUp, ZUp);
352   Visual3d_ViewMapping VM =  Handle(Visual3d_View)::DownCast(aProjector)->ViewMapping();
353   Standard_Boolean pers = (VM.Projection() == Visual3d_TOP_PERSPECTIVE);
354     Standard_Real focale = 0.0 ;
355   if (pers) {
356     Standard_Real Xrp,Yrp,Zrp,ViewPlane,FrontPlane ;
357     Graphic3d_Vertex Prp = VM.ProjectionReferencePoint() ;
358     Prp.Coord(Xrp,Yrp,Zrp);
359     FrontPlane = VM.FrontPlaneDistance() ;
360     ViewPlane = VM.ViewPlaneDistance() ;
361     focale = FrontPlane + Zrp - ViewPlane ;
362   }
363   Handle(Prs3d_Projector) Proj = new Prs3d_Projector(pers,focale,DX, DY, DZ,XAt, YAt , ZAt,XUp, YUp, ZUp);
364   return Proj;
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 }