Qtree is buggy.

Reaction score
86
Hellooo. Making a Qtree implementation. And I canNOT figure out what is wrong with the implementation. unfortunately its really annoying to debug so I was wondering if anyone can spot whats wrong with it? It gets buggy when it collapses. Although, there could be other bugs in it.

This code displays messages whenever the size of a Qtree changes and it also creates footmen and destroys them at the corners of the bounds.
It also displays when it splits and when it should collapse. I commented out the collapse function to see if that was causing it to bug. but there is still a bug for some reason.

In case you dont know what a qtree is it basically defines a rectangle. When you add nodes it splits the bounds into 4 spaces so that the nodes are added to those 4 spaces. This lets you check for nodes in the general area rather than check EVERY node in the bounds.

Implemented it using a doubly linked list for children of Qtree.
Insertion: logn/log4 I think?
Removal: logn/log4 I think? (needs to traverse to root of tree and decrease size)

OH and if you want to test out the code it uses T32

Code:
library Qtree initializer init
    globals
        integer MAXDEPTH=3
        integer MAXLEAVES=4
    endglobals
    struct node
        real x=0
        real y=0
        node prev=-1
        node next=-1
        Qtree root=-1
        method remove takes nothing returns nothing
            local Qtree qt=.root
            debug if(.root==-1)then
                debug call BJDebugMsg("Node not added!")
                debug return
            debug endif
            call .root.removeNode(this)
            loop
                set qt=qt.root
                exitwhen qt==-1
                set qt.size=qt.size-1
                if(qt.child.getType()==Qtree.typeid and qt.size<MAXLEAVES)then
                    call BJDebugMsg("Should collapse")
                    //call qt.collapse()
                endif
            endloop
        endmethod
        method update takes nothing returns boolean
            local Qtree qt=.root
            if(qt!=-1 and not .root.contains(.x,.y))then
                call .root.removeNode(this)
                loop
                set qt=qt.root
                exitwhen qt==-1
                set qt.size=qt.size-1
                call BJDebugMsg("SIZE "+I2S(qt.size))
                exitwhen qt.add(this)
                    if(qt.child.getType()==Qtree.typeid and qt.size<MAXLEAVES)then
                        call qt.collapse()
                    endif
                endloop
                if(qt==-1)then
                    return true
                endif
            endif
            return false
        endmethod
    endstruct
    struct Qtree extends node
        real r=0
        real b=0
        node child=-1
        integer depth=0
        integer size=0
        debug unit c1
        debug unit c2
        debug unit c3
        debug unit c4
        static method create takes real x, real y, real r, real b, integer d returns Qtree
            local Qtree qt = Qtree.allocate()
            debug set qt.c1= CreateUnit(Player(0),'hfoo',x,y,0)
            debug set qt.c2= CreateUnit(Player(0),'hfoo',x,b,0)
            debug set qt.c3= CreateUnit(Player(0),'hfoo',r,y,0)
            debug set qt.c4= CreateUnit(Player(0),'hfoo',r,b,0)
            set qt.x=x
            set qt.y=y
            set qt.r=r
            set qt.b=b
            set qt.depth=d
            return qt
        endmethod
        method contains takes real x, real y returns boolean
            return .x<x and .r>=x and .y<y and .b>=y
        endmethod
        method split takes nothing returns nothing
            local real mx=(.x+.r)/2
            local real my=(.y+.b)/2
            local integer newdepth=.depth+1
            local node temp
            local Qtree lt=Qtree.create(.x,.y,mx,my,newdepth)
            local Qtree rt=Qtree.create(mx,.y,.r,my,newdepth)
            local Qtree lb=Qtree.create(.x,my,mx,.b,newdepth)
            local Qtree rb=Qtree.create(mx,my,.r,.b,newdepth)
            call BJDebugMsg("SPLIT AT "+I2S(.depth+1))
            set lt.next=rt
            set rt.next=lb
            set lb.next=rb
            set lt.root=this
            set rt.root=this
            set lb.root=this
            set rb.root=this
            loop
            exitwhen .child==-1
                set temp=.child
                set .child=.child.next
                if(not (lt.add(temp) or rt.add(temp) or lb.add(temp) or rb.add(temp)))then
                    debug call BJDebugMsg("QTREE:FAILED TO ADD NODE")
                endif
            endloop
            set .child=lt
        endmethod
        method removeNode takes node n returns nothing
            if(not .contains(n.x,n.y))then
                //call BJDebugMsg("Remove Node")
                set .size=.size-1
                call BJDebugMsg("SIZE "+I2S(.size))
                set n.next.prev=n.prev
                set n.prev.next=n.next
                set n.next=-1
                set n.prev=-1
                set n.root=-1
            endif
        endmethod
        method collapse takes nothing returns nothing
            local node temp=-1
            local node tempLower
            local Qtree tempQtree
            call BJDebugMsg("Collapse at "+I2S(.depth+1))
            loop
            exitwhen .child==-1
                set tempQtree= Qtree(.child)
                loop
                exitwhen tempQtree.child==-1
                    set tempLower=tempQtree.child
                    set tempQtree.child=tempQtree.child.next
                    set tempLower.next=temp
                    if(temp!=-1)then
                        set temp.prev = tempLower
                    endif
                    set temp =tempLower
                    set temp.root=this
                endloop
                set .child=.child.next
                call tempQtree.destroy()
            endloop
            set .child=temp
        endmethod
        method add takes node n returns boolean
            local node temp
            if(not .contains(n.x,n.y))then
                return false
            endif
            set .size=.size+1
            call BJDebugMsg("SIZE "+I2S(.size))
            if(.child.getType()==Qtree.typeid)then
                set temp = .child
                loop
                exitwhen temp==-1 or Qtree(temp).add(n)
                    set temp=temp.next
                    if(temp==-1)then
                        set .size=.size-1
                        call BJDebugMsg("QTREE:THIS SHOULD NEVER HAPPEN")
                    endif
                endloop
                return true
            endif
            //debug call BJDebugMsg("ADDED TO "+I2S(.depth))
            set .child.prev=n
            set n.next=.child
            set .child=n
            set n.root=this
            if(.depth<MAXDEPTH and .size>MAXLEAVES)then
                call .split()
            endif
            return true
        endmethod
        debug method onDestroy takes nothing returns nothing
            debug call RemoveUnit(.c1)
            debug call RemoveUnit(.c2)
            debug call RemoveUnit(.c3)
            debug call RemoveUnit(.c4)
        debug endmethod
    endstruct
    struct unitCapture extends node
        unit u
        Qtree top
        method periodic takes nothing returns nothing
            set .x=GetUnitX(.u)
            set .y=GetUnitY(.u)
            if(not .root.contains(.x,.y))then
                call .remove()
                call .top.add(this)
            endif
        endmethod
        implement T32x
    endstruct
    function init takes nothing returns nothing
            local Qtree qt =Qtree.create(-2000,-2000,2000,2000,0)
            local integer i=0
            local unitCapture uc
            local real x
            local real y
            local node n
            loop
            exitwhen i>4
                set x= GetRandomReal(-2000,2000)
                set y= GetRandomReal(-2000,2000)
                set uc= unitCapture.create()
                set uc.u=CreateUnit(Player(0),'hgry',x,y,0)
                set uc.x=x
                set uc.y=y
                set uc.top=qt
                call uc.startPeriodic()
                call qt.add(uc)
                set i =i+1
            endloop
        endfunction
endlibrary
 

Sevion

The DIY Ninja
Reaction score
413
I'm not sure -1 as an index works. I generally just use 0 for the root/head/what-have-you.
 

Sevion

The DIY Ninja
Reaction score
413
Ah, okay. Give me a little bit to read through the code.

So, I just messed around in a test map and I ended up getting a lot of "QTREE:THIS SHOULD NEVER HAPPEN" messages along with "Node not added!" and sizes of over 12421.
 

SanKakU

Member
Reaction score
21
what is this qtree you're talking about? i looked at the code but i don't really see what i'd expect to see, so i don't know what it is at all...i don't feel like importing code without knowinh what it is supposed to do
 
Reaction score
86
q-tree= Quad tree => qtree wiki
It basically indexes points by location in trees. if 4 or more points are on one tree it splits into 4 trees. if four or more points are on any of those it splits again. This way, when you want to do collision detection you can get all the points in the top tree and then calculate distance between points. this way it wrks more efficiently than calculating all the distances between all units.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top