0026278: Canonical recognition from time to time raises exception on the attached...
[occt.git] / src / V3d / V3d_PositionalLight.cxx
CommitLineData
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/***********************************************************************
15
16 FONCTION :
17 ----------
18 Classe V3d_PositionalLight :
19
20 HISTORIQUE DES MODIFICATIONS :
21 --------------------------------
22 00-09-92 : GG ; Creation.
23 18-06-96 : FMN ; Ajout MyGraphicStructure1 pour sauvegarder snopick
24 24-12-97 : FMN ; Remplacement de math par MathGra
25 31-12-97 : CAL ; Suppression de MathGra
26 21-01-98 : CAL ; Window de Xw et WNT remplacee par Aspect_Window
27 23-02-98 : FMN ; Remplacement PI par Standard_PI
28 30-03-98 : ZOV ; PRO6774 (reconstruction of the class hierarchy and suppressing useless methods)
29
30************************************************************************/
31
32/*----------------------------------------------------------------------*/
33/*
34 * Includes
35 */
36
37#include <V3d.hxx>
38#include <V3d_PositionalLight.ixx>
39#include <Graphic3d_Vector.hxx>
40#include <Graphic3d_Vertex.hxx>
41#include <Graphic3d_Structure.hxx>
42#include <Graphic3d_Group.hxx>
b8ddfc2f 43#include <Graphic3d_ArrayOfSegments.hxx>
7fd59977 44#include <Graphic3d_AspectMarker3d.hxx>
45#include <Graphic3d_AspectLine3d.hxx>
46#include <Graphic3d_AspectText3d.hxx>
47#include <Visual3d_Light.hxx>
48#include <Visual3d_ViewManager.hxx>
49#include <Visual3d_ContextPick.hxx>
6942f04a 50#include <V3d_BadValue.hxx>
7fd59977 51#include <gp_Dir.hxx>
52#include <gp_Ax1.hxx>
53#include <gp_Vec.hxx>
54#include <gp_Pnt.hxx>
55#include <gp_Trsf.hxx>
56#include <TColStd_Array2OfReal.hxx>
57#include <TCollection_AsciiString.hxx>
58#include <Aspect_Window.hxx>
59
60//-Declarations
61
62//-Constructors
63
64V3d_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) {
65
66 Quantity_Color C(Name) ;
67 Graphic3d_Vertex P(X,Y,Z) ;
68 Graphic3d_Vertex T(0.,0.,0.);
69
6942f04a 70 V3d_BadValue_Raise_if ( A1 < 0 || A1 > 1. || A2 < 0 || A2 > 1,
7fd59977 71 "V3d_PositionalLight, bad coefficients");
72
73 MyType = V3d_POSITIONAL ;
74 MyLight = new Visual3d_Light(C,P,A1,A2) ;
75 MyTarget = T;
76
77}
78
79V3d_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) {
80
81 Quantity_Color C(Name) ;
82 Graphic3d_Vertex T(Xt,Yt,Zt) ;
83 Graphic3d_Vertex P(Xp,Yp,Zp) ;
84
6942f04a 85 V3d_BadValue_Raise_if ( A1 < 0 || A1 > 1. || A2 < 0 || A2 > 1,
7fd59977 86 "V3d_PositionalLight, bad coefficients");
87
88 MyType = V3d_POSITIONAL ;
89 MyLight = new Visual3d_Light(C,P,A1,A2) ;
90 MyTarget = T;
81bba717 91 // Graphic structure is initialized during the display.
7fd59977 92
93}
94
95//-Methods, in order
96
97void V3d_PositionalLight::SetPosition(const Standard_Real Xp, const Standard_Real Yp, const Standard_Real Zp) {
98 MyLight->SetPosition (Graphic3d_Vertex (Xp,Yp,Zp));
99}
100
101void V3d_PositionalLight::SetAttenuation(const Standard_Real A1, const Standard_Real A2) {
102
103
6942f04a 104 V3d_BadValue_Raise_if ( A1 < 0 || A1 > 1. || A2 < 0 || A2 > 1,
7fd59977 105 "V3d_PositionalLight::SetAttenuation, bad coefficients");
106
107 MyLight->SetAttenuation1(A1) ;
108 MyLight->SetAttenuation2(A2) ;
109}
110
111void V3d_PositionalLight::Position(Standard_Real& X, Standard_Real& Y, Standard_Real& Z)const {
112 Quantity_Color C ;
113 Graphic3d_Vertex P ;
114 Standard_Real A1,A2 ;
115
116 MyLight->Values(C,P,A1,A2) ;
117 P.Coord(X,Y,Z) ;
118}
119
120void V3d_PositionalLight::Attenuation(Standard_Real& A1, Standard_Real& A2)const {
121 Quantity_Color C ;
122 Graphic3d_Vertex P ;
123
124 MyLight->Values(C,P,A1,A2) ;
125}
126
127void V3d_PositionalLight::Symbol (const Handle(Graphic3d_Group)& gsymbol, const Handle(V3d_View)& aView) const {
128
129 Standard_Real Xi,Yi,Zi,Xf,Yf,Zf,Rayon,PXT,PYT,X,Y,Z,XT,YT,ZT;
130 Standard_Real A,B,C,Dist,Beta,CosBeta,SinBeta,Coef,X1,Y1,Z1;
131 Standard_Real VX,VY,VZ;
132 Standard_Integer IXP,IYP,j;
133 TColStd_Array2OfReal MatRot(0,2,0,2);
134
135 aView->Proj(VX,VY,VZ);
136 this->Position(Xi,Yi,Zi);
137 Rayon = this->Radius();
138 aView->Project(Xi,Yi,Zi,PXT,PYT);
139 aView->Convert(PXT,PYT,IXP,IYP);
81bba717 140// 3D Coordinate in the plane of projection of the source.
7fd59977 141 aView->Convert(IXP,IYP,XT,YT,ZT);
142 aView->Convert(PXT,PYT+Rayon,IXP,IYP);
143 aView->Convert(IXP,IYP,X,Y,Z);
144 X = X+Xi-XT; Y = Y+Yi-YT; Z = Z+Zi-ZT;
145 Dist = Sqrt( Square(X-Xi) + Square(Y-Yi) + Square(Z-Zi) );
81bba717 146// Axis of rotation.
7fd59977 147 A = (X-Xi)/Dist;
148 B = (Y-Yi)/Dist;
149 C = (Z-Zi)/Dist;
150
81bba717 151// A sphere is drawn
7fd59977 152 V3d::CircleInPlane(gsymbol,Xi,Yi,Zi,VX,VY,VZ,Rayon/40.);
153 for( j=1 ; j<=3 ; j++ ) {
c6541a0c 154 Beta = j * M_PI / 4.;
7fd59977 155 CosBeta = Cos(Beta);
156 SinBeta = Sin(Beta);
157 Coef = 1. - CosBeta;
158 MatRot(0,0) = A * A + (1. - A * A) * CosBeta;
159 MatRot(0,1) = -C * SinBeta + Coef * A * B;
160 MatRot(0,2) = B * SinBeta + Coef * A * C;
161 MatRot(1,0) = C * SinBeta + Coef * A * B;
162 MatRot(1,1) = B * B + (1. - B * B) * CosBeta;
163 MatRot(1,2) = -A * SinBeta + Coef * B * C;
164 MatRot(2,0) = -B * SinBeta + Coef * A * C;
165 MatRot(2,1) = A * SinBeta + Coef * B * C;
166 MatRot(2,2) = C * C + (1. - C * C) * CosBeta;
167 Xf = Xi * MatRot(0,0) + Yi * MatRot(0,1) + Zi * MatRot(0,2);
168 Yf = Xi * MatRot(1,0) + Yi * MatRot(1,1) + Zi * MatRot(1,2);
169 Zf = Xi * MatRot(2,0) + Yi * MatRot(2,1) + Zi * MatRot(2,2);
81bba717 170// Rotation of the normal
7fd59977 171 X1 = VX * MatRot(0,0) + VY * MatRot(0,1) + VZ * MatRot(0,2);
172 Y1 = VX * MatRot(1,0) + VY * MatRot(1,1) + VZ * MatRot(1,2);
173 Z1 = VX * MatRot(2,0) + VY * MatRot(2,1) + VZ * MatRot(2,2);
174 VX = X1 + Xi - Xf ; VY = Y1 + Yi - Yf ; VZ = Z1 + Zi - Zf;
175 V3d::CircleInPlane(gsymbol,Xi,Yi,Zi,VX,VY,VZ,Rayon/40.);
176 }
177}
178
179
180void V3d_PositionalLight::Display( const Handle(V3d_View)& aView,
b8ddfc2f 181 const V3d_TypeOfRepresentation TPres )
182{
7fd59977 183 Graphic3d_Vertex PText ;
184 Standard_Real X,Y,Z,Rayon;
185 Standard_Real X0,Y0,Z0,VX,VY,VZ;
186 Standard_Real X1,Y1,Z1;
187 Standard_Real DXRef,DYRef,DZRef,DXini,DYini,DZini;
188 Standard_Real R1,G1,B1;
189 V3d_TypeOfRepresentation Pres;
190 V3d_TypeOfUpdate UpdSov;
191
81bba717 192// Creation of a structure slight of markable elements (position of the
193// light, and the domain of lighting represented by a circle)
194// Creation of a structure snopick of non-markable elements (target, meridian and
195// parallel).
7fd59977 196
b8ddfc2f 197 Pres = TPres;
198 Handle(V3d_Viewer) TheViewer = aView->Viewer();
199 UpdSov = TheViewer->UpdateMode();
200 TheViewer->SetUpdateMode(V3d_WAIT);
201 if (!MyGraphicStructure.IsNull()) {
202 MyGraphicStructure->Disconnect(MyGraphicStructure1);
203 MyGraphicStructure->Clear();
204 MyGraphicStructure1->Clear();
205 if (Pres == V3d_SAMELAST) Pres = MyTypeOfRepresentation;
206 }
207 else {
208 if (Pres == V3d_SAMELAST) Pres = V3d_SIMPLE;
209 Handle(Graphic3d_Structure) slight = new Graphic3d_Structure(TheViewer->Viewer());
210 MyGraphicStructure = slight;
211 Handle(Graphic3d_Structure) snopick = new Graphic3d_Structure(TheViewer->Viewer());
212 MyGraphicStructure1 = snopick;
213 }
214
b64d84be 215 Handle(Graphic3d_Group) gradius, gExtArrow, gIntArrow;
216 if (Pres == V3d_COMPLETE)
217 {
218 gradius = MyGraphicStructure->NewGroup();
219 gExtArrow = MyGraphicStructure->NewGroup();
220 gIntArrow = MyGraphicStructure->NewGroup();
7fd59977 221 }
b64d84be 222 Handle(Graphic3d_Group) glight = MyGraphicStructure->NewGroup();
7fd59977 223 Handle(Graphic3d_Group) gsphere;
b64d84be 224 if (Pres == V3d_COMPLETE
225 || Pres == V3d_PARTIAL)
226 {
227 gsphere = MyGraphicStructure->NewGroup();
228 }
7fd59977 229
b64d84be 230 Handle(Graphic3d_Group) gnopick = MyGraphicStructure1->NewGroup();
7fd59977 231
232 X0 = MyTarget.X();
233 Y0 = MyTarget.Y();
234 Z0 = MyTarget.Z();
235
81bba717 236// Display of the position of the light.
7fd59977 237
7fd59977 238 this->Color(Quantity_TOC_RGB,R1,G1,B1);
239 Quantity_Color Col1(R1,G1,B1,Quantity_TOC_RGB);
240 Handle(Graphic3d_AspectLine3d) Asp1 = new Graphic3d_AspectLine3d();
241 Asp1->SetColor(Col1);
242 glight->SetPrimitivesAspect(Asp1);
243 this->Symbol(glight,aView);
244
81bba717 245// Display of the markable sphere (limit at the cercle).
7fd59977 246
247 if (Pres == V3d_COMPLETE || Pres == V3d_PARTIAL) {
248
249 Rayon = this->Radius();
250 aView->Proj(VX,VY,VZ);
7fd59977 251 V3d::CircleInPlane(gsphere,X0,Y0,Z0,VX,VY,VZ,Rayon);
252
81bba717 253// Display of the radius of the sphere (line + text)
7fd59977 254
255 if (Pres == V3d_COMPLETE) {
7fd59977 256 this->Position(X,Y,Z);
b8ddfc2f 257 Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(2);
258 aPrims->AddVertex(X0,Y0,Z0);
259 aPrims->AddVertex(X,Y,Z);
260 gnopick->AddPrimitiveArray(aPrims);
261 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 262 V3d::ArrowOfRadius(gIntArrow, X0, Y0, Z0, X0-X, Y0-Y, Z0-Z, M_PI / 15., Rayon / 20.);
7fd59977 263 TCollection_AsciiString ValOfRadius(Rayon);
b8ddfc2f 264 PText.SetCoord( 0.5*(X0+X), 0.5*(Y0+Y), 0.5*(Z0+Z) );
7fd59977 265 gradius->Text(ValOfRadius.ToCString(),PText,0.01);
266 }
267
81bba717 268// Display of the meridian
7fd59977 269
270 Quantity_Color Col2(Quantity_NOC_GREEN);
b8ddfc2f 271 Handle(Graphic3d_AspectLine3d) Asp2 = new Graphic3d_AspectLine3d(Col2,Aspect_TOL_SOLID,1.);
7fd59977 272 gnopick->SetPrimitivesAspect(Asp2);
273
81bba717 274// Definition of the axis of circle
7fd59977 275 aView->Up(DXRef,DYRef,DZRef);
276 this->Position(X,Y,Z);
277 DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
278 VX = DYRef*DZini - DZRef*DYini;
279 VY = DZRef*DXini - DXRef*DZini;
280 VZ = DXRef*DYini - DYRef*DXini;
281
282 V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
283
81bba717 284// Display of the parallel
7fd59977 285
81bba717 286// Definition of the axis of circle
7fd59977 287 aView->Proj(VX,VY,VZ);
288 aView->Up(X1,Y1,Z1);
289 DXRef = VY * Z1 - VZ * Y1;
290 DYRef = VZ * X1 - VX * Z1;
291 DZRef = VX * Y1 - VY * X1;
292 this->Position(X,Y,Z);
293 DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
294 VX = DYRef*DZini - DZRef*DYini;
295 VY = DZRef*DXini - DXRef*DZini;
296 VZ = DXRef*DYini - DYRef*DXini;
297
298 V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
7fd59977 299 }
300
301 MyGraphicStructure->Connect(MyGraphicStructure1,Graphic3d_TOC_DESCENDANT);
302 MyTypeOfRepresentation = Pres;
303 MyGraphicStructure->Display();
304 TheViewer->SetUpdateMode(UpdSov);
305}