00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 #ifndef __VCGLIB_CAMERA
00128 #define __VCGLIB_CAMERA
00129
00130
00131 #include <vcg/space/point3.h>
00132 #include <vcg/space/point2.h>
00133 #include <vcg/math/similarity.h>
00134
00135
00136
00137 namespace vcg{
00138
00139 enum {
00140 PERSPECTIVE = 0,
00141 ORTHO = 1,
00142 ISOMETRIC = 2,
00143 CAVALIERI = 3
00144 };
00145
00146
00147 template<class S>
00148 class Camera
00149 {
00150 public:
00151 typedef S ScalarType;
00152
00153 Camera():
00154 FocalMm(0.f),
00155 ViewportPx(vcg::Point2<int>(0,0)),
00156 PixelSizeMm(vcg::Point2<S>(0.0,0.0)),
00157 CenterPx(vcg::Point2<S>(0.0,0.0)),
00158 DistorCenterPx(vcg::Point2<S>(0.0,0.0)),
00159 cameraType(0)
00160 {
00161 k[0] = k[1] = k[2] = k[3] = 0;
00162 }
00163
00164
00165 ScalarType FocalMm;
00166 Point2<int> ViewportPx;
00167 Point2<S> PixelSizeMm;
00168 Point2<S> CenterPx;
00169
00170 Point2<S> DistorCenterPx;
00171 S k[4];
00172
00173
00174 int cameraType;
00175
00176 void SetOrtho(S dist)
00177 {
00178 cameraType = ORTHO;
00179 ViewportPx = ( ((ViewportPx[0] * PixelSizeMm[0]) * (ViewportPx[1] * PixelSizeMm[1])) / FocalMm ) * dist;
00180 };
00181
00182 bool IsOrtho() const
00183 {
00184 return ( cameraType == ORTHO );
00185 }
00186
00187
00188
00190 inline void SetPerspective(S AngleDeg, S AspectRatio, S Focal, vcg::Point2<int> Viewport);
00191
00193 inline void SetCavalieri(S sx, S dx, S bt, S tp, S Focal, vcg::Point2<int> Viewport);
00194
00196 inline void SetIsometric(S sx, S dx, S bt, S tp, S Focal, vcg::Point2<int> Viewport);
00197
00199 inline void SetFrustum(S dx, S sx, S bt, S tp, S Focal, vcg::Point2<int> Viewport);
00200
00201
00203 inline void GetFrustum(S & sx, S & dx, S & bt, S & tp, S & nr);
00204
00205
00206
00208 inline vcg::Point2<S> Project(const vcg::Point3<S> & p) const;
00209
00211 inline vcg::Point3<S> UnProject(const vcg::Point2<S> & p, const S & d) const;
00212
00214 inline vcg::Point2<S> LocalToViewportPx(const vcg::Point2<S> & p) const;
00215
00217 inline vcg::Point2<S> ViewportPxToLocal(const vcg::Point2<S> & p) const;
00218
00220 inline vcg::Point2<S> ViewportPxTo_neg1_1(const vcg::Point2<S> & p) const;
00221
00223 inline vcg::Point2<S> Neg1_1ToViewportPx(const vcg::Point2<S> & p) const;
00224
00226 inline vcg::Point2<S> LocalTo_0_1(const vcg::Point2<S> & p) const;
00227
00229 inline vcg::Point2<S> LocalTo_neg1_1(const vcg::Point2<S> & p) const;
00230
00232 vcg::Point2<S> UndistortedToDistorted(vcg::Point2<S> u) const;
00233
00235 vcg::Point2<S> DistortedToUndistorted(vcg::Point2<S> d) const;
00236
00237 };
00238
00239
00240
00242 template<class S>
00243 vcg::Point2<S> Camera<S>::Project(const vcg::Point3<S> & p) const
00244 {
00245 vcg::Point2<S> q = Point2<S>(p[0],p[1]);
00246 vcg::Point2<S> d = Point2<S>(p[0],p[1]);
00247
00248 if(!IsOrtho())
00249 {
00250 q[0] *= FocalMm/p.Z();
00251 q[1] *= FocalMm/p.Z();
00252
00253 if(k[0]!=0)
00254 {
00255 vcg::Point2<S> d;
00256 d=UndistortedToDistorted(q);
00257 q=d;
00258 }
00259
00260 }
00261
00262
00263 return q;
00264 }
00265
00267 template<class S>
00268 vcg::Point3<S> Camera<S>::UnProject(const vcg::Point2<S> & p, const S & d) const
00269 {
00270 vcg::Point3<S> np = Point3<S>(p[0], p[1], d);
00271
00272 if(!IsOrtho())
00273 {
00274 if(k[0]!=0)
00275 {
00276 vcg::Point2<S> d = Point2<S>(p[0], p[1]);
00277 vcg::Point2<S> u=DistortedToUndistorted(d);
00278 np[0]=u[0];
00279 np[1]=u[1];
00280 }
00281
00282 np[0] /= FocalMm/d;
00283 np[1] /= FocalMm/d;
00284 }
00285
00286 return np;
00287 }
00288
00290 template<class S>
00291 vcg::Point2<S> Camera<S>::LocalToViewportPx(const vcg::Point2<S> & p) const
00292 {
00293 vcg::Point2<S> np;
00294
00295 np[0] = (p[0] / PixelSizeMm.X()) + CenterPx.X();
00296 np[1] = (p[1] / PixelSizeMm.Y()) + CenterPx.Y();
00297
00298 return np;
00299 }
00300
00302 template<class S>
00303 vcg::Point2<S> Camera<S>::ViewportPxToLocal(const vcg::Point2<S> & p) const
00304 {
00305 vcg::Point2<S> ps;
00306 ps[0] = (p[0]-CenterPx.X()) * PixelSizeMm.X();
00307 ps[1] = (p[1]-CenterPx.Y()) * PixelSizeMm.Y();
00308 return ps;
00309 }
00310
00312 template<class S>
00313 vcg::Point2<S> Camera<S>::ViewportPxTo_neg1_1(const vcg::Point2<S> & p) const
00314 {
00315 vcg::Point2<S> ps;
00316 ps[0] = 2.0f * ((p[0]-CenterPx.X()) * PixelSizeMm.X())/ ( PixelSizeMm.X() * (S)ViewportPx[0] );
00317 ps[1] = 2.0f * ((p[1]-CenterPx.Y()) * PixelSizeMm.Y())/ ( PixelSizeMm.Y() * (S)ViewportPx[1] );
00318 return ps;
00319 }
00320
00322 template<class S>
00323 vcg::Point2<S> Camera<S>::Neg1_1ToViewportPx(const vcg::Point2<S> & p) const
00324 {
00325 vcg::Point2<S> ps;
00326 ps[0] = ((PixelSizeMm.X() * (S)ViewportPx[0] *p[0])/(2.0f * PixelSizeMm.X()))+CenterPx.X();
00327 ps[1] = ((PixelSizeMm.Y() * (S)ViewportPx[1] *p[1])/(2.0f * PixelSizeMm.Y()))+CenterPx.Y();
00328 return ps;
00329 }
00330
00332 template<class S>
00333 vcg::Point2<S> Camera<S>::LocalTo_0_1(const vcg::Point2<S> & p) const
00334 {
00335 vcg::Point2<S> ps;
00336 ps[0] = ( p[0]/PixelSizeMm.X() + CenterPx.X() ) / (S)ViewportPx[0];
00337 ps[1] = ( p[1]/PixelSizeMm.Y() + CenterPx.Y() ) / (S)ViewportPx[1];
00338 return ps;
00339 }
00340
00342 template<class S>
00343 vcg::Point2<S> Camera<S>::LocalTo_neg1_1(const vcg::Point2<S> & p) const
00344 {
00345 vcg::Point2<S> ps;
00346 ps[0] = 2.0f * p[0] / ( PixelSizeMm.X() * (S)ViewportPx[0] );
00347 ps[1] = 2.0f * p[1] / ( PixelSizeMm.Y() * (S)ViewportPx[1] );
00348 return ps;
00349 }
00350
00352 template<class Scalar>
00353 vcg::Point2<Scalar> Camera<Scalar>::UndistortedToDistorted(vcg::Point2<Scalar> u) const
00354 {
00355 vcg::Point2<Scalar> dis;
00356 vcg::Point2<Scalar> dc=ViewportPxTo_neg1_1(DistorCenterPx);
00357 const Scalar SQRT3 = Scalar(1.732050807568877293527446341505872366943);
00358 const Scalar CBRT = Scalar(0.33333333333333333333333);
00359 Scalar Ru,Rd,lambda,c,d,Q,R,D,S,T,sinT,cosT;
00360
00361 if(((u[0]-dc[0])==0 && (u[1]-dc[1])==0) || k[0] == 0)
00362 {
00363 dis[0] = u[0];
00364 dis[1] = u[1];
00365 return dis;
00366 }
00367
00368 Ru = hypot ((u[0]-dc[0]), (u[1]-dc[1]));
00369 c = 1 / k[0];
00370 d = -c * Ru;
00371
00372 Q = c / 3;
00373 R = -d / 2;
00374 if (R<0)
00375 D = pow(Q,3) + sqrt(-R);
00376 else
00377 D = pow(Q,3) + sqrt(R);
00378
00379 if (D >= 0)
00380 {
00381 D = sqrt(D);
00382 S = pow((R + D),CBRT);
00383 if (R>=D)
00384 T = pow((R - D),CBRT);
00385 else
00386 T = - pow(abs((int)(R - D)),CBRT);
00387 Rd = S + T;
00388
00389 if (Rd < 0)
00390 Rd = sqrt(-1 / (3 * k[0]));
00391 }
00392 else
00393 {
00394 D = sqrt(-D);
00395 S = pow((Scalar)(hypot (R, D)),(Scalar)CBRT);
00396 T = atan2 (D, R) / 3;
00397
00398 sinT=sin(T);
00399 cosT=cos(T);
00400
00401
00402
00403
00404 Rd = -S * cosT + SQRT3 * S * sinT;
00405 }
00406
00407 lambda = Rd / Ru;
00408
00409 dis[0] = u[0] * lambda;
00410 dis[1] = u[1] * lambda;
00411
00412 return dis;
00413 }
00414
00416 template<class S>
00417 vcg::Point2<S> Camera<S>::DistortedToUndistorted(vcg::Point2<S> d) const
00418 {
00419 vcg::Point2<S> u;
00420 vcg::Point2<S> dc=ViewportPxTo_neg1_1(DistorCenterPx);
00421 S r=sqrt(pow((d[0]-dc[0]),2)+pow((d[1]-dc[1]),2));
00422 u[0]=d[0]*(1-k[0]*r*r);
00423 u[1]=d[1]*(1-k[0]*r*r);
00424
00425 return u;
00426
00427 }
00428
00429
00430
00431
00433 template<class S>
00434 void Camera<S>::SetPerspective( S AngleDeg, S AspectRatio, S Focal, vcg::Point2<int> Viewport)
00435 {
00436 cameraType = PERSPECTIVE;
00437 S halfsize[2];
00438
00439 halfsize[1] = tan(math::ToRad(AngleDeg/2.0f)) * Focal;
00440 halfsize[0] = halfsize[1]*AspectRatio;
00441
00442 SetFrustum(-halfsize[0],halfsize[0],-halfsize[1],halfsize[1],Focal,Viewport);
00443 }
00444
00446 template<class S>
00447 void Camera<S>::SetFrustum( S sx, S dx, S bt, S tp, S Focal, vcg::Point2<int> Viewport)
00448 {
00449 S vp[2];
00450 vp[0] = dx-sx;
00451 vp[1] = tp-bt;
00452
00453 ViewportPx[0] = Viewport[0];
00454 if(vp[1] != -1)
00455 ViewportPx[1] = Viewport[1];
00456 else
00457 ViewportPx[1] = ViewportPx[0];
00458
00459 PixelSizeMm[0] = vp[0] / (S)Viewport[0];
00460 PixelSizeMm[1] = vp[1] / (S)Viewport[1];
00461
00462 CenterPx[0] = -sx/vp[0] * (S)Viewport[0];
00463 CenterPx[1] = -bt/vp[1] * (S)Viewport[1];
00464
00465 FocalMm =Focal;
00466 }
00467
00468
00469
00471 template<class S>
00472 void Camera<S>::SetCavalieri(S sx, S dx, S bt, S tp, S Focal, vcg::Point2<int> Viewport)
00473 {
00474 cameraType = CAVALIERI;
00475 SetFrustum(sx, dx, bt, tp, Focal,Viewport);
00476 }
00477
00479 template<class S>
00480 void Camera<S>::SetIsometric(S sx, S dx, S bt, S tp, S Focal, vcg::Point2<int> Viewport)
00481 {
00482 cameraType = ISOMETRIC;
00483 SetFrustum(sx, dx, bt, tp, Focal,Viewport);
00484 }
00485
00487 template<class S>
00488 void Camera<S>:: GetFrustum( S & sx, S & dx, S & bt, S & tp, S & nr)
00489 {
00490 dx = CenterPx.X()* PixelSizeMm.X();
00491 sx = -( (S)ViewportPx.X() - CenterPx.X() ) * PixelSizeMm.X();
00492
00493 bt = -CenterPx.Y()* PixelSizeMm.Y();
00494 tp = ( (S)ViewportPx.Y() - CenterPx.Y() ) * PixelSizeMm.Y();
00495
00496 nr = FocalMm;
00497 }
00498
00499 }
00500 #endif
00501
00502
00503
00504