Visualization for isosurface
You modify the program of "Creating structured volume data".
1. Make a working directory
First please create a working directory (e.g., IsoSurface) and move in this directory.
# mkdir IsoSurface
# cd IsoSurface
2. Programming
You copy from "Creating structured volume data":
# cp ../CreateField/main.cpp .
and delete under the "file export".
For example:
#include <kvs/Vector3>
#include <kvs/StructuredVolumeObject>
#include <kvs/StructuredVolumeExporter>
#include <kvs/AVSField>
inline float func( float x, float y, float z) {
return ( 3.0 - x*x - y*y - 4.0*z*z );
}
int main( int argc, char** argv )
{
// resolution of voxel grid
kvs::UInt32 gridNum = 64;
const kvs::Vector3ui resol(gridNum, gridNum, gridNum);
kvs::ValueArray<float> data( gridNum * gridNum * gridNum );
float* pdata = data.pointer<float>();
float min = -2.0, max = 2.0;
float dt = (float)(max - min)/(float)(gridNum-1);
kvs::UInt64 index = 0;
for(int i=0; i< 64; i++) {
for(int j = 0; j<64; j++) {
for(int k=0; k<64; k++) {
float x = (float)min + (float)k*dt;
float y = (float)min + (float)j*dt;
float z = (float)min + (float)i*dt;
// Ealuate function
pdata[ index++ ] = func(x, y, z) ;
}
}
}
// Create the instance
kvs::StructuredVolumeObject *volume = new kvs::StructuredVolumeObject ( );
// Store the data
volume->setGridType( kvs::StructuredVolumeObject::Uniform );
volume->setResolution( resol );
volume->setVeclen( veclen );
volume->setValues( data );
3. Maxmum and Minimun value of field data
You can check whether the kvs::StructuredVolumeObject has the minimum and maximum values of a field data or not by using the member function:
- hasMinMaxValues():
- return the true or false
If the object does not have the minimum and maximum values, you calculate these value by using the member function:
- updateMinMaxValues()
// Calculate min/max values
if ( !volume->hasMinMaxValues() )
volume->updateMinMaxValues();
4. Input isovalue from Standard input
In this program, you input the isovalue from standard input and check whether the input value is in the data range.
// Input isovalue
double input_iso, iso;
double value_min = (double)volume->minValue( ) // mimimun
double value_max = (double>)volume->maxValue( ); // maxmum
std::cout << "Input Isolevel [ " << value_min << " - " << value_max << "] : ";
// from standart input
std::cin >> input_iso;
if ( input_iso < value_min ) iso = value_min;
else if( input_iso > value_max ) iso = value_max;
else iso = input_iso;
5. Create the Isosurface
In KVS, there is kvs::Isosurface class that creates the isosurface by using Marching cubes method and transfers to the type of kvs::PolygonObject.
The constructor of kvs::Isosurface has following input parameters:
- const kvs::VolumeObjectBase* volume:
- volume data
- const double isolevel:
- isovalue
- const kvs::PolygonObject::NormalType normal_type:
- type of normal for polygon data
And then, you check whether the polygon object can be created or not and remove unnecessary volume data.
For example,
kvs::PolygonObject* object =
new kvs::Isosurface( volume,
iso,
kvs::PolygonObject::VertexNormal );
// Check: create the polygon data
if ( !object )
{
kvsMessageError( "Cannot create a polygon object." );
delete volume;
return( false );
}
// remove volume data
delete volume;
6. Drawing
For visualization, we should instantiate classes kvs::glut::Application and kvs::glut::Screen and resiter the viusalized objects to the screen.
// Create and set Screen class
kvs::glut::Screen screen( &app );
screen.registerObject( object );
screen.setGeometry( 0, 0, 512, 512 );
screen.setTitle( "Isosurface");
screen.show();
// Event loop
return( app.run() );
7. Header file
You have to include the header file for following class:
- kvs::PolygonObject
- kvs::Isosurface
- kvs::glut::Screen
- kvs::glut::Application
Therefore, you include :
#include <kvs/PolygonObject>
#include <kvs/Isosurface>
#include <kvs/glut/Application>
#include <kvs/glut/Screen>
8. Sample program
Sample program: Isosurface.tgz
9. Compile and execute
Let's try to compile and execute above program.
When compiling a KVS program, it is easy to use the automatic Makefile-generation tool kvsmake supplied by KVS.
# kvsmake -G <-- Create Makefile
# kvsmake <-- Compile
And then, this program requires the isovalue, you input the isovale.
Following example inputs the isovallue = 0.0.
# ./Isosurface
Input Isolevel [ -21 - 2.99395] : 0.0
You can show the isosurface same as kvsview.
Last Updated at Nov. 1, 2015