1 // File: Select3D_Projector.cxx
2 // Created: Fri Mar 13 11:08:32 1992
3 // Author: Christophe MARION
7 // Change RefToPix()/Convert() to Project() method.
9 #include <Select3D_Projector.ixx>
10 #include <Precision.hxx>
13 #include <gp_Vec2d.hxx>
15 // formula for derivating a perspective, from Mathematica
18 // D1 = -------- + -------------
20 // 1 - ---- f (1 - ----)
23 //=======================================================================
24 //function : Select3D_Projector
26 //=======================================================================
28 Select3D_Projector::Select3D_Projector(const Handle(V3d_View)& aViou)
29 : myPersp(aViou->Type()==V3d_PERSPECTIVE),
30 myFocus(aViou->Focale()),
35 myDepthMin(-Precision::Infinite()),
36 myDepthMax( Precision::Infinite())
38 Standard_Real Xat,Yat,Zat,XUp,YUp,ZUp,DX,DY,DZ;
39 //Standard_Boolean Pers=Standard_False;
41 aViou->At(Xat,Yat,Zat);
42 aViou->Up(XUp,YUp,ZUp);
43 aViou->Proj(DX,DY,DZ);
44 gp_Pnt At (Xat,Yat,Zat);
45 gp_Dir Zpers (DX,DY,DZ);
46 gp_Dir Ypers (XUp,YUp,ZUp);
47 gp_Dir Xpers = Ypers.Crossed(Zpers);
48 gp_Ax3 Axe (At, Zpers, Xpers);
49 myScaledTrsf.SetTransformation(Axe);
50 myGTrsf.SetTrsf(myScaledTrsf);
55 //=======================================================================
56 //function : Select3D_Projector
58 //=======================================================================
60 Select3D_Projector::Select3D_Projector()
61 : myPersp(Standard_False),
66 myDepthMin(-Precision::Infinite()),
67 myDepthMax( Precision::Infinite())
73 //=======================================================================
74 //function : Select3D_Projector
76 //=======================================================================
78 Select3D_Projector::Select3D_Projector (const gp_Ax2& CS)
79 : myPersp(Standard_False),
81 myDepthMin(-Precision::Infinite()),
82 myDepthMax( Precision::Infinite())
84 myScaledTrsf.SetTransformation(CS);
85 myGTrsf.SetTrsf(myScaledTrsf);
90 //=======================================================================
91 //function : Select3D_Projector
93 //=======================================================================
95 Select3D_Projector::Select3D_Projector (const gp_Ax2& CS,
96 const Standard_Real Focus)
97 : myPersp(Standard_True),
99 myDepthMin(-Precision::Infinite()),
100 myDepthMax( Precision::Infinite())
102 myScaledTrsf.SetTransformation(CS);
103 myGTrsf.SetTrsf(myScaledTrsf);
108 //=======================================================================
109 //function : Select3D_Projector
111 //=======================================================================
113 Select3D_Projector::Select3D_Projector (const gp_Trsf& T,
114 const Standard_Boolean Persp,
115 const Standard_Real Focus)
119 myDepthMin(-Precision::Infinite()),
120 myDepthMax( Precision::Infinite())
122 myGTrsf.SetTrsf(myScaledTrsf);
127 //=======================================================================
128 //function : Select3D_Projector
130 //=======================================================================
132 Select3D_Projector::Select3D_Projector (const gp_Trsf& T,
133 const Standard_Boolean Persp,
134 const Standard_Real Focus,
144 myDepthMin(-Precision::Infinite()),
145 myDepthMax( Precision::Infinite())
147 myGTrsf.SetTrsf(myScaledTrsf);
151 //=======================================================================
152 //function : Select3D_Projector
154 //=======================================================================
156 Select3D_Projector::Select3D_Projector (const gp_GTrsf& GT,
157 const Standard_Boolean Persp,
158 const Standard_Real Focus)
162 myDepthMin(-Precision::Infinite()),
163 myDepthMax( Precision::Infinite())
169 //=======================================================================
172 //=======================================================================
174 void Select3D_Projector::Set
176 const Standard_Boolean Persp,
177 const Standard_Real Focus)
186 //=======================================================================
189 //=======================================================================
191 #include <gp_Mat.hxx>
193 static Standard_Integer TrsfType(const gp_GTrsf& Trsf) {
194 const gp_Mat& Mat = Trsf.VectorialPart();
195 if( (Abs(Mat.Value(1,1)-1.0) < 1e-15)
196 && (Abs(Mat.Value(2,2)-1.0) < 1e-15)
197 && (Abs(Mat.Value(3,3)-1.0) < 1e-15)) {
200 else if( (Abs(Mat.Value(1,1)-0.7071067811865476) < 1e-15)
201 && (Abs(Mat.Value(1,2)+0.5) < 1e-15)
202 && (Abs(Mat.Value(1,3)-0.5) < 1e-15)
204 && (Abs(Mat.Value(2,1)-0.7071067811865476) < 1e-15)
205 && (Abs(Mat.Value(2,2)-0.5) < 1e-15)
206 && (Abs(Mat.Value(2,3)+0.5) < 1e-15)
208 && (Abs(Mat.Value(3,1)) < 1e-15)
209 && (Abs(Mat.Value(3,2)-0.7071067811865476) < 1e-15)
210 && (Abs(Mat.Value(3,3)-0.7071067811865476) < 1e-15)) {
213 else if( (Abs(Mat.Value(1,1)-1.0) < 1e-15)
214 && (Abs(Mat.Value(2,3)-1.0) < 1e-15)
215 && (Abs(Mat.Value(3,2)+1.0) < 1e-15)) {
216 return(2); //-- front
218 else if( (Abs(Mat.Value(1,1)-0.7071067811865476) < 1e-15)
219 && (Abs(Mat.Value(1,2)-0.7071067811865476) < 1e-15)
220 && (Abs(Mat.Value(1,3)) < 1e-15)
222 && (Abs(Mat.Value(2,1)+0.5) < 1e-15)
223 && (Abs(Mat.Value(2,2)-0.5) < 1e-15)
224 && (Abs(Mat.Value(2,3)-0.7071067811865476) < 1e-15)
226 && (Abs(Mat.Value(3,1)-0.5) < 1e-15)
227 && (Abs(Mat.Value(3,2)+0.5) < 1e-15)
228 && (Abs(Mat.Value(3,3)-0.7071067811865476) < 1e-15)) {
234 void Select3D_Projector::Scaled (const Standard_Boolean On)
239 //myGTrsf.SetTranslationPart(gp_XYZ(0.,0.,0.));
240 myType=TrsfType(myGTrsf);
247 //=======================================================================
250 //=======================================================================
252 void Select3D_Projector::Project (const gp_Pnt& P, gp_Pnt2d& Pout) const
255 if(!myView.IsNull()){
256 Standard_Real Xout,Yout;
259 myView->Project(P.X(),P.Y(),P.Z(),Xout,Yout);
261 Standard_Integer Xp,Yp;
262 myView->RefToPix(P.X(),P.Y(),P.Z(),Xp,Yp);
263 myView->Convert(Xp,Yp,Xout,Yout);
265 Pout.SetCoord(Xout,Yout);
271 case 0: { //-- axono standard
272 Standard_Real x07 = P.X()*0.7071067811865475;
273 Standard_Real y05 = P.Y()*0.5;
274 Standard_Real z05 = P.Z()*0.5;
277 //-- Z=0.7071067811865475*(P.Y()+P.Z());
281 X=P.X(); Y=P.Y(); //-- Z=P.Z();
286 X=P.X(); Y=P.Z(); //-- Z=-P.Y();
291 Standard_Real xmy05 = (P.X()-P.Y())*0.5;
292 Standard_Real z07 = P.Z()*0.7071067811865476;
293 X=0.7071067811865476*(P.X()+P.Y());
303 Standard_Real R = 1.-P2.Z()/myFocus;
304 Pout.SetCoord(P2.X()/R,P2.Y()/R);
307 Pout.SetCoord(P2.X(),P2.Y());
316 Standard_Real R = 1.-P2.Z()/myFocus;
317 Pout.SetCoord(P2.X()/R,P2.Y()/R);
320 Pout.SetCoord(P2.X(),P2.Y());
327 //=======================================================================
330 //=======================================================================
331 /* ====== TYPE 0 (??)
332 (0.7071067811865476, -0.5 , 0.4999999999999999)
333 (0.7071067811865475, 0.5000000000000001, -0.5 )
334 (0.0, 0.7071067811865475, 0.7071067811865476)
341 ======= TYPE 2 (front)
343 (0.0, 1.110223024625157e-16 , 1.0)
344 (0.0, -1.0 , 1.110223024625157e-16)
347 ( 0.7071067811865476, 0.7071067811865475, 0.0)
348 (-0.5 , 0.5000000000000001, 0.7071067811865475)
349 ( 0.4999999999999999, -0.5 , 0.7071067811865476)
351 void Select3D_Projector::Project (const gp_Pnt& P,
354 Standard_Real& Z) const
356 if(!myView.IsNull()){
357 // Standard_Real Xout,Yout;
360 myView->Project(P.X(),P.Y(),P.Z(),X,Y);
362 Standard_Integer Xp,Yp;
363 myView->RefToPix(P.X(),P.Y(),P.Z(),Xp,Yp);
364 myView->Convert(Xp,Yp,X,Y);
370 case 0: { //-- axono standard
371 Standard_Real x07 = P.X()*0.7071067811865475;
372 Standard_Real y05 = P.Y()*0.5;
373 Standard_Real z05 = P.Z()*0.5;
376 Z=0.7071067811865475*(P.Y()+P.Z());
380 X=P.X(); Y=P.Y(); Z=P.Z();
384 X=P.X(); Y=P.Z(); Z=-P.Y();
388 Standard_Real xmy05 = (P.X()-P.Y())*0.5;
389 Standard_Real z07 = P.Z()*0.7071067811865476;
390 X=0.7071067811865476*(P.X()+P.Y());
408 Standard_Real R = 1 - Z / myFocus;
415 //=======================================================================
418 //=======================================================================
420 void Select3D_Projector::Project (const gp_Pnt& P,
423 gp_Vec2d& D1out) const
430 Standard_Real R = 1. - PP.Z() / myFocus;
431 Pout .SetCoord(PP .X()/R , PP.Y()/R);
432 D1out.SetCoord(DD1.X()/R + PP.X()*DD1.Z()/(myFocus * R*R),
433 DD1.Y()/R + PP.Y()*DD1.Z()/(myFocus * R*R));
436 Pout .SetCoord(PP .X(),PP .Y());
437 D1out.SetCoord(DD1.X(),DD1.Y());
441 //=======================================================================
444 //=======================================================================
446 void Select3D_Projector::BoxAdd
450 gp_Vec2d V(P.X(),P.Y());
451 gp_Pnt PP(myD1 * V, myD2 * V, myD3 * V);
455 //=======================================================================
458 //=======================================================================
460 gp_Lin Select3D_Projector::Shoot
461 (const Standard_Real X,
462 const Standard_Real Y) const
467 L = gp_Lin(gp_Pnt(0,0, myFocus),
468 gp_Dir(X,Y,-myFocus));
471 L = gp_Lin(gp_Pnt(X,Y,0),
474 Transform(L, myInvTrsf);
479 //=======================================================================
480 //function : SetDirection
482 //=======================================================================
484 void Select3D_Projector::SetDirection ()
488 if ((Abs(V1.X()) + Abs(V1.Y())) < Precision::Angular()) V1.SetCoord(1,1,0);
489 gp_Vec2d D1(V1.X(),V1.Y());
490 myD1.SetCoord(-D1.Y(),D1.X());
493 if ((Abs(V2.X()) + Abs(V2.Y())) < Precision::Angular()) V2.SetCoord(1,1,0);
494 gp_Vec2d D2(V2.X(),V2.Y());
495 myD2.SetCoord(-D2.Y(),D2.X());
498 if ((Abs(V3.X()) + Abs(V3.Y())) < Precision::Angular()) V3.SetCoord(1,1,0);
499 gp_Vec2d D3(V3.X(),V3.Y());
500 myD3.SetCoord(-D3.Y(),D3.X());
503 void Select3D_Projector::SetView(const Handle(V3d_View)& aViou)
506 myPersp = aViou->Type()==V3d_PERSPECTIVE;
507 myFocus= aViou->Focale();
508 Standard_Real Xat,Yat,Zat,XUp,YUp,ZUp,DX,DY,DZ;
509 //Standard_Boolean Pers=Standard_False;
511 aViou->At(Xat,Yat,Zat);
512 aViou->Up(XUp,YUp,ZUp);
513 aViou->Proj(DX,DY,DZ);
514 gp_Pnt At (Xat,Yat,Zat);
515 gp_Dir Zpers (DX,DY,DZ);
516 gp_Dir Ypers (XUp,YUp,ZUp);
517 gp_Dir Xpers = Ypers.Crossed(Zpers);
518 gp_Ax3 Axe (At, Zpers, Xpers);
519 myScaledTrsf.SetTransformation(Axe);
524 void Select3D_Projector::DepthMinMax (const Standard_Real theDepthMin,
525 const Standard_Real theDepthMax)
527 myDepthMin = theDepthMin;
528 myDepthMax = theDepthMax;