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_PositionalLight : |
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 <TCollection_AsciiString.hxx> |
49 | #include <TColStd_Array2OfReal.hxx> |
50 | #include <V3d.hxx> |
51 | #include <V3d_BadValue.hxx> |
52 | #include <V3d_PositionalLight.hxx> |
53 | #include <V3d_View.hxx> |
54 | #include <V3d_Viewer.hxx> |
7fd59977 |
55 | |
92efcf78 |
56 | IMPLEMENT_STANDARD_RTTIEXT(V3d_PositionalLight,V3d_PositionLight) |
57 | |
c357e426 |
58 | // ======================================================================= |
59 | // function : V3d_PositionalLight |
60 | // purpose : |
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, |
87432b82 |
66 | const Quantity_Color& theColor, |
c357e426 |
67 | const Standard_Real theConstAttenuation, |
68 | const Standard_Real theLinearAttenuation) |
69 | : V3d_PositionLight (theViewer) |
70 | { |
71 | SetType (V3d_POSITIONAL); |
87432b82 |
72 | SetColor (theColor); |
c357e426 |
73 | SetTarget (0., 0., 0.); |
74 | SetPosition (theX, theY, theZ); |
75 | SetAttenuation (theConstAttenuation, theLinearAttenuation); |
7fd59977 |
76 | } |
77 | |
c357e426 |
78 | // ======================================================================= |
79 | // function : V3d_PositionalLight |
80 | // purpose : |
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, |
87432b82 |
89 | const Quantity_Color& theColor, |
c357e426 |
90 | const Standard_Real theConstAttenuation, |
91 | const Standard_Real theLinearAttenuation) |
92 | : V3d_PositionLight (theViewer) |
93 | { |
94 | SetType (V3d_POSITIONAL); |
87432b82 |
95 | SetColor (theColor); |
c357e426 |
96 | SetTarget (theXt, theYt, theZt); |
97 | SetPosition (theXp, theYp, theZp); |
98 | SetAttenuation (theConstAttenuation, theLinearAttenuation); |
7fd59977 |
99 | } |
100 | |
189f85a3 |
101 | // ======================================================================= |
102 | // function : SetSmoothRadius |
103 | // purpose : |
104 | // ======================================================================= |
105 | void V3d_PositionalLight::SetSmoothRadius (const Standard_Real theValue) |
106 | { |
c357e426 |
107 | V3d_BadValue_Raise_if (theValue < 0.0, |
108 | "V3d_PositionalLight::SetSmoothRadius," |
109 | "Bad value for smoothing radius"); |
7fd59977 |
110 | |
c357e426 |
111 | myLight.Smoothness = static_cast<Standard_ShortReal> (theValue); |
7fd59977 |
112 | } |
113 | |
c357e426 |
114 | // ======================================================================= |
115 | // function : SetPosition |
116 | // purpose : |
117 | // ======================================================================= |
118 | void V3d_PositionalLight::SetPosition (const Standard_Real theXp, |
119 | const Standard_Real theYp, |
120 | const Standard_Real theZp) |
121 | { |
7c3ef2f7 |
122 | myLight.Position.x() = theXp; |
123 | myLight.Position.y() = theYp; |
124 | myLight.Position.z() = theZp; |
7fd59977 |
125 | } |
126 | |
c357e426 |
127 | // ======================================================================= |
128 | // function : SetAttenuation |
129 | // purpose : |
130 | // ======================================================================= |
131 | void V3d_PositionalLight::SetAttenuation (const Standard_Real theConstAttenuation, |
132 | const Standard_Real theLinearAttenuation) |
133 | { |
134 | V3d_BadValue_Raise_if (theConstAttenuation < 0. |
135 | || theConstAttenuation > 1. |
136 | || theLinearAttenuation < 0. |
137 | || theLinearAttenuation > 1., |
138 | "V3d_PositionalLight::SetAttenuation, bad coefficients"); |
139 | |
140 | myLight.ChangeConstAttenuation() = static_cast<Standard_ShortReal> (theConstAttenuation); |
141 | myLight.ChangeLinearAttenuation() = static_cast<Standard_ShortReal> (theLinearAttenuation); |
7fd59977 |
142 | } |
143 | |
c357e426 |
144 | // ======================================================================= |
145 | // function : Position |
146 | // purpose : |
147 | // ======================================================================= |
148 | void V3d_PositionalLight::Position (Standard_Real& theX, Standard_Real& theY, Standard_Real& theZ) const |
149 | { |
150 | theX = myLight.Position.x(); |
151 | theY = myLight.Position.y(); |
152 | theZ = myLight.Position.z(); |
7fd59977 |
153 | } |
154 | |
c357e426 |
155 | // ======================================================================= |
156 | // function : Attenuation |
157 | // purpose : |
158 | // ======================================================================= |
159 | void V3d_PositionalLight::Attenuation (Standard_Real& theConstAttenuation, |
160 | Standard_Real& theLinearAttenuation) const |
161 | { |
162 | theConstAttenuation = myLight.ConstAttenuation(); |
163 | theLinearAttenuation = myLight.LinearAttenuation(); |
164 | } |
7fd59977 |
165 | |
c357e426 |
166 | // ======================================================================= |
167 | // function : Symbol |
168 | // purpose : |
169 | // ======================================================================= |
170 | void V3d_PositionalLight::Symbol (const Handle(Graphic3d_Group)& theSymbol, const Handle(V3d_View)& theView) const |
171 | { |
7fd59977 |
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); |
177 | |
c357e426 |
178 | theView->Proj(VX,VY,VZ); |
7fd59977 |
179 | this->Position(Xi,Yi,Zi); |
180 | Rayon = this->Radius(); |
c357e426 |
181 | theView->Project(Xi,Yi,Zi,PXT,PYT); |
182 | theView->Convert(PXT,PYT,IXP,IYP); |
81bba717 |
183 | // 3D Coordinate in the plane of projection of the source. |
c357e426 |
184 | theView->Convert(IXP,IYP,XT,YT,ZT); |
185 | theView->Convert(PXT,PYT+Rayon,IXP,IYP); |
186 | theView->Convert(IXP,IYP,X,Y,Z); |
7fd59977 |
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) ); |
81bba717 |
189 | // Axis of rotation. |
7fd59977 |
190 | A = (X-Xi)/Dist; |
191 | B = (Y-Yi)/Dist; |
192 | C = (Z-Zi)/Dist; |
193 | |
81bba717 |
194 | // A sphere is drawn |
c357e426 |
195 | V3d::CircleInPlane(theSymbol,Xi,Yi,Zi,VX,VY,VZ,Rayon/40.); |
7fd59977 |
196 | for( j=1 ; j<=3 ; j++ ) { |
c6541a0c |
197 | Beta = j * M_PI / 4.; |
7fd59977 |
198 | CosBeta = Cos(Beta); |
199 | SinBeta = Sin(Beta); |
200 | Coef = 1. - CosBeta; |
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); |
81bba717 |
213 | // Rotation of the normal |
7fd59977 |
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; |
c357e426 |
218 | V3d::CircleInPlane(theSymbol,Xi,Yi,Zi,VX,VY,VZ,Rayon/40.); |
7fd59977 |
219 | } |
220 | } |
221 | |
c357e426 |
222 | // ======================================================================= |
223 | // function : Display |
224 | // purpose : |
225 | // ======================================================================= |
226 | void V3d_PositionalLight::Display (const Handle(V3d_View)& theView, |
227 | const V3d_TypeOfRepresentation theRepresentation) |
b8ddfc2f |
228 | { |
7fd59977 |
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; |
7fd59977 |
234 | V3d_TypeOfRepresentation Pres; |
7fd59977 |
235 | |
81bba717 |
236 | // Creation of a structure slight of markable elements (position of the |
237 | // light, and the domain of lighting represented by a circle) |
238 | // Creation of a structure snopick of non-markable elements (target, meridian and |
239 | // parallel). |
7fd59977 |
240 | |
c357e426 |
241 | Pres = theRepresentation; |
242 | Handle(V3d_Viewer) TheViewer = theView->Viewer(); |
c357e426 |
243 | if (!myGraphicStructure.IsNull()) { |
244 | myGraphicStructure->Disconnect(myGraphicStructure1); |
245 | myGraphicStructure->Clear(); |
246 | myGraphicStructure1->Clear(); |
247 | if (Pres == V3d_SAMELAST) Pres = myTypeOfRepresentation; |
b8ddfc2f |
248 | } |
249 | else { |
250 | if (Pres == V3d_SAMELAST) Pres = V3d_SIMPLE; |
c357e426 |
251 | Handle(Graphic3d_Structure) slight = new Graphic3d_Structure(TheViewer->StructureManager()); |
252 | myGraphicStructure = slight; |
253 | Handle(Graphic3d_Structure) snopick = new Graphic3d_Structure(TheViewer->StructureManager()); |
254 | myGraphicStructure1 = snopick; |
b8ddfc2f |
255 | } |
256 | |
b64d84be |
257 | Handle(Graphic3d_Group) gradius, gExtArrow, gIntArrow; |
258 | if (Pres == V3d_COMPLETE) |
259 | { |
c357e426 |
260 | gradius = myGraphicStructure->NewGroup(); |
261 | gExtArrow = myGraphicStructure->NewGroup(); |
262 | gIntArrow = myGraphicStructure->NewGroup(); |
7fd59977 |
263 | } |
c357e426 |
264 | Handle(Graphic3d_Group) glight = myGraphicStructure->NewGroup(); |
7fd59977 |
265 | Handle(Graphic3d_Group) gsphere; |
b64d84be |
266 | if (Pres == V3d_COMPLETE |
267 | || Pres == V3d_PARTIAL) |
268 | { |
c357e426 |
269 | gsphere = myGraphicStructure->NewGroup(); |
b64d84be |
270 | } |
7fd59977 |
271 | |
c357e426 |
272 | Handle(Graphic3d_Group) gnopick = myGraphicStructure1->NewGroup(); |
7fd59977 |
273 | |
c357e426 |
274 | X0 = myTarget.X(); |
275 | Y0 = myTarget.Y(); |
276 | Z0 = myTarget.Z(); |
7fd59977 |
277 | |
81bba717 |
278 | // Display of the position of the light. |
7fd59977 |
279 | |
87432b82 |
280 | const Quantity_Color Col1 = this->Color(); |
7fd59977 |
281 | Handle(Graphic3d_AspectLine3d) Asp1 = new Graphic3d_AspectLine3d(); |
282 | Asp1->SetColor(Col1); |
283 | glight->SetPrimitivesAspect(Asp1); |
c357e426 |
284 | this->Symbol(glight,theView); |
7fd59977 |
285 | |
81bba717 |
286 | // Display of the markable sphere (limit at the cercle). |
7fd59977 |
287 | |
288 | if (Pres == V3d_COMPLETE || Pres == V3d_PARTIAL) { |
289 | |
290 | Rayon = this->Radius(); |
c357e426 |
291 | theView->Proj(VX,VY,VZ); |
7fd59977 |
292 | V3d::CircleInPlane(gsphere,X0,Y0,Z0,VX,VY,VZ,Rayon); |
293 | |
81bba717 |
294 | // Display of the radius of the sphere (line + text) |
7fd59977 |
295 | |
296 | if (Pres == V3d_COMPLETE) { |
7fd59977 |
297 | this->Position(X,Y,Z); |
b8ddfc2f |
298 | Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(2); |
299 | aPrims->AddVertex(X0,Y0,Z0); |
300 | aPrims->AddVertex(X,Y,Z); |
301 | gnopick->AddPrimitiveArray(aPrims); |
302 | 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.); |
c6541a0c |
303 | V3d::ArrowOfRadius(gIntArrow, X0, Y0, Z0, X0-X, Y0-Y, Z0-Z, M_PI / 15., Rayon / 20.); |
7fd59977 |
304 | TCollection_AsciiString ValOfRadius(Rayon); |
b8ddfc2f |
305 | PText.SetCoord( 0.5*(X0+X), 0.5*(Y0+Y), 0.5*(Z0+Z) ); |
7fd59977 |
306 | gradius->Text(ValOfRadius.ToCString(),PText,0.01); |
307 | } |
308 | |
81bba717 |
309 | // Display of the meridian |
7fd59977 |
310 | |
311 | Quantity_Color Col2(Quantity_NOC_GREEN); |
b8ddfc2f |
312 | Handle(Graphic3d_AspectLine3d) Asp2 = new Graphic3d_AspectLine3d(Col2,Aspect_TOL_SOLID,1.); |
7fd59977 |
313 | gnopick->SetPrimitivesAspect(Asp2); |
314 | |
81bba717 |
315 | // Definition of the axis of circle |
c357e426 |
316 | theView->Up(DXRef,DYRef,DZRef); |
7fd59977 |
317 | this->Position(X,Y,Z); |
318 | DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0; |
319 | VX = DYRef*DZini - DZRef*DYini; |
320 | VY = DZRef*DXini - DXRef*DZini; |
321 | VZ = DXRef*DYini - DYRef*DXini; |
322 | |
323 | V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon); |
324 | |
81bba717 |
325 | // Display of the parallel |
7fd59977 |
326 | |
81bba717 |
327 | // Definition of the axis of circle |
c357e426 |
328 | theView->Proj(VX,VY,VZ); |
329 | theView->Up(X1,Y1,Z1); |
7fd59977 |
330 | DXRef = VY * Z1 - VZ * Y1; |
331 | DYRef = VZ * X1 - VX * Z1; |
332 | DZRef = VX * Y1 - VY * X1; |
333 | this->Position(X,Y,Z); |
334 | DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0; |
335 | VX = DYRef*DZini - DZRef*DYini; |
336 | VY = DZRef*DXini - DXRef*DZini; |
337 | VZ = DXRef*DYini - DYRef*DXini; |
338 | |
339 | V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon); |
7fd59977 |
340 | } |
341 | |
c357e426 |
342 | myGraphicStructure->Connect(myGraphicStructure1,Graphic3d_TOC_DESCENDANT); |
343 | myTypeOfRepresentation = Pres; |
344 | myGraphicStructure->Display(); |
7fd59977 |
345 | } |