Class: RouteSegment
- Inherits:
-
Object
- Object
- RouteSegment
- Defined in:
- lib/route_segment.rb
Constant Summary collapse
- EPSILON =
0.001
Instance Attribute Summary collapse
-
#last_time ⇒ Object
Returns the value of attribute last_time.
-
#route ⇒ Object
Returns the value of attribute route.
-
#waypoints ⇒ Object
readonly
Returns the value of attribute waypoints.
Instance Method Summary collapse
- #add_waypoint(*points) ⇒ Object
- #calculate_waypoints ⇒ Object
- #has_time?(time) ⇒ Boolean
- #index ⇒ Object
-
#initialize(route, data = nil) ⇒ RouteSegment
constructor
A new instance of RouteSegment.
- #parameterized_location_from_time(time) ⇒ Object
- #read_waypoints(data) ⇒ Object
Constructor Details
#initialize(route, data = nil) ⇒ RouteSegment
Returns a new instance of RouteSegment.
7 8 9 10 11 12 13 |
# File 'lib/route_segment.rb', line 7 def initialize(route, data = nil) LOGGER.debug "Initializing route segment" @route = route @waypoints = [] read_waypoints(data) if data end |
Instance Attribute Details
#last_time ⇒ Object
Returns the value of attribute last_time.
4 5 6 |
# File 'lib/route_segment.rb', line 4 def last_time @last_time end |
#route ⇒ Object
Returns the value of attribute route.
4 5 6 |
# File 'lib/route_segment.rb', line 4 def route @route end |
#waypoints ⇒ Object (readonly)
Returns the value of attribute waypoints.
5 6 7 |
# File 'lib/route_segment.rb', line 5 def waypoints @waypoints end |
Instance Method Details
#add_waypoint(*points) ⇒ Object
25 26 27 28 29 |
# File 'lib/route_segment.rb', line 25 def add_waypoint(*points) points.each do |point| waypoints << point end end |
#calculate_waypoints ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/route_segment.rb', line 31 def calculate_waypoints segment_distance = 0 waypoints.each_with_index do |wp, idx| segment_distance += (idx == 0 ? 0 : wp.distance_to(waypoints[idx - 1])) wp.distance = route.distance + segment_distance wp.elapsed_time = route.elapsed_time + wp.time - waypoints.first.time end @route.add_distance(segment_distance) @route.add_elapsed_time(waypoints.last.time - waypoints.first.time) end |
#has_time?(time) ⇒ Boolean
74 75 76 |
# File 'lib/route_segment.rb', line 74 def has_time?(time) ((waypoints.first.time - EPSILON)..(waypoints.last.time + EPSILON)).include?(time) end |
#index ⇒ Object
43 44 45 |
# File 'lib/route_segment.rb', line 43 def index @route.segments.index(self) end |
#parameterized_location_from_time(time) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/route_segment.rb', line 47 def parameterized_location_from_time(time) lower, upper = 0, waypoints.size - 1 # Binary search to find the closest waypoint while lower <= upper idx = lower + (upper - lower) / 2 currtime = waypoints[idx].time if (time - currtime).abs < EPSILON return ParameterizedLocation.new(index, idx) end if time < currtime upper = idx - 1 else lower = idx + 1 end end t0 = waypoints[upper].time t1 = waypoints[lower].time if t1 == t0 return ParameterizedLocation.new(index, upper) end ParameterizedLocation.new(index, upper + (time - t0) / (t1 - t0)) end |
#read_waypoints(data) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/route_segment.rb', line 15 def read_waypoints(data) waypoint_count = BinData::Uint32le.read(data) LOGGER.debug "reading #{waypoint_count} waypoints" waypoint_count.times do |j| waypoint = Waypoint.new(self, data) add_waypoint(waypoint) @last_time = waypoint.time end end |