glviewvol

changeset 14:0d2447b9c512

did the histogram... doesn't seem to work right
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 01 Jan 2015 06:36:45 +0200
parents 64f874301b53
children 2a67ea257ac0
files src/viewer.cc src/volume.cc src/volume.h src/xfer_view.cc src/xfer_view.h
diffstat 5 files changed, 93 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- a/src/viewer.cc	Wed Dec 31 07:57:42 2014 +0200
     1.2 +++ b/src/viewer.cc	Thu Jan 01 06:36:45 2015 +0200
     1.3 @@ -71,6 +71,7 @@
     1.4  	if(!xfview_init(xfer)) {
     1.5  		return -1;
     1.6  	}
     1.7 +	xfview_set_volume(vol);
     1.8  
     1.9  	return 0;
    1.10  }
     2.1 --- a/src/volume.cc	Wed Dec 31 07:57:42 2014 +0200
     2.2 +++ b/src/volume.cc	Thu Jan 01 06:36:45 2015 +0200
     2.3 @@ -94,6 +94,10 @@
     2.4  VoxelVolume::VoxelVolume()
     2.5  {
     2.6  	size[0] = size[1] = size[2] = 0;
     2.7 +
     2.8 +	hist_valid = false;
     2.9 +	hist = 0;
    2.10 +	num_hist_samples = 0;
    2.11  }
    2.12  
    2.13  VoxelVolume::~VoxelVolume()
    2.14 @@ -151,6 +155,7 @@
    2.15  	}
    2.16  
    2.17  	size[2] = slices.size();
    2.18 +	hist_valid = false;
    2.19  	fclose(fp);
    2.20  	return true;
    2.21  }
    2.22 @@ -223,6 +228,39 @@
    2.23  	return slices[z].pixels[y * size[0] + x];
    2.24  }
    2.25  
    2.26 +float *VoxelVolume::calc_histogram(int hist_samples)
    2.27 +{
    2.28 +	if(hist && hist_samples == num_hist_samples && hist_valid) {
    2.29 +		return hist;
    2.30 +	}
    2.31 +
    2.32 +	int num_pixels = size[0] * size[1] * size[2];
    2.33 +	if(!num_pixels) {
    2.34 +		return 0;
    2.35 +	}
    2.36 +
    2.37 +	delete [] hist;
    2.38 +	hist = new float[hist_samples];
    2.39 +	memset(hist, 0, hist_samples * sizeof *hist);
    2.40 +
    2.41 +	for(int i=0; i<size[2]; i++) {
    2.42 +		float *pptr = slices[i].pixels;
    2.43 +		for(int j=0; j<size[0] * size[1]; j++) {
    2.44 +			int idx = (int)(*pptr++ * (float)hist_samples);
    2.45 +
    2.46 +			hist[idx] += 1.0;
    2.47 +		}
    2.48 +	}
    2.49 +
    2.50 +	for(int i=0; i<hist_samples; i++) {
    2.51 +		hist[i] /= (float)num_pixels;
    2.52 +	}
    2.53 +
    2.54 +	hist_valid = true;
    2.55 +	num_hist_samples = hist_samples;
    2.56 +	return hist;
    2.57 +}
    2.58 +
    2.59  static char *strip_space(char *s)
    2.60  {
    2.61  	while(*s && isspace(*s)) s++;
     3.1 --- a/src/volume.h	Wed Dec 31 07:57:42 2014 +0200
     3.2 +++ b/src/volume.h	Thu Jan 01 06:36:45 2015 +0200
     3.3 @@ -42,6 +42,10 @@
     3.4  	int size[3];
     3.5  	std::vector<Image> slices;
     3.6  
     3.7 +	float *hist;
     3.8 +	int num_hist_samples;
     3.9 +	bool hist_valid;
    3.10 +
    3.11  public:
    3.12  	VoxelVolume();
    3.13  	~VoxelVolume();
    3.14 @@ -52,6 +56,8 @@
    3.15  
    3.16  	float valuef(float x, float y, float z) const;
    3.17  	float valuei(int x, int y, int z) const;
    3.18 +
    3.19 +	float *calc_histogram(int hist_samples);
    3.20  };
    3.21  
    3.22  #endif	// VOLUME_H_
     4.1 --- a/src/xfer_view.cc	Wed Dec 31 07:57:42 2014 +0200
     4.2 +++ b/src/xfer_view.cc	Thu Jan 01 06:36:45 2015 +0200
     4.3 @@ -20,8 +20,12 @@
     4.4  #include "opengl.h"
     4.5  #include "xfer_view.h"
     4.6  #include "viewer.h"
     4.7 +#include "volume.h"
     4.8 +
     4.9 +static void draw_histogram();
    4.10  
    4.11  static TransferFunc *xfer;
    4.12 +static Volume *vol;
    4.13  
    4.14  static int act_color = -1;
    4.15  static int grabbed_handle = -1;
    4.16 @@ -37,6 +41,11 @@
    4.17  {
    4.18  }
    4.19  
    4.20 +void xfview_set_volume(Volume *volarg)
    4.21 +{
    4.22 +	vol = volarg;
    4.23 +}
    4.24 +
    4.25  void xfview_draw()
    4.26  {
    4.27  	float line_color[][3] = {
    4.28 @@ -64,6 +73,8 @@
    4.29  	glVertex2f(-1, 1);
    4.30  	glEnd();
    4.31  
    4.32 +	draw_histogram();
    4.33 +
    4.34  	glEnable(GL_LINE_SMOOTH);
    4.35  
    4.36  	glEnable(GL_BLEND);
    4.37 @@ -191,6 +202,40 @@
    4.38  	glDisable(GL_BLEND);
    4.39  }
    4.40  
    4.41 +#define HIST_SAMPLES	256
    4.42 +
    4.43 +static void draw_histogram()
    4.44 +{
    4.45 +	VoxelVolume *voxvol = dynamic_cast<VoxelVolume*>(vol);
    4.46 +	if(!voxvol) return;
    4.47 +
    4.48 +	float *hist = voxvol->calc_histogram(HIST_SAMPLES);
    4.49 +	if(!hist) return;
    4.50 +
    4.51 +	float max_y = 0.0f;
    4.52 +	for(int i=0; i<HIST_SAMPLES; i++) {
    4.53 +		if(hist[i] > max_y) {
    4.54 +			max_y = hist[i];
    4.55 +		}
    4.56 +	}
    4.57 +
    4.58 +	float dx = 1.0 / (float)HIST_SAMPLES;
    4.59 +	glBegin(GL_QUADS);
    4.60 +	glColor3f(0.6, 0.6, 0.6);
    4.61 +	for(int i=0; i<HIST_SAMPLES; i++) {
    4.62 +		float x0 = (float)i / (float)HIST_SAMPLES;
    4.63 +		float x1 = x0 + dx;
    4.64 +
    4.65 +		float y = hist[i] / max_y;
    4.66 +
    4.67 +		glVertex2f(x0, 0);
    4.68 +		glVertex2f(x1, 0);
    4.69 +		glVertex2f(x1, y);
    4.70 +		glVertex2f(x0, y);
    4.71 +	}
    4.72 +	glEnd();
    4.73 +}
    4.74 +
    4.75  static int prev_x, prev_y;
    4.76  
    4.77  void xfview_button(int bn, int press, int x, int y)
     5.1 --- a/src/xfer_view.h	Wed Dec 31 07:57:42 2014 +0200
     5.2 +++ b/src/xfer_view.h	Thu Jan 01 06:36:45 2015 +0200
     5.3 @@ -18,11 +18,14 @@
     5.4  #ifndef XFER_VIEW_H_
     5.5  #define XFER_VIEW_H_
     5.6  
     5.7 +#include "volume.h"
     5.8  #include "xfermap.h"
     5.9  
    5.10  bool xfview_init(TransferFunc *xfer);
    5.11  void xfview_destroy();
    5.12  
    5.13 +void xfview_set_volume(Volume *vol);
    5.14 +
    5.15  void xfview_draw();
    5.16  
    5.17  void xfview_button(int bn, int press, int x, int y);