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