0026738: Make Boolean operations safely treating arguments when running with fuzzy...
[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,
63 const Quantity_NameOfColor theColor,
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());
7fd59977 73}
74
c357e426 75// =======================================================================
76// function : V3d_DirectionalLight
77// purpose :
78// =======================================================================
79V3d_DirectionalLight::V3d_DirectionalLight (const Handle(V3d_Viewer)& theViewer,
80 const Standard_Real theXt,
81 const Standard_Real theYt,
82 const Standard_Real theZt,
83 const Standard_Real theXp,
84 const Standard_Real theYp,
85 const Standard_Real theZp,
86 const Quantity_NameOfColor theColor,
87 const Standard_Boolean theIsHeadlight)
88: V3d_PositionLight (theViewer)
89{
90 SetType (V3d_DIRECTIONAL);
91 SetColor (theColor);
92 SetHeadlight (theIsHeadlight);
93 SetTarget (theXt, theYt, theZt);
94 SetPosition (theXp, theYp, theZp);
7fd59977 95}
96
189f85a3 97// =======================================================================
98// function : SetSmoothAngle
99// purpose :
100// =======================================================================
101void V3d_DirectionalLight::SetSmoothAngle (const Standard_Real theValue)
102{
c357e426 103 V3d_BadValue_Raise_if (theValue < 0.0 || theValue > M_PI / 2.0,
104 "Bad value for smoothing angle");
189f85a3 105
c357e426 106 myLight.Smoothness = static_cast<Standard_ShortReal> (theValue);
107}
7fd59977 108
c357e426 109// =======================================================================
110// function : SetDirection
111// purpose :
112// =======================================================================
113void V3d_DirectionalLight::SetDirection (const V3d_TypeOfOrientation theDirection)
114{
115 Graphic3d_Vector aV = V3d::GetProjAxis (theDirection);
116 SetDirection (aV.X(), aV.Y(), aV.Z());
7fd59977 117}
118
c357e426 119// =======================================================================
120// function : SetDirection
121// purpose :
122// =======================================================================
123void V3d_DirectionalLight::SetDirection (const Standard_Real theVx,
124 const Standard_Real theVy,
125 const Standard_Real theVz)
126{
127 V3d_BadValue_Raise_if (Sqrt (theVx * theVx + theVy * theVy + theVz * theVz) <= 0.,
128 "V3d_DirectionalLight::SetDirection, "
129 "null vector" );
7fd59977 130
c357e426 131 Graphic3d_Vector aV (theVx, theVy, theVz);
132 aV.Normalize();
7fd59977 133
c357e426 134 myLight.Direction.x() = static_cast<Standard_ShortReal> (aV.X());
135 myLight.Direction.y() = static_cast<Standard_ShortReal> (aV.Y());
136 myLight.Direction.z() = static_cast<Standard_ShortReal> (aV.Z());
7fd59977 137}
138
c357e426 139// =======================================================================
140// function : SetDisplayPosition
141// purpose :
142// =======================================================================
143void V3d_DirectionalLight::SetDisplayPosition (const Standard_Real theX,
144 const Standard_Real theY,
145 const Standard_Real theZ)
146{
147 myDisplayPosition.SetCoord(theX, theY, theZ);
7fd59977 148
c357e426 149 Standard_Real aXt, aYt, aZt;
150 Target (aXt, aYt, aZt);
7fd59977 151
c357e426 152 Standard_Real aXd = aXt - theX;
153 Standard_Real aYd = aYt - theY;
154 Standard_Real aZd = aZt - theZ;
155 if (!Graphic3d_Vector (aXd, aYd, aZd).LengthZero())
156 {
157 SetDirection (aXd, aYd, aZd);
158 }
7fd59977 159}
160
c357e426 161// =======================================================================
162// function : SetPosition
163// purpose :
164// =======================================================================
165void V3d_DirectionalLight::SetPosition (const Standard_Real theXp,
166 const Standard_Real theYp,
167 const Standard_Real theZp)
168{
169 SetDisplayPosition (theXp, theYp, theZp);
7fd59977 170}
171
c357e426 172// =======================================================================
173// function : Position
174// purpose :
175// =======================================================================
176void V3d_DirectionalLight::Position (Standard_Real& theXp,
177 Standard_Real& theYp,
178 Standard_Real& theZp) const
179{
180 DisplayPosition (theXp, theYp, theZp) ;
7fd59977 181}
182
c357e426 183// =======================================================================
184// function : DisplayPosition
185// purpose :
186// =======================================================================
187void V3d_DirectionalLight::DisplayPosition (Standard_Real& theXp,
188 Standard_Real& theYp,
189 Standard_Real& theZp) const
190{
191 myDisplayPosition.Coord (theXp, theYp, theZp) ;
7fd59977 192}
193
c357e426 194// =======================================================================
195// function : DisplayPosition
196// purpose :
197// =======================================================================
198void V3d_DirectionalLight::Symbol (const Handle(Graphic3d_Group)& theSymbol, const Handle(V3d_View)& theView) const
b8ddfc2f 199{
7fd59977 200 Standard_Real Xi,Yi,Zi,Xf,Yf,Zf,Rayon,PXT,PYT,X,Y,Z,XT,YT,ZT;
201 Standard_Real A,B,C,Dist,Beta,CosBeta,SinBeta,Coef,X1,Y1,Z1;
202 Standard_Real DX,DY,DZ,VX,VY,VZ;
203 Standard_Integer IXP,IYP,j;
204 TColStd_Array2OfReal MatRot(0,2,0,2);
7fd59977 205
c357e426 206 theView->Proj(VX,VY,VZ);
7fd59977 207 this->DisplayPosition(Xi,Yi,Zi);
208 Rayon = this->Radius();
c357e426 209 theView->Project(Xi,Yi,Zi,PXT,PYT);
210 theView->Convert(PXT,PYT,IXP,IYP);
81bba717 211// Coordinated 3d in the plane of projection of the source.
c357e426 212 theView->Convert(IXP,IYP,XT,YT,ZT);
213 theView->Convert(PXT,PYT+Rayon,IXP,IYP);
214 theView->Convert(IXP,IYP,X,Y,Z);
7fd59977 215 X = X+Xi-XT; Y = Y+Yi-YT; Z = Z+Zi-ZT;
216 Dist = Sqrt( Square(X-Xi) + Square(Y-Yi) + Square(Z-Zi) );
81bba717 217// Axis of rotation.
7fd59977 218 A = (X-Xi)/Dist;
219 B = (Y-Yi)/Dist;
220 C = (Z-Zi)/Dist;
221
81bba717 222// A sphere is drawn
c357e426 223 V3d::CircleInPlane(theSymbol,Xi,Yi,Zi,VX,VY,VZ,Rayon/40.);
7fd59977 224 for( j=1 ; j<=3 ; j++ ) {
c6541a0c 225 Beta = j * M_PI / 4.;
7fd59977 226 CosBeta = Cos(Beta);
227 SinBeta = Sin(Beta);
228 Coef = 1. - CosBeta;
229 MatRot(0,0) = A * A + (1. - A * A) * CosBeta;
230 MatRot(0,1) = -C * SinBeta + Coef * A * B;
231 MatRot(0,2) = B * SinBeta + Coef * A * C;
232 MatRot(1,0) = C * SinBeta + Coef * A * B;
233 MatRot(1,1) = B * B + (1. - B * B) * CosBeta;
234 MatRot(1,2) = -A * SinBeta + Coef * B * C;
235 MatRot(2,0) = -B * SinBeta + Coef * A * C;
236 MatRot(2,1) = A * SinBeta + Coef * B * C;
237 MatRot(2,2) = C * C + (1. - C * C) * CosBeta;
238 Xf = Xi * MatRot(0,0) + Yi * MatRot(0,1) + Zi * MatRot(0,2);
239 Yf = Xi * MatRot(1,0) + Yi * MatRot(1,1) + Zi * MatRot(1,2);
240 Zf = Xi * MatRot(2,0) + Yi * MatRot(2,1) + Zi * MatRot(2,2);
81bba717 241// Rotation of the normal
7fd59977 242 X1 = VX * MatRot(0,0) + VY * MatRot(0,1) + VZ * MatRot(0,2);
243 Y1 = VX * MatRot(1,0) + VY * MatRot(1,1) + VZ * MatRot(1,2);
244 Z1 = VX * MatRot(2,0) + VY * MatRot(2,1) + VZ * MatRot(2,2);
245 VX = X1 + Xi - Xf ; VY = Y1 + Yi - Yf ; VZ = Z1 + Zi - Zf;
c357e426 246 V3d::CircleInPlane(theSymbol,Xi,Yi,Zi,VX,VY,VZ,Rayon/40.);
7fd59977 247 }
248
81bba717 249// The arrow is drawn
7fd59977 250 Rayon = this->Radius();
251 this->Direction(DX,DY,DZ);
252 X = Xi + DX*Rayon/10.; Y = Yi + DY*Rayon/10.; Z = Zi + DZ*Rayon/10.;
b8ddfc2f 253
254 Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(2);
255 aPrims->AddVertex(Standard_ShortReal(Xi),Standard_ShortReal(Yi),Standard_ShortReal(Zi));
256 aPrims->AddVertex(Standard_ShortReal(X),Standard_ShortReal(Y),Standard_ShortReal(Z));
c357e426 257 theSymbol->AddPrimitiveArray(aPrims);
b8ddfc2f 258
c357e426 259 V3d::ArrowOfRadius(theSymbol, X, Y, Z, DX, DY, DZ, M_PI / 15., Rayon / 20.);
7fd59977 260}
261
c357e426 262// =======================================================================
263// function : Display
264// purpose :
265// =======================================================================
266void V3d_DirectionalLight::Display (const Handle(V3d_View)& theView,
267 const V3d_TypeOfRepresentation theTPres)
268{
7fd59977 269 Standard_Real X,Y,Z,Rayon;
270 Standard_Real X0,Y0,Z0,VX,VY,VZ;
271 Standard_Real X1,Y1,Z1;
272 Standard_Real DXRef,DYRef,DZRef,DXini,DYini,DZini;
273 Standard_Real R1,G1,B1;
274 V3d_TypeOfRepresentation Pres;
275 V3d_TypeOfUpdate UpdSov;
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();
7fd59977 284 UpdSov = TheViewer->UpdateMode();
285 TheViewer->SetUpdateMode(V3d_WAIT);
c357e426 286 if (!myGraphicStructure.IsNull()) {
287 myGraphicStructure->Disconnect(myGraphicStructure1);
288 myGraphicStructure->Clear();
289 myGraphicStructure1->Clear();
290 if (Pres == V3d_SAMELAST) Pres = myTypeOfRepresentation;
7fd59977 291 }
292 else {
293 if (Pres == V3d_SAMELAST) Pres = V3d_SIMPLE;
c357e426 294 Handle(Graphic3d_Structure) slight = new Graphic3d_Structure(TheViewer->StructureManager());
295 myGraphicStructure = slight;
296 Handle(Graphic3d_Structure) snopick = new Graphic3d_Structure(TheViewer->StructureManager());
297 myGraphicStructure1 = snopick;
7fd59977 298 }
299
c357e426 300 Handle(Graphic3d_Group) glight = myGraphicStructure->NewGroup();
7fd59977 301 Handle(Graphic3d_Group) gsphere;
b64d84be 302 if (Pres == V3d_COMPLETE
303 || Pres == V3d_PARTIAL)
304 {
c357e426 305 gsphere = myGraphicStructure->NewGroup();
b64d84be 306 }
7fd59977 307
c357e426 308 Handle(Graphic3d_Group) gnopick = myGraphicStructure1->NewGroup();
7fd59977 309
c357e426 310 X0 = myTarget.X();
311 Y0 = myTarget.Y();
312 Z0 = myTarget.Z();
7fd59977 313
81bba717 314//Display of the position of the light.
7fd59977 315
7fd59977 316 this->Color(Quantity_TOC_RGB,R1,G1,B1);
317 Quantity_Color Col1(R1,G1,B1,Quantity_TOC_RGB);
318 Handle(Graphic3d_AspectLine3d) Asp1 = new Graphic3d_AspectLine3d();
319 Asp1->SetColor(Col1);
320 glight->SetPrimitivesAspect(Asp1);
c357e426 321 this->Symbol(glight,theView);
7fd59977 322
81bba717 323 // Display of the markable sphere (limit at the circle).
7fd59977 324
325 if (Pres == V3d_COMPLETE || Pres == V3d_PARTIAL) {
326
327 Rayon = this->Radius();
c357e426 328 theView->Proj(VX,VY,VZ);
7fd59977 329 V3d::CircleInPlane(gsphere,X0,Y0,Z0,VX,VY,VZ,Rayon);
330
81bba717 331//Display of the meridian
7fd59977 332
333 Quantity_Color Col2(Quantity_NOC_GREEN);
334 Handle(Graphic3d_AspectLine3d) Asp2 = new Graphic3d_AspectLine3d
335 (Col2,Aspect_TOL_SOLID,1.);
336 gnopick->SetPrimitivesAspect(Asp2);
337
81bba717 338// Definition of the axis of circle
c357e426 339 theView->Up(DXRef,DYRef,DZRef);
7fd59977 340 this->DisplayPosition(X,Y,Z);
341 DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
342 VX = DYRef*DZini - DZRef*DYini;
343 VY = DZRef*DXini - DXRef*DZini;
344 VZ = DXRef*DYini - DYRef*DXini;
345
346 V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
347
81bba717 348// Display of the parallel
7fd59977 349
81bba717 350// Definition of the axis of circle
c357e426 351 theView->Proj(VX,VY,VZ);
352 theView->Up(X1,Y1,Z1);
7fd59977 353 DXRef = VY * Z1 - VZ * Y1;
354 DYRef = VZ * X1 - VX * Z1;
355 DZRef = VX * Y1 - VY * X1;
356 this->DisplayPosition(X,Y,Z);
357 DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
358 VX = DYRef*DZini - DZRef*DYini;
359 VY = DZRef*DXini - DXRef*DZini;
360 VZ = DXRef*DYini - DYRef*DXini;
361
362 V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
363
364 }
365
c357e426 366 myGraphicStructure->Connect(myGraphicStructure1,Graphic3d_TOC_DESCENDANT);
7fd59977 367// cout << "MyGraphicStructure exploration \n" << flush; MyGraphicStructure->Exploration();
c357e426 368 myTypeOfRepresentation = Pres;
369 myGraphicStructure->Display();
7fd59977 370 TheViewer->SetUpdateMode(UpdSov);
371}
372
c357e426 373// =======================================================================
374// function : Direction
375// purpose :
376// =======================================================================
377void V3d_DirectionalLight::Direction (Standard_Real& theVx,
378 Standard_Real& theVy,
379 Standard_Real& theVz) const
380{
381 theVx = myLight.Direction.x();
382 theVy = myLight.Direction.y();
383 theVz = myLight.Direction.z();
7fd59977 384}