Skip to content

Commit

Permalink
Fix combine bug for triangular bounding boxes
Browse files Browse the repository at this point in the history
  • Loading branch information
vhf committed Nov 29, 2020
1 parent 35fc11c commit 0fccc48
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 24 deletions.
18 changes: 11 additions & 7 deletions lib/ddrt/dynamic_rtree_impl/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,19 @@ defmodule DDRT.DynamicRtreeImpl.Utils do
end

# Return the area of a bounding box
def area([{a, b}, {c, d}]) when (b - a) * (d - c) != 0 do
(b - a) * (d - c)
end

def area([{_a, _b}, {_c, _d}]) do
-1
def area([{a, b}, {c, d}]) do
ab = b - a
cd = d - c

cond do
ab == 0 and cd != 0 -> cd
ab != 0 and cd == 0 -> ab
ab != 0 and cd != 0 -> ab * cd
ab == 0 and cd == 0 -> -1
end
end

# Return de the middle bounding box value
# Return the middle bounding box value
def middle_value([{a, b}, {c, d}]) do
(a + b + c + d) / 2
end
Expand Down
22 changes: 5 additions & 17 deletions test/drtree_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -90,30 +90,18 @@ defmodule DynamicRtreeTest do
assert root_box == [{-50, 36}, {-10, 41}]
end

test "MerkleMap inserts a same single point without crash" do
DynamicRtree.new(type: MerkleMap)
new_tuple = {new_node, _new_box} = {UUID.uuid1(), [{1, 2}, {3, 4}]}
{:ok, t} = DynamicRtree.insert(new_tuple)
assert t == DynamicRtree.tree()
{cont, parent, box} = t |> MerkleMap.get(new_node)
assert cont == :leaf
assert parent == t[:root]
assert box == [{1, 2}, {3, 4}]

test "MerkleMap inserts a triangular bounding box without crash" do
DynamicRtree.new(type: MerkleMap)

# ok:
# {:ok, t} = DynamicRtree.insert({1, [{9, 9.1}, {9, 9.1}]})
# crash:
{:ok, t} = DynamicRtree.insert({1, [{9, 9}, {9, 9.1}]})

assert t == DynamicRtree.tree()

root = t |> MerkleMap.get(:root)
{ch, _root_ptr, root_box} = t |> MerkleMap.get(root)
assert t |> Enum.to_list() |> length == t |> Enum.uniq() |> length
assert length(ch) == 2
assert root_box == [{-50, 36}, {-10, 41}]
assert length(ch) == 1
assert root_box == [{9, 9}, {9, 9.1}]
end

test "Map delete leaf keeps tree consistency" do
Expand Down Expand Up @@ -141,7 +129,7 @@ defmodule DynamicRtreeTest do
refute delete_id in (t[old_parent] |> elem(0))

{:ok, same_t} = DynamicRtree.delete(delete_id)
assert t = same_t
assert ^t = same_t

{:ok, t} = DynamicRtree.delete(1..100 |> Enum.map(fn x -> x end))
root = t[:root]
Expand Down Expand Up @@ -180,7 +168,7 @@ defmodule DynamicRtreeTest do
refute delete_id in (t |> MerkleMap.get(old_parent) |> elem(0))

{:ok, same_t} = DynamicRtree.delete(delete_id)
assert t = same_t
assert ^t = same_t

{:ok, t} = DynamicRtree.delete(1..100 |> Enum.map(fn x -> x end))
root = t |> MerkleMap.get(:root)
Expand Down

0 comments on commit 0fccc48

Please sign in to comment.