ruby-polylabel 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f0c2577e61c99e78adfa9a5f651cac8732410211a3b90fa3e48b51f2ac6afbad
4
+ data.tar.gz: 11636879edea4e84ef98fdd704ed89328efe94e7526619cfd84c79c5731460d6
5
+ SHA512:
6
+ metadata.gz: 0ba94c257e1d00591c7c3ec54485f2e0b8654a6ac4686ea9b7ec640f1c6369e860d71bbd10cc4b31933f87f3cafd993b767cad68a0b42b1a00088a078f178be6
7
+ data.tar.gz: bc14dcde433a6b99b77fc7d0c33b9d9935c296624fc07454c219129b35f2d49498fc48a3d4a13c5b0c85092c4b32737d24631acac7c6798056eeaa89bc0cb7e8
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-01-09
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in polylabel.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,23 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ruby-polylabel (0.1.0)
5
+ pqueue (~> 2.1.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ minitest (5.15.0)
11
+ pqueue (2.1.0)
12
+ rake (13.0.6)
13
+
14
+ PLATFORMS
15
+ x86_64-linux
16
+
17
+ DEPENDENCIES
18
+ minitest (~> 5.0)
19
+ rake (~> 13.0)
20
+ ruby-polylabel!
21
+
22
+ BUNDLED WITH
23
+ 2.2.32
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Frédéric Planté
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Polylabel
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/polylabel`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'polylabel'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install polylabel
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/polylabel.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "polylabel"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,66 @@
1
+ module Polylabel
2
+ class Cell
3
+ attr_reader :x, :y, :h, :d, :max
4
+
5
+ def initialize(x, y, h, polygon)
6
+ @x = x
7
+ @y = y
8
+ @h = h
9
+ @d = point_to_polygon_dist(x, y, polygon)
10
+ @max = @d + @h * Math.sqrt(2)
11
+ end
12
+
13
+ private
14
+
15
+ def point_to_polygon_dist(x, y, polygon)
16
+ inside = false
17
+ min_dist_sq = Float::INFINITY
18
+
19
+ polygon.each do |ring|
20
+ b = ring[-1]
21
+ ring.each do |a|
22
+ if (a[1] > y) != (b[1] > y) && (x < (b[0] - a[0]) * (y - a[1]) / (b[1] - a[1]) + a[0])
23
+ inside = !inside
24
+ end
25
+ min_dist_sq = [min_dist_sq, get_seg_dist_sq(x, y, a, b)].min
26
+ b = a
27
+ end
28
+ end
29
+
30
+ if min_dist_sq.zero?
31
+ 0
32
+ else
33
+ result = Math.sqrt(min_dist_sq)
34
+ if inside
35
+ result
36
+ else
37
+ result * -1
38
+ end
39
+ end
40
+ end
41
+
42
+ def get_seg_dist_sq(px, py, a, b)
43
+ x = a[0]
44
+ y = a[1]
45
+ dx = b[0] - x
46
+ dy = b[1] - y
47
+
48
+ if dx != 0 || dy != 0
49
+ t = ((px - x) * dx + (py - y) * dy) / (dx * dx + dy * dy)
50
+
51
+ if t > 1
52
+ x = b[0]
53
+ y = b[1]
54
+ elsif t.positive?
55
+ x += dx * t
56
+ y += dy * t
57
+ end
58
+ end
59
+
60
+ dx = px - x
61
+ dy = py - y
62
+
63
+ dx * dx + dy * dy
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Polylabel
4
+ VERSION = "0.1.0"
5
+ end
data/lib/polylabel.rb ADDED
@@ -0,0 +1,102 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "polylabel/version"
4
+ require_relative "polylabel/cell"
5
+ require "pqueue"
6
+
7
+ module Polylabel
8
+ def self.compute(polygon, precision = 1.0)
9
+ # find the bounding box of the outer ring
10
+ first_item = polygon[0][0]
11
+
12
+ min_x = first_item[0]
13
+ min_y = first_item[1]
14
+ max_x = first_item[0]
15
+ max_y = first_item[1]
16
+
17
+ polygon[0].each do |p|
18
+ min_x = p[0] if p[0] < min_x
19
+ min_y = p[1] if p[1] < min_y
20
+ max_x = p[0] if p[0] > max_x
21
+ max_y = p[1] if p[1] > max_y
22
+ end
23
+
24
+ width = max_x - min_x
25
+ height = max_y - min_y
26
+ cell_size = [width, height].min
27
+ h = cell_size / 2.0
28
+
29
+ return { x: min_x, y: min_y, distance: 0 } if cell_size.zero?
30
+
31
+ # a priority queue of cells in order of their "potential" (max distance to polygon)
32
+ cell_queue = PQueue.new { |a, b| a.max > b.max }
33
+
34
+ # cover polygon with initial cells
35
+ x = min_x
36
+ while x < max_x
37
+ y = min_y
38
+ while y < max_y
39
+ cell_queue.push Cell.new(x + h, y + h, h, polygon)
40
+ y += cell_size
41
+ end
42
+ x += cell_size
43
+ end
44
+
45
+ # take centroid as the first best guess
46
+ best_cell = get_centroid_cell(polygon)
47
+
48
+ # second guess: bounding box centroid
49
+ bbox_cell = Cell.new(min_x + width / 2, min_y + height / 2, 0, polygon)
50
+
51
+ best_cell = bbox_cell if bbox_cell.d > best_cell.d
52
+
53
+ num_probes = cell_queue.length
54
+
55
+ until cell_queue.empty?
56
+ # pick the most promising cell from the queue
57
+ cell = cell_queue.pop
58
+
59
+ # update the best cell if we found a better one
60
+ best_cell = cell if cell.d > best_cell.d
61
+
62
+ # do not drill down further if there's no chance of a better solution
63
+ next if cell.max - best_cell.d <= precision
64
+
65
+ # split the cell into four cells
66
+ h = cell.h / 2
67
+ cell_queue.push(Cell.new(cell.x - h, cell.y - h, h, polygon))
68
+ cell_queue.push(Cell.new(cell.x + h, cell.y - h, h, polygon))
69
+ cell_queue.push(Cell.new(cell.x - h, cell.y + h, h, polygon))
70
+ cell_queue.push(Cell.new(cell.x + h, cell.y + h, h, polygon))
71
+ num_probes += 4
72
+ end
73
+
74
+ {
75
+ x: best_cell.x,
76
+ y: best_cell.y,
77
+ distance: best_cell.d
78
+ }
79
+ end
80
+
81
+ def self.get_centroid_cell(polygon)
82
+ area = 0
83
+ x = 0
84
+ y = 0
85
+ points = polygon[0]
86
+
87
+ b = points[-1]
88
+ points.each do |a|
89
+ f = a[0] * b[1] - b[0] * a[1]
90
+ x += (a[0] + b[0]) * f
91
+ y += (a[1] + b[1]) * f
92
+ area += f * 3
93
+ b = a
94
+ end
95
+
96
+ if area.zero?
97
+ Cell.new(points[0][0], points[0][1], 0, polygon)
98
+ else
99
+ Cell.new(x / area, y / area, 0, polygon)
100
+ end
101
+ end
102
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-polylabel
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Frédéric Planté
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-01-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pqueue
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.1.0
27
+ description:
28
+ email:
29
+ - frederic.jean.plante@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - CHANGELOG.md
35
+ - Gemfile
36
+ - Gemfile.lock
37
+ - LICENSE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - bin/console
41
+ - bin/setup
42
+ - lib/polylabel.rb
43
+ - lib/polylabel/cell.rb
44
+ - lib/polylabel/version.rb
45
+ homepage: https://github.com/fredplante/ruby-polylabel
46
+ licenses:
47
+ - MIT
48
+ metadata:
49
+ homepage_uri: https://github.com/fredplante/ruby-polylabel
50
+ source_code_uri: https://github.com/fredplante/ruby-polylabel
51
+ changelog_uri: https://github.com/fredplante/ruby-polylabel/blob/master/CHANGELOG
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 2.6.0
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubygems_version: 3.2.32
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: A ruby port of Mapbox's polylabel algorithm
71
+ test_files: []