Class: Route

Inherits:
Object
  • Object
show all
Defined in:
lib/route.rb

Constant Summary collapse

WAYPOINT_ATTRIBUTES =
{
  :position => 1,
  :time => 2,
  :heart_rate => 4,
  :altitude => 8
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRoute

Returns a new instance of Route.



16
17
18
19
20
21
# File 'lib/route.rb', line 16

def initialize
  LOGGER.debug "initializing new route"
  @segments = []
  @distance = 0
  @elapsed_time = 0
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



9
10
11
# File 'lib/route.rb', line 9

def attributes
  @attributes
end

#distanceObject (readonly)

Returns the value of attribute distance.



9
10
11
# File 'lib/route.rb', line 9

def distance
  @distance
end

#elapsed_timeObject (readonly)

Returns the value of attribute elapsed_time.



9
10
11
# File 'lib/route.rb', line 9

def elapsed_time
  @elapsed_time
end

#extra_waypoints_attributes_lengthObject (readonly)

Returns the value of attribute extra_waypoints_attributes_length.



9
10
11
# File 'lib/route.rb', line 9

def extra_waypoints_attributes_length
  @extra_waypoints_attributes_length
end

#segmentsObject (readonly)

Returns the value of attribute segments.



9
10
11
# File 'lib/route.rb', line 9

def segments
  @segments
end

Class Method Details

.from_data(data) ⇒ Object



12
13
14
# File 'lib/route.rb', line 12

def self.from_data(data)
  new.read_data(data)
end

Instance Method Details

#add_distance(dist) ⇒ Object



77
78
79
# File 'lib/route.rb', line 77

def add_distance(dist)
  @distance += dist
end

#add_elapsed_time(time) ⇒ Object



81
82
83
# File 'lib/route.rb', line 81

def add_elapsed_time(time)
  @elapsed_time += time
end

#calculate_parametersObject



34
35
36
# File 'lib/route.rb', line 34

def calculate_parameters
  segments.each{|s| s.calculate_waypoints}
end

#distance_from_parameterized_location(location) ⇒ Object



53
54
55
56
57
# File 'lib/route.rb', line 53

def distance_from_parameterized_location(location)
  return unless location
  w0, w1, t = waypoints_and_parameter_from_parameterized_location(location)
  w0.distance + t * (w1.distance - w0.distance)
end

#has_attribute?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/route.rb', line 30

def has_attribute?(attribute)
  0 != (attributes & WAYPOINT_ATTRIBUTES[attribute])
end

#parameterized_location_from_time(time) ⇒ Object



38
39
40
41
# File 'lib/route.rb', line 38

def parameterized_location_from_time(time)
  return unless segment = segment_for_time(time)
  segment.parameterized_location_from_time(time)
end

#position_from_parameterized_location(location) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/route.rb', line 43

def position_from_parameterized_location(location)
  return unless location
  w0, w1, t = waypoints_and_parameter_from_parameterized_location(location)

  LongLat.new(
    w0.position.longitude + t * (w1.position.longitude - w0.position.longitude),
    w0.position.latitude + t * (w1.position.latitude - w0.position.latitude)
  )
end

#read_data(data) ⇒ Object



23
24
25
26
27
28
# File 'lib/route.rb', line 23

def read_data(data)
  @attributes = BinData::Uint16le.read(data)
  @extra_waypoints_attributes_length = BinData::Uint16be.read(data)
  read_segments_from(data)
  self
end

#waypoints_and_parameter_from_parameterized_location(location) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/route.rb', line 59

def waypoints_and_parameter_from_parameterized_location(location)
  return unless location && segment = segments[location.segment_index]

  waypoints = segment.waypoints

  value = location.value.to_i

  if value >= waypoints.size - 1
    value = waypoints.size - 2
  end

  t = location.value - value

  waypoints.size < 2 ?
    [waypoints[0], waypoints[0], 0] :
    [waypoints[value], waypoints[value + 1], t]
end