- Joined
- Apr 24, 2012
- Messages
- 5,113
This is a WIP to the vJASS Octree
JASS:
library Octree uses Table
struct Octree extends array
private Table root
private static thistype temp
private static thistype ic = 0
private static thistype array r
private static integer size
private static integer rx
private static integer ry
private static integer rz
private static method allocate takes nothing returns thistype
local thistype this = r[0]
if 0 == this then
set this = ic + 1
set ic = this
else
set r[0] = r[this]
endif
return this
endmethod
static method new takes nothing returns thistype
local thistype this = allocate()
set root = Table.create()
set root[-1] = 1 // size
set root[-2] = 0 // x
set root[-3] = 0 // y
set root[-4] = 0 // z
return this
endmethod
method expand takes integer x, integer y, integer z returns nothing
local Table troot
local boolean xpos
local boolean xneg
local boolean ypos
local boolean yneg
local boolean zpos
local boolean zneg
local integer i
local Table node
if this == 0 then
set this = temp
endif
set troot = root
set size = troot[-1]
set rx = x - troot[-2]
set ry = y - troot[-3]
set rz = z - troot[-4]
set xpos = rx >= size
set ypos = ry >= size
set zpos = rz >= size
set xneg = rx < -size
set yneg = ry < -size
set zneg = ry < -size
if (xpos or ypos or zpos or xneg or yneg or zneg) then
set node = Table.create()
set node[-1] = size*2
set node[-2] = 0
set node[-3] = 0
set node[-4] = 0
set i = 0
if rx >= 0 then
set node[-2] = troot[-2] + size
endif
if ry >= 0 then
set node[-3] = troot[-3] + size
endif
if rz >= 0 then
set node[-4] = troot[-4] + size
endif
if rx < 0 then
set node[-2] = troot[-2] - size
set i = i + 1
endif
if ry < 0 then
set node[-3] = troot[-3] - size
set i = i + 2
endif
if rz < 0 then
set node[-4] = troot[-4] - size
set i = i + 4
endif
set node[i] = node
set troot[-1] = 0
set troot[-2] = 0
set troot[-3] = 0
set troot[-4] = 0
set root = node
set temp = this
call expand.execute(x, y, z)
endif
endmethod
method get takes integer x, integer y, integer z returns integer
local Table node
local integer i
call expand(x, y, z)
set node = root
set size = node[-1]
set rx = x - node[-2]
set ry = y - node[-3]
set rz = z - node[-4]
loop
set size = size/2
set i = 0
if rx >= 0 then
set i = i + 1
set rx = rx - size
else
set rx = rx + size
endif
if ry >= 0 then
set i = i + 2
set ry = ry - size
else
set ry = ry + size
endif
if rz >= 0 then
set i = i + 4
set rz = rz - size
else
set rz = rz + size
endif
endloop
return 0
endmethod
endstruct
endlibrary