clipper2 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.
Files changed (48) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +21 -0
  3. data/README.md +165 -0
  4. data/examples/larb_transform.rb +17 -0
  5. data/examples/rugl_mesh.rb +33 -0
  6. data/examples/svg_boolean.rb +28 -0
  7. data/ext/clipper2/CMakeLists.txt +27 -0
  8. data/ext/clipper2/clipper2_native.cpp +2 -0
  9. data/ext/clipper2/extconf.rb +27 -0
  10. data/ext/clipper2/generate_bindings.rb +73 -0
  11. data/ext/clipper2/memory_check.cpp +48 -0
  12. data/ext/clipper2/vendor/LICENSE +23 -0
  13. data/ext/clipper2/vendor/UPSTREAM.md +11 -0
  14. data/ext/clipper2/vendor/include/clipper2/clipper.core.h +1159 -0
  15. data/ext/clipper2/vendor/include/clipper2/clipper.engine.h +635 -0
  16. data/ext/clipper2/vendor/include/clipper2/clipper.export.h +851 -0
  17. data/ext/clipper2/vendor/include/clipper2/clipper.h +796 -0
  18. data/ext/clipper2/vendor/include/clipper2/clipper.minkowski.h +117 -0
  19. data/ext/clipper2/vendor/include/clipper2/clipper.offset.h +125 -0
  20. data/ext/clipper2/vendor/include/clipper2/clipper.rectclip.h +80 -0
  21. data/ext/clipper2/vendor/include/clipper2/clipper.triangulation.h +27 -0
  22. data/ext/clipper2/vendor/include/clipper2/clipper.version.h +6 -0
  23. data/ext/clipper2/vendor/src/clipper.engine.cpp +3152 -0
  24. data/ext/clipper2/vendor/src/clipper.offset.cpp +661 -0
  25. data/ext/clipper2/vendor/src/clipper.rectclip.cpp +1027 -0
  26. data/ext/clipper2/vendor/src/clipper.triangulation.cpp +1221 -0
  27. data/lib/clipper2/api.rb +45 -0
  28. data/lib/clipper2/c_paths64.rb +80 -0
  29. data/lib/clipper2/earcut/engine.rb +191 -0
  30. data/lib/clipper2/earcut/geometry.rb +100 -0
  31. data/lib/clipper2/earcut/node.rb +18 -0
  32. data/lib/clipper2/earcut/ring.rb +94 -0
  33. data/lib/clipper2/earcut.rb +18 -0
  34. data/lib/clipper2/errors.rb +19 -0
  35. data/lib/clipper2/generated.rb +62 -0
  36. data/lib/clipper2/native.rb +38 -0
  37. data/lib/clipper2/native_memory.rb +64 -0
  38. data/lib/clipper2/operations.rb +206 -0
  39. data/lib/clipper2/path.rb +171 -0
  40. data/lib/clipper2/path_simplifier.rb +61 -0
  41. data/lib/clipper2/paths.rb +130 -0
  42. data/lib/clipper2/quantizer.rb +48 -0
  43. data/lib/clipper2/triangulation.rb +103 -0
  44. data/lib/clipper2/version.rb +5 -0
  45. data/lib/clipper2.rb +34 -0
  46. data/licenses/CLIPPER2-BSL-1.0.txt +23 -0
  47. data/licenses/EARCUT-ISC.txt +15 -0
  48. metadata +113 -0
@@ -0,0 +1,103 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Clipper2
4
+ class Triangulation
5
+ attr_reader :indices, :vertices, :index_count, :vertex_count
6
+
7
+ def initialize(indices, vertices)
8
+ if indices.any? { |index| !index.between?(0, (2**32) - 1) }
9
+ raise RangeError, "triangulation index exceeds the u32 range"
10
+ end
11
+
12
+ @indices = indices.pack("L<*").freeze
13
+ @vertices = vertices.pack("e*").freeze
14
+ @index_count = indices.length
15
+ @vertex_count = vertices.length / 2
16
+ freeze
17
+ end
18
+
19
+ def to_ary
20
+ [indices, vertices]
21
+ end
22
+ alias to_a to_ary
23
+
24
+ def empty?
25
+ index_count.zero?
26
+ end
27
+ end
28
+
29
+ module Triangulator
30
+ module_function
31
+
32
+ def call(value)
33
+ paths = Paths.coerce(value)
34
+ rings = paths.reject(&:empty?)
35
+ invalid = rings.find { |path| path.size < 3 }
36
+ raise TriangulationError, "every non-empty path must contain at least three vertices" if invalid
37
+ return Triangulation.new([], []) if rings.empty?
38
+
39
+ groups = group_rings(rings)
40
+ all_indices = []
41
+ all_vertices = []
42
+ groups.each do |outer, holes|
43
+ group_rings = [outer, *holes]
44
+ vertices = group_rings.flat_map { |ring| ring.to_a.flatten }
45
+ hole_indices = []
46
+ vertex_cursor = outer.size
47
+ holes.each do |hole|
48
+ hole_indices << vertex_cursor
49
+ vertex_cursor += hole.size
50
+ end
51
+
52
+ indices = Earcut.call(vertices, hole_indices)
53
+ orient_counter_clockwise!(indices, vertices)
54
+ vertex_offset = all_vertices.length / 2
55
+ all_indices.concat(indices.map { |index| index + vertex_offset })
56
+ all_vertices.concat(vertices)
57
+ end
58
+ Triangulation.new(all_indices, all_vertices)
59
+ end
60
+
61
+ def group_rings(rings)
62
+ groups = rings.select { |ring| ring.orientation == :ccw }.map { |outer| [outer, []] }
63
+ holes = rings.select { |ring| ring.orientation == :cw }
64
+
65
+ holes.each do |hole|
66
+ containing = groups.select { |outer, _| outer.contains?(hole.first) }
67
+ if containing.empty?
68
+ groups << [hole, []]
69
+ else
70
+ containing.min_by { |outer, _| outer.area.abs }.last << hole
71
+ end
72
+ end
73
+ groups
74
+ end
75
+ private_class_method :group_rings
76
+
77
+ def orient_counter_clockwise!(indices, vertices)
78
+ indices.each_slice(3).with_index do |(first, second, third), triangle_index|
79
+ cross = triangle_cross(vertices, first, second, third)
80
+ next if cross.positive?
81
+ raise TriangulationError, "earcut produced a degenerate triangle" if cross.zero?
82
+
83
+ offset = triangle_index * 3
84
+ indices[offset + 1], indices[offset + 2] = indices[offset + 2], indices[offset + 1]
85
+ end
86
+ end
87
+ private_class_method :orient_counter_clockwise!
88
+
89
+ def triangle_cross(vertices, first, second, third)
90
+ ax, ay = vertices[first * 2, 2]
91
+ bx, by = vertices[second * 2, 2]
92
+ cx, cy = vertices[third * 2, 2]
93
+ ((bx - ax) * (cy - ay)) - ((by - ay) * (cx - ax))
94
+ end
95
+ private_class_method :triangle_cross
96
+ end
97
+
98
+ class << self
99
+ def triangulate(paths)
100
+ Triangulator.call(paths)
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Clipper2
4
+ VERSION = "0.1.0"
5
+ end
data/lib/clipper2.rb ADDED
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "clipper2/version"
4
+ require_relative "clipper2/generated"
5
+ require_relative "clipper2/native"
6
+ require_relative "clipper2/errors"
7
+ require_relative "clipper2/quantizer"
8
+ require_relative "clipper2/path"
9
+ require_relative "clipper2/paths"
10
+ require_relative "clipper2/c_paths64"
11
+ require_relative "clipper2/native_memory"
12
+ require_relative "clipper2/operations"
13
+ require_relative "clipper2/api"
14
+ require_relative "clipper2/path_simplifier"
15
+ require_relative "clipper2/earcut"
16
+ require_relative "clipper2/triangulation"
17
+
18
+ module Clipper2
19
+ DEFAULT_SCALE = 1_000
20
+
21
+ class << self
22
+ attr_reader :default_scale
23
+
24
+ def default_scale=(scale)
25
+ @default_scale = Quantizer.validate_scale(scale)
26
+ end
27
+ end
28
+
29
+ self.default_scale = DEFAULT_SCALE
30
+
31
+ def self.native_version
32
+ Native.version
33
+ end
34
+ end
@@ -0,0 +1,23 @@
1
+ Boost Software License - Version 1.0 - August 17th, 2003
2
+
3
+ Permission is hereby granted, free of charge, to any person or organization
4
+ obtaining a copy of the software and accompanying documentation covered by
5
+ this license (the "Software") to use, reproduce, display, distribute,
6
+ execute, and transmit the Software, and to prepare derivative works of the
7
+ Software, and to permit third-parties to whom the Software is furnished to
8
+ do so, all subject to the following:
9
+
10
+ The copyright notices in the Software and this entire statement, including
11
+ the above license grant, this restriction and the following disclaimer,
12
+ must be included in all copies of the Software, in whole or in part, and
13
+ all derivative works of the Software, unless such copies or derivative
14
+ works are solely in the form of machine-executable object code generated by
15
+ a source language processor.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
20
+ SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
21
+ FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
22
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23
+ DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,15 @@
1
+ ISC License
2
+
3
+ Copyright (c) 2016, Mapbox
4
+
5
+ Permission to use, copy, modify, and/or distribute this software for any purpose
6
+ with or without fee is hereby granted, provided that the above copyright notice
7
+ and this permission notice appear in all copies.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
11
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
13
+ OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
14
+ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
15
+ THIS SOFTWARE.
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: clipper2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yudai Takada
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: ffi
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '1.17'
19
+ - - "<"
20
+ - !ruby/object:Gem::Version
21
+ version: '2'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '1.17'
29
+ - - "<"
30
+ - !ruby/object:Gem::Version
31
+ version: '2'
32
+ description: Ruby FFI bindings for Clipper2 with scale-aware polygon value objects.
33
+ email:
34
+ - t.yudai92@gmail.com
35
+ executables: []
36
+ extensions:
37
+ - ext/clipper2/extconf.rb
38
+ extra_rdoc_files: []
39
+ files:
40
+ - LICENSE.txt
41
+ - README.md
42
+ - examples/larb_transform.rb
43
+ - examples/rugl_mesh.rb
44
+ - examples/svg_boolean.rb
45
+ - ext/clipper2/CMakeLists.txt
46
+ - ext/clipper2/clipper2_native.cpp
47
+ - ext/clipper2/extconf.rb
48
+ - ext/clipper2/generate_bindings.rb
49
+ - ext/clipper2/memory_check.cpp
50
+ - ext/clipper2/vendor/LICENSE
51
+ - ext/clipper2/vendor/UPSTREAM.md
52
+ - ext/clipper2/vendor/include/clipper2/clipper.core.h
53
+ - ext/clipper2/vendor/include/clipper2/clipper.engine.h
54
+ - ext/clipper2/vendor/include/clipper2/clipper.export.h
55
+ - ext/clipper2/vendor/include/clipper2/clipper.h
56
+ - ext/clipper2/vendor/include/clipper2/clipper.minkowski.h
57
+ - ext/clipper2/vendor/include/clipper2/clipper.offset.h
58
+ - ext/clipper2/vendor/include/clipper2/clipper.rectclip.h
59
+ - ext/clipper2/vendor/include/clipper2/clipper.triangulation.h
60
+ - ext/clipper2/vendor/include/clipper2/clipper.version.h
61
+ - ext/clipper2/vendor/src/clipper.engine.cpp
62
+ - ext/clipper2/vendor/src/clipper.offset.cpp
63
+ - ext/clipper2/vendor/src/clipper.rectclip.cpp
64
+ - ext/clipper2/vendor/src/clipper.triangulation.cpp
65
+ - lib/clipper2.rb
66
+ - lib/clipper2/api.rb
67
+ - lib/clipper2/c_paths64.rb
68
+ - lib/clipper2/earcut.rb
69
+ - lib/clipper2/earcut/engine.rb
70
+ - lib/clipper2/earcut/geometry.rb
71
+ - lib/clipper2/earcut/node.rb
72
+ - lib/clipper2/earcut/ring.rb
73
+ - lib/clipper2/errors.rb
74
+ - lib/clipper2/generated.rb
75
+ - lib/clipper2/native.rb
76
+ - lib/clipper2/native_memory.rb
77
+ - lib/clipper2/operations.rb
78
+ - lib/clipper2/path.rb
79
+ - lib/clipper2/path_simplifier.rb
80
+ - lib/clipper2/paths.rb
81
+ - lib/clipper2/quantizer.rb
82
+ - lib/clipper2/triangulation.rb
83
+ - lib/clipper2/version.rb
84
+ - licenses/CLIPPER2-BSL-1.0.txt
85
+ - licenses/EARCUT-ISC.txt
86
+ homepage: https://github.com/ydah/clipper2
87
+ licenses:
88
+ - MIT
89
+ - BSL-1.0
90
+ - ISC
91
+ metadata:
92
+ allowed_push_host: https://rubygems.org
93
+ homepage_uri: https://github.com/ydah/clipper2
94
+ source_code_uri: https://github.com/ydah/clipper2/tree/main
95
+ rubygems_mfa_required: 'true'
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: 3.2.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: 4.0.6
111
+ specification_version: 4
112
+ summary: Robust polygon clipping and offsetting for Ruby
113
+ test_files: []