It is currently Sat Aug 22, 2020 5:47 am


All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: about loadVolumeRaw
PostPosted: Thu Oct 25, 2012 1:12 pm 

Joined: Mon Oct 15, 2012 4:33 pm
Posts: 52
hi, this is my first post on this forum, I found polyvox after some research about voxel . I m very glad to use it and i started to do a little addon for OpenFrameworks : http://www.openframeworks.cc/. I was able to implement the little example into OpenFrameworks, just to see how to works with it.
Now one of the first functionality that i would like to add to the addon is the function to load .Raw files. i found :

Quote:
polyvox_shared_ptr< VolumeType< VoxelType > > PolyVox::loadVolumeRaw ( std::istream & stream,
VolumeSerializationProgressListener * progressListener = 0
)

but i m a bit confused on how to use it... you can see here what i tried to do:

Quote:
void loadraw(const char *filename){

int width = 256;
int height = 256;
int depth = 256;
//Create a volume

LargeVolume<MaterialDensityPair44> volData(&load, &unload, 256);
FILE *fp = fopen(filename, "rb");
if (!fp){
std::cout<<"raw file missed! "<<"\n";
return 0;
}

float *data = new float [width*height*depth];
fread(data, sizeof(float), width*height*depth, fp);
fclose(fp);

//Clear volume to zeros.
//FIXME - Add function to PolyVox for this.
for(unsigned int z = 0; z < volData.getDepth(); ++z)
{
for(unsigned int y = 0; y < volData.getHeight(); ++y)
{
for(unsigned int x = 0; x < volData.getWidth(); ++x)
{
float voxelVal;

MaterialDensityPair44 voxel;
if(voxelVal > 0.0f)
{
voxel.setDensity(MaterialDensityPair44::getMaxDensity());
voxel.setMaterial(1);
}
else
{
voxel.setDensity(MaterialDensityPair44::getMinDensity());
voxel.setMaterial(0);
}
volData.setVoxelAt(x,y,z,voxel);
}
}
cout << z << endl;
}

SimpleVolume<MaterialDensityPair88> Volume(const PolyVox::Region& reg);
//polyvox_shared_ptr< VolumeType > volume(new LargeVolume<VolumeType::VoxelType>(width, height, depth));
//polyvox_shared_ptr< volData > loadVolumeRaw(fp);
polyvox_shared_ptr< Volume > loadVolumeRaw(fp);
//loadVolumeRaw(fp);

}

please, could someone explain how to use loadVolumeRaw? maybe with a little piece of code?
and also wich .raw files can i'use? 32 bit or 16 bit?
Thanks
Walter


Top
Offline Profile  
Reply with quote  
 Post subject: Re: about loadVolumeRaw
PostPosted: Fri Oct 26, 2012 8:39 am 
Developer
User avatar

Joined: Sun May 04, 2008 6:35 pm
Posts: 1827
Actually that function has been deprecated in PolyVox... if you run in debug mode you should find you hit an assert() which prevents it running. Also, when it refers to 'raw' data it really just means that it dumps the data to disk without any real formatting, and does not neessarily correspond to any defined .raw format (and I don't think there is a .raw format defined anywhere).

For volume loading and saving I think your best bet is just to write it yourself based on the file format you are trying to load. Create a volume of the correct size and type, open the file, and just iterate over each voxel reading one at a time with fread() and writing it to the volume with setVoxelAt(). If you look at the implementation of 'loadVolumeRaw()' then it will still give you an idea about how the code can be structured.


Top
Offline Profile  
Reply with quote  
 Post subject: Re: about loadVolumeRaw
PostPosted: Fri Oct 26, 2012 12:24 pm 

Joined: Mon Oct 15, 2012 4:33 pm
Posts: 52
Quote:
Actually that function has been deprecated in PolyVox... if you run in debug mode you should find you hit an assert() which prevents it running. Also, when it refers to 'raw' data it really just means that it dumps the data to disk without any real formatting, and does not neessarily correspond to any defined .raw format (and I don't think there is a .raw format defined anywhere).


I haven't seen that was a deprecated function... i think the best solution is to use an interpreter software like Freeimage that is yet used by OpenFrameworks (or maybe using directly OpenFrameworks ofImage class that use it). .raw files aren't only "pixels" data so i think it is simple to use Freeimage As you say :
Quote:
Create a volume of the correct size and type, open the file, and just iterate over each voxel reading one at a time with fread() and writing it to the volume with setVoxelAt(). If you look at the implementation of 'loadVolumeRaw()' then it will still give you an idea about how the code can be structured.


Thanks David for the tips ! i will inform you of my progress.
Walter


Top
Offline Profile  
Reply with quote  
 Post subject: Re: about loadVolumeRaw
PostPosted: Fri Oct 26, 2012 6:33 pm 

Joined: Mon Oct 15, 2012 4:33 pm
Posts: 52
taking suggestion from loadVolume raw i get this code raw file is loaded but 0 vertices are created and so nothing is displayed:
Quote:
void load( const ConstVolumeProxy<MaterialDensityPair44>& volume, const PolyVox::Region& reg)
{
Vector3DFloat v3dVolCenter;

ifstream stream;
filebuf * fb;

fb =stream.rdbuf();
fb->open ("data.raw",ios::in);

if ( fb->is_open() )

cout << "file data.raw is open!.\n";
else
cout << "file data.raw is not open.\n";



//Read volume dimensions
uint8_t volumeWidthPower = 0;
uint8_t volumeHeightPower = 0;
uint8_t volumeDepthPower = 0;
stream.read(reinterpret_cast<char*>(&volumeWidthPower), sizeof(volumeWidthPower));
stream.read(reinterpret_cast<char*>(&volumeHeightPower), sizeof(volumeHeightPower));
stream.read(reinterpret_cast<char*>(&volumeDepthPower), sizeof(volumeDepthPower));

uint16_t volumeWidth = 0x0001 << volumeWidthPower;
uint16_t volumeHeight = 0x0001 << volumeHeightPower;
uint16_t volumeDepth = 0x0001 << volumeDepthPower;

//Read data
bool firstTime = true;
uint32_t runLength = 0;
//VolumeType::VoxelType value;
MaterialDensityPair44 voxel;
//stream.read(reinterpret_cast<char*>(&value), sizeof(value));
stream.read(reinterpret_cast<char*>(&voxel), sizeof(voxel));
stream.read(reinterpret_cast<char*>(&runLength), sizeof(runLength));
/*
int32_t wdt = reg.width();
int32_t hgt = reg.height();
int32_t dpt = reg.depth();
wdt = volumeWidth;
cout << "volumeWidth:" << wdt <<endl;
hgt = volumeHeight;
cout << "volumeHeight:" << hgt <<endl;
dpt = volumeDepth;
cout << "volumeDepth:" << dpt <<endl;
*/
for(int x = reg.getLowerCorner().getX() ; x <= reg.getUpperCorner().getX(); x++)
//for(int x = 0; x <= volumeWidth; x++)
{
for(int y = reg.getLowerCorner().getY(); y <= reg.getUpperCorner().getY(); y++)
//for(int y = 0; y <= volumeHeight; y++)
{

for(int z = reg.getLowerCorner().getZ(); z <= reg.getUpperCorner().getZ(); z++)
//for(int z = 0; z <= volumeDepth; z++)
{


if(runLength != 0)
{
voxel.setMaterial(0);
voxel.setDensity(MaterialDensityPair44::getMinDensity());
volume.setVoxelAt(x,y,z, voxel);
runLength--;
}
else
{
//stream.read(reinterpret_cast<char*>(&value), sizeof(value));
stream.read(reinterpret_cast<char*>(& voxel), sizeof( voxel));
stream.read(reinterpret_cast<char*>(&runLength), sizeof(runLength));
voxel.setMaterial(245);
voxel.setDensity(MaterialDensityPair44::getMaxDensity());
volume.setVoxelAt(x,y,z, voxel);
runLength--;
}
volume.setVoxelAt(x, y, z, voxel);
}
}
}
//fclose(fb);

fb->close();
}


load() refer to:
Quote:
LargeVolume<MaterialDensityPair44> volData(&load, &unload, 256);

volData.setMaxNumberOfBlocksInMemory(4096);
volData.setMaxNumberOfUncompressedBlocks(64);

sure my approach isn't correct, not sure if make sense create a region without get the dimensions of the raw file , i tried but reg.height() is deprecated and don't know another options, what can you suggest me to do?


Top
Offline Profile  
Reply with quote  
 Post subject: Re: about loadVolumeRaw
PostPosted: Fri Oct 26, 2012 7:36 pm 
Developer
User avatar

Joined: Sun May 04, 2008 6:35 pm
Posts: 1827
I think the first thing is to make sure you understand the format of the data you are working with so that you can decide how to interpret it. For example, my own 'raw' format started with 3 bytes which encoded the size of the volume. For example, a value of 7 meant a size of 128 because I would interpret that as 2^7. Your file format is almost certainly different (because .raw is not a standard) and so you probably want to remove/rewrite the lines which determine the volume size.

So what is the format of your data? Do you know how it is laid out on disk? You can use the loadVolumeRaw() function as an inspiration for how a volume loading function could look, but the actual details will need to be reimplemented according to your format.

kalwalt wrote:
load() refer to:
Quote:
LargeVolume<MaterialDensityPair44> volData(&load, &unload, 256);

volData.setMaxNumberOfBlocksInMemory(4096);
volData.setMaxNumberOfUncompressedBlocks(64);

sure my approach isn't correct, not sure if make sense create a region without get the dimensions of the raw file , i tried but reg.height() is deprecated and don't know another options, what can you suggest me to do?


The load and unload function which are passed as parameters to LargeVolume are seperate from this and are for the purpose of paging. I would suggest you use RawVolume instead to avoid confusion.

In summary, these are the stes you need to load a volume:
1) Work out how big your volume is. For testing, you might want to just do this in your head and hard code it.
2) Create a RawVolume with that size.
3) Use fread() to read one voxel at a time from your file. To do this you will need to understand the format of your data
4) Write that voxel into the volume using setVoxelAt().


Top
Offline Profile  
Reply with quote  
 Post subject: Re: about loadVolumeRaw
PostPosted: Wed Oct 31, 2012 4:50 pm 

Joined: Mon Oct 15, 2012 4:33 pm
Posts: 52
I was able to use an .exr file to make a terrain , without so much difficulties with the ofFloatImage OpenFrameworks class :

Quote:
// this in load()
ofFloatImage img;

img.loadImage("nyc-small.exr");

float perlinVal = img.getColor(x, y).getBrightness();

perlinVal += 1.0f;
perlinVal *= 0.5f;
perlinVal *= 255;


It is not exactly what I wanted (ie load a 3d texture, the file that I use in this example is a 2D texture) but for now is fine and is already an improvement! Also it is possible to convert .png files to .exr in Photoshop (Remember: you must convert to 32bit format before exporting to .exr format).


Top
Offline Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Theme created StylerBB.net