91b76763bb1f2ee42247124b7acaf878e60f29b7
[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 <V3d_BadValue.hxx>
51 #include <gp_Dir.hxx>
52 #include <gp_Ax1.hxx>
53 #include <gp_Vec.hxx>
54 #include <gp_Pnt.hxx>
55 #include <gp_Trsf.hxx>
56 #include <TColStd_Array2OfReal.hxx>
57 #include <Aspect_Window.hxx>
58
59 //-Constructors
60
61 V3d_DirectionalLight::V3d_DirectionalLight(const Handle(V3d_Viewer)& VM, const
62 V3d_TypeOfOrientation Direction,const Quantity_NameOfColor Name,const Standard_Boolean Headlight):V3d_PositionLight(VM) { 
63   Quantity_Color C(Name) ;
64   Graphic3d_Vertex T(0.,0.,0.) ;
65   Graphic3d_Vertex P ;
66   Graphic3d_Vector V = V3d::GetProjAxis(Direction) ;
67   
68   MyType = V3d_DIRECTIONAL ;
69   MyLight = new Visual3d_Light(C,V, Headlight) ;
70   // The initial target is chosen at random
71   MyTarget = T;
72 // Position is found
73   P.SetCoord(-V.X(),-V.Y(),-V.Z());
74   MyDisplayPosition = P; 
75
76 }
77
78 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) { 
79   Quantity_Color C(Name) ;
80   Graphic3d_Vertex T(Xt,Yt,Zt) ;
81   Graphic3d_Vertex P(Xp,Yp,Zp) ;
82   Graphic3d_Vector V(P,T);
83   
84   MyType = V3d_DIRECTIONAL ;
85   V.Normalize();
86   MyLight = new Visual3d_Light(C,V, Headlight) ;
87   MyTarget = T;
88   MyDisplayPosition = P;
89 }
90
91 //-Methods, in order
92
93 void V3d_DirectionalLight::SetDirection(const V3d_TypeOfOrientation Direction) {
94
95   Graphic3d_Vector V = V3d::GetProjAxis(Direction) ;
96   MyLight->SetDirection(V) ;
97 }
98
99 void V3d_DirectionalLight::SetDirection(const Standard_Real Vx, const Standard_Real Vy, const Standard_Real Vz) {
100
101   V3d_BadValue_Raise_if( sqrt( Vx*Vx + Vy*Vy + Vz*Vz ) <= 0.,"V3d_DirectionalLight::SetDirection, null vector" );
102
103   Graphic3d_Vector V(Vx,Vy,Vz) ;
104   V.Normalize() ;
105   MyLight->SetDirection(V) ;
106 }
107
108 void V3d_DirectionalLight::SetDisplayPosition(const Standard_Real X, const Standard_Real Y, const Standard_Real Z) {
109
110   Standard_Real Xt,Yt,Zt;
111
112   MyDisplayPosition.SetCoord(X,Y,Z) ;
113   MyTarget.Coord(Xt,Yt,Zt);
114   SetDirection(Xt-X,Yt-Y,Zt-Z);
115 }
116
117 void V3d_DirectionalLight::SetPosition(const Standard_Real Xp, const Standard_Real Yp, const Standard_Real Zp) {
118
119   SetDisplayPosition (Xp,Yp,Zp);
120 }
121
122 void V3d_DirectionalLight::Position(Standard_Real& Xp, Standard_Real& Yp, Standard_Real& Zp)const  {
123
124   DisplayPosition (Xp,Yp,Zp) ;
125 }
126
127 void V3d_DirectionalLight::DisplayPosition(Standard_Real& Xp, Standard_Real& Yp, Standard_Real& Zp)const  {
128
129   MyDisplayPosition.Coord(Xp,Yp,Zp) ;
130 }
131
132 void V3d_DirectionalLight::Symbol (const Handle(Graphic3d_Group)& gsymbol, const Handle(V3d_View)& aView) const
133 {
134   Standard_Real Xi,Yi,Zi,Xf,Yf,Zf,Rayon,PXT,PYT,X,Y,Z,XT,YT,ZT;
135   Standard_Real A,B,C,Dist,Beta,CosBeta,SinBeta,Coef,X1,Y1,Z1;
136   Standard_Real DX,DY,DZ,VX,VY,VZ;
137   Standard_Integer IXP,IYP,j;
138   TColStd_Array2OfReal MatRot(0,2,0,2);
139
140   aView->Proj(VX,VY,VZ);
141   this->DisplayPosition(Xi,Yi,Zi);
142   Rayon = this->Radius();
143   aView->Project(Xi,Yi,Zi,PXT,PYT); 
144   aView->Convert(PXT,PYT,IXP,IYP);
145 //  Coordinated 3d in the plane of projection of the source.
146   aView->Convert(IXP,IYP,XT,YT,ZT);
147   aView->Convert(PXT,PYT+Rayon,IXP,IYP);
148   aView->Convert(IXP,IYP,X,Y,Z);
149   X = X+Xi-XT; Y = Y+Yi-YT; Z = Z+Zi-ZT;
150   Dist = Sqrt( Square(X-Xi) + Square(Y-Yi) + Square(Z-Zi) );
151 //  Axis of rotation.
152   A = (X-Xi)/Dist;
153   B = (Y-Yi)/Dist;
154   C = (Z-Zi)/Dist;
155
156 //  A sphere is drawn
157   V3d::CircleInPlane(gsymbol,Xi,Yi,Zi,VX,VY,VZ,Rayon/40.);
158   for( j=1 ; j<=3 ; j++ ) {
159     Beta = j * M_PI / 4.;
160     CosBeta = Cos(Beta);
161     SinBeta = Sin(Beta);
162     Coef = 1. - CosBeta;
163     MatRot(0,0) =  A * A + (1. - A * A) * CosBeta;
164     MatRot(0,1) = -C * SinBeta + Coef * A * B;
165     MatRot(0,2) =  B * SinBeta + Coef * A * C;
166     MatRot(1,0) =  C * SinBeta + Coef * A * B; 
167     MatRot(1,1) =  B * B + (1. - B * B) * CosBeta;
168     MatRot(1,2) = -A * SinBeta + Coef * B * C;
169     MatRot(2,0) = -B * SinBeta + Coef * A * C;
170     MatRot(2,1) =  A * SinBeta + Coef * B * C;
171     MatRot(2,2) =  C * C + (1. - C * C) * CosBeta;
172     Xf = Xi * MatRot(0,0) + Yi * MatRot(0,1) + Zi * MatRot(0,2);
173     Yf = Xi * MatRot(1,0) + Yi * MatRot(1,1) + Zi * MatRot(1,2);
174     Zf = Xi * MatRot(2,0) + Yi * MatRot(2,1) + Zi * MatRot(2,2);
175 //    Rotation of the normal
176     X1 = VX * MatRot(0,0) + VY * MatRot(0,1) + VZ * MatRot(0,2);
177     Y1 = VX * MatRot(1,0) + VY * MatRot(1,1) + VZ * MatRot(1,2);
178     Z1 = VX * MatRot(2,0) + VY * MatRot(2,1) + VZ * MatRot(2,2);
179     VX = X1 + Xi - Xf ; VY = Y1 + Yi - Yf ; VZ = Z1 + Zi - Zf;
180     V3d::CircleInPlane(gsymbol,Xi,Yi,Zi,VX,VY,VZ,Rayon/40.);
181   }
182
183 //  The arrow is drawn
184   Rayon = this->Radius();
185   this->Direction(DX,DY,DZ);
186   X = Xi + DX*Rayon/10.; Y = Yi + DY*Rayon/10.; Z = Zi + DZ*Rayon/10.;
187
188   Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(2);
189   aPrims->AddVertex(Standard_ShortReal(Xi),Standard_ShortReal(Yi),Standard_ShortReal(Zi));
190   aPrims->AddVertex(Standard_ShortReal(X),Standard_ShortReal(Y),Standard_ShortReal(Z));
191   gsymbol->AddPrimitiveArray(aPrims);
192
193   V3d::ArrowOfRadius(gsymbol, X, Y, Z, DX, DY, DZ, M_PI / 15., Rayon / 20.);
194 }
195
196 void V3d_DirectionalLight::Display( const Handle(V3d_View)& aView,
197                                     const V3d_TypeOfRepresentation TPres) {
198
199   Standard_Real X,Y,Z,Rayon;
200   Standard_Real X0,Y0,Z0,VX,VY,VZ;
201   Standard_Real X1,Y1,Z1;
202   Standard_Real DXRef,DYRef,DZRef,DXini,DYini,DZini;
203   Standard_Real R1,G1,B1;
204   V3d_TypeOfRepresentation Pres;
205   V3d_TypeOfUpdate UpdSov;
206
207 //  Creation of a structure of markable elements (position of the
208 //  light, and the domain of lighting represented by a circle)
209 //  Creation of a structure of non-markable elements (target, meridian and 
210 //  parallel).
211
212     Pres = TPres;
213     Handle(V3d_Viewer) TheViewer = aView->Viewer();
214     UpdSov = TheViewer->UpdateMode();
215     TheViewer->SetUpdateMode(V3d_WAIT);
216     if (!MyGraphicStructure.IsNull()) {
217        MyGraphicStructure->Disconnect(MyGraphicStructure1);
218        MyGraphicStructure->Clear();
219        MyGraphicStructure1->Clear();
220        if (Pres == V3d_SAMELAST) Pres = MyTypeOfRepresentation;
221     }
222     else {
223       if (Pres == V3d_SAMELAST) Pres = V3d_SIMPLE;
224       Handle(Graphic3d_Structure) slight = new Graphic3d_Structure(TheViewer->Viewer());
225       MyGraphicStructure = slight;
226       Handle(Graphic3d_Structure) snopick = new Graphic3d_Structure(TheViewer->Viewer()); 
227       MyGraphicStructure1 = snopick;
228     }
229   
230   Handle(Graphic3d_Group) glight  = MyGraphicStructure->NewGroup();
231   Handle(Graphic3d_Group) gsphere;
232   if (Pres == V3d_COMPLETE
233    || Pres == V3d_PARTIAL)
234   {
235     gsphere = MyGraphicStructure->NewGroup();
236   }
237   
238   Handle(Graphic3d_Group) gnopick = MyGraphicStructure1->NewGroup();
239   MyGraphicStructure1->SetPick(Standard_False);
240   
241   X0 = MyTarget.X();
242   Y0 = MyTarget.Y();
243   Z0 = MyTarget.Z();
244
245 //Display of the position of the light.
246
247   this->Color(Quantity_TOC_RGB,R1,G1,B1);
248   Quantity_Color Col1(R1,G1,B1,Quantity_TOC_RGB);
249   Handle(Graphic3d_AspectLine3d) Asp1 = new Graphic3d_AspectLine3d();
250   Asp1->SetColor(Col1);
251   glight->SetPrimitivesAspect(Asp1);
252   this->Symbol(glight,aView);
253   
254   // Display of the markable sphere (limit at the circle).
255
256   if (Pres == V3d_COMPLETE || Pres == V3d_PARTIAL) {
257     
258     Rayon = this->Radius(); 
259     aView->Proj(VX,VY,VZ);
260     V3d::CircleInPlane(gsphere,X0,Y0,Z0,VX,VY,VZ,Rayon);
261     
262 //Display of the meridian
263
264     Quantity_Color Col2(Quantity_NOC_GREEN);
265     Handle(Graphic3d_AspectLine3d) Asp2 = new Graphic3d_AspectLine3d
266       (Col2,Aspect_TOL_SOLID,1.);
267     gnopick->SetPrimitivesAspect(Asp2);
268     
269 //    Definition of the axis of circle
270     aView->Up(DXRef,DYRef,DZRef);
271     this->DisplayPosition(X,Y,Z);
272     DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
273     VX = DYRef*DZini - DZRef*DYini;
274     VY = DZRef*DXini - DXRef*DZini;
275     VZ = DXRef*DYini - DYRef*DXini;
276     
277     V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
278       
279 //  Display of the parallel
280
281 //    Definition of the axis of circle
282     aView->Proj(VX,VY,VZ);
283     aView->Up(X1,Y1,Z1);
284     DXRef = VY * Z1 - VZ * Y1;
285     DYRef = VZ * X1 - VX * Z1;
286     DZRef = VX * Y1 - VY * X1;
287     this->DisplayPosition(X,Y,Z);
288     DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
289     VX = DYRef*DZini - DZRef*DYini;
290     VY = DZRef*DXini - DXRef*DZini;
291     VZ = DXRef*DYini - DYRef*DXini;
292     
293     V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
294     
295   }
296   
297   MyGraphicStructure->Connect(MyGraphicStructure1,Graphic3d_TOC_DESCENDANT);
298 //    cout << "MyGraphicStructure exploration \n" << flush; MyGraphicStructure->Exploration();
299   MyTypeOfRepresentation = Pres;
300   MyGraphicStructure->Display();
301   TheViewer->SetUpdateMode(UpdSov);
302 }
303
304 void V3d_DirectionalLight::Direction(Standard_Real& Vx, Standard_Real& Vy, Standard_Real& Vz)const  {
305   
306   Quantity_Color C ;
307   Graphic3d_Vector V ;
308
309   MyLight->Values(C,V) ;
310   V.Coord(Vx,Vy,Vz) ;
311 }