Creating structured volume data
Let's create the structured volume data defined as following function:
f(x, y, z) = 3 - x2 - y2 - 4z2,
and then, this example outputs the "AVS Field" file.
In KVS, StructuredVolumeObject class has the member variables:
- Resolution of voxel grid (kvs::Vector3ui),
- Grid type (enum {UnknownGridType, Uniform, Rectilinear, Curvilinear} ),
- Field data (kvs::AnyValueArray),
- Grid coordinates (kvs::ValueArray<float>),
and so on.
1. Make a working directory
First, please create a working directory (e.g., CreateField) where you will run an store output data.
# mkdir CreateField
# cd CreateField
2. Programming
In order to evaluate the function, you define the in-line function.
inline float func( float x, float y, float z) {
return ( 3.0 - x*x - y*y - 4.0*z*z );
}
int main( void )
{
return ( 0 );
}
3. Preparation for volume data
You declare a resolution of voxel grid as kvs::Vector3ui.
kvs::UInt32 gridNum = 64;
const kvs::Vector3ui resol(gridNum, gridNum, gridNum);
The field data registers by using kvs::ValueArray class and transfers to the type of kvs::AnyValueArray.
kvs::ValueArray class allocates memory when the instance is created, this defined by
- Size of data
- Type
- Dynamic array to store the field data
For example the data allocates as the size of 64×64×64 , type of float:
kvs::ValueArray<float> data( gridNum * gridNum * gridNum );
4. Data storage
In kvs::ValueArray class, the field data stores by using pointer holding the address of data, for example:
float* pdata = data.data();
The function value is evaluated on 64×64×64 grid of a square lattice as the function’s domain [-2, 2].
float min = -2.0, max = 2.0;
float dt = (float)(max - min)/(float)(gridNum-1);
kvs::UInt64 index = 0;
for(int i=0; i< gridNum; i++) {
for(int j = 0; j<gridNum; j++) {
for(int k=0; k<gridNum; k++) {
float x = (float)min + (float)k*dt;
float y = (float)min + (float)j*dt;
float z = (float)min + (float)i*dt;
// Evaluate function
pdata[ index++ ] = func(x, y, z) ;
}
}
}
5. Create "StructuredVolumeObject"
The instance of kvs:: StructuredVolumeObject is allocated and then, the data for the structured volume object sets as following the member functions:
- setGridType( enum GridType type):
- sets to the grid type of field
- setResolution( const kvs::Vector3ui resolution):
- sets to the resolution of voxel grid
- setVeclen( const size_t veclen):
- sets to the number of data values per field element
- setValues( const kvs::AnyValueArray data);
- setores the field data
For example:
// 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 );
6. File exporter
In KVS, the structured volume object can be exported to a "AVS Field" file.
You create the instance of kvs::AVSField class in order to export the AVSField file by using kvs::StructuredVolumeExporter class.
kvs::StructuredVolumeExporter specifies the file format (e.g., kvs::AVSField, kvs::KVSMLObjectStructuredVolume ) and passes kvs::StructuredVolumeObject.
For example:
kvs::AVSField* field =
new kvs::StructuredVolumeExporter<kvs::AVSField>( volume );
if( !field ) {
std::cout << "Cannot export Resized volume data." <<std::endl;
delete volume;
return (false);
}
And then, the data is written in the file by using the menber function:
- write( const std::string& filename).
For example:
if ( !field->write( "test.fld" ) ) {
std::cout << "Cannot write to the file as AVS field format." << std::endl;
delete volume;
return( false );
}
7. Header file
You have to include the header file for following class:
- kvs::Vector3ui
- kvs::StructuredVolumeObject
- kvs::StructuredVolumeExporter
- kvs::AVSField
Therefore, you include :
#include <kvs/Vector3>
#include <kvs/StructuredVolumeObject>
#include <kvs/StructuredVolumeExporter>
#include <kvs/AVSField>
8. Sample program
Sample program: CreateField.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 <-- Generate Makefile
# kvsmake <-- Compile
And then, this program create the file “test.fld”.
# ./CreateField
# ls
CreateField Makefile.kvs main.cpp test.fld
You can draw the volume data by using the command-line application "kvsview".
# kvsview -Isosurface -l 0 test.fld <--- Draw Isosurface
Drawing Isosurface
Last Updated at Nov. 1, 2015