0028726: Quantity_NameOfColor should be replaced by Quantity_Color in function input...
[occt.git] / src / V3d / V3d_PositionLight.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/***********************************************************************
7fd59977 15 V3d_PositionLight.cxx
16 Created: 30-03-98 ZOV (ZELENKOV Oleg)
7fd59977 17************************************************************************/
18
42cf5bc1 19#include <gp_Ax1.hxx>
20#include <gp_Dir.hxx>
21#include <gp_Pnt.hxx>
22#include <gp_Trsf.hxx>
23#include <gp_Vec.hxx>
b8ddfc2f 24#include <Graphic3d_ArrayOfSegments.hxx>
7fd59977 25#include <Graphic3d_AspectLine3d.hxx>
42cf5bc1 26#include <Graphic3d_AspectMarker3d.hxx>
7fd59977 27#include <Graphic3d_AspectText3d.hxx>
42cf5bc1 28#include <Graphic3d_Group.hxx>
29#include <Graphic3d_Structure.hxx>
30#include <Graphic3d_Vector.hxx>
31#include <Graphic3d_Vertex.hxx>
32#include <Standard_Type.hxx>
33#include <TCollection_AsciiString.hxx>
34#include <V3d.hxx>
35#include <V3d_BadValue.hxx>
36#include <V3d_PositionLight.hxx>
7fd59977 37#include <V3d_SpotLight.hxx>
42cf5bc1 38#include <V3d_View.hxx>
39#include <V3d_Viewer.hxx>
7fd59977 40
92efcf78 41IMPLEMENT_STANDARD_RTTIEXT(V3d_PositionLight,V3d_Light)
42
c357e426 43// =======================================================================
44// function : V3d_PositionLight
45// purpose :
46// =======================================================================
47V3d_PositionLight::V3d_PositionLight (const Handle(V3d_Viewer)& theViewer)
48: V3d_Light(theViewer)
49{
7fd59977 50}
51
c357e426 52// =======================================================================
53// function : SetTarget
54// purpose :
55// =======================================================================
56void V3d_PositionLight::SetTarget (const Standard_Real theX, const Standard_Real theY, const Standard_Real theZ)
57{
7fd59977 58 Standard_Real Xc,Yc,Zc, Xp,Yp,Zp;
59
81bba717 60 // Recalculation of the position
c357e426 61 myTarget.Coord(Xc,Yc,Zc);
7fd59977 62 Position (Xp,Yp,Zp) ;
63
c357e426 64 Xp = Xp + (theX - Xc);
65 Yp = Yp + (theY - Yc);
66 Zp = Zp + (theZ - Zc);
7fd59977 67
68 // Affectation
c357e426 69 myTarget.SetCoord(theX,theY,theZ);
7fd59977 70 SetPosition(Xp,Yp,Zp) ;
71}
72
c357e426 73// =======================================================================
74// function : SetRadius
75// purpose :
76// =======================================================================
77void V3d_PositionLight::SetRadius (const Standard_Real theRadius)
78{
79 V3d_BadValue_Raise_if( theRadius <= 0. , "V3d_PositionLight::SetRadius, bad radius");
80 V3d_BadValue_Raise_if( Type() == V3d_DIRECTIONAL , "V3d_PositionLight::SetRadius, bad light type");
7fd59977 81
82 Standard_Real X0,Y0,Z0, Xn,Yn,Zn, Xp,Yp,Zp;
83
81bba717 84 // The target point remains unchanged, only the position of the light is modified
85 // by preserving the direction.
7fd59977 86 Position (Xp,Yp,Zp);
c357e426 87 Graphic3d_Vector D(myTarget, Graphic3d_Vertex(Xp, Yp, Zp));
7fd59977 88 D.Normalize();
89 D.Coord(Xn,Yn,Zn);
c357e426 90 myTarget.Coord(X0,Y0,Z0);
91 Xn = X0 + theRadius*Xn;
92 Yn = Y0 + theRadius*Yn;
93 Zn = Z0 + theRadius*Zn;
7fd59977 94
95 SetPosition(Xn,Yn,Zn) ;
96}
97
c357e426 98// =======================================================================
99// function : OnHideFace
100// purpose :
101// =======================================================================
102void V3d_PositionLight::OnHideFace (const Handle(V3d_View)& theView)
103{
7fd59977 104 Standard_Real Xp,Yp,Zp, X,Y,Z, VX,VY,VZ;
105
106 Position (Xp,Yp,Zp);
c357e426 107 V3d_Light::SymetricPointOnSphere (theView,
108 myTarget, Graphic3d_Vertex(Xp,Yp,Yp), Radius(), X,Y,Z, VX,VY,VZ);
7fd59977 109
81bba717 110 // This is a visible point
7fd59977 111 if ((VX*(X-Xp) < 0.) && (VY*(Y-Yp) < 0.) && (VZ*(Z-Zp) < 0.))
112 SetPosition (X,Y,Z);
113}
114
c357e426 115// =======================================================================
116// function : OnSeeFace
117// purpose :
118// =======================================================================
119void V3d_PositionLight::OnSeeFace (const Handle(V3d_View)& theView)
120{
7fd59977 121 Standard_Real Xp,Yp,Zp, X,Y,Z, VX,VY,VZ;
122
123 Position (Xp,Yp,Zp);
c357e426 124 V3d_Light::SymetricPointOnSphere (theView,
125 myTarget, Graphic3d_Vertex(Xp,Yp,Yp), Radius(), X,Y,Z, VX,VY,VZ);
7fd59977 126
81bba717 127 // This is a hidden point
7fd59977 128 if ((VX*(X-Xp) > 0.) && (VY*(Y-Yp) > 0.) && (VZ*(Z-Zp) > 0.))
129 SetPosition (X,Y,Z);
130}
131
c357e426 132// =======================================================================
133// function : SeeOrHide
134// purpose :
135// =======================================================================
136Standard_Boolean V3d_PositionLight::SeeOrHide (const Handle(V3d_View)& theView) const
137{
7fd59977 138 Standard_Real Xp,Yp,Zp, X,Y,Z, VX,VY,VZ;
139
140 Position (Xp,Yp,Zp);
c357e426 141 V3d_Light::SymetricPointOnSphere (theView,
142 myTarget, Graphic3d_Vertex(Xp,Yp,Yp), Radius(), X,Y,Z, VX,VY,VZ);
7fd59977 143
81bba717 144 // Is it a visible or a hidden point
7fd59977 145 return ( (VX*(X-Xp) > 0.) || (VY*(Y-Yp) > 0.) || (VZ*(Z-Zp) > 0.) )?
81bba717 146 // the source is on the hidden face
7fd59977 147 Standard_False:
81bba717 148 // the source is on the visible face.
7fd59977 149 Standard_True;
150}
151
c357e426 152// =======================================================================
153// function : Target
154// purpose :
155// =======================================================================
156void V3d_PositionLight::Target (Standard_Real& theXp, Standard_Real& theYp, Standard_Real& theZp) const
157{
158 myTarget.Coord (theXp, theYp, theZp);
7fd59977 159}
160
c357e426 161// =======================================================================
162// function : Display
163// purpose :
164// =======================================================================
165void V3d_PositionLight::Display (const Handle(V3d_View)& theView, const V3d_TypeOfRepresentation theTPres)
b8ddfc2f 166{
7fd59977 167 Graphic3d_Vertex PText ;
168 Standard_Real X,Y,Z,Rayon;
169 Standard_Real X0,Y0,Z0,VX,VY,VZ;
170 Standard_Real X1,Y1,Z1;
171 Standard_Real DXRef,DYRef,DZRef,DXini,DYini,DZini;
7fd59977 172 V3d_TypeOfRepresentation Pres;
7fd59977 173
81bba717 174// Creation of a structure of markable elements (position of the
175// light, and the domain of lighting represented by a circle)
176// Creation of a structure snopick of non-markable elements (target, meridian and
177// parallel).
178
c357e426 179 Pres = theTPres;
180 Handle(V3d_Viewer) TheViewer = theView->Viewer();
c357e426 181 if (!myGraphicStructure.IsNull()) {
182 myGraphicStructure->Disconnect(myGraphicStructure1);
183 myGraphicStructure->Clear();
184 myGraphicStructure1->Clear();
185 if (Pres == V3d_SAMELAST) Pres = myTypeOfRepresentation;
b8ddfc2f 186 }
187 else {
188 if (Pres == V3d_SAMELAST) Pres = V3d_SIMPLE;
c357e426 189 Handle(Graphic3d_Structure) slight = new Graphic3d_Structure(TheViewer->StructureManager());
190 myGraphicStructure = slight;
191 Handle(Graphic3d_Structure) snopick = new Graphic3d_Structure(TheViewer->StructureManager());
192 myGraphicStructure1 = snopick;
b8ddfc2f 193 }
194
b64d84be 195 Handle(Graphic3d_Group) gradius, gExtArrow, gIntArrow;
c357e426 196 if (Type() != V3d_DIRECTIONAL
b64d84be 197 && Pres == V3d_COMPLETE)
198 {
c357e426 199 gradius = myGraphicStructure->NewGroup();
200 gExtArrow = myGraphicStructure->NewGroup();
201 gIntArrow = myGraphicStructure->NewGroup();
b8ddfc2f 202 }
c357e426 203 Handle(Graphic3d_Group) glight = myGraphicStructure->NewGroup();
7fd59977 204 Handle(Graphic3d_Group) gsphere;
b64d84be 205 if (Pres == V3d_COMPLETE
206 || Pres == V3d_PARTIAL)
207 {
c357e426 208 gsphere = myGraphicStructure->NewGroup();
b64d84be 209 }
7fd59977 210
c357e426 211 Handle(Graphic3d_Group) gnopick = myGraphicStructure1->NewGroup();
7fd59977 212
c357e426 213 X0 = myTarget.X();
214 Y0 = myTarget.Y();
215 Z0 = myTarget.Z();
7fd59977 216
81bba717 217// Display of the light position.
7fd59977 218
87432b82 219 const Quantity_Color Col1 = this->Color();
7fd59977 220 Handle(Graphic3d_AspectLine3d) Asp1 = new Graphic3d_AspectLine3d();
221 Asp1->SetColor(Col1);
222 glight->SetPrimitivesAspect(Asp1);
c357e426 223 this->Symbol(glight,theView);
7fd59977 224
81bba717 225// Display of the marking sphere (limit at the circle).
7fd59977 226
227 if (Pres == V3d_COMPLETE || Pres == V3d_PARTIAL) {
228
229 Rayon = this->Radius();
c357e426 230 theView->Proj(VX,VY,VZ);
7fd59977 231 V3d::CircleInPlane(gsphere,X0,Y0,Z0,VX,VY,VZ,Rayon);
232
c357e426 233 if (Type() != V3d_DIRECTIONAL) {
7fd59977 234
81bba717 235 //Display of the radius of the sphere (line + text)
7fd59977 236
b8ddfc2f 237 if (Pres == V3d_COMPLETE) {
238 this->Position(X,Y,Z);
239 Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(2);
240 aPrims->AddVertex(X0,Y0,Z0);
241 aPrims->AddVertex(X,Y,Z);
242 gnopick->AddPrimitiveArray(aPrims);
243 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.);
244 V3d::ArrowOfRadius(gIntArrow,X0,Y0,Z0,X0-X,Y0-Y,Z0-Z,M_PI/15.,Rayon/20.);
245 TCollection_AsciiString ValOfRadius(Rayon);
246 PText.SetCoord( .5*(X0+X), .5*(Y0+Y), .5*(Z0+Z) );
247 gradius->Text(ValOfRadius.ToCString(),PText,0.01);
248 }
249 }
7fd59977 250
81bba717 251 // Display of the meridian
7fd59977 252
253 Quantity_Color Col2(Quantity_NOC_GREEN);
b8ddfc2f 254 Handle(Graphic3d_AspectLine3d) Asp2 = new Graphic3d_AspectLine3d(Col2,Aspect_TOL_SOLID,1.);
7fd59977 255 gnopick->SetPrimitivesAspect(Asp2);
256
81bba717 257 // Definition of the axis of circle
c357e426 258 theView->Up(DXRef,DYRef,DZRef);
7fd59977 259 this->Position(X,Y,Z);
260 DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
261 VX = DYRef*DZini - DZRef*DYini;
262 VY = DZRef*DXini - DXRef*DZini;
263 VZ = DXRef*DYini - DYRef*DXini;
264
265 V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
266
81bba717 267 // Display of the parallel
7fd59977 268
81bba717 269 // Definition of the axis of circle
c357e426 270 theView->Proj(VX,VY,VZ);
271 theView->Up(X1,Y1,Z1);
7fd59977 272 DXRef = VY * Z1 - VZ * Y1;
273 DYRef = VZ * X1 - VX * Z1;
274 DZRef = VX * Y1 - VY * X1;
275 this->Position(X,Y,Z);
276 DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
277 VX = DYRef*DZini - DZRef*DYini;
278 VY = DZRef*DXini - DXRef*DZini;
279 VZ = DXRef*DYini - DYRef*DXini;
280
281 V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
282 }
283
c357e426 284 myGraphicStructure->Connect(myGraphicStructure1,Graphic3d_TOC_DESCENDANT);
285 myTypeOfRepresentation = Pres;
286 myGraphicStructure->Display();
7fd59977 287}
288
c357e426 289// =======================================================================
290// function : Display
291// purpose :
292// =======================================================================
293void V3d_PositionLight::Tracking (const Handle(V3d_View)& theView,
294 const V3d_TypeOfPickLight theWhatPick,
295 const Standard_Integer theXpix,
296 const Standard_Integer theYpix)
297{
7fd59977 298// Quantity_Color Col ;
299 Standard_Real xPos, yPos, zPos;
300 Standard_Real XPp,YPp,PXT,PYT,X,Y,Z,Rayon,Ylim;
301 Standard_Real XMinTrack,XMaxTrack,YMinTrack,YMaxTrack;
302 Standard_Real XT,YT,ZT,X0,Y0,Z0,XP,YP,ZP,VX,VY,VZ,A,B,C,Delta;
303 Standard_Real DX,DY,PXP,PYP,Xproj,Yproj;
304 Standard_Real A1,A2,B1,B2,Rap,OldRprj,NewRprj;
305 Standard_Real Xi,Yi,Zi,DeltaX,DeltaY,DeltaZ,Lambda;
306 Standard_Integer IPX,IPY;
307
c357e426 308 theView->Convert(theXpix,theYpix,XPp,YPp);
309 X0 = myTarget.X();
310 Y0 = myTarget.Y();
311 Z0 = myTarget.Z();
312 theView->Project(X0,Y0,Z0,PXT,PYT);
313 theView->Convert(PXT,PYT,IPX,IPY);
81bba717 314// Coord 3d in the plane of projection of the target.
c357e426 315 theView->Convert(IPX,IPY,XT,YT,ZT);
316 switch (theWhatPick) {
7fd59977 317 case V3d_POSITIONLIGHT :
81bba717 318 // The Coordinates should remain inside of the sphere
7fd59977 319 Rayon = Radius();
320 XMinTrack = PXT - Rayon;
321 XMaxTrack = PXT + Rayon;
322 Ylim = Sqrt( Square(Rayon) - Square(XPp - PXT) );
323 YMinTrack = PYT - Ylim;
324 YMaxTrack = PYT + Ylim;
325 if (XPp >= XMinTrack && XPp <= XMaxTrack) {
326 if (YPp >= YMinTrack && YPp <= YMaxTrack) {
c357e426 327 theView->ProjReferenceAxe(theXpix,theYpix,XP,YP,ZP,VX,VY,VZ);
7fd59977 328 DeltaX = X0 - XP;
329 DeltaY = Y0 - YP;
330 DeltaZ = Z0 - ZP;
331
81bba717 332// The point of intersection of straight lines defined by :
333// - Straight line passing by the point of projection and the eye
334// if this is a perspective, parralel to the normal of the view
335// if this is an axonometric view.
336// position in the view is parallel to the normal of the view
337// - The distance position of the target camera is equal to the radius.
7fd59977 338
339 A = VX*VX + VY*VY + VZ*VZ ;
340 B = -2. * (VX*DeltaX + VY*DeltaY + VZ*DeltaZ);
341 C = DeltaX*DeltaX + DeltaY*DeltaY + DeltaZ*DeltaZ
342 - Rayon*Rayon ;
343 Delta = B*B - 4.*A*C;
344 if ( Delta >= 0 ) {
345 Lambda = (-B + Sqrt(Delta))/(2.*A);
346 X = XP + Lambda*VX;
347 Y = YP + Lambda*VY;
348 Z = ZP + Lambda*VZ;
349 SetPosition(X,Y,Z);
350
c357e426 351 if (Type() == V3d_SPOT)
7fd59977 352 ((V3d_SpotLight*)this)->SetDirection(X0-X,Y0-Y,Z0-Z);
353
c357e426 354 Display(theView,myTypeOfRepresentation);
355 (theView->Viewer())->UpdateLights();
7fd59977 356 }
357 }
358 }
359 break;
360
361 case V3d_SPACELIGHT :
c357e426 362 theView->Convert(PXT,PYT,IPX,IPY);
81bba717 363// In this case Xpix,Ypix correspond to a distance, relative
364// to the translation that is planned to be performed on the sphere.
c357e426 365 theView->Convert(IPX+theXpix,IPY+theYpix,X,Y,Z);
7fd59977 366 X = X+X0-XT;
367 Y = Y+Y0-YT;
368 Z = Z+Z0-ZT;
369 SetTarget(X,Y,Z);
c357e426 370 Display(theView,myTypeOfRepresentation);
371 (theView->Viewer())->UpdateLights();
7fd59977 372 break;
373
374 case V3d_ExtRADIUSLIGHT :
c357e426 375 if (Type() == V3d_DIRECTIONAL)
7fd59977 376 break;
81bba717 377// it is attempted to preserve the target direction position of the
378// source ==> the point is projected on the target source direction.
7fd59977 379 this->Position(Xi,Yi,Zi);
c357e426 380 theView->Project(Xi,Yi,Zi,PXP,PYP);
7fd59977 381 DX = PXP - PXT;
382 DY = PYP - PYT;
383 A1 = DY/DX ; B1 = PYT - A1*PXT;
384 A2 = -DX/DY; B2 = YPp - A2*XPp;
385 Xproj = (B2 - B1) / (A1 - A2);
386 Yproj = A1*Xproj + B1;
387 if ( (DX*(Xproj-PXT) > 0.) && (DY*(Yproj-PYT) > 0.) ) {
388 OldRprj = Sqrt ( Square (PXP-PXT) + Square (PYP-PYT) );
389 NewRprj = Sqrt ( Square (Xproj-PXT) + Square (Yproj-PYT) );
390 Rap = NewRprj/OldRprj;
391 Rayon = Radius();
392 Rayon = Rayon * Rap;
393 SetRadius(Rayon);
c357e426 394 Display(theView,myTypeOfRepresentation);
395 (theView->Viewer())->UpdateLights();
7fd59977 396 }
397 break;
398
399 case V3d_IntRADIUSLIGHT :
c357e426 400 if (Type() == V3d_DIRECTIONAL)
7fd59977 401 break;
81bba717 402// it is attempted to preserve the target direction position of the
403// source ==> the point is projected on the target source direction.
7fd59977 404 Position(Xi,Yi,Zi);
c357e426 405 theView->Project(Xi,Yi,Zi,PXP,PYP);
7fd59977 406 DX = PXP - PXT;
407 DY = PYP - PYT;
408 A1 = DY/DX ; B1 = PYT - A1*PXT;
409 A2 = -DX/DY; B2 = YPp - A2*XPp;
410 Xproj = (B2 - B1) / (A1 - A2);
411 Yproj = A1*Xproj + B1;
412 if ( (DX*(Xproj-PXP) < 0.) && (DY*(Yproj-PYP) < 0.) ) {
413 OldRprj = Sqrt ( Square (PXP-PXT) + Square (PYP-PYT) );
414 NewRprj = Sqrt ( Square (Xproj-PXP) + Square (Yproj-PYP) );
415 Rap = NewRprj/OldRprj;
416 Rayon = Radius();
417 Rayon = Rayon * Rap;
81bba717 418// the source should remain at a fixed position,
419// only the target is modified.
7fd59977 420 Position (xPos, yPos, zPos);
c357e426 421 Graphic3d_Vector Dir(Graphic3d_Vertex(xPos,yPos,zPos), myTarget);
7fd59977 422 Dir.Normalize();
423 Dir.Coord(X,Y,Z);
424 X = Xi + Rayon*X;
425 Y = Yi + Rayon*Y;
426 Z = Zi + Rayon*Z;
81bba717 427// the source should remain at a fixed position,
428// only the target is modified.
c357e426 429 myTarget.SetCoord(X,Y,Z);
430 Display(theView,myTypeOfRepresentation);
431 (theView->Viewer())->UpdateLights();
7fd59977 432 }
433 break;
434
435 case V3d_RADIUSTEXTLIGHT :
436 break;
437
438 case V3d_NOTHING :
439 break;
440 }
441}
442
c357e426 443// =======================================================================
444// function : Radius
445// purpose :
446// =======================================================================
447Standard_Real V3d_PositionLight::Radius() const
448{
7fd59977 449 Standard_Real Xp,Yp,Zp, Xc,Yc,Zc;
450
451 Position (Xp,Yp,Zp);
c357e426 452 myTarget.Coord(Xc,Yc,Zc);
7fd59977 453
454 return Sqrt (Square(Xc - Xp) + Square(Yc - Yp) + Square(Zc - Zp));
455}
456
c357e426 457// =======================================================================
458// function : Radius
459// purpose :
460// =======================================================================
461void V3d_PositionLight::Erase()
462{
463 if (!myGraphicStructure.IsNull()) myGraphicStructure->Erase();
464 if (!myGraphicStructure1.IsNull()) myGraphicStructure1->Erase();
7fd59977 465}
466