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 /***********************************************************************
17 Classe V3d_SpotLight :
18 HISTORIQUE DES MODIFICATIONS :
19 --------------------------------
20 00-09-92 : GG ; Creation.
21 18-06-96 : FMN ; Ajout MyGraphicStructure1 pour sauvegarder snopick
22 30-03-98 : ZOV ; PRO6774 (reconstruction of the class hierarchy and suppressing useless methods)
23 02.15.100 : JR : Clutter
24 ************************************************************************/
25 /*----------------------------------------------------------------------*/
33 #include <gp_Trsf.hxx>
35 #include <Graphic3d_ArrayOfSegments.hxx>
36 #include <Graphic3d_AspectLine3d.hxx>
37 #include <Graphic3d_AspectMarker3d.hxx>
38 #include <Graphic3d_AspectText3d.hxx>
39 #include <Graphic3d_Group.hxx>
40 #include <Graphic3d_Structure.hxx>
41 #include <Graphic3d_Vector.hxx>
42 #include <Graphic3d_Vertex.hxx>
43 #include <Standard_Type.hxx>
44 #include <TCollection_AsciiString.hxx>
46 #include <V3d_BadValue.hxx>
47 #include <V3d_SpotLight.hxx>
48 #include <V3d_View.hxx>
49 #include <V3d_Viewer.hxx>
51 IMPLEMENT_STANDARD_RTTIEXT(V3d_SpotLight,V3d_PositionLight)
53 // =======================================================================
54 // function : V3d_SpotLight
56 // =======================================================================
57 V3d_SpotLight::V3d_SpotLight (const Handle(V3d_Viewer)& theViewer,
58 const Standard_Real theX,
59 const Standard_Real theY,
60 const Standard_Real theZ,
61 const V3d_TypeOfOrientation theDirection,
62 const Quantity_NameOfColor theColor,
63 const Standard_Real theConstAttenuation,
64 const Standard_Real theLinearAttenuation,
65 const Standard_Real theConcentration,
66 const Standard_Real theAngle)
67 : V3d_PositionLight (theViewer)
69 Graphic3d_Vector aDir = V3d::GetProjAxis (theDirection);
72 SetTarget (theX + aDir.X(), theY + aDir.Y(), theZ + aDir.Z());
73 SetPosition (theX, theY, theZ);
74 SetDirection (aDir.X(), aDir.Y(), aDir.Z());
75 SetAttenuation (theConstAttenuation, theLinearAttenuation);
76 SetConcentration (theConcentration);
80 // =======================================================================
81 // function : V3d_SpotLight
83 // =======================================================================
84 V3d_SpotLight::V3d_SpotLight (const Handle(V3d_Viewer)& theViewer,
85 const Standard_Real theXt,
86 const Standard_Real theYt,
87 const Standard_Real theZt,
88 const Standard_Real theXp,
89 const Standard_Real theYp,
90 const Standard_Real theZp,
91 const Quantity_NameOfColor theColor,
92 const Standard_Real theConstAttenuation,
93 const Standard_Real theLinearAttenuation,
94 const Standard_Real theConcentration,
95 const Standard_Real theAngle)
96 : V3d_PositionLight (theViewer)
100 SetTarget (theXt, theYt, theZt);
101 SetPosition (theXp, theYp, theZp);
102 SetDirection (theXt - theXp, theYt - theYp, theZt - theZp);
103 SetAttenuation (theConstAttenuation, theLinearAttenuation);
104 SetConcentration (theConcentration);
108 // =======================================================================
109 // function : SetPosition
111 // =======================================================================
112 void V3d_SpotLight::SetPosition (const Standard_Real theXp,
113 const Standard_Real theYp,
114 const Standard_Real theZp)
116 myLight.Position.x() = theXp;
117 myLight.Position.y() = theYp;
118 myLight.Position.z() = theZp;
121 // =======================================================================
122 // function : SetDirection
124 // =======================================================================
125 void V3d_SpotLight::SetDirection (const Standard_Real theVx,
126 const Standard_Real theVy,
127 const Standard_Real theVz)
129 myLight.Direction.x() = static_cast<Standard_ShortReal> (theVx);
130 myLight.Direction.y() = static_cast<Standard_ShortReal> (theVy);
131 myLight.Direction.z() = static_cast<Standard_ShortReal> (theVz);
134 // =======================================================================
135 // function : SetDirection
137 // =======================================================================
138 void V3d_SpotLight::SetDirection (const V3d_TypeOfOrientation theDirection)
140 Graphic3d_Vector aDir = V3d::GetProjAxis (theDirection);
141 SetDirection (aDir.X(), aDir.Y(), aDir.Z());
144 // =======================================================================
145 // function : SetAttenuation
147 // =======================================================================
148 void V3d_SpotLight::SetAttenuation (const Standard_Real theConstAttenuation,
149 const Standard_Real theLinearAttenuation)
151 V3d_BadValue_Raise_if (theConstAttenuation < 0. ||
152 theConstAttenuation > 1. ||
153 theLinearAttenuation < 0. ||
154 theLinearAttenuation > 1 ,
155 "V3d_SpotLight::SetAttenuation, "
158 myLight.ChangeConstAttenuation() = static_cast<Standard_ShortReal> (theConstAttenuation);
159 myLight.ChangeLinearAttenuation() = static_cast<Standard_ShortReal> (theLinearAttenuation);
162 // =======================================================================
163 // function : SetConcentration
165 // =======================================================================
166 void V3d_SpotLight::SetConcentration (const Standard_Real theConcentration)
168 V3d_BadValue_Raise_if (theConcentration < 0. ||
169 theConcentration > 1.,
170 "V3d_SpotLight::SetConcentration, "
173 myLight.ChangeConcentration() = static_cast<Standard_ShortReal> (theConcentration);
176 // =======================================================================
177 // function : SetAngle
179 // =======================================================================
180 void V3d_SpotLight::SetAngle (const Standard_Real theAngle)
182 V3d_BadValue_Raise_if (theAngle <= 0. ||
184 "V3d_SpotLight::SetAngle, "
187 myLight.ChangeAngle() = static_cast<Standard_ShortReal> (theAngle);
190 // =======================================================================
191 // function : Direction
193 // =======================================================================
194 void V3d_SpotLight::Direction (Standard_Real& theVx,
195 Standard_Real& theVy,
196 Standard_Real& theVz) const
198 theVx = myLight.Direction.x();
199 theVy = myLight.Direction.y();
200 theVz = myLight.Direction.z();
203 // =======================================================================
204 // function : Direction
206 // =======================================================================
207 void V3d_SpotLight::Position (Standard_Real& theXp,
208 Standard_Real& theYp,
209 Standard_Real& theZp) const
211 theXp = myLight.Position.x();
212 theYp = myLight.Position.y();
213 theZp = myLight.Position.z();
216 // =======================================================================
217 // function : Attenuation
219 // =======================================================================
220 void V3d_SpotLight::Attenuation (Standard_Real& theConstAttenuation,
221 Standard_Real& theLinearAttenuation) const
223 theConstAttenuation = myLight.ConstAttenuation();
224 theLinearAttenuation = myLight.LinearAttenuation();
227 // =======================================================================
228 // function : Concentration
230 // =======================================================================
231 Standard_Real V3d_SpotLight::Concentration ()const
233 return myLight.Concentration();
236 // =======================================================================
237 // function : Concentration
239 // =======================================================================
240 Standard_Real V3d_SpotLight::Angle()const
242 return myLight.Angle();
245 // =======================================================================
248 // =======================================================================
249 void V3d_SpotLight::Symbol (const Handle(Graphic3d_Group)& theSymbol,
250 const Handle(V3d_View)& ) const
253 Standard_Real DX,DY,DZ;
254 this->Position(X,Y,Z);
255 this->Direction(DX,DY,DZ);
257 V3d::ArrowOfRadius(theSymbol,X,Y,Z,-DX,-DY,-DZ,M_PI/8.,this->Radius()/15.);
260 // =======================================================================
261 // function : Display
263 // =======================================================================
264 void V3d_SpotLight::Display (const Handle(V3d_View)& theView,
265 const V3d_TypeOfRepresentation theTPres)
267 Graphic3d_Vertex PText ;
268 Standard_Real X,Y,Z,Rayon;
269 Standard_Real X0,Y0,Z0,VX,VY,VZ;
270 Standard_Real X1,Y1,Z1;
271 Standard_Real DXRef,DYRef,DZRef,DXini,DYini,DZini;
272 Standard_Real R1,G1,B1;
273 V3d_TypeOfRepresentation Pres;
275 // Creation of a structure slight of markable elements (position of the
276 // light, and the domain of lighting represented by a circle)
277 // Creation of a structure snopick of non-markable elements (target, meridian and
281 Handle(V3d_Viewer) TheViewer = theView->Viewer();
282 if (!myGraphicStructure.IsNull()) {
283 myGraphicStructure->Disconnect(myGraphicStructure1);
284 myGraphicStructure->Clear();
285 myGraphicStructure1->Clear();
286 if (Pres == V3d_SAMELAST) Pres = myTypeOfRepresentation;
289 if (Pres == V3d_SAMELAST) Pres = V3d_SIMPLE;
290 Handle(Graphic3d_Structure) slight = new Graphic3d_Structure(TheViewer->StructureManager());
291 myGraphicStructure = slight;
292 Handle(Graphic3d_Structure) snopick = new Graphic3d_Structure(TheViewer->StructureManager());
293 myGraphicStructure1 = snopick;
296 Handle(Graphic3d_Group) gradius, gExtArrow, gIntArrow;
297 if (Pres == V3d_COMPLETE)
299 gradius = myGraphicStructure->NewGroup();
300 gExtArrow = myGraphicStructure->NewGroup();
301 gIntArrow = myGraphicStructure->NewGroup();
303 Handle(Graphic3d_Group) glight = myGraphicStructure->NewGroup();
304 Handle(Graphic3d_Group) gsphere;
305 if (Pres == V3d_COMPLETE
306 || Pres == V3d_PARTIAL)
308 gsphere = myGraphicStructure->NewGroup();
311 Handle(Graphic3d_Group) gnopick = myGraphicStructure1->NewGroup();
317 //Display of the position of the light.
319 this->Color(Quantity_TOC_RGB,R1,G1,B1);
320 Quantity_Color Col1(R1,G1,B1,Quantity_TOC_RGB);
321 Handle(Graphic3d_AspectLine3d) Asp1 = new Graphic3d_AspectLine3d();
322 Asp1->SetColor(Col1);
323 glight->SetPrimitivesAspect(Asp1);
324 this->Symbol(glight,theView);
326 // Display of the reference sphere (limited by circle).
328 if (Pres == V3d_COMPLETE || Pres == V3d_PARTIAL) {
330 Rayon = this->Radius();
331 theView->Proj(VX,VY,VZ);
332 V3d::CircleInPlane(gsphere,X0,Y0,Z0,VX,VY,VZ,Rayon);
334 // Display of the radius of the sphere (line + text)
336 if (Pres == V3d_COMPLETE) {
337 this->Position(X,Y,Z);
338 Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(2);
339 aPrims->AddVertex(X0,Y0,Z0);
340 aPrims->AddVertex(X,Y,Z);
341 gnopick->AddPrimitiveArray(aPrims);
342 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.);
343 V3d::ArrowOfRadius(gIntArrow,X0,Y0,Z0,X0-X,Y0-Y,Z0-Z,M_PI/15.,Rayon/20.);
344 TCollection_AsciiString ValOfRadius(Rayon);
345 PText.SetCoord( .5*(X0+X), .5*(Y0+Y), .5*(Z0+Z) );
346 gradius->Text(ValOfRadius.ToCString(),PText,0.01);
349 // Display of the meridian
351 Quantity_Color Col2(Quantity_NOC_GREEN);
352 Handle(Graphic3d_AspectLine3d) Asp2 = new Graphic3d_AspectLine3d(Col2,Aspect_TOL_SOLID,1.);
353 gnopick->SetPrimitivesAspect(Asp2);
355 // Definition of the axis of the circle
356 theView->Up(DXRef,DYRef,DZRef);
357 this->Position(X,Y,Z);
358 DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
359 VX = DYRef*DZini - DZRef*DYini;
360 VY = DZRef*DXini - DXRef*DZini;
361 VZ = DXRef*DYini - DYRef*DXini;
363 V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
365 // Display of the parallel
367 // Definition of the axis of the circle
368 theView->Proj(VX,VY,VZ);
369 theView->Up(X1,Y1,Z1);
370 DXRef = VY * Z1 - VZ * Y1;
371 DYRef = VZ * X1 - VX * Z1;
372 DZRef = VX * Y1 - VY * X1;
373 this->Position(X,Y,Z);
374 DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
375 VX = DYRef*DZini - DZRef*DYini;
376 VY = DZRef*DXini - DXRef*DZini;
377 VZ = DXRef*DYini - DYRef*DXini;
379 V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
382 myGraphicStructure->Connect(myGraphicStructure1,Graphic3d_TOC_DESCENDANT);
383 myTypeOfRepresentation = Pres;
384 myGraphicStructure->Display();