1 // Created on: 1992-03-13
2 // Created by: Christophe MARION
3 // Copyright (c) 1992-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
22 #define IMP240100 //GG
23 // Change RefToPix()/Convert() to Project() method.
25 #include <Select3D_Projector.ixx>
26 #include <Precision.hxx>
29 #include <gp_Vec2d.hxx>
31 // formula for derivating a perspective, from Mathematica
34 // D1 = -------- + -------------
36 // 1 - ---- f (1 - ----)
39 //=======================================================================
40 //function : Select3D_Projector
42 //=======================================================================
44 Select3D_Projector::Select3D_Projector(const Handle(V3d_View)& aViou)
45 : myPersp(aViou->Type()==V3d_PERSPECTIVE),
46 myFocus(aViou->Focale()),
48 myDepthMin(-Precision::Infinite()),
49 myDepthMax( Precision::Infinite())
51 Standard_Real Xat,Yat,Zat,XUp,YUp,ZUp,DX,DY,DZ;
52 //Standard_Boolean Pers=Standard_False;
54 aViou->At(Xat,Yat,Zat);
55 aViou->Up(XUp,YUp,ZUp);
56 aViou->Proj(DX,DY,DZ);
57 gp_Pnt At (Xat,Yat,Zat);
58 gp_Dir Zpers (DX,DY,DZ);
59 gp_Dir Ypers (XUp,YUp,ZUp);
60 gp_Dir Xpers = Ypers.Crossed(Zpers);
61 gp_Ax3 Axe (At, Zpers, Xpers);
62 myScaledTrsf.SetTransformation(Axe);
63 myGTrsf.SetTrsf(myScaledTrsf);
68 //=======================================================================
69 //function : Select3D_Projector
71 //=======================================================================
73 Select3D_Projector::Select3D_Projector()
74 : myPersp(Standard_False),
76 myDepthMin(-Precision::Infinite()),
77 myDepthMax( Precision::Infinite())
82 //=======================================================================
83 //function : Select3D_Projector
85 //=======================================================================
87 Select3D_Projector::Select3D_Projector (const gp_Ax2& CS)
88 : myPersp(Standard_False),
90 myDepthMin(-Precision::Infinite()),
91 myDepthMax( Precision::Infinite())
93 myScaledTrsf.SetTransformation(CS);
94 myGTrsf.SetTrsf(myScaledTrsf);
98 //=======================================================================
99 //function : Select3D_Projector
101 //=======================================================================
103 Select3D_Projector::Select3D_Projector (const gp_Ax2& CS,
104 const Standard_Real Focus)
105 : myPersp(Standard_True),
107 myDepthMin(-Precision::Infinite()),
108 myDepthMax( Precision::Infinite())
110 myScaledTrsf.SetTransformation(CS);
111 myGTrsf.SetTrsf(myScaledTrsf);
115 //=======================================================================
116 //function : Select3D_Projector
118 //=======================================================================
120 Select3D_Projector::Select3D_Projector (const gp_Trsf& T,
121 const Standard_Boolean Persp,
122 const Standard_Real Focus)
126 myDepthMin(-Precision::Infinite()),
127 myDepthMax( Precision::Infinite())
129 myGTrsf.SetTrsf(myScaledTrsf);
133 //=======================================================================
134 //function : Select3D_Projector
136 //=======================================================================
138 Select3D_Projector::Select3D_Projector (const gp_GTrsf& GT,
139 const Standard_Boolean Persp,
140 const Standard_Real Focus)
144 myDepthMin(-Precision::Infinite()),
145 myDepthMax( Precision::Infinite())
150 //=======================================================================
153 //=======================================================================
155 void Select3D_Projector::Set
157 const Standard_Boolean Persp,
158 const Standard_Real Focus)
166 //=======================================================================
169 //=======================================================================
171 #include <gp_Mat.hxx>
173 static Standard_Integer TrsfType(const gp_GTrsf& Trsf) {
174 const gp_Mat& Mat = Trsf.VectorialPart();
175 if( (Abs(Mat.Value(1,1)-1.0) < 1e-15)
176 && (Abs(Mat.Value(2,2)-1.0) < 1e-15)
177 && (Abs(Mat.Value(3,3)-1.0) < 1e-15)) {
180 else if( (Abs(Mat.Value(1,1)-0.7071067811865476) < 1e-15)
181 && (Abs(Mat.Value(1,2)+0.5) < 1e-15)
182 && (Abs(Mat.Value(1,3)-0.5) < 1e-15)
184 && (Abs(Mat.Value(2,1)-0.7071067811865476) < 1e-15)
185 && (Abs(Mat.Value(2,2)-0.5) < 1e-15)
186 && (Abs(Mat.Value(2,3)+0.5) < 1e-15)
188 && (Abs(Mat.Value(3,1)) < 1e-15)
189 && (Abs(Mat.Value(3,2)-0.7071067811865476) < 1e-15)
190 && (Abs(Mat.Value(3,3)-0.7071067811865476) < 1e-15)) {
193 else if( (Abs(Mat.Value(1,1)-1.0) < 1e-15)
194 && (Abs(Mat.Value(2,3)-1.0) < 1e-15)
195 && (Abs(Mat.Value(3,2)+1.0) < 1e-15)) {
196 return(2); //-- front
198 else if( (Abs(Mat.Value(1,1)-0.7071067811865476) < 1e-15)
199 && (Abs(Mat.Value(1,2)-0.7071067811865476) < 1e-15)
200 && (Abs(Mat.Value(1,3)) < 1e-15)
202 && (Abs(Mat.Value(2,1)+0.5) < 1e-15)
203 && (Abs(Mat.Value(2,2)-0.5) < 1e-15)
204 && (Abs(Mat.Value(2,3)-0.7071067811865476) < 1e-15)
206 && (Abs(Mat.Value(3,1)-0.5) < 1e-15)
207 && (Abs(Mat.Value(3,2)+0.5) < 1e-15)
208 && (Abs(Mat.Value(3,3)-0.7071067811865476) < 1e-15)) {
214 void Select3D_Projector::Scaled (const Standard_Boolean On)
219 //myGTrsf.SetTranslationPart(gp_XYZ(0.,0.,0.));
220 myType=TrsfType(myGTrsf);
227 //=======================================================================
230 //=======================================================================
232 void Select3D_Projector::Project (const gp_Pnt& P, gp_Pnt2d& Pout) const
235 if(!myView.IsNull()){
236 Standard_Real Xout,Yout;
239 myView->Project(P.X(),P.Y(),P.Z(),Xout,Yout);
241 Standard_Integer Xp,Yp;
242 myView->RefToPix(P.X(),P.Y(),P.Z(),Xp,Yp);
243 myView->Convert(Xp,Yp,Xout,Yout);
245 Pout.SetCoord(Xout,Yout);
251 case 0: { //-- axono standard
252 Standard_Real x07 = P.X()*0.7071067811865475;
253 Standard_Real y05 = P.Y()*0.5;
254 Standard_Real z05 = P.Z()*0.5;
257 //-- Z=0.7071067811865475*(P.Y()+P.Z());
261 X=P.X(); Y=P.Y(); //-- Z=P.Z();
266 X=P.X(); Y=P.Z(); //-- Z=-P.Y();
271 Standard_Real xmy05 = (P.X()-P.Y())*0.5;
272 Standard_Real z07 = P.Z()*0.7071067811865476;
273 X=0.7071067811865476*(P.X()+P.Y());
283 Standard_Real R = 1.-P2.Z()/myFocus;
284 Pout.SetCoord(P2.X()/R,P2.Y()/R);
287 Pout.SetCoord(P2.X(),P2.Y());
296 Standard_Real R = 1.-P2.Z()/myFocus;
297 Pout.SetCoord(P2.X()/R,P2.Y()/R);
300 Pout.SetCoord(P2.X(),P2.Y());
307 //=======================================================================
310 //=======================================================================
311 /* ====== TYPE 0 (??)
312 (0.7071067811865476, -0.5 , 0.4999999999999999)
313 (0.7071067811865475, 0.5000000000000001, -0.5 )
314 (0.0, 0.7071067811865475, 0.7071067811865476)
321 ======= TYPE 2 (front)
323 (0.0, 1.110223024625157e-16 , 1.0)
324 (0.0, -1.0 , 1.110223024625157e-16)
327 ( 0.7071067811865476, 0.7071067811865475, 0.0)
328 (-0.5 , 0.5000000000000001, 0.7071067811865475)
329 ( 0.4999999999999999, -0.5 , 0.7071067811865476)
331 void Select3D_Projector::Project (const gp_Pnt& P,
334 Standard_Real& Z) const
336 if(!myView.IsNull()){
337 // Standard_Real Xout,Yout;
340 myView->Project(P.X(),P.Y(),P.Z(),X,Y);
342 Standard_Integer Xp,Yp;
343 myView->RefToPix(P.X(),P.Y(),P.Z(),Xp,Yp);
344 myView->Convert(Xp,Yp,X,Y);
350 case 0: { //-- axono standard
351 Standard_Real x07 = P.X()*0.7071067811865475;
352 Standard_Real y05 = P.Y()*0.5;
353 Standard_Real z05 = P.Z()*0.5;
356 Z=0.7071067811865475*(P.Y()+P.Z());
360 X=P.X(); Y=P.Y(); Z=P.Z();
364 X=P.X(); Y=P.Z(); Z=-P.Y();
368 Standard_Real xmy05 = (P.X()-P.Y())*0.5;
369 Standard_Real z07 = P.Z()*0.7071067811865476;
370 X=0.7071067811865476*(P.X()+P.Y());
388 Standard_Real R = 1 - Z / myFocus;
395 //=======================================================================
398 //=======================================================================
400 void Select3D_Projector::Project (const gp_Pnt& P,
403 gp_Vec2d& D1out) const
410 Standard_Real R = 1. - PP.Z() / myFocus;
411 Pout .SetCoord(PP .X()/R , PP.Y()/R);
412 D1out.SetCoord(DD1.X()/R + PP.X()*DD1.Z()/(myFocus * R*R),
413 DD1.Y()/R + PP.Y()*DD1.Z()/(myFocus * R*R));
416 Pout .SetCoord(PP .X(),PP .Y());
417 D1out.SetCoord(DD1.X(),DD1.Y());
422 //=======================================================================
425 //=======================================================================
427 gp_Lin Select3D_Projector::Shoot
428 (const Standard_Real X,
429 const Standard_Real Y) const
434 L = gp_Lin(gp_Pnt(0,0, myFocus),
435 gp_Dir(X,Y,-myFocus));
438 L = gp_Lin(gp_Pnt(X,Y,0),
441 Transform(L, myInvTrsf);
446 void Select3D_Projector::SetView(const Handle(V3d_View)& aViou)
449 myPersp = aViou->Type()==V3d_PERSPECTIVE;
450 myFocus= aViou->Focale();
451 Standard_Real Xat,Yat,Zat,XUp,YUp,ZUp,DX,DY,DZ;
452 //Standard_Boolean Pers=Standard_False;
454 aViou->At(Xat,Yat,Zat);
455 aViou->Up(XUp,YUp,ZUp);
456 aViou->Proj(DX,DY,DZ);
457 gp_Pnt At (Xat,Yat,Zat);
458 gp_Dir Zpers (DX,DY,DZ);
459 gp_Dir Ypers (XUp,YUp,ZUp);
460 gp_Dir Xpers = Ypers.Crossed(Zpers);
461 gp_Ax3 Axe (At, Zpers, Xpers);
462 myScaledTrsf.SetTransformation(Axe);
467 void Select3D_Projector::DepthMinMax (const Standard_Real theDepthMin,
468 const Standard_Real theDepthMax)
470 myDepthMin = theDepthMin;
471 myDepthMax = theDepthMax;