0022683: Incorrect result of Select3D_Projector::Project() in the case of perspective...
[occt.git] / src / Select3D / Select3D_Projector.cxx
CommitLineData
7fd59977 1// File: Select3D_Projector.cxx
2// Created: Fri Mar 13 11:08:32 1992
3// Author: Christophe MARION
4// <cma@sdsun2>
5
4952a30a 6#define IMP240100 //GG
7fd59977 7// Change RefToPix()/Convert() to Project() method.
8
9#include <Select3D_Projector.ixx>
10#include <Precision.hxx>
11#include <gp_Ax3.hxx>
12#include <gp_Vec.hxx>
13#include <gp_Vec2d.hxx>
14
15// formula for derivating a perspective, from Mathematica
16
4952a30a 17// X'[t] X[t] Z'[t]
18// D1 = -------- + -------------
19// Z[t] Z[t] 2
20// 1 - ---- f (1 - ----)
21// f f
7fd59977 22
23//=======================================================================
24//function : Select3D_Projector
4952a30a 25//purpose :
7fd59977 26//=======================================================================
27
4952a30a 28Select3D_Projector::Select3D_Projector(const Handle(V3d_View)& aViou)
29: myPersp(aViou->Type()==V3d_PERSPECTIVE),
30 myFocus(aViou->Focale()),
4952a30a 31 myView(aViou),
32 myDepthMin(-Precision::Infinite()),
33 myDepthMax( Precision::Infinite())
7fd59977 34{
35 Standard_Real Xat,Yat,Zat,XUp,YUp,ZUp,DX,DY,DZ;
36 //Standard_Boolean Pers=Standard_False;
4952a30a 37
7fd59977 38 aViou->At(Xat,Yat,Zat);
39 aViou->Up(XUp,YUp,ZUp);
40 aViou->Proj(DX,DY,DZ);
4952a30a 41 gp_Pnt At (Xat,Yat,Zat);
7fd59977 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);
48 Scaled();
4952a30a 49
7fd59977 50}
51
4952a30a 52//=======================================================================
53//function : Select3D_Projector
54//purpose :
55//=======================================================================
56
57Select3D_Projector::Select3D_Projector()
58: myPersp(Standard_False),
59 myFocus(0),
4952a30a 60 myDepthMin(-Precision::Infinite()),
61 myDepthMax( Precision::Infinite())
7fd59977 62{
63 Scaled();
4952a30a 64}
7fd59977 65
66//=======================================================================
67//function : Select3D_Projector
4952a30a 68//purpose :
7fd59977 69//=======================================================================
70
4952a30a 71Select3D_Projector::Select3D_Projector (const gp_Ax2& CS)
72: myPersp(Standard_False),
73 myFocus(0),
74 myDepthMin(-Precision::Infinite()),
75 myDepthMax( Precision::Infinite())
7fd59977 76{
77 myScaledTrsf.SetTransformation(CS);
78 myGTrsf.SetTrsf(myScaledTrsf);
79 Scaled();
7fd59977 80}
81
82//=======================================================================
83//function : Select3D_Projector
4952a30a 84//purpose :
7fd59977 85//=======================================================================
86
4952a30a 87Select3D_Projector::Select3D_Projector (const gp_Ax2& CS,
88 const Standard_Real Focus)
89: myPersp(Standard_True),
90 myFocus(Focus),
91 myDepthMin(-Precision::Infinite()),
92 myDepthMax( Precision::Infinite())
7fd59977 93{
94 myScaledTrsf.SetTransformation(CS);
95 myGTrsf.SetTrsf(myScaledTrsf);
96 Scaled();
7fd59977 97}
98
99//=======================================================================
100//function : Select3D_Projector
4952a30a 101//purpose :
7fd59977 102//=======================================================================
103
4952a30a 104Select3D_Projector::Select3D_Projector (const gp_Trsf& T,
105 const Standard_Boolean Persp,
106 const Standard_Real Focus)
107: myPersp(Persp),
7fd59977 108 myFocus(Focus),
4952a30a 109 myScaledTrsf(T),
110 myDepthMin(-Precision::Infinite()),
111 myDepthMax( Precision::Infinite())
7fd59977 112{
113 myGTrsf.SetTrsf(myScaledTrsf);
114 Scaled();
115}
116
117//=======================================================================
118//function : Select3D_Projector
4952a30a 119//purpose :
7fd59977 120//=======================================================================
121
4952a30a 122Select3D_Projector::Select3D_Projector (const gp_GTrsf& GT,
123 const Standard_Boolean Persp,
124 const Standard_Real Focus)
125: myPersp(Persp),
7fd59977 126 myFocus(Focus),
4952a30a 127 myGTrsf(GT),
128 myDepthMin(-Precision::Infinite()),
129 myDepthMax( Precision::Infinite())
7fd59977 130{
131 Scaled();
7fd59977 132}
133
7fd59977 134//=======================================================================
135//function : Set
4952a30a 136//purpose :
7fd59977 137//=======================================================================
138
139void Select3D_Projector::Set
4952a30a 140 (const gp_Trsf& T,
7fd59977 141 const Standard_Boolean Persp,
4952a30a 142 const Standard_Real Focus)
7fd59977 143{
144 myPersp = Persp;
145 myFocus = Focus;
146 myScaledTrsf = T;
147 Scaled();
7fd59977 148}
149
150//=======================================================================
151//function : Scaled
4952a30a 152//purpose :
7fd59977 153//=======================================================================
154
155#include <gp_Mat.hxx>
156
4952a30a 157static Standard_Integer TrsfType(const gp_GTrsf& Trsf) {
7fd59977 158 const gp_Mat& Mat = Trsf.VectorialPart();
4952a30a 159 if( (Abs(Mat.Value(1,1)-1.0) < 1e-15)
7fd59977 160 && (Abs(Mat.Value(2,2)-1.0) < 1e-15)
4952a30a 161 && (Abs(Mat.Value(3,3)-1.0) < 1e-15)) {
7fd59977 162 return(1); //-- top
163 }
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)
4952a30a 167
7fd59977 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)
4952a30a 171
172 && (Abs(Mat.Value(3,1)) < 1e-15)
7fd59977 173 && (Abs(Mat.Value(3,2)-0.7071067811865476) < 1e-15)
4952a30a 174 && (Abs(Mat.Value(3,3)-0.7071067811865476) < 1e-15)) {
175 return(0); //--
7fd59977 176 }
4952a30a 177 else if( (Abs(Mat.Value(1,1)-1.0) < 1e-15)
7fd59977 178 && (Abs(Mat.Value(2,3)-1.0) < 1e-15)
4952a30a 179 && (Abs(Mat.Value(3,2)+1.0) < 1e-15)) {
7fd59977 180 return(2); //-- front
181 }
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)
4952a30a 185
7fd59977 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)
4952a30a 189
190 && (Abs(Mat.Value(3,1)-0.5) < 1e-15)
7fd59977 191 && (Abs(Mat.Value(3,2)+0.5) < 1e-15)
4952a30a 192 && (Abs(Mat.Value(3,3)-0.7071067811865476) < 1e-15)) {
7fd59977 193 return(3); //-- axo
194 }
195 return(-1);
196}
197
198void Select3D_Projector::Scaled (const Standard_Boolean On)
4952a30a 199{
7fd59977 200 myType=-1;
201 if (!On) {
4952a30a 202 if (!myPersp) {
7fd59977 203 //myGTrsf.SetTranslationPart(gp_XYZ(0.,0.,0.));
4952a30a 204 myType=TrsfType(myGTrsf);
7fd59977 205 }
206 }
207 myInvTrsf = myGTrsf;
208 myInvTrsf.Invert();
209}
210
211//=======================================================================
212//function : Project
4952a30a 213//purpose :
7fd59977 214//=======================================================================
215
216void Select3D_Projector::Project (const gp_Pnt& P, gp_Pnt2d& Pout) const
217{
218
219 if(!myView.IsNull()){
220 Standard_Real Xout,Yout;
221// V3d_View
222#ifdef IMP240100
223 myView->Project(P.X(),P.Y(),P.Z(),Xout,Yout);
224#else
225 Standard_Integer Xp,Yp;
226 myView->RefToPix(P.X(),P.Y(),P.Z(),Xp,Yp);
227 myView->Convert(Xp,Yp,Xout,Yout);
228#endif
229 Pout.SetCoord(Xout,Yout);
230 }
231 else{
4952a30a 232 if(myType!=-1) {
7fd59977 233 Standard_Real X,Y;
4952a30a 234 switch (myType) {
7fd59977 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;
239 X=x07-y05+z05;
240 Y=x07+y05-z05;
241 //-- Z=0.7071067811865475*(P.Y()+P.Z());
242 break;
243 }
244 case 1: { //-- top
245 X=P.X(); Y=P.Y(); //-- Z=P.Z();
246 Pout.SetCoord(X,Y);
247 break;
248 }
4952a30a 249 case 2: {
7fd59977 250 X=P.X(); Y=P.Z(); //-- Z=-P.Y();
251 Pout.SetCoord(X,Y);
252 break;
253 }
4952a30a 254 case 3: {
7fd59977 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());
258 Y=-xmy05+z07;
259 Pout.SetCoord(X,Y);
260 //-- Z= xmy05+z07;
261 break;
262 }
4952a30a 263 default: {
7fd59977 264 gp_Pnt P2 = P;
265 Transform(P2);
266 if (myPersp) {
267 Standard_Real R = 1.-P2.Z()/myFocus;
268 Pout.SetCoord(P2.X()/R,P2.Y()/R);
269 }
4952a30a 270 else
271 Pout.SetCoord(P2.X(),P2.Y());
7fd59977 272 break;
273 }
274 }
275 }
4952a30a 276 else {
7fd59977 277 gp_Pnt P2 = P;
278 Transform(P2);
279 if (myPersp) {
280 Standard_Real R = 1.-P2.Z()/myFocus;
281 Pout.SetCoord(P2.X()/R,P2.Y()/R);
282 }
4952a30a 283 else
284 Pout.SetCoord(P2.X(),P2.Y());
7fd59977 285 }
286 }
4952a30a 287
7fd59977 288
289}
290
291//=======================================================================
292//function : Project
4952a30a 293//purpose :
7fd59977 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)
299
300 ====== TYPE 1 (top)
301(1.0, 0.0, 0.0)
302(0.0, 1.0, 0.0)
303(0.0, 0.0, 1.0)
304
305 ======= TYPE 2 (front)
306(1.0, 0.0 , 0.0)
307(0.0, 1.110223024625157e-16 , 1.0)
308(0.0, -1.0 , 1.110223024625157e-16)
309
4952a30a 310 ======= TYPE 3
7fd59977 311( 0.7071067811865476, 0.7071067811865475, 0.0)
312(-0.5 , 0.5000000000000001, 0.7071067811865475)
313( 0.4999999999999999, -0.5 , 0.7071067811865476)
314*/
315void Select3D_Projector::Project (const gp_Pnt& P,
316 Standard_Real& X,
317 Standard_Real& Y,
318 Standard_Real& Z) const
319{
320 if(!myView.IsNull()){
321// Standard_Real Xout,Yout;
322// V3d_View
323#ifdef IMP240100
324 myView->Project(P.X(),P.Y(),P.Z(),X,Y);
325#else
326 Standard_Integer Xp,Yp;
327 myView->RefToPix(P.X(),P.Y(),P.Z(),Xp,Yp);
328 myView->Convert(Xp,Yp,X,Y);
329#endif
330 }
331 else{
4952a30a 332 if(myType!=-1) {
333 switch (myType) {
7fd59977 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;
338 X=x07-y05+z05;
339 Y=x07+y05-z05;
340 Z=0.7071067811865475*(P.Y()+P.Z());
341 break;
342 }
343 case 1: { //-- top
344 X=P.X(); Y=P.Y(); Z=P.Z();
345 break;
346 }
4952a30a 347 case 2: {
7fd59977 348 X=P.X(); Y=P.Z(); Z=-P.Y();
349 break;
350 }
4952a30a 351 case 3: {
7fd59977 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());
355 Y=-xmy05+z07;
356 Z= xmy05+z07;
357 break;
358 }
4952a30a 359 default: {
7fd59977 360 gp_Pnt P2 = P;
361 Transform(P2);
362 P2.Coord(X,Y,Z);
363 break;
364 }
365 }
366 }
4952a30a 367 else {
7fd59977 368 gp_Pnt P2 = P;
369 Transform(P2);
370 P2.Coord(X,Y,Z);
371 if (myPersp) {
372 Standard_Real R = 1 - Z / myFocus;
373 X = X / R;
374 Y = Y / R;
375 }
376 }
377 }
378}
379//=======================================================================
380//function : Project
4952a30a 381//purpose :
7fd59977 382//=======================================================================
383
384void Select3D_Projector::Project (const gp_Pnt& P,
385 const gp_Vec& D1,
386 gp_Pnt2d& Pout,
387 gp_Vec2d& D1out) const
388{
389 gp_Pnt PP = P;
390 Transform(PP);
391 gp_Vec DD1 = D1;
392 Transform(DD1);
393 if (myPersp) {
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));
398 }
399 else {
400 Pout .SetCoord(PP .X(),PP .Y());
401 D1out.SetCoord(DD1.X(),DD1.Y());
402 }
403}
404
7fd59977 405
406//=======================================================================
407//function : Shoot
4952a30a 408//purpose :
7fd59977 409//=======================================================================
410
411gp_Lin Select3D_Projector::Shoot
412 (const Standard_Real X,
413 const Standard_Real Y) const
414{
415 gp_Lin L;
4952a30a 416
7fd59977 417 if (myPersp) {
418 L = gp_Lin(gp_Pnt(0,0, myFocus),
419 gp_Dir(X,Y,-myFocus));
420 }
421 else {
422 L = gp_Lin(gp_Pnt(X,Y,0),
423 gp_Dir(0,0,-1));
424 }
425 Transform(L, myInvTrsf);
426 return L;
427}
428
429
7fd59977 430void Select3D_Projector::SetView(const Handle(V3d_View)& aViou)
431{
432 myView = 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;
4952a30a 437
7fd59977 438 aViou->At(Xat,Yat,Zat);
439 aViou->Up(XUp,YUp,ZUp);
440 aViou->Proj(DX,DY,DZ);
4952a30a 441 gp_Pnt At (Xat,Yat,Zat);
7fd59977 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);
447 Scaled();
4952a30a 448
449}
450
451void Select3D_Projector::DepthMinMax (const Standard_Real theDepthMin,
452 const Standard_Real theDepthMax)
453{
454 myDepthMin = theDepthMin;
455 myDepthMax = theDepthMax;
7fd59977 456}