spatial_hash 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.
- checksums.yaml +15 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.md +4 -0
- data/Rakefile +1 -0
- data/lib/spatial_hash.rb +40 -0
- data/lib/spatial_hash/version.rb +3 -0
- data/spatial_hash.gemspec +24 -0
- data/spec/lib/spatial_hash_spec.rb +99 -0
- data/spec/spec_helper.rb +20 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
NTQ0MDdiMGZjYTBjOWIyMTcxNWQwMWFlZDc5YmM4MTNkY2M5MWZjMQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
ZjA3ZjJjOTA1YjE2YjBmNTUyNDhiNjkxZThlZmNhZWRkNGJjMmExZQ==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
ZGY1M2FjOGMwNDI3NmZjZGE5NGE3OWI1NDU2Y2VkY2QyMzAxYTNmZjgyNGMz
|
10
|
+
MmM5ZjUyZDFhZDExNjQxZjYwMWU5YzkxZTg4MTg3N2Y5ODBlMjgxZDk4N2Zl
|
11
|
+
YTFmNGJjMjI1YTkyOGIzNDNlMDA3MzUxOGU3Y2I5ODhlMjkzMjI=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
N2IyOTNlZmQ1ZjNkYzAyM2VjYTdjZmIwMTM1MjI0M2FmODlkOTUwNDI3YjJm
|
14
|
+
YWFmNGEyMjgzYjBkYjJhYjk2M2U2M2E0YThiOTEwZDJmNjY5NmRjNzZmNmU0
|
15
|
+
YTRlNmE3MTRhMGY1YzllZDdlYzkwYjgyY2JjYTY3MzI4NDRjYjc=
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2014 Daniel Marín
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/spatial_hash.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'matrix'
|
2
|
+
|
3
|
+
class SpatialHash
|
4
|
+
|
5
|
+
attr_reader :cell_size, :cells
|
6
|
+
|
7
|
+
def initialize(cell_size)
|
8
|
+
@cell_size = cell_size
|
9
|
+
@cells = {}
|
10
|
+
end
|
11
|
+
|
12
|
+
def insert(object)
|
13
|
+
get_cells(object.box).each do |cell|
|
14
|
+
@cells[cell] ||= []
|
15
|
+
@cells[cell].push object
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def query(box)
|
20
|
+
objects = []
|
21
|
+
get_cells(box).each do |cell|
|
22
|
+
objects.push @cells[cell]
|
23
|
+
end
|
24
|
+
objects.flatten.compact.uniq
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def get_cell(point)
|
30
|
+
(point / cell_size).map(&:to_i)
|
31
|
+
end
|
32
|
+
|
33
|
+
def get_cells(box)
|
34
|
+
box = box.map { |point| get_cell(point) }
|
35
|
+
x_range = (box[0][0]..box[1][0]).to_a
|
36
|
+
y_range = (box[0][1]..box[1][1]).to_a
|
37
|
+
|
38
|
+
x_range.product y_range
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'spatial_hash/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "spatial_hash"
|
8
|
+
spec.version = SpatialHash::VERSION
|
9
|
+
spec.authors = ["Daniel Marin Cabillas"]
|
10
|
+
spec.email = ["danmarcab@gmail.com"]
|
11
|
+
spec.summary = %q{Ruby Spatial Hash.}
|
12
|
+
spec.description = %q{Simple implementation of a Spatial Hash.}
|
13
|
+
spec.homepage = "https://github.com/danmarcab/spatial_hash"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
22
|
+
spec.add_development_dependency "rake", "~> 10"
|
23
|
+
spec.add_development_dependency "rspec", "~> 2"
|
24
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe SpatialHash do
|
4
|
+
|
5
|
+
describe "#initialize" do
|
6
|
+
it "sets the cell size" do
|
7
|
+
spatial_hash = SpatialHash.new(30)
|
8
|
+
spatial_hash.cell_size.should == 30
|
9
|
+
end
|
10
|
+
|
11
|
+
it "creates empty hash of cells" do
|
12
|
+
spatial_hash = SpatialHash.new(30)
|
13
|
+
spatial_hash.cells == {}
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "#insert" do
|
18
|
+
context "when object fits in one cell" do
|
19
|
+
before do
|
20
|
+
@spatial_hash = SpatialHash.new(30)
|
21
|
+
@object = double('object')
|
22
|
+
allow(@object).to receive(:box) { [Vector[1, 1], Vector[2, 2]] }
|
23
|
+
@spatial_hash.insert(@object)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "inserts the object in the right cell" do
|
27
|
+
@spatial_hash.cells[[0, 0]].should include @object
|
28
|
+
end
|
29
|
+
|
30
|
+
it "inserts the object only in the right cell" do
|
31
|
+
@spatial_hash.cells.length.should == 1
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context "when object lies in multiple cells" do
|
36
|
+
before do
|
37
|
+
@spatial_hash = SpatialHash.new(30)
|
38
|
+
@object = double('object')
|
39
|
+
allow(@object).to receive(:box) { [Vector[1, 1], Vector[40, 40]] }
|
40
|
+
@spatial_hash.insert(@object)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "inserts the object in all the relevant cells" do
|
44
|
+
@spatial_hash.cells[[0, 0]].should include @object
|
45
|
+
@spatial_hash.cells[[0, 1]].should include @object
|
46
|
+
@spatial_hash.cells[[1, 0]].should include @object
|
47
|
+
@spatial_hash.cells[[1, 1]].should include @object
|
48
|
+
end
|
49
|
+
|
50
|
+
it "inserts the object only in the relevant cells" do
|
51
|
+
@spatial_hash.cells.length.should == 4
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "#query" do
|
57
|
+
context "when two objects in a cell" do
|
58
|
+
before do
|
59
|
+
@spatial_hash = SpatialHash.new(30)
|
60
|
+
@spatial_hash.instance_variable_set(:@cells, {[0, 0] => [:object_1, :object_2]})
|
61
|
+
@result = @spatial_hash.query([Vector[1, 1], Vector[2, 2]])
|
62
|
+
end
|
63
|
+
|
64
|
+
it "returns both objects" do
|
65
|
+
@result.should == [:object_1, :object_2]
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context "when two objects in diffrent cells" do
|
70
|
+
before do
|
71
|
+
@spatial_hash = SpatialHash.new(30)
|
72
|
+
@spatial_hash.instance_variable_set(:@cells, {[0, 0] => [:object_1], [1, 1] => [:object_2]})
|
73
|
+
end
|
74
|
+
|
75
|
+
it "returns only the object in the cell we are querying" do
|
76
|
+
@result = @spatial_hash.query([Vector[1, 1], Vector[2, 2]])
|
77
|
+
@result.should == [:object_1]
|
78
|
+
end
|
79
|
+
|
80
|
+
it "returns both objects if querying on both cells" do
|
81
|
+
@result = @spatial_hash.query([Vector[1, 1], Vector[40, 40]])
|
82
|
+
@result.should == [:object_1, :object_2]
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
context "when the same object in several cells" do
|
87
|
+
before do
|
88
|
+
@spatial_hash = SpatialHash.new(30)
|
89
|
+
@spatial_hash.instance_variable_set(:@cells, {[0, 0] => [:object_1, :object_2], [1, 1] => [:object_2]})
|
90
|
+
@result = @spatial_hash.query([Vector[1, 1], Vector[40, 40]])
|
91
|
+
end
|
92
|
+
|
93
|
+
it "removes duplicated objects" do
|
94
|
+
@result.should == [:object_1, :object_2]
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
|
8
|
+
Dir["#{Dir.pwd}/lib/*.rb"].each { |f| require f }
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
12
|
+
config.run_all_when_everything_filtered = true
|
13
|
+
config.filter_run :focus
|
14
|
+
|
15
|
+
# Run specs in random order to surface order dependencies. If you find an
|
16
|
+
# order dependency and want to debug it, you can fix the order by providing
|
17
|
+
# the seed, which is printed after each run.
|
18
|
+
# --seed 1234
|
19
|
+
config.order = 'random'
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: spatial_hash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daniel Marin Cabillas
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2'
|
55
|
+
description: Simple implementation of a Spatial Hash.
|
56
|
+
email:
|
57
|
+
- danmarcab@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- lib/spatial_hash.rb
|
68
|
+
- lib/spatial_hash/version.rb
|
69
|
+
- spatial_hash.gemspec
|
70
|
+
- spec/lib/spatial_hash_spec.rb
|
71
|
+
- spec/spec_helper.rb
|
72
|
+
homepage: https://github.com/danmarcab/spatial_hash
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ! '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ! '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.2.2
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Ruby Spatial Hash.
|
96
|
+
test_files:
|
97
|
+
- spec/lib/spatial_hash_spec.rb
|
98
|
+
- spec/spec_helper.rb
|