#! /usr/bin/python
# encoding = utf-8
###
### Octree Implementation
###
class Octree(object):
id = 0
'''Octree Implementation for PyVoxel. An octree is a tree with 8 children nodes per node.'''
def __init__(self, depth=0, limit=4, parent=None):
'''Creates a new octree. Limit defines the number of subdivisions it should have.
The default paramenter for limit is 8, or 2**8 = 256 voxels.'''
self.depth = depth
self.parent = parent
self.children = None
self.index = Octree.id
Octree.id += 1
# print self.index
# Voxel information
self.r = 1.0 / (depth + 1)
self.g = 1.0 / (depth + 1)
self.b = 1.0 / (depth + 1)
self.a = 1.0 / (depth + 1)
# Continue to create all subdivisions
if self.depth < limit:
self.children = [Octree(self.depth+1, limit, self) for i in range(8)]
def traversePrint(self):
print 'Found Level {0} Node with index {1}. Color({2:.3f}, {3:.3f}, {4:.3f}, {5:.3f})'.format(
self.depth, self.index, self.r, self.g, self.b, self.a)
if self.children is not None:
for c in self.children:
c.traversePrint()
def traverseTo(self, depth, nodes):
if depth > self.depth:
for c in self.children:
c.traverseTo(depth, nodes)
elif depth == self.depth:
nodes.append((self.r, self.g, self.b, self.a))
###
### Unit Test
###
if __name__ == '__main__':
octree = Octree()
nodes = []
octree.traverseTo(2, nodes)
print nodes
#! /usr/bin/python
# encoding = utf-8
###
### Octree Implementation
###
class Octree(object):
id = 0
'''Octree Implementation for PyVoxel. An octree is a tree with 8 children nodes per node.'''
def __init__(self, depth=0, limit=4, parent=None):
'''Creates a new octree. Limit defines the number of subdivisions it should have.
The default paramenter for limit is 8, or 2**8 = 256 voxels.'''
self.depth = depth
self.parent = parent
self.children = None
self.index = Octree.id
Octree.id += 1
# print self.index
# Voxel information
self.r = 1.0 / (depth + 1)
self.g = 1.0 / (depth + 1)
self.b = 1.0 / (depth + 1)
self.a = 1.0 / (depth + 1)
# Continue to create all subdivisions
if self.depth < limit:
self.children = [Octree(self.depth+1, limit, self) for i in range(8)]
def traversePrint(self):
print 'Found Level {0} Node with index {1}. Color({2:.3f}, {3:.3f}, {4:.3f}, {5:.3f})'.format(
self.depth, self.index, self.r, self.g, self.b, self.a)
if self.children is not None:
for c in self.children:
c.traversePrint()
def traverseTo(self, depth, nodes):
if depth > self.depth:
for c in self.children:
c.traverseTo(depth, nodes)
elif depth == self.depth:
nodes.append((self.r, self.g, self.b, self.a))
###
### Unit Test
###
if __name__ == '__main__':
octree = Octree()
nodes = []
octree.traverseTo(2, nodes)
print nodes