Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Heavy refactoring suggestion #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 8 additions & 17 deletions kdtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"math"
"sort"

"github.com/kyroy/kdtree/kdrange"
"github.com/kyroy/priority-queue"
)

Expand All @@ -32,6 +31,10 @@ type Point interface {
Dimensions() int
// Dimension returns the value of the i-th dimension.
Dimension(i int) float64
// Distance returns the distance between two points.
Distance(p Point) float64
// PlaneDistance returns the distance between the point and the plane X_{dim}=val.
PlaneDistance(val float64, dim int) float64
}

// KDTree represents the k-d tree.
Expand Down Expand Up @@ -138,7 +141,7 @@ func (t *KDTree) KNN(p Point, k int) []Point {
// RangeSearch returns all points in the given range r.
//
// Returns an empty slice when input is nil or len(r) does not equal Point.Dimensions().
func (t *KDTree) RangeSearch(r kdrange.Range) []Point {
func (t *KDTree) RangeSearch(r kdrangeRange) []Point {
if t.root == nil || r == nil || len(r) != t.root.Dimensions() {
return []Point{}
}
Expand Down Expand Up @@ -168,15 +171,15 @@ func knn(p Point, k int, start *node, currentAxis int, nearestPQ *pq.PriorityQue
// 2. move up
currentAxis = (currentAxis - 1 + p.Dimensions()) % p.Dimensions()
for path, currentNode = popLast(path); currentNode != nil; path, currentNode = popLast(path) {
currentDistance := distance(p, currentNode)
currentDistance := p.Distance(currentNode.Point)
checkedDistance := getKthOrLastDistance(nearestPQ, k-1)
if currentDistance < checkedDistance {
nearestPQ.Insert(currentNode, currentDistance)
checkedDistance = getKthOrLastDistance(nearestPQ, k-1)
}

// check other side of plane
if planeDistance(p, currentNode.Dimension(currentAxis), currentAxis) < checkedDistance {
if p.PlaneDistance(currentNode.Dimension(currentAxis), currentAxis) < checkedDistance {
var next *node
if p.Dimension(currentAxis) < currentNode.Dimension(currentAxis) {
next = currentNode.Right
Expand All @@ -189,18 +192,6 @@ func knn(p Point, k int, start *node, currentAxis int, nearestPQ *pq.PriorityQue
}
}

func distance(p1, p2 Point) float64 {
sum := 0.
for i := 0; i < p1.Dimensions(); i++ {
sum += math.Pow(p1.Dimension(i)-p2.Dimension(i), 2.0)
}
return math.Sqrt(sum)
}

func planeDistance(p Point, planePosition float64, dim int) float64 {
return math.Abs(planePosition - p.Dimension(dim))
}

func popLast(arr []*node) ([]*node, *node) {
l := len(arr) - 1
if l < 0 {
Expand Down Expand Up @@ -368,7 +359,7 @@ func (n *node) FindLargest(axis int, largest *node) *node {
return largest
}

func (n *node) RangeSearch(r kdrange.Range, axis int) []Point {
func (n *node) RangeSearch(r kdrangeRange, axis int) []Point {
points := []Point{}

for dim, limit := range r {
Expand Down
Loading