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_DirectionalLight :
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)
27 ************************************************************************/
28 /*----------------------------------------------------------------------*/
33 #include <Aspect_Window.hxx>
37 #include <gp_Trsf.hxx>
39 #include <Graphic3d_ArrayOfSegments.hxx>
40 #include <Graphic3d_AspectLine3d.hxx>
41 #include <Graphic3d_AspectMarker3d.hxx>
42 #include <Graphic3d_AspectText3d.hxx>
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>
50 #include <V3d_BadValue.hxx>
51 #include <V3d_DirectionalLight.hxx>
52 #include <V3d_View.hxx>
53 #include <V3d_Viewer.hxx>
55 IMPLEMENT_STANDARD_RTTIEXT(V3d_DirectionalLight,V3d_PositionLight)
57 // =======================================================================
58 // function : V3d_DirectionalLight
60 // =======================================================================
61 V3d_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)
67 Graphic3d_Vector aV = V3d::GetProjAxis (theDirection);
68 SetType (V3d_DIRECTIONAL);
70 SetHeadlight (theIsHeadlight);
71 SetTarget (0., 0., 0.);
72 SetPosition (-aV.X(), -aV.Y(), -aV.Z());
77 // =======================================================================
78 // function : V3d_DirectionalLight
80 // =======================================================================
81 V3d_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,
88 const Quantity_NameOfColor theColor,
89 const Standard_Boolean theIsHeadlight)
90 : V3d_PositionLight (theViewer)
92 SetType (V3d_DIRECTIONAL);
94 SetHeadlight (theIsHeadlight);
95 SetTarget (theXt, theYt, theZt);
96 SetPosition (theXp, theYp, theZp);
99 // =======================================================================
100 // function : SetSmoothAngle
102 // =======================================================================
103 void V3d_DirectionalLight::SetSmoothAngle (const Standard_Real theValue)
105 V3d_BadValue_Raise_if (theValue < 0.0 || theValue > M_PI / 2.0,
106 "Bad value for smoothing angle");
108 myLight.Smoothness = static_cast<Standard_ShortReal> (theValue);
111 // =======================================================================
112 // function : SetDirection
114 // =======================================================================
115 void V3d_DirectionalLight::SetDirection (const V3d_TypeOfOrientation theDirection)
117 Graphic3d_Vector aV = V3d::GetProjAxis (theDirection);
118 SetDirection (aV.X(), aV.Y(), aV.Z());
121 // =======================================================================
122 // function : SetDirection
124 // =======================================================================
125 void V3d_DirectionalLight::SetDirection (const Standard_Real theVx,
126 const Standard_Real theVy,
127 const Standard_Real theVz)
129 V3d_BadValue_Raise_if (Sqrt (theVx * theVx + theVy * theVy + theVz * theVz) <= 0.,
130 "V3d_DirectionalLight::SetDirection, "
133 Graphic3d_Vector aV (theVx, theVy, theVz);
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());
141 // =======================================================================
142 // function : SetDisplayPosition
144 // =======================================================================
145 void V3d_DirectionalLight::SetDisplayPosition (const Standard_Real theX,
146 const Standard_Real theY,
147 const Standard_Real theZ)
149 myDisplayPosition.SetCoord(theX, theY, theZ);
151 Standard_Real aXt, aYt, aZt;
152 Target (aXt, aYt, aZt);
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())
159 SetDirection (aXd, aYd, aZd);
163 // =======================================================================
164 // function : SetPosition
166 // =======================================================================
167 void V3d_DirectionalLight::SetPosition (const Standard_Real theXp,
168 const Standard_Real theYp,
169 const Standard_Real theZp)
171 SetDisplayPosition (theXp, theYp, theZp);
174 // =======================================================================
175 // function : Position
177 // =======================================================================
178 void V3d_DirectionalLight::Position (Standard_Real& theXp,
179 Standard_Real& theYp,
180 Standard_Real& theZp) const
182 DisplayPosition (theXp, theYp, theZp) ;
185 // =======================================================================
186 // function : DisplayPosition
188 // =======================================================================
189 void V3d_DirectionalLight::DisplayPosition (Standard_Real& theXp,
190 Standard_Real& theYp,
191 Standard_Real& theZp) const
193 myDisplayPosition.Coord (theXp, theYp, theZp) ;
196 // =======================================================================
197 // function : DisplayPosition
199 // =======================================================================
200 void V3d_DirectionalLight::Symbol (const Handle(Graphic3d_Group)& theSymbol, const Handle(V3d_View)& theView) const
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);
208 theView->Proj(VX,VY,VZ);
209 this->DisplayPosition(Xi,Yi,Zi);
210 Rayon = this->Radius();
211 theView->Project(Xi,Yi,Zi,PXT,PYT);
212 theView->Convert(PXT,PYT,IXP,IYP);
213 // Coordinated 3d in the plane of projection of the source.
214 theView->Convert(IXP,IYP,XT,YT,ZT);
215 theView->Convert(PXT,PYT+Rayon,IXP,IYP);
216 theView->Convert(IXP,IYP,X,Y,Z);
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) );
225 V3d::CircleInPlane(theSymbol,Xi,Yi,Zi,VX,VY,VZ,Rayon/40.);
226 for( j=1 ; j<=3 ; j++ ) {
227 Beta = j * M_PI / 4.;
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);
243 // Rotation of the normal
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;
248 V3d::CircleInPlane(theSymbol,Xi,Yi,Zi,VX,VY,VZ,Rayon/40.);
251 // The arrow is drawn
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.;
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));
259 theSymbol->AddPrimitiveArray(aPrims);
261 V3d::ArrowOfRadius(theSymbol, X, Y, Z, DX, DY, DZ, M_PI / 15., Rayon / 20.);
264 // =======================================================================
265 // function : Display
267 // =======================================================================
268 void V3d_DirectionalLight::Display (const Handle(V3d_View)& theView,
269 const V3d_TypeOfRepresentation theTPres)
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;
275 Standard_Real R1,G1,B1;
276 V3d_TypeOfRepresentation Pres;
278 // Creation of a structure of markable elements (position of the
279 // light, and the domain of lighting represented by a circle)
280 // Creation of a structure of non-markable elements (target, meridian and
284 Handle(V3d_Viewer) TheViewer = theView->Viewer();
285 if (!myGraphicStructure.IsNull()) {
286 myGraphicStructure->Disconnect(myGraphicStructure1);
287 myGraphicStructure->Clear();
288 myGraphicStructure1->Clear();
289 if (Pres == V3d_SAMELAST) Pres = myTypeOfRepresentation;
292 if (Pres == V3d_SAMELAST) Pres = V3d_SIMPLE;
293 Handle(Graphic3d_Structure) slight = new Graphic3d_Structure(TheViewer->StructureManager());
294 myGraphicStructure = slight;
295 Handle(Graphic3d_Structure) snopick = new Graphic3d_Structure(TheViewer->StructureManager());
296 myGraphicStructure1 = snopick;
299 Handle(Graphic3d_Group) glight = myGraphicStructure->NewGroup();
300 Handle(Graphic3d_Group) gsphere;
301 if (Pres == V3d_COMPLETE
302 || Pres == V3d_PARTIAL)
304 gsphere = myGraphicStructure->NewGroup();
307 Handle(Graphic3d_Group) gnopick = myGraphicStructure1->NewGroup();
313 //Display of the position of the light.
315 this->Color(Quantity_TOC_RGB,R1,G1,B1);
316 Quantity_Color Col1(R1,G1,B1,Quantity_TOC_RGB);
317 Handle(Graphic3d_AspectLine3d) Asp1 = new Graphic3d_AspectLine3d();
318 Asp1->SetColor(Col1);
319 glight->SetPrimitivesAspect(Asp1);
320 this->Symbol(glight,theView);
322 // Display of the markable sphere (limit at the circle).
324 if (Pres == V3d_COMPLETE || Pres == V3d_PARTIAL) {
326 Rayon = this->Radius();
327 theView->Proj(VX,VY,VZ);
328 V3d::CircleInPlane(gsphere,X0,Y0,Z0,VX,VY,VZ,Rayon);
330 //Display of the meridian
332 Quantity_Color Col2(Quantity_NOC_GREEN);
333 Handle(Graphic3d_AspectLine3d) Asp2 = new Graphic3d_AspectLine3d
334 (Col2,Aspect_TOL_SOLID,1.);
335 gnopick->SetPrimitivesAspect(Asp2);
337 // Definition of the axis of circle
338 theView->Up(DXRef,DYRef,DZRef);
339 this->DisplayPosition(X,Y,Z);
340 DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
341 VX = DYRef*DZini - DZRef*DYini;
342 VY = DZRef*DXini - DXRef*DZini;
343 VZ = DXRef*DYini - DYRef*DXini;
345 V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
347 // Display of the parallel
349 // Definition of the axis of circle
350 theView->Proj(VX,VY,VZ);
351 theView->Up(X1,Y1,Z1);
352 DXRef = VY * Z1 - VZ * Y1;
353 DYRef = VZ * X1 - VX * Z1;
354 DZRef = VX * Y1 - VY * X1;
355 this->DisplayPosition(X,Y,Z);
356 DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
357 VX = DYRef*DZini - DZRef*DYini;
358 VY = DZRef*DXini - DXRef*DZini;
359 VZ = DXRef*DYini - DYRef*DXini;
361 V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
365 myGraphicStructure->Connect(myGraphicStructure1,Graphic3d_TOC_DESCENDANT);
366 // cout << "MyGraphicStructure exploration \n" << flush; MyGraphicStructure->Exploration();
367 myTypeOfRepresentation = Pres;
368 myGraphicStructure->Display();
371 // =======================================================================
372 // function : Direction
374 // =======================================================================
375 void V3d_DirectionalLight::Direction (Standard_Real& theVx,
376 Standard_Real& theVy,
377 Standard_Real& theVz) const
379 theVx = myLight.Direction.x();
380 theVy = myLight.Direction.y();
381 theVz = myLight.Direction.z();