1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
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.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 #include <V3d_SpotLight.hxx>
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>
22 #include <V3d_BadValue.hxx>
23 #include <V3d_View.hxx>
24 #include <V3d_Viewer.hxx>
26 IMPLEMENT_STANDARD_RTTIEXT(V3d_SpotLight,V3d_PositionLight)
28 // =======================================================================
29 // function : V3d_SpotLight
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)
44 gp_Dir aDir = V3d::GetProjAxis (theDirection);
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);
55 // =======================================================================
56 // function : V3d_SpotLight
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)
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);
83 // =======================================================================
84 // function : SetDirection
86 // =======================================================================
87 void V3d_SpotLight::SetDirection (V3d_TypeOfOrientation theDirection)
89 gp_Dir aDir = V3d::GetProjAxis (theDirection);
90 SetDirection (aDir.X(), aDir.Y(), aDir.Z());
93 // =======================================================================
94 // function : SetAttenuation
96 // =======================================================================
97 void V3d_SpotLight::SetAttenuation (const Standard_Real theConstAttenuation,
98 const Standard_Real theLinearAttenuation)
100 V3d_BadValue_Raise_if (theConstAttenuation < 0. ||
101 theConstAttenuation > 1. ||
102 theLinearAttenuation < 0. ||
103 theLinearAttenuation > 1 ,
104 "V3d_SpotLight::SetAttenuation, "
107 myLight.ChangeConstAttenuation() = static_cast<Standard_ShortReal> (theConstAttenuation);
108 myLight.ChangeLinearAttenuation() = static_cast<Standard_ShortReal> (theLinearAttenuation);
111 // =======================================================================
112 // function : SetConcentration
114 // =======================================================================
115 void V3d_SpotLight::SetConcentration (const Standard_Real theConcentration)
117 V3d_BadValue_Raise_if (theConcentration < 0. ||
118 theConcentration > 1.,
119 "V3d_SpotLight::SetConcentration, "
122 myLight.ChangeConcentration() = static_cast<Standard_ShortReal> (theConcentration);
125 // =======================================================================
126 // function : SetAngle
128 // =======================================================================
129 void V3d_SpotLight::SetAngle (const Standard_Real theAngle)
131 V3d_BadValue_Raise_if (theAngle <= 0. ||
133 "V3d_SpotLight::SetAngle, "
136 myLight.ChangeAngle() = static_cast<Standard_ShortReal> (theAngle);
138 // =======================================================================
141 // =======================================================================
142 void V3d_SpotLight::Symbol (const Handle(Graphic3d_Group)& theSymbol,
143 const Handle(V3d_View)& ) const
146 Standard_Real DX,DY,DZ;
147 this->Position(X,Y,Z);
148 this->Direction(DX,DY,DZ);
150 V3d::ArrowOfRadius(theSymbol,X,Y,Z,-DX,-DY,-DZ,M_PI/8.,this->Radius()/15.);
153 // =======================================================================
154 // function : Display
156 // =======================================================================
157 void V3d_SpotLight::Display (const Handle(V3d_View)& theView,
158 const V3d_TypeOfRepresentation theTPres)
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;
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
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;
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;
187 Handle(Graphic3d_Group) gradius, gExtArrow, gIntArrow;
188 if (Pres == V3d_COMPLETE)
190 gradius = myGraphicStructure->NewGroup();
191 gExtArrow = myGraphicStructure->NewGroup();
192 gIntArrow = myGraphicStructure->NewGroup();
194 Handle(Graphic3d_Group) glight = myGraphicStructure->NewGroup();
195 Handle(Graphic3d_Group) gsphere;
196 if (Pres == V3d_COMPLETE
197 || Pres == V3d_PARTIAL)
199 gsphere = myGraphicStructure->NewGroup();
202 Handle(Graphic3d_Group) gnopick = myGraphicStructure1->NewGroup();
208 //Display of the position of the light.
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);
216 // Display of the reference sphere (limited by circle).
218 if (Pres == V3d_COMPLETE || Pres == V3d_PARTIAL) {
220 Rayon = this->Radius();
221 theView->Proj(VX,VY,VZ);
222 V3d::CircleInPlane(gsphere,X0,Y0,Z0,VX,VY,VZ,Rayon);
224 // Display of the radius of the sphere (line + text)
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);
239 // Display of the meridian
241 Quantity_Color Col2(Quantity_NOC_GREEN);
242 Handle(Graphic3d_AspectLine3d) Asp2 = new Graphic3d_AspectLine3d(Col2,Aspect_TOL_SOLID,1.);
243 gnopick->SetPrimitivesAspect(Asp2);
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;
253 V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
255 // Display of the parallel
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;
269 V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
272 myGraphicStructure->Connect(myGraphicStructure1,Graphic3d_TOC_DESCENDANT);
273 myTypeOfRepresentation = Pres;
274 myGraphicStructure->Display();