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