0028726: Quantity_NameOfColor should be replaced by Quantity_Color in function input...
[occt.git] / src / V3d / V3d_DirectionalLight.cxx
CommitLineData
973c2be1 1// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 2//
973c2be1 3// This file is part of Open CASCADE Technology software library.
b311480e 4//
d5f74e42 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
973c2be1 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.
b311480e 10//
973c2be1 11// Alternatively, this file may be used under the terms of Open CASCADE
12// commercial license or contractual agreement.
b311480e 13
7fd59977 14/***********************************************************************
7fd59977 15 FONCTION :
16 ----------
17 Classe V3d_DirectionalLight :
7fd59977 18 HISTORIQUE DES MODIFICATIONS :
19 --------------------------------
20 00-09-92 : GG ; Creation.
21 18-06-96 : FMN ; Ajout MyGraphicStructure1 pour sauvegarder snopick
22 24-12-97 : FMN ; Remplacement de math par MathGra
23 31-12-97 : CAL ; Suppression de MathGra
24 21-01-98 : CAL ; Window de Xw et WNT remplacee par Aspect_Window
25 23-02-98 : FMN ; Remplacement PI par Standard_PI
26 30-03-98 : ZOV ; PRO6774 (reconstruction of the class hierarchy and suppressing useless methods)
7fd59977 27************************************************************************/
7fd59977 28/*----------------------------------------------------------------------*/
29/*
30 * Includes
31 */
32
42cf5bc1 33#include <Aspect_Window.hxx>
34#include <gp_Ax1.hxx>
35#include <gp_Dir.hxx>
36#include <gp_Pnt.hxx>
37#include <gp_Trsf.hxx>
38#include <gp_Vec.hxx>
b8ddfc2f 39#include <Graphic3d_ArrayOfSegments.hxx>
7fd59977 40#include <Graphic3d_AspectLine3d.hxx>
42cf5bc1 41#include <Graphic3d_AspectMarker3d.hxx>
7fd59977 42#include <Graphic3d_AspectText3d.hxx>
42cf5bc1 43#include <Graphic3d_Group.hxx>
44#include <Graphic3d_Structure.hxx>
45#include <Graphic3d_Vector.hxx>
46#include <Graphic3d_Vertex.hxx>
47#include <Standard_Type.hxx>
48#include <TColStd_Array2OfReal.hxx>
49#include <V3d.hxx>
50#include <V3d_BadValue.hxx>
51#include <V3d_DirectionalLight.hxx>
52#include <V3d_View.hxx>
53#include <V3d_Viewer.hxx>
7fd59977 54
92efcf78 55IMPLEMENT_STANDARD_RTTIEXT(V3d_DirectionalLight,V3d_PositionLight)
56
c357e426 57// =======================================================================
58// function : V3d_DirectionalLight
59// purpose :
60// =======================================================================
61V3d_DirectionalLight::V3d_DirectionalLight (const Handle(V3d_Viewer)& theViewer,
62 const V3d_TypeOfOrientation theDirection,
87432b82 63 const Quantity_Color& theColor,
c357e426 64 const Standard_Boolean theIsHeadlight)
65: V3d_PositionLight (theViewer)
66{
67 Graphic3d_Vector aV = V3d::GetProjAxis (theDirection);
68 SetType (V3d_DIRECTIONAL);
69 SetColor (theColor);
70 SetHeadlight (theIsHeadlight);
71 SetTarget (0., 0., 0.);
72 SetPosition (-aV.X(), -aV.Y(), -aV.Z());
6e728f3b 73 SetSmoothAngle (0.2);
74 SetIntensity (20.0);
7fd59977 75}
76
c357e426 77// =======================================================================
78// function : V3d_DirectionalLight
79// purpose :
80// =======================================================================
81V3d_DirectionalLight::V3d_DirectionalLight (const Handle(V3d_Viewer)& theViewer,
82 const Standard_Real theXt,
83 const Standard_Real theYt,
84 const Standard_Real theZt,
85 const Standard_Real theXp,
86 const Standard_Real theYp,
87 const Standard_Real theZp,
87432b82 88 const Quantity_Color& theColor,
c357e426 89 const Standard_Boolean theIsHeadlight)
90: V3d_PositionLight (theViewer)
91{
92 SetType (V3d_DIRECTIONAL);
93 SetColor (theColor);
94 SetHeadlight (theIsHeadlight);
95 SetTarget (theXt, theYt, theZt);
96 SetPosition (theXp, theYp, theZp);
7fd59977 97}
98
189f85a3 99// =======================================================================
100// function : SetSmoothAngle
101// purpose :
102// =======================================================================
103void V3d_DirectionalLight::SetSmoothAngle (const Standard_Real theValue)
104{
c357e426 105 V3d_BadValue_Raise_if (theValue < 0.0 || theValue > M_PI / 2.0,
106 "Bad value for smoothing angle");
189f85a3 107
c357e426 108 myLight.Smoothness = static_cast<Standard_ShortReal> (theValue);
109}
7fd59977 110
c357e426 111// =======================================================================
112// function : SetDirection
113// purpose :
114// =======================================================================
115void V3d_DirectionalLight::SetDirection (const V3d_TypeOfOrientation theDirection)
116{
117 Graphic3d_Vector aV = V3d::GetProjAxis (theDirection);
118 SetDirection (aV.X(), aV.Y(), aV.Z());
7fd59977 119}
120
c357e426 121// =======================================================================
122// function : SetDirection
123// purpose :
124// =======================================================================
125void V3d_DirectionalLight::SetDirection (const Standard_Real theVx,
126 const Standard_Real theVy,
127 const Standard_Real theVz)
128{
129 V3d_BadValue_Raise_if (Sqrt (theVx * theVx + theVy * theVy + theVz * theVz) <= 0.,
130 "V3d_DirectionalLight::SetDirection, "
131 "null vector" );
7fd59977 132
c357e426 133 Graphic3d_Vector aV (theVx, theVy, theVz);
134 aV.Normalize();
7fd59977 135
c357e426 136 myLight.Direction.x() = static_cast<Standard_ShortReal> (aV.X());
137 myLight.Direction.y() = static_cast<Standard_ShortReal> (aV.Y());
138 myLight.Direction.z() = static_cast<Standard_ShortReal> (aV.Z());
7fd59977 139}
140
c357e426 141// =======================================================================
142// function : SetDisplayPosition
143// purpose :
144// =======================================================================
145void V3d_DirectionalLight::SetDisplayPosition (const Standard_Real theX,
146 const Standard_Real theY,
147 const Standard_Real theZ)
148{
149 myDisplayPosition.SetCoord(theX, theY, theZ);
7fd59977 150
c357e426 151 Standard_Real aXt, aYt, aZt;
152 Target (aXt, aYt, aZt);
7fd59977 153
c357e426 154 Standard_Real aXd = aXt - theX;
155 Standard_Real aYd = aYt - theY;
156 Standard_Real aZd = aZt - theZ;
157 if (!Graphic3d_Vector (aXd, aYd, aZd).LengthZero())
158 {
159 SetDirection (aXd, aYd, aZd);
160 }
7fd59977 161}
162
c357e426 163// =======================================================================
164// function : SetPosition
165// purpose :
166// =======================================================================
167void V3d_DirectionalLight::SetPosition (const Standard_Real theXp,
168 const Standard_Real theYp,
169 const Standard_Real theZp)
170{
171 SetDisplayPosition (theXp, theYp, theZp);
7fd59977 172}
173
c357e426 174// =======================================================================
175// function : Position
176// purpose :
177// =======================================================================
178void V3d_DirectionalLight::Position (Standard_Real& theXp,
179 Standard_Real& theYp,
180 Standard_Real& theZp) const
181{
182 DisplayPosition (theXp, theYp, theZp) ;
7fd59977 183}
184
c357e426 185// =======================================================================
186// function : DisplayPosition
187// purpose :
188// =======================================================================
189void V3d_DirectionalLight::DisplayPosition (Standard_Real& theXp,
190 Standard_Real& theYp,
191 Standard_Real& theZp) const
192{
193 myDisplayPosition.Coord (theXp, theYp, theZp) ;
7fd59977 194}
195
c357e426 196// =======================================================================
197// function : DisplayPosition
198// purpose :
199// =======================================================================
200void V3d_DirectionalLight::Symbol (const Handle(Graphic3d_Group)& theSymbol, const Handle(V3d_View)& theView) const
b8ddfc2f 201{
7fd59977 202 Standard_Real Xi,Yi,Zi,Xf,Yf,Zf,Rayon,PXT,PYT,X,Y,Z,XT,YT,ZT;
203 Standard_Real A,B,C,Dist,Beta,CosBeta,SinBeta,Coef,X1,Y1,Z1;
204 Standard_Real DX,DY,DZ,VX,VY,VZ;
205 Standard_Integer IXP,IYP,j;
206 TColStd_Array2OfReal MatRot(0,2,0,2);
7fd59977 207
c357e426 208 theView->Proj(VX,VY,VZ);
7fd59977 209 this->DisplayPosition(Xi,Yi,Zi);
210 Rayon = this->Radius();
c357e426 211 theView->Project(Xi,Yi,Zi,PXT,PYT);
212 theView->Convert(PXT,PYT,IXP,IYP);
81bba717 213// Coordinated 3d in the plane of projection of the source.
c357e426 214 theView->Convert(IXP,IYP,XT,YT,ZT);
215 theView->Convert(PXT,PYT+Rayon,IXP,IYP);
216 theView->Convert(IXP,IYP,X,Y,Z);
7fd59977 217 X = X+Xi-XT; Y = Y+Yi-YT; Z = Z+Zi-ZT;
218 Dist = Sqrt( Square(X-Xi) + Square(Y-Yi) + Square(Z-Zi) );
81bba717 219// Axis of rotation.
7fd59977 220 A = (X-Xi)/Dist;
221 B = (Y-Yi)/Dist;
222 C = (Z-Zi)/Dist;
223
81bba717 224// A sphere is drawn
c357e426 225 V3d::CircleInPlane(theSymbol,Xi,Yi,Zi,VX,VY,VZ,Rayon/40.);
7fd59977 226 for( j=1 ; j<=3 ; j++ ) {
c6541a0c 227 Beta = j * M_PI / 4.;
7fd59977 228 CosBeta = Cos(Beta);
229 SinBeta = Sin(Beta);
230 Coef = 1. - CosBeta;
231 MatRot(0,0) = A * A + (1. - A * A) * CosBeta;
232 MatRot(0,1) = -C * SinBeta + Coef * A * B;
233 MatRot(0,2) = B * SinBeta + Coef * A * C;
234 MatRot(1,0) = C * SinBeta + Coef * A * B;
235 MatRot(1,1) = B * B + (1. - B * B) * CosBeta;
236 MatRot(1,2) = -A * SinBeta + Coef * B * C;
237 MatRot(2,0) = -B * SinBeta + Coef * A * C;
238 MatRot(2,1) = A * SinBeta + Coef * B * C;
239 MatRot(2,2) = C * C + (1. - C * C) * CosBeta;
240 Xf = Xi * MatRot(0,0) + Yi * MatRot(0,1) + Zi * MatRot(0,2);
241 Yf = Xi * MatRot(1,0) + Yi * MatRot(1,1) + Zi * MatRot(1,2);
242 Zf = Xi * MatRot(2,0) + Yi * MatRot(2,1) + Zi * MatRot(2,2);
81bba717 243// Rotation of the normal
7fd59977 244 X1 = VX * MatRot(0,0) + VY * MatRot(0,1) + VZ * MatRot(0,2);
245 Y1 = VX * MatRot(1,0) + VY * MatRot(1,1) + VZ * MatRot(1,2);
246 Z1 = VX * MatRot(2,0) + VY * MatRot(2,1) + VZ * MatRot(2,2);
247 VX = X1 + Xi - Xf ; VY = Y1 + Yi - Yf ; VZ = Z1 + Zi - Zf;
c357e426 248 V3d::CircleInPlane(theSymbol,Xi,Yi,Zi,VX,VY,VZ,Rayon/40.);
7fd59977 249 }
250
81bba717 251// The arrow is drawn
7fd59977 252 Rayon = this->Radius();
253 this->Direction(DX,DY,DZ);
254 X = Xi + DX*Rayon/10.; Y = Yi + DY*Rayon/10.; Z = Zi + DZ*Rayon/10.;
b8ddfc2f 255
256 Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(2);
257 aPrims->AddVertex(Standard_ShortReal(Xi),Standard_ShortReal(Yi),Standard_ShortReal(Zi));
258 aPrims->AddVertex(Standard_ShortReal(X),Standard_ShortReal(Y),Standard_ShortReal(Z));
c357e426 259 theSymbol->AddPrimitiveArray(aPrims);
b8ddfc2f 260
c357e426 261 V3d::ArrowOfRadius(theSymbol, X, Y, Z, DX, DY, DZ, M_PI / 15., Rayon / 20.);
7fd59977 262}
263
c357e426 264// =======================================================================
265// function : Display
266// purpose :
267// =======================================================================
268void V3d_DirectionalLight::Display (const Handle(V3d_View)& theView,
269 const V3d_TypeOfRepresentation theTPres)
270{
7fd59977 271 Standard_Real X,Y,Z,Rayon;
272 Standard_Real X0,Y0,Z0,VX,VY,VZ;
273 Standard_Real X1,Y1,Z1;
274 Standard_Real DXRef,DYRef,DZRef,DXini,DYini,DZini;
7fd59977 275 V3d_TypeOfRepresentation Pres;
7fd59977 276
81bba717 277// Creation of a structure of markable elements (position of the
278// light, and the domain of lighting represented by a circle)
279// Creation of a structure of non-markable elements (target, meridian and
280// parallel).
7fd59977 281
c357e426 282 Pres = theTPres;
283 Handle(V3d_Viewer) TheViewer = theView->Viewer();
c357e426 284 if (!myGraphicStructure.IsNull()) {
285 myGraphicStructure->Disconnect(myGraphicStructure1);
286 myGraphicStructure->Clear();
287 myGraphicStructure1->Clear();
288 if (Pres == V3d_SAMELAST) Pres = myTypeOfRepresentation;
7fd59977 289 }
290 else {
291 if (Pres == V3d_SAMELAST) Pres = V3d_SIMPLE;
c357e426 292 Handle(Graphic3d_Structure) slight = new Graphic3d_Structure(TheViewer->StructureManager());
293 myGraphicStructure = slight;
294 Handle(Graphic3d_Structure) snopick = new Graphic3d_Structure(TheViewer->StructureManager());
295 myGraphicStructure1 = snopick;
7fd59977 296 }
297
c357e426 298 Handle(Graphic3d_Group) glight = myGraphicStructure->NewGroup();
7fd59977 299 Handle(Graphic3d_Group) gsphere;
b64d84be 300 if (Pres == V3d_COMPLETE
301 || Pres == V3d_PARTIAL)
302 {
c357e426 303 gsphere = myGraphicStructure->NewGroup();
b64d84be 304 }
7fd59977 305
c357e426 306 Handle(Graphic3d_Group) gnopick = myGraphicStructure1->NewGroup();
7fd59977 307
c357e426 308 X0 = myTarget.X();
309 Y0 = myTarget.Y();
310 Z0 = myTarget.Z();
7fd59977 311
81bba717 312//Display of the position of the light.
7fd59977 313
87432b82 314 const Quantity_Color Col1 = this->Color();
7fd59977 315 Handle(Graphic3d_AspectLine3d) Asp1 = new Graphic3d_AspectLine3d();
316 Asp1->SetColor(Col1);
317 glight->SetPrimitivesAspect(Asp1);
c357e426 318 this->Symbol(glight,theView);
7fd59977 319
81bba717 320 // Display of the markable sphere (limit at the circle).
7fd59977 321
322 if (Pres == V3d_COMPLETE || Pres == V3d_PARTIAL) {
323
324 Rayon = this->Radius();
c357e426 325 theView->Proj(VX,VY,VZ);
7fd59977 326 V3d::CircleInPlane(gsphere,X0,Y0,Z0,VX,VY,VZ,Rayon);
327
81bba717 328//Display of the meridian
7fd59977 329
330 Quantity_Color Col2(Quantity_NOC_GREEN);
331 Handle(Graphic3d_AspectLine3d) Asp2 = new Graphic3d_AspectLine3d
332 (Col2,Aspect_TOL_SOLID,1.);
333 gnopick->SetPrimitivesAspect(Asp2);
334
81bba717 335// Definition of the axis of circle
c357e426 336 theView->Up(DXRef,DYRef,DZRef);
7fd59977 337 this->DisplayPosition(X,Y,Z);
338 DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
339 VX = DYRef*DZini - DZRef*DYini;
340 VY = DZRef*DXini - DXRef*DZini;
341 VZ = DXRef*DYini - DYRef*DXini;
342
343 V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
344
81bba717 345// Display of the parallel
7fd59977 346
81bba717 347// Definition of the axis of circle
c357e426 348 theView->Proj(VX,VY,VZ);
349 theView->Up(X1,Y1,Z1);
7fd59977 350 DXRef = VY * Z1 - VZ * Y1;
351 DYRef = VZ * X1 - VX * Z1;
352 DZRef = VX * Y1 - VY * X1;
353 this->DisplayPosition(X,Y,Z);
354 DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
355 VX = DYRef*DZini - DZRef*DYini;
356 VY = DZRef*DXini - DXRef*DZini;
357 VZ = DXRef*DYini - DYRef*DXini;
358
359 V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
360
361 }
362
c357e426 363 myGraphicStructure->Connect(myGraphicStructure1,Graphic3d_TOC_DESCENDANT);
7fd59977 364// cout << "MyGraphicStructure exploration \n" << flush; MyGraphicStructure->Exploration();
c357e426 365 myTypeOfRepresentation = Pres;
366 myGraphicStructure->Display();
7fd59977 367}
368
c357e426 369// =======================================================================
370// function : Direction
371// purpose :
372// =======================================================================
373void V3d_DirectionalLight::Direction (Standard_Real& theVx,
374 Standard_Real& theVy,
375 Standard_Real& theVz) const
376{
377 theVx = myLight.Direction.x();
378 theVy = myLight.Direction.y();
379 theVz = myLight.Direction.z();
7fd59977 380}