new functions to handle distortion: should not affect previous stuff. tested but still error prone...

This commit is contained in:
matteodelle 2008-09-09 09:20:48 +00:00
parent e7c9656219
commit 0c2491e1b4
1 changed files with 485 additions and 367 deletions

View File

@ -1,367 +1,485 @@
/**************************************************************************** /****************************************************************************
* VCGLib o o * * VCGLib o o *
* Visual and Computer Graphics Library o o * * Visual and Computer Graphics Library o o *
* _ O _ * * _ O _ *
* Copyright(C) 2004 \/)\/ * * Copyright(C) 2004 \/)\/ *
* Visual Computing Lab /\/| * * Visual Computing Lab /\/| *
* ISTI - Italian National Research Council | * * ISTI - Italian National Research Council | *
* \ * * \ *
* All rights reserved. * * All rights reserved. *
* * * *
* This program is free software; you can redistribute it and/or modify * * This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by * * it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or * * the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. * * (at your option) any later version. *
* * * *
* This program is distributed in the hope that it will be useful, * * This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of * * but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
* for more details. * * for more details. *
* * * *
****************************************************************************/ ****************************************************************************/
/**************************************************************************** /****************************************************************************
History History
$Log: not supported by cvs2svn $ $Log: not supported by cvs2svn $
Revision 1.28 2006/12/21 00:13:27 cignoni Revision 1.28 2006/12/21 00:13:27 cignoni
Corrected a syntax error detected only by gcc. Corrected a syntax error detected only by gcc.
Corrected the order of initialization in the constructor to match the declaration order Corrected the order of initialization in the constructor to match the declaration order
Revision 1.27 2006/12/18 16:02:55 matteodelle Revision 1.27 2006/12/18 16:02:55 matteodelle
minor eroor correction on variable names minor eroor correction on variable names
Revision 1.26 2006/12/18 09:46:38 callieri Revision 1.26 2006/12/18 09:46:38 callieri
camera+shot revamp: changed field names to something with more sense, cleaning of various functions, correction of minor bugs/incongruences, removal of the infamous reference in shot. camera+shot revamp: changed field names to something with more sense, cleaning of various functions, correction of minor bugs/incongruences, removal of the infamous reference in shot.
Revision 1.25 2005/12/12 16:52:55 callieri Revision 1.25 2005/12/12 16:52:55 callieri
Added Unproject, from 2D local space + Zdepth to 3D camera space. Added ViewportToLocal, inverse of LocalToViewport Added Unproject, from 2D local space + Zdepth to 3D camera space. Added ViewportToLocal, inverse of LocalToViewport
Revision 1.24 2005/12/01 01:03:37 cignoni Revision 1.24 2005/12/01 01:03:37 cignoni
Removed excess ';' from end of template functions, for gcc compiling Removed excess ';' from end of template functions, for gcc compiling
Revision 1.23 2005/10/12 16:43:32 ponchio Revision 1.23 2005/10/12 16:43:32 ponchio
Added IsOrtho... Added IsOrtho...
Revision 1.22 2005/07/11 13:12:34 cignoni Revision 1.22 2005/07/11 13:12:34 cignoni
small gcc-related compiling issues (typenames,ending cr, initialization order) small gcc-related compiling issues (typenames,ending cr, initialization order)
Revision 1.21 2005/07/01 10:55:42 cignoni Revision 1.21 2005/07/01 10:55:42 cignoni
Removed default values from the implementation of SetCavalieri and SetIsometric Removed default values from the implementation of SetCavalieri and SetIsometric
Revision 1.20 2005/06/29 14:59:03 spinelli Revision 1.20 2005/06/29 14:59:03 spinelli
aggiunto: aggiunto:
- l' enum dei tipi PERSPECTIVE, ORTHO, ISOMETRIC, CAVALIERI - l' enum dei tipi PERSPECTIVE, ORTHO, ISOMETRIC, CAVALIERI
- inline void SetCavalieri(...) - inline void SetCavalieri(...)
- inline void SetIsometric(...) - inline void SetIsometric(...)
- modificato - modificato
- void SetOrtho( .. ) - void SetOrtho( .. )
Revision 1.19 2005/02/22 10:57:58 tommyfranken Revision 1.19 2005/02/22 10:57:58 tommyfranken
Corrected declaration and some syntax errors in GetFrustum Corrected declaration and some syntax errors in GetFrustum
Revision 1.18 2005/02/21 18:11:07 ganovelli Revision 1.18 2005/02/21 18:11:07 ganovelli
GetFrustum moved from gl/camera to math/camera.h GetFrustum moved from gl/camera to math/camera.h
Revision 1.17 2005/02/15 14:55:52 tommyfranken Revision 1.17 2005/02/15 14:55:52 tommyfranken
added principal point added principal point
Revision 1.16 2005/01/18 16:40:50 ricciodimare Revision 1.16 2005/01/18 16:40:50 ricciodimare
*** empty log message *** *** empty log message ***
Revision 1.15 2005/01/18 15:14:22 ponchio Revision 1.15 2005/01/18 15:14:22 ponchio
Far and end are reserved. Far and end are reserved.
Revision 1.14 2005/01/14 15:28:33 ponchio Revision 1.14 2005/01/14 15:28:33 ponchio
vcg/Point.h -> vcg/point.h (again!) vcg/Point.h -> vcg/point.h (again!)
Revision 1.13 2005/01/05 13:25:29 ganovelli Revision 1.13 2005/01/05 13:25:29 ganovelli
aggiunte conversione di coordinate aggiunte conversione di coordinate
Revision 1.12 2004/12/16 11:22:30 ricciodimare Revision 1.12 2004/12/16 11:22:30 ricciodimare
*** empty log message *** *** empty log message ***
Revision 1.11 2004/12/16 11:21:03 ricciodimare Revision 1.11 2004/12/16 11:21:03 ricciodimare
*** empty log message *** *** empty log message ***
Revision 1.10 2004/12/15 18:45:50 tommyfranken Revision 1.10 2004/12/15 18:45:50 tommyfranken
*** empty log message *** *** empty log message ***
<<<<<<< camera.h <<<<<<< camera.h
======= =======
Revision 1.8 2004/11/23 10:15:38 cignoni Revision 1.8 2004/11/23 10:15:38 cignoni
removed comment in comment gcc warning removed comment in comment gcc warning
Revision 1.7 2004/11/03 09:40:53 ganovelli Revision 1.7 2004/11/03 09:40:53 ganovelli
Point?.h to point?.h Point?.h to point?.h
Revision 1.6 2004/11/03 09:32:50 ganovelli Revision 1.6 2004/11/03 09:32:50 ganovelli
SetPerspective and SetFrustum added (same parameters as in opengl) SetPerspective and SetFrustum added (same parameters as in opengl)
>>>>>>> 1.8 >>>>>>> 1.8
Revision 1.4 2004/10/07 14:39:57 fasano Revision 1.4 2004/10/07 14:39:57 fasano
Remove glew.h include Remove glew.h include
Revision 1.3 2004/10/07 14:22:38 ganovelli Revision 1.3 2004/10/07 14:22:38 ganovelli
y axis reverse in projecting (!) y axis reverse in projecting (!)
Revision 1.2 2004/10/05 19:04:25 ganovelli Revision 1.2 2004/10/05 19:04:25 ganovelli
version 5-10-2004 in progress version 5-10-2004 in progress
Revision 1.1 2004/09/15 22:58:05 ganovelli Revision 1.1 2004/09/15 22:58:05 ganovelli
re-creation re-creation
Revision 1.2 2004/09/06 21:41:30 ganovelli Revision 1.2 2004/09/06 21:41:30 ganovelli
*** empty log message *** *** empty log message ***
Revision 1.1 2004/09/03 13:01:51 ganovelli Revision 1.1 2004/09/03 13:01:51 ganovelli
creation creation
****************************************************************************/ ****************************************************************************/
#ifndef __VCGLIB_CAMERA #ifndef __VCGLIB_CAMERA
#define __VCGLIB_CAMERA #define __VCGLIB_CAMERA
// VCG // VCG
#include <vcg/space/point3.h> #include <vcg/space/point3.h>
#include <vcg/space/point2.h> #include <vcg/space/point2.h>
#include <vcg/math/similarity.h> #include <vcg/math/similarity.h>
namespace vcg{
enum { namespace vcg{
PERSPECTIVE = 0,
ORTHO = 1, enum {
ISOMETRIC = 2, PERSPECTIVE = 0,
CAVALIERI = 3 ORTHO = 1,
}; ISOMETRIC = 2,
CAVALIERI = 3
};
template<class S>
class Camera
{ template<class S>
public: class Camera
typedef S ScalarType; {
public:
Camera(): typedef S ScalarType;
FocalMm(0.f),
ViewportPx(vcg::Point2<int>(0,0)), Camera():
PixelSizeMm(vcg::Point2<S>(0.0,0.0)), FocalMm(0.f),
CenterPx(vcg::Point2<S>(0.0,0.0)), ViewportPx(vcg::Point2<int>(0,0)),
DistorCenterPx(vcg::Point2<S>(0.0,0.0)), PixelSizeMm(vcg::Point2<S>(0.0,0.0)),
cameraType(0) CenterPx(vcg::Point2<S>(0.0,0.0)),
{} DistorCenterPx(vcg::Point2<S>(0.0,0.0)),
cameraType(0)
//------ camera intrinsics {}
ScalarType FocalMm; /// Focal Distance: the distance between focal center and image plane. Expressed in mm
Point2<int> ViewportPx; /// Dimension of the Image Plane (in pixels) //------ camera intrinsics
Point2<S> PixelSizeMm; /// Dimension in mm of a single pixel ScalarType FocalMm; /// Focal Distance: the distance between focal center and image plane. Expressed in mm
Point2<S> CenterPx; /// Position of the projection of the focal center on the image plane. Expressed in pixels Point2<int> ViewportPx; /// Dimension of the Image Plane (in pixels)
Point2<S> PixelSizeMm; /// Dimension in mm of a single pixel
Point2<S> DistorCenterPx; /// Position of the radial distortion center on the image plane in pixels Point2<S> CenterPx; /// Position of the projection of the focal center on the image plane. Expressed in pixels
S k[4]; /// 1st & 2nd order radial lens distortion coefficient (only the first 2 terms are used)
//------------------------ Point2<S> DistorCenterPx; /// Position of the radial distortion center on the image plane in pixels
S k[4]; /// 1st & 2nd order radial lens distortion coefficient (only the first 2 terms are used)
int cameraType; /// Type of camera: PERSPECTIVE,ORTHO,ISOMETRIC,CAVALIERI //------------------------
void SetOrtho(S dist) int cameraType; /// Type of camera: PERSPECTIVE,ORTHO,ISOMETRIC,CAVALIERI
{
cameraType = ORTHO; void SetOrtho(S dist)
ViewportPx = ( ((ViewportPx[0] * PixelSizeMm[0]) * (ViewportPx[1] * PixelSizeMm[1])) / FocalMm ) * dist; {
}; cameraType = ORTHO;
ViewportPx = ( ((ViewportPx[0] * PixelSizeMm[0]) * (ViewportPx[1] * PixelSizeMm[1])) / FocalMm ) * dist;
bool IsOrtho() const };
{
return ( cameraType == ORTHO ); bool IsOrtho() const
} {
return ( cameraType == ORTHO );
//--- Set-up methods }
/// set the camera specifying the perspecive view //--- Set-up methods
inline void SetPerspective(S AngleDeg, S AspectRatio, S Focal, vcg::Point2<int> Viewport);
/// set the camera specifying the perspecive view
/// set the camera specifying the cavalieri view inline void SetPerspective(S AngleDeg, S AspectRatio, S Focal, vcg::Point2<int> Viewport);
inline void SetCavalieri(S sx, S dx, S bt, S tp, S Focal, vcg::Point2<int> Viewport);
/// set the camera specifying the cavalieri view
/// set the camera specifying the isometric view inline void SetCavalieri(S sx, S dx, S bt, S tp, S Focal, vcg::Point2<int> Viewport);
inline void SetIsometric(S sx, S dx, S bt, S tp, S Focal, vcg::Point2<int> Viewport);
/// set the camera specifying the isometric view
/// set the camera specifying the frustum view inline void SetIsometric(S sx, S dx, S bt, S tp, S Focal, vcg::Point2<int> Viewport);
inline void SetFrustum(S dx, S sx, S bt, S tp, S Focal, vcg::Point2<int> Viewport);
//------------------ /// set the camera specifying the frustum view
inline void SetFrustum(S dx, S sx, S bt, S tp, S Focal, vcg::Point2<int> Viewport);
/// returns the frustum //------------------
inline void GetFrustum(S & sx, S & dx, S & bt, S & tp, S & nr);
/// returns the frustum
//--- Space transformation methods inline void GetFrustum(S & sx, S & dx, S & bt, S & tp, S & nr);
/// project a point from 3d CAMERA space to the camera local plane //--- Space transformation methods
inline vcg::Point2<S> Project(const vcg::Point3<S> & p) const;
/// project a point from 3d CAMERA space to the camera local plane
/// unproject a point from the camera local plane (plus depth) to 3d CAMERA space inline vcg::Point2<S> Project(const vcg::Point3<S> & p) const;
inline vcg::Point3<S> UnProject(const vcg::Point2<S> & p, const S & d) const;
/// unproject a point from the camera local plane (plus depth) to 3d CAMERA space
/// transforms local plane coords to vieport (pixel) coords inline vcg::Point3<S> UnProject(const vcg::Point2<S> & p, const S & d) const;
inline vcg::Point2<S> LocalToViewportPx(const vcg::Point2<S> & p) const;
/// transforms local plane coords to vieport (pixel) coords
/// transforms vieport (pixel) coords to local plane coords inline vcg::Point2<S> LocalToViewportPx(const vcg::Point2<S> & p) const;
inline vcg::Point2<S> ViewportPxToLocal(const vcg::Point2<S> & p) const;
/// transforms vieport (pixel) coords to local plane coords
/// transforms local plane coords to [0 1] coords inline vcg::Point2<S> ViewportPxToLocal(const vcg::Point2<S> & p) const;
inline vcg::Point2<S> LocalTo_0_1(const vcg::Point2<S> & p) const;
/// transforms vieport (pixel) coords to [-1 1] coords
/// transforms local plane coords to [-1 1] coords inline vcg::Point2<S> Camera<S>::ViewportPxTo_neg1_1(const vcg::Point2<S> & p) const;
inline vcg::Point2<S> LocalTo_neg1_1(const vcg::Point2<S> & p) const;
//-------------------------------- /// transforms local plane coords to [0 1] coords
}; inline vcg::Point2<S> LocalTo_0_1(const vcg::Point2<S> & p) const;
/// transforms local plane coords to [-1 1] coords
inline vcg::Point2<S> LocalTo_neg1_1(const vcg::Point2<S> & p) const;
/// project a point from 3d CAMERA space to the camera's plane]
template<class S> /// transforms an undistorted 2D camera plane point in a distorted 2D camera plane point
vcg::Point2<S> Camera<S>::Project(const vcg::Point3<S> & p) const vcg::Point2<S> UndistortedToDistorted(vcg::Point2<S> u) const;
{
vcg::Point2<S> q = Point2<S>(p[0],p[1]); /// transforms a distorted 2D camera plane point in an undistorted 2D camera plane point
vcg::Point2<S> DistortedToUndistorted(vcg::Point2<S> d) const;
if(!IsOrtho()) //--------------------------------
{ };
q[0] *= FocalMm/p.Z();
q[1] *= FocalMm/p.Z();
}
/// project a point from 3d CAMERA space to the camera's plane]
return q; template<class S>
} vcg::Point2<S> Camera<S>::Project(const vcg::Point3<S> & p) const
{
/// unproject a point from the camera 2d plane [-1,-1]x[1,1] (plus depth) to 3d CAMERA space vcg::Point2<S> q = Point2<S>(p[0],p[1]);
template<class S> vcg::Point2<S> d = Point2<S>(p[0],p[1]);
vcg::Point3<S> Camera<S>::UnProject(const vcg::Point2<S> & p, const S & d) const
{ if(!IsOrtho())
vcg::Point3<S> np = Point3<S>(p[0], p[1], d); {
q[0] *= FocalMm/p.Z();
if(!IsOrtho()) q[1] *= FocalMm/p.Z();
{
np[0] /= FocalMm/d; if(k[0]!=0)
np[1] /= FocalMm/d; {
} vcg::Point2<S> d;
d=UndistortedToDistorted(q);
return np; q=d;
} }
/// transforms local plane coords to vieport (pixel) coords }
template<class S>
vcg::Point2<S> Camera<S>::LocalToViewportPx(const vcg::Point2<S> & p) const
{ return q;
vcg::Point2<S> np; }
np[0] = (p[0] / PixelSizeMm.X()) + CenterPx.X(); /// unproject a point from the camera 2d plane [-1,-1]x[1,1] (plus depth) to 3d CAMERA space
np[1] = (p[1] / PixelSizeMm.Y()) + CenterPx.Y(); template<class S>
vcg::Point3<S> Camera<S>::UnProject(const vcg::Point2<S> & p, const S & d) const
return np; {
} vcg::Point3<S> np = Point3<S>(p[0], p[1], d);
/// transforms vieport (pixel) coords to local plane coords if(!IsOrtho())
template<class S> {
vcg::Point2<S> Camera<S>::ViewportPxToLocal(const vcg::Point2<S> & p) const if(k[0]!=0)
{ {
vcg::Point2<S> ps; vcg::Point2<S> d = Point2<S>(p[0], p[1]);
ps[0] = (p[0]-CenterPx.X()) * PixelSizeMm.X(); vcg::Point2<S> u=DistortedToUndistorted(d);
ps[1] = (p[1]-CenterPx.Y()) * PixelSizeMm.Y(); np[0]=u[0];
return ps; np[1]=u[1];
} }
/// transforms local plane coords to [0-1] coords np[0] /= FocalMm/d;
template<class S> np[1] /= FocalMm/d;
vcg::Point2<S> Camera<S>::LocalTo_0_1(const vcg::Point2<S> & p) const }
{
vcg::Point2<S> ps; return np;
ps[0] = ( p[0]/PixelSizeMm.X() + CenterPx.X() ) / (S)ViewportPx[0]; }
ps[1] = ( p[1]/PixelSizeMm.Y() + CenterPx.Y() ) / (S)ViewportPx[1];
return ps; /// transforms local plane coords to vieport (pixel) coords
} template<class S>
vcg::Point2<S> Camera<S>::LocalToViewportPx(const vcg::Point2<S> & p) const
/// transforms local plane coords to [-1 1] coords {
template<class S> vcg::Point2<S> np;
vcg::Point2<S> Camera<S>::LocalTo_neg1_1(const vcg::Point2<S> & p) const
{ np[0] = (p[0] / PixelSizeMm.X()) + CenterPx.X();
vcg::Point2<S> ps; np[1] = (p[1] / PixelSizeMm.Y()) + CenterPx.Y();
ps[0] = 2.0f * p[0] / ( PixelSizeMm.X() * (S)ViewportPx[0] );
ps[1] = 2.0f * p[1] / ( PixelSizeMm.Y() * (S)ViewportPx[1] ); return np;
return ps; }
}
/// transforms vieport (pixel) coords to local plane coords
//--- basic camera setup (GL-like) template<class S>
vcg::Point2<S> Camera<S>::ViewportPxToLocal(const vcg::Point2<S> & p) const
/// set the camera specifying the perspective view {
template<class S> vcg::Point2<S> ps;
void Camera<S>::SetPerspective( S AngleDeg, S AspectRatio, S Focal, vcg::Point2<int> Viewport) ps[0] = (p[0]-CenterPx.X()) * PixelSizeMm.X();
{ ps[1] = (p[1]-CenterPx.Y()) * PixelSizeMm.Y();
cameraType = PERSPECTIVE; return ps;
S halfsize[2]; }
halfsize[1] = tan(math::ToRad(AngleDeg/2.0f)) * Focal; /// transforms vieport (pixel) coords to [-1 1] coords
halfsize[0] = halfsize[1]*AspectRatio; template<class S>
vcg::Point2<S> Camera<S>::ViewportPxTo_neg1_1(const vcg::Point2<S> & p) const
SetFrustum(-halfsize[0],halfsize[0],-halfsize[1],halfsize[1],Focal,Viewport); {
} vcg::Point2<S> ps;
ps[0] = 2.0f * ((p[0]-CenterPx.X()) * PixelSizeMm.X())/ ( PixelSizeMm.X() * (S)ViewportPx[0] );
/// set the camera specifying the frustum view ps[1] = 2.0f * ((p[1]-CenterPx.Y()) * PixelSizeMm.Y())/ ( PixelSizeMm.Y() * (S)ViewportPx[1] );
template<class S> return ps;
void Camera<S>::SetFrustum( S sx, S dx, S bt, S tp, S Focal, vcg::Point2<int> Viewport) }
{
S vp[2]; /// transforms local plane coords to [0-1] coords
vp[0] = dx-sx; template<class S>
vp[1] = tp-bt; vcg::Point2<S> Camera<S>::LocalTo_0_1(const vcg::Point2<S> & p) const
{
ViewportPx[0] = Viewport[0]; vcg::Point2<S> ps;
if(vp[1] != -1) ps[0] = ( p[0]/PixelSizeMm.X() + CenterPx.X() ) / (S)ViewportPx[0];
ViewportPx[1] = Viewport[1]; // the user specified the viewport ps[1] = ( p[1]/PixelSizeMm.Y() + CenterPx.Y() ) / (S)ViewportPx[1];
else return ps;
ViewportPx[1] = ViewportPx[0]; // default viewport }
PixelSizeMm[0] = vp[0] / (S)Viewport[0]; /// transforms local plane coords to [-1 1] coords
PixelSizeMm[1] = vp[1] / (S)Viewport[1]; template<class S>
vcg::Point2<S> Camera<S>::LocalTo_neg1_1(const vcg::Point2<S> & p) const
CenterPx[0] = -sx/vp[0] * (S)Viewport[0]; {
CenterPx[1] = -bt/vp[1] * (S)Viewport[1]; vcg::Point2<S> ps;
ps[0] = 2.0f * p[0] / ( PixelSizeMm.X() * (S)ViewportPx[0] );
FocalMm =Focal; ps[1] = 2.0f * p[1] / ( PixelSizeMm.Y() * (S)ViewportPx[1] );
} return ps;
}
//--- special cameras setup
/// transforms an undistorted 2D camera plane point in a distorted 2D camera plane point
/// set the camera specifying the cavalieri view template<class S>
template<class S> vcg::Point2<S> Camera<S>::UndistortedToDistorted(vcg::Point2<S> u) const
void Camera<S>::SetCavalieri(S sx, S dx, S bt, S tp, S Focal, vcg::Point2<int> Viewport) {
{ vcg::Point2<S> dis;
cameraType = CAVALIERI; vcg::Point2<S> dc=ViewportPxTo_neg1_1(DistorCenterPx);
SetFrustum(sx, dx, bt, tp, Focal,Viewport); const S SQRT3 = S(1.732050807568877293527446341505872366943);
} const S CBRT = S(0.33333333333333333333333);
S Ru,Rd,lambda,c,d,Q,R,D,S,T,sinT,cosT;
/// set the camera specifying the isometric view
template<class S> if(((u[0]-dc[0])==0 && (u[1]-dc[1])==0) || k[0] == 0)
void Camera<S>::SetIsometric(S sx, S dx, S bt, S tp, S Focal, vcg::Point2<int> Viewport) {
{ dis[0] = u[0];
cameraType = ISOMETRIC; dis[1] = u[1];
SetFrustum(sx, dx, bt, tp, Focal,Viewport); return dis;
} }
/// returns the frustum Ru = hypot ((u[0]-dc[0]), (u[1]-dc[1])); /* SQRT(Xu*Xu+Yu*Yu) */
template<class S> c = 1 / k[0];
void Camera<S>:: GetFrustum( S & sx, S & dx, S & bt, S & tp, S & nr) d = -c * Ru;
{
dx = CenterPx.X()* PixelSizeMm.X(); //scaled center Q = c / 3;
sx = -( (S)ViewportPx.X() - CenterPx.X() ) * PixelSizeMm.X(); R = -d / 2;
if (R<0)
bt = -CenterPx.Y()* PixelSizeMm.Y(); D = pow(Q,3) + sqrt(-R);
tp = ( (S)ViewportPx.Y() - CenterPx.Y() ) * PixelSizeMm.Y(); else
D = pow(Q,3) + sqrt(R);
nr = FocalMm;
} if (D >= 0) /* one real root */
{
} D = sqrt(D);
#endif S = pow((R + D),CBRT);
if (R>=D)
T = pow((R - D),CBRT);
else
T = - pow(abs(R - D),CBRT);
Rd = S + T;
if (Rd < 0)
Rd = sqrt(-1 / (3 * k[0]));
}
else /* three real roots */
{
D = sqrt(-D);
S = pow((hypot (R, D)),CBRT);
T = atan2 (D, R) / 3;
//SinCos(T, sinT, cosT);
sinT=sin(T);
cosT=cos(T);
/* the larger positive root is 2*S*cos(T) */
/* the smaller positive root is -S*cos(T) + SQRT(3)*S*sin(T) */
/* the negative root is -S*cos(T) - SQRT(3)*S*sin(T) */
Rd = -S * cosT + SQRT3 * S * sinT; /* use the smaller positive root */
}
lambda = Rd / Ru;
dis[0] = u[0] * lambda;
dis[1] = u[1] * lambda;
return dis;
}
/// transforms a distorted 2D camera plane point in an undistorted 2D camera plane point
template<class S>
vcg::Point2<S> Camera<S>::DistortedToUndistorted(vcg::Point2<S> d) const
{
vcg::Point2<S> u;
vcg::Point2<S> dc=ViewportPxTo_neg1_1(DistorCenterPx);
S r=sqrt(pow((d[0]-dc[0]),2)+pow((d[1]-dc[1]),2));
u[0]=d[0]*(1-k[0]*r*r);
u[1]=d[1]*(1-k[0]*r*r);
return u;
}
//--- basic camera setup (GL-like)
/// set the camera specifying the perspective view
template<class S>
void Camera<S>::SetPerspective( S AngleDeg, S AspectRatio, S Focal, vcg::Point2<int> Viewport)
{
cameraType = PERSPECTIVE;
S halfsize[2];
halfsize[1] = tan(math::ToRad(AngleDeg/2.0f)) * Focal;
halfsize[0] = halfsize[1]*AspectRatio;
SetFrustum(-halfsize[0],halfsize[0],-halfsize[1],halfsize[1],Focal,Viewport);
}
/// set the camera specifying the frustum view
template<class S>
void Camera<S>::SetFrustum( S sx, S dx, S bt, S tp, S Focal, vcg::Point2<int> Viewport)
{
S vp[2];
vp[0] = dx-sx;
vp[1] = tp-bt;
ViewportPx[0] = Viewport[0];
if(vp[1] != -1)
ViewportPx[1] = Viewport[1]; // the user specified the viewport
else
ViewportPx[1] = ViewportPx[0]; // default viewport
PixelSizeMm[0] = vp[0] / (S)Viewport[0];
PixelSizeMm[1] = vp[1] / (S)Viewport[1];
CenterPx[0] = -sx/vp[0] * (S)Viewport[0];
CenterPx[1] = -bt/vp[1] * (S)Viewport[1];
FocalMm =Focal;
}
//--- special cameras setup
/// set the camera specifying the cavalieri view
template<class S>
void Camera<S>::SetCavalieri(S sx, S dx, S bt, S tp, S Focal, vcg::Point2<int> Viewport)
{
cameraType = CAVALIERI;
SetFrustum(sx, dx, bt, tp, Focal,Viewport);
}
/// set the camera specifying the isometric view
template<class S>
void Camera<S>::SetIsometric(S sx, S dx, S bt, S tp, S Focal, vcg::Point2<int> Viewport)
{
cameraType = ISOMETRIC;
SetFrustum(sx, dx, bt, tp, Focal,Viewport);
}
/// returns the frustum
template<class S>
void Camera<S>:: GetFrustum( S & sx, S & dx, S & bt, S & tp, S & nr)
{
dx = CenterPx.X()* PixelSizeMm.X(); //scaled center
sx = -( (S)ViewportPx.X() - CenterPx.X() ) * PixelSizeMm.X();
bt = -CenterPx.Y()* PixelSizeMm.Y();
tp = ( (S)ViewportPx.Y() - CenterPx.Y() ) * PixelSizeMm.Y();
nr = FocalMm;
}
}
#endif