Warnings on vc14 were eliminated
[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;
172 Standard_Real R1,G1,B1;
173 V3d_TypeOfRepresentation Pres;
7fd59977 174
81bba717 175// Creation of a structure of markable elements (position of the
176// light, and the domain of lighting represented by a circle)
177// Creation of a structure snopick of non-markable elements (target, meridian and
178// parallel).
179
c357e426 180 Pres = theTPres;
181 Handle(V3d_Viewer) TheViewer = theView->Viewer();
c357e426 182 if (!myGraphicStructure.IsNull()) {
183 myGraphicStructure->Disconnect(myGraphicStructure1);
184 myGraphicStructure->Clear();
185 myGraphicStructure1->Clear();
186 if (Pres == V3d_SAMELAST) Pres = myTypeOfRepresentation;
b8ddfc2f 187 }
188 else {
189 if (Pres == V3d_SAMELAST) Pres = V3d_SIMPLE;
c357e426 190 Handle(Graphic3d_Structure) slight = new Graphic3d_Structure(TheViewer->StructureManager());
191 myGraphicStructure = slight;
192 Handle(Graphic3d_Structure) snopick = new Graphic3d_Structure(TheViewer->StructureManager());
193 myGraphicStructure1 = snopick;
b8ddfc2f 194 }
195
b64d84be 196 Handle(Graphic3d_Group) gradius, gExtArrow, gIntArrow;
c357e426 197 if (Type() != V3d_DIRECTIONAL
b64d84be 198 && Pres == V3d_COMPLETE)
199 {
c357e426 200 gradius = myGraphicStructure->NewGroup();
201 gExtArrow = myGraphicStructure->NewGroup();
202 gIntArrow = myGraphicStructure->NewGroup();
b8ddfc2f 203 }
c357e426 204 Handle(Graphic3d_Group) glight = myGraphicStructure->NewGroup();
7fd59977 205 Handle(Graphic3d_Group) gsphere;
b64d84be 206 if (Pres == V3d_COMPLETE
207 || Pres == V3d_PARTIAL)
208 {
c357e426 209 gsphere = myGraphicStructure->NewGroup();
b64d84be 210 }
7fd59977 211
c357e426 212 Handle(Graphic3d_Group) gnopick = myGraphicStructure1->NewGroup();
7fd59977 213
c357e426 214 X0 = myTarget.X();
215 Y0 = myTarget.Y();
216 Z0 = myTarget.Z();
7fd59977 217
81bba717 218// Display of the light position.
7fd59977 219
7fd59977 220 this->Color(Quantity_TOC_RGB,R1,G1,B1);
221 Quantity_Color Col1(R1,G1,B1,Quantity_TOC_RGB);
222 Handle(Graphic3d_AspectLine3d) Asp1 = new Graphic3d_AspectLine3d();
223 Asp1->SetColor(Col1);
224 glight->SetPrimitivesAspect(Asp1);
c357e426 225 this->Symbol(glight,theView);
7fd59977 226
81bba717 227// Display of the marking sphere (limit at the circle).
7fd59977 228
229 if (Pres == V3d_COMPLETE || Pres == V3d_PARTIAL) {
230
231 Rayon = this->Radius();
c357e426 232 theView->Proj(VX,VY,VZ);
7fd59977 233 V3d::CircleInPlane(gsphere,X0,Y0,Z0,VX,VY,VZ,Rayon);
234
c357e426 235 if (Type() != V3d_DIRECTIONAL) {
7fd59977 236
81bba717 237 //Display of the radius of the sphere (line + text)
7fd59977 238
b8ddfc2f 239 if (Pres == V3d_COMPLETE) {
240 this->Position(X,Y,Z);
241 Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(2);
242 aPrims->AddVertex(X0,Y0,Z0);
243 aPrims->AddVertex(X,Y,Z);
244 gnopick->AddPrimitiveArray(aPrims);
245 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.);
246 V3d::ArrowOfRadius(gIntArrow,X0,Y0,Z0,X0-X,Y0-Y,Z0-Z,M_PI/15.,Rayon/20.);
247 TCollection_AsciiString ValOfRadius(Rayon);
248 PText.SetCoord( .5*(X0+X), .5*(Y0+Y), .5*(Z0+Z) );
249 gradius->Text(ValOfRadius.ToCString(),PText,0.01);
250 }
251 }
7fd59977 252
81bba717 253 // Display of the meridian
7fd59977 254
255 Quantity_Color Col2(Quantity_NOC_GREEN);
b8ddfc2f 256 Handle(Graphic3d_AspectLine3d) Asp2 = new Graphic3d_AspectLine3d(Col2,Aspect_TOL_SOLID,1.);
7fd59977 257 gnopick->SetPrimitivesAspect(Asp2);
258
81bba717 259 // Definition of the axis of circle
c357e426 260 theView->Up(DXRef,DYRef,DZRef);
7fd59977 261 this->Position(X,Y,Z);
262 DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
263 VX = DYRef*DZini - DZRef*DYini;
264 VY = DZRef*DXini - DXRef*DZini;
265 VZ = DXRef*DYini - DYRef*DXini;
266
267 V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
268
81bba717 269 // Display of the parallel
7fd59977 270
81bba717 271 // Definition of the axis of circle
c357e426 272 theView->Proj(VX,VY,VZ);
273 theView->Up(X1,Y1,Z1);
7fd59977 274 DXRef = VY * Z1 - VZ * Y1;
275 DYRef = VZ * X1 - VX * Z1;
276 DZRef = VX * Y1 - VY * X1;
277 this->Position(X,Y,Z);
278 DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
279 VX = DYRef*DZini - DZRef*DYini;
280 VY = DZRef*DXini - DXRef*DZini;
281 VZ = DXRef*DYini - DYRef*DXini;
282
283 V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
284 }
285
c357e426 286 myGraphicStructure->Connect(myGraphicStructure1,Graphic3d_TOC_DESCENDANT);
287 myTypeOfRepresentation = Pres;
288 myGraphicStructure->Display();
7fd59977 289}
290
c357e426 291// =======================================================================
292// function : Display
293// purpose :
294// =======================================================================
295void V3d_PositionLight::Tracking (const Handle(V3d_View)& theView,
296 const V3d_TypeOfPickLight theWhatPick,
297 const Standard_Integer theXpix,
298 const Standard_Integer theYpix)
299{
7fd59977 300// Quantity_Color Col ;
301 Standard_Real xPos, yPos, zPos;
302 Standard_Real XPp,YPp,PXT,PYT,X,Y,Z,Rayon,Ylim;
303 Standard_Real XMinTrack,XMaxTrack,YMinTrack,YMaxTrack;
304 Standard_Real XT,YT,ZT,X0,Y0,Z0,XP,YP,ZP,VX,VY,VZ,A,B,C,Delta;
305 Standard_Real DX,DY,PXP,PYP,Xproj,Yproj;
306 Standard_Real A1,A2,B1,B2,Rap,OldRprj,NewRprj;
307 Standard_Real Xi,Yi,Zi,DeltaX,DeltaY,DeltaZ,Lambda;
308 Standard_Integer IPX,IPY;
309
c357e426 310 theView->Convert(theXpix,theYpix,XPp,YPp);
311 X0 = myTarget.X();
312 Y0 = myTarget.Y();
313 Z0 = myTarget.Z();
314 theView->Project(X0,Y0,Z0,PXT,PYT);
315 theView->Convert(PXT,PYT,IPX,IPY);
81bba717 316// Coord 3d in the plane of projection of the target.
c357e426 317 theView->Convert(IPX,IPY,XT,YT,ZT);
318 switch (theWhatPick) {
7fd59977 319 case V3d_POSITIONLIGHT :
81bba717 320 // The Coordinates should remain inside of the sphere
7fd59977 321 Rayon = Radius();
322 XMinTrack = PXT - Rayon;
323 XMaxTrack = PXT + Rayon;
324 Ylim = Sqrt( Square(Rayon) - Square(XPp - PXT) );
325 YMinTrack = PYT - Ylim;
326 YMaxTrack = PYT + Ylim;
327 if (XPp >= XMinTrack && XPp <= XMaxTrack) {
328 if (YPp >= YMinTrack && YPp <= YMaxTrack) {
c357e426 329 theView->ProjReferenceAxe(theXpix,theYpix,XP,YP,ZP,VX,VY,VZ);
7fd59977 330 DeltaX = X0 - XP;
331 DeltaY = Y0 - YP;
332 DeltaZ = Z0 - ZP;
333
81bba717 334// The point of intersection of straight lines defined by :
335// - Straight line passing by the point of projection and the eye
336// if this is a perspective, parralel to the normal of the view
337// if this is an axonometric view.
338// position in the view is parallel to the normal of the view
339// - The distance position of the target camera is equal to the radius.
7fd59977 340
341 A = VX*VX + VY*VY + VZ*VZ ;
342 B = -2. * (VX*DeltaX + VY*DeltaY + VZ*DeltaZ);
343 C = DeltaX*DeltaX + DeltaY*DeltaY + DeltaZ*DeltaZ
344 - Rayon*Rayon ;
345 Delta = B*B - 4.*A*C;
346 if ( Delta >= 0 ) {
347 Lambda = (-B + Sqrt(Delta))/(2.*A);
348 X = XP + Lambda*VX;
349 Y = YP + Lambda*VY;
350 Z = ZP + Lambda*VZ;
351 SetPosition(X,Y,Z);
352
c357e426 353 if (Type() == V3d_SPOT)
7fd59977 354 ((V3d_SpotLight*)this)->SetDirection(X0-X,Y0-Y,Z0-Z);
355
c357e426 356 Display(theView,myTypeOfRepresentation);
357 (theView->Viewer())->UpdateLights();
7fd59977 358 }
359 }
360 }
361 break;
362
363 case V3d_SPACELIGHT :
c357e426 364 theView->Convert(PXT,PYT,IPX,IPY);
81bba717 365// In this case Xpix,Ypix correspond to a distance, relative
366// to the translation that is planned to be performed on the sphere.
c357e426 367 theView->Convert(IPX+theXpix,IPY+theYpix,X,Y,Z);
7fd59977 368 X = X+X0-XT;
369 Y = Y+Y0-YT;
370 Z = Z+Z0-ZT;
371 SetTarget(X,Y,Z);
c357e426 372 Display(theView,myTypeOfRepresentation);
373 (theView->Viewer())->UpdateLights();
7fd59977 374 break;
375
376 case V3d_ExtRADIUSLIGHT :
c357e426 377 if (Type() == V3d_DIRECTIONAL)
7fd59977 378 break;
81bba717 379// it is attempted to preserve the target direction position of the
380// source ==> the point is projected on the target source direction.
7fd59977 381 this->Position(Xi,Yi,Zi);
c357e426 382 theView->Project(Xi,Yi,Zi,PXP,PYP);
7fd59977 383 DX = PXP - PXT;
384 DY = PYP - PYT;
385 A1 = DY/DX ; B1 = PYT - A1*PXT;
386 A2 = -DX/DY; B2 = YPp - A2*XPp;
387 Xproj = (B2 - B1) / (A1 - A2);
388 Yproj = A1*Xproj + B1;
389 if ( (DX*(Xproj-PXT) > 0.) && (DY*(Yproj-PYT) > 0.) ) {
390 OldRprj = Sqrt ( Square (PXP-PXT) + Square (PYP-PYT) );
391 NewRprj = Sqrt ( Square (Xproj-PXT) + Square (Yproj-PYT) );
392 Rap = NewRprj/OldRprj;
393 Rayon = Radius();
394 Rayon = Rayon * Rap;
395 SetRadius(Rayon);
c357e426 396 Display(theView,myTypeOfRepresentation);
397 (theView->Viewer())->UpdateLights();
7fd59977 398 }
399 break;
400
401 case V3d_IntRADIUSLIGHT :
c357e426 402 if (Type() == V3d_DIRECTIONAL)
7fd59977 403 break;
81bba717 404// it is attempted to preserve the target direction position of the
405// source ==> the point is projected on the target source direction.
7fd59977 406 Position(Xi,Yi,Zi);
c357e426 407 theView->Project(Xi,Yi,Zi,PXP,PYP);
7fd59977 408 DX = PXP - PXT;
409 DY = PYP - PYT;
410 A1 = DY/DX ; B1 = PYT - A1*PXT;
411 A2 = -DX/DY; B2 = YPp - A2*XPp;
412 Xproj = (B2 - B1) / (A1 - A2);
413 Yproj = A1*Xproj + B1;
414 if ( (DX*(Xproj-PXP) < 0.) && (DY*(Yproj-PYP) < 0.) ) {
415 OldRprj = Sqrt ( Square (PXP-PXT) + Square (PYP-PYT) );
416 NewRprj = Sqrt ( Square (Xproj-PXP) + Square (Yproj-PYP) );
417 Rap = NewRprj/OldRprj;
418 Rayon = Radius();
419 Rayon = Rayon * Rap;
81bba717 420// the source should remain at a fixed position,
421// only the target is modified.
7fd59977 422 Position (xPos, yPos, zPos);
c357e426 423 Graphic3d_Vector Dir(Graphic3d_Vertex(xPos,yPos,zPos), myTarget);
7fd59977 424 Dir.Normalize();
425 Dir.Coord(X,Y,Z);
426 X = Xi + Rayon*X;
427 Y = Yi + Rayon*Y;
428 Z = Zi + Rayon*Z;
81bba717 429// the source should remain at a fixed position,
430// only the target is modified.
c357e426 431 myTarget.SetCoord(X,Y,Z);
432 Display(theView,myTypeOfRepresentation);
433 (theView->Viewer())->UpdateLights();
7fd59977 434 }
435 break;
436
437 case V3d_RADIUSTEXTLIGHT :
438 break;
439
440 case V3d_NOTHING :
441 break;
442 }
443}
444
c357e426 445// =======================================================================
446// function : Radius
447// purpose :
448// =======================================================================
449Standard_Real V3d_PositionLight::Radius() const
450{
7fd59977 451 Standard_Real Xp,Yp,Zp, Xc,Yc,Zc;
452
453 Position (Xp,Yp,Zp);
c357e426 454 myTarget.Coord(Xc,Yc,Zc);
7fd59977 455
456 return Sqrt (Square(Xc - Xp) + Square(Yc - Yp) + Square(Zc - Zp));
457}
458
c357e426 459// =======================================================================
460// function : Radius
461// purpose :
462// =======================================================================
463void V3d_PositionLight::Erase()
464{
465 if (!myGraphicStructure.IsNull()) myGraphicStructure->Erase();
466 if (!myGraphicStructure1.IsNull()) myGraphicStructure1->Erase();
7fd59977 467}
468