1 // Created on: 1992-03-13
2 // Created by: Christophe MARION
3 // Copyright (c) 1992-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
18 // #define No_Exception
26 #include <gp_Pnt2d.hxx>
27 #include <gp_Trsf.hxx>
29 #include <gp_Vec2d.hxx>
30 #include <HLRAlgo_Projector.hxx>
31 #include <Precision.hxx>
32 #include <Standard_NoSuchObject.hxx>
34 // formula for derivating a perspective, from Mathematica
36 // D1 = -------- + -------------
38 // 1 - ---- f (1 - ----)
40 //=======================================================================
41 //function : HLRAlgo_Projector
43 //=======================================================================
44 HLRAlgo_Projector::HLRAlgo_Projector () :
45 myPersp(Standard_False),myFocus(0)
50 //=======================================================================
51 //function : HLRAlgo_Projector
53 //=======================================================================
55 HLRAlgo_Projector::HLRAlgo_Projector (const gp_Ax2& CS) :
56 myPersp(Standard_False), myFocus(0)
58 myScaledTrsf.SetTransformation(CS);
63 //=======================================================================
64 //function : HLRAlgo_Projector
66 //=======================================================================
68 HLRAlgo_Projector::HLRAlgo_Projector (const gp_Ax2& CS,
69 const Standard_Real Focus) :
70 myPersp(Standard_True), myFocus(Focus)
72 myScaledTrsf.SetTransformation(CS);
77 //=======================================================================
78 //function : HLRAlgo_Projector
80 //=======================================================================
82 HLRAlgo_Projector::HLRAlgo_Projector (const gp_Trsf& T,
83 const Standard_Boolean Persp,
84 const Standard_Real Focus) :
93 //=======================================================================
94 //function : HLRAlgo_Projector
96 //=======================================================================
98 HLRAlgo_Projector::HLRAlgo_Projector (const gp_Trsf& T,
99 const Standard_Boolean Persp,
100 const Standard_Real Focus,
103 const gp_Vec2d& v3) :
114 //=======================================================================
117 //=======================================================================
119 void HLRAlgo_Projector::Set (const gp_Trsf& T,
120 const Standard_Boolean Persp,
121 const Standard_Real Focus)
130 //=======================================================================
133 //=======================================================================
135 #include <gp_Mat.hxx>
137 static Standard_Integer TrsfType(const gp_Trsf& Trsf) {
138 const gp_Mat& Mat = Trsf.VectorialPart();
139 if( (Abs(Mat.Value(1,1)-1.0) < 1e-15)
140 && (Abs(Mat.Value(2,2)-1.0) < 1e-15)
141 && (Abs(Mat.Value(3,3)-1.0) < 1e-15)) {
144 else if( (Abs(Mat.Value(1,1)-0.7071067811865476) < 1e-15)
145 && (Abs(Mat.Value(1,2)+0.5) < 1e-15)
146 && (Abs(Mat.Value(1,3)-0.5) < 1e-15)
148 && (Abs(Mat.Value(2,1)-0.7071067811865476) < 1e-15)
149 && (Abs(Mat.Value(2,2)-0.5) < 1e-15)
150 && (Abs(Mat.Value(2,3)+0.5) < 1e-15)
152 && (Abs(Mat.Value(3,1)) < 1e-15)
153 && (Abs(Mat.Value(3,2)-0.7071067811865476) < 1e-15)
154 && (Abs(Mat.Value(3,3)-0.7071067811865476) < 1e-15)) {
157 else if( (Abs(Mat.Value(1,1)-1.0) < 1e-15)
158 && (Abs(Mat.Value(2,3)-1.0) < 1e-15)
159 && (Abs(Mat.Value(3,2)+1.0) < 1e-15)) {
160 return(2); //-- front
162 else if( (Abs(Mat.Value(1,1)-0.7071067811865476) < 1e-15)
163 && (Abs(Mat.Value(1,2)-0.7071067811865476) < 1e-15)
164 && (Abs(Mat.Value(1,3)) < 1e-15)
166 && (Abs(Mat.Value(2,1)+0.5) < 1e-15)
167 && (Abs(Mat.Value(2,2)-0.5) < 1e-15)
168 && (Abs(Mat.Value(2,3)-0.7071067811865476) < 1e-15)
170 && (Abs(Mat.Value(3,1)-0.5) < 1e-15)
171 && (Abs(Mat.Value(3,2)+0.5) < 1e-15)
172 && (Abs(Mat.Value(3,3)-0.7071067811865476) < 1e-15)) {
178 void HLRAlgo_Projector::Scaled (const Standard_Boolean On)
181 myTrsf = myScaledTrsf;
183 myTrsf.SetScaleFactor(1.);
185 myTrsf.SetTranslationPart(gp_Vec(0.,0.,0.));
186 myType=TrsfType(myTrsf);
193 //=======================================================================
196 //=======================================================================
198 void HLRAlgo_Projector::Project (const gp_Pnt& P, gp_Pnt2d& Pout) const
203 case 0: { //-- axono standard
204 Standard_Real x07 = P.X()*0.7071067811865475;
205 Standard_Real y05 = P.Y()*0.5;
206 Standard_Real z05 = P.Z()*0.5;
209 //-- Z=0.7071067811865475*(P.Y()+P.Z());
213 X=P.X(); Y=P.Y(); //-- Z=P.Z();
218 X=P.X(); Y=P.Z(); //-- Z=-P.Y();
223 Standard_Real xmy05 = (P.X()-P.Y())*0.5;
224 Standard_Real z07 = P.Z()*0.7071067811865476;
225 X=0.7071067811865476*(P.X()+P.Y());
235 Standard_Real R = 1.-P2.Z()/myFocus;
236 Pout.SetCoord(P2.X()/R,P2.Y()/R);
239 Pout.SetCoord(P2.X(),P2.Y());
248 Standard_Real R = 1.-P2.Z()/myFocus;
249 Pout.SetCoord(P2.X()/R,P2.Y()/R);
252 Pout.SetCoord(P2.X(),P2.Y());
256 //=======================================================================
259 //=======================================================================
260 /* ====== TYPE 0 (??)
261 (0.7071067811865476, -0.5 , 0.4999999999999999)
262 (0.7071067811865475, 0.5000000000000001, -0.5 )
263 (0.0, 0.7071067811865475, 0.7071067811865476)
270 ======= TYPE 2 (front)
272 (0.0, 1.110223024625157e-16 , 1.0)
273 (0.0, -1.0 , 1.110223024625157e-16)
276 ( 0.7071067811865476, 0.7071067811865475, 0.0)
277 (-0.5 , 0.5000000000000001, 0.7071067811865475)
278 ( 0.4999999999999999, -0.5 , 0.7071067811865476)
280 void HLRAlgo_Projector::Project (const gp_Pnt& P,
283 Standard_Real& Z) const
287 case 0: { //-- axono standard
288 Standard_Real x07 = P.X()*0.7071067811865475;
289 Standard_Real y05 = P.Y()*0.5;
290 Standard_Real z05 = P.Z()*0.5;
293 Z=0.7071067811865475*(P.Y()+P.Z());
297 X=P.X(); Y=P.Y(); Z=P.Z();
301 X=P.X(); Y=P.Z(); Z=-P.Y();
305 Standard_Real xmy05 = (P.X()-P.Y())*0.5;
306 Standard_Real z07 = P.Z()*0.7071067811865476;
307 X=0.7071067811865476*(P.X()+P.Y());
325 Standard_Real R = 1 - Z / myFocus;
332 //=======================================================================
335 //=======================================================================
337 void HLRAlgo_Projector::Project (const gp_Pnt& P,
340 gp_Vec2d& D1out) const
343 PP.Transform(myTrsf);
345 DD1.Transform(myTrsf);
347 Standard_Real R = 1. - PP.Z() / myFocus;
348 Pout .SetCoord(PP .X()/R , PP.Y()/R);
349 D1out.SetCoord(DD1.X()/R + PP.X()*DD1.Z()/(myFocus * R*R),
350 DD1.Y()/R + PP.Y()*DD1.Z()/(myFocus * R*R));
353 Pout .SetCoord(PP .X(),PP .Y());
354 D1out.SetCoord(DD1.X(),DD1.Y());
358 //=======================================================================
361 //=======================================================================
363 gp_Lin HLRAlgo_Projector::Shoot (const Standard_Real X,
364 const Standard_Real Y) const
368 L = gp_Lin(gp_Pnt(0,0, myFocus),
369 gp_Dir(X,Y,-myFocus));
372 L = gp_Lin(gp_Pnt(X,Y,0),
375 L.Transform(myInvTrsf);
379 //=======================================================================
380 //function : SetDirection
382 //=======================================================================
384 void HLRAlgo_Projector::SetDirection ()
387 V1.Transform(myTrsf);
388 if ((Abs(V1.X()) + Abs(V1.Y())) < Precision::Angular()) V1.SetCoord(1,1,0);
389 gp_Vec2d D1(V1.X(),V1.Y());
390 myD1.SetCoord(-D1.Y(),D1.X());
392 V2.Transform(myTrsf);
393 if ((Abs(V2.X()) + Abs(V2.Y())) < Precision::Angular()) V2.SetCoord(1,1,0);
394 gp_Vec2d D2(V2.X(),V2.Y());
395 myD2.SetCoord(-D2.Y(),D2.X());
397 V3.Transform(myTrsf);
398 if ((Abs(V3.X()) + Abs(V3.Y())) < Precision::Angular()) V3.SetCoord(1,1,0);
399 gp_Vec2d D3(V3.X(),V3.Y());
400 myD3.SetCoord(-D3.Y(),D3.X());
403 //=======================================================================
404 //function : Transformation
406 //=======================================================================
408 const gp_Trsf & HLRAlgo_Projector::Transformation() const