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 /***********************************************************************
17 Created: 30-03-98 ZOV (ZELENKOV Oleg)
19 ************************************************************************/
22 #include <V3d_PositionLight.ixx>
23 #include <Graphic3d_Vector.hxx>
24 #include <Graphic3d_Vertex.hxx>
25 #include <Graphic3d_Structure.hxx>
26 #include <Graphic3d_Group.hxx>
27 #include <Graphic3d_ArrayOfSegments.hxx>
28 #include <Graphic3d_AspectMarker3d.hxx>
29 #include <Graphic3d_AspectLine3d.hxx>
30 #include <Graphic3d_AspectText3d.hxx>
31 #include <V3d_SpotLight.hxx>
32 #include <Visual3d_Light.hxx>
33 #include <Visual3d_ViewManager.hxx>
34 #include <Visual3d_ContextPick.hxx>
35 #include <Visual3d_PickDescriptor.hxx>
36 #include <Visual3d_HSequenceOfPickPath.hxx>
37 #include <Visual3d_PickPath.hxx>
38 #include <V3d_BadValue.hxx>
43 #include <gp_Trsf.hxx>
44 #include <TCollection_AsciiString.hxx>
46 V3d_PositionLight::V3d_PositionLight(const Handle(V3d_Viewer)& VM) : V3d_Light(VM) {
49 void V3d_PositionLight::SetTarget(const Standard_Real X, const Standard_Real Y, const Standard_Real Z) {
51 Standard_Real Xc,Yc,Zc, Xp,Yp,Zp;
53 // Recalculation of the position
54 MyTarget.Coord(Xc,Yc,Zc);
62 MyTarget.SetCoord(X,Y,Z);
63 SetPosition(Xp,Yp,Zp) ;
66 void V3d_PositionLight::SetRadius(const Standard_Real Radius) {
68 V3d_BadValue_Raise_if( Radius <= 0. , "V3d_PositionLight::SetRadius, bad radius");
69 V3d_BadValue_Raise_if( MyType == V3d_DIRECTIONAL , "V3d_PositionLight::SetRadius, bad light type");
71 Standard_Real X0,Y0,Z0, Xn,Yn,Zn, Xp,Yp,Zp;
73 // The target point remains unchanged, only the position of the light is modified
74 // by preserving the direction.
76 Graphic3d_Vector D(MyTarget, Graphic3d_Vertex(Xp, Yp, Zp));
79 MyTarget.Coord(X0,Y0,Z0);
84 SetPosition(Xn,Yn,Zn) ;
87 void V3d_PositionLight::OnHideFace(const Handle(V3d_View)& aView) {
89 Standard_Real Xp,Yp,Zp, X,Y,Z, VX,VY,VZ;
92 V3d_Light::SymetricPointOnSphere (aView,
93 MyTarget, Graphic3d_Vertex(Xp,Yp,Yp), Radius(), X,Y,Z, VX,VY,VZ);
95 // This is a visible point
96 if ((VX*(X-Xp) < 0.) && (VY*(Y-Yp) < 0.) && (VZ*(Z-Zp) < 0.))
100 void V3d_PositionLight::OnSeeFace(const Handle(V3d_View)& aView) {
102 Standard_Real Xp,Yp,Zp, X,Y,Z, VX,VY,VZ;
105 V3d_Light::SymetricPointOnSphere (aView,
106 MyTarget, Graphic3d_Vertex(Xp,Yp,Yp), Radius(), X,Y,Z, VX,VY,VZ);
108 // This is a hidden point
109 if ((VX*(X-Xp) > 0.) && (VY*(Y-Yp) > 0.) && (VZ*(Z-Zp) > 0.))
113 Standard_Boolean V3d_PositionLight::SeeOrHide(const Handle(V3d_View)& aView) const {
115 Standard_Real Xp,Yp,Zp, X,Y,Z, VX,VY,VZ;
118 V3d_Light::SymetricPointOnSphere (aView,
119 MyTarget, Graphic3d_Vertex(Xp,Yp,Yp), Radius(), X,Y,Z, VX,VY,VZ);
121 // Is it a visible or a hidden point
122 return ( (VX*(X-Xp) > 0.) || (VY*(Y-Yp) > 0.) || (VZ*(Z-Zp) > 0.) )?
123 // the source is on the hidden face
125 // the source is on the visible face.
129 void V3d_PositionLight::Target(Standard_Real& Xp, Standard_Real& Yp, Standard_Real& Zp)const {
131 MyTarget.Coord(Xp,Yp,Zp) ;
134 void V3d_PositionLight::Display( const Handle(V3d_View)& aView,
135 const V3d_TypeOfRepresentation TPres )
137 Graphic3d_Vertex PText ;
138 Standard_Real X,Y,Z,Rayon;
139 Standard_Real X0,Y0,Z0,VX,VY,VZ;
140 Standard_Real X1,Y1,Z1;
141 Standard_Real DXRef,DYRef,DZRef,DXini,DYini,DZini;
142 Standard_Real R1,G1,B1;
143 V3d_TypeOfRepresentation Pres;
144 V3d_TypeOfUpdate UpdSov;
146 // Creation of a structure of markable elements (position of the
147 // light, and the domain of lighting represented by a circle)
148 // Creation of a structure snopick of non-markable elements (target, meridian and
152 Handle(V3d_Viewer) TheViewer = aView->Viewer();
153 UpdSov = TheViewer->UpdateMode();
154 TheViewer->SetUpdateMode(V3d_WAIT);
155 if (!MyGraphicStructure.IsNull()) {
156 MyGraphicStructure->Disconnect(MyGraphicStructure1);
157 MyGraphicStructure->Clear();
158 MyGraphicStructure1->Clear();
159 if (Pres == V3d_SAMELAST) Pres = MyTypeOfRepresentation;
162 if (Pres == V3d_SAMELAST) Pres = V3d_SIMPLE;
163 Handle(Graphic3d_Structure) slight = new Graphic3d_Structure(TheViewer->Viewer());
164 MyGraphicStructure = slight;
165 Handle(Graphic3d_Structure) snopick = new Graphic3d_Structure(TheViewer->Viewer());
166 MyGraphicStructure1 = snopick;
169 Handle(Graphic3d_Group) gradius;
170 Handle(Graphic3d_Group) gExtArrow;
171 Handle(Graphic3d_Group) gIntArrow;
173 if (MyType != V3d_DIRECTIONAL) {
174 if (Pres == V3d_COMPLETE) {
175 gradius = new Graphic3d_Group(MyGraphicStructure);
176 gExtArrow = new Graphic3d_Group(MyGraphicStructure);
177 gIntArrow = new Graphic3d_Group(MyGraphicStructure);
180 Handle(Graphic3d_Group) glight = new Graphic3d_Group(MyGraphicStructure);
181 Handle(Graphic3d_Group) gsphere;
182 if (Pres == V3d_COMPLETE || Pres == V3d_PARTIAL) gsphere = new Graphic3d_Group(MyGraphicStructure);
184 Handle(Graphic3d_Group) gnopick = new Graphic3d_Group(MyGraphicStructure1);
185 MyGraphicStructure1->SetPick(Standard_False);
191 // Display of the light position.
193 this->Color(Quantity_TOC_RGB,R1,G1,B1);
194 Quantity_Color Col1(R1,G1,B1,Quantity_TOC_RGB);
195 Handle(Graphic3d_AspectLine3d) Asp1 = new Graphic3d_AspectLine3d();
196 Asp1->SetColor(Col1);
197 glight->SetPrimitivesAspect(Asp1);
198 this->Symbol(glight,aView);
200 // Display of the marking sphere (limit at the circle).
202 if (Pres == V3d_COMPLETE || Pres == V3d_PARTIAL) {
204 Rayon = this->Radius();
205 aView->Proj(VX,VY,VZ);
206 V3d::CircleInPlane(gsphere,X0,Y0,Z0,VX,VY,VZ,Rayon);
208 if (MyType != V3d_DIRECTIONAL) {
210 //Display of the radius of the sphere (line + text)
212 if (Pres == V3d_COMPLETE) {
213 this->Position(X,Y,Z);
214 Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(2);
215 aPrims->AddVertex(X0,Y0,Z0);
216 aPrims->AddVertex(X,Y,Z);
217 gnopick->AddPrimitiveArray(aPrims);
218 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.);
219 V3d::ArrowOfRadius(gIntArrow,X0,Y0,Z0,X0-X,Y0-Y,Z0-Z,M_PI/15.,Rayon/20.);
220 TCollection_AsciiString ValOfRadius(Rayon);
221 PText.SetCoord( .5*(X0+X), .5*(Y0+Y), .5*(Z0+Z) );
222 gradius->Text(ValOfRadius.ToCString(),PText,0.01);
226 // Display of the meridian
228 Quantity_Color Col2(Quantity_NOC_GREEN);
229 Handle(Graphic3d_AspectLine3d) Asp2 = new Graphic3d_AspectLine3d(Col2,Aspect_TOL_SOLID,1.);
230 gnopick->SetPrimitivesAspect(Asp2);
232 // Definition of the axis of circle
233 aView->Up(DXRef,DYRef,DZRef);
234 this->Position(X,Y,Z);
235 DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
236 VX = DYRef*DZini - DZRef*DYini;
237 VY = DZRef*DXini - DXRef*DZini;
238 VZ = DXRef*DYini - DYRef*DXini;
240 V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
242 // Display of the parallel
244 // Definition of the axis of circle
245 aView->Proj(VX,VY,VZ);
247 DXRef = VY * Z1 - VZ * Y1;
248 DYRef = VZ * X1 - VX * Z1;
249 DZRef = VX * Y1 - VY * X1;
250 this->Position(X,Y,Z);
251 DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
252 VX = DYRef*DZini - DZRef*DYini;
253 VY = DZRef*DXini - DXRef*DZini;
254 VZ = DXRef*DYini - DYRef*DXini;
256 V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
259 MyGraphicStructure->Connect(MyGraphicStructure1,Graphic3d_TOC_DESCENDANT);
260 MyTypeOfRepresentation = Pres;
261 MyGraphicStructure->Display();
262 TheViewer->SetUpdateMode(UpdSov);
266 void V3d_PositionLight::Tracking( const Handle(V3d_View)& aView,
267 const V3d_TypeOfPickLight WhatPick,
268 const Standard_Integer Xpix,
269 const Standard_Integer Ypix) {
271 // Quantity_Color Col ;
272 Standard_Real xPos, yPos, zPos;
273 Standard_Real XPp,YPp,PXT,PYT,X,Y,Z,Rayon,Ylim;
274 Standard_Real XMinTrack,XMaxTrack,YMinTrack,YMaxTrack;
275 Standard_Real XT,YT,ZT,X0,Y0,Z0,XP,YP,ZP,VX,VY,VZ,A,B,C,Delta;
276 Standard_Real DX,DY,PXP,PYP,Xproj,Yproj;
277 Standard_Real A1,A2,B1,B2,Rap,OldRprj,NewRprj;
278 Standard_Real Xi,Yi,Zi,DeltaX,DeltaY,DeltaZ,Lambda;
279 Standard_Integer IPX,IPY;
281 aView->Convert(Xpix,Ypix,XPp,YPp);
285 aView->Project(X0,Y0,Z0,PXT,PYT);
286 aView->Convert(PXT,PYT,IPX,IPY);
287 // Coord 3d in the plane of projection of the target.
288 aView->Convert(IPX,IPY,XT,YT,ZT);
290 case V3d_POSITIONLIGHT :
291 // The Coordinates should remain inside of the sphere
293 XMinTrack = PXT - Rayon;
294 XMaxTrack = PXT + Rayon;
295 Ylim = Sqrt( Square(Rayon) - Square(XPp - PXT) );
296 YMinTrack = PYT - Ylim;
297 YMaxTrack = PYT + Ylim;
298 if (XPp >= XMinTrack && XPp <= XMaxTrack) {
299 if (YPp >= YMinTrack && YPp <= YMaxTrack) {
300 aView->ProjReferenceAxe(Xpix,Ypix,XP,YP,ZP,VX,VY,VZ);
305 // The point of intersection of straight lines defined by :
306 // - Straight line passing by the point of projection and the eye
307 // if this is a perspective, parralel to the normal of the view
308 // if this is an axonometric view.
309 // position in the view is parallel to the normal of the view
310 // - The distance position of the target camera is equal to the radius.
312 A = VX*VX + VY*VY + VZ*VZ ;
313 B = -2. * (VX*DeltaX + VY*DeltaY + VZ*DeltaZ);
314 C = DeltaX*DeltaX + DeltaY*DeltaY + DeltaZ*DeltaZ
316 Delta = B*B - 4.*A*C;
318 Lambda = (-B + Sqrt(Delta))/(2.*A);
324 if (MyType == V3d_SPOT)
325 ((V3d_SpotLight*)this)->SetDirection(X0-X,Y0-Y,Z0-Z);
327 Display(aView,MyTypeOfRepresentation);
328 (aView->Viewer())->UpdateLights();
334 case V3d_SPACELIGHT :
335 aView->Convert(PXT,PYT,IPX,IPY);
336 // In this case Xpix,Ypix correspond to a distance, relative
337 // to the translation that is planned to be performed on the sphere.
338 aView->Convert(IPX+Xpix,IPY+Ypix,X,Y,Z);
343 Display(aView,MyTypeOfRepresentation);
344 (aView->Viewer())->UpdateLights();
347 case V3d_ExtRADIUSLIGHT :
348 if (MyType == V3d_DIRECTIONAL)
350 // it is attempted to preserve the target direction position of the
351 // source ==> the point is projected on the target source direction.
352 this->Position(Xi,Yi,Zi);
353 aView->Project(Xi,Yi,Zi,PXP,PYP);
356 A1 = DY/DX ; B1 = PYT - A1*PXT;
357 A2 = -DX/DY; B2 = YPp - A2*XPp;
358 Xproj = (B2 - B1) / (A1 - A2);
359 Yproj = A1*Xproj + B1;
360 if ( (DX*(Xproj-PXT) > 0.) && (DY*(Yproj-PYT) > 0.) ) {
361 OldRprj = Sqrt ( Square (PXP-PXT) + Square (PYP-PYT) );
362 NewRprj = Sqrt ( Square (Xproj-PXT) + Square (Yproj-PYT) );
363 Rap = NewRprj/OldRprj;
367 Display(aView,MyTypeOfRepresentation);
368 (aView->Viewer())->UpdateLights();
372 case V3d_IntRADIUSLIGHT :
373 if (MyType == V3d_DIRECTIONAL)
375 // it is attempted to preserve the target direction position of the
376 // source ==> the point is projected on the target source direction.
378 aView->Project(Xi,Yi,Zi,PXP,PYP);
381 A1 = DY/DX ; B1 = PYT - A1*PXT;
382 A2 = -DX/DY; B2 = YPp - A2*XPp;
383 Xproj = (B2 - B1) / (A1 - A2);
384 Yproj = A1*Xproj + B1;
385 if ( (DX*(Xproj-PXP) < 0.) && (DY*(Yproj-PYP) < 0.) ) {
386 OldRprj = Sqrt ( Square (PXP-PXT) + Square (PYP-PYT) );
387 NewRprj = Sqrt ( Square (Xproj-PXP) + Square (Yproj-PYP) );
388 Rap = NewRprj/OldRprj;
391 // the source should remain at a fixed position,
392 // only the target is modified.
393 Position (xPos, yPos, zPos);
394 Graphic3d_Vector Dir(Graphic3d_Vertex(xPos,yPos,zPos), MyTarget);
400 // the source should remain at a fixed position,
401 // only the target is modified.
402 MyTarget.SetCoord(X,Y,Z);
403 Display(aView,MyTypeOfRepresentation);
404 (aView->Viewer())->UpdateLights();
408 case V3d_RADIUSTEXTLIGHT :
416 Standard_Real V3d_PositionLight::Radius() const {
418 Standard_Real Xp,Yp,Zp, Xc,Yc,Zc;
421 MyTarget.Coord(Xc,Yc,Zc);
423 return Sqrt (Square(Xc - Xp) + Square(Yc - Yp) + Square(Zc - Zp));
426 void V3d_PositionLight::Erase() {
428 if (!MyGraphicStructure.IsNull()) MyGraphicStructure->Erase();
429 if (!MyGraphicStructure1.IsNull()) MyGraphicStructure1->Erase();