Class: Contrek::Finder::NodeCluster
- Inherits:
-
Object
- Object
- Contrek::Finder::NodeCluster
- Defined in:
- lib/contrek/finder/node_cluster.rb
Constant Summary collapse
- VERSUS_INVERTER =
{a: :o, o: :a}
Instance Attribute Summary collapse
-
#lists ⇒ Object
readonly
Returns the value of attribute lists.
-
#polygons ⇒ Object
readonly
Returns the value of attribute polygons.
-
#root_nodes ⇒ Object
readonly
Returns the value of attribute root_nodes.
-
#sequences ⇒ Object
readonly
Returns the value of attribute sequences.
-
#treemap ⇒ Object
readonly
Returns the value of attribute treemap.
-
#vert_nodes ⇒ Object
readonly
Returns the value of attribute vert_nodes.
Instance Method Summary collapse
- #add_node(node) ⇒ Object
-
#build_tangs_sequence ⇒ Object
builds node sequences (with space coordinates) array scanning upper and lower element needs root_nodes ready.
- #compress_coords ⇒ Object
- #draw_sequence(bitmap, val = nil) ⇒ Object
-
#initialize(h, options) ⇒ NodeCluster
constructor
A new instance of NodeCluster.
-
#named_sequence ⇒ Object
nominal sequence.
- #path_sequences ⇒ Object
- #plot(bitmap) ⇒ Object
-
#plot_inner_node(node, versus, stop_at, start_node) ⇒ Object
inner way nodes in @plot_sequence coordinates in @sequence_coords.
-
#plot_node(node, start_node, versus = :a) ⇒ Object
contour tracing core logic loop.
- #test_in_hole_a(node) ⇒ Object
- #test_in_hole_o(node) ⇒ Object
Constructor Details
#initialize(h, options) ⇒ NodeCluster
Returns a new instance of NodeCluster.
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/contrek/finder/node_cluster.rb', line 7 def initialize(h, ) @options = @vert_nodes = Array.new(h) { [] } # per y immetto i nodi @sequences = [] @polygons = [] @treemap = [] @nodes = 0 @lists = Contrek::Finder::Lists.new @root_nodes = @lists.add_list @inner_plot = @lists.add_list @inner_new = @lists.add_list end |
Instance Attribute Details
#lists ⇒ Object (readonly)
Returns the value of attribute lists.
4 5 6 |
# File 'lib/contrek/finder/node_cluster.rb', line 4 def lists @lists end |
#polygons ⇒ Object (readonly)
Returns the value of attribute polygons.
4 5 6 |
# File 'lib/contrek/finder/node_cluster.rb', line 4 def polygons @polygons end |
#root_nodes ⇒ Object (readonly)
Returns the value of attribute root_nodes.
4 5 6 |
# File 'lib/contrek/finder/node_cluster.rb', line 4 def root_nodes @root_nodes end |
#sequences ⇒ Object (readonly)
Returns the value of attribute sequences.
4 5 6 |
# File 'lib/contrek/finder/node_cluster.rb', line 4 def sequences @sequences end |
#treemap ⇒ Object (readonly)
Returns the value of attribute treemap.
4 5 6 |
# File 'lib/contrek/finder/node_cluster.rb', line 4 def treemap @treemap end |
#vert_nodes ⇒ Object (readonly)
Returns the value of attribute vert_nodes.
4 5 6 |
# File 'lib/contrek/finder/node_cluster.rb', line 4 def vert_nodes @vert_nodes end |
Instance Method Details
#add_node(node) ⇒ Object
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/contrek/finder/node_cluster.rb', line 253 def add_node(node) @nodes += 1 node.abs_x_index = @vert_nodes[node.y].size @vert_nodes[node.y] << node @root_nodes << node if node.y > 0 # all nodes untle up_node.max_x >= node.min_x up_nodes = @vert_nodes[node.y - 1] up_nodes_count = up_nodes.size if up_nodes_count > 0 index = 0 loop do up_node = up_nodes[index] if up_node.max_x >= node.min_x if up_node.min_x <= node.max_x node.add_intersection(up_node) up_node.add_intersection(node) end return if (index += 1) == up_nodes_count loop do up_node = up_nodes[index] if up_node.min_x <= node.max_x node.add_intersection(up_node) up_node.add_intersection(node) else return end break if (index += 1) == up_nodes_count end return end break if (index += 1) == up_nodes_count end end end end |
#build_tangs_sequence ⇒ Object
builds node sequences (with space coordinates) array scanning upper and lower element needs root_nodes ready
44 45 46 47 48 49 50 |
# File 'lib/contrek/finder/node_cluster.rb', line 44 def build_tangs_sequence @vert_nodes.each do |line| line.each do |node| node.precalc_tangs_sequences end end end |
#compress_coords ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/contrek/finder/node_cluster.rb', line 29 def compress_coords path_sequences do |seq| Contrek::Reducers::UniqReducer.new(points: seq).reduce! if @options[:compress].has_key?(:uniq) Contrek::Reducers::LinearReducer.new(points: seq, options: @options[:compress][:linear]).reduce! if @options[:compress].has_key?(:linear) Contrek::Reducers::VisvalingamReducer.new(points: seq, options: @options[:compress][:visvalingam]).reduce! if @options[:compress].has_key?(:visvalingam) end end |
#draw_sequence(bitmap, val = nil) ⇒ Object
173 174 175 176 177 178 179 |
# File 'lib/contrek/finder/node_cluster.rb', line 173 def draw_sequence(bitmap, val = nil) count = 1 @sequence_coords.each do |coords| bitmap.value_set(coords[:x], coords[:y], val.nil? ? count.alph : val) count += 1 end end |
#named_sequence ⇒ Object
nominal sequence
38 39 40 |
# File 'lib/contrek/finder/node_cluster.rb', line 38 def named_sequence @plot_sequence.map(&:name).join end |
#path_sequences ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/contrek/finder/node_cluster.rb', line 20 def path_sequences @polygons.compact.each do |polygon| yield polygon[:outer] polygon[:inner].each do |sequence| yield sequence end end end |
#plot(bitmap) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/contrek/finder/node_cluster.rb', line 52 def plot(bitmap) versus = @options[:versus] inner_v = VERSUS_INVERTER[versus] index_order = 0 while @root_nodes.size > 0 root_node = @root_nodes.shift root_node.outer_index = index_order @plot_sequence = [] @sequence_coords = [] # external polygon @plot_sequence << root_node next_node_nd = if versus == :a root_node.tangs_sequence.last else root_node.tangs_sequence.first end #--------------- if !next_node_nd.nil? next_node = next_node_nd.node @sequence_coords << next_node.coords_entering_to(root_node, VERSUS_INVERTER[versus], Contrek::Finder::Node::OUTER) end #--------------- plot_node(next_node, root_node, versus) if @nodes > 0 && !next_node_nd.nil? draw_sequence(bitmap, "X") unless bitmap.nil? @polygons << {outer: @sequence_coords, inner: []} if @sequence_coords.size >= 2 @sequences << @plot_sequence @count = 0 index_inner = 0 while @inner_plot.size > 0 @plot_sequence = [] @sequence_coords = [] # mia test first = @inner_plot.find { |x| x.tangs_count <= 2 } || @inner_plot.first @plot_sequence << first @inner_plot.delete(first) @root_nodes.delete(first) first.inner_index = index_inner # @count += 1 # if @count > 10000 # puts "Houston, we have a problem!" # break # end next_node = if (first.track & Contrek::Finder::Node::OMAX) != 0 (inner_v == :a) ? first.tangs[Contrek::Finder::Node::T_UP].first : first.tangs[Contrek::Finder::Node::T_DOWN].first else (inner_v == :a) ? first.tangs[Contrek::Finder::Node::T_DOWN].last : first.tangs[Contrek::Finder::Node::T_UP].last end if !next_node.nil? @sequence_coords << next_node.coords_entering_to(first, inner_v, Contrek::Finder::Node::INNER) end plot_inner_node(next_node, inner_v, first, root_node) if !next_node.nil? draw_sequence(bitmap, "+") unless bitmap.nil? @polygons.last[:inner] << @sequence_coords @inner_plot.grab(@inner_new) index_inner += 1 end # tree @treemap << ((versus == :a) ? test_in_hole_a(root_node) : test_in_hole_o(root_node)) if @options.has_key?(:treemap) index_order += 1 end end |
#plot_inner_node(node, versus, stop_at, start_node) ⇒ Object
inner way nodes in @plot_sequence coordinates in @sequence_coords
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/contrek/finder/node_cluster.rb', line 184 def plot_inner_node(node, versus, stop_at, start_node) node.outer_index = start_node.outer_index node.inner_index = stop_at.inner_index @root_nodes.delete(node) @inner_plot.delete(node) last_node = @plot_sequence.last next_node_nd = node.my_next(last_node, versus, :inner) next_node = next_node_nd.node @plot_sequence << node plot = true if next_node.y == last_node.y plot = (node.tangs_sequence.send((versus == :a) ? :first : :last).node == next_node) end if plot @sequence_coords << last_node.coords_entering_to(node, VERSUS_INVERTER[versus], Contrek::Finder::Node::INNER) if node != start_node if last_node.y == next_node.y @sequence_coords << next_node.coords_entering_to(node, versus, Contrek::Finder::Node::INNER) end end end if node.track_uncomplete @inner_new << node else @inner_new.delete(node) end return if next_node == stop_at plot_inner_node(next_node, versus, stop_at, start_node) end |
#plot_node(node, start_node, versus = :a) ⇒ Object
contour tracing core logic loop
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/contrek/finder/node_cluster.rb', line 218 def plot_node(node, start_node, versus = :a) @root_nodes.delete(node) node.outer_index = start_node.outer_index last_node = @plot_sequence.last next_node_nd = node.my_next(last_node, versus, :outer) next_node = next_node_nd.node @plot_sequence << node plot = true if next_node.y == last_node.y plot = (node.tangs_sequence.send((versus == :a) ? :last : :first).node == next_node) end # coord if plot @sequence_coords << last_node.coords_entering_to(node, versus, Contrek::Finder::Node::OUTER) if node != start_node @inner_plot.contains(node) ? @inner_plot.delete(node) : @inner_plot << node if last_node.y == next_node.y @sequence_coords << next_node.coords_entering_to(node, VERSUS_INVERTER[versus], Contrek::Finder::Node::OUTER) @inner_plot.contains(node) ? @inner_plot.delete(node) : @inner_plot << node end end end # exit if root_node if node == start_node return if node.track_complete end plot_node(next_node, start_node, versus) end |
#test_in_hole_a(node) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/contrek/finder/node_cluster.rb', line 125 def test_in_hole_a(node) if node.outer_index > 0 start_left = node.abs_x_index - 1 loop do prev = @vert_nodes[node.y][start_left] if ((cindex = prev.outer_index) < node.outer_index) && ((prev.track & Contrek::Finder::Node::IMAX) != 0) start_right = node.abs_x_index while (start_right += 1) != @vert_nodes[node.y].size tnext = @vert_nodes[node.y][start_right] if tnext.outer_index == cindex if (tnext.track & Contrek::Finder::Node::IMIN) != 0 return [cindex, prev.inner_index] else return [-1, -1] end end end end break if (start_left -= 1) < 0 end end [-1, -1] end |
#test_in_hole_o(node) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/contrek/finder/node_cluster.rb', line 149 def test_in_hole_o(node) if node.outer_index > 0 && @vert_nodes[node.y].last != node start_left = node.abs_x_index + 1 loop do prev = @vert_nodes[node.y][start_left] if ((cindex = prev.outer_index) < node.outer_index) && ((prev.track & Contrek::Finder::Node::IMIN) != 0) start_right = node.abs_x_index while (start_right -= 1) >= 0 tnext = @vert_nodes[node.y][start_right] if tnext.outer_index == cindex if (tnext.track & Contrek::Finder::Node::IMAX) != 0 return [cindex, prev.inner_index] else return [-1, -1] end end end end break if (start_left += 1) == @vert_nodes[node.y].size end end [-1, -1] end |