# Marching cubes toxiclibs

**URL:** https://discourse.processing.org/t/marching-cubes-toxiclibs/4120
**Category:** Libraries
**Created:** [October 4, 2018, 10:04am UTC](https://discourse.processing.org/t/marching-cubes-toxiclibs/4120 "2018-10-04T10:04:29Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![bryan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bryan/32/2538_2.png) [@bryan](https://discourse.processing.org/u/bryan)
#### Post date: [October 4, 2018, 10:04am UTC](https://discourse.processing.org/t/marching-cubes-toxiclibs/4120/1 "2018-10-04T10:04:29Z")

</div>

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

```auto
 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!

---

<div class="post-metadata">

### Author: ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)
#### Post date: [October 4, 2018, 10:37am UTC](https://discourse.processing.org/t/marching-cubes-toxiclibs/4120/2 "2018-10-04T10:37:49Z")

</div>

Hi! Did you see this example?

> **[postspectacular/toxiclibs](https://github.com/postspectacular/toxiclibs/tree/master/examples/volume/MRISurface)**
>
> Official master repo (Git version). Contribute to postspectacular/toxiclibs development by creating an account on GitHub.

  
In the code it says

```auto
  // 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:

```auto
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:

 ![2018-10-04-123213_1024x768_scrot](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/c0eef6f6cbe32b2292b94d000a5830a4db0a48c9.png)

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).

🙂

---

<div class="post-metadata">

### Author: ![bryan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bryan/32/2538_2.png) [@bryan](https://discourse.processing.org/u/bryan)
#### Post date: [October 5, 2018, 12:41am UTC](https://discourse.processing.org/t/marching-cubes-toxiclibs/4120/3 "2018-10-05T00:41:49Z")

</div>

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.
