Skip to content

Commit

Permalink
Merge pull request #5 from vhf/triangular-bug
Browse files Browse the repository at this point in the history
Fix for triangular bounding boxes
  • Loading branch information
sikanrong authored Dec 12, 2023
2 parents 3c0ba6d + 0fccc48 commit 4f6aa4b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 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
18 changes: 16 additions & 2 deletions test/drtree_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,20 @@ defmodule DynamicRtreeTest do
assert root_box == [{-50, 36}, {-10, 41}]
end

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

{: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) == 1
assert root_box == [{9, 9}, {9, 9.1}]
end

test "Map delete leaf keeps tree consistency" do
DynamicRtree.new()

Expand All @@ -115,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 @@ -154,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 4f6aa4b

Please sign in to comment.