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()),
32 myDepthMin(-Precision::Infinite()),
33 myDepthMax( Precision::Infinite())
35 Standard_Real Xat,Yat,Zat,XUp,YUp,ZUp,DX,DY,DZ;
36 //Standard_Boolean Pers=Standard_False;
38 aViou->At(Xat,Yat,Zat);
39 aViou->Up(XUp,YUp,ZUp);
40 aViou->Proj(DX,DY,DZ);
41 gp_Pnt At (Xat,Yat,Zat);
42 gp_Dir Zpers (DX,DY,DZ);
43 gp_Dir Ypers (XUp,YUp,ZUp);
44 gp_Dir Xpers = Ypers.Crossed(Zpers);
45 gp_Ax3 Axe (At, Zpers, Xpers);
46 myScaledTrsf.SetTransformation(Axe);
47 myGTrsf.SetTrsf(myScaledTrsf);
52 //=======================================================================
53 //function : Select3D_Projector
55 //=======================================================================
57 Select3D_Projector::Select3D_Projector()
58 : myPersp(Standard_False),
60 myDepthMin(-Precision::Infinite()),
61 myDepthMax( Precision::Infinite())
66 //=======================================================================
67 //function : Select3D_Projector
69 //=======================================================================
71 Select3D_Projector::Select3D_Projector (const gp_Ax2& CS)
72 : myPersp(Standard_False),
74 myDepthMin(-Precision::Infinite()),
75 myDepthMax( Precision::Infinite())
77 myScaledTrsf.SetTransformation(CS);
78 myGTrsf.SetTrsf(myScaledTrsf);
82 //=======================================================================
83 //function : Select3D_Projector
85 //=======================================================================
87 Select3D_Projector::Select3D_Projector (const gp_Ax2& CS,
88 const Standard_Real Focus)
89 : myPersp(Standard_True),
91 myDepthMin(-Precision::Infinite()),
92 myDepthMax( Precision::Infinite())
94 myScaledTrsf.SetTransformation(CS);
95 myGTrsf.SetTrsf(myScaledTrsf);
99 //=======================================================================
100 //function : Select3D_Projector
102 //=======================================================================
104 Select3D_Projector::Select3D_Projector (const gp_Trsf& T,
105 const Standard_Boolean Persp,
106 const Standard_Real Focus)
110 myDepthMin(-Precision::Infinite()),
111 myDepthMax( Precision::Infinite())
113 myGTrsf.SetTrsf(myScaledTrsf);
117 //=======================================================================
118 //function : Select3D_Projector
120 //=======================================================================
122 Select3D_Projector::Select3D_Projector (const gp_GTrsf& GT,
123 const Standard_Boolean Persp,
124 const Standard_Real Focus)
128 myDepthMin(-Precision::Infinite()),
129 myDepthMax( Precision::Infinite())
134 //=======================================================================
137 //=======================================================================
139 void Select3D_Projector::Set
141 const Standard_Boolean Persp,
142 const Standard_Real Focus)
150 //=======================================================================
153 //=======================================================================
155 #include <gp_Mat.hxx>
157 static Standard_Integer TrsfType(const gp_GTrsf& Trsf) {
158 const gp_Mat& Mat = Trsf.VectorialPart();
159 if( (Abs(Mat.Value(1,1)-1.0) < 1e-15)
160 && (Abs(Mat.Value(2,2)-1.0) < 1e-15)
161 && (Abs(Mat.Value(3,3)-1.0) < 1e-15)) {
164 else if( (Abs(Mat.Value(1,1)-0.7071067811865476) < 1e-15)
165 && (Abs(Mat.Value(1,2)+0.5) < 1e-15)
166 && (Abs(Mat.Value(1,3)-0.5) < 1e-15)
168 && (Abs(Mat.Value(2,1)-0.7071067811865476) < 1e-15)
169 && (Abs(Mat.Value(2,2)-0.5) < 1e-15)
170 && (Abs(Mat.Value(2,3)+0.5) < 1e-15)
172 && (Abs(Mat.Value(3,1)) < 1e-15)
173 && (Abs(Mat.Value(3,2)-0.7071067811865476) < 1e-15)
174 && (Abs(Mat.Value(3,3)-0.7071067811865476) < 1e-15)) {
177 else if( (Abs(Mat.Value(1,1)-1.0) < 1e-15)
178 && (Abs(Mat.Value(2,3)-1.0) < 1e-15)
179 && (Abs(Mat.Value(3,2)+1.0) < 1e-15)) {
180 return(2); //-- front
182 else if( (Abs(Mat.Value(1,1)-0.7071067811865476) < 1e-15)
183 && (Abs(Mat.Value(1,2)-0.7071067811865476) < 1e-15)
184 && (Abs(Mat.Value(1,3)) < 1e-15)
186 && (Abs(Mat.Value(2,1)+0.5) < 1e-15)
187 && (Abs(Mat.Value(2,2)-0.5) < 1e-15)
188 && (Abs(Mat.Value(2,3)-0.7071067811865476) < 1e-15)
190 && (Abs(Mat.Value(3,1)-0.5) < 1e-15)
191 && (Abs(Mat.Value(3,2)+0.5) < 1e-15)
192 && (Abs(Mat.Value(3,3)-0.7071067811865476) < 1e-15)) {
198 void Select3D_Projector::Scaled (const Standard_Boolean On)
203 //myGTrsf.SetTranslationPart(gp_XYZ(0.,0.,0.));
204 myType=TrsfType(myGTrsf);
211 //=======================================================================
214 //=======================================================================
216 void Select3D_Projector::Project (const gp_Pnt& P, gp_Pnt2d& Pout) const
219 if(!myView.IsNull()){
220 Standard_Real Xout,Yout;
223 myView->Project(P.X(),P.Y(),P.Z(),Xout,Yout);
225 Standard_Integer Xp,Yp;
226 myView->RefToPix(P.X(),P.Y(),P.Z(),Xp,Yp);
227 myView->Convert(Xp,Yp,Xout,Yout);
229 Pout.SetCoord(Xout,Yout);
235 case 0: { //-- axono standard
236 Standard_Real x07 = P.X()*0.7071067811865475;
237 Standard_Real y05 = P.Y()*0.5;
238 Standard_Real z05 = P.Z()*0.5;
241 //-- Z=0.7071067811865475*(P.Y()+P.Z());
245 X=P.X(); Y=P.Y(); //-- Z=P.Z();
250 X=P.X(); Y=P.Z(); //-- Z=-P.Y();
255 Standard_Real xmy05 = (P.X()-P.Y())*0.5;
256 Standard_Real z07 = P.Z()*0.7071067811865476;
257 X=0.7071067811865476*(P.X()+P.Y());
267 Standard_Real R = 1.-P2.Z()/myFocus;
268 Pout.SetCoord(P2.X()/R,P2.Y()/R);
271 Pout.SetCoord(P2.X(),P2.Y());
280 Standard_Real R = 1.-P2.Z()/myFocus;
281 Pout.SetCoord(P2.X()/R,P2.Y()/R);
284 Pout.SetCoord(P2.X(),P2.Y());
291 //=======================================================================
294 //=======================================================================
295 /* ====== TYPE 0 (??)
296 (0.7071067811865476, -0.5 , 0.4999999999999999)
297 (0.7071067811865475, 0.5000000000000001, -0.5 )
298 (0.0, 0.7071067811865475, 0.7071067811865476)
305 ======= TYPE 2 (front)
307 (0.0, 1.110223024625157e-16 , 1.0)
308 (0.0, -1.0 , 1.110223024625157e-16)
311 ( 0.7071067811865476, 0.7071067811865475, 0.0)
312 (-0.5 , 0.5000000000000001, 0.7071067811865475)
313 ( 0.4999999999999999, -0.5 , 0.7071067811865476)
315 void Select3D_Projector::Project (const gp_Pnt& P,
318 Standard_Real& Z) const
320 if(!myView.IsNull()){
321 // Standard_Real Xout,Yout;
324 myView->Project(P.X(),P.Y(),P.Z(),X,Y);
326 Standard_Integer Xp,Yp;
327 myView->RefToPix(P.X(),P.Y(),P.Z(),Xp,Yp);
328 myView->Convert(Xp,Yp,X,Y);
334 case 0: { //-- axono standard
335 Standard_Real x07 = P.X()*0.7071067811865475;
336 Standard_Real y05 = P.Y()*0.5;
337 Standard_Real z05 = P.Z()*0.5;
340 Z=0.7071067811865475*(P.Y()+P.Z());
344 X=P.X(); Y=P.Y(); Z=P.Z();
348 X=P.X(); Y=P.Z(); Z=-P.Y();
352 Standard_Real xmy05 = (P.X()-P.Y())*0.5;
353 Standard_Real z07 = P.Z()*0.7071067811865476;
354 X=0.7071067811865476*(P.X()+P.Y());
372 Standard_Real R = 1 - Z / myFocus;
379 //=======================================================================
382 //=======================================================================
384 void Select3D_Projector::Project (const gp_Pnt& P,
387 gp_Vec2d& D1out) const
394 Standard_Real R = 1. - PP.Z() / myFocus;
395 Pout .SetCoord(PP .X()/R , PP.Y()/R);
396 D1out.SetCoord(DD1.X()/R + PP.X()*DD1.Z()/(myFocus * R*R),
397 DD1.Y()/R + PP.Y()*DD1.Z()/(myFocus * R*R));
400 Pout .SetCoord(PP .X(),PP .Y());
401 D1out.SetCoord(DD1.X(),DD1.Y());
406 //=======================================================================
409 //=======================================================================
411 gp_Lin Select3D_Projector::Shoot
412 (const Standard_Real X,
413 const Standard_Real Y) const
418 L = gp_Lin(gp_Pnt(0,0, myFocus),
419 gp_Dir(X,Y,-myFocus));
422 L = gp_Lin(gp_Pnt(X,Y,0),
425 Transform(L, myInvTrsf);
430 void Select3D_Projector::SetView(const Handle(V3d_View)& aViou)
433 myPersp = aViou->Type()==V3d_PERSPECTIVE;
434 myFocus= aViou->Focale();
435 Standard_Real Xat,Yat,Zat,XUp,YUp,ZUp,DX,DY,DZ;
436 //Standard_Boolean Pers=Standard_False;
438 aViou->At(Xat,Yat,Zat);
439 aViou->Up(XUp,YUp,ZUp);
440 aViou->Proj(DX,DY,DZ);
441 gp_Pnt At (Xat,Yat,Zat);
442 gp_Dir Zpers (DX,DY,DZ);
443 gp_Dir Ypers (XUp,YUp,ZUp);
444 gp_Dir Xpers = Ypers.Crossed(Zpers);
445 gp_Ax3 Axe (At, Zpers, Xpers);
446 myScaledTrsf.SetTransformation(Axe);
451 void Select3D_Projector::DepthMinMax (const Standard_Real theDepthMin,
452 const Standard_Real theDepthMax)
454 myDepthMin = theDepthMin;
455 myDepthMax = theDepthMax;