Class: SpaceTimeId

Inherits:
Object
  • Object
show all
Extended by:
InheritableAttribute
Defined in:
lib/space_time_id.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from InheritableAttribute

inherit_for, inheritable_attr

Constructor Details

#initialize(*args) ⇒ SpaceTimeId

Returns a new instance of SpaceTimeId.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/space_time_id.rb', line 20

def initialize(*args)
  self.options = self.class.default_options.merge(args.extract_options!)
  self.interval = options.delete(:interval) || 0
  self.level = options.delete(:level) || 0
  if args.length == 1 && args.first.is_a?(String)
    initialize(*args.first.split("_").map(&:to_f), original_options)
  elsif args.length == 1 && args.first.is_a?(Array) && args.first.length == 2
    initialize(*(args.first), original_options)
  elsif args.all? { |arg| arg.is_a?(Fixnum) || arg.is_a?(Float) }
    case args.length
    when 1
      self.ts_sec = args.first
      self.ts_sec = ts_id.to_i
    when 2
      self.x, self.y = args
      self.x, self.y = xy_id_2d
    when 3
      self.ts_sec, self.x, self.y = args
      self.x, self.y = xy_id_2d
      self.ts_sec = ts_id.to_i
    else
      raise "Invalid initialize arguments! (#{args.inspect})"
    end
  elsif args.length == 2 &&
      (args.first.is_a?(Fixnum) || args.first.is_a?(Float)) && args.last.is_a?(Array)
    ts = args.first
    x, y = args.last
    initialize(ts, x, y, original_options)
  else
    raise "Invalid initialize arguments! (#{args.inspect})"
  end
end

Instance Attribute Details

#intervalObject

Returns the value of attribute interval.



17
18
19
# File 'lib/space_time_id.rb', line 17

def interval
  @interval
end

#levelObject

Returns the value of attribute level.



18
19
20
# File 'lib/space_time_id.rb', line 18

def level
  @level
end

#optionsObject

Returns the value of attribute options.



16
17
18
# File 'lib/space_time_id.rb', line 16

def options
  @options
end

#ts_secObject

Returns the value of attribute ts_sec.



13
14
15
# File 'lib/space_time_id.rb', line 13

def ts_sec
  @ts_sec
end

#xObject

Returns the value of attribute x.



14
15
16
# File 'lib/space_time_id.rb', line 14

def x
  @x
end

#yObject

Returns the value of attribute y.



15
16
17
# File 'lib/space_time_id.rb', line 15

def y
  @y
end

Instance Method Details

#==(other) ⇒ Object



80
81
82
# File 'lib/space_time_id.rb', line 80

def ==(other)
  other.nil? || !other.respond_to?(:id) ? false : id == other.id
end

#decObject



96
97
98
# File 'lib/space_time_id.rb', line 96

def dec
  options[:decimals]
end

#dup(new_id = id, options = self.options) ⇒ Object



92
93
94
# File 'lib/space_time_id.rb', line 92

def dup(new_id = id, options = self.options)
  SpaceTimeId.new(new_id, original_options.merge(options))
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/space_time_id.rb', line 84

def eql?(other)
  other.is_a?(SpaceTimeId) && self == other
end

#hashObject



88
89
90
# File 'lib/space_time_id.rb', line 88

def hash
  [id, interval, level].hash
end

#idObject



53
54
55
# File 'lib/space_time_id.rb', line 53

def id
  [ts_id, xy_id_str].compact.join("_")
end

#inspectObject



61
62
63
# File 'lib/space_time_id.rb', line 61

def inspect
  "<SpaceTimeId:#{id} #{original_options.inspect}>"
end

#next_ts(i = 1) ⇒ Object

# # # # # # # # # # # # T I M E R E L A T I O N S # # # # # # # # # # # #



136
137
138
139
# File 'lib/space_time_id.rb', line 136

def next_ts(i = 1)
  @_next_ts ||= {}
  @_next_ts[i] ||= digitize(ts_id + ts_step * i + ts_step / 2.0, ts_step) if ts?
end

#next_x(i = 1) ⇒ Object

# # # # # # # # # # # S P A C E R E L A T I O N S # # # # # # # # # # # #



193
194
195
196
# File 'lib/space_time_id.rb', line 193

def next_x(i = 1)
  @_next_x ||= {}
  @_next_x[i] ||= digitize(x + xy_step * i + xy_step / 2.0, xy_step) if xy?
end

#next_y(i = 1) ⇒ Object



198
199
200
201
# File 'lib/space_time_id.rb', line 198

def next_y(i = 1)
  @_next_y ||= {}
  @_next_y[i] ||= digitize(y + xy_step * i + xy_step / 2.0, xy_step) if xy?
end

#original_optionsObject



100
101
102
# File 'lib/space_time_id.rb', line 100

def original_options
  {level: level, interval: interval}.merge(options)
end

#to_htmlObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/space_time_id.rb', line 65

def to_html
  html = "id: #{id}<br/>"
  if ts?
    html << "interval: #{inteval}<br/>"
    html << "time_base_step: #{time_base_step}<br/>"
    html << "time_expansion: #{time_expansion}<br/>"
  end
  if xy?
    html << "level: #{level}<br/>"
    html << "xy_base_step: #{xy_base_step}<br/>"
    html << "xy_expansion: #{xy_expansion}<br/>"
  end
  html
end

#to_sObject



57
58
59
# File 'lib/space_time_id.rb', line 57

def to_s
  id
end

#tsObject



110
111
112
# File 'lib/space_time_id.rb', line 110

def ts
  Time.at(ts_sec) if ts_sec
end

#ts?Boolean

# # # # # # # # # # # T I M E # # # # # # # # # # # # # # # # # # # # # # #

Returns:

  • (Boolean)


106
107
108
# File 'lib/space_time_id.rb', line 106

def ts?
  !ts_sec.nil?
end

#ts_base_stepObject



126
127
128
# File 'lib/space_time_id.rb', line 126

def ts_base_step
  options[:ts_base_step]
end

#ts_expansionObject



130
131
132
# File 'lib/space_time_id.rb', line 130

def ts_expansion
  options[:ts_expansion]
end

#ts_idObject



118
119
120
# File 'lib/space_time_id.rb', line 118

def ts_id
  digitize(ts_sec, ts_step).to_i if ts_sec
end

#ts_msObject



114
115
116
# File 'lib/space_time_id.rb', line 114

def ts_ms
  ts_sec * 1000 if ts_sec
end

#ts_neighborsObject



141
142
143
144
145
146
147
# File 'lib/space_time_id.rb', line 141

def ts_neighbors
  @ts_neighbors ||= begin
    prevt = dup(next_ts(-1))
    nextt = dup(next_ts)
    @ts_neighbors = [prevt, nextt]
  end if ts?
end

#ts_parentObject



149
150
151
# File 'lib/space_time_id.rb', line 149

def ts_parent
  dup(id, interval: interval + 1) if ts?
end

#ts_stepObject



122
123
124
# File 'lib/space_time_id.rb', line 122

def ts_step
  interval == 0 ? ts_base_step : ts_base_step * ts_expansion * interval
end

#xyObject



159
160
161
# File 'lib/space_time_id.rb', line 159

def xy
  [x, y].compact
end

#xy?Boolean

# # # # # # # # # # # S P A C E # # # # # # # # # # # # # # # # # # # # # #

Returns:

  • (Boolean)


155
156
157
# File 'lib/space_time_id.rb', line 155

def xy?
  !xy.empty?
end

#xy_base_stepObject



183
184
185
# File 'lib/space_time_id.rb', line 183

def xy_base_step
  options[:xy_base_step]
end

#xy_boundaryObject



251
252
253
# File 'lib/space_time_id.rb', line 251

def xy_boundary
  xy_four_corners << xy if xy?
end

#xy_boxObject



243
244
245
# File 'lib/space_time_id.rb', line 243

def xy_box
  [xy, [next_x, next_y]] if xy?
end

#xy_childrenObject



221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/space_time_id.rb', line 221

def xy_children
  raise "No children for level zero!" if level == 0
  @xy_children ||= begin
    bottom_left = dup(id, level: level - 1)
    @xy_children = []
    (0...xy_expansion).each do |h|
      (0...xy_expansion).each do |v|
        @xy_children << bottom_left.dup([bottom_left.next_x(h), bottom_left.next_y(v)])
      end
    end
    @xy_children
  end if xy?
end

#xy_descendantsObject



235
236
237
# File 'lib/space_time_id.rb', line 235

def xy_descendants
  xy_children + (level == 1 ? [] : xy_children.map(&:xy_descendants).flatten) if xy?
end

#xy_expansionObject



187
188
189
# File 'lib/space_time_id.rb', line 187

def xy_expansion
  options[:xy_expansion]
end

#xy_four_cornersObject



247
248
249
# File 'lib/space_time_id.rb', line 247

def xy_four_corners
  [xy, [next_x, y], [next_x, next_y], [x, next_y]] if xy?
end

#xy_id_2dObject



163
164
165
# File 'lib/space_time_id.rb', line 163

def xy_id_2d
  xy.map { |c| digitize(c, xy_step).round(dec) } if xy?
end

#xy_id_2d_centerObject



171
172
173
# File 'lib/space_time_id.rb', line 171

def xy_id_2d_center
  xy_id_2d.map { |c| (c + (xy_step / 2.0)).round(dec * 2) } if xy?
end

#xy_id_strObject



167
168
169
# File 'lib/space_time_id.rb', line 167

def xy_id_str
  xy.map { |c| digitize_str(c, xy_step, dec) } .join("_") if xy?
end

#xy_id_str_centerObject



175
176
177
# File 'lib/space_time_id.rb', line 175

def xy_id_str_center
  xy_id_2d_center.map { |c| digitize_str(c, (xy_step / 2.0), dec * 2) } .join("_") if xy?
end

#xy_neighborsObject



203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/space_time_id.rb', line 203

def xy_neighbors
  @xy_neighbors ||= begin
    topLeft      = dup([next_x(-1), next_y(1) ])
    top          = dup([x         , next_y(1) ])
    topRight     = dup([next_x(1) , next_y(1) ])
    right        = dup([next_x(1) , y         ])
    bottomRight  = dup([next_x(1) , next_y(-1)])
    bottom       = dup([x         , next_y(-1)])
    bottomLeft   = dup([next_x(-1), next_y(-1)])
    left         = dup([next_x(-1), y         ])
    @xy_neighbors = [topLeft, top, topRight, right, bottomRight, bottom, bottomLeft, left]
  end if xy?
end

#xy_parentObject



217
218
219
# File 'lib/space_time_id.rb', line 217

def xy_parent
  dup(id, level: level + 1) if xy?
end

#xy_siblingsObject



239
240
241
# File 'lib/space_time_id.rb', line 239

def xy_siblings
  xy_parent.xy_children if xy?
end

#xy_stepObject



179
180
181
# File 'lib/space_time_id.rb', line 179

def xy_step
  (xy_expansion ** level) * xy_base_step
end