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