53a8fa58d6b9021b416cf8173b6824e45e4be4fb
[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 #include <V3d_SpotLight.hxx>
15
16 #include <Graphic3d_ArrayOfSegments.hxx>
17 #include <Graphic3d_AspectLine3d.hxx>
18 #include <Graphic3d_Group.hxx>
19 #include <Graphic3d_Structure.hxx>
20 #include <TCollection_AsciiString.hxx>
21 #include <V3d.hxx>
22 #include <V3d_BadValue.hxx>
23 #include <V3d_View.hxx>
24 #include <V3d_Viewer.hxx>
25
26 IMPLEMENT_STANDARD_RTTIEXT(V3d_SpotLight,V3d_PositionLight)
27
28 // =======================================================================
29 // function : V3d_SpotLight
30 // purpose  :
31 // =======================================================================
32 V3d_SpotLight::V3d_SpotLight (const Handle(V3d_Viewer)& theViewer,
33                               const Standard_Real theX,
34                               const Standard_Real theY,
35                               const Standard_Real theZ,
36                               const V3d_TypeOfOrientation theDirection,
37                               const Quantity_Color& theColor,
38                               const Standard_Real theConstAttenuation,
39                               const Standard_Real theLinearAttenuation,
40                               const Standard_Real theConcentration,
41                               const Standard_Real theAngle)
42 : V3d_PositionLight (theViewer)
43 {
44   gp_Dir aDir = V3d::GetProjAxis (theDirection);
45   SetType (V3d_SPOT);
46   SetColor (theColor);
47   SetTarget (theX + aDir.X(), theY + aDir.Y(), theZ + aDir.Z());
48   SetPosition (theX, theY, theZ);
49   SetDirection (aDir.X(), aDir.Y(), aDir.Z());
50   SetAttenuation (theConstAttenuation, theLinearAttenuation);
51   SetConcentration (theConcentration);
52   SetAngle (theAngle);
53 }
54
55 // =======================================================================
56 // function : V3d_SpotLight
57 // purpose  :
58 // =======================================================================
59 V3d_SpotLight::V3d_SpotLight (const Handle(V3d_Viewer)& theViewer,
60                               const Standard_Real theXt,
61                               const Standard_Real theYt,
62                               const Standard_Real theZt,
63                               const Standard_Real theXp,
64                               const Standard_Real theYp,
65                               const Standard_Real theZp,
66                               const Quantity_Color& theColor,
67                               const Standard_Real theConstAttenuation,
68                               const Standard_Real theLinearAttenuation,
69                               const Standard_Real theConcentration,
70                               const Standard_Real theAngle)
71 : V3d_PositionLight (theViewer)
72 {
73   SetType (V3d_SPOT);
74   SetColor (theColor);
75   SetTarget (theXt, theYt, theZt);
76   SetPosition (theXp, theYp, theZp);
77   SetDirection (theXt - theXp, theYt - theYp, theZt - theZp);
78   SetAttenuation (theConstAttenuation, theLinearAttenuation);
79   SetConcentration (theConcentration);
80   SetAngle (theAngle);
81 }
82
83 // =======================================================================
84 // function : SetDirection
85 // purpose  :
86 // =======================================================================
87 void V3d_SpotLight::SetDirection (V3d_TypeOfOrientation theDirection)
88 {
89   gp_Dir aDir = V3d::GetProjAxis (theDirection);
90   SetDirection (aDir.X(), aDir.Y(), aDir.Z());
91 }
92
93 // =======================================================================
94 // function : SetAttenuation
95 // purpose  :
96 // =======================================================================
97 void V3d_SpotLight::SetAttenuation (const Standard_Real theConstAttenuation,
98                                     const Standard_Real theLinearAttenuation)
99 {
100   V3d_BadValue_Raise_if (theConstAttenuation  < 0. ||
101                          theConstAttenuation  > 1. ||
102                          theLinearAttenuation < 0. ||
103                          theLinearAttenuation > 1 ,
104                          "V3d_SpotLight::SetAttenuation, "
105                          "bad coefficients");
106
107   myLight.ChangeConstAttenuation()  = static_cast<Standard_ShortReal> (theConstAttenuation);
108   myLight.ChangeLinearAttenuation() = static_cast<Standard_ShortReal> (theLinearAttenuation);
109 }
110
111 // =======================================================================
112 // function : SetConcentration
113 // purpose  :
114 // =======================================================================
115 void V3d_SpotLight::SetConcentration (const Standard_Real theConcentration)
116 {
117   V3d_BadValue_Raise_if (theConcentration < 0. ||
118                          theConcentration > 1.,
119                          "V3d_SpotLight::SetConcentration, "
120                          "bad coefficient");
121
122   myLight.ChangeConcentration() = static_cast<Standard_ShortReal> (theConcentration);
123 }
124
125 // =======================================================================
126 // function : SetAngle
127 // purpose  :
128 // =======================================================================
129 void V3d_SpotLight::SetAngle (const Standard_Real theAngle)
130 {
131   V3d_BadValue_Raise_if (theAngle <= 0. || 
132                          theAngle >= M_PI,
133                          "V3d_SpotLight::SetAngle, "
134                          "bad angle");
135
136   myLight.ChangeAngle() = static_cast<Standard_ShortReal> (theAngle);
137 }
138 // =======================================================================
139 // function : Symbol
140 // purpose  :
141 // =======================================================================
142 void V3d_SpotLight::Symbol (const Handle(Graphic3d_Group)& theSymbol,
143                             const Handle(V3d_View)& ) const
144 {
145   Standard_Real X,Y,Z;
146   Standard_Real DX,DY,DZ;
147   this->Position(X,Y,Z);
148   this->Direction(DX,DY,DZ);
149
150   V3d::ArrowOfRadius(theSymbol,X,Y,Z,-DX,-DY,-DZ,M_PI/8.,this->Radius()/15.);
151 }
152
153 // =======================================================================
154 // function : Display
155 // purpose  :
156 // =======================================================================
157 void V3d_SpotLight::Display (const Handle(V3d_View)& theView,
158                              const V3d_TypeOfRepresentation theTPres)
159 {
160   Standard_Real X,Y,Z,Rayon;
161   Standard_Real X0,Y0,Z0,VX,VY,VZ;
162   Standard_Real X1,Y1,Z1;
163   Standard_Real DXRef,DYRef,DZRef,DXini,DYini,DZini;
164   V3d_TypeOfRepresentation Pres;
165
166 //  Creation of a structure slight of markable elements (position of the
167 //  light, and the domain of lighting represented by a circle)
168 //  Creation of a structure snopick of non-markable elements (target, meridian and 
169 //  parallel).// 
170
171   Pres = theTPres;
172   Handle(V3d_Viewer) TheViewer = theView->Viewer();
173   if (!myGraphicStructure.IsNull()) {
174     myGraphicStructure->Disconnect(myGraphicStructure1);
175     myGraphicStructure->Clear();
176     myGraphicStructure1->Clear();
177     if (Pres == V3d_SAMELAST) Pres = myTypeOfRepresentation;
178   }
179   else {
180     if (Pres == V3d_SAMELAST) Pres = V3d_SIMPLE;
181     Handle(Graphic3d_Structure) slight = new Graphic3d_Structure(TheViewer->StructureManager());
182     myGraphicStructure = slight;
183     Handle(Graphic3d_Structure) snopick = new Graphic3d_Structure(TheViewer->StructureManager()); 
184     myGraphicStructure1 = snopick;
185   }
186
187   Handle(Graphic3d_Group) gradius, gExtArrow, gIntArrow;
188   if (Pres == V3d_COMPLETE)
189   {
190     gradius   = myGraphicStructure->NewGroup();
191     gExtArrow = myGraphicStructure->NewGroup();
192     gIntArrow = myGraphicStructure->NewGroup();
193   }
194   Handle(Graphic3d_Group) glight = myGraphicStructure->NewGroup();
195   Handle(Graphic3d_Group) gsphere;
196   if (Pres == V3d_COMPLETE
197    || Pres == V3d_PARTIAL)
198   {
199     gsphere = myGraphicStructure->NewGroup();
200   }
201   
202   Handle(Graphic3d_Group) gnopick = myGraphicStructure1->NewGroup();
203   
204   X0 = myTarget.X();
205   Y0 = myTarget.Y();
206   Z0 = myTarget.Z();
207   
208 //Display of the position of the light.
209
210   const Quantity_Color Col1 = this->Color();
211   Handle(Graphic3d_AspectLine3d) Asp1 = new Graphic3d_AspectLine3d();
212   Asp1->SetColor(Col1);
213   glight->SetPrimitivesAspect(Asp1);
214   this->Symbol(glight,theView);
215   
216 // Display of the reference sphere (limited by circle).
217
218   if (Pres == V3d_COMPLETE || Pres == V3d_PARTIAL) {
219     
220     Rayon = this->Radius(); 
221     theView->Proj(VX,VY,VZ);
222     V3d::CircleInPlane(gsphere,X0,Y0,Z0,VX,VY,VZ,Rayon);
223
224 // Display of the radius of the sphere (line + text)
225
226     if (Pres == V3d_COMPLETE) {
227       this->Position(X,Y,Z);
228       Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(2);
229       aPrims->AddVertex(X0,Y0,Z0);
230       aPrims->AddVertex(X,Y,Z);
231       gnopick->AddPrimitiveArray(aPrims);
232       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.);
233       V3d::ArrowOfRadius(gIntArrow,X0,Y0,Z0,X0-X,Y0-Y,Z0-Z,M_PI/15.,Rayon/20.);
234       TCollection_AsciiString ValOfRadius(Rayon);
235       Graphic3d_Vertex PText ( .5*(X0+X), .5*(Y0+Y), .5*(Z0+Z) );
236       gradius->Text(ValOfRadius.ToCString(),PText,0.01);
237     }
238     
239 // Display of the meridian
240
241     Quantity_Color Col2(Quantity_NOC_GREEN);
242     Handle(Graphic3d_AspectLine3d) Asp2 = new Graphic3d_AspectLine3d(Col2,Aspect_TOL_SOLID,1.);
243     gnopick->SetPrimitivesAspect(Asp2);
244     
245 //    Definition of the axis of the circle
246     theView->Up(DXRef,DYRef,DZRef);
247     this->Position(X,Y,Z);
248     DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
249     VX = DYRef*DZini - DZRef*DYini;
250     VY = DZRef*DXini - DXRef*DZini;
251     VZ = DXRef*DYini - DYRef*DXini;
252     
253     V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
254
255 //    Display of the parallel
256
257 //    Definition of the axis of the circle
258     theView->Proj(VX,VY,VZ);
259     theView->Up(X1,Y1,Z1);
260     DXRef = VY * Z1 - VZ * Y1;
261     DYRef = VZ * X1 - VX * Z1;
262     DZRef = VX * Y1 - VY * X1;
263     this->Position(X,Y,Z);
264     DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
265     VX = DYRef*DZini - DZRef*DYini;
266     VY = DZRef*DXini - DXRef*DZini;
267     VZ = DXRef*DYini - DYRef*DXini;
268     
269     V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
270   }
271   
272   myGraphicStructure->Connect(myGraphicStructure1,Graphic3d_TOC_DESCENDANT);
273   myTypeOfRepresentation = Pres;
274   myGraphicStructure->Display();
275 }