Commit b102f57b authored by Luis Peñaranda's avatar Luis Peñaranda
Browse files

some numerical stability

parent e6fa883b
...@@ -22,6 +22,11 @@ ...@@ -22,6 +22,11 @@
#define VERT_SHADER_FILE "test_vertex_shader.vert" #define VERT_SHADER_FILE "test_vertex_shader.vert"
#define FRAG_SHADER_FILE "fragment_shader.frag" #define FRAG_SHADER_FILE "fragment_shader.frag"
#define CONST_PI 3.141592653589793238462
#define CONST_PI_2 1.57079632679489661923
#define CONST_PI_F 3.14159265358979f
#define CONST_PI_2_F 1.570796326794897f // TODO: check this one
OpenGLCanvas::OpenGLCanvas(QWidget *parent) : OpenGLCanvas::OpenGLCanvas(QWidget *parent) :
QGLWidget(parent) QGLWidget(parent)
{ {
...@@ -50,8 +55,8 @@ void OpenGLCanvas::change_fov(double f){ ...@@ -50,8 +55,8 @@ void OpenGLCanvas::change_fov(double f){
fov=f; fov=f;
if (fov<=fov_max) if (fov<=fov_max)
scale=1.f; scale=1.f;
else if (fov>295.f) //else if (fov>295.f)
scale = 0.02f; // TODO: check this value wrt fov_max // scale = 0.02f; // TODO: check this value wrt fov_max
else { else {
if (fov_scale_relation == "Naive") if (fov_scale_relation == "Naive")
scale=fov_max/fov; scale=fov_max/fov;
...@@ -69,7 +74,7 @@ void OpenGLCanvas::change_fov(double f){ ...@@ -69,7 +74,7 @@ void OpenGLCanvas::change_fov(double f){
// scale = 0.3f; // scale = 0.3f;
fprintf(stderr,"change fov=%f, fov_max=%f\n",fov,fov_max); fprintf(stderr,"change fov, fov=%f, fov_max=%f, scale=%f\n",fov,fov_max,scale);
emit fov_changed((int)fov); emit fov_changed((int)fov);
updateGL(); updateGL();
...@@ -77,15 +82,17 @@ void OpenGLCanvas::change_fov(double f){ ...@@ -77,15 +82,17 @@ void OpenGLCanvas::change_fov(double f){
} }
void OpenGLCanvas::change_fov(int new_fov){ void OpenGLCanvas::change_fov(int new_fov){
change_fov((double)new_fov); if(new_fov<=360&&new_fov>=1)
change_fov((double)new_fov);
} }
void OpenGLCanvas::change_fov_max(int new_fov_max){ void OpenGLCanvas::change_fov_max(int new_fov_max){
fov_max=(double)new_fov_max; if(new_fov_max<=360&&new_fov_max>=0)
fov_max=(double)new_fov_max;
if (fov<=fov_max) if (fov<=fov_max)
scale=1.f; scale=1.f;
else if (fov>295.f) //else if (fov>295.f)
scale = 0.02f; // TODO: check this value wrt fov_max // scale = 0.02f; // TODO: check this value wrt fov_max
else { else {
if (fov_scale_relation == "Naive") if (fov_scale_relation == "Naive")
scale=fov_max/fov; scale=fov_max/fov;
...@@ -100,7 +107,7 @@ void OpenGLCanvas::change_fov_max(int new_fov_max){ ...@@ -100,7 +107,7 @@ void OpenGLCanvas::change_fov_max(int new_fov_max){
else if (fov_scale_relation == "Logarithm") else if (fov_scale_relation == "Logarithm")
scale=log(exp(1.f)+(1.f-exp(1.f))*(fov-fov_max)/(360.-2*fov_max)); scale=log(exp(1.f)+(1.f-exp(1.f))*(fov-fov_max)/(360.-2*fov_max));
} }
fprintf(stderr,"change fov_max=%f, new scale=%f\n",fov_max,scale); fprintf(stderr,"change fov_max, fov=%f, fov_max=%f, new scale=%f\n",fov,fov_max,scale);
emit max_fov_changed((int)fov_max); emit max_fov_changed((int)fov_max);
updateGL(); updateGL();
} }
...@@ -114,7 +121,7 @@ void OpenGLCanvas::change_fov_max(int new_fov_max){ ...@@ -114,7 +121,7 @@ void OpenGLCanvas::change_fov_max(int new_fov_max){
void OpenGLCanvas::change_center_lambda(double lambda){ void OpenGLCanvas::change_center_lambda(double lambda){
if (center_lambda!=lambda && lambda>=-3.14f && lambda<=3.14f) { if (center_lambda!=lambda && lambda>=-CONST_PI && lambda<=CONST_PI) {
center_lambda = lambda; center_lambda = lambda;
updateGL(); updateGL();
} }
...@@ -123,7 +130,7 @@ void OpenGLCanvas::change_center_lambda(double lambda){ ...@@ -123,7 +130,7 @@ void OpenGLCanvas::change_center_lambda(double lambda){
void OpenGLCanvas::change_center_phi(double phi){ void OpenGLCanvas::change_center_phi(double phi){
if (center_phi!=phi && phi>=-1.57f && phi<=1.57f) { if (center_phi!=phi && phi>=-CONST_PI_2 && phi<=CONST_PI_2) {
center_phi = phi; center_phi = phi;
updateGL(); updateGL();
} }
...@@ -139,8 +146,8 @@ void OpenGLCanvas::re_center(){ ...@@ -139,8 +146,8 @@ void OpenGLCanvas::re_center(){
void OpenGLCanvas::change_fov_scale_relation(QString name){ void OpenGLCanvas::change_fov_scale_relation(QString name){
fov_scale_relation = name; fov_scale_relation = name;
if (fov<60.f) scale = 1.f; if (fov<fov_max) scale = 1.f;
else if (fov>295.f) scale = 0.01f; //else if (fov>295.f) scale = 0.01f;
else{ else{
if (fov_scale_relation == "Naive") if (fov_scale_relation == "Naive")
scale=fov_max/fov; scale=fov_max/fov;
...@@ -278,7 +285,7 @@ void OpenGLCanvas::initializeGL(){ ...@@ -278,7 +285,7 @@ void OpenGLCanvas::initializeGL(){
if (texCoord == NULL){ if (texCoord == NULL){
printf("problem allocating memory for texture coordinates \n"); printf("problem allocating memory for texture coordinates \n");
} }
define_texture_coordinates(texCoord, m, n, -1.57f, 1.57f, -3.14f, 3.14f); define_texture_coordinates(texCoord, m, n, -CONST_PI_2_F, CONST_PI_2_F, -CONST_PI_F, CONST_PI_F);
//defining positions of the sphere vertices //defining positions of the sphere vertices
int meshNumVertices = m*n; int meshNumVertices = m*n;
...@@ -311,8 +318,8 @@ void OpenGLCanvas::define_texture_coordinates(float *texCoord, int m, int n, flo ...@@ -311,8 +318,8 @@ void OpenGLCanvas::define_texture_coordinates(float *texCoord, int m, int n, flo
for (int i = 0; i<m; i++){ for (int i = 0; i<m; i++){
for (int j = 0; j<n; j++){ for (int j = 0; j<n; j++){
texCoord[2*(j+i*n)] = (min_lambda+delta_lambda*j)/(6.2832) + 0.5; texCoord[2*(j+i*n)] = (min_lambda+delta_lambda*j)/(2*CONST_PI_F) + 0.5;
texCoord[2*(j+i*n)+1] = (min_phi+delta_phi*i)/(3.1416) + 0.5; texCoord[2*(j+i*n)+1] = (min_phi+delta_phi*i)/(CONST_PI_F) + 0.5;
} }
} }
...@@ -320,10 +327,10 @@ void OpenGLCanvas::define_texture_coordinates(float *texCoord, int m, int n, flo ...@@ -320,10 +327,10 @@ void OpenGLCanvas::define_texture_coordinates(float *texCoord, int m, int n, flo
void OpenGLCanvas::vertex_transformation(float *positions, int m, int n, float center_lambda, float center_phi, float fov_rads, float scale){ void OpenGLCanvas::vertex_transformation(float *positions, int m, int n, float center_lambda, float center_phi, float fov_rads, float scale){
float min_lambda = -3.1415f; float min_lambda = -CONST_PI_F;
float max_lambda = 3.1415f; float max_lambda = CONST_PI_F;
float min_phi = -1.5708f; float min_phi = -CONST_PI_2_F;
float max_phi = 1.5708f; float max_phi = CONST_PI_2_F;
float delta_lambda = (max_lambda-min_lambda)/(1.0*(n-1)); float delta_lambda = (max_lambda-min_lambda)/(1.0*(n-1));
float delta_phi = (max_phi-min_phi)/(1.0*(m-1)); float delta_phi = (max_phi-min_phi)/(1.0*(m-1));
...@@ -391,8 +398,8 @@ void OpenGLCanvas::vertex_transformation(float *positions, int m, int n, float c ...@@ -391,8 +398,8 @@ void OpenGLCanvas::vertex_transformation(float *positions, int m, int n, float c
y = (4.f*v)/(u*u+v*v+4.f); y = (4.f*v)/(u*u+v*v+4.f);
z = (u*u+v*v-4.f)/(u*u+v*v+4.f); z = (u*u+v*v-4.f)/(u*u+v*v+4.f);
lambda = atan2(x,-z)/3.1415; lambda = atan2(x,-z)/CONST_PI_F;
phi = asin(y)/1.5708; phi = asin(y)/CONST_PI_2_F;
u = x/(-z); u = x/(-z);
v = y/(-z); v = y/(-z);
...@@ -422,10 +429,10 @@ void OpenGLCanvas::vertex_transformation(float *positions, int m, int n, float c ...@@ -422,10 +429,10 @@ void OpenGLCanvas::vertex_transformation(float *positions, int m, int n, float c
void OpenGLCanvas::load_sphere_mesh(float *positions, int m, int n){ void OpenGLCanvas::load_sphere_mesh(float *positions, int m, int n){
float min_lambda = -3.1415f; float min_lambda = -CONST_PI_F;
float max_lambda = 3.1415f; float max_lambda = CONST_PI_F;
float min_phi = -1.5708f; float min_phi = -CONST_PI_2_F;
float max_phi = 1.5708f; float max_phi = CONST_PI_2_F;
float delta_lambda = (max_lambda-min_lambda)/(1.0*(n-1)); float delta_lambda = (max_lambda-min_lambda)/(1.0*(n-1));
float delta_phi = (max_phi-min_phi)/(1.0*(m-1)); float delta_phi = (max_phi-min_phi)/(1.0*(m-1));
...@@ -454,24 +461,24 @@ void OpenGLCanvas::load_sphere_mesh(float *positions, int m, int n){ ...@@ -454,24 +461,24 @@ void OpenGLCanvas::load_sphere_mesh(float *positions, int m, int n){
float OpenGLCanvas::calculate_extent(float fov_rads){ float OpenGLCanvas::calculate_extent(float fov_rads){
float lambda, phi, x, y, z, u, v, r, theta; double lambda, phi, x, y, z, u, v, r, theta;
//calculating the extent of the projection for the given FOV //calculating the extent of the projection for the given FOV
lambda = fov_rads; lambda = fov_rads;
phi = 0.f; phi = 0.;
// OpenGL: x is the vertical axes pointg downwards, and y is horizontal axes // OpenGL: x is the vertical axes pointg downwards, and y is horizontal axes
y = sin(phi); y = sin(phi);
x = -sin(lambda)*cos(phi); x = -sin(lambda)*cos(phi);
z = -cos(lambda)*cos(phi); z = -cos(lambda)*cos(phi);
u = 2.f*x/(-z+1.f); u = 2.*x/(-z+1.);
v = 2.f*y/(-z+1.f); v = 2.*y/(-z+1.);
r = sqrt(u*u+v*v); r = sqrt(u*u+v*v);
theta = atan2(u,v); theta = atan2(u,v);
r *= scale; r *= scale;
u = -r*sin(theta); u = -r*sin(theta);
v = r*cos(theta); v = r*cos(theta);
x = (4.f*u)/(u*u+v*v+4.f); x = (4.*u)/(u*u+v*v+4.);
y = (4.f*v)/(u*u+v*v+4.f); y = (4.*v)/(u*u+v*v+4.);
z = (u*u+v*v-4.f)/(u*u+v*v+4.f); z = (u*u+v*v-4.)/(u*u+v*v+4.);
u = x/(-z); u = x/(-z);
v = y/(-z); v = y/(-z);
return u; return u;
...@@ -670,8 +677,8 @@ void OpenGLCanvas::mouseMoveEvent(QMouseEvent *event){ ...@@ -670,8 +677,8 @@ void OpenGLCanvas::mouseMoveEvent(QMouseEvent *event){
// scroll with the left button // scroll with the left button
if(event->buttons()==Qt::LeftButton){ if(event->buttons()==Qt::LeftButton){
// compute the delta and move the image // compute the delta and move the image
center_lambda+=(event->x()-lastPos.x())*3.1415926f/image_size_x; center_lambda+=(event->x()-lastPos.x())*CONST_PI_F/image_size_x;
center_phi+=(event->y()-lastPos.y())*3.1415926f/image_size_y; center_phi+=(event->y()-lastPos.y())*CONST_PI_F/image_size_y;
lastPos=event->pos(); lastPos=event->pos();
updateGL(); updateGL();
} }
...@@ -689,7 +696,7 @@ void OpenGLCanvas::wheelEvent(QWheelEvent *event){ ...@@ -689,7 +696,7 @@ void OpenGLCanvas::wheelEvent(QWheelEvent *event){
void OpenGLCanvas::paintGL(){ void OpenGLCanvas::paintGL(){
float fov_rads = (fov/180.f)*1.5708f; float fov_rads = (fov/360.)*CONST_PI;
// // changing scale to generate the figures for the paper (remove it after) // // changing scale to generate the figures for the paper (remove it after)
// scale = 0.8; // scale = 0.8;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment