glviewvol

changeset 3:32c4a7160350

den kanei kryo stin ellada
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 28 Dec 2014 21:48:15 +0200
parents 701507c8238f
children 04330eb80b36
files .hgignore src/dicomview.cc src/image.cc src/image.h src/main.cc src/opt.cc src/opt.h src/volume.cc src/volume.h
diffstat 9 files changed, 138 insertions(+), 5 deletions(-) [+]
line diff
     1.1 --- a/.hgignore	Sun Dec 28 15:26:36 2014 +0200
     1.2 +++ b/.hgignore	Sun Dec 28 21:48:15 2014 +0200
     1.3 @@ -2,3 +2,4 @@
     1.4  \.d$
     1.5  \.swp$
     1.6  ^dicomview$
     1.7 +^data/
     2.1 --- a/src/dicomview.cc	Sun Dec 28 15:26:36 2014 +0200
     2.2 +++ b/src/dicomview.cc	Sun Dec 28 21:48:15 2014 +0200
     2.3 @@ -1,21 +1,46 @@
     2.4 +#include <stdio.h>
     2.5  #include "opengl.h"
     2.6  #include "dicomview.h"
     2.7  #include "rend_fast.h"
     2.8 +#include "opt.h"
     2.9 +#include "volume.h"
    2.10  
    2.11  static int win_width, win_height;
    2.12  static float cam_theta, cam_phi, cam_dist = 6;
    2.13  
    2.14  static Renderer *rend;
    2.15 +static Volume *vol;
    2.16  
    2.17  extern "C" {
    2.18  
    2.19  int init()
    2.20  {
    2.21 -	rend = new RendererFast;
    2.22 -	if(!rend->init()) {
    2.23 +	if(!opt.fname) {
    2.24 +		fprintf(stderr, "you must specify the volume data filename\n");
    2.25  		return -1;
    2.26  	}
    2.27  
    2.28 +	switch(opt.rend_type) {
    2.29 +	case REND_FAST:
    2.30 +		rend = new RendererFast;
    2.31 +		break;
    2.32 +	default:
    2.33 +		return -1;
    2.34 +	}
    2.35 +
    2.36 +	if(!rend->init()) {
    2.37 +		fprintf(stderr, "renderer initialization failed\n");
    2.38 +		return -1;
    2.39 +	}
    2.40 +
    2.41 +	VoxelVolume *voxvol = new VoxelVolume;
    2.42 +	if(!voxvol->load(opt.fname)) {
    2.43 +		fprintf(stderr, "failed to load volume data from: %s\n", opt.fname);
    2.44 +		return -1;
    2.45 +	}
    2.46 +	vol = voxvol;
    2.47 +	rend->set_volume(vol);
    2.48 +
    2.49  	return 0;
    2.50  }
    2.51  
    2.52 @@ -23,6 +48,7 @@
    2.53  {
    2.54  	rend->destroy();
    2.55  	delete rend;
    2.56 +	delete vol;
    2.57  }
    2.58  
    2.59  void ev_display()
     3.1 --- a/src/image.cc	Sun Dec 28 15:26:36 2014 +0200
     3.2 +++ b/src/image.cc	Sun Dec 28 21:48:15 2014 +0200
     3.3 @@ -8,7 +8,7 @@
     3.4  	pixels = 0;
     3.5  }
     3.6  
     3.7 -Image::~Image()
     3.8 +void Image::destroy()
     3.9  {
    3.10  	delete [] pixels;
    3.11  }
     4.1 --- a/src/image.h	Sun Dec 28 15:26:36 2014 +0200
     4.2 +++ b/src/image.h	Sun Dec 28 21:48:15 2014 +0200
     4.3 @@ -7,7 +7,8 @@
     4.4  	float *pixels;
     4.5  
     4.6  	Image();
     4.7 -	~Image();
     4.8 +
     4.9 +	void destroy();
    4.10  
    4.11  	// if data == 0, just allocate space
    4.12  	bool set_pixels(int x, int y, float *data = 0);
     5.1 --- a/src/main.cc	Sun Dec 28 15:26:36 2014 +0200
     5.2 +++ b/src/main.cc	Sun Dec 28 21:48:15 2014 +0200
     5.3 @@ -11,6 +11,7 @@
     5.4  #endif
     5.5  
     5.6  #include "dicomview.h"
     5.7 +#include "opt.h"
     5.8  
     5.9  static void display();
    5.10  static void reshape(int x, int y);
    5.11 @@ -22,6 +23,11 @@
    5.12  int main(int argc, char **argv)
    5.13  {
    5.14  	glutInit(&argc, argv);
    5.15 +
    5.16 +	if(parse_args(argc, argv) == -1) {
    5.17 +		return 1;
    5.18 +	}
    5.19 +
    5.20  	glutInitWindowSize(1280, 800);
    5.21  	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    5.22  	glutCreateWindow("dicom viewer");
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/src/opt.cc	Sun Dec 28 21:48:15 2014 +0200
     6.3 @@ -0,0 +1,72 @@
     6.4 +#include <stdio.h>
     6.5 +#include <string.h>
     6.6 +#include "opt.h"
     6.7 +
     6.8 +static struct {
     6.9 +	const char *name;
    6.10 +	RendererType rtype;
    6.11 +} rend[] = {
    6.12 +	{ "fast", REND_FAST },
    6.13 +	{ 0, REND_NONE }
    6.14 +};
    6.15 +
    6.16 +Options opt;
    6.17 +
    6.18 +static void init_opt()
    6.19 +{
    6.20 +	opt.fname = 0;
    6.21 +	opt.rend_type = REND_FAST;
    6.22 +}
    6.23 +
    6.24 +static RendererType renderer_type(const char *name)
    6.25 +{
    6.26 +	for(int i=0; rend[i].name; i++) {
    6.27 +		if(strcmp(rend[i].name, name) == 0) {
    6.28 +			return rend[i].rtype;
    6.29 +		}
    6.30 +	}
    6.31 +	return REND_NONE;
    6.32 +}
    6.33 +
    6.34 +int parse_args(int argc, char **argv)
    6.35 +{
    6.36 +	init_opt();
    6.37 +
    6.38 +	for(int i=1; i<argc; i++) {
    6.39 +		if(argv[i][0] == '-') {
    6.40 +			if(argv[i][2] == 0) {
    6.41 +				switch(argv[i][1]) {
    6.42 +				case 'r':
    6.43 +					if((opt.rend_type = renderer_type(argv[++i])) == REND_NONE) {
    6.44 +						if(strcmp(argv[i], "help") == 0) {
    6.45 +							printf("available renderers: ");
    6.46 +							for(int j=0; rend[j].name; j++) {
    6.47 +								printf("%s ", rend[j].name);
    6.48 +							}
    6.49 +							putchar('\n');
    6.50 +							return -1;
    6.51 +						}
    6.52 +						fprintf(stderr, "unknown renderer type: %s (use -r help to show list of renderers)\n", argv[i]);
    6.53 +						return -1;
    6.54 +					}
    6.55 +					break;
    6.56 +
    6.57 +				default:
    6.58 +					fprintf(stderr, "invalid option: %s\n", argv[i]);
    6.59 +					return -1;
    6.60 +				}
    6.61 +			} else {
    6.62 +				fprintf(stderr, "invalid option: %s\n", argv[i]);
    6.63 +				return -1;
    6.64 +			}
    6.65 +		} else {
    6.66 +			if(opt.fname) {
    6.67 +				fprintf(stderr, "unexpected argument: %s\n", argv[i]);
    6.68 +				return -1;
    6.69 +			}
    6.70 +			opt.fname = argv[i];
    6.71 +		}
    6.72 +	}
    6.73 +
    6.74 +	return 0;
    6.75 +}
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/src/opt.h	Sun Dec 28 21:48:15 2014 +0200
     7.3 @@ -0,0 +1,18 @@
     7.4 +#ifndef OPT_H_
     7.5 +#define OPT_H_
     7.6 +
     7.7 +enum RendererType {
     7.8 +	REND_NONE,
     7.9 +	REND_FAST
    7.10 +};
    7.11 +
    7.12 +struct Options {
    7.13 +	char *fname;
    7.14 +	RendererType rend_type;
    7.15 +};
    7.16 +
    7.17 +extern Options opt;
    7.18 +
    7.19 +int parse_args(int argc, char **argv);
    7.20 +
    7.21 +#endif	// OPT_H_
     8.1 --- a/src/volume.cc	Sun Dec 28 15:26:36 2014 +0200
     8.2 +++ b/src/volume.cc	Sun Dec 28 21:48:15 2014 +0200
     8.3 @@ -79,8 +79,17 @@
     8.4  	size[0] = size[1] = size[2] = 0;
     8.5  }
     8.6  
     8.7 +VoxelVolume::~VoxelVolume()
     8.8 +{
     8.9 +	for(size_t i=0; i<slices.size(); i++) {
    8.10 +		slices[i].destroy();
    8.11 +	}
    8.12 +}
    8.13 +
    8.14  bool VoxelVolume::load(const char *fname)
    8.15  {
    8.16 +	if(!fname) return false;
    8.17 +
    8.18  	char *prefix = (char*)alloca(strlen(fname) + 1);
    8.19  	strcpy(prefix, fname);
    8.20  	char *slash = strrchr(prefix, '/');
    8.21 @@ -120,7 +129,6 @@
    8.22  		}
    8.23  
    8.24  		slices.push_back(img);
    8.25 -		img.pixels = 0;	// otherwise the destructor will free it
    8.26  	}
    8.27  
    8.28  	size[2] = slices.size();
     9.1 --- a/src/volume.h	Sun Dec 28 15:26:36 2014 +0200
     9.2 +++ b/src/volume.h	Sun Dec 28 21:48:15 2014 +0200
     9.3 @@ -27,6 +27,7 @@
     9.4  
     9.5  public:
     9.6  	VoxelVolume();
     9.7 +	~VoxelVolume();
     9.8  
     9.9  	bool load(const char *fname);
    9.10