Python Forum
Need help with octree - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Need help with octree (/thread-7338.html)



Need help with octree - cee012 - Jan-04-2018

Hello I'm trying to implement an octree, but when I query the centre positions they all return zero, but these should be offset from zero. Here is my code:
class Octree:

    def __init__( self, _centre, _size ):
        '''
        Class to create an Octree
        
        @param: _centre: [float,float,float], position in world space for centre of octree
        @param: _size: float, length of one edge of the first cube, should be big enough to fit all data
        '''

        self.centre = _centre
        self.size = _size
        self.octreeNodes = []

    def subdivideOctree( self, _subdivisions ):
    	'''
        subdivides octree 
        
        @param: _subdivisions: int, number of times to subdivide octree
        '''

            

        for i in range(8):
            newCentre = self.centre

            if ((i&2) == 2):
                    newCentre[0] += self.size * 0.25
            else:
                    newCentre[0] -= self.size * 0.25

            if ((i&4) == 4):
                    newCentre[1] += self.size * 0.25
            else:
                    newCentre[1] -= self.size * 0.25

            if ((i&1) == 1):
                    newCentre[2] += self.size * 0.25
            else:
                    newCentre[2] -= self.size * 0.25
                    
            print newCentre

            self.octreeNodes.append(Octree( newCentre, self.size * 0.5 ))

            if( _subdivisions > 0 ):
                    self.octreeNodes[i].subdivideOctree( _subdivisions - 1 )

    def isLeaf( self ):
    	return self.octreeNodes == None
                
    def getCentre( self ):
        return self.centre

    def getSize( self ):
        return self.size
                
a = Octree([0,0,0], 2.0)
a.subdivideOctree(2)

print a.getCentre()
print a.octreeNodes[0].getCentre()
print a.octreeNodes[0]

for n in a.octreeNodes:
    for b in n.octreeNodes:
        print b.getCentre()



RE: Need help with octree - Windspar - Jan-05-2018

int is rounding float.
self.centre = list(map(float, _centre))