Class: RAGE::LocationsManager
- Inherits:
-
Object
- Object
- RAGE::LocationsManager
- Includes:
- Singleton
- Defined in:
- lib/rage/location.rb
Overview
Singleton class managing all locations
Instance Attribute Summary collapse
-
#locations ⇒ Object
Array of locations being managed.
Instance Method Summary collapse
-
#add(location) ⇒ Object
Add location to be managed.
-
#boundaries ⇒ Object
Return the maxima and minima of the x,y,z coordinate values for all the locations being managed.
-
#center(metric = :mean) ⇒ Object
Return the center coordinate of location system.
-
#clear ⇒ Object
Empty the array of managed locations.
-
#initialize ⇒ LocationsManager
constructor
A new instance of LocationsManager.
Constructor Details
#initialize ⇒ LocationsManager
Returns a new instance of LocationsManager.
85 86 87 |
# File 'lib/rage/location.rb', line 85 def initialize @locations = [] end |
Instance Attribute Details
#locations ⇒ Object
Array of locations being managed
83 84 85 |
# File 'lib/rage/location.rb', line 83 def locations @locations end |
Instance Method Details
#add(location) ⇒ Object
Add location to be managed
90 91 92 |
# File 'lib/rage/location.rb', line 90 def add(location) @locations.push location unless @locations.include? location end |
#boundaries ⇒ Object
Return the maxima and minima of the x,y,z coordinate values for all the locations being managed. Return value is an array of six values as follows
max_x, max_y, max_z, min_x, min_y, min_z
This method essentially gives you the “box” in which all locations being managed are in
103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/rage/location.rb', line 103 def boundaries max_x = max_y = max_z = min_x = min_y = min_z = 0 @locations.each { |loc| locb = loc.boundaries max_x = locb[0] if locb[0] > max_x max_y = locb[1] if locb[1] > max_y max_z = locb[2] if locb[2] > max_z min_x = locb[3] if locb[3] < min_x min_y = locb[4] if locb[4] < min_y min_z = locb[5] if locb[5] < min_z } return max_x, max_y, max_z, min_x, min_y, min_z end |
#center(metric = :mean) ⇒ Object
Return the center coordinate of location system. Must specify the metric which to generate center, which may be either
-
:avg - compute and return bounaries averages
-
:mean - compute and return mean of all coords
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/rage/location.rb', line 121 def center(metric = :mean) if metric == :mean ls = @locations.size mx = my = mz = 0 @locations.each { |lm| mx += lm.x / ls my += lm.y / ls mz += lm.z / ls } return mx,my,mz elsif metric == :avg b = self.boundaries return (b[0] + b[3]) / 2, (b[1] + b[4]) / 2, (b[2] + b[5]) / 2 end end |
#clear ⇒ Object
Empty the array of managed locations
95 96 97 |
# File 'lib/rage/location.rb', line 95 def clear @locations.clear end |