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