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