0024023: Revamp the OCCT Handle -- ambiguity
[occt.git] / src / V3d / V3d_SpotLight.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_SpotLight :
19
20 HISTORIQUE DES MODIFICATIONS :
21 --------------------------------
22 00-09-92 : GG ; Creation.
23 18-06-96 : FMN ; Ajout MyGraphicStructure1 pour sauvegarder snopick
24 30-03-98 : ZOV ; PRO6774 (reconstruction of the class hierarchy and suppressing useless methods)
25 02.15.100 : JR : Clutter
26
27************************************************************************/
28
29/*----------------------------------------------------------------------*/
30/*
31 * Includes
32 */
33
34#include <V3d.hxx>
35#include <V3d_SpotLight.ixx>
36#include <Graphic3d_Vector.hxx>
37#include <Graphic3d_Vertex.hxx>
38#include <Graphic3d_Structure.hxx>
39#include <Graphic3d_Group.hxx>
b8ddfc2f 40#include <Graphic3d_ArrayOfSegments.hxx>
7fd59977 41#include <Graphic3d_AspectMarker3d.hxx>
42#include <Graphic3d_AspectLine3d.hxx>
43#include <Graphic3d_AspectText3d.hxx>
44#include <Visual3d_Light.hxx>
45#include <Visual3d_ViewManager.hxx>
46#include <Visual3d_ContextPick.hxx>
7fd59977 47#include <gp_Dir.hxx>
48#include <gp_Ax1.hxx>
49#include <gp_Vec.hxx>
50#include <gp_Pnt.hxx>
51#include <gp_Trsf.hxx>
52#include <TCollection_AsciiString.hxx>
53
7fd59977 54V3d_SpotLight::V3d_SpotLight(const Handle(V3d_Viewer)& VM, const Standard_Real X, const Standard_Real Y, const Standard_Real Z, const V3d_TypeOfOrientation Direction, const Quantity_NameOfColor Name, const Standard_Real A1, const Standard_Real A2, const Standard_Real CN, const Standard_Real AN):V3d_PositionLight(VM) {
55
6942f04a 56 V3d_BadValue_Raise_if( A1 < 0 || A1 > 1. || A2 < 0 || A2 > 1
c6541a0c 57 || AN < 0. || AN > M_PI, "V3d_SpotLight, bad coefficient or angle");
7fd59977 58
59 Quantity_Color C(Name) ;
60 Graphic3d_Vector D = V3d::GetProjAxis(Direction) ;
61 Graphic3d_Vertex P(X,Y,Z) ;
62 Graphic3d_Vertex T;
63
64 MyType = V3d_SPOT ;
65 MyLight = new Visual3d_Light(C,P,D,CN,A1,A2,AN) ;
81bba717 66 // The target is fixed, starting from the light position and the direction.
7fd59977 67 T.SetCoord(X + D.X(), Y + D.Y(), Z + D.Z());
68 MyTarget = T;
69}
70
71V3d_SpotLight::V3d_SpotLight(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, const Standard_Real CN, const Standard_Real AN):V3d_PositionLight(VM) {
72
6942f04a 73 V3d_BadValue_Raise_if( A1 < 0 || A1 > 1. || A2 < 0 || A2 > 1
c6541a0c 74 || AN < 0. || AN > M_PI, "V3d_SpotLight, bad coefficient or angle");
7fd59977 75
76 Quantity_Color C(Name) ;
77 Graphic3d_Vertex T(Xt,Yt,Zt) ;
78 Graphic3d_Vertex P(Xp,Yp,Zp) ;
79 Graphic3d_Vector D(P,T);
80
81 MyType = V3d_SPOT ;
82 D.Normalize();
83 MyLight = new Visual3d_Light(C,P,D,CN,A1,A2,AN) ;
84 MyTarget = T;
85 // La Structure graphique sera initialisee lors de l'affichage.
86}
87
88
89//-Methods, in order
90
91void V3d_SpotLight::SetPosition(const Standard_Real Xp, const Standard_Real Yp, const Standard_Real Zp) {
92 MyLight->SetPosition (Graphic3d_Vertex (Xp,Yp,Zp));
93}
94
95void V3d_SpotLight::SetDirection(const Standard_Real Vx, const Standard_Real Vy, const Standard_Real Vz) {
96
97 Graphic3d_Vector D ;
98 D.SetCoord(Vx,Vy,Vz) ; D.Normalize() ;
99 MyLight->SetDirection(D) ;
100}
101
102void V3d_SpotLight::SetDirection(const V3d_TypeOfOrientation Direction) {
103
104 Graphic3d_Vector D = V3d::GetProjAxis(Direction) ;
105 MyLight->SetDirection(D) ;
106}
107
108void V3d_SpotLight::SetAttenuation(const Standard_Real A1, const Standard_Real A2) {
109
6942f04a 110 V3d_BadValue_Raise_if( A1 < 0 || A1 > 1. || A2 < 0 || A2 > 1 ,
7fd59977 111 "V3d_SpotLight::SetAttenuation, bad coefficients");
112
113 MyLight->SetAttenuation1(A1) ;
114 MyLight->SetAttenuation2(A2) ;
115}
116
117void V3d_SpotLight::SetConcentration(const Standard_Real C) {
118
119
6942f04a 120 V3d_BadValue_Raise_if( C < 0 || C > 1.,
7fd59977 121 "V3d_SpotLight::SetConcentration, bad coefficient");
122
123 MyLight->SetConcentration(C) ;
124}
125
126void V3d_SpotLight::SetAngle(const Standard_Real Angle) {
127
6942f04a 128 V3d_BadValue_Raise_if( Angle <= 0. || Angle >= M_PI,
7fd59977 129 "V3d_SpotLight::SetAngle, bad angle");
130 MyLight->SetAngle(Angle) ;
131
132}
133
134void V3d_SpotLight::Direction(Standard_Real& Vx, Standard_Real& Vy, Standard_Real& Vz)const {
135
136 Quantity_Color C ;
137 Graphic3d_Vector D ;
138 Graphic3d_Vertex P ;
139 Standard_Real CN,A1,A2,AN ;
140
141 MyLight->Values(C,P,D,CN,A1,A2,AN) ;
142 D.Coord(Vx,Vy,Vz) ;
143}
144
145void V3d_SpotLight::Position(Standard_Real& Xp, Standard_Real& Yp, Standard_Real& Zp)const {
146
147 Quantity_Color C ;
148 Graphic3d_Vector D ;
149 Graphic3d_Vertex P ;
150 Standard_Real CN,A1,A2,AN ;
151
152 MyLight->Values(C,P,D,CN,A1,A2,AN) ;
153 P.Coord(Xp,Yp,Zp) ;
154}
155
156void V3d_SpotLight::Attenuation(Standard_Real& A1, Standard_Real& A2) const {
157 Quantity_Color C ;
158 Graphic3d_Vector D ;
159 Graphic3d_Vertex P ;
160 Standard_Real CN,AN ;
161
162 MyLight->Values(C,P,D,CN,A1,A2,AN) ;
163}
164
b8ddfc2f 165Standard_Real V3d_SpotLight::Concentration()const
166{
7fd59977 167 Quantity_Color C ;
168 Graphic3d_Vector D ;
169 Graphic3d_Vertex P ;
170 Standard_Real AN,A1,A2,CN ;
171
172 MyLight->Values(C,P,D,CN,A1,A2,AN) ;
173 return CN ;
174}
175
b8ddfc2f 176Standard_Real V3d_SpotLight::Angle()const
177{
7fd59977 178 Quantity_Color C ;
179 Graphic3d_Vector D ;
180 Graphic3d_Vertex P ;
181 Standard_Real CN,A1,A2,AN ;
182
183 MyLight->Values(C,P,D,CN,A1,A2,AN) ;
184 return AN ;
185}
186
187void V3d_SpotLight::Symbol (const Handle(Graphic3d_Group)& gsymbol,
b8ddfc2f 188 const Handle(V3d_View)& ) const
189{
190 Standard_Real X,Y,Z;
7fd59977 191 Standard_Real DX,DY,DZ;
7fd59977 192 this->Position(X,Y,Z);
193 this->Direction(DX,DY,DZ);
b8ddfc2f 194
195 V3d::ArrowOfRadius(gsymbol,X,Y,Z,-DX,-DY,-DZ,M_PI/8.,this->Radius()/15.);
7fd59977 196}
197
198void V3d_SpotLight::Display( const Handle(V3d_View)& aView,
b8ddfc2f 199 const V3d_TypeOfRepresentation TPres)
200{
7fd59977 201 Graphic3d_Vertex PText ;
202 Standard_Real X,Y,Z,Rayon;
203 Standard_Real X0,Y0,Z0,VX,VY,VZ;
204 Standard_Real X1,Y1,Z1;
205 Standard_Real DXRef,DYRef,DZRef,DXini,DYini,DZini;
206 Standard_Real R1,G1,B1;
207 V3d_TypeOfRepresentation Pres;
208 V3d_TypeOfUpdate UpdSov;
209
81bba717 210// Creation of a structure slight of markable elements (position of the
211// light, and the domain of lighting represented by a circle)
212// Creation of a structure snopick of non-markable elements (target, meridian and
213// parallel).//
7fd59977 214
b8ddfc2f 215 Pres = TPres;
216 Handle(V3d_Viewer) TheViewer = aView->Viewer();
217 UpdSov = TheViewer->UpdateMode();
218 TheViewer->SetUpdateMode(V3d_WAIT);
219 if (!MyGraphicStructure.IsNull()) {
220 MyGraphicStructure->Disconnect(MyGraphicStructure1);
221 MyGraphicStructure->Clear();
222 MyGraphicStructure1->Clear();
223 if (Pres == V3d_SAMELAST) Pres = MyTypeOfRepresentation;
224 }
225 else {
226 if (Pres == V3d_SAMELAST) Pres = V3d_SIMPLE;
227 Handle(Graphic3d_Structure) slight = new Graphic3d_Structure(TheViewer->Viewer());
228 MyGraphicStructure = slight;
229 Handle(Graphic3d_Structure) snopick = new Graphic3d_Structure(TheViewer->Viewer());
230 MyGraphicStructure1 = snopick;
231 }
232
b64d84be 233 Handle(Graphic3d_Group) gradius, gExtArrow, gIntArrow;
234 if (Pres == V3d_COMPLETE)
235 {
236 gradius = MyGraphicStructure->NewGroup();
237 gExtArrow = MyGraphicStructure->NewGroup();
238 gIntArrow = MyGraphicStructure->NewGroup();
7fd59977 239 }
b64d84be 240 Handle(Graphic3d_Group) glight = MyGraphicStructure->NewGroup();
7fd59977 241 Handle(Graphic3d_Group) gsphere;
b64d84be 242 if (Pres == V3d_COMPLETE
243 || Pres == V3d_PARTIAL)
244 {
245 gsphere = MyGraphicStructure->NewGroup();
246 }
7fd59977 247
b64d84be 248 Handle(Graphic3d_Group) gnopick = MyGraphicStructure1->NewGroup();
7fd59977 249
250 X0 = MyTarget.X();
251 Y0 = MyTarget.Y();
252 Z0 = MyTarget.Z();
253
81bba717 254//Display of the position of the light.
7fd59977 255
7fd59977 256 this->Color(Quantity_TOC_RGB,R1,G1,B1);
257 Quantity_Color Col1(R1,G1,B1,Quantity_TOC_RGB);
258 Handle(Graphic3d_AspectLine3d) Asp1 = new Graphic3d_AspectLine3d();
259 Asp1->SetColor(Col1);
260 glight->SetPrimitivesAspect(Asp1);
261 this->Symbol(glight,aView);
262
81bba717 263// Display of the reference sphere (limited by circle).
7fd59977 264
265 if (Pres == V3d_COMPLETE || Pres == V3d_PARTIAL) {
266
267 Rayon = this->Radius();
268 aView->Proj(VX,VY,VZ);
7fd59977 269 V3d::CircleInPlane(gsphere,X0,Y0,Z0,VX,VY,VZ,Rayon);
270
81bba717 271// Display of the radius of the sphere (line + text)
7fd59977 272
273 if (Pres == V3d_COMPLETE) {
7fd59977 274 this->Position(X,Y,Z);
b8ddfc2f 275 Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(2);
276 aPrims->AddVertex(X0,Y0,Z0);
277 aPrims->AddVertex(X,Y,Z);
278 gnopick->AddPrimitiveArray(aPrims);
279 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.);
280 V3d::ArrowOfRadius(gIntArrow,X0,Y0,Z0,X0-X,Y0-Y,Z0-Z,M_PI/15.,Rayon/20.);
7fd59977 281 TCollection_AsciiString ValOfRadius(Rayon);
b8ddfc2f 282 PText.SetCoord( .5*(X0+X), .5*(Y0+Y), .5*(Z0+Z) );
7fd59977 283 gradius->Text(ValOfRadius.ToCString(),PText,0.01);
284 }
285
81bba717 286// Display of the meridian
7fd59977 287
288 Quantity_Color Col2(Quantity_NOC_GREEN);
b8ddfc2f 289 Handle(Graphic3d_AspectLine3d) Asp2 = new Graphic3d_AspectLine3d(Col2,Aspect_TOL_SOLID,1.);
7fd59977 290 gnopick->SetPrimitivesAspect(Asp2);
291
81bba717 292// Definition of the axis of the circle
7fd59977 293 aView->Up(DXRef,DYRef,DZRef);
294 this->Position(X,Y,Z);
295 DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
296 VX = DYRef*DZini - DZRef*DYini;
297 VY = DZRef*DXini - DXRef*DZini;
298 VZ = DXRef*DYini - DYRef*DXini;
299
300 V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
301
81bba717 302// Display of the parallel
7fd59977 303
81bba717 304// Definition of the axis of the circle
7fd59977 305 aView->Proj(VX,VY,VZ);
306 aView->Up(X1,Y1,Z1);
307 DXRef = VY * Z1 - VZ * Y1;
308 DYRef = VZ * X1 - VX * Z1;
309 DZRef = VX * Y1 - VY * X1;
310 this->Position(X,Y,Z);
311 DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
312 VX = DYRef*DZini - DZRef*DYini;
313 VY = DZRef*DXini - DXRef*DZini;
314 VZ = DXRef*DYini - DYRef*DXini;
315
316 V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
7fd59977 317 }
318
319 MyGraphicStructure->Connect(MyGraphicStructure1,Graphic3d_TOC_DESCENDANT);
320 MyTypeOfRepresentation = Pres;
321 MyGraphicStructure->Display();
322 TheViewer->SetUpdateMode(UpdSov);
323}