1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
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.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 /***********************************************************************
16 Created: 30-03-98 ZOV (ZELENKOV Oleg)
17 ************************************************************************/
22 #include <gp_Trsf.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>
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>
41 IMPLEMENT_STANDARD_RTTIEXT(V3d_PositionLight,V3d_Light)
43 // =======================================================================
44 // function : V3d_PositionLight
46 // =======================================================================
47 V3d_PositionLight::V3d_PositionLight (const Handle(V3d_Viewer)& theViewer)
48 : V3d_Light(theViewer)
52 // =======================================================================
53 // function : SetTarget
55 // =======================================================================
56 void V3d_PositionLight::SetTarget (const Standard_Real theX, const Standard_Real theY, const Standard_Real theZ)
58 Standard_Real Xc,Yc,Zc, Xp,Yp,Zp;
60 // Recalculation of the position
61 myTarget.Coord(Xc,Yc,Zc);
64 Xp = Xp + (theX - Xc);
65 Yp = Yp + (theY - Yc);
66 Zp = Zp + (theZ - Zc);
69 myTarget.SetCoord(theX,theY,theZ);
70 SetPosition(Xp,Yp,Zp) ;
73 // =======================================================================
74 // function : SetRadius
76 // =======================================================================
77 void V3d_PositionLight::SetRadius (const Standard_Real theRadius)
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");
82 Standard_Real X0,Y0,Z0, Xn,Yn,Zn, Xp,Yp,Zp;
84 // The target point remains unchanged, only the position of the light is modified
85 // by preserving the direction.
87 Graphic3d_Vector D(myTarget, Graphic3d_Vertex(Xp, Yp, Zp));
90 myTarget.Coord(X0,Y0,Z0);
91 Xn = X0 + theRadius*Xn;
92 Yn = Y0 + theRadius*Yn;
93 Zn = Z0 + theRadius*Zn;
95 SetPosition(Xn,Yn,Zn) ;
98 // =======================================================================
99 // function : OnHideFace
101 // =======================================================================
102 void V3d_PositionLight::OnHideFace (const Handle(V3d_View)& theView)
104 Standard_Real Xp,Yp,Zp, X,Y,Z, VX,VY,VZ;
107 V3d_Light::SymetricPointOnSphere (theView,
108 myTarget, Graphic3d_Vertex(Xp,Yp,Yp), Radius(), X,Y,Z, VX,VY,VZ);
110 // This is a visible point
111 if ((VX*(X-Xp) < 0.) && (VY*(Y-Yp) < 0.) && (VZ*(Z-Zp) < 0.))
115 // =======================================================================
116 // function : OnSeeFace
118 // =======================================================================
119 void V3d_PositionLight::OnSeeFace (const Handle(V3d_View)& theView)
121 Standard_Real Xp,Yp,Zp, X,Y,Z, VX,VY,VZ;
124 V3d_Light::SymetricPointOnSphere (theView,
125 myTarget, Graphic3d_Vertex(Xp,Yp,Yp), Radius(), X,Y,Z, VX,VY,VZ);
127 // This is a hidden point
128 if ((VX*(X-Xp) > 0.) && (VY*(Y-Yp) > 0.) && (VZ*(Z-Zp) > 0.))
132 // =======================================================================
133 // function : SeeOrHide
135 // =======================================================================
136 Standard_Boolean V3d_PositionLight::SeeOrHide (const Handle(V3d_View)& theView) const
138 Standard_Real Xp,Yp,Zp, X,Y,Z, VX,VY,VZ;
141 V3d_Light::SymetricPointOnSphere (theView,
142 myTarget, Graphic3d_Vertex(Xp,Yp,Yp), Radius(), X,Y,Z, VX,VY,VZ);
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
148 // the source is on the visible face.
152 // =======================================================================
155 // =======================================================================
156 void V3d_PositionLight::Target (Standard_Real& theXp, Standard_Real& theYp, Standard_Real& theZp) const
158 myTarget.Coord (theXp, theYp, theZp);
161 // =======================================================================
162 // function : Display
164 // =======================================================================
165 void V3d_PositionLight::Display (const Handle(V3d_View)& theView, const V3d_TypeOfRepresentation theTPres)
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;
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
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;
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;
196 Handle(Graphic3d_Group) gradius, gExtArrow, gIntArrow;
197 if (Type() != V3d_DIRECTIONAL
198 && Pres == V3d_COMPLETE)
200 gradius = myGraphicStructure->NewGroup();
201 gExtArrow = myGraphicStructure->NewGroup();
202 gIntArrow = myGraphicStructure->NewGroup();
204 Handle(Graphic3d_Group) glight = myGraphicStructure->NewGroup();
205 Handle(Graphic3d_Group) gsphere;
206 if (Pres == V3d_COMPLETE
207 || Pres == V3d_PARTIAL)
209 gsphere = myGraphicStructure->NewGroup();
212 Handle(Graphic3d_Group) gnopick = myGraphicStructure1->NewGroup();
218 // Display of the light position.
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);
227 // Display of the marking sphere (limit at the circle).
229 if (Pres == V3d_COMPLETE || Pres == V3d_PARTIAL) {
231 Rayon = this->Radius();
232 theView->Proj(VX,VY,VZ);
233 V3d::CircleInPlane(gsphere,X0,Y0,Z0,VX,VY,VZ,Rayon);
235 if (Type() != V3d_DIRECTIONAL) {
237 //Display of the radius of the sphere (line + text)
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);
253 // Display of the meridian
255 Quantity_Color Col2(Quantity_NOC_GREEN);
256 Handle(Graphic3d_AspectLine3d) Asp2 = new Graphic3d_AspectLine3d(Col2,Aspect_TOL_SOLID,1.);
257 gnopick->SetPrimitivesAspect(Asp2);
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;
267 V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
269 // Display of the parallel
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;
283 V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
286 myGraphicStructure->Connect(myGraphicStructure1,Graphic3d_TOC_DESCENDANT);
287 myTypeOfRepresentation = Pres;
288 myGraphicStructure->Display();
291 // =======================================================================
292 // function : Display
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)
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;
310 theView->Convert(theXpix,theYpix,XPp,YPp);
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
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);
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.
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
345 Delta = B*B - 4.*A*C;
347 Lambda = (-B + Sqrt(Delta))/(2.*A);
353 if (Type() == V3d_SPOT)
354 ((V3d_SpotLight*)this)->SetDirection(X0-X,Y0-Y,Z0-Z);
356 Display(theView,myTypeOfRepresentation);
357 (theView->Viewer())->UpdateLights();
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);
372 Display(theView,myTypeOfRepresentation);
373 (theView->Viewer())->UpdateLights();
376 case V3d_ExtRADIUSLIGHT :
377 if (Type() == V3d_DIRECTIONAL)
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);
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;
396 Display(theView,myTypeOfRepresentation);
397 (theView->Viewer())->UpdateLights();
401 case V3d_IntRADIUSLIGHT :
402 if (Type() == V3d_DIRECTIONAL)
404 // it is attempted to preserve the target direction position of the
405 // source ==> the point is projected on the target source direction.
407 theView->Project(Xi,Yi,Zi,PXP,PYP);
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;
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);
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();
437 case V3d_RADIUSTEXTLIGHT :
445 // =======================================================================
448 // =======================================================================
449 Standard_Real V3d_PositionLight::Radius() const
451 Standard_Real Xp,Yp,Zp, Xc,Yc,Zc;
454 myTarget.Coord(Xc,Yc,Zc);
456 return Sqrt (Square(Xc - Xp) + Square(Yc - Yp) + Square(Zc - Zp));
459 // =======================================================================
462 // =======================================================================
463 void V3d_PositionLight::Erase()
465 if (!myGraphicStructure.IsNull()) myGraphicStructure->Erase();
466 if (!myGraphicStructure1.IsNull()) myGraphicStructure1->Erase();