Class: Contrek::Reducers::Vertex

Inherits:
Object
  • Object
show all
Defined in:
lib/contrek/reducers/visvalingam_reducer.rb

Constant Summary collapse

MAX_AREA =
Float::MAX

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pt) ⇒ Vertex

Returns a new instance of Vertex.



60
61
62
63
64
65
66
# File 'lib/contrek/reducers/visvalingam_reducer.rb', line 60

def initialize(pt)
  @pt = pt
  @prev = nil
  @next = nil
  @area = MAX_AREA
  @is_live = true
end

Instance Attribute Details

#is_liveObject (readonly)

Returns the value of attribute is_live.



106
107
108
# File 'lib/contrek/reducers/visvalingam_reducer.rb', line 106

def is_live
  @is_live
end

#nextObject (readonly)

Returns the value of attribute next.



58
59
60
# File 'lib/contrek/reducers/visvalingam_reducer.rb', line 58

def next
  @next
end

#prevObject (readonly)

Returns the value of attribute prev.



58
59
60
# File 'lib/contrek/reducers/visvalingam_reducer.rb', line 58

def prev
  @prev
end

#ptObject (readonly)

Returns the value of attribute pt.



58
59
60
# File 'lib/contrek/reducers/visvalingam_reducer.rb', line 58

def pt
  @pt
end

Class Method Details

.build_line(pts) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/contrek/reducers/visvalingam_reducer.rb', line 68

def self.build_line(pts)
  first = nil
  prev = nil
  pts.each_with_index do |pt, i|
    v = Vertex.new(pts[i])
    first = v if first.nil?

    v.setPrev(prev)
    if !prev.nil?
      prev.setNext(v)
      prev.updateArea
    end
    prev = v
  end
  first
end

Instance Method Details

#get_areaObject



102
103
104
# File 'lib/contrek/reducers/visvalingam_reducer.rb', line 102

def get_area
  @area
end

#get_coordinatesObject



127
128
129
130
131
132
133
134
135
136
# File 'lib/contrek/reducers/visvalingam_reducer.rb', line 127

def get_coordinates
  coords = []
  curr = self
  loop do
    coords << curr.pt
    curr = curr.next
    break if curr.nil?
  end
  coords
end

#removeObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/contrek/reducers/visvalingam_reducer.rb', line 108

def remove
  tmp_prev = @prev
  tmp_next = @next
  result = nil
  if !prev.nil?
    @prev.setNext(tmp_next)
    @prev.updateArea
    result = prev
  end
  if !@next.nil?
    @next.setPrev(tmp_prev)
    @next.updateArea
    result = @next if result.nil?
  end

  @is_live = false
  result
end

#setNext(nextp) ⇒ Object



89
90
91
# File 'lib/contrek/reducers/visvalingam_reducer.rb', line 89

def setNext(nextp)
  @next = nextp
end

#setPrev(prev) ⇒ Object



85
86
87
# File 'lib/contrek/reducers/visvalingam_reducer.rb', line 85

def setPrev(prev)
  @prev = prev
end

#updateAreaObject



93
94
95
96
97
98
99
100
# File 'lib/contrek/reducers/visvalingam_reducer.rb', line 93

def updateArea
  if @prev.nil? || @next.nil?
    @area = MAX_AREA

    return
  end
  @area = Triangle.area(@prev.pt, pt, @next.pt).abs
end