Class: SimplifyRb
- Inherits:
-
Object
- Object
- SimplifyRb
- Defined in:
- lib/simplify_rb.rb,
lib/simplify_rb/version.rb
Constant Summary collapse
- VERSION =
"0.1.2"
Class Method Summary collapse
-
.getSqDist(point_1, point_2) ⇒ Object
Square distance between two points.
-
.getSqSegDist(point, point_1, point_2) ⇒ Object
Square distance from a point to a segment.
-
.keys_are_symbols?(keys) ⇒ Boolean
Check if keys are symbols.
-
.simplify(points, tolerance = 1, highest_quality = false) ⇒ Object
Main method.
-
.simplifyDouglasPeucker(points, sq_tolerance) ⇒ Object
Simplification using optimized Douglas-Peucker algorithm with recursion elimination.
-
.simplifyRadialDist(points, sq_tolerance) ⇒ Object
Basic distance-based simplification.
-
.symbolize_keys(collection) ⇒ Object
Symbolize all the hash keys in an array of hashes.
Class Method Details
.getSqDist(point_1, point_2) ⇒ Object
Square distance between two points
69 70 71 72 73 74 |
# File 'lib/simplify_rb.rb', line 69 def self.getSqDist (point_1, point_2) dx = point_1[:x] - point_2[:x] dy = point_1[:y] - point_2[:y] dx * dx + dy * dy end |
.getSqSegDist(point, point_1, point_2) ⇒ Object
Square distance from a point to a segment
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/simplify_rb.rb', line 77 def self.getSqSegDist (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 |
.keys_are_symbols?(keys) ⇒ Boolean
Check if keys are symbols
103 104 105 |
# File 'lib/simplify_rb.rb', line 103 def self.keys_are_symbols? (keys) keys.all? {|k| k.is_a? Symbol} end |
.simplify(points, tolerance = 1, highest_quality = false) ⇒ Object
Main method
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/simplify_rb.rb', line 5 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 points = symbolize_keys(points) unless keys_are_symbols?(points.map(&:keys)) sq_tolerance = tolerance * tolerance # Optimisation step 1 points = simplifyRadialDist(points, sq_tolerance) unless highest_quality # Optimisation step 2 simplifyDouglasPeucker(points, sq_tolerance) end |
.simplifyDouglasPeucker(points, sq_tolerance) ⇒ Object
Simplification using optimized Douglas-Peucker algorithm with recursion elimination
35 36 37 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 |
# File 'lib/simplify_rb.rb', line 35 def self.simplifyDouglasPeucker (points, sq_tolerance) first = 0 last = points.length - 1 index = nil stack = [] points.first[:keep] = true points.last[:keep] = true while last do max_sq_dist = 0 ((first + 1)...last).each do |i| sq_dist = getSqSegDist(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 |
.simplifyRadialDist(points, sq_tolerance) ⇒ Object
Basic distance-based simplification
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/simplify_rb.rb', line 22 def self.simplifyRadialDist (points, sq_tolerance) new_points = [points.first] points.each do |point| new_points << point if (getSqDist(point, new_points.last) > sq_tolerance) end new_points << points.last unless new_points.last == points.last new_points end |
.symbolize_keys(collection) ⇒ Object
Symbolize all the hash keys in an array of hashes
108 109 110 111 112 |
# File 'lib/simplify_rb.rb', line 108 def self.symbolize_keys (collection) collection.map do |item| item.each_with_object({}) { |(k,v), memo| memo[k.to_sym] = v } end end |