0023024: Update headers of OCCT files
[occt.git] / src / V3d / V3d_SpotLight.cxx
1 // Copyright (c) 1999-2012 OPEN CASCADE SAS
2 //
3 // The content of this file is subject to the Open CASCADE Technology Public
4 // License Version 6.5 (the "License"). You may not use the content of this file
5 // except in compliance with the License. Please obtain a copy of the License
6 // at http://www.opencascade.org and read it completely before using this file.
7 //
8 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
10 //
11 // The Original Code and all software distributed under the License is
12 // distributed on an "AS IS" basis, without warranty of any kind, and the
13 // Initial Developer hereby disclaims all such warranties, including without
14 // limitation, any warranties of merchantability, fitness for a particular
15 // purpose or non-infringement. Please see the License for the specific terms
16 // and conditions governing the rights and limitations under the License.
17
18 /***********************************************************************
19  
20      FONCTION :
21      ----------
22         Classe V3d_SpotLight :
23  
24      HISTORIQUE DES MODIFICATIONS   :
25      --------------------------------
26       00-09-92 : GG  ; Creation.
27       18-06-96 : FMN ; Ajout MyGraphicStructure1 pour sauvegarder snopick
28       30-03-98 : ZOV ; PRO6774 (reconstruction of the class hierarchy and suppressing useless methods)
29       02.15.100 : JR : Clutter
30
31 ************************************************************************/
32
33 /*----------------------------------------------------------------------*/
34 /*
35  * Includes
36  */
37
38 #include <V3d.hxx>
39 #include <V3d_SpotLight.ixx>
40 #include <Graphic3d_Vector.hxx>
41 #include <Graphic3d_Vertex.hxx>
42 #include <Graphic3d_Structure.hxx>
43 #include <Graphic3d_Group.hxx>
44 #include <Graphic3d_Array1OfVertex.hxx>
45 #include <Graphic3d_AspectMarker3d.hxx>
46 #include <Graphic3d_AspectLine3d.hxx>
47 #include <Graphic3d_AspectText3d.hxx>
48 #include <Visual3d_Light.hxx>
49 #include <Visual3d_ViewManager.hxx>
50 #include <Visual3d_ContextPick.hxx>
51 #include <Visual3d_PickDescriptor.hxx>
52 #include <Visual3d_HSequenceOfPickPath.hxx>
53 #include <Visual3d_PickPath.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 <TCollection_AsciiString.hxx>
60
61 #ifdef WNT
62 # include <WNT_Window.hxx>
63 #else
64 # include <Xw_Window.hxx>
65 #endif
66
67
68 V3d_SpotLight::V3d_SpotLight(const Handle(V3d_Viewer)& VM, const Standard_Real X, const Standard_Real Y, const Standard_Real Z, const V3d_TypeOfOrientation Direction, const Quantity_NameOfColor Name, const Standard_Real A1, const Standard_Real A2, const Standard_Real CN, const Standard_Real AN):V3d_PositionLight(VM) {
69
70   Viewer_BadValue_Raise_if( A1 < 0 || A1 > 1. || A2 < 0 || A2 > 1  
71                         || AN < 0. || AN > M_PI, "V3d_SpotLight, bad coefficient or angle");
72
73   Quantity_Color C(Name) ;
74   Graphic3d_Vector D = V3d::GetProjAxis(Direction) ;
75   Graphic3d_Vertex P(X,Y,Z) ;
76   Graphic3d_Vertex T;
77
78   MyType = V3d_SPOT ;
79   MyLight = new Visual3d_Light(C,P,D,CN,A1,A2,AN) ;
80   // The target is fixed, starting from the light position and the direction.
81   T.SetCoord(X + D.X(), Y + D.Y(), Z + D.Z());
82   MyTarget = T;
83
84
85 V3d_SpotLight::V3d_SpotLight(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, const Standard_Real CN, const Standard_Real AN):V3d_PositionLight(VM) {
86
87   Viewer_BadValue_Raise_if( A1 < 0 || A1 > 1. || A2 < 0 || A2 > 1  
88                         || AN < 0. || AN > M_PI, "V3d_SpotLight, bad coefficient or angle");
89
90   Quantity_Color C(Name) ;
91   Graphic3d_Vertex T(Xt,Yt,Zt) ;
92   Graphic3d_Vertex P(Xp,Yp,Zp) ;
93   Graphic3d_Vector D(P,T);
94
95   MyType = V3d_SPOT ;
96   D.Normalize();
97   MyLight = new Visual3d_Light(C,P,D,CN,A1,A2,AN) ;
98   MyTarget = T;
99   // La Structure graphique sera initialisee lors de l'affichage.
100 }
101
102
103 //-Methods, in order
104
105 void V3d_SpotLight::SetPosition(const Standard_Real Xp, const Standard_Real Yp, const Standard_Real Zp) {
106   MyLight->SetPosition (Graphic3d_Vertex (Xp,Yp,Zp));
107 }
108
109 void V3d_SpotLight::SetDirection(const Standard_Real Vx, const Standard_Real Vy, const Standard_Real Vz) {
110
111   Graphic3d_Vector D ;
112   D.SetCoord(Vx,Vy,Vz) ; D.Normalize() ;
113   MyLight->SetDirection(D) ;
114 }
115
116 void V3d_SpotLight::SetDirection(const V3d_TypeOfOrientation Direction) {
117
118   Graphic3d_Vector D = V3d::GetProjAxis(Direction) ;
119   MyLight->SetDirection(D) ;
120 }
121
122 void V3d_SpotLight::SetAttenuation(const Standard_Real A1, const Standard_Real A2) {
123
124   Viewer_BadValue_Raise_if( A1 < 0 || A1 > 1. || A2 < 0 || A2 > 1 ,
125                         "V3d_SpotLight::SetAttenuation, bad coefficients");
126
127   MyLight->SetAttenuation1(A1) ;
128   MyLight->SetAttenuation2(A2) ;
129 }
130
131 void V3d_SpotLight::SetConcentration(const Standard_Real C) {
132
133   
134   Viewer_BadValue_Raise_if( C < 0 || C > 1.,
135                         "V3d_SpotLight::SetConcentration, bad coefficient");
136
137   MyLight->SetConcentration(C) ;
138 }
139
140 void V3d_SpotLight::SetAngle(const Standard_Real Angle) {
141
142   Viewer_BadValue_Raise_if( Angle <= 0. || Angle >= M_PI,
143                         "V3d_SpotLight::SetAngle, bad angle");
144   MyLight->SetAngle(Angle) ;
145
146 }
147
148 void V3d_SpotLight::Direction(Standard_Real& Vx, Standard_Real& Vy, Standard_Real& Vz)const  {
149
150   Quantity_Color C ;
151   Graphic3d_Vector D ;
152   Graphic3d_Vertex P ;
153   Standard_Real CN,A1,A2,AN ;
154
155   MyLight->Values(C,P,D,CN,A1,A2,AN) ;
156   D.Coord(Vx,Vy,Vz) ;
157 }
158
159 void V3d_SpotLight::Position(Standard_Real& Xp, Standard_Real& Yp, Standard_Real& Zp)const  {
160   
161   Quantity_Color C ;
162   Graphic3d_Vector D ;
163   Graphic3d_Vertex P ;
164   Standard_Real CN,A1,A2,AN ;
165
166   MyLight->Values(C,P,D,CN,A1,A2,AN) ;
167   P.Coord(Xp,Yp,Zp) ;
168 }
169
170 void V3d_SpotLight::Attenuation(Standard_Real& A1, Standard_Real& A2) const  {
171     Quantity_Color C ;
172     Graphic3d_Vector D ;
173     Graphic3d_Vertex P ;
174     Standard_Real CN,AN ;
175     
176     MyLight->Values(C,P,D,CN,A1,A2,AN) ;
177 }
178
179 Standard_Real V3d_SpotLight::Concentration()const  {
180
181   Quantity_Color C ;
182   Graphic3d_Vector D ;
183   Graphic3d_Vertex P ;
184   Standard_Real AN,A1,A2,CN ;
185   
186   MyLight->Values(C,P,D,CN,A1,A2,AN) ;
187   return CN ;
188 }
189
190 Standard_Real V3d_SpotLight::Angle()const  {
191
192   Quantity_Color C ;
193   Graphic3d_Vector D ;
194   Graphic3d_Vertex P ;
195   Standard_Real CN,A1,A2,AN ;
196   
197   MyLight->Values(C,P,D,CN,A1,A2,AN) ;
198   return AN ;
199 }
200
201 void V3d_SpotLight::Symbol (const Handle(Graphic3d_Group)& gsymbol,
202 //                            const Handle(V3d_View)& aView) const {
203                             const Handle(V3d_View)& ) const {
204
205   Standard_Real X,Y,Z,Rayon;
206   Standard_Real DX,DY,DZ;
207
208   this->Position(X,Y,Z);
209   this->Direction(DX,DY,DZ);
210   Rayon = this->Radius();
211   V3d::ArrowOfRadius(gsymbol,X,Y,Z,-DX,-DY,-DZ,M_PI/8.,Rayon/15.);
212 }
213     
214 void V3d_SpotLight::Display( const Handle(V3d_View)& aView,
215                                     const V3d_TypeOfRepresentation TPres) {
216
217   Graphic3d_Array1OfVertex PRadius(0,1);
218   Graphic3d_Vertex PText ;
219   Standard_Real X,Y,Z,Rayon;
220   Standard_Real X0,Y0,Z0,VX,VY,VZ;
221   Standard_Real X1,Y1,Z1;
222   Standard_Real DXRef,DYRef,DZRef,DXini,DYini,DZini;
223   Standard_Real R1,G1,B1;
224   V3d_TypeOfRepresentation Pres;
225   V3d_TypeOfUpdate UpdSov;
226
227 //  Creation of a structure slight of markable elements (position of the
228 //  light, and the domain of lighting represented by a circle)
229 //  Creation of a structure snopick of non-markable elements (target, meridian and 
230 //  parallel).// 
231
232     Pres = TPres;
233     Handle(V3d_Viewer) TheViewer = aView->Viewer();
234     UpdSov = TheViewer->UpdateMode();
235     TheViewer->SetUpdateMode(V3d_WAIT);
236     if (!MyGraphicStructure.IsNull()) {
237        MyGraphicStructure->Disconnect(MyGraphicStructure1);
238        MyGraphicStructure->Clear();
239        MyGraphicStructure1->Clear();
240        if (Pres == V3d_SAMELAST) Pres = MyTypeOfRepresentation;
241     }
242     else {
243       if (Pres == V3d_SAMELAST) Pres = V3d_SIMPLE;
244       Handle(Graphic3d_Structure) slight = new Graphic3d_Structure(TheViewer->Viewer());
245       MyGraphicStructure = slight;
246       Handle(Graphic3d_Structure) snopick = new Graphic3d_Structure(TheViewer->Viewer()); 
247       MyGraphicStructure1 = snopick;
248     }
249   
250   Handle(Graphic3d_Group) gradius;
251   Handle(Graphic3d_Group) gExtArrow;
252   Handle(Graphic3d_Group) gIntArrow;
253   if (Pres == V3d_COMPLETE) {
254     gradius = new Graphic3d_Group(MyGraphicStructure);
255     gExtArrow = new Graphic3d_Group(MyGraphicStructure);
256     gIntArrow = new Graphic3d_Group(MyGraphicStructure);
257   }
258   Handle(Graphic3d_Group) glight  = new Graphic3d_Group(MyGraphicStructure);
259   Handle(Graphic3d_Group) gsphere;
260   if (Pres == V3d_COMPLETE || Pres == V3d_PARTIAL) gsphere = new Graphic3d_Group(MyGraphicStructure);
261   
262   Handle(Graphic3d_Group) gnopick = new Graphic3d_Group(MyGraphicStructure1);
263   MyGraphicStructure1->SetPick(Standard_False);
264   
265   X0 = MyTarget.X();
266   Y0 = MyTarget.Y();
267   Z0 = MyTarget.Z();
268   
269 //Display of the position of the light.
270
271   this->Color(Quantity_TOC_RGB,R1,G1,B1);
272   Quantity_Color Col1(R1,G1,B1,Quantity_TOC_RGB);
273   Handle(Graphic3d_AspectLine3d) Asp1 = new Graphic3d_AspectLine3d();
274   Asp1->SetColor(Col1);
275   glight->SetPrimitivesAspect(Asp1);
276   this->Symbol(glight,aView);
277   
278 // Display of the reference sphere (limited by circle).
279
280   if (Pres == V3d_COMPLETE || Pres == V3d_PARTIAL) {
281     
282     Rayon = this->Radius(); 
283     aView->Proj(VX,VY,VZ);
284     V3d::CircleInPlane(gsphere,X0,Y0,Z0,VX,VY,VZ,Rayon);
285
286 // Display of the radius of the sphere (line + text)
287
288     if (Pres == V3d_COMPLETE) {
289       PRadius(0).SetCoord(X0,Y0,Z0);
290       this->Position(X,Y,Z);
291       PRadius(1).SetCoord(X,Y,Z);
292       gnopick->Polyline(PRadius);
293       V3d::ArrowOfRadius(gExtArrow,X-(X-X0)/10.,
294                          Y-(Y-Y0)/10.,
295                          Z-(Z-Z0) / 10., X-X0, Y-Y0, Z-Z0, M_PI / 15., Rayon / 20.);
296       V3d::ArrowOfRadius(gIntArrow, X0, Y0, Z0, X0-X, Y0-Y, Z0-Z, M_PI / 15., Rayon / 20.);
297       TCollection_AsciiString ValOfRadius(Rayon);
298       PText.SetCoord( (X0+X)/2., (Y0+Y)/2. , (Z0+Z)/2. );
299       gradius->Text(ValOfRadius.ToCString(),PText,0.01);
300     }
301     
302 // Display of the meridian
303
304     Quantity_Color Col2(Quantity_NOC_GREEN);
305     Handle(Graphic3d_AspectLine3d) Asp2 = new Graphic3d_AspectLine3d
306       (Col2,Aspect_TOL_SOLID,1.);
307     gnopick->SetPrimitivesAspect(Asp2);
308     
309 //    Definition of the axis of the circle
310     aView->Up(DXRef,DYRef,DZRef);
311     this->Position(X,Y,Z);
312     DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
313     VX = DYRef*DZini - DZRef*DYini;
314     VY = DZRef*DXini - DXRef*DZini;
315     VZ = DXRef*DYini - DYRef*DXini;
316     
317     V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
318
319 //    Display of the parallel
320
321 //    Definition of the axis of the circle
322     aView->Proj(VX,VY,VZ);
323     aView->Up(X1,Y1,Z1);
324     DXRef = VY * Z1 - VZ * Y1;
325     DYRef = VZ * X1 - VX * Z1;
326     DZRef = VX * Y1 - VY * X1;
327     this->Position(X,Y,Z);
328     DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
329     VX = DYRef*DZini - DZRef*DYini;
330     VY = DZRef*DXini - DXRef*DZini;
331     VZ = DXRef*DYini - DYRef*DXini;
332     
333     V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
334     
335   }
336   
337   MyGraphicStructure->Connect(MyGraphicStructure1,Graphic3d_TOC_DESCENDANT);
338   MyTypeOfRepresentation = Pres;
339   MyGraphicStructure->Display();
340   TheViewer->SetUpdateMode(UpdSov);
341 }