Skip to content

Commit

Permalink
Merge pull request #20 from nhartland/edit_distance
Browse files Browse the repository at this point in the history
Edit Distance method
  • Loading branch information
nhartland committed Apr 10, 2020
2 parents 3254826 + 9d1b6d9 commit 13bf223
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Expand Up @@ -73,13 +73,14 @@ Initial release
# 0.5 (WIP)

## Features
- Convex hull computation
- Convex hull computation
- Edit distance between patterns

## Bugfix
- Including the circle raster unit test.
- Require a radius of at least 1 for primitives.circle

## Misc
- Require a radius of at least 1 for primitives.circle
- Ordering the example neighbourhood vector lists clockwise
- Improved error messages on some subpattern methods
- Slightly improved example gallery generation
Expand Down
11 changes: 11 additions & 0 deletions forma/pattern.lua
Expand Up @@ -234,6 +234,17 @@ function pattern.size_sort(pa, pb)
return pa:size() > pb:size()
end

--- Return the total number of differing cells between two patterns.
-- @param a first pattern for edit distance calculation
-- @param b second pattern for edit distance calculation
function pattern.edit_distance(a, b)
assert(getmetatable(a) == pattern, "pattern.edit_distance requires a pattern as the first argument")
assert(getmetatable(b) == pattern, "pattern.edit_distance requires a pattern as the second argument")
local common = pattern.intersection(a,b)
local edit_distance = (a - common):size() + (b-common):size()
return edit_distance
end

-----------------------
--- Iterators.
-- @section Iterators
Expand Down
16 changes: 16 additions & 0 deletions tests/pattern.lua
Expand Up @@ -340,3 +340,19 @@ function testPattern:testPacktileCentre()
lu.assertEquals(pp.x, 1)
lu.assertEquals(pp.y, 1)
end

function testPattern:testEditDistance()
-- Self-distances
lu.assertEquals(self.pattern_1:edit_distance(self.pattern_1), 0)
lu.assertEquals(self.pattern_2:edit_distance(self.pattern_2), 0)
lu.assertEquals(self.pattern_3:edit_distance(self.pattern_3), 0)
-- Non-overlapping
local p1 = pattern.new({{1,0},
{0,1}})
local p2 = pattern.new({{0,1},
{1,0}})
lu.assertEquals(p1:edit_distance(p2), 4)
-- Overlapping
local edit_distance_23 = self.pattern_2:edit_distance(self.pattern_3)
lu.assertEquals(edit_distance_23, 5*5 - 1) -- one common point
end

0 comments on commit 13bf223

Please sign in to comment.