1 // Copyright (c) 1999-2012 OPEN CASCADE SAS
3 // The content of this file is subject to the Open CASCADE Technology Public
4 // License Version 6.5 (the "License"). You may not use the content of this file
5 // except in compliance with the License. Please obtain a copy of the License
6 // at http://www.opencascade.org and read it completely before using this file.
8 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
11 // The Original Code and all software distributed under the License is
12 // distributed on an "AS IS" basis, without warranty of any kind, and the
13 // Initial Developer hereby disclaims all such warranties, including without
14 // limitation, any warranties of merchantability, fitness for a particular
15 // purpose or non-infringement. Please see the License for the specific terms
16 // and conditions governing the rights and limitations under the License.
18 /***********************************************************************
22 Classe V3d_PositionalLight :
24 HISTORIQUE DES MODIFICATIONS :
25 --------------------------------
26 00-09-92 : GG ; Creation.
27 18-06-96 : FMN ; Ajout MyGraphicStructure1 pour sauvegarder snopick
28 24-12-97 : FMN ; Remplacement de math par MathGra
29 31-12-97 : CAL ; Suppression de MathGra
30 21-01-98 : CAL ; Window de Xw et WNT remplacee par Aspect_Window
31 23-02-98 : FMN ; Remplacement PI par Standard_PI
32 30-03-98 : ZOV ; PRO6774 (reconstruction of the class hierarchy and suppressing useless methods)
34 ************************************************************************/
36 /*----------------------------------------------------------------------*/
42 #include <V3d_PositionalLight.ixx>
43 #include <Graphic3d_Vector.hxx>
44 #include <Graphic3d_Vertex.hxx>
45 #include <Graphic3d_Structure.hxx>
46 #include <Graphic3d_Group.hxx>
47 #include <Graphic3d_ArrayOfSegments.hxx>
48 #include <Graphic3d_AspectMarker3d.hxx>
49 #include <Graphic3d_AspectLine3d.hxx>
50 #include <Graphic3d_AspectText3d.hxx>
51 #include <Visual3d_Light.hxx>
52 #include <Visual3d_ViewManager.hxx>
53 #include <Visual3d_ContextPick.hxx>
54 #include <Visual3d_PickDescriptor.hxx>
55 #include <Visual3d_HSequenceOfPickPath.hxx>
56 #include <Visual3d_PickPath.hxx>
57 #include <Viewer_BadValue.hxx>
62 #include <gp_Trsf.hxx>
63 #include <TColStd_Array2OfReal.hxx>
64 #include <TCollection_AsciiString.hxx>
65 #include <Aspect_Window.hxx>
71 V3d_PositionalLight::V3d_PositionalLight(const Handle(V3d_Viewer)& VM, const Standard_Real X, const Standard_Real Y, const Standard_Real Z, const Quantity_NameOfColor Name, const Standard_Real A1, const Standard_Real A2):V3d_PositionLight(VM) {
73 Quantity_Color C(Name) ;
74 Graphic3d_Vertex P(X,Y,Z) ;
75 Graphic3d_Vertex T(0.,0.,0.);
77 Viewer_BadValue_Raise_if ( A1 < 0 || A1 > 1. || A2 < 0 || A2 > 1,
78 "V3d_PositionalLight, bad coefficients");
80 MyType = V3d_POSITIONAL ;
81 MyLight = new Visual3d_Light(C,P,A1,A2) ;
86 V3d_PositionalLight::V3d_PositionalLight(const Handle(V3d_Viewer)& VM, const Standard_Real Xt, const Standard_Real Yt, const Standard_Real Zt, const Standard_Real Xp, const Standard_Real Yp, const Standard_Real Zp, const Quantity_NameOfColor Name, const Standard_Real A1, const Standard_Real A2):V3d_PositionLight(VM) {
88 Quantity_Color C(Name) ;
89 Graphic3d_Vertex T(Xt,Yt,Zt) ;
90 Graphic3d_Vertex P(Xp,Yp,Zp) ;
92 Viewer_BadValue_Raise_if ( A1 < 0 || A1 > 1. || A2 < 0 || A2 > 1,
93 "V3d_PositionalLight, bad coefficients");
95 MyType = V3d_POSITIONAL ;
96 MyLight = new Visual3d_Light(C,P,A1,A2) ;
98 // Graphic structure is initialized during the display.
104 void V3d_PositionalLight::SetPosition(const Standard_Real Xp, const Standard_Real Yp, const Standard_Real Zp) {
105 MyLight->SetPosition (Graphic3d_Vertex (Xp,Yp,Zp));
108 void V3d_PositionalLight::SetAttenuation(const Standard_Real A1, const Standard_Real A2) {
111 Viewer_BadValue_Raise_if ( A1 < 0 || A1 > 1. || A2 < 0 || A2 > 1,
112 "V3d_PositionalLight::SetAttenuation, bad coefficients");
114 MyLight->SetAttenuation1(A1) ;
115 MyLight->SetAttenuation2(A2) ;
118 void V3d_PositionalLight::Position(Standard_Real& X, Standard_Real& Y, Standard_Real& Z)const {
121 Standard_Real A1,A2 ;
123 MyLight->Values(C,P,A1,A2) ;
127 void V3d_PositionalLight::Attenuation(Standard_Real& A1, Standard_Real& A2)const {
131 MyLight->Values(C,P,A1,A2) ;
134 void V3d_PositionalLight::Symbol (const Handle(Graphic3d_Group)& gsymbol, const Handle(V3d_View)& aView) const {
136 Standard_Real Xi,Yi,Zi,Xf,Yf,Zf,Rayon,PXT,PYT,X,Y,Z,XT,YT,ZT;
137 Standard_Real A,B,C,Dist,Beta,CosBeta,SinBeta,Coef,X1,Y1,Z1;
138 Standard_Real VX,VY,VZ;
139 Standard_Integer IXP,IYP,j;
140 TColStd_Array2OfReal MatRot(0,2,0,2);
142 aView->Proj(VX,VY,VZ);
143 this->Position(Xi,Yi,Zi);
144 Rayon = this->Radius();
145 aView->Project(Xi,Yi,Zi,PXT,PYT);
146 aView->Convert(PXT,PYT,IXP,IYP);
147 // 3D Coordinate in the plane of projection of the source.
148 aView->Convert(IXP,IYP,XT,YT,ZT);
149 aView->Convert(PXT,PYT+Rayon,IXP,IYP);
150 aView->Convert(IXP,IYP,X,Y,Z);
151 X = X+Xi-XT; Y = Y+Yi-YT; Z = Z+Zi-ZT;
152 Dist = Sqrt( Square(X-Xi) + Square(Y-Yi) + Square(Z-Zi) );
159 V3d::CircleInPlane(gsymbol,Xi,Yi,Zi,VX,VY,VZ,Rayon/40.);
160 for( j=1 ; j<=3 ; j++ ) {
161 Beta = j * M_PI / 4.;
165 MatRot(0,0) = A * A + (1. - A * A) * CosBeta;
166 MatRot(0,1) = -C * SinBeta + Coef * A * B;
167 MatRot(0,2) = B * SinBeta + Coef * A * C;
168 MatRot(1,0) = C * SinBeta + Coef * A * B;
169 MatRot(1,1) = B * B + (1. - B * B) * CosBeta;
170 MatRot(1,2) = -A * SinBeta + Coef * B * C;
171 MatRot(2,0) = -B * SinBeta + Coef * A * C;
172 MatRot(2,1) = A * SinBeta + Coef * B * C;
173 MatRot(2,2) = C * C + (1. - C * C) * CosBeta;
174 Xf = Xi * MatRot(0,0) + Yi * MatRot(0,1) + Zi * MatRot(0,2);
175 Yf = Xi * MatRot(1,0) + Yi * MatRot(1,1) + Zi * MatRot(1,2);
176 Zf = Xi * MatRot(2,0) + Yi * MatRot(2,1) + Zi * MatRot(2,2);
177 // Rotation of the normal
178 X1 = VX * MatRot(0,0) + VY * MatRot(0,1) + VZ * MatRot(0,2);
179 Y1 = VX * MatRot(1,0) + VY * MatRot(1,1) + VZ * MatRot(1,2);
180 Z1 = VX * MatRot(2,0) + VY * MatRot(2,1) + VZ * MatRot(2,2);
181 VX = X1 + Xi - Xf ; VY = Y1 + Yi - Yf ; VZ = Z1 + Zi - Zf;
182 V3d::CircleInPlane(gsymbol,Xi,Yi,Zi,VX,VY,VZ,Rayon/40.);
187 void V3d_PositionalLight::Display( const Handle(V3d_View)& aView,
188 const V3d_TypeOfRepresentation TPres )
190 Graphic3d_Vertex PText ;
191 Standard_Real X,Y,Z,Rayon;
192 Standard_Real X0,Y0,Z0,VX,VY,VZ;
193 Standard_Real X1,Y1,Z1;
194 Standard_Real DXRef,DYRef,DZRef,DXini,DYini,DZini;
195 Standard_Real R1,G1,B1;
196 V3d_TypeOfRepresentation Pres;
197 V3d_TypeOfUpdate UpdSov;
199 // Creation of a structure slight of markable elements (position of the
200 // light, and the domain of lighting represented by a circle)
201 // Creation of a structure snopick of non-markable elements (target, meridian and
205 Handle(V3d_Viewer) TheViewer = aView->Viewer();
206 UpdSov = TheViewer->UpdateMode();
207 TheViewer->SetUpdateMode(V3d_WAIT);
208 if (!MyGraphicStructure.IsNull()) {
209 MyGraphicStructure->Disconnect(MyGraphicStructure1);
210 MyGraphicStructure->Clear();
211 MyGraphicStructure1->Clear();
212 if (Pres == V3d_SAMELAST) Pres = MyTypeOfRepresentation;
215 if (Pres == V3d_SAMELAST) Pres = V3d_SIMPLE;
216 Handle(Graphic3d_Structure) slight = new Graphic3d_Structure(TheViewer->Viewer());
217 MyGraphicStructure = slight;
218 Handle(Graphic3d_Structure) snopick = new Graphic3d_Structure(TheViewer->Viewer());
219 MyGraphicStructure1 = snopick;
222 Handle(Graphic3d_Group) gradius;
223 Handle(Graphic3d_Group) gExtArrow;
224 Handle(Graphic3d_Group) gIntArrow;
225 if (Pres == V3d_COMPLETE) {
226 gradius = new Graphic3d_Group(MyGraphicStructure);
227 gExtArrow = new Graphic3d_Group(MyGraphicStructure);
228 gIntArrow = new Graphic3d_Group(MyGraphicStructure);
230 Handle(Graphic3d_Group) glight = new Graphic3d_Group(MyGraphicStructure);
231 Handle(Graphic3d_Group) gsphere;
232 if (Pres == V3d_COMPLETE || Pres == V3d_PARTIAL) gsphere = new Graphic3d_Group(MyGraphicStructure);
234 Handle(Graphic3d_Group) gnopick = new Graphic3d_Group(MyGraphicStructure1);
235 MyGraphicStructure1->SetPick(Standard_False);
241 // Display of the position of the light.
243 this->Color(Quantity_TOC_RGB,R1,G1,B1);
244 Quantity_Color Col1(R1,G1,B1,Quantity_TOC_RGB);
245 Handle(Graphic3d_AspectLine3d) Asp1 = new Graphic3d_AspectLine3d();
246 Asp1->SetColor(Col1);
247 glight->SetPrimitivesAspect(Asp1);
248 this->Symbol(glight,aView);
250 // Display of the markable sphere (limit at the cercle).
252 if (Pres == V3d_COMPLETE || Pres == V3d_PARTIAL) {
254 Rayon = this->Radius();
255 aView->Proj(VX,VY,VZ);
256 V3d::CircleInPlane(gsphere,X0,Y0,Z0,VX,VY,VZ,Rayon);
258 // Display of the radius of the sphere (line + text)
260 if (Pres == V3d_COMPLETE) {
261 this->Position(X,Y,Z);
262 Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(2);
263 aPrims->AddVertex(X0,Y0,Z0);
264 aPrims->AddVertex(X,Y,Z);
265 gnopick->AddPrimitiveArray(aPrims);
266 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.);
267 V3d::ArrowOfRadius(gIntArrow, X0, Y0, Z0, X0-X, Y0-Y, Z0-Z, M_PI / 15., Rayon / 20.);
268 TCollection_AsciiString ValOfRadius(Rayon);
269 PText.SetCoord( 0.5*(X0+X), 0.5*(Y0+Y), 0.5*(Z0+Z) );
270 gradius->Text(ValOfRadius.ToCString(),PText,0.01);
273 // Display of the meridian
275 Quantity_Color Col2(Quantity_NOC_GREEN);
276 Handle(Graphic3d_AspectLine3d) Asp2 = new Graphic3d_AspectLine3d(Col2,Aspect_TOL_SOLID,1.);
277 gnopick->SetPrimitivesAspect(Asp2);
279 // Definition of the axis of circle
280 aView->Up(DXRef,DYRef,DZRef);
281 this->Position(X,Y,Z);
282 DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
283 VX = DYRef*DZini - DZRef*DYini;
284 VY = DZRef*DXini - DXRef*DZini;
285 VZ = DXRef*DYini - DYRef*DXini;
287 V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
289 // Display of the parallel
291 // Definition of the axis of circle
292 aView->Proj(VX,VY,VZ);
294 DXRef = VY * Z1 - VZ * Y1;
295 DYRef = VZ * X1 - VX * Z1;
296 DZRef = VX * Y1 - VY * X1;
297 this->Position(X,Y,Z);
298 DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
299 VX = DYRef*DZini - DZRef*DYini;
300 VY = DZRef*DXini - DXRef*DZini;
301 VZ = DXRef*DYini - DYRef*DXini;
303 V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
306 MyGraphicStructure->Connect(MyGraphicStructure1,Graphic3d_TOC_DESCENDANT);
307 MyTypeOfRepresentation = Pres;
308 MyGraphicStructure->Display();
309 TheViewer->SetUpdateMode(UpdSov);