Class: Contrek::Finder::NodeCluster

Inherits:
Object
  • Object
show all
Defined in:
lib/contrek/finder/node_cluster.rb

Constant Summary collapse

VERSUS_INVERTER =
{a: :o, o: :a}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(h, options) ⇒ NodeCluster

Returns a new instance of NodeCluster.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/contrek/finder/node_cluster.rb', line 9

def initialize(h, options)
  @options = 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

#listsObject (readonly)

Returns the value of attribute lists.



6
7
8
# File 'lib/contrek/finder/node_cluster.rb', line 6

def lists
  @lists
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/contrek/finder/node_cluster.rb', line 6

def options
  @options
end

#polygonsObject (readonly)

Returns the value of attribute polygons.



6
7
8
# File 'lib/contrek/finder/node_cluster.rb', line 6

def polygons
  @polygons
end

#root_nodesObject (readonly)

Returns the value of attribute root_nodes.



6
7
8
# File 'lib/contrek/finder/node_cluster.rb', line 6

def root_nodes
  @root_nodes
end

#sequencesObject (readonly)

Returns the value of attribute sequences.



6
7
8
# File 'lib/contrek/finder/node_cluster.rb', line 6

def sequences
  @sequences
end

#treemapObject (readonly)

Returns the value of attribute treemap.



6
7
8
# File 'lib/contrek/finder/node_cluster.rb', line 6

def treemap
  @treemap
end

#vert_nodesObject (readonly)

Returns the value of attribute vert_nodes.



6
7
8
# File 'lib/contrek/finder/node_cluster.rb', line 6

def vert_nodes
  @vert_nodes
end

Instance Method Details

#add_node(node, offset) ⇒ Object



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/contrek/finder/node_cluster.rb', line 338

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 until 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 - 1) + offset) >= node.min_x
          if (up_node.min_x - offset) <= (node.max_x - 1)
            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 - 1)
              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_sequenceObject

builds node sequences (with space coordinates) array scanning upper and lower element needs root_nodes ready



48
49
50
51
52
53
54
# File 'lib/contrek/finder/node_cluster.rb', line 48

def build_tangs_sequence
  @vert_nodes.each do |line|
    line.each do |node|
      node.precalc_tangs_sequences(cluster: self)
    end
  end
end

#compress_coordsObject



31
32
33
34
35
36
37
38
39
# File 'lib/contrek/finder/node_cluster.rb', line 31

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::RasterReducer.new(points: seq, options: @options).reduce! if @options[:compress].has_key?(:raster)
    Contrek::Reducers::VisvalingamReducer.new(points: seq, options: @options[:compress][:visvalingam]).reduce! if @options[:compress].has_key?(:visvalingam)
    Contrek::Reducers::DouglasPeuckerReducer.new(points: seq).reduce! if @options[:compress].has_key?(:douglas_peucker)
  end
end

#draw_sequence(bitmap, val = nil) ⇒ Object



217
218
219
220
221
222
223
# File 'lib/contrek/finder/node_cluster.rb', line 217

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_sequenceObject

nominal sequence



42
43
44
# File 'lib/contrek/finder/node_cluster.rb', line 42

def named_sequence
  @plot_sequence.map(&:name).join
end

#path_sequencesObject



22
23
24
25
26
27
28
29
# File 'lib/contrek/finder/node_cluster.rb', line 22

def path_sequences
  @polygons.compact.each do |polygon|
    yield polygon[:outer]
    polygon[:inner].each do |sequence|
      yield sequence
    end
  end
end

#plot(bitmap) ⇒ Object



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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/contrek/finder/node_cluster.rb', line 56

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
      beginning_point = (versus == :a) ? root_node.nw_point : root_node.ne_point
      @sequence_coords << beginning_point
      bounds.expand(**beginning_point)

      coord = next_node.coords_entering_to(root_node, VERSUS_INVERTER[versus], Contrek::Finder::Node::OUTER)
      @sequence_coords << coord
      bounds.expand(**coord)
    end

    plot_node(next_node, root_node, bounds, versus) if @nodes > 0 && !next_node.nil?

    if next_node.nil?
      if versus == :a
        nw_point = root_node.nw_point
        @sequence_coords << nw_point
        bounds.expand(**nw_point)
        @sequence_coords << root_node.sw_point
        se_point = root_node.se_point
        @sequence_coords << se_point
        bounds.expand(**se_point)
        @sequence_coords << root_node.ne_point
      else
        ne_point = root_node.ne_point
        @sequence_coords << ne_point
        bounds.expand(**ne_point)
        @sequence_coords << root_node.se_point
        sw_point = root_node.sw_point
        @sequence_coords << sw_point
        bounds.expand(**sw_point)
        @sequence_coords << root_node.nw_point
      end
    end

    draw_sequence(bitmap, "X") unless bitmap.nil?

    if @sequence_coords.any?
      @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)
          last_node = plot_inner_node(next_node, inner_v, first, root_node)
          last_coord = last_node.coords_entering_to(first, VERSUS_INVERTER[inner_v], Contrek::Finder::Node::INNER)
          @sequence_coords << last_coord if @sequence_coords.last != last_coord
        end

        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) ⇒ Object

inner way nodes in @plot_sequence coordinates in @sequence_coords



228
229
230
231
232
233
234
235
236
237
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
# File 'lib/contrek/finder/node_cluster.rb', line 228

def plot_inner_node(node, versus, stop_at, start_node)
  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
    first_point = last_node.coords_entering_to(node, VERSUS_INVERTER[versus], Contrek::Finder::Node::INNER)
    @sequence_coords << first_point if @sequence_coords.last != first_point
    if next_node.y == last_node.y
      if next_node.y < node.y
        pt_a, pt_b = node.sw_point, node.se_point
      else
        pt_a, pt_b = node.ne_point, node.nw_point
      end
      if versus == :o
        @sequence_coords << pt_a
        @sequence_coords << pt_b
      else
        @sequence_coords << pt_b
        @sequence_coords << pt_a
      end
    end
    @sequence_coords << next_node.coords_entering_to(node, versus, Contrek::Finder::Node::INNER)
  end

  if node.track_uncomplete
    @inner_new << node
  else
    @inner_new.delete(node)
  end

  return node if next_node == stop_at
  plot_inner_node(next_node, versus, stop_at, start_node)
end

#plot_node(node, start_node, bounds, versus = :a) ⇒ Object

contour tracing core logic loop



280
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/contrek/finder/node_cluster.rb', line 280

def plot_node(node, start_node, bounds, versus = :a)
  @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

  if plot
    start_coord = last_node.coords_entering_to(node, versus, Contrek::Finder::Node::OUTER)
    if @sequence_coords.last != start_coord
      @sequence_coords << start_coord
      bounds.expand(**start_coord)
    end

    final_step = node == start_node && node.track_complete
    if next_node.y == last_node.y
      if next_node.y < node.y
        pt_a, pt_b = node.se_point, node.sw_point
      else
        pt_a, pt_b = node.nw_point, node.ne_point
      end
      first = (versus == :o) ? pt_a : pt_b
      second = (versus == :o) ? pt_b : pt_a
      @sequence_coords << first
      bounds.expand(**first)
      unless final_step
        @sequence_coords << second
        bounds.expand(**second)
      end
    end

    unless final_step
      end_coord = next_node.coords_entering_to(node, VERSUS_INVERTER[versus], Contrek::Finder::Node::OUTER)
      @sequence_coords << end_coord
      bounds.expand(**end_coord)
    end

    if node != start_node
      @inner_plot.contains(node) ? @inner_plot.delete(node) : @inner_plot << node
      if last_node.y == next_node.y
        @inner_plot.contains(node) ? @inner_plot.delete(node) : @inner_plot << node
      end
    end
  end
  # exit if root_node
  return if node == start_node && node.track_complete

  plot_node(next_node, start_node, bounds, versus)
end

#test_in_hole_a(node) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/contrek/finder/node_cluster.rb', line 169

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



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 193

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