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