0024752: Visualization - inherit OpenGl_Group from Graphic3d_Group
[occt.git] / src / V3d / V3d_SpotLight.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_SpotLight :
19  
20      HISTORIQUE DES MODIFICATIONS   :
21      --------------------------------
22       00-09-92 : GG  ; Creation.
23       18-06-96 : FMN ; Ajout MyGraphicStructure1 pour sauvegarder snopick
24       30-03-98 : ZOV ; PRO6774 (reconstruction of the class hierarchy and suppressing useless methods)
25       02.15.100 : JR : Clutter
26
27 ************************************************************************/
28
29 /*----------------------------------------------------------------------*/
30 /*
31  * Includes
32  */
33
34 #include <V3d.hxx>
35 #include <V3d_SpotLight.ixx>
36 #include <Graphic3d_Vector.hxx>
37 #include <Graphic3d_Vertex.hxx>
38 #include <Graphic3d_Structure.hxx>
39 #include <Graphic3d_Group.hxx>
40 #include <Graphic3d_ArrayOfSegments.hxx>
41 #include <Graphic3d_AspectMarker3d.hxx>
42 #include <Graphic3d_AspectLine3d.hxx>
43 #include <Graphic3d_AspectText3d.hxx>
44 #include <Visual3d_Light.hxx>
45 #include <Visual3d_ViewManager.hxx>
46 #include <Visual3d_ContextPick.hxx>
47 #include <Visual3d_PickDescriptor.hxx>
48 #include <Visual3d_HSequenceOfPickPath.hxx>
49 #include <Visual3d_PickPath.hxx>
50 #include <gp_Dir.hxx>
51 #include <gp_Ax1.hxx>
52 #include <gp_Vec.hxx>
53 #include <gp_Pnt.hxx>
54 #include <gp_Trsf.hxx>
55 #include <TCollection_AsciiString.hxx>
56
57 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) {
58
59   V3d_BadValue_Raise_if( A1 < 0 || A1 > 1. || A2 < 0 || A2 > 1  
60                         || AN < 0. || AN > M_PI, "V3d_SpotLight, bad coefficient or angle");
61
62   Quantity_Color C(Name) ;
63   Graphic3d_Vector D = V3d::GetProjAxis(Direction) ;
64   Graphic3d_Vertex P(X,Y,Z) ;
65   Graphic3d_Vertex T;
66
67   MyType = V3d_SPOT ;
68   MyLight = new Visual3d_Light(C,P,D,CN,A1,A2,AN) ;
69   // The target is fixed, starting from the light position and the direction.
70   T.SetCoord(X + D.X(), Y + D.Y(), Z + D.Z());
71   MyTarget = T;
72
73
74 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) {
75
76   V3d_BadValue_Raise_if( A1 < 0 || A1 > 1. || A2 < 0 || A2 > 1  
77                         || AN < 0. || AN > M_PI, "V3d_SpotLight, bad coefficient or angle");
78
79   Quantity_Color C(Name) ;
80   Graphic3d_Vertex T(Xt,Yt,Zt) ;
81   Graphic3d_Vertex P(Xp,Yp,Zp) ;
82   Graphic3d_Vector D(P,T);
83
84   MyType = V3d_SPOT ;
85   D.Normalize();
86   MyLight = new Visual3d_Light(C,P,D,CN,A1,A2,AN) ;
87   MyTarget = T;
88   // La Structure graphique sera initialisee lors de l'affichage.
89 }
90
91
92 //-Methods, in order
93
94 void V3d_SpotLight::SetPosition(const Standard_Real Xp, const Standard_Real Yp, const Standard_Real Zp) {
95   MyLight->SetPosition (Graphic3d_Vertex (Xp,Yp,Zp));
96 }
97
98 void V3d_SpotLight::SetDirection(const Standard_Real Vx, const Standard_Real Vy, const Standard_Real Vz) {
99
100   Graphic3d_Vector D ;
101   D.SetCoord(Vx,Vy,Vz) ; D.Normalize() ;
102   MyLight->SetDirection(D) ;
103 }
104
105 void V3d_SpotLight::SetDirection(const V3d_TypeOfOrientation Direction) {
106
107   Graphic3d_Vector D = V3d::GetProjAxis(Direction) ;
108   MyLight->SetDirection(D) ;
109 }
110
111 void V3d_SpotLight::SetAttenuation(const Standard_Real A1, const Standard_Real A2) {
112
113   V3d_BadValue_Raise_if( A1 < 0 || A1 > 1. || A2 < 0 || A2 > 1 ,
114                         "V3d_SpotLight::SetAttenuation, bad coefficients");
115
116   MyLight->SetAttenuation1(A1) ;
117   MyLight->SetAttenuation2(A2) ;
118 }
119
120 void V3d_SpotLight::SetConcentration(const Standard_Real C) {
121
122   
123   V3d_BadValue_Raise_if( C < 0 || C > 1.,
124                         "V3d_SpotLight::SetConcentration, bad coefficient");
125
126   MyLight->SetConcentration(C) ;
127 }
128
129 void V3d_SpotLight::SetAngle(const Standard_Real Angle) {
130
131   V3d_BadValue_Raise_if( Angle <= 0. || Angle >= M_PI,
132                         "V3d_SpotLight::SetAngle, bad angle");
133   MyLight->SetAngle(Angle) ;
134
135 }
136
137 void V3d_SpotLight::Direction(Standard_Real& Vx, Standard_Real& Vy, Standard_Real& Vz)const  {
138
139   Quantity_Color C ;
140   Graphic3d_Vector D ;
141   Graphic3d_Vertex P ;
142   Standard_Real CN,A1,A2,AN ;
143
144   MyLight->Values(C,P,D,CN,A1,A2,AN) ;
145   D.Coord(Vx,Vy,Vz) ;
146 }
147
148 void V3d_SpotLight::Position(Standard_Real& Xp, Standard_Real& Yp, Standard_Real& Zp)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   P.Coord(Xp,Yp,Zp) ;
157 }
158
159 void V3d_SpotLight::Attenuation(Standard_Real& A1, Standard_Real& A2) const  {
160     Quantity_Color C ;
161     Graphic3d_Vector D ;
162     Graphic3d_Vertex P ;
163     Standard_Real CN,AN ;
164     
165     MyLight->Values(C,P,D,CN,A1,A2,AN) ;
166 }
167
168 Standard_Real V3d_SpotLight::Concentration()const
169 {
170   Quantity_Color C ;
171   Graphic3d_Vector D ;
172   Graphic3d_Vertex P ;
173   Standard_Real AN,A1,A2,CN ;
174   
175   MyLight->Values(C,P,D,CN,A1,A2,AN) ;
176   return CN ;
177 }
178
179 Standard_Real V3d_SpotLight::Angle()const
180 {
181   Quantity_Color C ;
182   Graphic3d_Vector D ;
183   Graphic3d_Vertex P ;
184   Standard_Real CN,A1,A2,AN ;
185   
186   MyLight->Values(C,P,D,CN,A1,A2,AN) ;
187   return AN ;
188 }
189
190 void V3d_SpotLight::Symbol (const Handle(Graphic3d_Group)& gsymbol,
191                             const Handle(V3d_View)& ) const
192 {
193   Standard_Real X,Y,Z;
194   Standard_Real DX,DY,DZ;
195   this->Position(X,Y,Z);
196   this->Direction(DX,DY,DZ);
197
198   V3d::ArrowOfRadius(gsymbol,X,Y,Z,-DX,-DY,-DZ,M_PI/8.,this->Radius()/15.);
199 }
200     
201 void V3d_SpotLight::Display( const Handle(V3d_View)& aView,
202                              const V3d_TypeOfRepresentation TPres)
203 {
204   Graphic3d_Vertex PText ;
205   Standard_Real X,Y,Z,Rayon;
206   Standard_Real X0,Y0,Z0,VX,VY,VZ;
207   Standard_Real X1,Y1,Z1;
208   Standard_Real DXRef,DYRef,DZRef,DXini,DYini,DZini;
209   Standard_Real R1,G1,B1;
210   V3d_TypeOfRepresentation Pres;
211   V3d_TypeOfUpdate UpdSov;
212
213 //  Creation of a structure slight of markable elements (position of the
214 //  light, and the domain of lighting represented by a circle)
215 //  Creation of a structure snopick of non-markable elements (target, meridian and 
216 //  parallel).// 
217
218   Pres = TPres;
219   Handle(V3d_Viewer) TheViewer = aView->Viewer();
220   UpdSov = TheViewer->UpdateMode();
221   TheViewer->SetUpdateMode(V3d_WAIT);
222   if (!MyGraphicStructure.IsNull()) {
223     MyGraphicStructure->Disconnect(MyGraphicStructure1);
224     MyGraphicStructure->Clear();
225     MyGraphicStructure1->Clear();
226     if (Pres == V3d_SAMELAST) Pres = MyTypeOfRepresentation;
227   }
228   else {
229     if (Pres == V3d_SAMELAST) Pres = V3d_SIMPLE;
230     Handle(Graphic3d_Structure) slight = new Graphic3d_Structure(TheViewer->Viewer());
231     MyGraphicStructure = slight;
232     Handle(Graphic3d_Structure) snopick = new Graphic3d_Structure(TheViewer->Viewer()); 
233     MyGraphicStructure1 = snopick;
234   }
235
236   Handle(Graphic3d_Group) gradius, gExtArrow, gIntArrow;
237   if (Pres == V3d_COMPLETE)
238   {
239     gradius   = MyGraphicStructure->NewGroup();
240     gExtArrow = MyGraphicStructure->NewGroup();
241     gIntArrow = MyGraphicStructure->NewGroup();
242   }
243   Handle(Graphic3d_Group) glight = MyGraphicStructure->NewGroup();
244   Handle(Graphic3d_Group) gsphere;
245   if (Pres == V3d_COMPLETE
246    || Pres == V3d_PARTIAL)
247   {
248     gsphere = MyGraphicStructure->NewGroup();
249   }
250   
251   Handle(Graphic3d_Group) gnopick = MyGraphicStructure1->NewGroup();
252   MyGraphicStructure1->SetPick(Standard_False);
253   
254   X0 = MyTarget.X();
255   Y0 = MyTarget.Y();
256   Z0 = MyTarget.Z();
257   
258 //Display of the position of the light.
259
260   this->Color(Quantity_TOC_RGB,R1,G1,B1);
261   Quantity_Color Col1(R1,G1,B1,Quantity_TOC_RGB);
262   Handle(Graphic3d_AspectLine3d) Asp1 = new Graphic3d_AspectLine3d();
263   Asp1->SetColor(Col1);
264   glight->SetPrimitivesAspect(Asp1);
265   this->Symbol(glight,aView);
266   
267 // Display of the reference sphere (limited by circle).
268
269   if (Pres == V3d_COMPLETE || Pres == V3d_PARTIAL) {
270     
271     Rayon = this->Radius(); 
272     aView->Proj(VX,VY,VZ);
273     V3d::CircleInPlane(gsphere,X0,Y0,Z0,VX,VY,VZ,Rayon);
274
275 // Display of the radius of the sphere (line + text)
276
277     if (Pres == V3d_COMPLETE) {
278       this->Position(X,Y,Z);
279       Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(2);
280       aPrims->AddVertex(X0,Y0,Z0);
281       aPrims->AddVertex(X,Y,Z);
282       gnopick->AddPrimitiveArray(aPrims);
283       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.);
284       V3d::ArrowOfRadius(gIntArrow,X0,Y0,Z0,X0-X,Y0-Y,Z0-Z,M_PI/15.,Rayon/20.);
285       TCollection_AsciiString ValOfRadius(Rayon);
286       PText.SetCoord( .5*(X0+X), .5*(Y0+Y), .5*(Z0+Z) );
287       gradius->Text(ValOfRadius.ToCString(),PText,0.01);
288     }
289     
290 // Display of the meridian
291
292     Quantity_Color Col2(Quantity_NOC_GREEN);
293     Handle(Graphic3d_AspectLine3d) Asp2 = new Graphic3d_AspectLine3d(Col2,Aspect_TOL_SOLID,1.);
294     gnopick->SetPrimitivesAspect(Asp2);
295     
296 //    Definition of the axis of the circle
297     aView->Up(DXRef,DYRef,DZRef);
298     this->Position(X,Y,Z);
299     DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
300     VX = DYRef*DZini - DZRef*DYini;
301     VY = DZRef*DXini - DXRef*DZini;
302     VZ = DXRef*DYini - DYRef*DXini;
303     
304     V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
305
306 //    Display of the parallel
307
308 //    Definition of the axis of the circle
309     aView->Proj(VX,VY,VZ);
310     aView->Up(X1,Y1,Z1);
311     DXRef = VY * Z1 - VZ * Y1;
312     DYRef = VZ * X1 - VX * Z1;
313     DZRef = VX * Y1 - VY * X1;
314     this->Position(X,Y,Z);
315     DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
316     VX = DYRef*DZini - DZRef*DYini;
317     VY = DZRef*DXini - DXRef*DZini;
318     VZ = DXRef*DYini - DYRef*DXini;
319     
320     V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
321   }
322   
323   MyGraphicStructure->Connect(MyGraphicStructure1,Graphic3d_TOC_DESCENDANT);
324   MyTypeOfRepresentation = Pres;
325   MyGraphicStructure->Display();
326   TheViewer->SetUpdateMode(UpdSov);
327 }