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 //=======================================================================
29 Select3D_Projector::Select3D_Projector(const Handle(V3d_View)& aViou):
30 myPersp(aViou->Type()==V3d_PERSPECTIVE),
31 myFocus(aViou->Focale()),
37 Standard_Real Xat,Yat,Zat,XUp,YUp,ZUp,DX,DY,DZ;
38 //Standard_Boolean Pers=Standard_False;
40 aViou->At(Xat,Yat,Zat);
41 aViou->Up(XUp,YUp,ZUp);
42 aViou->Proj(DX,DY,DZ);
43 gp_Pnt At (Xat,Yat,Zat);
44 gp_Dir Zpers (DX,DY,DZ);
45 gp_Dir Ypers (XUp,YUp,ZUp);
46 gp_Dir Xpers = Ypers.Crossed(Zpers);
47 gp_Ax3 Axe (At, Zpers, Xpers);
48 myScaledTrsf.SetTransformation(Axe);
49 myGTrsf.SetTrsf(myScaledTrsf);
54 Select3D_Projector::Select3D_Projector () :
55 myPersp(Standard_False),myFocus(0),myD1(1,0),myD2(0,1),myD3(1,1)
61 //=======================================================================
62 //function : Select3D_Projector
64 //=======================================================================
66 Select3D_Projector::Select3D_Projector
68 myPersp(Standard_False), myFocus(0)
70 myScaledTrsf.SetTransformation(CS);
71 myGTrsf.SetTrsf(myScaledTrsf);
76 //=======================================================================
77 //function : Select3D_Projector
79 //=======================================================================
81 Select3D_Projector::Select3D_Projector
83 const Standard_Real Focus) :
84 myPersp(Standard_True), myFocus(Focus)
86 myScaledTrsf.SetTransformation(CS);
87 myGTrsf.SetTrsf(myScaledTrsf);
92 //=======================================================================
93 //function : Select3D_Projector
95 //=======================================================================
97 Select3D_Projector::Select3D_Projector
99 const Standard_Boolean Persp,
100 const Standard_Real Focus) :
105 myGTrsf.SetTrsf(myScaledTrsf);
110 //=======================================================================
111 //function : Select3D_Projector
113 //=======================================================================
115 Select3D_Projector::Select3D_Projector
117 const Standard_Boolean Persp,
118 const Standard_Real Focus,
121 const gp_Vec2d& v3) :
129 myGTrsf.SetTrsf(myScaledTrsf);
133 //=======================================================================
134 //function : Select3D_Projector
136 //=======================================================================
138 Select3D_Projector::Select3D_Projector
140 const Standard_Boolean Persp,
141 const Standard_Real Focus) :
150 void Select3D_Projector::Delete()
153 //=======================================================================
156 //=======================================================================
158 void Select3D_Projector::Set
160 const Standard_Boolean Persp,
161 const Standard_Real Focus)
170 //=======================================================================
173 //=======================================================================
175 #include <gp_Mat.hxx>
177 static Standard_Integer TrsfType(const gp_GTrsf& Trsf) {
178 const gp_Mat& Mat = Trsf.VectorialPart();
179 if( (Abs(Mat.Value(1,1)-1.0) < 1e-15)
180 && (Abs(Mat.Value(2,2)-1.0) < 1e-15)
181 && (Abs(Mat.Value(3,3)-1.0) < 1e-15)) {
184 else if( (Abs(Mat.Value(1,1)-0.7071067811865476) < 1e-15)
185 && (Abs(Mat.Value(1,2)+0.5) < 1e-15)
186 && (Abs(Mat.Value(1,3)-0.5) < 1e-15)
188 && (Abs(Mat.Value(2,1)-0.7071067811865476) < 1e-15)
189 && (Abs(Mat.Value(2,2)-0.5) < 1e-15)
190 && (Abs(Mat.Value(2,3)+0.5) < 1e-15)
192 && (Abs(Mat.Value(3,1)) < 1e-15)
193 && (Abs(Mat.Value(3,2)-0.7071067811865476) < 1e-15)
194 && (Abs(Mat.Value(3,3)-0.7071067811865476) < 1e-15)) {
197 else if( (Abs(Mat.Value(1,1)-1.0) < 1e-15)
198 && (Abs(Mat.Value(2,3)-1.0) < 1e-15)
199 && (Abs(Mat.Value(3,2)+1.0) < 1e-15)) {
200 return(2); //-- front
202 else if( (Abs(Mat.Value(1,1)-0.7071067811865476) < 1e-15)
203 && (Abs(Mat.Value(1,2)-0.7071067811865476) < 1e-15)
204 && (Abs(Mat.Value(1,3)) < 1e-15)
206 && (Abs(Mat.Value(2,1)+0.5) < 1e-15)
207 && (Abs(Mat.Value(2,2)-0.5) < 1e-15)
208 && (Abs(Mat.Value(2,3)-0.7071067811865476) < 1e-15)
210 && (Abs(Mat.Value(3,1)-0.5) < 1e-15)
211 && (Abs(Mat.Value(3,2)+0.5) < 1e-15)
212 && (Abs(Mat.Value(3,3)-0.7071067811865476) < 1e-15)) {
218 void Select3D_Projector::Scaled (const Standard_Boolean On)
223 //myGTrsf.SetTranslationPart(gp_XYZ(0.,0.,0.));
224 myType=TrsfType(myGTrsf);
231 //=======================================================================
234 //=======================================================================
236 void Select3D_Projector::Project (const gp_Pnt& P, gp_Pnt2d& Pout) const
239 if(!myView.IsNull()){
240 Standard_Real Xout,Yout;
243 myView->Project(P.X(),P.Y(),P.Z(),Xout,Yout);
245 Standard_Integer Xp,Yp;
246 myView->RefToPix(P.X(),P.Y(),P.Z(),Xp,Yp);
247 myView->Convert(Xp,Yp,Xout,Yout);
249 Pout.SetCoord(Xout,Yout);
255 case 0: { //-- axono standard
256 Standard_Real x07 = P.X()*0.7071067811865475;
257 Standard_Real y05 = P.Y()*0.5;
258 Standard_Real z05 = P.Z()*0.5;
261 //-- Z=0.7071067811865475*(P.Y()+P.Z());
265 X=P.X(); Y=P.Y(); //-- Z=P.Z();
270 X=P.X(); Y=P.Z(); //-- Z=-P.Y();
275 Standard_Real xmy05 = (P.X()-P.Y())*0.5;
276 Standard_Real z07 = P.Z()*0.7071067811865476;
277 X=0.7071067811865476*(P.X()+P.Y());
287 Standard_Real R = 1.-P2.Z()/myFocus;
288 Pout.SetCoord(P2.X()/R,P2.Y()/R);
291 Pout.SetCoord(P2.X(),P2.Y());
300 Standard_Real R = 1.-P2.Z()/myFocus;
301 Pout.SetCoord(P2.X()/R,P2.Y()/R);
304 Pout.SetCoord(P2.X(),P2.Y());
311 //=======================================================================
314 //=======================================================================
315 /* ====== TYPE 0 (??)
316 (0.7071067811865476, -0.5 , 0.4999999999999999)
317 (0.7071067811865475, 0.5000000000000001, -0.5 )
318 (0.0, 0.7071067811865475, 0.7071067811865476)
325 ======= TYPE 2 (front)
327 (0.0, 1.110223024625157e-16 , 1.0)
328 (0.0, -1.0 , 1.110223024625157e-16)
331 ( 0.7071067811865476, 0.7071067811865475, 0.0)
332 (-0.5 , 0.5000000000000001, 0.7071067811865475)
333 ( 0.4999999999999999, -0.5 , 0.7071067811865476)
335 void Select3D_Projector::Project (const gp_Pnt& P,
338 Standard_Real& Z) const
340 if(!myView.IsNull()){
341 // Standard_Real Xout,Yout;
344 myView->Project(P.X(),P.Y(),P.Z(),X,Y);
346 Standard_Integer Xp,Yp;
347 myView->RefToPix(P.X(),P.Y(),P.Z(),Xp,Yp);
348 myView->Convert(Xp,Yp,X,Y);
354 case 0: { //-- axono standard
355 Standard_Real x07 = P.X()*0.7071067811865475;
356 Standard_Real y05 = P.Y()*0.5;
357 Standard_Real z05 = P.Z()*0.5;
360 Z=0.7071067811865475*(P.Y()+P.Z());
364 X=P.X(); Y=P.Y(); Z=P.Z();
368 X=P.X(); Y=P.Z(); Z=-P.Y();
372 Standard_Real xmy05 = (P.X()-P.Y())*0.5;
373 Standard_Real z07 = P.Z()*0.7071067811865476;
374 X=0.7071067811865476*(P.X()+P.Y());
392 Standard_Real R = 1 - Z / myFocus;
399 //=======================================================================
402 //=======================================================================
404 void Select3D_Projector::Project (const gp_Pnt& P,
407 gp_Vec2d& D1out) const
414 Standard_Real R = 1. - PP.Z() / myFocus;
415 Pout .SetCoord(PP .X()/R , PP.Y()/R);
416 D1out.SetCoord(DD1.X()/R + PP.X()*DD1.Z()/(myFocus * R*R),
417 DD1.Y()/R + PP.Y()*DD1.Z()/(myFocus * R*R));
420 Pout .SetCoord(PP .X(),PP .Y());
421 D1out.SetCoord(DD1.X(),DD1.Y());
425 //=======================================================================
428 //=======================================================================
430 void Select3D_Projector::BoxAdd
434 gp_Vec2d V(P.X(),P.Y());
435 gp_Pnt PP(myD1 * V, myD2 * V, myD3 * V);
439 //=======================================================================
442 //=======================================================================
444 gp_Lin Select3D_Projector::Shoot
445 (const Standard_Real X,
446 const Standard_Real Y) const
451 L = gp_Lin(gp_Pnt(0,0, myFocus),
452 gp_Dir(X,Y,-myFocus));
455 L = gp_Lin(gp_Pnt(X,Y,0),
458 Transform(L, myInvTrsf);
463 //=======================================================================
464 //function : SetDirection
466 //=======================================================================
468 void Select3D_Projector::SetDirection ()
472 if ((Abs(V1.X()) + Abs(V1.Y())) < Precision::Angular()) V1.SetCoord(1,1,0);
473 gp_Vec2d D1(V1.X(),V1.Y());
474 myD1.SetCoord(-D1.Y(),D1.X());
477 if ((Abs(V2.X()) + Abs(V2.Y())) < Precision::Angular()) V2.SetCoord(1,1,0);
478 gp_Vec2d D2(V2.X(),V2.Y());
479 myD2.SetCoord(-D2.Y(),D2.X());
482 if ((Abs(V3.X()) + Abs(V3.Y())) < Precision::Angular()) V3.SetCoord(1,1,0);
483 gp_Vec2d D3(V3.X(),V3.Y());
484 myD3.SetCoord(-D3.Y(),D3.X());
487 void Select3D_Projector::SetView(const Handle(V3d_View)& aViou)
490 myPersp = aViou->Type()==V3d_PERSPECTIVE;
491 myFocus= aViou->Focale();
492 Standard_Real Xat,Yat,Zat,XUp,YUp,ZUp,DX,DY,DZ;
493 //Standard_Boolean Pers=Standard_False;
495 aViou->At(Xat,Yat,Zat);
496 aViou->Up(XUp,YUp,ZUp);
497 aViou->Proj(DX,DY,DZ);
498 gp_Pnt At (Xat,Yat,Zat);
499 gp_Dir Zpers (DX,DY,DZ);
500 gp_Dir Ypers (XUp,YUp,ZUp);
501 gp_Dir Xpers = Ypers.Crossed(Zpers);
502 gp_Ax3 Axe (At, Zpers, Xpers);
503 myScaledTrsf.SetTransformation(Axe);