2007-03-20 20:54:52 +00:00
# geo functions
# -------------------------------------------------------------------------------------------------
2009-03-18 18:26:30 +00:00
#
#
# geo.Coord class
# -------------------------------------------------------------------------------------------------
#
2007-03-20 20:54:52 +00:00
# geo.Coord.new([<coord>]) ... class that holds and maintains geographical coordinates
# can be initialized with another geo.Coord instance
#
2009-03-18 18:26:30 +00:00
# SETTER METHODS:
#
# .set(<coord>) ... sets coordinates from another geo.Coord instance
#
# .set_lat(<num>) ... functions for setting latitude/longitude/altitude
# .set_lon(<num>)
# .set_alt(<num>)
# .set_latlon(<num>, <num> [, <num>]) (altitude is optional; default=0)
#
# .set_x(<num>) ... functions for setting cartesian x/y/z coordinates
# .set_y(<num>)
# .set_z(<num>)
# .set_xyz(<num>, <num>, <num>)
#
#
# GETTER METHODS:
#
# .lat()
# .lon() ... functions for getting lat/lon/alt
# .alt() ... returns altitude in m
# .latlon() ... returns vector [<lat>, <lon>, <alt>]
2007-03-20 20:54:52 +00:00
#
2009-03-18 18:26:30 +00:00
# .x() ... functions for reading cartesian coords (in m)
# .y()
# .z()
# .xyz() ... returns vector [<x>, <y>, <z>]
2007-03-20 20:54:52 +00:00
#
#
2009-03-18 18:26:30 +00:00
# QUERY METHODS:
2007-03-20 20:54:52 +00:00
#
2009-03-18 18:26:30 +00:00
# .is_defined() ... returns whether the coords are defined
# .dump() ... outputs coordinates
# .course_to(<coord>) ... returns course to another geo.Coord instance (degree)
# .distance_to(<coord>) ... returns distance in m along Earth curvature, ignoring altitudes
2007-03-20 20:54:52 +00:00
# useful for map distance
2009-03-18 18:26:30 +00:00
# .direct_distance_to(<coord>) ... distance in m direct, considers altitude,
2007-05-05 10:09:13 +00:00
# but cuts through Earth surface
2007-03-20 20:54:52 +00:00
#
2009-03-18 18:26:30 +00:00
#
# MANIPULATION METHODS:
#
# .apply_course_distance(<course>, <distance>) ... guess what
#
#
#
#
# -------------------------------------------------------------------------------------------------
2007-03-20 20:54:52 +00:00
#
# geo.aircraft_position() ... returns current aircraft position as geo.Coord
2009-03-18 18:26:30 +00:00
# geo.viewer_position() ... returns viewer position as geo.Coord
2007-03-20 20:54:52 +00:00
# geo.click_position() ... returns last click coords as geo.Coord or nil before first click
#
2007-06-18 10:22:17 +00:00
# geo.tile_path(<lat>, <lon>) ... returns tile path string (e.g. "w130n30/w123n37/942056.stg")
2009-05-19 20:02:43 +00:00
# geo.elevation(<lat>, <lon> [, <top:10000>])
# ... returns elevation in meter for given lat/lon, or nil on error;
# <top> is the altitude at which the intersection test starts
#
2013-06-17 19:40:16 +00:00
# geo.normdeg(<angle>) ... returns angle normalized to 0 <= angle < 360
# geo.normdeg180(<angle>) ... returns angle normalized to -180 < angle <= 360
2007-05-31 20:19:26 +00:00
#
2007-06-18 10:22:17 +00:00
# geo.put_model(<path>, <lat>, <lon> [, <elev:nil> [, <hdg:0> [, <pitch:0> [, <roll:0>]]]]);
# ... put model <path> at location <lat>/<lon> with given elevation
2007-05-31 20:19:26 +00:00
# (optional, default: surface). <hdg>/<pitch>/<roll> are optional
# and default to zero.
2007-06-18 11:06:10 +00:00
# geo.put_model(<path>, <coord> [, <hdg:0> [, <pitch:0> [, <roll:0>]]]);
# ... same as above, but lat/lon/elev are taken from a Coord object
2007-03-20 20:54:52 +00:00
2009-03-18 18:26:30 +00:00
var EPSILON = 1e-15;
2007-03-20 20:54:52 +00:00
var ERAD = 6378138.12; # Earth radius (m)
2009-03-18 18:26:30 +00:00
var floor = func(v) v < 0.0 ? -int(-v) - 1 : int(v);
2007-03-20 20:54:52 +00:00
2007-06-18 10:22:17 +00:00
# class that maintains one set of geographical coordinates
2007-03-20 20:54:52 +00:00
#
var Coord = {
2009-03-18 18:26:30 +00:00
new: func(copy = nil) {
2007-03-20 20:54:52 +00:00
var m = { parents: [Coord] };
m._pdirty = 1; # polar
m._cdirty = 1; # cartesian
2007-06-18 10:22:17 +00:00
m._lat = nil; # in radian
m._lon = nil; #
2007-03-20 20:54:52 +00:00
m._alt = nil; # ASL
m._x = nil; # in m
m._y = nil;
m._z = nil;
2007-06-18 10:33:37 +00:00
if (copy != nil)
2007-03-20 20:54:52 +00:00
m.set(copy);
return m;
},
2009-03-18 18:26:30 +00:00
_cupdate: func {
2007-03-20 20:54:52 +00:00
me._cdirty or return;
2007-06-16 18:26:06 +00:00
var xyz = geodtocart(me._lat * R2D, me._lon * R2D, me._alt);
me._x = xyz[0];
me._y = xyz[1];
me._z = xyz[2];
2007-03-20 20:54:52 +00:00
me._cdirty = 0;
},
2009-03-18 18:26:30 +00:00
_pupdate: func {
2007-03-20 20:54:52 +00:00
me._pdirty or return;
2007-06-16 18:26:06 +00:00
var lla = carttogeod(me._x, me._y, me._z);
me._lat = lla[0] * D2R;
me._lon = lla[1] * D2R;
me._alt = lla[2];
2007-03-20 20:54:52 +00:00
me._pdirty = 0;
},
2009-03-18 18:26:30 +00:00
x: func { me._cupdate(); me._x },
y: func { me._cupdate(); me._y },
z: func { me._cupdate(); me._z },
xyz: func { me._cupdate(); [me._x, me._y, me._z] },
2007-03-20 20:54:52 +00:00
2009-03-18 18:26:30 +00:00
lat: func { me._pupdate(); me._lat * R2D }, # return in degree
lon: func { me._pupdate(); me._lon * R2D },
alt: func { me._pupdate(); me._alt },
latlon: func { me._pupdate(); [me._lat * R2D, me._lon * R2D, me._alt] },
2007-03-20 20:54:52 +00:00
2009-03-18 18:26:30 +00:00
set_x: func(x) { me._pupdate(); me._pdirty = 1; me._x = x; me },
set_y: func(y) { me._pupdate(); me._pdirty = 1; me._y = y; me },
set_z: func(z) { me._pupdate(); me._pdirty = 1; me._z = z; me },
2007-03-20 20:54:52 +00:00
2009-03-18 18:26:30 +00:00
set_lat: func(lat) { me._cupdate(); me._cdirty = 1; me._lat = lat * D2R; me },
set_lon: func(lon) { me._cupdate(); me._cdirty = 1; me._lon = lon * D2R; me },
set_alt: func(alt) { me._cupdate(); me._cdirty = 1; me._alt = alt; me },
2007-03-20 20:54:52 +00:00
2009-03-18 18:26:30 +00:00
set: func(c) {
2007-03-20 20:54:52 +00:00
c._pupdate();
me._lat = c._lat;
2007-06-18 10:22:17 +00:00
me._lon = c._lon;
2007-03-20 20:54:52 +00:00
me._alt = c._alt;
me._cdirty = 1;
me._pdirty = 0;
me;
},
2009-03-18 18:26:30 +00:00
set_latlon: func(lat, lon, alt = 0) {
2007-03-20 20:54:52 +00:00
me._lat = lat * D2R;
2007-06-18 10:22:17 +00:00
me._lon = lon * D2R;
2007-03-20 20:54:52 +00:00
me._alt = alt;
me._cdirty = 1;
me._pdirty = 0;
me;
},
2009-03-18 18:26:30 +00:00
set_xyz: func(x, y, z) {
2007-03-20 20:54:52 +00:00
me._x = x;
me._y = y;
me._z = z;
me._pdirty = 1;
me._cdirty = 0;
me;
},
2009-03-18 18:26:30 +00:00
apply_course_distance: func(course, dist) {
2007-03-20 20:54:52 +00:00
me._pupdate();
course *= D2R;
dist /= ERAD;
2013-04-10 20:25:59 +00:00
if (dist < 0.0) {
dist = abs(dist);
course = course - math.pi;
}
2008-12-19 13:41:25 +00:00
me._lat = math.asin(math.sin(me._lat) * math.cos(dist)
+ math.cos(me._lat) * math.sin(dist) * math.cos(course));
2007-03-20 20:54:52 +00:00
2008-12-19 13:41:25 +00:00
if (math.cos(me._lat) > EPSILON)
me._lon = math.pi - math.mod(math.pi - me._lon
- math.asin(math.sin(course) * math.sin(dist)
/ math.cos(me._lat)), 2 * math.pi);
2007-06-18 10:33:37 +00:00
2007-03-20 20:54:52 +00:00
me._cdirty = 1;
me;
},
2009-03-18 18:26:30 +00:00
course_to: func(dest) {
2007-03-20 20:54:52 +00:00
me._pupdate();
dest._pupdate();
2007-06-18 10:33:37 +00:00
if (me._lat == dest._lat and me._lon == dest._lon)
2007-03-20 20:54:52 +00:00
return 0;
2007-06-18 10:33:37 +00:00
2007-03-20 20:54:52 +00:00
var dlon = dest._lon - me._lon;
2009-03-18 18:26:30 +00:00
var ret = nil;
call(func ret = math.mod(math.atan2(math.sin(dlon) * math.cos(dest._lat),
2008-12-19 13:41:25 +00:00
math.cos(me._lat) * math.sin(dest._lat)
- math.sin(me._lat) * math.cos(dest._lat)
2009-03-18 18:26:30 +00:00
* math.cos(dlon)), 2 * math.pi) * R2D, nil, var err = []);
if (size(err)) {
debug.printerror(err);
debug.dump(me._lat, me._lon, dlon, dest._lat, dest._lon, "--------------------------");
}
return ret;
2007-03-20 20:54:52 +00:00
},
# arc distance on an earth sphere; doesn't consider altitude
2009-03-18 18:26:30 +00:00
distance_to: func(dest) {
2007-03-20 20:54:52 +00:00
me._pupdate();
dest._pupdate();
2007-06-18 10:33:37 +00:00
if (me._lat == dest._lat and me._lon == dest._lon)
2007-03-20 20:54:52 +00:00
return 0;
2007-06-18 10:33:37 +00:00
2008-12-19 13:41:25 +00:00
var a = math.sin((me._lat - dest._lat) * 0.5);
var o = math.sin((me._lon - dest._lon) * 0.5);
return 2.0 * ERAD * math.asin(math.sqrt(a * a + math.cos(me._lat)
* math.cos(dest._lat) * o * o));
2007-03-20 20:54:52 +00:00
},
2009-03-18 18:26:30 +00:00
direct_distance_to: func(dest) {
2007-03-20 20:54:52 +00:00
me._cupdate();
dest._cupdate();
var dx = dest._x - me._x;
var dy = dest._y - me._y;
var dz = dest._z - me._z;
2008-12-19 13:41:25 +00:00
return math.sqrt(dx * dx + dy * dy + dz * dz);
2007-03-20 20:54:52 +00:00
},
2009-03-18 18:26:30 +00:00
is_defined: func {
2007-03-20 20:54:52 +00:00
return !(me._cdirty and me._pdirty);
},
2009-03-18 18:26:30 +00:00
dump: func {
2007-06-18 10:33:37 +00:00
if (me._cdirty and me._pdirty)
2009-03-18 18:26:30 +00:00
print("Coord.dump(): coordinates undefined");
2007-06-18 10:33:37 +00:00
2007-03-20 20:54:52 +00:00
me._cupdate();
me._pupdate();
2007-06-18 10:22:17 +00:00
printf("x=%f y=%f z=%f lat=%f lon=%f alt=%f",
me.x(), me.y(), me.z(), me.lat(), me.lon(), me.alt());
2007-03-20 20:54:52 +00:00
},
};
# normalize degree to 0 <= angle < 360
#
var normdeg = func(angle) {
2007-06-18 10:33:37 +00:00
while (angle < 0)
2007-03-20 20:54:52 +00:00
angle += 360;
2007-06-18 10:33:37 +00:00
while (angle >= 360)
2007-03-20 20:54:52 +00:00
angle -= 360;
return angle;
}
2013-06-17 19:40:16 +00:00
# normalize degree to -180 < angle <= 180
#
var normdeg180 = func(angle) {
while (angle <= -180)
angle += 360;
while (angle > 180)
angle -= 360;
return angle;
}
2007-03-20 20:54:52 +00:00
2007-06-18 10:22:17 +00:00
var tile_index = func(lat, lon) {
2012-04-25 23:01:47 +00:00
return tileIndex(lat, lon);
2007-03-20 20:54:52 +00:00
}
2007-06-18 10:22:17 +00:00
var format = func(lat, lon) {
2007-03-20 20:54:52 +00:00
sprintf("%s%03d%s%02d", lon < 0 ? "w" : "e", abs(lon), lat < 0 ? "s" : "n", abs(lat));
}
2007-06-18 10:22:17 +00:00
var tile_path = func(lat, lon) {
2012-04-25 23:01:47 +00:00
var p = tilePath(lat, lon) ~ "/" ~ tileIndex(lat, lon) ~ ".stg";
2007-03-20 20:54:52 +00:00
}
2007-06-22 18:49:38 +00:00
2007-06-18 11:06:10 +00:00
var put_model = func(path, c, arg...) {
call(_put_model, [path] ~ (isa(c, Coord) ? c.latlon() : [c]) ~ arg);
}
2007-03-20 20:54:52 +00:00
2007-06-22 18:49:38 +00:00
2007-06-18 11:06:10 +00:00
var _put_model = func(path, lat, lon, elev_m = nil, hdg = 0, pitch = 0, roll = 0) {
2007-05-12 18:38:57 +00:00
if (elev_m == nil)
2007-06-18 10:22:17 +00:00
elev_m = elevation(lat, lon);
2007-05-12 18:38:57 +00:00
if (elev_m == nil)
2008-11-20 20:18:39 +00:00
die("geo.put_model(): cannot get elevation for " ~ lat ~ "/" ~ lon);
2008-11-20 11:24:46 +00:00
fgcommand("add-model", var n = props.Node.new({ "path": path,
"latitude-deg": lat, "longitude-deg": lon, "elevation-m": elev_m,
"heading-deg": hdg, "pitch-deg": pitch, "roll-deg": roll,
}));
return props.globals.getNode(n.getNode("property").getValue());
2007-05-12 18:38:57 +00:00
}
2009-03-18 18:26:30 +00:00
var elevation = func(lat, lon, maxalt = 10000) {
var d = geodinfo(lat, lon, maxalt);
2007-06-23 15:28:08 +00:00
return d == nil ? nil : d[0];
2007-05-03 22:09:34 +00:00
}
2007-03-20 20:54:52 +00:00
var click_coord = Coord.new();
_setlistener("/sim/signals/click", func {
2008-12-19 13:41:25 +00:00
var lat = getprop("/sim/input/click/latitude-deg");
var lon = getprop("/sim/input/click/longitude-deg");
var elev = getprop("/sim/input/click/elevation-m");
2007-06-18 10:22:17 +00:00
click_coord.set_latlon(lat, lon, elev);
2007-03-20 20:54:52 +00:00
});
var click_position = func {
2007-05-12 18:38:57 +00:00
return click_coord.is_defined() ? Coord.new(click_coord) : nil;
2007-03-20 20:54:52 +00:00
}
2008-12-19 13:41:25 +00:00
var aircraft_position = func {
var lat = getprop("/position/latitude-deg");
var lon = getprop("/position/longitude-deg");
var alt = getprop("/position/altitude-ft") * FT2M;
return Coord.new().set_latlon(lat, lon, alt);
}
2007-03-20 20:54:52 +00:00
2007-06-18 15:15:16 +00:00
2008-12-19 13:41:25 +00:00
var viewer_position = func {
var x = getprop("/sim/current-view/viewer-x-m");
var y = getprop("/sim/current-view/viewer-y-m");
var z = getprop("/sim/current-view/viewer-z-m");
return Coord.new().set_xyz(x, y, z);
}
2007-03-20 20:54:52 +00:00
2013-12-01 12:29:22 +00:00
# A object to handle differential positioned searches:
# searchCmd executes and returns the actual search,
# onAdded and onRemoved are callbacks,
# and obj is a "me" reference (defaults to "me" in the
2014-04-29 02:26:32 +00:00
# caller's namespace). If searchCmd returns nil, nothing
# happens, i.e. the diff is cancelled.
2013-12-01 12:29:22 +00:00
var PositionedSearch = {
new: func(searchCmd, onAdded, onRemoved, obj=nil) {
return {
parents:[PositionedSearch],
obj: obj == nil ? caller(1)[0]["me"] : obj,
searchCmd: searchCmd,
onAdded: onAdded,
onRemoved: onRemoved,
result: [],
};
},
_equals: func(a,b) {
2014-01-27 02:18:30 +00:00
return a == b; # positioned objects are created once
#return (a == b or a.id == b.id);
},
condense: func(vec) {
var ret = [];
foreach (var e; vec)
if (e != nil) append(ret, e);
return ret;
},
diff: func(old, new) {
2014-04-29 02:26:32 +00:00
if (new == nil)
return [old, [], []];
2014-01-27 02:18:30 +00:00
var removed = old~[]; #copyvec
var added = new~[];
# Mark common elements from removed and added:
forindex (OUTER; var i; removed)
forindex (var j; new)
if (removed[i] != nil and added[j] != nil and me._equals(removed[i], added[j])) {
removed[i] = added[j] = nil;
continue OUTER;
}
# And remove those common elements, returning the result:
return [new, me.condense(removed), me.condense(added)];
2013-12-01 12:29:22 +00:00
},
update: func(searchCmd=nil) {
if (searchCmd == nil) searchCmd = me.searchCmd;
2014-01-27 02:18:30 +00:00
if (me._equals == PositionedSearch._equals) {
# Optimized search using C code
var old = me.result~[]; #copyvec
me.result = call(searchCmd, nil, me.obj);
2014-04-29 02:26:32 +00:00
if (me.result == nil)
{ me.result = old; return }
if (typeof(me.result) != 'vector') die("geo.PositionedSearch(): A searchCmd must return a vector of elements or nil !!"); # TODO: Maybe make this a hash instead to wrap a vector, so that we can implement basic type-checking - e.g. doing isa(PositionedSearchResult, me.result) would be kinda neat and could help troubleshooting
else
2014-01-27 02:18:30 +00:00
positioned.diff( old,
me.result,
func call(me.onAdded, arg, me.obj),
func call(me.onRemoved, arg, me.obj) );
} else {
(me.result, var removed, var added) = me.diff(me.result, call(searchCmd, nil, me.obj));
foreach (var e; removed)
call(me.onRemoved, [e], me.obj);
foreach (var e; added)
call(me.onAdded, [e], me.obj);
}
2013-12-01 12:29:22 +00:00
},
# this is the worst case scenario: switching from 640 to 320 (or vice versa)
test: func(from=640, to=320) {
var s= geo.PositionedSearch.new(
func positioned.findWithinRange(from, 'fix'),
func print('added:', arg[0].id),
func print('removed:', arg[0].id)
);
debug.benchmark('Toggle '~from~'nm/'~to~'nm', func {
s.update();
s.update( func positioned.findWithinRange(to, 'fix') );
}); # ~ takes
}, # of test
};