delaunator 0.1.0
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 +7 -0
- data/.gitignore +13 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +6 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/delaunator.gemspec +32 -0
- data/fixtures/ukraine.yml +1 -0
- data/lib/delaunator.rb +54 -0
- data/lib/delaunator/triangulator.rb +431 -0
- data/lib/delaunator/version.rb +3 -0
- metadata +114 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ba1fa13db2ec7a62c745e00d5778eb22ba26c07ae2b4d7766ec7d35a0a93da73
|
4
|
+
data.tar.gz: 32c5d8eaada41b5b2b5d51848227def3b2e4fba70523a5b98f7e8225fb712ddd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5df303e21a1b177a2365b00c4e6e004a4d647a4157c4fa7ed4487f5d1dae671e07b2aa3b14ca1b9d6fd2560f57fe2bfe71aedd7bb8e97b11232d5c15e4010e34
|
7
|
+
data.tar.gz: 51ab346370f54f2362d227cce4e516b96d195e5ef794b1798242c8ffb9c817cc34452f762350106a863eeab0c1f9b95d93f6bc497958f884b188bf3fb9bf944a
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Wolfgang Wohanka
|
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
|
+
# Delaunay Triangulation
|
2
|
+
|
3
|
+
This is a port of [Mapbox's Delaunator project](https://github.com/mapbox/delaunator).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'delaunator'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install delaunator
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
Delaunator.triangulate([[516, 661], [369, 793], [426, 539]...])
|
25
|
+
```
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` 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 tags, 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/hendrixfan/delaunator-ruby.
|
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
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "delaunator"
|
5
|
+
require 'yaml'
|
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
data/delaunator.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
lib = File.expand_path("lib", __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "delaunator"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "delaunator"
|
7
|
+
spec.version = Delaunator::VERSION
|
8
|
+
spec.authors = ["Wolfgang Wohanka"]
|
9
|
+
spec.email = ["wolfgang.wohanka@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = "Delaunay triangulation of 2D points"
|
12
|
+
spec.description = "Delaunay triangulation of 2D points"
|
13
|
+
spec.homepage = 'https://github.com/hendrixfan/delaunator-ruby'
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
17
|
+
spec.metadata["source_code_uri"] = 'https://github.com/hendrixfan/delaunator-ruby'
|
18
|
+
|
19
|
+
# Specify which files should be added to the gem when it is released.
|
20
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
21
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
22
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
23
|
+
end
|
24
|
+
spec.bindir = "exe"
|
25
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
26
|
+
spec.require_paths = ["lib"]
|
27
|
+
|
28
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
29
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
30
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
31
|
+
spec.add_development_dependency "pry", "~> 0.12.2"
|
32
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
[[168, 180], [168, 178], [168, 179], [168, 181], [168, 183], [167, 183], [167, 184], [165, 184], [162, 186], [164, 188], [161, 188], [160, 191], [158, 193], [156, 193], [152, 195], [152, 198], [150, 198], [147, 198], [148, 205], [150, 210], [148, 210], [148, 208], [145, 206], [142, 206], [140, 206], [138, 206], [135, 206], [135, 209], [131, 209], [131, 211], [127, 211], [124, 210], [120, 207], [120, 204], [120, 202], [124, 201], [123, 201], [125, 198], [125, 194], [127, 194], [127, 191], [130, 191], [132, 189], [134, 189], [134, 186], [136, 184], [134, 182], [134, 179], [134, 176], [136, 174], [139, 174], [141, 177], [142, 176], [144, 176], [147, 178], [148, 176], [151, 178], [154, 178], [153, 175], [152, 174], [152, 170], [152, 168], [150, 166], [148, 166], [147, 165], [145, 162], [146, 160], [146, 157], [146, 155], [144, 155], [142, 152], [140, 150], [138, 150], [138, 148], [140, 145], [140, 142], [140, 138], [139, 138], [137, 138], [135, 138], [133, 135], [132, 132], [129, 132], [128, 132], [124, 132], [124, 130], [123, 130], [118, 126], [116, 124], [112, 122], [109, 122], [105, 122], [102, 124], [100, 124], [97, 124], [95, 126], [92, 127], [89, 127], [88, 130], [85, 132], [80, 134], [72, 134], [69, 134], [65, 138], [64, 138], [58, 137], [56, 133], [52, 133], [51, 133], [48, 133], [44, 133], [41, 131], [38, 130], [35, 130], [32, 127], [30, 127], [27, 127], [24, 127], [24, 126], [23, 124], [20, 122], [17, 122], [16, 118], [15, 116], [15, 110], [18, 108], [20, 102], [24, 97], [28, 102], [28, 98], [26, 97], [28, 94], [27, 85], [29, 79], [32, 76], [39, 70], [44, 66], [48, 65], [53, 61], [53, 58], [51, 54], [54, 54], [52, 48], [51, 43], [48, 42], [49, 38], [48, 34], [51, 30], [53, 33], [58, 30], [61, 30], [60, 27], [64, 26], [68, 24], [74, 24], [80, 24], [85, 26], [92, 26], [96, 29], [103, 32], [109, 33], [112, 37], [116, 37], [120, 37], [124, 35], [126, 35], [128, 38], [132, 38], [134, 41], [138, 38], [140, 36], [142, 40], [144, 43], [145, 41], [149, 41], [155, 41], [159, 41], [161, 46], [165, 46], [164, 42], [164, 39], [164, 34], [167, 30], [173, 24], [178, 24], [184, 24], [189, 26], [195, 21], [195, 20], [199, 20], [203, 20], [207, 17], [211, 17], [216, 17], [218, 16], [222, 22], [225, 27], [228, 31], [226, 34], [224, 34], [226, 39], [228, 43], [230, 46], [236, 46], [242, 46], [243, 50], [245, 50], [247, 54], [247, 56], [248, 60], [248, 65], [253, 66], [255, 64], [260, 64], [264, 67], [268, 71], [272, 66], [275, 66], [281, 61], [285, 66], [286, 70], [292, 74], [294, 74], [296, 74], [296, 71], [301, 74], [307, 74], [311, 78], [315, 74], [315, 77], [319, 77], [322, 82], [328, 82], [331, 81], [331, 84], [333, 86], [333, 90], [330, 95], [326, 98], [328, 99], [332, 98], [333, 101], [331, 104], [329, 104], [327, 106], [329, 111], [332, 116], [333, 119], [333, 122], [332, 126], [332, 130], [327, 130], [321, 130], [317, 130], [315, 134], [312, 134], [308, 138], [306, 138], [306, 144], [306, 149], [306, 152], [301, 152], [297, 154], [295, 154], [292, 154], [292, 158], [288, 158], [283, 162], [281, 164], [279, 163], [276, 163], [273, 166], [272, 169], [268, 168], [265, 170], [260, 172], [256, 176], [252, 176], [248, 181], [246, 182], [246, 189], [246, 194], [248, 197], [250, 198], [252, 200], [252, 203], [254, 205], [260, 205], [264, 202], [267, 202], [269, 202], [272, 199], [280, 199], [278, 202], [278, 207], [278, 211], [276, 211], [272, 213], [268, 213], [265, 213], [264, 211], [262, 210], [260, 210], [257, 212], [257, 214], [255, 217], [253, 217], [253, 221], [249, 220], [247, 220], [243, 222], [240, 223], [239, 226], [234, 231], [229, 231], [224, 231], [219, 227], [220, 227], [222, 224], [222, 222], [222, 219], [224, 217], [222, 214], [220, 212], [217, 210], [215, 210], [211, 209], [208, 206], [202, 209], [202, 205], [206, 202], [211, 198], [216, 195], [220, 192], [224, 192], [221, 186], [218, 186], [214, 185], [208, 185], [204, 186], [200, 186], [193, 183], [190, 182], [188, 182], [190, 178], [186, 178], [184, 174], [182, 171], [178, 171], [173, 174], [169, 174], [169, 175], [169, 179], [167, 182], [164, 186], [160, 192], [155, 195], [152, 198], [150, 198], [148, 198], [148, 202], [151, 208], [148, 210], [146, 208], [144, 205], [140, 205], [137, 208], [132, 208], [132, 210], [127, 210], [124, 210], [120, 206], [120, 202], [123, 202], [124, 201], [124, 198], [128, 195], [131, 191], [133, 187], [135, 183], [130, 203], [129, 208], [123, 203], [129, 203], [129, 198], [133, 198], [136, 200], [142, 200], [143, 199], [143, 197], [137, 196], [136, 194], [133, 194], [136, 186], [136, 182], [141, 186], [144, 186], [150, 186], [150, 190], [155, 190], [159, 188], [156, 182], [151, 182], [144, 182], [164, 176], [161, 177], [157, 177], [166, 176], [168, 165], [175, 167], [180, 167], [188, 159], [195, 164], [195, 162], [187, 162], [178, 163], [173, 166], [168, 170], [156, 170], [157, 165], [164, 165], [164, 161], [170, 159], [167, 158], [159, 154], [149, 151], [145, 145], [145, 138], [152, 138], [152, 146], [159, 146], [165, 153], [176, 153], [180, 153], [187, 153], [194, 153], [202, 153], [202, 158], [197, 158], [193, 158], [193, 142], [180, 142], [171, 142], [163, 135], [176, 135], [186, 139], [201, 139], [206, 139], [205, 147], [205, 160], [198, 160], [206, 174], [205, 178], [196, 178], [196, 182], [202, 182], [206, 181], [209, 181], [215, 181], [222, 181], [230, 177], [238, 175], [241, 175], [237, 175], [237, 168], [237, 161], [232, 156], [231, 162], [225, 166], [217, 169], [210, 173], [224, 173], [227, 173], [235, 175], [237, 178], [228, 192], [222, 199], [216, 199], [211, 204], [205, 206], [219, 207], [222, 211], [229, 214], [236, 214], [244, 211], [247, 211], [268, 206], [277, 201], [279, 201], [281, 202], [278, 202], [242, 178], [236, 170], [236, 162], [255, 162], [251, 156], [240, 156], [253, 152], [261, 152], [277, 157], [268, 151], [255, 143], [260, 142], [267, 145], [271, 149], [273, 154], [258, 146], [257, 131], [256, 134], [248, 137], [260, 137], [260, 134], [271, 137], [276, 138], [276, 144], [289, 144], [285, 150], [294, 150], [298, 149], [301, 145], [292, 145], [282, 134], [276, 134], [283, 127], [282, 116], [277, 113], [283, 113], [288, 106], [296, 106], [297, 113], [297, 118], [298, 118], [310, 122], [310, 128], [300, 130], [300, 140], [292, 129], [292, 114], [283, 122], [289, 122], [299, 122], [299, 134], [294, 134], [288, 124], [314, 121], [311, 113], [308, 110], [304, 96], [299, 90], [299, 82], [305, 87], [309, 94], [311, 101], [312, 102], [314, 107], [320, 112], [320, 115], [326, 116], [323, 109], [321, 102], [321, 94], [321, 90], [328, 90], [328, 88], [316, 88], [316, 84], [307, 84], [290, 77], [289, 88], [289, 97], [278, 97], [268, 106], [268, 110], [261, 105], [255, 103], [244, 103], [252, 100], [252, 91], [252, 82], [242, 78], [252, 78], [259, 78], [264, 87], [267, 92], [272, 91], [272, 83], [264, 83], [260, 79], [276, 79], [283, 84], [283, 94], [289, 94], [284, 86], [272, 77], [253, 110], [248, 110], [239, 110], [234, 114], [222, 125], [219, 127], [219, 131], [219, 138], [219, 141], [224, 139], [224, 135], [225, 130], [232, 136], [240, 138], [237, 131], [237, 118], [248, 120], [256, 122], [262, 127], [255, 118], [245, 110], [207, 129], [199, 134], [195, 134], [188, 130], [180, 130], [165, 129], [156, 129], [165, 128], [173, 125], [185, 126], [193, 126], [201, 124], [204, 123], [208, 116], [214, 114], [207, 114], [196, 114], [183, 121], [183, 111], [189, 117], [196, 112], [172, 126], [164, 126], [159, 114], [174, 106], [186, 106], [192, 105], [184, 105], [184, 96], [173, 96], [163, 111], [159, 110], [152, 110], [168, 110], [171, 106], [183, 98], [193, 101], [219, 96], [225, 97], [225, 104], [232, 92], [240, 92], [237, 86], [229, 86], [216, 88], [214, 79], [203, 79], [203, 75], [212, 75], [221, 75], [229, 80], [230, 89], [217, 88], [217, 77], [228, 77], [228, 69], [235, 71], [240, 71], [244, 66], [236, 54], [236, 62], [232, 68], [229, 61], [216, 61], [212, 58], [212, 47], [212, 39], [214, 28], [215, 48], [225, 55], [236, 55], [202, 65], [202, 54], [202, 44], [202, 24], [198, 32], [199, 38], [192, 38], [185, 38], [174, 42], [174, 48], [178, 51], [184, 51], [194, 55], [191, 68], [182, 68], [174, 69], [167, 67], [153, 59], [153, 49], [147, 49], [152, 58], [152, 74], [154, 83], [161, 83], [165, 88], [153, 97], [153, 89], [152, 82], [168, 88], [168, 101], [156, 102], [156, 119], [173, 110], [184, 110], [177, 106], [160, 106], [145, 125], [137, 122], [131, 120], [124, 120], [122, 118], [113, 118], [114, 111], [129, 111], [140, 110], [143, 106], [137, 102], [127, 102], [119, 98], [126, 93], [139, 93], [139, 99], [141, 95], [128, 89], [118, 74], [128, 76], [135, 76], [141, 83], [141, 71], [137, 61], [137, 50], [129, 50], [118, 50], [109, 52], [112, 61], [123, 60], [134, 60], [129, 76], [121, 67], [124, 76], [123, 76], [111, 74], [128, 73], [109, 83], [109, 94], [105, 103], [102, 118], [92, 113], [98, 105], [99, 93], [94, 93], [94, 81], [99, 81], [100, 73], [100, 89], [100, 60], [100, 55], [105, 37], [101, 34], [93, 37], [90, 37], [90, 49], [99, 49], [88, 68], [80, 68], [78, 64], [88, 62], [86, 77], [76, 89], [71, 91], [71, 106], [78, 106], [82, 118], [84, 110], [71, 104], [76, 103], [76, 91], [78, 83], [85, 89], [83, 103], [83, 119], [76, 130], [62, 130], [68, 127], [74, 126], [83, 123], [62, 123], [56, 123], [59, 129], [59, 120], [49, 110], [46, 106], [56, 100], [62, 94], [62, 109], [72, 112], [67, 112], [57, 112], [61, 122], [60, 102], [52, 125], [44, 121], [36, 114], [32, 110], [20, 110], [22, 118], [35, 118], [44, 124], [32, 119], [22, 111], [44, 96], [36, 106], [36, 94], [32, 94], [35, 83], [44, 91], [52, 91], [52, 80], [59, 80], [62, 76], [62, 70], [47, 78], [55, 75], [64, 71], [64, 60], [58, 53], [58, 43], [65, 43], [65, 60], [76, 52], [73, 38], [76, 36], [93, 48], [89, 39], [99, 40], [98, 50], [94, 63], [117, 63], [131, 67], [131, 74], [142, 78], [140, 61], [124, 58], [124, 48], [136, 55], [236, 200], [228, 200], [226, 192], [232, 198], [238, 210], [248, 210], [236, 220], [230, 223], [230, 213], [175, 32], [172, 32], [171, 38], [184, 30]]
|
data/lib/delaunator.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'delaunator/version'
|
2
|
+
require 'delaunator/triangulator'
|
3
|
+
|
4
|
+
module Delaunator
|
5
|
+
def self.triangulate(points)
|
6
|
+
coords = points.flatten
|
7
|
+
Delaunator::Triangulator.new(coords).triangulate
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.validate(points)
|
11
|
+
coords = points.flatten
|
12
|
+
d = Delaunator::Triangulator.new(coords)
|
13
|
+
d.triangulate
|
14
|
+
(0..d.halfedges.length - 1).each do |i|
|
15
|
+
i2 = d.halfedges[i]
|
16
|
+
#if i2 != -1 && d.halfedges[i2] != i
|
17
|
+
raise ArgumentError, "invalid_halfedge #{i}" if i2 != -1 && d.halfedges[i2] != i
|
18
|
+
end
|
19
|
+
# validate triangulation
|
20
|
+
hull_areas = []
|
21
|
+
len = d.hull.length
|
22
|
+
j = len - 1
|
23
|
+
(0..j).each do |i|
|
24
|
+
start_point = points[d.hull[j]]
|
25
|
+
end_point = points[d.hull[i]]
|
26
|
+
hull_areas << ((end_point.first - start_point.first) * (end_point.last + start_point.last))
|
27
|
+
c = convex(points[d.hull[j]], points[d.hull[(j + 1) % d.hull.length]], points[d.hull[(j + 3) % d.hull.length]])
|
28
|
+
j = i - 1
|
29
|
+
raise ArgumentError, :not_convex unless c
|
30
|
+
end
|
31
|
+
hull_area = hull_areas.inject(0){ |sum, x| sum + x }
|
32
|
+
|
33
|
+
triangle_areas = []
|
34
|
+
(0..d.triangles.length-1).step(10) do |i|
|
35
|
+
ax, ay = points[d.triangles[i]]
|
36
|
+
bx, by = points[d.triangles[i + 1]]
|
37
|
+
cx, cy = points[d.triangles[i + 2]]
|
38
|
+
triangle_areas << ((by - ay) * (cx - bx) - (bx - ax) * (cy - by)).abs
|
39
|
+
end
|
40
|
+
triangles_area = triangle_areas.inject(0){ |sum, x| sum + x }
|
41
|
+
err = ((hull_area - triangles_area) / hull_area).abs
|
42
|
+
raise ArgumentError, :invalid_triangulation unless err <= 2.pow(-51)
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.convex(r, q, p)
|
46
|
+
(orient(p, r, q) || orient(r, q, p) || orient(q, p, r)) >= 0
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.orient((px, py), (rx, ry), (qx, qy))
|
50
|
+
l = (ry - py) * (qx - px)
|
51
|
+
r = (rx - px) * (qy - py)
|
52
|
+
((l - r).abs >= 3.3306690738754716e-16 * (l + r).abs) ? l - r : 0;
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,431 @@
|
|
1
|
+
module Delaunator
|
2
|
+
class Triangulator
|
3
|
+
attr_reader :halfedges, :hull, :triangles, :coords
|
4
|
+
|
5
|
+
EPSILON = 2.pow(-52)
|
6
|
+
|
7
|
+
EDGE_STACK = Array.new(512, 0)
|
8
|
+
|
9
|
+
def initialize(coords)
|
10
|
+
@coords = coords
|
11
|
+
@n = @coords.length >> 1
|
12
|
+
@max_triangles = [2 * @n - 5, 0].max
|
13
|
+
@triangles_len = 0
|
14
|
+
@triangles = Array.new(@max_triangles * 3, 0)
|
15
|
+
@halfedges = Array.new(@max_triangles * 3, 0)
|
16
|
+
|
17
|
+
@hash_size = Math.sqrt(@n).ceil
|
18
|
+
@hull_prev = Array.new(@n, 0)
|
19
|
+
@hull_next = Array.new(@n, 0)
|
20
|
+
@hull_tri = Array.new(@n, 0)
|
21
|
+
@hull_hash = Array.new(@hash_size, -1)
|
22
|
+
end
|
23
|
+
|
24
|
+
def triangulate
|
25
|
+
dists = Array.new(@n, 0.0)
|
26
|
+
ids = Array.new(@n, 0)
|
27
|
+
min_x = Float::INFINITY
|
28
|
+
min_y = Float::INFINITY
|
29
|
+
max_x = -Float::INFINITY
|
30
|
+
max_y = -Float::INFINITY
|
31
|
+
# compute bounds
|
32
|
+
(0..@n - 1).each do |i|
|
33
|
+
x = @coords[2 * i]
|
34
|
+
y = @coords[2 * i + 1]
|
35
|
+
min_x = x if x < min_x
|
36
|
+
min_y = y if y < min_y
|
37
|
+
max_x = x if x > max_x
|
38
|
+
max_y = y if y > max_y
|
39
|
+
end
|
40
|
+
|
41
|
+
cx = (min_x + max_x).to_f / 2
|
42
|
+
cy = (min_y + max_y).to_f / 2
|
43
|
+
min_dist = Float::INFINITY
|
44
|
+
i0 = 0
|
45
|
+
i1 = 0
|
46
|
+
i2 = 0
|
47
|
+
|
48
|
+
# pick a seed point close to midpoint i0
|
49
|
+
(0..@n - 1).each do |i|
|
50
|
+
d = dist(cx, cy, @coords[2 * i], @coords[2 * i + 1])
|
51
|
+
if d < min_dist
|
52
|
+
i0 = i
|
53
|
+
min_dist = d
|
54
|
+
end
|
55
|
+
end
|
56
|
+
i0x = @coords[2 * i0]
|
57
|
+
i0y = @coords[2 * i0 + 1]
|
58
|
+
|
59
|
+
min_dist = Float::INFINITY
|
60
|
+
|
61
|
+
(0..@n - 1).each do |i|
|
62
|
+
next if i == i0
|
63
|
+
|
64
|
+
d = dist(i0x, i0y, @coords[2 * i], @coords[2 * i + 1])
|
65
|
+
if (d < min_dist) && (d > 0)
|
66
|
+
i1 = i
|
67
|
+
min_dist = d
|
68
|
+
end
|
69
|
+
end
|
70
|
+
i1x = @coords[2 * i1]
|
71
|
+
i1y = @coords[2 * i1 + 1]
|
72
|
+
|
73
|
+
min_radius = Float::INFINITY
|
74
|
+
|
75
|
+
# find the third point which forms the smallest circumcircle with the first two
|
76
|
+
|
77
|
+
(0..@n - 1).each do |i|
|
78
|
+
next if (i == i0) || (i == i1)
|
79
|
+
|
80
|
+
r = circumradius(i0x, i0y, i1x, i1y, @coords[2 * i], @coords[2 * i + 1])
|
81
|
+
if r < min_radius
|
82
|
+
i2 = i
|
83
|
+
min_radius = r
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
i2x = @coords[2 * i2]
|
88
|
+
i2y = @coords[2 * i2 + 1]
|
89
|
+
if min_radius == Float::INFINITY
|
90
|
+
# order collinear points by dx (or dy if all x are identical)
|
91
|
+
# and return the list as a hull
|
92
|
+
(0..n - 1).each do |i|
|
93
|
+
dists[i] = (@coords[2 * i] - @coords[0]) || (@coords[2 * i + 1] - @coords[1])
|
94
|
+
end
|
95
|
+
ids = dists.map.with_index.sort.map(&:last)
|
96
|
+
@hull = Array.new(@n, 0)
|
97
|
+
j = 0
|
98
|
+
d0 = -Float::INFINITY
|
99
|
+
(0..@n - 1).each do |i|
|
100
|
+
id = ids[i]
|
101
|
+
next unless dists[id] > d0
|
102
|
+
|
103
|
+
@hull[j] = id
|
104
|
+
j += 1
|
105
|
+
d0 = dists[id]
|
106
|
+
end
|
107
|
+
@hull = hull.slice(0, j)
|
108
|
+
@triangles = []
|
109
|
+
@halfedges = []
|
110
|
+
return
|
111
|
+
end
|
112
|
+
|
113
|
+
# swap the order of the seed points for counter-clockwise orientation
|
114
|
+
if orient(i0x, i0y, i1x, i1y, i2x, i2y)
|
115
|
+
i = i1
|
116
|
+
x = i1x
|
117
|
+
y = i1y
|
118
|
+
i1 = i2
|
119
|
+
i1x = i2x
|
120
|
+
i1y = i2y
|
121
|
+
i2 = i
|
122
|
+
i2x = x
|
123
|
+
i2y = y
|
124
|
+
end
|
125
|
+
center = circumcenter(i0x, i0y, i1x, i1y, i2x, i2y)
|
126
|
+
@cx, @cy = center
|
127
|
+
|
128
|
+
(0..@n - 1).each do |k|
|
129
|
+
dists[k] = dist(@coords[2 * k], @coords[2 * k + 1], center[0], center[1])
|
130
|
+
end
|
131
|
+
# sort the points by distance from the seed triangle circumcenter
|
132
|
+
ids = dists.map.with_index.sort.map(&:last)
|
133
|
+
|
134
|
+
@hull_start = i0
|
135
|
+
@hull_size = 3
|
136
|
+
|
137
|
+
@hull_next[i0] = @hull_prev[i2] = i1
|
138
|
+
@hull_next[i1] = @hull_prev[i0] = i2
|
139
|
+
@hull_next[i2] = @hull_prev[i1] = i0
|
140
|
+
|
141
|
+
@hull_tri[i0] = 0
|
142
|
+
@hull_tri[i1] = 1
|
143
|
+
@hull_tri[i2] = 2
|
144
|
+
|
145
|
+
@hull_hash.fill(-1)
|
146
|
+
@hull_hash[hash_key(i0x, i0y)] = i0
|
147
|
+
@hull_hash[hash_key(i1x, i1y)] = i1
|
148
|
+
@hull_hash[hash_key(i2x, i2y)] = i2
|
149
|
+
|
150
|
+
@triangles_len = 0
|
151
|
+
add_triangle(i0, i1, i2, -1, -1, -1)
|
152
|
+
xp = 0
|
153
|
+
yp = 0
|
154
|
+
(0..ids.length - 1).each do |k|
|
155
|
+
i = ids[k]
|
156
|
+
x = @coords[2 * i]
|
157
|
+
y = @coords[2 * i + 1]
|
158
|
+
if (k > 0 && (x - xp).abs <= EPSILON && (y - yp).abs <= EPSILON)
|
159
|
+
next
|
160
|
+
end
|
161
|
+
xp = x
|
162
|
+
yp = y
|
163
|
+
#skip seed triangle points
|
164
|
+
if i == i0 || i == i1 || i == i2
|
165
|
+
next
|
166
|
+
end
|
167
|
+
# find a visible edge on the convex hull using edge hash
|
168
|
+
|
169
|
+
start = 0
|
170
|
+
|
171
|
+
key = hash_key(x, y)
|
172
|
+
(0..@hash_size - 1).each do |m|
|
173
|
+
start = @hull_hash[(key + m) % @hash_size]
|
174
|
+
break if (start != -1) && (start != @hull_next[start])
|
175
|
+
end
|
176
|
+
start = @hull_prev[start]
|
177
|
+
e = start
|
178
|
+
q = nil
|
179
|
+
loop do
|
180
|
+
q = @hull_next[e]
|
181
|
+
break if orient(x, y, @coords[2 * e], @coords[2 * e + 1], @coords[2 * q], @coords[2 * q + 1])
|
182
|
+
e = q
|
183
|
+
if e == start
|
184
|
+
e = -1
|
185
|
+
break
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
if e == -1
|
190
|
+
next
|
191
|
+
end
|
192
|
+
t = add_triangle(e, i, @hull_next[e], -1, -1, @hull_tri[e])
|
193
|
+
@hull_tri[i] = legalize(t + 2)
|
194
|
+
@hull_tri[e] = t
|
195
|
+
@hull_size += 1
|
196
|
+
n = @hull_next[e]
|
197
|
+
loop do
|
198
|
+
q = @hull_next[n]
|
199
|
+
break if !orient(x, y, @coords[2 * n], @coords[2 * n + 1], @coords[2 * q], @coords[2 * q + 1])
|
200
|
+
t = add_triangle(n, i, q, @hull_tri[i], -1, @hull_tri[n])
|
201
|
+
@hull_tri[i] = legalize(t + 2)
|
202
|
+
@hull_next[n] = n
|
203
|
+
@hull_size -= 1
|
204
|
+
n = q
|
205
|
+
end
|
206
|
+
|
207
|
+
if e == start
|
208
|
+
loop do
|
209
|
+
q = @hull_prev[e]
|
210
|
+
break if !orient(x, y, @coords[2 * q], @coords[2 * q + 1], @coords[2 * e], @coords[2 * e + 1])
|
211
|
+
|
212
|
+
t = add_triangle(q, i, e, -1, @hull_tri[e], @hull_tri[q])
|
213
|
+
|
214
|
+
legalize(t + 2)
|
215
|
+
@hull_tri[q] = t
|
216
|
+
@hull_next[e] = e
|
217
|
+
@hull_size -= 1
|
218
|
+
e = q
|
219
|
+
end
|
220
|
+
end
|
221
|
+
@hull_start = e
|
222
|
+
@hull_prev[i] = e
|
223
|
+
@hull_next[e] = i
|
224
|
+
@hull_prev[n] = i
|
225
|
+
@hull_next[i] = n
|
226
|
+
|
227
|
+
# save the two new edges in the hash table
|
228
|
+
# this._hashKey(x, y) = 0 FEHLER
|
229
|
+
@hull_hash[hash_key(x, y)] = i
|
230
|
+
@hull_hash[hash_key(@coords[2 * e], @coords[2 * e + 1])] = e
|
231
|
+
end
|
232
|
+
@hull = Array.new(@hull_size, 0)
|
233
|
+
e = @hull_start
|
234
|
+
(0..@hull_size - 1).each do |k|
|
235
|
+
@hull[k] = e
|
236
|
+
e = @hull_next[e]
|
237
|
+
end
|
238
|
+
|
239
|
+
# trim typed triangle mesh arrays
|
240
|
+
@triangles = @triangles.slice(0, @triangles_len)
|
241
|
+
@halfedges = @halfedges.slice(0, @triangles_len)
|
242
|
+
end
|
243
|
+
|
244
|
+
def dist(ax, ay, bx, by)
|
245
|
+
dx = ax - bx
|
246
|
+
dy = ay - by
|
247
|
+
dx * dx + dy * dy
|
248
|
+
end
|
249
|
+
|
250
|
+
# return 2d orientation sign if we're confident in it through J. Shewchuk's error bound check
|
251
|
+
def orient_if_sure(px, py, rx, ry, qx, qy)
|
252
|
+
l = (ry - py) * (qx - px)
|
253
|
+
r = (rx - px) * (qy - py)
|
254
|
+
((l - r).abs >= (3.3306690738754716e-16 * (l + r).abs)) ? l - r : 0
|
255
|
+
end
|
256
|
+
|
257
|
+
# a more robust orientation test that's stable in a given triangle (to fix robustness issues)
|
258
|
+
def orient(rx, ry, qx, qy, px, py)
|
259
|
+
(orient_if_sure(px, py, rx, ry, qx, qy) || orient_if_sure(rx, ry, qx, qy, px, py) || orient_if_sure(qx, qy, px, py, rx, ry)) < 0
|
260
|
+
end
|
261
|
+
|
262
|
+
def circumcenter(ax, ay, bx, by, cx, cy)
|
263
|
+
dx = bx - ax
|
264
|
+
dy = by - ay
|
265
|
+
ex = cx - ax
|
266
|
+
ey = cy - ay
|
267
|
+
|
268
|
+
bl = dx * dx + dy * dy
|
269
|
+
cl = ex * ex + ey * ey
|
270
|
+
d = 0.5 / ( dx * ey - dy * ex).to_f
|
271
|
+
|
272
|
+
x = ax + (ey * bl - dy * cl) * d
|
273
|
+
y = ay + (dx * cl - ex * bl) * d
|
274
|
+
[x, y]
|
275
|
+
end
|
276
|
+
|
277
|
+
def circumradius(ax, ay, bx, by, cx, cy)
|
278
|
+
dx = bx - ax
|
279
|
+
dy = by - ay
|
280
|
+
ex = cx - ax
|
281
|
+
ey = cy - ay
|
282
|
+
|
283
|
+
bl = dx * dx + dy * dy
|
284
|
+
cl = ex * ex + ey * ey
|
285
|
+
d = 0.5 / (dx * ey - dy * ex).to_f
|
286
|
+
|
287
|
+
x = (ey * bl - dy * cl) * d
|
288
|
+
y = (dx * cl - ex * bl) * d
|
289
|
+
|
290
|
+
r = x * x + y * y
|
291
|
+
|
292
|
+
return 0 if [bl, cl, d, r].include? 0
|
293
|
+
|
294
|
+
r
|
295
|
+
end
|
296
|
+
|
297
|
+
def in_circle(ax, ay, bx, by, cx, cy, px, py)
|
298
|
+
dx = ax - px
|
299
|
+
dy = ay - py
|
300
|
+
ex = bx - px
|
301
|
+
ey = by - py
|
302
|
+
fx = cx - px
|
303
|
+
fy = cy - py
|
304
|
+
|
305
|
+
ap = dx * dx + dy * dy
|
306
|
+
bp = ex * ex + ey * ey
|
307
|
+
cp = fx * fx + fy * fy
|
308
|
+
|
309
|
+
(
|
310
|
+
dx * (ey * cp - bp * fy) -
|
311
|
+
dy * (ex * cp - bp * fx) +
|
312
|
+
ap * (ex * fy - ey * fx)
|
313
|
+
) < 0
|
314
|
+
end
|
315
|
+
|
316
|
+
def pseudo_angle(dx, dy)
|
317
|
+
p = dx / (dx.abs + dy.abs).to_f
|
318
|
+
(dy > 0 ? 3 - p : 1 + p) / 4.to_f
|
319
|
+
end
|
320
|
+
|
321
|
+
def add_triangle(i0, i1, i2, a, b, c)
|
322
|
+
i = @triangles_len
|
323
|
+
@triangles[i] = i0
|
324
|
+
@triangles[i + 1] = i1
|
325
|
+
@triangles[i + 2] = i2
|
326
|
+
link(i, a)
|
327
|
+
link(i + 1, b)
|
328
|
+
link(i + 2, c)
|
329
|
+
@triangles_len += 3
|
330
|
+
i
|
331
|
+
end
|
332
|
+
|
333
|
+
def hash_key(x, y)
|
334
|
+
angle = (pseudo_angle(x - @cx, y - @cy) * @hash_size).floor()
|
335
|
+
angle % @hash_size
|
336
|
+
end
|
337
|
+
|
338
|
+
def link(a, b)
|
339
|
+
@halfedges[a] = b
|
340
|
+
@halfedges[b] = a if b != -1
|
341
|
+
end
|
342
|
+
|
343
|
+
def legalize(a)
|
344
|
+
# if the pair of triangles doesn't satisfy the Delaunay condition
|
345
|
+
# (p1 is inside the circumcircle of [p0, pl, pr]), flip them,
|
346
|
+
# then do the same check/flip recursively for the new pair of triangles
|
347
|
+
#
|
348
|
+
# pl pl
|
349
|
+
# /||\ / \
|
350
|
+
# al/ || \bl al/ \a
|
351
|
+
# / || \ / \
|
352
|
+
# / a||b \ flip /___ar___\
|
353
|
+
# p0\ || /p1 => p0\---bl---/p1
|
354
|
+
# \ || / \ /
|
355
|
+
# ar\ || /br b\ /br
|
356
|
+
# \||/ \ /
|
357
|
+
# pr pr
|
358
|
+
i = 0
|
359
|
+
ar = 0
|
360
|
+
loop do
|
361
|
+
b = @halfedges[a]
|
362
|
+
a0 = a - a % 3
|
363
|
+
|
364
|
+
ar = a0 + (a + 2) % 3
|
365
|
+
# convex hull edge
|
366
|
+
if b == -1
|
367
|
+
break if i.zero?
|
368
|
+
i -= 1
|
369
|
+
a = EDGE_STACK[i]
|
370
|
+
next
|
371
|
+
end
|
372
|
+
|
373
|
+
b0 = b - b % 3
|
374
|
+
al = a0 + (a + 1) % 3
|
375
|
+
bl = b0 + (b + 2) % 3
|
376
|
+
|
377
|
+
p0 = @triangles[ar]
|
378
|
+
pr = @triangles[a]
|
379
|
+
pl = @triangles[al]
|
380
|
+
p1 = @triangles[bl]
|
381
|
+
|
382
|
+
illegal = in_circle(@coords[2 * p0], @coords[2 * p0 + 1],
|
383
|
+
@coords[2 * pr], @coords[2 * pr + 1],
|
384
|
+
@coords[2 * pl], @coords[2 * pl + 1],
|
385
|
+
@coords[2 * p1], @coords[2 * p1 + 1])
|
386
|
+
|
387
|
+
if illegal
|
388
|
+
@triangles[a] = p1
|
389
|
+
@triangles[b] = p0
|
390
|
+
hbl = @halfedges[bl]
|
391
|
+
# edge swapped on the other side of the hull (rare)
|
392
|
+
# fix the halfedge reference
|
393
|
+
if hbl == -1
|
394
|
+
e = @hull_start
|
395
|
+
loop do
|
396
|
+
if @hull_tri[e] == bl
|
397
|
+
@hull_tri[e] = a
|
398
|
+
break
|
399
|
+
end
|
400
|
+
e = @hull_prev[e]
|
401
|
+
break if e != @hull_start
|
402
|
+
end
|
403
|
+
end
|
404
|
+
link(a, hbl)
|
405
|
+
link(b, @halfedges[ar])
|
406
|
+
link(ar, bl)
|
407
|
+
|
408
|
+
br = b0 + (b + 1) % 3
|
409
|
+
|
410
|
+
if i < EDGE_STACK.length
|
411
|
+
EDGE_STACK[i] = br
|
412
|
+
i += 1
|
413
|
+
end
|
414
|
+
else
|
415
|
+
break if i.zero?
|
416
|
+
|
417
|
+
i -= 1
|
418
|
+
a = EDGE_STACK[i]
|
419
|
+
end
|
420
|
+
end
|
421
|
+
ar
|
422
|
+
end
|
423
|
+
end
|
424
|
+
end
|
425
|
+
|
426
|
+
class Array
|
427
|
+
def swap!(a, b)
|
428
|
+
self[a], self[b] = self[b], self[a]
|
429
|
+
self
|
430
|
+
end
|
431
|
+
end
|
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: delaunator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Wolfgang Wohanka
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-12-26 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: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
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.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.12.2
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.12.2
|
69
|
+
description: Delaunay triangulation of 2D points
|
70
|
+
email:
|
71
|
+
- wolfgang.wohanka@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- bin/console
|
83
|
+
- bin/setup
|
84
|
+
- delaunator.gemspec
|
85
|
+
- fixtures/ukraine.yml
|
86
|
+
- lib/delaunator.rb
|
87
|
+
- lib/delaunator/triangulator.rb
|
88
|
+
- lib/delaunator/version.rb
|
89
|
+
homepage: https://github.com/hendrixfan/delaunator-ruby
|
90
|
+
licenses:
|
91
|
+
- MIT
|
92
|
+
metadata:
|
93
|
+
homepage_uri: https://github.com/hendrixfan/delaunator-ruby
|
94
|
+
source_code_uri: https://github.com/hendrixfan/delaunator-ruby
|
95
|
+
post_install_message:
|
96
|
+
rdoc_options: []
|
97
|
+
require_paths:
|
98
|
+
- lib
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
requirements: []
|
110
|
+
rubygems_version: 3.0.6
|
111
|
+
signing_key:
|
112
|
+
specification_version: 4
|
113
|
+
summary: Delaunay triangulation of 2D points
|
114
|
+
test_files: []
|