What is the best way to handle 3d voxel data?

Question:

At the moment I am storing voxels in a simple list. ([0,0,0,0,0,1,0,0,1 … ])
To retrieve voxel positon in the list I use this function:

def getBlockKey(self, x, y, z):

    blockX = x % CFG_CHUNK_SIZE
    blockY = y % CFG_CHUNK_SIZE
    blockZ = z % CFG_CHUNK_SIZE

    return blockX + blockY * CFG_CHUNK_SIZE + blockZ * (CFG_CHUNK_SIZE * CFG_CHUNK_SIZE)

But this is very slow when handling big amounts of data. For example iterating over each cube in 256*256*256 chunk takes 50 seconds.

I guess it’d be faster to use dict, but it uses way too much memory.

Could you help me to find a compromise between these two?

Asked By: Tom Ray

||

Answers:

You might replace the list with a bytearray. Assuming you keep one bit of information per voxel, you can store 8 voxels per byte.

You may also try ndarray, from NumPy. You can efficiently construct a 256×256×256 array which takes up less space and is faster to access and manipulate than a normal Python data structure (in certain ways).

Answered By: ephemient

You might want to check an open-source library from DreamWorks if you need to handle massive amount of voxels.

http://www.openvdb.org

Answered By: eonil