Class: SGS::Waypoint

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

Overview

Waypoint, Attractor, and Repellor definitions

Constant Summary collapse

@@count =
0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(location = nil, normal = 0.0, range = 0.1, name = "", attractor = true) ⇒ Waypoint

Define a new Attractor or Repellor, based on certain parameters. The location is the centre of the waypoint. The normal is the compass angle of the start of the semicircle, and the range is the size of the arc. You can specify an optional name for the waypoint and also indicate if we should be attracted or repelled by it.



73
74
75
76
77
78
79
80
81
# File 'lib/sgs/waypoint.rb', line 73

def initialize(location = nil, normal = 0.0, range = 0.1, name = "", attractor = true)
  @location = location || Location.new
  @normal = normal
  @range = range
  @name = name
  @attractor = attractor
  @bearing = nil
  @distance = 0
end

Instance Attribute Details

#attractorObject

Returns the value of attribute attractor.



43
44
45
# File 'lib/sgs/waypoint.rb', line 43

def attractor
  @attractor
end

#bearingObject (readonly)

Returns the value of attribute bearing.



44
45
46
# File 'lib/sgs/waypoint.rb', line 44

def bearing
  @bearing
end

#distanceObject (readonly)

Returns the value of attribute distance.



44
45
46
# File 'lib/sgs/waypoint.rb', line 44

def distance
  @distance
end

#locationObject

Returns the value of attribute location.



43
44
45
# File 'lib/sgs/waypoint.rb', line 43

def location
  @location
end

#nameObject

Returns the value of attribute name.



43
44
45
# File 'lib/sgs/waypoint.rb', line 43

def name
  @name
end

#normalObject

Returns the value of attribute normal.



43
44
45
# File 'lib/sgs/waypoint.rb', line 43

def normal
  @normal
end

#rangeObject

Returns the value of attribute range.



43
44
45
# File 'lib/sgs/waypoint.rb', line 43

def range
  @range
end

Class Method Details

.parse(data) ⇒ Object

Parse a waypoint from a hash. Uses the instance method to do the work.



50
51
52
53
54
# File 'lib/sgs/waypoint.rb', line 50

def self.parse(data)
  waypt = new
  waypt.parse(data)
  waypt
end

Instance Method Details

#attractor?Boolean

Is this an attractor?

Returns:

  • (Boolean)


104
105
106
# File 'lib/sgs/waypoint.rb', line 104

def attractor?
  @attractor == true
end

#compute_bearing(loc) ⇒ Object

Calculate the back-vector from the waypoint to the specified position. Calculate the adjusted distance between the position and the mark. Check to see if our back-bearing from the waypoint to our location is inside the chord of the waypoint, which is a semicircle commencing at the normal. If so, reduce the distance to the waypoint by the length of the chord. @distance is the adjusted distance to the location



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/sgs/waypoint.rb', line 90

def compute_bearing(loc)
  @bearing = loc - @location
  @distance = @bearing.distance
  d = Bearing.new(@bearing.back_angle - @normal, @bearing.distance)
  # A chord angle of 0 gives a semicircle from 0 to 180 degrees. If our
  # approach angle is within range, then reduce our distance to the mark
  # by the chord distance (range).
  @distance -= @range if d.angle >= 0.0 and d.angle < Math::PI
  @distance = 0.0 if @distance < 0.0
  @distance
end

#in_scope?Boolean

Is the waypoint in scope? In other words, is our angle inside the chord.

Returns:

  • (Boolean)


116
117
118
119
# File 'lib/sgs/waypoint.rb', line 116

def in_scope?
  puts "In-scope distance is %f..." % @distance
  @distance == 0.0
end

#normal_dObject

Convert the waypoint normal to/from degrees



123
124
125
# File 'lib/sgs/waypoint.rb', line 123

def normal_d
  Bearing.rtod @normal
end

#normal_d=(val) ⇒ Object

Convert the waypoint normal to/from degrees



129
130
131
# File 'lib/sgs/waypoint.rb', line 129

def normal_d=(val)
  @normal = Bearing.dtor val
end

#parse(data) ⇒ Object

 Parse the waypoint data and save it



58
59
60
61
62
63
64
65
# File 'lib/sgs/waypoint.rb', line 58

def parse(data)
  @@count += 1
  @name = data["name"] || "Waypoint ##{@@count}"
  @location = Location.new
  @location.parse_hash(data)
  @normal = data["normal"] || 0.0
  @range = data["range"] || 0.1
end

#repellor?Boolean

Is this a repellor?

Returns:

  • (Boolean)


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

def repellor?
  @attractor == true
end

#to_axis_kmlObject

Show the axis line for the waypoint (as a KML)



163
164
165
166
167
168
169
170
# File 'lib/sgs/waypoint.rb', line 163

def to_axis_kml
  puts "TO_AXIS_KML!"
  #::FIXME::
  #c2 = @chord.clone
  #c2.angle += 1.5 * Math::PI
  #pos1 = @location.calculate(c2)
  #"#{@location.to_kml(',')} #{pos1.to_kml(',')}"
end

#to_hashObject

Convert to a hash



135
136
137
138
139
140
141
# File 'lib/sgs/waypoint.rb', line 135

def to_hash
  hash = @location.to_hash
  hash["name"] = @name
  hash["normal"] = @normal
  hash["range"] = @range
  hash
end

#to_kmlObject

Display a string for a KML file



151
152
153
154
155
156
157
158
159
# File 'lib/sgs/waypoint.rb', line 151

def to_kml
  puts "TO KML!"
  #p self
  #c2 = @chord.clone
  #c2.angle += Math::PI
  #pos1 = @location.calculate(@chord)
  #pos2 = @location.calculate(c2)
  #"#{pos1.to_kml(',')} #{pos2.to_kml(',')}"
end

#to_sObject

Pretty version of the waypoint.



145
146
147
# File 'lib/sgs/waypoint.rb', line 145

def to_s
  "'#{@name}' at #{@location} => #{normal_d}%#{@range}"
end