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