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.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#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, offset) ⇒ 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, strict_bounds = false) ⇒ Object
inner way nodes in @plot_sequence coordinates in @sequence_coords.
-
#plot_node(node, start_node, bounds, versus = :a, strict_bounds = false) ⇒ 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) { [] } @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 |
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'lib/contrek/finder/node_cluster.rb', line 4 def @options 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, offset) ⇒ Object
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/contrek/finder/node_cluster.rb', line 281 def add_node(node, offset) @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 + offset) >= node.min_x if (up_node.min_x - offset) <= node.max_x node.add_intersection(up_node, index) up_node.add_intersection(node, node.abs_x_index) end return if (index += 1) == up_nodes_count loop do up_node = up_nodes[index] if (up_node.min_x - offset) <= node.max_x node.add_intersection(up_node, index) up_node.add_intersection(node, node.abs_x_index) 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(cluster: self) 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
184 185 186 187 188 189 190 |
# File 'lib/contrek/finder/node_cluster.rb', line 184 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 124 125 126 127 128 129 130 131 132 133 134 |
# 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 = [] bounds = Bounds.empty # external polygon @plot_sequence << root_node next_node = if versus == :a root_node.get_tangent_node_by_virtual_index(root_node.tangs_sequence.last) else root_node.get_tangent_node_by_virtual_index(root_node.tangs_sequence.first) end if !next_node.nil? coord = next_node.coords_entering_to(root_node, VERSUS_INVERTER[versus], Contrek::Finder::Node::OUTER) @sequence_coords << coord bounds.(x: coord[:x], y: coord[:y]) end plot_node(next_node, root_node, bounds, versus, @options[:strict_bounds]) if @nodes > 0 && !next_node.nil? draw_sequence(bitmap, "X") unless bitmap.nil? if @sequence_coords.size >= 2 @polygons << {outer: @sequence_coords, inner: [], bounds: (bounds.to_h if @options[:bounds])}.compact @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 if inner_v == :a vert_nodes[first.y + Node::T_UP][first.upper_start] else vert_nodes[first.y + Node::T_DOWN][first.lower_start] end elsif inner_v == :a vert_nodes[first.y + Node::T_DOWN][first.lower_end] else vert_nodes[first.y + Node::T_UP][first.upper_end] 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, [:strict_bounds]) 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 end |
#plot_inner_node(node, versus, stop_at, start_node, strict_bounds = false) ⇒ Object
inner way nodes in @plot_sequence coordinates in @sequence_coords
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/contrek/finder/node_cluster.rb', line 195 def plot_inner_node(node, versus, stop_at, start_node, strict_bounds = false) node.outer_index = start_node.outer_index @root_nodes.delete(node) @inner_plot.delete(node) last_node = @plot_sequence.last next_node = node.my_next(last_node, versus, :inner) @plot_sequence << node first_is_max = ((node.y > last_node.y) == (versus == :a)) if first_is_max node.inner_right_index = stop_at.inner_index if node.inner_right_index == -1 elsif node.inner_left_index == -1 node.inner_left_index = stop_at.inner_index end plot = true if next_node.y == last_node.y virtual_index = node.tangs_sequence.send((versus == :a) ? :first : :last) plot = (node.get_tangent_node_by_virtual_index(virtual_index) == 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 elsif strict_bounds @sequence_coords << {y: node.y, x: (first_is_max ? last_node.max_x : last_node.min_x)} @sequence_coords << {y: node.y, x: (first_is_max ? next_node.min_x : next_node.max_x)} 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, strict_bounds) end |
#plot_node(node, start_node, bounds, versus = :a, strict_bounds = false) ⇒ Object
contour tracing core logic loop
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 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 |
# File 'lib/contrek/finder/node_cluster.rb', line 238 def plot_node(node, start_node, bounds, versus = :a, strict_bounds = false) @root_nodes.delete(node) node.outer_index = start_node.outer_index last_node = @plot_sequence.last next_node = node.my_next(last_node, versus, :outer) @plot_sequence << node plot = true if next_node.y == last_node.y virtual_index = node.tangs_sequence.send((versus == :a) ? :last : :first) plot = (node.get_tangent_node_by_virtual_index(virtual_index) == next_node) end # coord if plot c = last_node.coords_entering_to(node, versus, Contrek::Finder::Node::OUTER) @sequence_coords << c bounds.(x: c[:x], y: c[:y]) if node != start_node @inner_plot.contains(node) ? @inner_plot.delete(node) : @inner_plot << node if last_node.y == next_node.y c = next_node.coords_entering_to(node, VERSUS_INVERTER[versus], Contrek::Finder::Node::OUTER) @sequence_coords << c bounds.(x: c[:x], y: c[:y]) @inner_plot.contains(node) ? @inner_plot.delete(node) : @inner_plot << node end end elsif strict_bounds is_down = node.y > last_node.y is_a = (versus == :a) @sequence_coords << {y: node.y, x: ((is_down == is_a) ? last_node.min_x : last_node.max_x)} @sequence_coords << {y: node.y, x: ((is_down == is_a) ? next_node.max_x : next_node.min_x)} end # exit if root_node if node == start_node return if node.track_complete end plot_node(next_node, start_node, bounds, versus, strict_bounds) end |
#test_in_hole_a(node) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/contrek/finder/node_cluster.rb', line 136 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_right_index == -1) ? prev.inner_left_index : prev.inner_right_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
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/contrek/finder/node_cluster.rb', line 160 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_left_index == -1) ? prev.inner_right_index : prev.inner_left_index] else return [-1, -1] end end end end break if (start_left += 1) == @vert_nodes[node.y].size end end [-1, -1] end |