1 // Copyright (c) 1999-2012 OPEN CASCADE SAS
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.
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.
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.
18 /***********************************************************************
21 Created: 30-03-98 ZOV (ZELENKOV Oleg)
23 ************************************************************************/
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>
47 #include <gp_Trsf.hxx>
48 #include <TCollection_AsciiString.hxx>
50 V3d_PositionLight::V3d_PositionLight(const Handle(V3d_Viewer)& VM) : V3d_Light(VM) {
53 void V3d_PositionLight::SetTarget(const Standard_Real X, const Standard_Real Y, const Standard_Real Z) {
55 Standard_Real Xc,Yc,Zc, Xp,Yp,Zp;
57 // Recalculation of the position
58 MyTarget.Coord(Xc,Yc,Zc);
66 MyTarget.SetCoord(X,Y,Z);
67 SetPosition(Xp,Yp,Zp) ;
70 void V3d_PositionLight::SetRadius(const Standard_Real Radius) {
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");
75 Standard_Real X0,Y0,Z0, Xn,Yn,Zn, Xp,Yp,Zp;
77 // The target point remains unchanged, only the position of the light is modified
78 // by preserving the direction.
80 Graphic3d_Vector D(MyTarget, Graphic3d_Vertex(Xp, Yp, Zp));
83 MyTarget.Coord(X0,Y0,Z0);
88 SetPosition(Xn,Yn,Zn) ;
91 void V3d_PositionLight::OnHideFace(const Handle(V3d_View)& aView) {
93 Standard_Real Xp,Yp,Zp, X,Y,Z, VX,VY,VZ;
96 V3d_Light::SymetricPointOnSphere (aView,
97 MyTarget, Graphic3d_Vertex(Xp,Yp,Yp), Radius(), X,Y,Z, VX,VY,VZ);
99 // This is a visible point
100 if ((VX*(X-Xp) < 0.) && (VY*(Y-Yp) < 0.) && (VZ*(Z-Zp) < 0.))
104 void V3d_PositionLight::OnSeeFace(const Handle(V3d_View)& aView) {
106 Standard_Real Xp,Yp,Zp, X,Y,Z, VX,VY,VZ;
109 V3d_Light::SymetricPointOnSphere (aView,
110 MyTarget, Graphic3d_Vertex(Xp,Yp,Yp), Radius(), X,Y,Z, VX,VY,VZ);
112 // This is a hidden point
113 if ((VX*(X-Xp) > 0.) && (VY*(Y-Yp) > 0.) && (VZ*(Z-Zp) > 0.))
117 Standard_Boolean V3d_PositionLight::SeeOrHide(const Handle(V3d_View)& aView) const {
119 Standard_Real Xp,Yp,Zp, X,Y,Z, VX,VY,VZ;
122 V3d_Light::SymetricPointOnSphere (aView,
123 MyTarget, Graphic3d_Vertex(Xp,Yp,Yp), Radius(), X,Y,Z, VX,VY,VZ);
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
129 // the source is on the visible face.
133 void V3d_PositionLight::Target(Standard_Real& Xp, Standard_Real& Yp, Standard_Real& Zp)const {
135 MyTarget.Coord(Xp,Yp,Zp) ;
138 void V3d_PositionLight::Display( const Handle(V3d_View)& aView,
139 const V3d_TypeOfRepresentation TPres )
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;
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
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;
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;
173 Handle(Graphic3d_Group) gradius;
174 Handle(Graphic3d_Group) gExtArrow;
175 Handle(Graphic3d_Group) gIntArrow;
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);
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);
188 Handle(Graphic3d_Group) gnopick = new Graphic3d_Group(MyGraphicStructure1);
189 MyGraphicStructure1->SetPick(Standard_False);
195 // Display of the light position.
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);
204 // Display of the marking sphere (limit at the circle).
206 if (Pres == V3d_COMPLETE || Pres == V3d_PARTIAL) {
208 Rayon = this->Radius();
209 aView->Proj(VX,VY,VZ);
210 V3d::CircleInPlane(gsphere,X0,Y0,Z0,VX,VY,VZ,Rayon);
212 if (MyType != V3d_DIRECTIONAL) {
214 //Display of the radius of the sphere (line + text)
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);
230 // Display of the meridian
232 Quantity_Color Col2(Quantity_NOC_GREEN);
233 Handle(Graphic3d_AspectLine3d) Asp2 = new Graphic3d_AspectLine3d(Col2,Aspect_TOL_SOLID,1.);
234 gnopick->SetPrimitivesAspect(Asp2);
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;
244 V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
246 // Display of the parallel
248 // Definition of the axis of circle
249 aView->Proj(VX,VY,VZ);
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;
260 V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
263 MyGraphicStructure->Connect(MyGraphicStructure1,Graphic3d_TOC_DESCENDANT);
264 MyTypeOfRepresentation = Pres;
265 MyGraphicStructure->Display();
266 TheViewer->SetUpdateMode(UpdSov);
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) {
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;
285 aView->Convert(Xpix,Ypix,XPp,YPp);
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);
294 case V3d_POSITIONLIGHT :
295 // The Coordinates should remain inside of the sphere
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);
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.
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
320 Delta = B*B - 4.*A*C;
322 Lambda = (-B + Sqrt(Delta))/(2.*A);
328 if (MyType == V3d_SPOT)
329 ((V3d_SpotLight*)this)->SetDirection(X0-X,Y0-Y,Z0-Z);
331 Display(aView,MyTypeOfRepresentation);
332 (aView->Viewer())->UpdateLights();
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);
347 Display(aView,MyTypeOfRepresentation);
348 (aView->Viewer())->UpdateLights();
351 case V3d_ExtRADIUSLIGHT :
352 if (MyType == V3d_DIRECTIONAL)
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);
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;
371 Display(aView,MyTypeOfRepresentation);
372 (aView->Viewer())->UpdateLights();
376 case V3d_IntRADIUSLIGHT :
377 if (MyType == 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.
382 aView->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-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;
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);
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();
412 case V3d_RADIUSTEXTLIGHT :
420 Standard_Real V3d_PositionLight::Radius() const {
422 Standard_Real Xp,Yp,Zp, Xc,Yc,Zc;
425 MyTarget.Coord(Xc,Yc,Zc);
427 return Sqrt (Square(Xc - Xp) + Square(Yc - Yp) + Square(Zc - Zp));
430 void V3d_PositionLight::Erase() {
432 if (!MyGraphicStructure.IsNull()) MyGraphicStructure->Erase();
433 if (!MyGraphicStructure1.IsNull()) MyGraphicStructure1->Erase();