volmetrics

diff src/volume.cc @ 12:1272e3335608

using 2 thresholds (sigmoid functions) custom resolution in polygonization
author Eleni Maria Stea <elene.mst@gmail.com>
date Sun, 02 Feb 2014 02:18:08 +0200
parents c5af275b6a60
children 77a376198d38
line diff
     1.1 --- a/src/volume.cc	Mon Jan 27 01:22:02 2014 +0200
     1.2 +++ b/src/volume.cc	Sun Feb 02 02:18:08 2014 +0200
     1.3 @@ -168,6 +168,16 @@
     1.4  
     1.5  static Mesh *cur_mesh;
     1.6  static Volume *cur_vol;
     1.7 +static float low_thres, high_thres;
     1.8 +
     1.9 +static float smoothstep(float x, float low, float high)
    1.10 +{
    1.11 +	if(x < low) return 0.0;
    1.12 +	if(x >= high) return 1.0;
    1.13 +
    1.14 +	x = (x - low) / (high - low);
    1.15 +	return x * x * (3.0 - 2.0 * x);
    1.16 +}
    1.17  
    1.18  static float cb_eval(float x, float y, float z)
    1.19  {
    1.20 @@ -177,7 +187,9 @@
    1.21  	int px = (x + 1) / 2.0 * img->get_width();
    1.22  	int py = (y + 1) / 2.0 * img->get_height();
    1.23  
    1.24 -	return pixels[px + img->get_width() * py];
    1.25 +	float val = pixels[px + img->get_width() * py];
    1.26 +	float dt = 0.25 * (high_thres - low_thres);
    1.27 +	return smoothstep(val, low_thres - dt, low_thres + dt) * (1 - smoothstep(val, high_thres - dt, high_thres + dt));
    1.28  }
    1.29  
    1.30  static void cb_vertex(float x, float y, float z)
    1.31 @@ -193,14 +205,28 @@
    1.32  	cur_mesh->add_vertex(Vector3(x, y, z));
    1.33  }
    1.34  
    1.35 -void Volume::create_mesh(Mesh *mesh, float threshold)
    1.36 +void Volume::create_mesh(Mesh *mesh, float tmin, float tmax, int xres, int yres, int zres)
    1.37  {
    1.38 +	if (tmin > tmax) {
    1.39 +		float t = tmax;
    1.40 +		tmax = tmin;
    1.41 +		tmin = t;
    1.42 +	}
    1.43 +	low_thres = tmin; high_thres = tmax;
    1.44 +
    1.45 +	if(xres <= 0)
    1.46 +		xres = width;
    1.47 +	if(yres <= 0)
    1.48 +		yres = height;
    1.49 +	if(zres <= 0)
    1.50 +		zres = slices.size();
    1.51 +
    1.52  	cur_mesh = mesh;
    1.53  	cur_vol = this;
    1.54  
    1.55  	metasurface *ms = msurf_create();
    1.56 -	msurf_threshold(ms, threshold);
    1.57 -	msurf_resolution(ms, width, height, slices.size());
    1.58 +	msurf_threshold(ms, 0.5);
    1.59 +	msurf_resolution(ms, xres, yres, zres);
    1.60  	msurf_vertex_func(ms, cb_vertex);
    1.61  	msurf_eval_func(ms, cb_eval);
    1.62  	msurf_polygonize(ms);