0024752: Visualization - inherit OpenGl_Group from Graphic3d_Group
[occt.git] / src / V3d / V3d_DirectionalLight.cxx
1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 /***********************************************************************
15  
16      FONCTION :
17      ----------
18         Classe V3d_DirectionalLight :
19  
20      HISTORIQUE DES MODIFICATIONS   :
21      --------------------------------
22       00-09-92 : GG  ; Creation.
23       18-06-96 : FMN ; Ajout MyGraphicStructure1 pour sauvegarder snopick
24       24-12-97 : FMN ; Remplacement de math par MathGra
25       31-12-97 : CAL ; Suppression de MathGra
26       21-01-98 : CAL ; Window de Xw et WNT remplacee par Aspect_Window
27       23-02-98 : FMN ; Remplacement PI par Standard_PI
28       30-03-98 : ZOV ; PRO6774 (reconstruction of the class hierarchy and suppressing useless methods)
29
30 ************************************************************************/
31
32 /*----------------------------------------------------------------------*/
33 /*
34  * Includes
35  */
36
37 #include <V3d.hxx>
38 #include <V3d_DirectionalLight.ixx>
39 #include <Graphic3d_Vector.hxx>
40 #include <Graphic3d_Vertex.hxx>
41 #include <Graphic3d_Structure.hxx>
42 #include <Graphic3d_Group.hxx>
43 #include <Graphic3d_ArrayOfSegments.hxx>
44 #include <Graphic3d_AspectMarker3d.hxx>
45 #include <Graphic3d_AspectLine3d.hxx>
46 #include <Graphic3d_AspectText3d.hxx>
47 #include <Visual3d_Light.hxx>
48 #include <Visual3d_ViewManager.hxx>
49 #include <Visual3d_ContextPick.hxx>
50 #include <Visual3d_PickDescriptor.hxx>
51 #include <Visual3d_HSequenceOfPickPath.hxx>
52 #include <Visual3d_PickPath.hxx>
53 #include <V3d_BadValue.hxx>
54 #include <gp_Dir.hxx>
55 #include <gp_Ax1.hxx>
56 #include <gp_Vec.hxx>
57 #include <gp_Pnt.hxx>
58 #include <gp_Trsf.hxx>
59 #include <TColStd_Array2OfReal.hxx>
60 #include <Aspect_Window.hxx>
61
62 //-Constructors
63
64 V3d_DirectionalLight::V3d_DirectionalLight(const Handle(V3d_Viewer)& VM, const
65 V3d_TypeOfOrientation Direction,const Quantity_NameOfColor Name,const Standard_Boolean Headlight):V3d_PositionLight(VM) { 
66   Quantity_Color C(Name) ;
67   Graphic3d_Vertex T(0.,0.,0.) ;
68   Graphic3d_Vertex P ;
69   Graphic3d_Vector V = V3d::GetProjAxis(Direction) ;
70   
71   MyType = V3d_DIRECTIONAL ;
72   MyLight = new Visual3d_Light(C,V, Headlight) ;
73   // The initial target is chosen at random
74   MyTarget = T;
75 // Position is found
76   P.SetCoord(-V.X(),-V.Y(),-V.Z());
77   MyDisplayPosition = P; 
78
79 }
80
81 V3d_DirectionalLight::V3d_DirectionalLight(const Handle(V3d_Viewer)& VM,const Standard_Real Xt,const Standard_Real Yt,const Standard_Real Zt,const Standard_Real Xp,const Standard_Real Yp,const Standard_Real Zp,const Quantity_NameOfColor Name,const Standard_Boolean Headlight):V3d_PositionLight(VM) { 
82   Quantity_Color C(Name) ;
83   Graphic3d_Vertex T(Xt,Yt,Zt) ;
84   Graphic3d_Vertex P(Xp,Yp,Zp) ;
85   Graphic3d_Vector V(P,T);
86   
87   MyType = V3d_DIRECTIONAL ;
88   V.Normalize();
89   MyLight = new Visual3d_Light(C,V, Headlight) ;
90   MyTarget = T;
91   MyDisplayPosition = P;
92 }
93
94 //-Methods, in order
95
96 void V3d_DirectionalLight::SetDirection(const V3d_TypeOfOrientation Direction) {
97
98   Graphic3d_Vector V = V3d::GetProjAxis(Direction) ;
99   MyLight->SetDirection(V) ;
100 }
101
102 void V3d_DirectionalLight::SetDirection(const Standard_Real Vx, const Standard_Real Vy, const Standard_Real Vz) {
103
104   V3d_BadValue_Raise_if( sqrt( Vx*Vx + Vy*Vy + Vz*Vz ) <= 0.,"V3d_DirectionalLight::SetDirection, null vector" );
105
106   Graphic3d_Vector V(Vx,Vy,Vz) ;
107   V.Normalize() ;
108   MyLight->SetDirection(V) ;
109 }
110
111 void V3d_DirectionalLight::SetDisplayPosition(const Standard_Real X, const Standard_Real Y, const Standard_Real Z) {
112
113   Standard_Real Xt,Yt,Zt;
114
115   MyDisplayPosition.SetCoord(X,Y,Z) ;
116   MyTarget.Coord(Xt,Yt,Zt);
117   SetDirection(Xt-X,Yt-Y,Zt-Z);
118 }
119
120 void V3d_DirectionalLight::SetPosition(const Standard_Real Xp, const Standard_Real Yp, const Standard_Real Zp) {
121
122   SetDisplayPosition (Xp,Yp,Zp);
123 }
124
125 void V3d_DirectionalLight::Position(Standard_Real& Xp, Standard_Real& Yp, Standard_Real& Zp)const  {
126
127   DisplayPosition (Xp,Yp,Zp) ;
128 }
129
130 void V3d_DirectionalLight::DisplayPosition(Standard_Real& Xp, Standard_Real& Yp, Standard_Real& Zp)const  {
131
132   MyDisplayPosition.Coord(Xp,Yp,Zp) ;
133 }
134
135 void V3d_DirectionalLight::Symbol (const Handle(Graphic3d_Group)& gsymbol, const Handle(V3d_View)& aView) const
136 {
137   Standard_Real Xi,Yi,Zi,Xf,Yf,Zf,Rayon,PXT,PYT,X,Y,Z,XT,YT,ZT;
138   Standard_Real A,B,C,Dist,Beta,CosBeta,SinBeta,Coef,X1,Y1,Z1;
139   Standard_Real DX,DY,DZ,VX,VY,VZ;
140   Standard_Integer IXP,IYP,j;
141   TColStd_Array2OfReal MatRot(0,2,0,2);
142
143   aView->Proj(VX,VY,VZ);
144   this->DisplayPosition(Xi,Yi,Zi);
145   Rayon = this->Radius();
146   aView->Project(Xi,Yi,Zi,PXT,PYT); 
147   aView->Convert(PXT,PYT,IXP,IYP);
148 //  Coordinated 3d in the plane of projection of the source.
149   aView->Convert(IXP,IYP,XT,YT,ZT);
150   aView->Convert(PXT,PYT+Rayon,IXP,IYP);
151   aView->Convert(IXP,IYP,X,Y,Z);
152   X = X+Xi-XT; Y = Y+Yi-YT; Z = Z+Zi-ZT;
153   Dist = Sqrt( Square(X-Xi) + Square(Y-Yi) + Square(Z-Zi) );
154 //  Axis of rotation.
155   A = (X-Xi)/Dist;
156   B = (Y-Yi)/Dist;
157   C = (Z-Zi)/Dist;
158
159 //  A sphere is drawn
160   V3d::CircleInPlane(gsymbol,Xi,Yi,Zi,VX,VY,VZ,Rayon/40.);
161   for( j=1 ; j<=3 ; j++ ) {
162     Beta = j * M_PI / 4.;
163     CosBeta = Cos(Beta);
164     SinBeta = Sin(Beta);
165     Coef = 1. - CosBeta;
166     MatRot(0,0) =  A * A + (1. - A * A) * CosBeta;
167     MatRot(0,1) = -C * SinBeta + Coef * A * B;
168     MatRot(0,2) =  B * SinBeta + Coef * A * C;
169     MatRot(1,0) =  C * SinBeta + Coef * A * B; 
170     MatRot(1,1) =  B * B + (1. - B * B) * CosBeta;
171     MatRot(1,2) = -A * SinBeta + Coef * B * C;
172     MatRot(2,0) = -B * SinBeta + Coef * A * C;
173     MatRot(2,1) =  A * SinBeta + Coef * B * C;
174     MatRot(2,2) =  C * C + (1. - C * C) * CosBeta;
175     Xf = Xi * MatRot(0,0) + Yi * MatRot(0,1) + Zi * MatRot(0,2);
176     Yf = Xi * MatRot(1,0) + Yi * MatRot(1,1) + Zi * MatRot(1,2);
177     Zf = Xi * MatRot(2,0) + Yi * MatRot(2,1) + Zi * MatRot(2,2);
178 //    Rotation of the normal
179     X1 = VX * MatRot(0,0) + VY * MatRot(0,1) + VZ * MatRot(0,2);
180     Y1 = VX * MatRot(1,0) + VY * MatRot(1,1) + VZ * MatRot(1,2);
181     Z1 = VX * MatRot(2,0) + VY * MatRot(2,1) + VZ * MatRot(2,2);
182     VX = X1 + Xi - Xf ; VY = Y1 + Yi - Yf ; VZ = Z1 + Zi - Zf;
183     V3d::CircleInPlane(gsymbol,Xi,Yi,Zi,VX,VY,VZ,Rayon/40.);
184   }
185
186 //  The arrow is drawn
187   Rayon = this->Radius();
188   this->Direction(DX,DY,DZ);
189   X = Xi + DX*Rayon/10.; Y = Yi + DY*Rayon/10.; Z = Zi + DZ*Rayon/10.;
190
191   Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(2);
192   aPrims->AddVertex(Standard_ShortReal(Xi),Standard_ShortReal(Yi),Standard_ShortReal(Zi));
193   aPrims->AddVertex(Standard_ShortReal(X),Standard_ShortReal(Y),Standard_ShortReal(Z));
194   gsymbol->AddPrimitiveArray(aPrims);
195
196   V3d::ArrowOfRadius(gsymbol, X, Y, Z, DX, DY, DZ, M_PI / 15., Rayon / 20.);
197 }
198
199 void V3d_DirectionalLight::Display( const Handle(V3d_View)& aView,
200                                     const V3d_TypeOfRepresentation TPres) {
201
202   Standard_Real X,Y,Z,Rayon;
203   Standard_Real X0,Y0,Z0,VX,VY,VZ;
204   Standard_Real X1,Y1,Z1;
205   Standard_Real DXRef,DYRef,DZRef,DXini,DYini,DZini;
206   Standard_Real R1,G1,B1;
207   V3d_TypeOfRepresentation Pres;
208   V3d_TypeOfUpdate UpdSov;
209
210 //  Creation of a structure of markable elements (position of the
211 //  light, and the domain of lighting represented by a circle)
212 //  Creation of a structure of non-markable elements (target, meridian and 
213 //  parallel).
214
215     Pres = TPres;
216     Handle(V3d_Viewer) TheViewer = aView->Viewer();
217     UpdSov = TheViewer->UpdateMode();
218     TheViewer->SetUpdateMode(V3d_WAIT);
219     if (!MyGraphicStructure.IsNull()) {
220        MyGraphicStructure->Disconnect(MyGraphicStructure1);
221        MyGraphicStructure->Clear();
222        MyGraphicStructure1->Clear();
223        if (Pres == V3d_SAMELAST) Pres = MyTypeOfRepresentation;
224     }
225     else {
226       if (Pres == V3d_SAMELAST) Pres = V3d_SIMPLE;
227       Handle(Graphic3d_Structure) slight = new Graphic3d_Structure(TheViewer->Viewer());
228       MyGraphicStructure = slight;
229       Handle(Graphic3d_Structure) snopick = new Graphic3d_Structure(TheViewer->Viewer()); 
230       MyGraphicStructure1 = snopick;
231     }
232   
233   Handle(Graphic3d_Group) glight  = MyGraphicStructure->NewGroup();
234   Handle(Graphic3d_Group) gsphere;
235   if (Pres == V3d_COMPLETE
236    || Pres == V3d_PARTIAL)
237   {
238     gsphere = MyGraphicStructure->NewGroup();
239   }
240   
241   Handle(Graphic3d_Group) gnopick = MyGraphicStructure1->NewGroup();
242   MyGraphicStructure1->SetPick(Standard_False);
243   
244   X0 = MyTarget.X();
245   Y0 = MyTarget.Y();
246   Z0 = MyTarget.Z();
247
248 //Display of the position of the light.
249
250   this->Color(Quantity_TOC_RGB,R1,G1,B1);
251   Quantity_Color Col1(R1,G1,B1,Quantity_TOC_RGB);
252   Handle(Graphic3d_AspectLine3d) Asp1 = new Graphic3d_AspectLine3d();
253   Asp1->SetColor(Col1);
254   glight->SetPrimitivesAspect(Asp1);
255   this->Symbol(glight,aView);
256   
257   // Display of the markable sphere (limit at the circle).
258
259   if (Pres == V3d_COMPLETE || Pres == V3d_PARTIAL) {
260     
261     Rayon = this->Radius(); 
262     aView->Proj(VX,VY,VZ);
263     V3d::CircleInPlane(gsphere,X0,Y0,Z0,VX,VY,VZ,Rayon);
264     
265 //Display of the meridian
266
267     Quantity_Color Col2(Quantity_NOC_GREEN);
268     Handle(Graphic3d_AspectLine3d) Asp2 = new Graphic3d_AspectLine3d
269       (Col2,Aspect_TOL_SOLID,1.);
270     gnopick->SetPrimitivesAspect(Asp2);
271     
272 //    Definition of the axis of circle
273     aView->Up(DXRef,DYRef,DZRef);
274     this->DisplayPosition(X,Y,Z);
275     DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
276     VX = DYRef*DZini - DZRef*DYini;
277     VY = DZRef*DXini - DXRef*DZini;
278     VZ = DXRef*DYini - DYRef*DXini;
279     
280     V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
281       
282 //  Display of the parallel
283
284 //    Definition of the axis of circle
285     aView->Proj(VX,VY,VZ);
286     aView->Up(X1,Y1,Z1);
287     DXRef = VY * Z1 - VZ * Y1;
288     DYRef = VZ * X1 - VX * Z1;
289     DZRef = VX * Y1 - VY * X1;
290     this->DisplayPosition(X,Y,Z);
291     DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
292     VX = DYRef*DZini - DZRef*DYini;
293     VY = DZRef*DXini - DXRef*DZini;
294     VZ = DXRef*DYini - DYRef*DXini;
295     
296     V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
297     
298   }
299   
300   MyGraphicStructure->Connect(MyGraphicStructure1,Graphic3d_TOC_DESCENDANT);
301 //    cout << "MyGraphicStructure exploration \n" << flush; MyGraphicStructure->Exploration();
302   MyTypeOfRepresentation = Pres;
303   MyGraphicStructure->Display();
304   TheViewer->SetUpdateMode(UpdSov);
305 }
306
307 void V3d_DirectionalLight::Direction(Standard_Real& Vx, Standard_Real& Vy, Standard_Real& Vz)const  {
308   
309   Quantity_Color C ;
310   Graphic3d_Vector V ;
311
312   MyLight->Values(C,V) ;
313   V.Coord(Vx,Vy,Vz) ;
314 }