rb_kd_tree 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +54 -0
- data/Rakefile +4 -0
- data/ext/kd_tree/extconf.rb +3 -0
- data/ext/kd_tree/kd_tree.c +114 -0
- data/ext/kd_tree/vendor/kdtree.c +836 -0
- data/ext/kd_tree/vendor/kdtree.h +129 -0
- data/kd_tree.gemspec +26 -0
- data/lib/kd_tree.bundle +0 -0
- data/lib/kd_tree.rb +5 -0
- data/lib/kd_tree/version.rb +3 -0
- data/spec/kd_tree_spec.rb +111 -0
- data/spec/spec_helper.rb +5 -0
- metadata +130 -0
@@ -0,0 +1,129 @@
|
|
1
|
+
/*
|
2
|
+
This file is part of ``kdtree'', a library for working with kd-trees.
|
3
|
+
Copyright (C) 2007-2011 John Tsiombikas <nuclear@member.fsf.org>
|
4
|
+
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
7
|
+
|
8
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
9
|
+
list of conditions and the following disclaimer.
|
10
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
11
|
+
this list of conditions and the following disclaimer in the documentation
|
12
|
+
and/or other materials provided with the distribution.
|
13
|
+
3. The name of the author may not be used to endorse or promote products
|
14
|
+
derived from this software without specific prior written permission.
|
15
|
+
|
16
|
+
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
17
|
+
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
18
|
+
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
19
|
+
EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
20
|
+
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
21
|
+
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
22
|
+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
23
|
+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
24
|
+
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
25
|
+
OF SUCH DAMAGE.
|
26
|
+
*/
|
27
|
+
#ifndef _KDTREE_H_
|
28
|
+
#define _KDTREE_H_
|
29
|
+
|
30
|
+
#ifdef __cplusplus
|
31
|
+
extern "C" {
|
32
|
+
#endif
|
33
|
+
|
34
|
+
struct kdtree;
|
35
|
+
struct kdres;
|
36
|
+
|
37
|
+
|
38
|
+
/* create a kd-tree for "k"-dimensional data */
|
39
|
+
struct kdtree *kd_create(int k);
|
40
|
+
|
41
|
+
/* free the struct kdtree */
|
42
|
+
void kd_free(struct kdtree *tree);
|
43
|
+
|
44
|
+
/* remove all the elements from the tree */
|
45
|
+
void kd_clear(struct kdtree *tree);
|
46
|
+
|
47
|
+
/* if called with non-null 2nd argument, the function provided
|
48
|
+
* will be called on data pointers (see kd_insert) when nodes
|
49
|
+
* are to be removed from the tree.
|
50
|
+
*/
|
51
|
+
void kd_data_destructor(struct kdtree *tree, void (*destr)(void*));
|
52
|
+
|
53
|
+
/* insert a node, specifying its position, and optional data */
|
54
|
+
int kd_insert(struct kdtree *tree, const double *pos, void *data);
|
55
|
+
int kd_insertf(struct kdtree *tree, const float *pos, void *data);
|
56
|
+
int kd_insert3(struct kdtree *tree, double x, double y, double z, void *data);
|
57
|
+
int kd_insert3f(struct kdtree *tree, float x, float y, float z, void *data);
|
58
|
+
|
59
|
+
/* Find the nearest node from a given point.
|
60
|
+
*
|
61
|
+
* This function returns a pointer to a result set with at most one element.
|
62
|
+
*/
|
63
|
+
struct kdres *kd_nearest(struct kdtree *tree, const double *pos);
|
64
|
+
struct kdres *kd_nearestf(struct kdtree *tree, const float *pos);
|
65
|
+
struct kdres *kd_nearest3(struct kdtree *tree, double x, double y, double z);
|
66
|
+
struct kdres *kd_nearest3f(struct kdtree *tree, float x, float y, float z);
|
67
|
+
|
68
|
+
/* Find the N nearest nodes from a given point.
|
69
|
+
*
|
70
|
+
* This function returns a pointer to a result set, with at most N elements,
|
71
|
+
* which can be manipulated with the kd_res_* functions.
|
72
|
+
* The returned pointer can be null as an indication of an error. Otherwise
|
73
|
+
* a valid result set is always returned which may contain 0 or more elements.
|
74
|
+
* The result set must be deallocated with kd_res_free after use.
|
75
|
+
*/
|
76
|
+
/*
|
77
|
+
struct kdres *kd_nearest_n(struct kdtree *tree, const double *pos, int num);
|
78
|
+
struct kdres *kd_nearest_nf(struct kdtree *tree, const float *pos, int num);
|
79
|
+
struct kdres *kd_nearest_n3(struct kdtree *tree, double x, double y, double z);
|
80
|
+
struct kdres *kd_nearest_n3f(struct kdtree *tree, float x, float y, float z);
|
81
|
+
*/
|
82
|
+
|
83
|
+
/* Find any nearest nodes from a given point within a range.
|
84
|
+
*
|
85
|
+
* This function returns a pointer to a result set, which can be manipulated
|
86
|
+
* by the kd_res_* functions.
|
87
|
+
* The returned pointer can be null as an indication of an error. Otherwise
|
88
|
+
* a valid result set is always returned which may contain 0 or more elements.
|
89
|
+
* The result set must be deallocated with kd_res_free after use.
|
90
|
+
*/
|
91
|
+
struct kdres *kd_nearest_range(struct kdtree *tree, const double *pos, double range);
|
92
|
+
struct kdres *kd_nearest_rangef(struct kdtree *tree, const float *pos, float range);
|
93
|
+
struct kdres *kd_nearest_range3(struct kdtree *tree, double x, double y, double z, double range);
|
94
|
+
struct kdres *kd_nearest_range3f(struct kdtree *tree, float x, float y, float z, float range);
|
95
|
+
|
96
|
+
/* frees a result set returned by kd_nearest_range() */
|
97
|
+
void kd_res_free(struct kdres *set);
|
98
|
+
|
99
|
+
/* returns the size of the result set (in elements) */
|
100
|
+
int kd_res_size(struct kdres *set);
|
101
|
+
|
102
|
+
/* rewinds the result set iterator */
|
103
|
+
void kd_res_rewind(struct kdres *set);
|
104
|
+
|
105
|
+
/* returns non-zero if the set iterator reached the end after the last element */
|
106
|
+
int kd_res_end(struct kdres *set);
|
107
|
+
|
108
|
+
/* advances the result set iterator, returns non-zero on success, zero if
|
109
|
+
* there are no more elements in the result set.
|
110
|
+
*/
|
111
|
+
int kd_res_next(struct kdres *set);
|
112
|
+
|
113
|
+
/* returns the data pointer (can be null) of the current result set item
|
114
|
+
* and optionally sets its position to the pointers(s) if not null.
|
115
|
+
*/
|
116
|
+
void *kd_res_item(struct kdres *set, double *pos);
|
117
|
+
void *kd_res_itemf(struct kdres *set, float *pos);
|
118
|
+
void *kd_res_item3(struct kdres *set, double *x, double *y, double *z);
|
119
|
+
void *kd_res_item3f(struct kdres *set, float *x, float *y, float *z);
|
120
|
+
|
121
|
+
/* equivalent to kd_res_item(set, 0) */
|
122
|
+
void *kd_res_item_data(struct kdres *set);
|
123
|
+
|
124
|
+
|
125
|
+
#ifdef __cplusplus
|
126
|
+
}
|
127
|
+
#endif
|
128
|
+
|
129
|
+
#endif /* _KDTREE_H_ */
|
data/kd_tree.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'kd_tree/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rb_kd_tree"
|
8
|
+
spec.version = KdTree::VERSION
|
9
|
+
spec.authors = ["Ryan Closner"]
|
10
|
+
spec.email = ["ryan@ryanclosner.com"]
|
11
|
+
spec.description = %q{A KDTree Library}
|
12
|
+
spec.summary = %q{A KDTree Library}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/|^kd_tree$}) { |f| File.basename(f) }
|
18
|
+
spec.extensions = ['ext/kd_tree/extconf.rb']
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ["lib", "ext"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
spec.add_development_dependency "rake-compiler"
|
25
|
+
spec.add_development_dependency "rspec"
|
26
|
+
end
|
data/lib/kd_tree.bundle
ADDED
Binary file
|
data/lib/kd_tree.rb
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
|
4
|
+
describe KDTree do
|
5
|
+
|
6
|
+
let(:nodes) { [[1,2,3], [4,5,6]] }
|
7
|
+
let(:dims) { 3 }
|
8
|
+
|
9
|
+
subject { described_class.new(nodes, dims) }
|
10
|
+
|
11
|
+
describe ".new" do
|
12
|
+
subject { described_class }
|
13
|
+
|
14
|
+
context "with valid arguments" do
|
15
|
+
it "returns an instance of KDTree" do
|
16
|
+
subject.new(nodes, dims).should be_a(KDTree)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context "with no nodes" do
|
21
|
+
let(:nodes) { nil }
|
22
|
+
it "raises an error" do
|
23
|
+
expect { subject.new(nodes, dims) }.to raise_error
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context "with no dimensions" do
|
28
|
+
let(:dims) { nil }
|
29
|
+
it "raises an error" do
|
30
|
+
expect { subject.new(nodes, dims) }.to raise_error
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context "with non-array nodes" do
|
35
|
+
let(:nodes) { {} }
|
36
|
+
it "raises an error" do
|
37
|
+
expect { subject.new(nodes, dims) }.to raise_error
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context "with non-integer dimensions" do
|
42
|
+
let(:dims) { '3' }
|
43
|
+
it "raises an error" do
|
44
|
+
expect { subject.new(nodes, dims) }.to raise_error
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
context "with invalid dimensions" do
|
49
|
+
let(:dims) { 2 }
|
50
|
+
it "raises an error" do
|
51
|
+
expect { subject.new(nodes, dims) }.to raise_error
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "#insert" do
|
58
|
+
|
59
|
+
let(:node) { [6,7,8] }
|
60
|
+
|
61
|
+
it "returns the inserted node" do
|
62
|
+
subject.insert(node).should == node
|
63
|
+
end
|
64
|
+
|
65
|
+
it "inserts the node into the tree" do
|
66
|
+
subject.insert(node)
|
67
|
+
res = subject.nearest_neighbor(node)
|
68
|
+
res.fetch("query").should == node
|
69
|
+
res.fetch("position").should == node
|
70
|
+
res.fetch("distance").should == 0.0
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
describe "#nearest_neighbor" do
|
76
|
+
let(:query) { nodes.first }
|
77
|
+
|
78
|
+
let(:response) { subject.nearest_neighbor(query) }
|
79
|
+
|
80
|
+
it "returns a hash" do
|
81
|
+
response.should be_a(Hash)
|
82
|
+
end
|
83
|
+
|
84
|
+
it "returns the query" do
|
85
|
+
response.fetch("query").should == query
|
86
|
+
end
|
87
|
+
|
88
|
+
it "returns the matched node" do
|
89
|
+
response.fetch("position").should == query
|
90
|
+
end
|
91
|
+
|
92
|
+
it "returns the distance" do
|
93
|
+
response.fetch("distance").should == 0.0
|
94
|
+
end
|
95
|
+
|
96
|
+
context "distance" do
|
97
|
+
|
98
|
+
# Query Node: [ 101, 102, 103 ]
|
99
|
+
# Expected Node: [ 4, 5, 6 ]
|
100
|
+
# Calculation: (101 - 4)^2 + (102 - 5)^2 + (103 - 6)^2 = 28227.0
|
101
|
+
|
102
|
+
let(:query) { nodes.first.map {|i| i + 100 } }
|
103
|
+
|
104
|
+
it "calculates correctly" do
|
105
|
+
response.fetch("distance").should == 28227.0
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rb_kd_tree
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ryan Closner
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-08-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake-compiler
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: A KDTree Library
|
79
|
+
email:
|
80
|
+
- ryan@ryanclosner.com
|
81
|
+
executables: []
|
82
|
+
extensions:
|
83
|
+
- ext/kd_tree/extconf.rb
|
84
|
+
extra_rdoc_files: []
|
85
|
+
files:
|
86
|
+
- .gitignore
|
87
|
+
- Gemfile
|
88
|
+
- LICENSE.txt
|
89
|
+
- README.md
|
90
|
+
- Rakefile
|
91
|
+
- ext/kd_tree/extconf.rb
|
92
|
+
- ext/kd_tree/kd_tree.c
|
93
|
+
- ext/kd_tree/vendor/kdtree.c
|
94
|
+
- ext/kd_tree/vendor/kdtree.h
|
95
|
+
- kd_tree.gemspec
|
96
|
+
- lib/kd_tree.bundle
|
97
|
+
- lib/kd_tree.rb
|
98
|
+
- lib/kd_tree/version.rb
|
99
|
+
- spec/kd_tree_spec.rb
|
100
|
+
- spec/spec_helper.rb
|
101
|
+
homepage: ''
|
102
|
+
licenses:
|
103
|
+
- MIT
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
require_paths:
|
107
|
+
- lib
|
108
|
+
- ext
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
+
none: false
|
111
|
+
requirements:
|
112
|
+
- - ! '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
none: false
|
117
|
+
requirements:
|
118
|
+
- - ! '>='
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
requirements: []
|
122
|
+
rubyforge_project:
|
123
|
+
rubygems_version: 1.8.23
|
124
|
+
signing_key:
|
125
|
+
specification_version: 3
|
126
|
+
summary: A KDTree Library
|
127
|
+
test_files:
|
128
|
+
- spec/kd_tree_spec.rb
|
129
|
+
- spec/spec_helper.rb
|
130
|
+
has_rdoc:
|