volmetrics

changeset 34:eb87d4a12bd3

histogram
author Eleni Maria Stea <elene.mst@gmail.com>
date Mon, 18 Aug 2014 10:44:11 +0300
parents 3e4eb9a0d999
children df4a277adb82
files src/main.cc src/volume.cc src/volume.h
diffstat 3 files changed, 54 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- a/src/main.cc	Sun Aug 17 02:25:01 2014 +0300
     1.2 +++ b/src/main.cc	Mon Aug 18 10:44:11 2014 +0300
     1.3 @@ -468,6 +468,23 @@
     1.4  		high = thres;
     1.5  	}
     1.6  
     1.7 +
     1.8 +	//drawing the histogram
     1.9 +	float hmax = 1 / (float)vol->max_histogram_value;
    1.10 +
    1.11 +	glBegin(GL_QUADS);
    1.12 +	for (int i=0; i<HIST_SIZE; i++) {
    1.13 +		float x0 = (float)i / HIST_SIZE;
    1.14 +		float x1 = (float)(i + 1) / HIST_SIZE;
    1.15 +
    1.16 +		glColor3f(0.3, 0.3, 0.3);
    1.17 +		glVertex3f(x0, 0, 0);
    1.18 +		glVertex3f(x1, 0, 0);
    1.19 +		glVertex3f(x1, hmax * vol->histogram[i + 1], 0);
    1.20 +		glVertex3f(x0, hmax * vol->histogram[i], 0);
    1.21 +	}
    1.22 +	glEnd();
    1.23 +
    1.24  	//drawing the transfer function curve
    1.25  
    1.26  	glLineWidth(3);
     2.1 --- a/src/volume.cc	Sun Aug 17 02:25:01 2014 +0300
     2.2 +++ b/src/volume.cc	Mon Aug 18 10:44:11 2014 +0300
     2.3 @@ -4,6 +4,7 @@
     2.4  
     2.5  #include <ctype.h>
     2.6  #include <errno.h>
     2.7 +#include <float.h>
     2.8  #include <stdio.h>
     2.9  #include <string.h>
    2.10  
    2.11 @@ -24,6 +25,9 @@
    2.12  	x_rot = 0;
    2.13  	vol_tex_valid = false;
    2.14  
    2.15 +	memset(histogram, 0, HIST_SIZE * sizeof(int));
    2.16 +	max_histogram_value = 0;
    2.17 +
    2.18  	glGenTextures(1, &vol_tex);
    2.19  }
    2.20  
    2.21 @@ -55,6 +59,31 @@
    2.22  	}
    2.23  }
    2.24  
    2.25 +void Volume::create_histogram()
    2.26 +{
    2.27 +	if (slices.empty())
    2.28 +		return;
    2.29 +
    2.30 +	for (size_t i=0; i<slices.size(); i++)
    2.31 +	{
    2.32 +		float *slice_pixels = (float*)slices[i].get_pixels();
    2.33 +		for (int j=0; j < width * height; j++)
    2.34 +		{
    2.35 +			float val = slice_pixels[j];
    2.36 +
    2.37 +			int ival = (int)(val * 256);
    2.38 +			histogram[ival]++;
    2.39 +		}
    2.40 +	}
    2.41 +
    2.42 +	max_histogram_value = 0;
    2.43 +	for (int i=0; i<HIST_SIZE; i++) {
    2.44 +		if (i < 10)
    2.45 +			continue;
    2.46 +		max_histogram_value = std::max(max_histogram_value, histogram[i]);
    2.47 +	}
    2.48 +}
    2.49 +
    2.50  bool Volume::load(const char *fname)
    2.51  {
    2.52  	char up = 'y';
    2.53 @@ -110,6 +139,7 @@
    2.54  	if (up == 'z')
    2.55  		x_rot = 90;
    2.56  
    2.57 +	create_histogram();
    2.58  	return true;
    2.59  }
    2.60  
     3.1 --- a/src/volume.h	Sun Aug 17 02:25:01 2014 +0300
     3.2 +++ b/src/volume.h	Mon Aug 18 10:44:11 2014 +0300
     3.3 @@ -4,6 +4,8 @@
     3.4  #include <vector>
     3.5  #include "image.h"
     3.6  
     3.7 +#define HIST_SIZE 256
     3.8 +
     3.9  class Mesh;
    3.10  
    3.11  class Volume {
    3.12 @@ -21,6 +23,10 @@
    3.13  	void create_vol_tex() const;
    3.14  
    3.15  public:
    3.16 +	int histogram[HIST_SIZE];
    3.17 +	int max_histogram_value;
    3.18 +
    3.19 +public:
    3.20  	Volume();
    3.21  	~Volume();
    3.22  
    3.23 @@ -43,6 +49,7 @@
    3.24  
    3.25  	void create_mesh(Mesh *mesh, float tmin, float tmax, int xres, int yres, int zres);
    3.26  
    3.27 +	void create_histogram();
    3.28  	void draw() const;
    3.29  };
    3.30