Class: SpaceTimeId
- Inherits:
-
Object
- Object
- SpaceTimeId
- Extended by:
- InheritableAttribute
- Defined in:
- lib/space_time_id.rb
Instance Attribute Summary collapse
-
#interval ⇒ Object
Returns the value of attribute interval.
-
#level ⇒ Object
Returns the value of attribute level.
-
#options ⇒ Object
Returns the value of attribute options.
-
#ts_sec ⇒ Object
Returns the value of attribute ts_sec.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #dec ⇒ Object
- #dup(new_id = id, options = self.options) ⇒ Object
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
- #id ⇒ Object
-
#initialize(*args) ⇒ SpaceTimeId
constructor
A new instance of SpaceTimeId.
- #inspect ⇒ Object
-
#next_ts(i = 1) ⇒ Object
# # # # # # # # # # # # T I M E R E L A T I O N S # # # # # # # # # # # #.
-
#next_x(i = 1) ⇒ Object
# # # # # # # # # # # S P A C E R E L A T I O N S # # # # # # # # # # # #.
- #next_y(i = 1) ⇒ Object
- #original_options ⇒ Object
- #to_html ⇒ Object
- #to_s ⇒ Object
- #ts ⇒ Object
-
#ts? ⇒ Boolean
# # # # # # # # # # # T I M E # # # # # # # # # # # # # # # # # # # # # # #.
- #ts_base_step ⇒ Object
- #ts_expansion ⇒ Object
- #ts_id ⇒ Object
- #ts_ms ⇒ Object
- #ts_neighbors ⇒ Object
- #ts_parent ⇒ Object
- #ts_step ⇒ Object
- #xy ⇒ Object
-
#xy? ⇒ Boolean
# # # # # # # # # # # S P A C E # # # # # # # # # # # # # # # # # # # # # #.
- #xy_base_step ⇒ Object
- #xy_boundary ⇒ Object
- #xy_box ⇒ Object
- #xy_children ⇒ Object
- #xy_descendants ⇒ Object
- #xy_expansion ⇒ Object
- #xy_four_corners ⇒ Object
- #xy_id_2d ⇒ Object
- #xy_id_2d_center ⇒ Object
- #xy_id_str ⇒ Object
- #xy_id_str_center ⇒ Object
- #xy_neighbors ⇒ Object
- #xy_parent ⇒ Object
- #xy_siblings ⇒ Object
- #xy_step ⇒ Object
Methods included from InheritableAttribute
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. = self.class..merge(args.) self.interval = .delete(:interval) || 0 self.level = .delete(:level) || 0 if args.length == 1 && args.first.is_a?(String) initialize(*args.first.split("_").map(&:to_f), ) elsif args.length == 1 && args.first.is_a?(Array) && args.first.length == 2 initialize(*(args.first), ) 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, ) else raise "Invalid initialize arguments! (#{args.inspect})" end end |
Instance Attribute Details
#interval ⇒ Object
Returns the value of attribute interval.
17 18 19 |
# File 'lib/space_time_id.rb', line 17 def interval @interval end |
#level ⇒ Object
Returns the value of attribute level.
18 19 20 |
# File 'lib/space_time_id.rb', line 18 def level @level end |
#options ⇒ Object
Returns the value of attribute options.
16 17 18 |
# File 'lib/space_time_id.rb', line 16 def @options end |
#ts_sec ⇒ Object
Returns the value of attribute ts_sec.
13 14 15 |
# File 'lib/space_time_id.rb', line 13 def ts_sec @ts_sec end |
#x ⇒ Object
Returns the value of attribute x.
14 15 16 |
# File 'lib/space_time_id.rb', line 14 def x @x end |
#y ⇒ Object
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 |
#dec ⇒ Object
96 97 98 |
# File 'lib/space_time_id.rb', line 96 def dec [: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, = self.) SpaceTimeId.new(new_id, .merge()) end |
#eql?(other) ⇒ Boolean
84 85 86 |
# File 'lib/space_time_id.rb', line 84 def eql?(other) other.is_a?(SpaceTimeId) && self == other end |
#hash ⇒ Object
88 89 90 |
# File 'lib/space_time_id.rb', line 88 def hash [id, interval, level].hash end |
#id ⇒ Object
53 54 55 |
# File 'lib/space_time_id.rb', line 53 def id [ts_id, xy_id_str].compact.join("_") end |
#inspect ⇒ Object
61 62 63 |
# File 'lib/space_time_id.rb', line 61 def inspect "<SpaceTimeId:#{id} #{.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_options ⇒ Object
100 101 102 |
# File 'lib/space_time_id.rb', line 100 def {level: level, interval: interval}.merge() end |
#to_html ⇒ Object
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_s ⇒ Object
57 58 59 |
# File 'lib/space_time_id.rb', line 57 def to_s id end |
#ts ⇒ Object
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 # # # # # # # # # # # # # # # # # # # # # # #
106 107 108 |
# File 'lib/space_time_id.rb', line 106 def ts? !ts_sec.nil? end |
#ts_base_step ⇒ Object
126 127 128 |
# File 'lib/space_time_id.rb', line 126 def ts_base_step [:ts_base_step] end |
#ts_expansion ⇒ Object
130 131 132 |
# File 'lib/space_time_id.rb', line 130 def ts_expansion [:ts_expansion] end |
#ts_id ⇒ Object
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_ms ⇒ Object
114 115 116 |
# File 'lib/space_time_id.rb', line 114 def ts_ms ts_sec * 1000 if ts_sec end |
#ts_neighbors ⇒ Object
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_parent ⇒ Object
149 150 151 |
# File 'lib/space_time_id.rb', line 149 def ts_parent dup(id, interval: interval + 1) if ts? end |
#ts_step ⇒ Object
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 |
#xy ⇒ Object
159 160 161 |
# File 'lib/space_time_id.rb', line 159 def xy [x, y].compact end |
#xy? ⇒ Boolean
# # # # # # # # # # # S P A C E # # # # # # # # # # # # # # # # # # # # # #
155 156 157 |
# File 'lib/space_time_id.rb', line 155 def xy? !xy.empty? end |
#xy_base_step ⇒ Object
183 184 185 |
# File 'lib/space_time_id.rb', line 183 def xy_base_step [:xy_base_step] end |
#xy_boundary ⇒ Object
251 252 253 |
# File 'lib/space_time_id.rb', line 251 def xy_boundary xy_four_corners << xy if xy? end |
#xy_box ⇒ Object
243 244 245 |
# File 'lib/space_time_id.rb', line 243 def xy_box [xy, [next_x, next_y]] if xy? end |
#xy_children ⇒ Object
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_descendants ⇒ Object
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_expansion ⇒ Object
187 188 189 |
# File 'lib/space_time_id.rb', line 187 def xy_expansion [:xy_expansion] end |
#xy_four_corners ⇒ Object
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_2d ⇒ Object
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_center ⇒ Object
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_str ⇒ Object
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_center ⇒ Object
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_neighbors ⇒ Object
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_parent ⇒ Object
217 218 219 |
# File 'lib/space_time_id.rb', line 217 def xy_parent dup(id, level: level + 1) if xy? end |
#xy_siblings ⇒ Object
239 240 241 |
# File 'lib/space_time_id.rb', line 239 def xy_siblings xy_parent.xy_children if xy? end |
#xy_step ⇒ Object
179 180 181 |
# File 'lib/space_time_id.rb', line 179 def xy_step (xy_expansion ** level) * xy_base_step end |