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()),
49 Standard_Real Xat,Yat,Zat,XUp,YUp,ZUp,DX,DY,DZ;
50 //Standard_Boolean Pers=Standard_False;
52 aViou->At(Xat,Yat,Zat);
53 aViou->Up(XUp,YUp,ZUp);
54 aViou->Proj(DX,DY,DZ);
55 gp_Pnt At (Xat,Yat,Zat);
56 gp_Dir Zpers (DX,DY,DZ);
57 gp_Dir Ypers (XUp,YUp,ZUp);
58 gp_Dir Xpers = Ypers.Crossed(Zpers);
59 gp_Ax3 Axe (At, Zpers, Xpers);
60 myScaledTrsf.SetTransformation(Axe);
61 myGTrsf.SetTrsf(myScaledTrsf);
66 //=======================================================================
67 //function : Select3D_Projector
69 //=======================================================================
71 Select3D_Projector::Select3D_Projector()
72 : myPersp(Standard_False),
78 //=======================================================================
79 //function : Select3D_Projector
81 //=======================================================================
83 Select3D_Projector::Select3D_Projector (const gp_Ax2& CS)
84 : myPersp(Standard_False),
87 myScaledTrsf.SetTransformation(CS);
88 myGTrsf.SetTrsf(myScaledTrsf);
92 //=======================================================================
93 //function : Select3D_Projector
95 //=======================================================================
97 Select3D_Projector::Select3D_Projector (const gp_Ax2& CS,
98 const Standard_Real Focus)
99 : myPersp(Standard_True),
102 myScaledTrsf.SetTransformation(CS);
103 myGTrsf.SetTrsf(myScaledTrsf);
107 //=======================================================================
108 //function : Select3D_Projector
110 //=======================================================================
112 Select3D_Projector::Select3D_Projector (const gp_Trsf& T,
113 const Standard_Boolean Persp,
114 const Standard_Real Focus)
119 myGTrsf.SetTrsf(myScaledTrsf);
123 //=======================================================================
124 //function : Select3D_Projector
126 //=======================================================================
128 Select3D_Projector::Select3D_Projector (const gp_GTrsf& GT,
129 const Standard_Boolean Persp,
130 const Standard_Real Focus)
138 //=======================================================================
141 //=======================================================================
143 void Select3D_Projector::Set
145 const Standard_Boolean Persp,
146 const Standard_Real Focus)
154 //=======================================================================
157 //=======================================================================
159 #include <gp_Mat.hxx>
161 static Standard_Integer TrsfType(const gp_GTrsf& Trsf) {
162 const gp_Mat& Mat = Trsf.VectorialPart();
163 if( (Abs(Mat.Value(1,1)-1.0) < 1e-15)
164 && (Abs(Mat.Value(2,2)-1.0) < 1e-15)
165 && (Abs(Mat.Value(3,3)-1.0) < 1e-15)) {
168 else if( (Abs(Mat.Value(1,1)-0.7071067811865476) < 1e-15)
169 && (Abs(Mat.Value(1,2)+0.5) < 1e-15)
170 && (Abs(Mat.Value(1,3)-0.5) < 1e-15)
172 && (Abs(Mat.Value(2,1)-0.7071067811865476) < 1e-15)
173 && (Abs(Mat.Value(2,2)-0.5) < 1e-15)
174 && (Abs(Mat.Value(2,3)+0.5) < 1e-15)
176 && (Abs(Mat.Value(3,1)) < 1e-15)
177 && (Abs(Mat.Value(3,2)-0.7071067811865476) < 1e-15)
178 && (Abs(Mat.Value(3,3)-0.7071067811865476) < 1e-15)) {
181 else if( (Abs(Mat.Value(1,1)-1.0) < 1e-15)
182 && (Abs(Mat.Value(2,3)-1.0) < 1e-15)
183 && (Abs(Mat.Value(3,2)+1.0) < 1e-15)) {
184 return(2); //-- front
186 else if( (Abs(Mat.Value(1,1)-0.7071067811865476) < 1e-15)
187 && (Abs(Mat.Value(1,2)-0.7071067811865476) < 1e-15)
188 && (Abs(Mat.Value(1,3)) < 1e-15)
190 && (Abs(Mat.Value(2,1)+0.5) < 1e-15)
191 && (Abs(Mat.Value(2,2)-0.5) < 1e-15)
192 && (Abs(Mat.Value(2,3)-0.7071067811865476) < 1e-15)
194 && (Abs(Mat.Value(3,1)-0.5) < 1e-15)
195 && (Abs(Mat.Value(3,2)+0.5) < 1e-15)
196 && (Abs(Mat.Value(3,3)-0.7071067811865476) < 1e-15)) {
202 void Select3D_Projector::Scaled (const Standard_Boolean On)
207 //myGTrsf.SetTranslationPart(gp_XYZ(0.,0.,0.));
208 myType=TrsfType(myGTrsf);
215 //=======================================================================
218 //=======================================================================
220 void Select3D_Projector::Project (const gp_Pnt& P, gp_Pnt2d& Pout) const
223 if(!myView.IsNull()){
224 Standard_Real Xout,Yout;
227 myView->Project(P.X(),P.Y(),P.Z(),Xout,Yout);
229 Standard_Integer Xp,Yp;
230 myView->RefToPix(P.X(),P.Y(),P.Z(),Xp,Yp);
231 myView->Convert(Xp,Yp,Xout,Yout);
233 Pout.SetCoord(Xout,Yout);
239 case 0: { //-- axono standard
240 Standard_Real x07 = P.X()*0.7071067811865475;
241 Standard_Real y05 = P.Y()*0.5;
242 Standard_Real z05 = P.Z()*0.5;
245 //-- Z=0.7071067811865475*(P.Y()+P.Z());
249 X=P.X(); Y=P.Y(); //-- Z=P.Z();
254 X=P.X(); Y=P.Z(); //-- Z=-P.Y();
259 Standard_Real xmy05 = (P.X()-P.Y())*0.5;
260 Standard_Real z07 = P.Z()*0.7071067811865476;
261 X=0.7071067811865476*(P.X()+P.Y());
271 Standard_Real R = 1.-P2.Z()/myFocus;
272 Pout.SetCoord(P2.X()/R,P2.Y()/R);
275 Pout.SetCoord(P2.X(),P2.Y());
284 Standard_Real R = 1.-P2.Z()/myFocus;
285 Pout.SetCoord(P2.X()/R,P2.Y()/R);
288 Pout.SetCoord(P2.X(),P2.Y());
295 //=======================================================================
298 //=======================================================================
299 /* ====== TYPE 0 (??)
300 (0.7071067811865476, -0.5 , 0.4999999999999999)
301 (0.7071067811865475, 0.5000000000000001, -0.5 )
302 (0.0, 0.7071067811865475, 0.7071067811865476)
309 ======= TYPE 2 (front)
311 (0.0, 1.110223024625157e-16 , 1.0)
312 (0.0, -1.0 , 1.110223024625157e-16)
315 ( 0.7071067811865476, 0.7071067811865475, 0.0)
316 (-0.5 , 0.5000000000000001, 0.7071067811865475)
317 ( 0.4999999999999999, -0.5 , 0.7071067811865476)
319 void Select3D_Projector::Project (const gp_Pnt& P,
322 Standard_Real& Z) const
324 if(!myView.IsNull()){
325 // Standard_Real Xout,Yout;
328 myView->Project(P.X(),P.Y(),P.Z(),X,Y);
330 Standard_Integer Xp,Yp;
331 myView->RefToPix(P.X(),P.Y(),P.Z(),Xp,Yp);
332 myView->Convert(Xp,Yp,X,Y);
338 case 0: { //-- axono standard
339 Standard_Real x07 = P.X()*0.7071067811865475;
340 Standard_Real y05 = P.Y()*0.5;
341 Standard_Real z05 = P.Z()*0.5;
344 Z=0.7071067811865475*(P.Y()+P.Z());
348 X=P.X(); Y=P.Y(); Z=P.Z();
352 X=P.X(); Y=P.Z(); Z=-P.Y();
356 Standard_Real xmy05 = (P.X()-P.Y())*0.5;
357 Standard_Real z07 = P.Z()*0.7071067811865476;
358 X=0.7071067811865476*(P.X()+P.Y());
376 Standard_Real R = 1 - Z / myFocus;
383 //=======================================================================
386 //=======================================================================
388 void Select3D_Projector::Project (const gp_Pnt& P,
391 gp_Vec2d& D1out) const
398 Standard_Real R = 1. - PP.Z() / myFocus;
399 Pout .SetCoord(PP .X()/R , PP.Y()/R);
400 D1out.SetCoord(DD1.X()/R + PP.X()*DD1.Z()/(myFocus * R*R),
401 DD1.Y()/R + PP.Y()*DD1.Z()/(myFocus * R*R));
404 Pout .SetCoord(PP .X(),PP .Y());
405 D1out.SetCoord(DD1.X(),DD1.Y());
410 //=======================================================================
413 //=======================================================================
415 gp_Lin Select3D_Projector::Shoot
416 (const Standard_Real X,
417 const Standard_Real Y) const
422 L = gp_Lin(gp_Pnt(0,0, myFocus),
423 gp_Dir(X,Y,-myFocus));
426 L = gp_Lin(gp_Pnt(X,Y,0),
429 Transform(L, myInvTrsf);
434 void Select3D_Projector::SetView(const Handle(V3d_View)& aViou)
437 myPersp = aViou->Type()==V3d_PERSPECTIVE;
438 myFocus= aViou->Focale();
439 Standard_Real Xat,Yat,Zat,XUp,YUp,ZUp,DX,DY,DZ;
440 //Standard_Boolean Pers=Standard_False;
442 aViou->At(Xat,Yat,Zat);
443 aViou->Up(XUp,YUp,ZUp);
444 aViou->Proj(DX,DY,DZ);
445 gp_Pnt At (Xat,Yat,Zat);
446 gp_Dir Zpers (DX,DY,DZ);
447 gp_Dir Ypers (XUp,YUp,ZUp);
448 gp_Dir Xpers = Ypers.Crossed(Zpers);
449 gp_Ax3 Axe (At, Zpers, Xpers);
450 myScaledTrsf.SetTransformation(Axe);