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_PositionalLight :
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 <TCollection_AsciiString.hxx>
49 #include <TColStd_Array2OfReal.hxx>
51 #include <V3d_BadValue.hxx>
52 #include <V3d_PositionalLight.hxx>
53 #include <V3d_View.hxx>
54 #include <V3d_Viewer.hxx>
56 IMPLEMENT_STANDARD_RTTIEXT(V3d_PositionalLight,V3d_PositionLight)
58 // =======================================================================
59 // function : V3d_PositionalLight
61 // =======================================================================
62 V3d_PositionalLight::V3d_PositionalLight (const Handle(V3d_Viewer)& theViewer,
63 const Standard_Real theX,
64 const Standard_Real theY,
65 const Standard_Real theZ,
66 const Quantity_NameOfColor theColor,
67 const Standard_Real theConstAttenuation,
68 const Standard_Real theLinearAttenuation)
69 : V3d_PositionLight (theViewer)
71 SetType (V3d_POSITIONAL);
72 SetColor (Quantity_Color (theColor));
73 SetTarget (0., 0., 0.);
74 SetPosition (theX, theY, theZ);
75 SetAttenuation (theConstAttenuation, theLinearAttenuation);
78 // =======================================================================
79 // function : V3d_PositionalLight
81 // =======================================================================
82 V3d_PositionalLight::V3d_PositionalLight (const Handle(V3d_Viewer)& theViewer,
83 const Standard_Real theXt,
84 const Standard_Real theYt,
85 const Standard_Real theZt,
86 const Standard_Real theXp,
87 const Standard_Real theYp,
88 const Standard_Real theZp,
89 const Quantity_NameOfColor theColor,
90 const Standard_Real theConstAttenuation,
91 const Standard_Real theLinearAttenuation)
92 : V3d_PositionLight (theViewer)
94 SetType (V3d_POSITIONAL);
95 SetColor (Quantity_Color (theColor));
96 SetTarget (theXt, theYt, theZt);
97 SetPosition (theXp, theYp, theZp);
98 SetAttenuation (theConstAttenuation, theLinearAttenuation);
101 // =======================================================================
102 // function : SetSmoothRadius
104 // =======================================================================
105 void V3d_PositionalLight::SetSmoothRadius (const Standard_Real theValue)
107 V3d_BadValue_Raise_if (theValue < 0.0,
108 "V3d_PositionalLight::SetSmoothRadius,"
109 "Bad value for smoothing radius");
111 myLight.Smoothness = static_cast<Standard_ShortReal> (theValue);
114 // =======================================================================
115 // function : SetPosition
117 // =======================================================================
118 void V3d_PositionalLight::SetPosition (const Standard_Real theXp,
119 const Standard_Real theYp,
120 const Standard_Real theZp)
122 myLight.Position.x() = theXp;
123 myLight.Position.y() = theYp;
124 myLight.Position.z() = theZp;
127 // =======================================================================
128 // function : SetAttenuation
130 // =======================================================================
131 void V3d_PositionalLight::SetAttenuation (const Standard_Real theConstAttenuation,
132 const Standard_Real theLinearAttenuation)
134 V3d_BadValue_Raise_if (theConstAttenuation < 0.
135 || theConstAttenuation > 1.
136 || theLinearAttenuation < 0.
137 || theLinearAttenuation > 1.,
138 "V3d_PositionalLight::SetAttenuation, bad coefficients");
140 myLight.ChangeConstAttenuation() = static_cast<Standard_ShortReal> (theConstAttenuation);
141 myLight.ChangeLinearAttenuation() = static_cast<Standard_ShortReal> (theLinearAttenuation);
144 // =======================================================================
145 // function : Position
147 // =======================================================================
148 void V3d_PositionalLight::Position (Standard_Real& theX, Standard_Real& theY, Standard_Real& theZ) const
150 theX = myLight.Position.x();
151 theY = myLight.Position.y();
152 theZ = myLight.Position.z();
155 // =======================================================================
156 // function : Attenuation
158 // =======================================================================
159 void V3d_PositionalLight::Attenuation (Standard_Real& theConstAttenuation,
160 Standard_Real& theLinearAttenuation) const
162 theConstAttenuation = myLight.ConstAttenuation();
163 theLinearAttenuation = myLight.LinearAttenuation();
166 // =======================================================================
169 // =======================================================================
170 void V3d_PositionalLight::Symbol (const Handle(Graphic3d_Group)& theSymbol, const Handle(V3d_View)& theView) const
172 Standard_Real Xi,Yi,Zi,Xf,Yf,Zf,Rayon,PXT,PYT,X,Y,Z,XT,YT,ZT;
173 Standard_Real A,B,C,Dist,Beta,CosBeta,SinBeta,Coef,X1,Y1,Z1;
174 Standard_Real VX,VY,VZ;
175 Standard_Integer IXP,IYP,j;
176 TColStd_Array2OfReal MatRot(0,2,0,2);
178 theView->Proj(VX,VY,VZ);
179 this->Position(Xi,Yi,Zi);
180 Rayon = this->Radius();
181 theView->Project(Xi,Yi,Zi,PXT,PYT);
182 theView->Convert(PXT,PYT,IXP,IYP);
183 // 3D Coordinate in the plane of projection of the source.
184 theView->Convert(IXP,IYP,XT,YT,ZT);
185 theView->Convert(PXT,PYT+Rayon,IXP,IYP);
186 theView->Convert(IXP,IYP,X,Y,Z);
187 X = X+Xi-XT; Y = Y+Yi-YT; Z = Z+Zi-ZT;
188 Dist = Sqrt( Square(X-Xi) + Square(Y-Yi) + Square(Z-Zi) );
195 V3d::CircleInPlane(theSymbol,Xi,Yi,Zi,VX,VY,VZ,Rayon/40.);
196 for( j=1 ; j<=3 ; j++ ) {
197 Beta = j * M_PI / 4.;
201 MatRot(0,0) = A * A + (1. - A * A) * CosBeta;
202 MatRot(0,1) = -C * SinBeta + Coef * A * B;
203 MatRot(0,2) = B * SinBeta + Coef * A * C;
204 MatRot(1,0) = C * SinBeta + Coef * A * B;
205 MatRot(1,1) = B * B + (1. - B * B) * CosBeta;
206 MatRot(1,2) = -A * SinBeta + Coef * B * C;
207 MatRot(2,0) = -B * SinBeta + Coef * A * C;
208 MatRot(2,1) = A * SinBeta + Coef * B * C;
209 MatRot(2,2) = C * C + (1. - C * C) * CosBeta;
210 Xf = Xi * MatRot(0,0) + Yi * MatRot(0,1) + Zi * MatRot(0,2);
211 Yf = Xi * MatRot(1,0) + Yi * MatRot(1,1) + Zi * MatRot(1,2);
212 Zf = Xi * MatRot(2,0) + Yi * MatRot(2,1) + Zi * MatRot(2,2);
213 // Rotation of the normal
214 X1 = VX * MatRot(0,0) + VY * MatRot(0,1) + VZ * MatRot(0,2);
215 Y1 = VX * MatRot(1,0) + VY * MatRot(1,1) + VZ * MatRot(1,2);
216 Z1 = VX * MatRot(2,0) + VY * MatRot(2,1) + VZ * MatRot(2,2);
217 VX = X1 + Xi - Xf ; VY = Y1 + Yi - Yf ; VZ = Z1 + Zi - Zf;
218 V3d::CircleInPlane(theSymbol,Xi,Yi,Zi,VX,VY,VZ,Rayon/40.);
222 // =======================================================================
223 // function : Display
225 // =======================================================================
226 void V3d_PositionalLight::Display (const Handle(V3d_View)& theView,
227 const V3d_TypeOfRepresentation theRepresentation)
229 Graphic3d_Vertex PText ;
230 Standard_Real X,Y,Z,Rayon;
231 Standard_Real X0,Y0,Z0,VX,VY,VZ;
232 Standard_Real X1,Y1,Z1;
233 Standard_Real DXRef,DYRef,DZRef,DXini,DYini,DZini;
234 Standard_Real R1,G1,B1;
235 V3d_TypeOfRepresentation Pres;
237 // Creation of a structure slight of markable elements (position of the
238 // light, and the domain of lighting represented by a circle)
239 // Creation of a structure snopick of non-markable elements (target, meridian and
242 Pres = theRepresentation;
243 Handle(V3d_Viewer) TheViewer = theView->Viewer();
244 if (!myGraphicStructure.IsNull()) {
245 myGraphicStructure->Disconnect(myGraphicStructure1);
246 myGraphicStructure->Clear();
247 myGraphicStructure1->Clear();
248 if (Pres == V3d_SAMELAST) Pres = myTypeOfRepresentation;
251 if (Pres == V3d_SAMELAST) Pres = V3d_SIMPLE;
252 Handle(Graphic3d_Structure) slight = new Graphic3d_Structure(TheViewer->StructureManager());
253 myGraphicStructure = slight;
254 Handle(Graphic3d_Structure) snopick = new Graphic3d_Structure(TheViewer->StructureManager());
255 myGraphicStructure1 = snopick;
258 Handle(Graphic3d_Group) gradius, gExtArrow, gIntArrow;
259 if (Pres == V3d_COMPLETE)
261 gradius = myGraphicStructure->NewGroup();
262 gExtArrow = myGraphicStructure->NewGroup();
263 gIntArrow = myGraphicStructure->NewGroup();
265 Handle(Graphic3d_Group) glight = myGraphicStructure->NewGroup();
266 Handle(Graphic3d_Group) gsphere;
267 if (Pres == V3d_COMPLETE
268 || Pres == V3d_PARTIAL)
270 gsphere = myGraphicStructure->NewGroup();
273 Handle(Graphic3d_Group) gnopick = myGraphicStructure1->NewGroup();
279 // Display of the position of the light.
281 this->Color(Quantity_TOC_RGB,R1,G1,B1);
282 Quantity_Color Col1(R1,G1,B1,Quantity_TOC_RGB);
283 Handle(Graphic3d_AspectLine3d) Asp1 = new Graphic3d_AspectLine3d();
284 Asp1->SetColor(Col1);
285 glight->SetPrimitivesAspect(Asp1);
286 this->Symbol(glight,theView);
288 // Display of the markable sphere (limit at the cercle).
290 if (Pres == V3d_COMPLETE || Pres == V3d_PARTIAL) {
292 Rayon = this->Radius();
293 theView->Proj(VX,VY,VZ);
294 V3d::CircleInPlane(gsphere,X0,Y0,Z0,VX,VY,VZ,Rayon);
296 // Display of the radius of the sphere (line + text)
298 if (Pres == V3d_COMPLETE) {
299 this->Position(X,Y,Z);
300 Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(2);
301 aPrims->AddVertex(X0,Y0,Z0);
302 aPrims->AddVertex(X,Y,Z);
303 gnopick->AddPrimitiveArray(aPrims);
304 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.);
305 V3d::ArrowOfRadius(gIntArrow, X0, Y0, Z0, X0-X, Y0-Y, Z0-Z, M_PI / 15., Rayon / 20.);
306 TCollection_AsciiString ValOfRadius(Rayon);
307 PText.SetCoord( 0.5*(X0+X), 0.5*(Y0+Y), 0.5*(Z0+Z) );
308 gradius->Text(ValOfRadius.ToCString(),PText,0.01);
311 // Display of the meridian
313 Quantity_Color Col2(Quantity_NOC_GREEN);
314 Handle(Graphic3d_AspectLine3d) Asp2 = new Graphic3d_AspectLine3d(Col2,Aspect_TOL_SOLID,1.);
315 gnopick->SetPrimitivesAspect(Asp2);
317 // Definition of the axis of circle
318 theView->Up(DXRef,DYRef,DZRef);
319 this->Position(X,Y,Z);
320 DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
321 VX = DYRef*DZini - DZRef*DYini;
322 VY = DZRef*DXini - DXRef*DZini;
323 VZ = DXRef*DYini - DYRef*DXini;
325 V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
327 // Display of the parallel
329 // Definition of the axis of circle
330 theView->Proj(VX,VY,VZ);
331 theView->Up(X1,Y1,Z1);
332 DXRef = VY * Z1 - VZ * Y1;
333 DYRef = VZ * X1 - VX * Z1;
334 DZRef = VX * Y1 - VY * X1;
335 this->Position(X,Y,Z);
336 DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
337 VX = DYRef*DZini - DZRef*DYini;
338 VY = DZRef*DXini - DXRef*DZini;
339 VZ = DXRef*DYini - DYRef*DXini;
341 V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
344 myGraphicStructure->Connect(myGraphicStructure1,Graphic3d_TOC_DESCENDANT);
345 myTypeOfRepresentation = Pres;
346 myGraphicStructure->Display();