Marching cubes toxiclibs

Hi everyone! I’m struggling trying to figure out how to load a volume dataset (like the example of MRI surface in the toxiclibs volume library). The thing is, that i cannot find other volume to upload successfully. in what format can i upload volumes, i tried some, but still don’t know if did it correctly.

Here’s the example of the MRISurface in volume utils from toxiclibs library

 byte[] mriData=loadBytes("bonsai.raw.gz");
  // scale factor to normalize 8bit to the 0.0 - 1.0 interval
  float mriNormalize=1/255.0;
  // setup lower resolution grid for IsoSurface
  VolumetricSpaceArray volume=new VolumetricSpaceArray(SCALE,DIM,DIM,DIM);
  float[] cloud=volume.getData();
  int stride=256/DIM;
  for(int z=0,idx=0; z<256; z+=stride) {
    for(int y=0; y<256; y+=stride) {
      int sliceIdx=y*256+z*65536;
      for(int x=0; x<256; x+=stride) {
        byte b=mriData[x+sliceIdx];
        cloud[idx++]=(int)(b<0 ? 256+b : b)*mriNormalize;
      }
    }
  }

Thanks!

1 Like

Hi! Did you see this example?


In the code it says

  // convert MRI scan data into floats
  // MRI data is 256 x 256 x 256 voxels @ 8bit/voxel

If you look at the file name, it ends with .gz. That means it has been compressed with gzip. In Linux you can use gunzip file.raw.gz to uncompress, and gzip file.raw to compress. In other platforms you need to find out. (Maybe it’s not necessary that the file is compressed).

Once uncompressed, you will notice the file size is 16777216, which is exactly 256x256x256 bytes, a 3D byte array. Each byte in that file is a value between 0 and 255. A 0 means empty space. A higher number probably means that cell is occupied.

With this little program you can test creating a sphere:

byte[] bytes = new byte[256*256*256];

for (int x=0; x<256; x++) {
  for (int y=0; y<256; y++) {
    for (int z=0; z<256; z++) {
      boolean on = dist(x, y, z, 128, 128, 128) < 50;
      bytes[x+y*256+z*256*256] = (byte)(on ? 255 : 0);
    }
  }
}
saveBytes("sphere.raw", bytes);

Then you can call gzip sphere.raw to reduce the file size. It looks like this:

ps. It would be nice if you edit your post, select the code and click </> to enable syntax highlighting. Even better if you format the code in Processing before pasting it in the forum (by pressing Ctrl+T or CMD+T).

:slight_smile:

2 Likes

Many thanks! it is really helpful, also, i was looking for some datasets of MRI scans or something related, to generate the volume on the marching cubes algorithm, but so far i can only run the example that you showed.

Thanks again, i really appreciate your time.