mirror of
https://github.com/cyberarm/cyberarm_engine.git
synced 2026-09-21 22:33:47 +00:00
Refactored mesh handling, imported AABB tree implementation from I-MIC FPS
This commit is contained in:
126
lib/cyberarm_engine/trees/aabb_node.rb
Normal file
126
lib/cyberarm_engine/trees/aabb_node.rb
Normal file
@@ -0,0 +1,126 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module CyberarmEngine
|
||||
class AABBTree
|
||||
class AABBNode
|
||||
attr_accessor :bounding_box, :parent, :object
|
||||
attr_reader :a, :b
|
||||
|
||||
def initialize(parent:, object:, bounding_box:)
|
||||
@parent = parent
|
||||
@object = object
|
||||
@bounding_box = bounding_box
|
||||
|
||||
@a = nil
|
||||
@b = nil
|
||||
end
|
||||
|
||||
def a=(leaf)
|
||||
@a = leaf
|
||||
@a.parent = self
|
||||
end
|
||||
|
||||
def b=(leaf)
|
||||
@b = leaf
|
||||
@b.parent = self
|
||||
end
|
||||
|
||||
def leaf?
|
||||
@object
|
||||
end
|
||||
|
||||
def insert_subtree(leaf)
|
||||
if leaf?
|
||||
new_node = AABBNode.new(parent: nil, object: nil, bounding_box: @bounding_box.union(leaf.bounding_box))
|
||||
|
||||
new_node.a = self
|
||||
new_node.b = leaf
|
||||
|
||||
new_node
|
||||
else
|
||||
cost_a = @a.bounding_box.volume + @b.bounding_box.union(leaf.bounding_box).volume
|
||||
cost_b = @b.bounding_box.volume + @a.bounding_box.union(leaf.bounding_box).volume
|
||||
|
||||
if cost_a == cost_b
|
||||
cost_a = @a.proximity(leaf)
|
||||
cost_b = @b.proximity(leaf)
|
||||
end
|
||||
|
||||
if cost_b < cost_a
|
||||
self.b = @b.insert_subtree(leaf)
|
||||
else
|
||||
self.a = @a.insert_subtree(leaf)
|
||||
end
|
||||
|
||||
@bounding_box = @bounding_box.union(leaf.bounding_box)
|
||||
|
||||
self
|
||||
end
|
||||
end
|
||||
|
||||
def search_subtree(collider, items = [])
|
||||
if @bounding_box.intersect?(collider)
|
||||
if leaf?
|
||||
items << self
|
||||
else
|
||||
@a.search_subtree(collider, items)
|
||||
@b.search_subtree(collider, items)
|
||||
end
|
||||
end
|
||||
|
||||
items
|
||||
end
|
||||
|
||||
def remove_subtree(leaf)
|
||||
if leaf
|
||||
self
|
||||
elsif leaf.parent == self
|
||||
other_child = other(leaf)
|
||||
other_child.parent = @parent
|
||||
other_child
|
||||
else
|
||||
leaf.parent.disown_child(leaf)
|
||||
self
|
||||
end
|
||||
end
|
||||
|
||||
def other(leaf)
|
||||
@a == leaf ? @b : @a
|
||||
end
|
||||
|
||||
def disown_child(leaf)
|
||||
value = other(leaf)
|
||||
raise "Can not replace child of a leaf!" if @parent.leaf?
|
||||
raise "Node is not a child of parent!" unless leaf.child_of?(@parent)
|
||||
|
||||
if @parent.a == self
|
||||
@parent.a = value
|
||||
else
|
||||
@parent.b = value
|
||||
end
|
||||
|
||||
@parent.update_bounding_box
|
||||
end
|
||||
|
||||
def child_of?(leaf)
|
||||
self == leaf.a || self == leaf.b
|
||||
end
|
||||
|
||||
def proximity(leaf)
|
||||
(@bounding_box - leaf.bounding_box).sum.abs
|
||||
end
|
||||
|
||||
def update_bounding_box
|
||||
node = self
|
||||
|
||||
unless node.leaf?
|
||||
node.bounding_box = node.a.bounding_box.union(node.b.bounding_box)
|
||||
|
||||
while (node = node.parent)
|
||||
node.bounding_box = node.a.bounding_box.union(node.b.bounding_box)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
55
lib/cyberarm_engine/trees/aabb_tree.rb
Normal file
55
lib/cyberarm_engine/trees/aabb_tree.rb
Normal file
@@ -0,0 +1,55 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module CyberarmEngine
|
||||
class AABBTree
|
||||
include AABBTreeDebug
|
||||
|
||||
attr_reader :root, :objects, :branches, :leaves
|
||||
|
||||
def initialize
|
||||
@objects = {}
|
||||
@root = nil
|
||||
@branches = 0
|
||||
@leaves = 0
|
||||
end
|
||||
|
||||
def insert(object, bounding_box)
|
||||
raise "BoundingBox can't be nil!" unless bounding_box
|
||||
raise "Object can't be nil!" unless object
|
||||
# raise "Object already in tree!" if @objects[object] # FIXME
|
||||
|
||||
leaf = AABBNode.new(parent: nil, object: object, bounding_box: bounding_box.dup)
|
||||
@objects[object] = leaf
|
||||
|
||||
insert_leaf(leaf)
|
||||
end
|
||||
|
||||
def insert_leaf(leaf)
|
||||
@root = @root ? @root.insert_subtree(leaf) : leaf
|
||||
end
|
||||
|
||||
def update(object, bounding_box)
|
||||
leaf = remove(object)
|
||||
leaf.bounding_box = bounding_box
|
||||
insert_leaf(leaf)
|
||||
end
|
||||
|
||||
# Returns a list of all objects that collided with collider
|
||||
def search(collider, return_nodes = false)
|
||||
items = []
|
||||
if @root
|
||||
items = @root.search_subtree(collider)
|
||||
items.map!(&:object) unless return_nodes
|
||||
end
|
||||
|
||||
items
|
||||
end
|
||||
|
||||
def remove(object)
|
||||
leaf = @objects.delete(object)
|
||||
@root = @root.remove_subtree(leaf) if leaf
|
||||
|
||||
leaf
|
||||
end
|
||||
end
|
||||
end
|
||||
29
lib/cyberarm_engine/trees/aabb_tree_debug.rb
Normal file
29
lib/cyberarm_engine/trees/aabb_tree_debug.rb
Normal file
@@ -0,0 +1,29 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module CyberarmEngine
|
||||
# Gets included into AABBTree
|
||||
module AABBTreeDebug
|
||||
def inspect
|
||||
@branches = 0
|
||||
@leaves = 0
|
||||
if @root
|
||||
node = @root
|
||||
|
||||
debug_search(node.a)
|
||||
debug_search(node.b)
|
||||
end
|
||||
|
||||
puts "<#{self.class}:#{object_id}> has #{@branches} branches and #{@leaves} leaves"
|
||||
end
|
||||
|
||||
def debug_search(node)
|
||||
if node.leaf?
|
||||
@leaves += 1
|
||||
else
|
||||
@branches += 1
|
||||
debug_search(node.a)
|
||||
debug_search(node.b)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user