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 | |
21b2385f |
14 | #include <V3d_PositionalLight.hxx> |
7fd59977 |
15 | |
b8ddfc2f |
16 | #include <Graphic3d_ArrayOfSegments.hxx> |
7fd59977 |
17 | #include <Graphic3d_AspectLine3d.hxx> |
42cf5bc1 |
18 | #include <Graphic3d_Group.hxx> |
19 | #include <Graphic3d_Structure.hxx> |
42cf5bc1 |
20 | #include <TCollection_AsciiString.hxx> |
21 | #include <TColStd_Array2OfReal.hxx> |
22 | #include <V3d.hxx> |
23 | #include <V3d_BadValue.hxx> |
42cf5bc1 |
24 | #include <V3d_View.hxx> |
25 | #include <V3d_Viewer.hxx> |
7fd59977 |
26 | |
92efcf78 |
27 | IMPLEMENT_STANDARD_RTTIEXT(V3d_PositionalLight,V3d_PositionLight) |
28 | |
c357e426 |
29 | // ======================================================================= |
30 | // function : V3d_PositionalLight |
31 | // purpose : |
32 | // ======================================================================= |
33 | V3d_PositionalLight::V3d_PositionalLight (const Handle(V3d_Viewer)& theViewer, |
34 | const Standard_Real theX, |
35 | const Standard_Real theY, |
36 | const Standard_Real theZ, |
87432b82 |
37 | const Quantity_Color& theColor, |
c357e426 |
38 | const Standard_Real theConstAttenuation, |
39 | const Standard_Real theLinearAttenuation) |
40 | : V3d_PositionLight (theViewer) |
41 | { |
42 | SetType (V3d_POSITIONAL); |
87432b82 |
43 | SetColor (theColor); |
c357e426 |
44 | SetTarget (0., 0., 0.); |
45 | SetPosition (theX, theY, theZ); |
46 | SetAttenuation (theConstAttenuation, theLinearAttenuation); |
7fd59977 |
47 | } |
48 | |
c357e426 |
49 | // ======================================================================= |
50 | // function : V3d_PositionalLight |
51 | // purpose : |
52 | // ======================================================================= |
53 | V3d_PositionalLight::V3d_PositionalLight (const Handle(V3d_Viewer)& theViewer, |
54 | const Standard_Real theXt, |
55 | const Standard_Real theYt, |
56 | const Standard_Real theZt, |
57 | const Standard_Real theXp, |
58 | const Standard_Real theYp, |
59 | const Standard_Real theZp, |
87432b82 |
60 | const Quantity_Color& theColor, |
c357e426 |
61 | const Standard_Real theConstAttenuation, |
62 | const Standard_Real theLinearAttenuation) |
63 | : V3d_PositionLight (theViewer) |
64 | { |
65 | SetType (V3d_POSITIONAL); |
87432b82 |
66 | SetColor (theColor); |
c357e426 |
67 | SetTarget (theXt, theYt, theZt); |
68 | SetPosition (theXp, theYp, theZp); |
69 | SetAttenuation (theConstAttenuation, theLinearAttenuation); |
7fd59977 |
70 | } |
71 | |
189f85a3 |
72 | // ======================================================================= |
73 | // function : SetSmoothRadius |
74 | // purpose : |
75 | // ======================================================================= |
76 | void V3d_PositionalLight::SetSmoothRadius (const Standard_Real theValue) |
77 | { |
c357e426 |
78 | V3d_BadValue_Raise_if (theValue < 0.0, |
79 | "V3d_PositionalLight::SetSmoothRadius," |
80 | "Bad value for smoothing radius"); |
7fd59977 |
81 | |
c357e426 |
82 | myLight.Smoothness = static_cast<Standard_ShortReal> (theValue); |
7fd59977 |
83 | } |
84 | |
c357e426 |
85 | // ======================================================================= |
86 | // function : SetAttenuation |
87 | // purpose : |
88 | // ======================================================================= |
89 | void V3d_PositionalLight::SetAttenuation (const Standard_Real theConstAttenuation, |
90 | const Standard_Real theLinearAttenuation) |
91 | { |
92 | V3d_BadValue_Raise_if (theConstAttenuation < 0. |
93 | || theConstAttenuation > 1. |
94 | || theLinearAttenuation < 0. |
95 | || theLinearAttenuation > 1., |
96 | "V3d_PositionalLight::SetAttenuation, bad coefficients"); |
97 | |
98 | myLight.ChangeConstAttenuation() = static_cast<Standard_ShortReal> (theConstAttenuation); |
99 | myLight.ChangeLinearAttenuation() = static_cast<Standard_ShortReal> (theLinearAttenuation); |
7fd59977 |
100 | } |
101 | |
c357e426 |
102 | // ======================================================================= |
103 | // function : Symbol |
104 | // purpose : |
105 | // ======================================================================= |
106 | void V3d_PositionalLight::Symbol (const Handle(Graphic3d_Group)& theSymbol, const Handle(V3d_View)& theView) const |
107 | { |
7fd59977 |
108 | Standard_Real Xi,Yi,Zi,Xf,Yf,Zf,Rayon,PXT,PYT,X,Y,Z,XT,YT,ZT; |
109 | Standard_Real A,B,C,Dist,Beta,CosBeta,SinBeta,Coef,X1,Y1,Z1; |
110 | Standard_Real VX,VY,VZ; |
111 | Standard_Integer IXP,IYP,j; |
112 | TColStd_Array2OfReal MatRot(0,2,0,2); |
113 | |
c357e426 |
114 | theView->Proj(VX,VY,VZ); |
7fd59977 |
115 | this->Position(Xi,Yi,Zi); |
116 | Rayon = this->Radius(); |
c357e426 |
117 | theView->Project(Xi,Yi,Zi,PXT,PYT); |
118 | theView->Convert(PXT,PYT,IXP,IYP); |
81bba717 |
119 | // 3D Coordinate in the plane of projection of the source. |
c357e426 |
120 | theView->Convert(IXP,IYP,XT,YT,ZT); |
121 | theView->Convert(PXT,PYT+Rayon,IXP,IYP); |
122 | theView->Convert(IXP,IYP,X,Y,Z); |
7fd59977 |
123 | X = X+Xi-XT; Y = Y+Yi-YT; Z = Z+Zi-ZT; |
124 | Dist = Sqrt( Square(X-Xi) + Square(Y-Yi) + Square(Z-Zi) ); |
81bba717 |
125 | // Axis of rotation. |
7fd59977 |
126 | A = (X-Xi)/Dist; |
127 | B = (Y-Yi)/Dist; |
128 | C = (Z-Zi)/Dist; |
129 | |
81bba717 |
130 | // A sphere is drawn |
c357e426 |
131 | V3d::CircleInPlane(theSymbol,Xi,Yi,Zi,VX,VY,VZ,Rayon/40.); |
7fd59977 |
132 | for( j=1 ; j<=3 ; j++ ) { |
c6541a0c |
133 | Beta = j * M_PI / 4.; |
7fd59977 |
134 | CosBeta = Cos(Beta); |
135 | SinBeta = Sin(Beta); |
136 | Coef = 1. - CosBeta; |
137 | MatRot(0,0) = A * A + (1. - A * A) * CosBeta; |
138 | MatRot(0,1) = -C * SinBeta + Coef * A * B; |
139 | MatRot(0,2) = B * SinBeta + Coef * A * C; |
140 | MatRot(1,0) = C * SinBeta + Coef * A * B; |
141 | MatRot(1,1) = B * B + (1. - B * B) * CosBeta; |
142 | MatRot(1,2) = -A * SinBeta + Coef * B * C; |
143 | MatRot(2,0) = -B * SinBeta + Coef * A * C; |
144 | MatRot(2,1) = A * SinBeta + Coef * B * C; |
145 | MatRot(2,2) = C * C + (1. - C * C) * CosBeta; |
146 | Xf = Xi * MatRot(0,0) + Yi * MatRot(0,1) + Zi * MatRot(0,2); |
147 | Yf = Xi * MatRot(1,0) + Yi * MatRot(1,1) + Zi * MatRot(1,2); |
148 | Zf = Xi * MatRot(2,0) + Yi * MatRot(2,1) + Zi * MatRot(2,2); |
81bba717 |
149 | // Rotation of the normal |
7fd59977 |
150 | X1 = VX * MatRot(0,0) + VY * MatRot(0,1) + VZ * MatRot(0,2); |
151 | Y1 = VX * MatRot(1,0) + VY * MatRot(1,1) + VZ * MatRot(1,2); |
152 | Z1 = VX * MatRot(2,0) + VY * MatRot(2,1) + VZ * MatRot(2,2); |
153 | VX = X1 + Xi - Xf ; VY = Y1 + Yi - Yf ; VZ = Z1 + Zi - Zf; |
c357e426 |
154 | V3d::CircleInPlane(theSymbol,Xi,Yi,Zi,VX,VY,VZ,Rayon/40.); |
7fd59977 |
155 | } |
156 | } |
157 | |
c357e426 |
158 | // ======================================================================= |
159 | // function : Display |
160 | // purpose : |
161 | // ======================================================================= |
162 | void V3d_PositionalLight::Display (const Handle(V3d_View)& theView, |
163 | const V3d_TypeOfRepresentation theRepresentation) |
b8ddfc2f |
164 | { |
7fd59977 |
165 | Standard_Real X,Y,Z,Rayon; |
166 | Standard_Real X0,Y0,Z0,VX,VY,VZ; |
167 | Standard_Real X1,Y1,Z1; |
168 | Standard_Real DXRef,DYRef,DZRef,DXini,DYini,DZini; |
7fd59977 |
169 | V3d_TypeOfRepresentation Pres; |
7fd59977 |
170 | |
81bba717 |
171 | // Creation of a structure slight of markable elements (position of the |
172 | // light, and the domain of lighting represented by a circle) |
173 | // Creation of a structure snopick of non-markable elements (target, meridian and |
174 | // parallel). |
7fd59977 |
175 | |
c357e426 |
176 | Pres = theRepresentation; |
177 | Handle(V3d_Viewer) TheViewer = theView->Viewer(); |
c357e426 |
178 | if (!myGraphicStructure.IsNull()) { |
179 | myGraphicStructure->Disconnect(myGraphicStructure1); |
180 | myGraphicStructure->Clear(); |
181 | myGraphicStructure1->Clear(); |
182 | if (Pres == V3d_SAMELAST) Pres = myTypeOfRepresentation; |
b8ddfc2f |
183 | } |
184 | else { |
185 | if (Pres == V3d_SAMELAST) Pres = V3d_SIMPLE; |
c357e426 |
186 | Handle(Graphic3d_Structure) slight = new Graphic3d_Structure(TheViewer->StructureManager()); |
187 | myGraphicStructure = slight; |
188 | Handle(Graphic3d_Structure) snopick = new Graphic3d_Structure(TheViewer->StructureManager()); |
189 | myGraphicStructure1 = snopick; |
b8ddfc2f |
190 | } |
191 | |
b64d84be |
192 | Handle(Graphic3d_Group) gradius, gExtArrow, gIntArrow; |
193 | if (Pres == V3d_COMPLETE) |
194 | { |
c357e426 |
195 | gradius = myGraphicStructure->NewGroup(); |
196 | gExtArrow = myGraphicStructure->NewGroup(); |
197 | gIntArrow = myGraphicStructure->NewGroup(); |
7fd59977 |
198 | } |
c357e426 |
199 | Handle(Graphic3d_Group) glight = myGraphicStructure->NewGroup(); |
7fd59977 |
200 | Handle(Graphic3d_Group) gsphere; |
b64d84be |
201 | if (Pres == V3d_COMPLETE |
202 | || Pres == V3d_PARTIAL) |
203 | { |
c357e426 |
204 | gsphere = myGraphicStructure->NewGroup(); |
b64d84be |
205 | } |
7fd59977 |
206 | |
c357e426 |
207 | Handle(Graphic3d_Group) gnopick = myGraphicStructure1->NewGroup(); |
7fd59977 |
208 | |
c357e426 |
209 | X0 = myTarget.X(); |
210 | Y0 = myTarget.Y(); |
211 | Z0 = myTarget.Z(); |
7fd59977 |
212 | |
81bba717 |
213 | // Display of the position of the light. |
7fd59977 |
214 | |
87432b82 |
215 | const Quantity_Color Col1 = this->Color(); |
7fd59977 |
216 | Handle(Graphic3d_AspectLine3d) Asp1 = new Graphic3d_AspectLine3d(); |
217 | Asp1->SetColor(Col1); |
218 | glight->SetPrimitivesAspect(Asp1); |
c357e426 |
219 | this->Symbol(glight,theView); |
7fd59977 |
220 | |
81bba717 |
221 | // Display of the markable sphere (limit at the cercle). |
7fd59977 |
222 | |
223 | if (Pres == V3d_COMPLETE || Pres == V3d_PARTIAL) { |
224 | |
225 | Rayon = this->Radius(); |
c357e426 |
226 | theView->Proj(VX,VY,VZ); |
7fd59977 |
227 | V3d::CircleInPlane(gsphere,X0,Y0,Z0,VX,VY,VZ,Rayon); |
228 | |
81bba717 |
229 | // Display of the radius of the sphere (line + text) |
7fd59977 |
230 | |
231 | if (Pres == V3d_COMPLETE) { |
7fd59977 |
232 | this->Position(X,Y,Z); |
b8ddfc2f |
233 | Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(2); |
234 | aPrims->AddVertex(X0,Y0,Z0); |
235 | aPrims->AddVertex(X,Y,Z); |
236 | gnopick->AddPrimitiveArray(aPrims); |
237 | 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 |
238 | V3d::ArrowOfRadius(gIntArrow, X0, Y0, Z0, X0-X, Y0-Y, Z0-Z, M_PI / 15., Rayon / 20.); |
7fd59977 |
239 | TCollection_AsciiString ValOfRadius(Rayon); |
21b2385f |
240 | Graphic3d_Vertex PText (0.5*(X0+X), 0.5*(Y0+Y), 0.5*(Z0+Z)); |
7fd59977 |
241 | gradius->Text(ValOfRadius.ToCString(),PText,0.01); |
242 | } |
243 | |
81bba717 |
244 | // Display of the meridian |
7fd59977 |
245 | |
246 | Quantity_Color Col2(Quantity_NOC_GREEN); |
b8ddfc2f |
247 | Handle(Graphic3d_AspectLine3d) Asp2 = new Graphic3d_AspectLine3d(Col2,Aspect_TOL_SOLID,1.); |
7fd59977 |
248 | gnopick->SetPrimitivesAspect(Asp2); |
249 | |
81bba717 |
250 | // Definition of the axis of circle |
c357e426 |
251 | theView->Up(DXRef,DYRef,DZRef); |
7fd59977 |
252 | this->Position(X,Y,Z); |
253 | DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0; |
254 | VX = DYRef*DZini - DZRef*DYini; |
255 | VY = DZRef*DXini - DXRef*DZini; |
256 | VZ = DXRef*DYini - DYRef*DXini; |
257 | |
258 | V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon); |
259 | |
81bba717 |
260 | // Display of the parallel |
7fd59977 |
261 | |
81bba717 |
262 | // Definition of the axis of circle |
c357e426 |
263 | theView->Proj(VX,VY,VZ); |
264 | theView->Up(X1,Y1,Z1); |
7fd59977 |
265 | DXRef = VY * Z1 - VZ * Y1; |
266 | DYRef = VZ * X1 - VX * Z1; |
267 | DZRef = VX * Y1 - VY * X1; |
268 | this->Position(X,Y,Z); |
269 | DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0; |
270 | VX = DYRef*DZini - DZRef*DYini; |
271 | VY = DZRef*DXini - DXRef*DZini; |
272 | VZ = DXRef*DYini - DYRef*DXini; |
273 | |
274 | V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon); |
7fd59977 |
275 | } |
276 | |
c357e426 |
277 | myGraphicStructure->Connect(myGraphicStructure1,Graphic3d_TOC_DESCENDANT); |
278 | myTypeOfRepresentation = Pres; |
279 | myGraphicStructure->Display(); |
7fd59977 |
280 | } |