Class: SimplifyRb
- Inherits:
-
Object
- Object
- SimplifyRb
- Defined in:
- lib/simplify_rb.rb,
lib/simplify_rb/version.rb
Constant Summary collapse
- VERSION =
"0.1.3"
Class Method Summary collapse
-
.get_sq_dist(point_1, point_2) ⇒ Object
Square distance between two points.
-
.get_sq_seg_dist(point, point_1, point_2) ⇒ Object
Square distance from a point to a segment.
- .simplify(points, tolerance = 1, highest_quality = false) ⇒ Object
-
.simplify_douglas_peucker(points, sq_tolerance) ⇒ Object
Simplification using optimized Douglas-Peucker algorithm with recursion elimination.
-
.simplify_radial_dist(points, sq_tolerance) ⇒ Object
Basic distance-based simplification.
Class Method Details
.get_sq_dist(point_1, point_2) ⇒ Object
Square distance between two points
72 73 74 75 76 77 |
# File 'lib/simplify_rb.rb', line 72 def self.get_sq_dist(point_1, point_2) dx = point_1[:x] - point_2[:x] dy = point_1[:y] - point_2[:y] dx * dx + dy * dy end |
.get_sq_seg_dist(point, point_1, point_2) ⇒ Object
Square distance from a point to a segment
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/simplify_rb.rb', line 80 def self.get_sq_seg_dist(point, point_1, point_2) x = point_1[:x] y = point_1[:y] dx = point_2[:x] - x dy = point_2[:y] - y if dx != 0 || dy != 0 t = ((point[:x] - x) * dx + (point[:y] - y) * dy) / (dx * dx + dy * dy) if t > 1 x = point_2[:x] y = point_2[:y] elsif t > 0 x += dx * t y += dy * t end end dx = point[:x] - x dy = point[:y] - y dx * dx + dy * dy end |
.simplify(points, tolerance = 1, highest_quality = false) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/simplify_rb.rb', line 6 def self.simplify(points, tolerance = 1, highest_quality = false) raise ArgumentError.new('Points must be an array') unless points.is_a? Array return points if points.length <= 1 symbolizer = SimplifyRbUtils::Symbolizer.new points = symbolizer.symbolize_keys(points) unless points.all? { |p| symbolizer.keys_are_symbols?(p.keys) } sq_tolerance = tolerance * tolerance # Optimisation step 1 points = simplify_radial_dist(points, sq_tolerance) unless highest_quality # Optimisation step 2 simplify_douglas_peucker(points, sq_tolerance) end |
.simplify_douglas_peucker(points, sq_tolerance) ⇒ Object
Simplification using optimized Douglas-Peucker algorithm with recursion elimination
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/simplify_rb.rb', line 38 def self.simplify_douglas_peucker(points, sq_tolerance) first = 0 last = points.length - 1 index = nil stack = [] points.first[:keep] = true points.last[:keep] = true while last max_sq_dist = 0 ((first + 1)...last).each do |i| sq_dist = get_sq_seg_dist(points[i], points[first], points[last]) if sq_dist > max_sq_dist index = i max_sq_dist = sq_dist end end if max_sq_dist > sq_tolerance points[index][:keep] = true stack.push(first, index, index, last) end first, last = stack.pop(2) end # end while points.select { |p| p[:keep] && p.delete(:keep) } end |
.simplify_radial_dist(points, sq_tolerance) ⇒ Object
Basic distance-based simplification
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/simplify_rb.rb', line 25 def self.simplify_radial_dist(points, sq_tolerance) new_points = [points.first] points.each do |point| new_points << point if (get_sq_dist(point, new_points.last) > sq_tolerance) end new_points << points.last unless new_points.last == points.last new_points end |