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 # include <WNT_Window.hxx>
52 # include <Xw_Window.hxx>
55 V3d_PositionLight::V3d_PositionLight(const Handle(V3d_Viewer)& VM) : V3d_Light(VM) {
58 void V3d_PositionLight::SetTarget(const Standard_Real X, const Standard_Real Y, const Standard_Real Z) {
60 Standard_Real Xc,Yc,Zc, Xp,Yp,Zp;
62 // Recalculation of the position
63 MyTarget.Coord(Xc,Yc,Zc);
71 MyTarget.SetCoord(X,Y,Z);
72 SetPosition(Xp,Yp,Zp) ;
75 void V3d_PositionLight::SetRadius(const Standard_Real Radius) {
77 V3d_BadValue_Raise_if( Radius <= 0. , "V3d_PositionLight::SetRadius, bad radius");
78 V3d_BadValue_Raise_if( MyType == V3d_DIRECTIONAL , "V3d_PositionLight::SetRadius, bad light type");
80 Standard_Real X0,Y0,Z0, Xn,Yn,Zn, Xp,Yp,Zp;
82 // The target point remains unchanged, only the position of the light is modified
83 // by preserving the direction.
85 Graphic3d_Vector D(MyTarget, Graphic3d_Vertex(Xp, Yp, Zp));
88 MyTarget.Coord(X0,Y0,Z0);
93 SetPosition(Xn,Yn,Zn) ;
96 void V3d_PositionLight::OnHideFace(const Handle(V3d_View)& aView) {
98 Standard_Real Xp,Yp,Zp, X,Y,Z, VX,VY,VZ;
101 V3d_Light::SymetricPointOnSphere (aView,
102 MyTarget, Graphic3d_Vertex(Xp,Yp,Yp), Radius(), X,Y,Z, VX,VY,VZ);
104 // This is a visible point
105 if ((VX*(X-Xp) < 0.) && (VY*(Y-Yp) < 0.) && (VZ*(Z-Zp) < 0.))
109 void V3d_PositionLight::OnSeeFace(const Handle(V3d_View)& aView) {
111 Standard_Real Xp,Yp,Zp, X,Y,Z, VX,VY,VZ;
114 V3d_Light::SymetricPointOnSphere (aView,
115 MyTarget, Graphic3d_Vertex(Xp,Yp,Yp), Radius(), X,Y,Z, VX,VY,VZ);
117 // This is a hidden point
118 if ((VX*(X-Xp) > 0.) && (VY*(Y-Yp) > 0.) && (VZ*(Z-Zp) > 0.))
122 Standard_Boolean V3d_PositionLight::SeeOrHide(const Handle(V3d_View)& aView) const {
124 Standard_Real Xp,Yp,Zp, X,Y,Z, VX,VY,VZ;
127 V3d_Light::SymetricPointOnSphere (aView,
128 MyTarget, Graphic3d_Vertex(Xp,Yp,Yp), Radius(), X,Y,Z, VX,VY,VZ);
130 // Is it a visible or a hidden point
131 return ( (VX*(X-Xp) > 0.) || (VY*(Y-Yp) > 0.) || (VZ*(Z-Zp) > 0.) )?
132 // the source is on the hidden face
134 // the source is on the visible face.
138 void V3d_PositionLight::Target(Standard_Real& Xp, Standard_Real& Yp, Standard_Real& Zp)const {
140 MyTarget.Coord(Xp,Yp,Zp) ;
143 void V3d_PositionLight::Display( const Handle(V3d_View)& aView,
144 const V3d_TypeOfRepresentation TPres )
146 Graphic3d_Vertex PText ;
147 Standard_Real X,Y,Z,Rayon;
148 Standard_Real X0,Y0,Z0,VX,VY,VZ;
149 Standard_Real X1,Y1,Z1;
150 Standard_Real DXRef,DYRef,DZRef,DXini,DYini,DZini;
151 Standard_Real R1,G1,B1;
152 V3d_TypeOfRepresentation Pres;
153 V3d_TypeOfUpdate UpdSov;
155 // Creation of a structure of markable elements (position of the
156 // light, and the domain of lighting represented by a circle)
157 // Creation of a structure snopick of non-markable elements (target, meridian and
161 Handle(V3d_Viewer) TheViewer = aView->Viewer();
162 UpdSov = TheViewer->UpdateMode();
163 TheViewer->SetUpdateMode(V3d_WAIT);
164 if (!MyGraphicStructure.IsNull()) {
165 MyGraphicStructure->Disconnect(MyGraphicStructure1);
166 MyGraphicStructure->Clear();
167 MyGraphicStructure1->Clear();
168 if (Pres == V3d_SAMELAST) Pres = MyTypeOfRepresentation;
171 if (Pres == V3d_SAMELAST) Pres = V3d_SIMPLE;
172 Handle(Graphic3d_Structure) slight = new Graphic3d_Structure(TheViewer->Viewer());
173 MyGraphicStructure = slight;
174 Handle(Graphic3d_Structure) snopick = new Graphic3d_Structure(TheViewer->Viewer());
175 MyGraphicStructure1 = snopick;
178 Handle(Graphic3d_Group) gradius;
179 Handle(Graphic3d_Group) gExtArrow;
180 Handle(Graphic3d_Group) gIntArrow;
182 if (MyType != V3d_DIRECTIONAL) {
183 if (Pres == V3d_COMPLETE) {
184 gradius = new Graphic3d_Group(MyGraphicStructure);
185 gExtArrow = new Graphic3d_Group(MyGraphicStructure);
186 gIntArrow = new Graphic3d_Group(MyGraphicStructure);
189 Handle(Graphic3d_Group) glight = new Graphic3d_Group(MyGraphicStructure);
190 Handle(Graphic3d_Group) gsphere;
191 if (Pres == V3d_COMPLETE || Pres == V3d_PARTIAL) gsphere = new Graphic3d_Group(MyGraphicStructure);
193 Handle(Graphic3d_Group) gnopick = new Graphic3d_Group(MyGraphicStructure1);
194 MyGraphicStructure1->SetPick(Standard_False);
200 // Display of the light position.
202 this->Color(Quantity_TOC_RGB,R1,G1,B1);
203 Quantity_Color Col1(R1,G1,B1,Quantity_TOC_RGB);
204 Handle(Graphic3d_AspectLine3d) Asp1 = new Graphic3d_AspectLine3d();
205 Asp1->SetColor(Col1);
206 glight->SetPrimitivesAspect(Asp1);
207 this->Symbol(glight,aView);
209 // Display of the marking sphere (limit at the circle).
211 if (Pres == V3d_COMPLETE || Pres == V3d_PARTIAL) {
213 Rayon = this->Radius();
214 aView->Proj(VX,VY,VZ);
215 V3d::CircleInPlane(gsphere,X0,Y0,Z0,VX,VY,VZ,Rayon);
217 if (MyType != V3d_DIRECTIONAL) {
219 //Display of the radius of the sphere (line + text)
221 if (Pres == V3d_COMPLETE) {
222 this->Position(X,Y,Z);
223 Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(2);
224 aPrims->AddVertex(X0,Y0,Z0);
225 aPrims->AddVertex(X,Y,Z);
226 gnopick->AddPrimitiveArray(aPrims);
227 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.);
228 V3d::ArrowOfRadius(gIntArrow,X0,Y0,Z0,X0-X,Y0-Y,Z0-Z,M_PI/15.,Rayon/20.);
229 TCollection_AsciiString ValOfRadius(Rayon);
230 PText.SetCoord( .5*(X0+X), .5*(Y0+Y), .5*(Z0+Z) );
231 gradius->Text(ValOfRadius.ToCString(),PText,0.01);
235 // Display of the meridian
237 Quantity_Color Col2(Quantity_NOC_GREEN);
238 Handle(Graphic3d_AspectLine3d) Asp2 = new Graphic3d_AspectLine3d(Col2,Aspect_TOL_SOLID,1.);
239 gnopick->SetPrimitivesAspect(Asp2);
241 // Definition of the axis of circle
242 aView->Up(DXRef,DYRef,DZRef);
243 this->Position(X,Y,Z);
244 DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
245 VX = DYRef*DZini - DZRef*DYini;
246 VY = DZRef*DXini - DXRef*DZini;
247 VZ = DXRef*DYini - DYRef*DXini;
249 V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
251 // Display of the parallel
253 // Definition of the axis of circle
254 aView->Proj(VX,VY,VZ);
256 DXRef = VY * Z1 - VZ * Y1;
257 DYRef = VZ * X1 - VX * Z1;
258 DZRef = VX * Y1 - VY * X1;
259 this->Position(X,Y,Z);
260 DXini = X-X0; DYini = Y-Y0; DZini = Z-Z0;
261 VX = DYRef*DZini - DZRef*DYini;
262 VY = DZRef*DXini - DXRef*DZini;
263 VZ = DXRef*DYini - DYRef*DXini;
265 V3d::CircleInPlane(gnopick,X0,Y0,Z0,VX,VY,VZ,Rayon);
268 MyGraphicStructure->Connect(MyGraphicStructure1,Graphic3d_TOC_DESCENDANT);
269 MyTypeOfRepresentation = Pres;
270 MyGraphicStructure->Display();
271 TheViewer->SetUpdateMode(UpdSov);
275 void V3d_PositionLight::Tracking( const Handle(V3d_View)& aView,
276 const V3d_TypeOfPickLight WhatPick,
277 const Standard_Integer Xpix,
278 const Standard_Integer Ypix) {
280 // Quantity_Color Col ;
281 Standard_Real xPos, yPos, zPos;
282 Standard_Real XPp,YPp,PXT,PYT,X,Y,Z,Rayon,Ylim;
283 Standard_Real XMinTrack,XMaxTrack,YMinTrack,YMaxTrack;
284 Standard_Real XT,YT,ZT,X0,Y0,Z0,XP,YP,ZP,VX,VY,VZ,A,B,C,Delta;
285 Standard_Real DX,DY,PXP,PYP,Xproj,Yproj;
286 Standard_Real A1,A2,B1,B2,Rap,OldRprj,NewRprj;
287 Standard_Real Xi,Yi,Zi,DeltaX,DeltaY,DeltaZ,Lambda;
288 Standard_Integer IPX,IPY;
290 aView->Convert(Xpix,Ypix,XPp,YPp);
294 aView->Project(X0,Y0,Z0,PXT,PYT);
295 aView->Convert(PXT,PYT,IPX,IPY);
296 // Coord 3d in the plane of projection of the target.
297 aView->Convert(IPX,IPY,XT,YT,ZT);
299 case V3d_POSITIONLIGHT :
300 // The Coordinates should remain inside of the sphere
302 XMinTrack = PXT - Rayon;
303 XMaxTrack = PXT + Rayon;
304 Ylim = Sqrt( Square(Rayon) - Square(XPp - PXT) );
305 YMinTrack = PYT - Ylim;
306 YMaxTrack = PYT + Ylim;
307 if (XPp >= XMinTrack && XPp <= XMaxTrack) {
308 if (YPp >= YMinTrack && YPp <= YMaxTrack) {
309 aView->ProjReferenceAxe(Xpix,Ypix,XP,YP,ZP,VX,VY,VZ);
314 // The point of intersection of straight lines defined by :
315 // - Straight line passing by the point of projection and the eye
316 // if this is a perspective, parralel to the normal of the view
317 // if this is an axonometric view.
318 // position in the view is parallel to the normal of the view
319 // - The distance position of the target camera is equal to the radius.
321 A = VX*VX + VY*VY + VZ*VZ ;
322 B = -2. * (VX*DeltaX + VY*DeltaY + VZ*DeltaZ);
323 C = DeltaX*DeltaX + DeltaY*DeltaY + DeltaZ*DeltaZ
325 Delta = B*B - 4.*A*C;
327 Lambda = (-B + Sqrt(Delta))/(2.*A);
333 if (MyType == V3d_SPOT)
334 ((V3d_SpotLight*)this)->SetDirection(X0-X,Y0-Y,Z0-Z);
336 Display(aView,MyTypeOfRepresentation);
337 (aView->Viewer())->UpdateLights();
343 case V3d_SPACELIGHT :
344 aView->Convert(PXT,PYT,IPX,IPY);
345 // In this case Xpix,Ypix correspond to a distance, relative
346 // to the translation that is planned to be performed on the sphere.
347 aView->Convert(IPX+Xpix,IPY+Ypix,X,Y,Z);
352 Display(aView,MyTypeOfRepresentation);
353 (aView->Viewer())->UpdateLights();
356 case V3d_ExtRADIUSLIGHT :
357 if (MyType == V3d_DIRECTIONAL)
359 // it is attempted to preserve the target direction position of the
360 // source ==> the point is projected on the target source direction.
361 this->Position(Xi,Yi,Zi);
362 aView->Project(Xi,Yi,Zi,PXP,PYP);
365 A1 = DY/DX ; B1 = PYT - A1*PXT;
366 A2 = -DX/DY; B2 = YPp - A2*XPp;
367 Xproj = (B2 - B1) / (A1 - A2);
368 Yproj = A1*Xproj + B1;
369 if ( (DX*(Xproj-PXT) > 0.) && (DY*(Yproj-PYT) > 0.) ) {
370 OldRprj = Sqrt ( Square (PXP-PXT) + Square (PYP-PYT) );
371 NewRprj = Sqrt ( Square (Xproj-PXT) + Square (Yproj-PYT) );
372 Rap = NewRprj/OldRprj;
376 Display(aView,MyTypeOfRepresentation);
377 (aView->Viewer())->UpdateLights();
381 case V3d_IntRADIUSLIGHT :
382 if (MyType == V3d_DIRECTIONAL)
384 // it is attempted to preserve the target direction position of the
385 // source ==> the point is projected on the target source direction.
387 aView->Project(Xi,Yi,Zi,PXP,PYP);
390 A1 = DY/DX ; B1 = PYT - A1*PXT;
391 A2 = -DX/DY; B2 = YPp - A2*XPp;
392 Xproj = (B2 - B1) / (A1 - A2);
393 Yproj = A1*Xproj + B1;
394 if ( (DX*(Xproj-PXP) < 0.) && (DY*(Yproj-PYP) < 0.) ) {
395 OldRprj = Sqrt ( Square (PXP-PXT) + Square (PYP-PYT) );
396 NewRprj = Sqrt ( Square (Xproj-PXP) + Square (Yproj-PYP) );
397 Rap = NewRprj/OldRprj;
400 // the source should remain at a fixed position,
401 // only the target is modified.
402 Position (xPos, yPos, zPos);
403 Graphic3d_Vector Dir(Graphic3d_Vertex(xPos,yPos,zPos), MyTarget);
409 // the source should remain at a fixed position,
410 // only the target is modified.
411 MyTarget.SetCoord(X,Y,Z);
412 Display(aView,MyTypeOfRepresentation);
413 (aView->Viewer())->UpdateLights();
417 case V3d_RADIUSTEXTLIGHT :
425 Standard_Real V3d_PositionLight::Radius() const {
427 Standard_Real Xp,Yp,Zp, Xc,Yc,Zc;
430 MyTarget.Coord(Xc,Yc,Zc);
432 return Sqrt (Square(Xc - Xp) + Square(Yc - Yp) + Square(Zc - Zp));
435 void V3d_PositionLight::Erase() {
437 if (!MyGraphicStructure.IsNull()) MyGraphicStructure->Erase();
438 if (!MyGraphicStructure1.IsNull()) MyGraphicStructure1->Erase();