# HG changeset patch # User John Tsiombikas # Date 1423255191 -7200 # Node ID 1df14c5ffa71b053a47565f4035a655f7b3f5fdb # Parent df4a277adb82b6bc4ee878b15a01295304495b00 conversion to Qt diff -r df4a277adb82 -r 1df14c5ffa71 .hgignore --- a/.hgignore Fri Feb 06 21:15:23 2015 +0200 +++ b/.hgignore Fri Feb 06 22:39:51 2015 +0200 @@ -3,3 +3,5 @@ \.swp$ ^data/ ^test1$ +^build/ +\.user$ diff -r df4a277adb82 -r 1df14c5ffa71 src/glview.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/glview.cc Fri Feb 06 22:39:51 2015 +0200 @@ -0,0 +1,38 @@ +#include +#include "opengl.h" +#include "glview.h" +#include "view3d.h" +//#include "view2d.h" + +extern View3D view3d; + +GLView::GLView(QWidget *parent) + : QOpenGLWidget(parent) +{ +} + +GLView::~GLView() +{ +} + + +void GLView::initializeGL() +{ + glewInit(); + + if(!view3d.init(this)) { + QApplication::quit(); + } +} + +void GLView::paintGL() +{ + view3d.display(); +} + +void GLView::resizeGL(int x, int y) +{ + glViewport(0, 0, x, y); + + view3d.reshape(x, y); +} diff -r df4a277adb82 -r 1df14c5ffa71 src/glview.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/glview.h Fri Feb 06 22:39:51 2015 +0200 @@ -0,0 +1,16 @@ +#ifndef GLVIEW_H +#define GLVIEW_H + +#include + +class GLView : public QOpenGLWidget { +public: + GLView(QWidget * parent = 0); + ~GLView(); + + void initializeGL(); + void paintGL(); + void resizeGL(int x, int y); +}; + +#endif // GLVIEW_H diff -r df4a277adb82 -r 1df14c5ffa71 src/main.cc --- a/src/main.cc Fri Feb 06 21:15:23 2015 +0200 +++ b/src/main.cc Fri Feb 06 22:39:51 2015 +0200 @@ -1,588 +1,11 @@ -#include "opengl.h" +#include "mainwin.h" +#include -#include -#include -#include +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWin w; + w.show(); -#include - -#include "mesh.h" -#include "sdr.h" -#include "volume.h" - -static bool init(void); -static GLUI *create_ui(void); -static void display(void); -static void reshape(int x, int y); -static void keyboard(unsigned char key, int x, int y); -static void keyboard_up(unsigned char key, int x, int y); -static void mouse(int button, int state, int x, int y); -static void motion(int x, int y); -static bool init_xfer(void); -static void display_xfer(void); -static void reshape_xfer(int x, int y); -static void mouse_xfer(int button, int state, int x, int y); -static void motion_xfer(int x, int y); -static void volume_preview(); -static void draw_iso(); -static void draw_volume(); - -static int mainwin_id, xferwin_id; -//todo keyb esc - -static int win_xsz, win_ysz; -static float cam_phi, cam_theta, cam_dist = 6; -static std::vector key_state(256); - -static const char *vol_fname = "data/test1.vol"; -static const char *vsdr_path = "data/shaders/transfer.v.glsl"; -static const char *fsdr_path = "data/shaders/transfer.f.glsl"; -static const char *vsdr_vol_path = "data/shaders/vol.v.glsl"; -static const char *fsdr_vol_path = "data/shaders/vol.f.glsl"; - -static unsigned int sprog; -static unsigned int sprog_vol; - -static Volume *vol; -static Mesh *mesh; -static float cur_z, thres = 0.5, thres2 = 1.0; -static float slice_z = 0.5; - -static int use_orig_vol_res = 1; -static int vol_res[3]; // volume sampling resolution x/y/z -static float bound_scale = 1.42; - -static GLUI *ui; - -static int num_poly = 128; - -int main(int argc, char **argv) -{ - glutInitWindowSize(512, 512); - glutInit(&argc, argv); - if(argv[1]) - vol_fname = argv[1]; - - if(!init()) { - fprintf(stderr, "Failed to initialize program.\n"); - return 1; - } - - init_xfer(); - - glutMainLoop(); - return 0; + return a.exec(); } - -static bool init() -{ - glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); - - mainwin_id = glutCreateWindow("CT scan"); - glutDisplayFunc(display); - glutReshapeFunc(reshape); - glutKeyboardFunc(keyboard); - glutKeyboardUpFunc(keyboard_up); - glutMouseFunc(mouse); - glutMotionFunc(motion); - - glewInit(); - - glEnable(GL_DEPTH_TEST); - glEnable(GL_NORMALIZE); - - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - - //FIXME shaders setup - if(!(sprog = sdr_getprog(vsdr_path, fsdr_path))) - { - fprintf(stderr, "Failed to create shader program!\n"); - return false; - } - - if(!(sprog_vol = sdr_getprog(vsdr_vol_path, fsdr_vol_path))) - { - fprintf(stderr, "Failed to create shader program!\n"); - return false; - } - - vol = new Volume; - if(!vol->load(vol_fname)) { - fprintf(stderr, "Failed to load %s", vol_fname); - return false; - } - mesh = new Mesh; - cur_z = 0.5; - - vol_res[0] = vol->get_slice(0)->get_width(); - vol_res[1] = vol->get_slice(0)->get_height(); - vol_res[2] = vol->get_slice_count(); - - num_poly = std::max(vol_res[0], std::max(vol_res[1], vol_res[2])) * bound_scale; - - if(!(ui = create_ui())) { - return false; - } - - return true; -} - -static GLUI_Spinner *res_spin[3]; -static void toggle_use_orig(int id) -{ - for(int i=0; i<3; i++) { - if(use_orig_vol_res) { - res_spin[i]->disable(); - } else { - res_spin[i]->enable(); - } - } -} -static void thres_change(int id) -{ - static float prev_thres = thres; - static float prev_thres2 = thres2; - - if(prev_thres != thres || prev_thres2 != thres2) { - prev_thres = thres; - prev_thres2 = thres2; - - mesh->clear(); - - glutSetWindow(xferwin_id); - glutPostRedisplay(); - glutSetWindow(mainwin_id); - glutPostRedisplay(); - } -} - -static void res_change(int id) -{ - static float prev_resx = vol_res[0]; - static float prev_resy = vol_res[1]; - static float prev_resz = vol_res[2]; - - if(prev_resx != vol_res[0] || prev_resy != vol_res[1] || prev_resz != vol_res[2]) { - prev_resx = vol_res[0]; - prev_resy = vol_res[1]; - prev_resz = vol_res[2]; - mesh->clear(); - } -} -static GLUI *create_ui() -{ - GLUI *ui = GLUI_Master.create_glui("ui"); - assert(ui); - - ui->set_main_gfx_window(glutGetWindow()); - - GLUI_Panel *thres_panel = ui->add_panel("iso thresholds"); - - GLUI_Spinner *thres_spin = ui->add_spinner_to_panel(thres_panel, "T1", GLUI_SPINNER_FLOAT, &thres, 0, thres_change); - thres_spin->set_float_limits(0, 1); - - GLUI_Spinner *thres2_spin = ui->add_spinner_to_panel(thres_panel, "T2", GLUI_SPINNER_FLOAT, &thres2, 0, thres_change); - thres2_spin->set_float_limits(0, 1); - -#ifdef ISO - GLUI_Panel *res_panel = ui->add_panel("volume resolution"); - - ui->add_checkbox_to_panel(res_panel, "original resolution", &use_orig_vol_res, 0, toggle_use_orig); - - static const char *res_spin_name[] = {"x", "y", "z"}; - for(int i=0; i<3; i++) { - res_spin[i] = ui->add_spinner_to_panel(res_panel, res_spin_name[i], GLUI_SPINNER_INT, vol_res + i, 0, res_change); - res_spin[i]->set_int_limits(8, 256); // TODO limits as arguments or config - res_spin[i]->disable(); - } -#endif - GLUI_Panel *preview_panel = ui->add_panel("volume preview"); - - GLUI_Spinner *preview_spin = ui->add_spinner_to_panel(preview_panel, "slice z", GLUI_SPINNER_FLOAT, &slice_z, 0); - preview_spin->set_float_limits(0, 1); - - return ui; -} - -static void volume_preview () -{ - float aspect = win_xsz / win_ysz; - - glDisable(GL_DEPTH_TEST); - - glUseProgram(sprog); - int tmin_loc = glGetUniformLocation(sprog, "tmin"); - if(tmin_loc != -1) - glUniform1f(tmin_loc, std::min(thres, thres2)); - int tmax_loc = glGetUniformLocation(sprog, "tmax"); - if(tmax_loc != -1) - glUniform1f(tmax_loc, std::max(thres, thres2)); - - glMatrixMode(GL_MODELVIEW); - glPushMatrix(); - glLoadIdentity(); - - glMatrixMode(GL_PROJECTION); - glPushMatrix(); - glLoadIdentity(); - glOrtho(0, aspect, 0, 1, -1, 1); - - glBindTexture(GL_TEXTURE_3D, vol->get_texture()); - glEnable(GL_TEXTURE_3D); - glBegin(GL_QUADS); - glColor3f(1.0, 1.0, 1.0); - glTexCoord3f(0, 0, slice_z); glVertex3f(0, 1, 0); - glTexCoord3f(0, 1, slice_z); glVertex3f(0, 0.75, 0); - glTexCoord3f(1, 1, slice_z); glVertex3f(0.25, 0.75, 0); - glTexCoord3f(1, 0, slice_z); glVertex3f(0.25, 1, 0); - glEnd(); - glDisable(GL_TEXTURE_3D); - - glUseProgram(0); - - glLineWidth(2); - glBegin(GL_LINE_LOOP); - glColor3f(0.8, 0.3, 0.8); - glVertex3f(0, 1, 0); - glVertex3f(0, 0.75, 0); - glVertex3f(0.25, 0.75, 0); - glVertex3f(0.25, 1, 0); - glEnd(); - - glMatrixMode(GL_PROJECTION); - glPopMatrix(); - - glMatrixMode(GL_MODELVIEW); - glPopMatrix(); - - glEnable(GL_DEPTH_TEST); -} - -static void draw_iso() -{ - if(mesh->is_empty()) { - printf("recalculating isosurface ... "); - fflush(stdout); - vol->create_mesh(mesh, thres, thres2, vol_res[0], vol_res[1], vol_res[2]); - printf("done.\n"); - } - mesh->draw(); -} - -static void draw_volume() -{ - glUseProgram(sprog_vol); - - int tmin_loc = glGetUniformLocation(sprog_vol, "tmin"); - if(tmin_loc != -1) - glUniform1f(tmin_loc, std::min(thres, thres2)); - int tmax_loc = glGetUniformLocation(sprog_vol, "tmax"); - if(tmax_loc != -1) - glUniform1f(tmax_loc, std::max(thres, thres2)); - - int res_loc = glGetUniformLocation(sprog_vol, "res"); - if(res_loc != -1) - glUniform3f(res_loc, (float)vol_res[0], (float)vol_res[1], (float)vol_res[2]); - - glRotatef(-vol->get_volume_rotation(), 1, 0, 0); - - glDisable(GL_DEPTH_TEST); - glBindTexture(GL_TEXTURE_3D, vol->get_texture()); - glEnable(GL_TEXTURE_3D); - glEnable(GL_BLEND); - glBegin(GL_QUADS); - for(int i=0; iclear(); - } - } -} - -static void motion(int x, int y) -{ - int dx = x - prev_x; - int dy = y - prev_y; - - if(!dx && !dy) - return; - - prev_x = x; - prev_y = y; - - if(key_state[(int)'t']) { - thres = (float)x / (float)win_xsz; - printf("threshold: %f\n", thres); - - glutPostRedisplay(); - return; - } - - // camera - if(bn_state[0]) { - if(key_state[(int)'z']) { - //zoom the camera - - cam_dist += dy * 0.1; - - if(cam_dist < 0) - cam_dist = 0; - } - else { - //rotate the camera - - cam_phi += dy * 0.5; - cam_theta += dx * 0.5; - - if(cam_phi > 90) - cam_phi = 90; - if(cam_phi < -90) - cam_phi = -90; - } - - glutPostRedisplay(); - } -} - -static bool init_xfer(void) -{ - glutSetWindow(mainwin_id); - int x = glutGet(GLUT_WINDOW_X); - int y = glutGet(GLUT_WINDOW_Y) + glutGet(GLUT_WINDOW_HEIGHT); - - glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); - glutInitWindowSize(512, 100); - glutInitWindowPosition(x, y); - - xferwin_id = glutCreateWindow("Transfer Function"); - glutDisplayFunc(display_xfer); - glutReshapeFunc(reshape_xfer); - glutMouseFunc(mouse_xfer); - glutMotionFunc(motion_xfer); - - return true; -} - -static void display_xfer(void) -{ - glClear(GL_COLOR_BUFFER_BIT); - - int num_val = glutGet(GLUT_WINDOW_WIDTH) / 4.0; - float x = 0; - float dx = 1.0 / num_val; - float low, high; - if(thres < thres2) { - low = thres; - high = thres2; - } - else { - low = thres2; - high = thres; - } - - - //drawing the histogram - float hmax = 1 / (float)vol->max_histogram_value; - - glBegin(GL_QUADS); - for (int i=0; ihistogram[i + 1], 0); - glVertex3f(x0, hmax * vol->histogram[i], 0); - } - glEnd(); - - //drawing the transfer function curve - - glLineWidth(3); - glBegin(GL_LINE_STRIP); - glColor3f(0.8, 0.3, 0.8); - for(int i=0; i 0.001) { - mesh->clear(); - ui->sync_live(); - } - select_thres = 0; - } - } -} - -static void motion_xfer(int x, int y) -{ - if(!select_thres) - return; - - int width = glutGet(GLUT_WINDOW_WIDTH); - float xpos = (float)x / (float)width; - - *select_thres = xpos; - thres_change(0); - - glutPostRedisplay(); - glutSetWindow(mainwin_id); - glutPostRedisplay(); -} diff -r df4a277adb82 -r 1df14c5ffa71 src/mainwin.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/mainwin.cc Fri Feb 06 22:39:51 2015 +0200 @@ -0,0 +1,42 @@ +#include +#include "mainwin.h" +#include "ui_mainwin.h" +#include "view3d.h" + +View3D view3d; + +MainWin::MainWin(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::MainWin) +{ + ui->setupUi(this); +} + +MainWin::~MainWin() +{ + delete ui; +} + +void MainWin::on_action_quit_triggered() +{ + close(); +} + +void MainWin::on_action_about_triggered() +{ + static const char *text = + "Volmetrics version whatever\n" + "Copyright (C) 2015 Eleni-Maria Stea, Stathis Kamperis\n" + "web page, emails, support, blah blah ... whatever."; + QMessageBox::about(this, "About", text); +} + +void MainWin::on_spin_win_low_valueChanged(double arg1) +{ + view3d.set_thresholds(arg1, ui->spin_win_high->value()); +} + +void MainWin::on_spin_win_high_valueChanged(double arg1) +{ + view3d.set_thresholds(ui->spin_win_low->value(), arg1); +} diff -r df4a277adb82 -r 1df14c5ffa71 src/mainwin.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/mainwin.h Fri Feb 06 22:39:51 2015 +0200 @@ -0,0 +1,31 @@ +#ifndef MAINWIN_H +#define MAINWIN_H + +#include + +namespace Ui { +class MainWin; +} + +class MainWin : public QMainWindow +{ + Q_OBJECT + +public: + explicit MainWin(QWidget *parent = 0); + ~MainWin(); + +private slots: + void on_action_quit_triggered(); + + void on_action_about_triggered(); + + void on_spin_win_low_valueChanged(double arg1); + + void on_spin_win_high_valueChanged(double arg1); + +private: + Ui::MainWin *ui; +}; + +#endif // MAINWIN_H diff -r df4a277adb82 -r 1df14c5ffa71 src/opengl.h --- a/src/opengl.h Fri Feb 06 21:15:23 2015 +0200 +++ b/src/opengl.h Fri Feb 06 22:39:51 2015 +0200 @@ -3,13 +3,4 @@ #include -#ifndef __APPLE__ -#include -#else -#include -#endif - -#include - - #endif // OPENGL_H_ diff -r df4a277adb82 -r 1df14c5ffa71 src/view3d.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/view3d.cc Fri Feb 06 22:39:51 2015 +0200 @@ -0,0 +1,68 @@ +#include +#include "opengl.h" +#include "view3d.h" +#include "volume.h" + +static int win_width, win_height; + +View3D::View3D() +{ + vol = 0; + thres_low = 0.0; + thres_high = 1.0; +} + +View3D::~View3D() +{ + destroy(); +} + +bool View3D::init(GLView *glview) +{ + this->glview = glview; + return true; +} + +void View3D::destroy() +{ +} + +void View3D::display() +{ + glClearColor(0, 0, 0, 1); + glClear(GL_COLOR_BUFFER_BIT); + + glBegin(GL_QUADS); + glColor3f(1, 0, 0); + glVertex2f(thres_low * 2.0 - 1.0, 1); + glVertex2f(thres_low * 2.0 - 1.0, -1); + glColor3f(0, 0, 1); + glVertex2f(thres_high * 2.0 - 1.0, -1); + glVertex2f(thres_high * 2.0 - 1.0, 1); + glEnd(); +} + + +void View3D::reshape(int x, int y) +{ + win_width = x; + win_height = y; +} + +void View3D::set_volume(Volume *vol) +{ + if(this->vol) { + delete this->vol; + } + this->vol = vol; + + glview->update(); +} + +void View3D::set_thresholds(float low, float high) +{ + thres_low = std::min(low, high); + thres_high = std::max(low, high); + + glview->update(); +} diff -r df4a277adb82 -r 1df14c5ffa71 src/view3d.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/view3d.h Fri Feb 06 22:39:51 2015 +0200 @@ -0,0 +1,28 @@ +#ifndef VIEW3D_H_ +#define VIEW3D_H_ + +#include "glview.h" +#include "volume.h" + +class View3D { +private: + Volume *vol; + float thres_low, thres_high; + GLView *glview; + +public: + View3D(); + ~View3D(); + + bool init(GLView *glview); + void destroy(); + + void display(); + void reshape(int x, int y); + + void set_volume(Volume *vol); + void set_thresholds(float low, float high); +}; + +#endif // VIEW3D_H_ + diff -r df4a277adb82 -r 1df14c5ffa71 ui/mainwin.ui --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ui/mainwin.ui Fri Feb 06 22:39:51 2015 +0200 @@ -0,0 +1,160 @@ + + + MainWin + + + + 0 + 0 + 701 + 471 + + + + MainWin + + + + + + + + + + + + 0 + 0 + 701 + 22 + + + + + &File + + + + + + + + &Help + + + + + + + + + TopToolBarArea + + + false + + + + + + 1 + + + + + + + Windowing + + + + + + + + low + + + + + + + 1.000000000000000 + + + 0.010000000000000 + + + + + + + + + + + high + + + + + + + 1.000000000000000 + + + 0.010000000000000 + + + 1.000000000000000 + + + + + + + + + + + + Qt::Vertical + + + + 20 + 252 + + + + + + + + + + &Open + + + + + &Quit + + + + + &About + + + + + + + GLView + QOpenGLWidget +
glview.h
+
+
+ + +
diff -r df4a277adb82 -r 1df14c5ffa71 volmetrics.pro --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/volmetrics.pro Fri Feb 06 22:39:51 2015 +0200 @@ -0,0 +1,48 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2015-02-06T21:21:47 +# +#------------------------------------------------- + +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = volmetrics +TEMPLATE = app + +CONFIG += c++11 + + +SOURCES += \ + src/image.cc \ + src/main.cc \ + src/mainwin.cc \ + src/matrix.cc \ + src/mesh.cc \ + src/sdr.cc \ + src/vector.cc \ + src/volume.cc \ + src/glview.cc \ + src/view3d.cc + +HEADERS += \ + src/image.h \ + src/mainwin.h \ + src/matrix.h \ + src/mesh.h \ + src/opengl.h \ + src/sdr.h \ + src/vector.h \ + src/volume.h \ + src/glview.h \ + src/view3d.h + +FORMS += \ + ui/mainwin.ui + +INCLUDEPATH += src /usr/local/include + +LIBS += -limago +unix: LIBS += -L/usr/local/lib -lGLEW +win32: LIBS += -lglew32