cross-links: refactored collision test
This commit is contained in:
parent
f47c9bb234
commit
e40249fcd0
@ -18,48 +18,53 @@
|
|||||||
|
|
||||||
// PLUGIN START ////////////////////////////////////////////////////////
|
// PLUGIN START ////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
|
||||||
window.plugin.crossLinks = function() {};
|
window.plugin.crossLinks = function() {};
|
||||||
|
|
||||||
// Great Circle Arc Intersection
|
/* Great Circle Arc Intersection
|
||||||
// http://geospatialmethods.org/spheres/GCAIntersect.html
|
Conecpt in short:
|
||||||
function intersect(a, b) {
|
- build a plane of each arc (p1,p2,center)
|
||||||
var PI = Math.PI,
|
- find intersection line and intersection points on sphere
|
||||||
radians = PI / 180,
|
- check if a point are on both arcs
|
||||||
ε = 1e-6;
|
see: http://geospatialmethods.org/spheres/GCAIntersect.html
|
||||||
|
*/
|
||||||
|
var PI = Math.PI;
|
||||||
|
var radians = PI / 180;
|
||||||
|
var near_0 = 1e-6;
|
||||||
|
|
||||||
var λ0 = a[0][0],
|
function greatCircleArcIntersect(a0,a1,b0,b1) {
|
||||||
λ1 = a[1][0],
|
|
||||||
λ2 = b[0][0],
|
// Order points
|
||||||
λ3 = b[1][0],
|
if (a1.lat < a0.lat) { var t=a1;a1=a0;a0=t;}
|
||||||
|
if (b1.lat < b0.lat) { var t=b1;b1=b0;b0=t;}
|
||||||
|
|
||||||
|
var λ0 = a0.lat,
|
||||||
|
λ1 = a1.lat,
|
||||||
|
λ2 = b0.lat,
|
||||||
|
λ3 = b1.lat,
|
||||||
δλ0 = λ1 - λ0,
|
δλ0 = λ1 - λ0,
|
||||||
δλ1 = λ3 - λ2,
|
δλ1 = λ3 - λ2,
|
||||||
aδλ0 = Math.abs(δλ0),
|
sλ0 = δλ0 > 180,
|
||||||
aδλ1 = Math.abs(δλ1),
|
sλ1 = δλ1 > 180,
|
||||||
sλ0 = aδλ0 > 180,
|
φ0 = a0.lng * radians,
|
||||||
sλ1 = aδλ1 > 180,
|
φ1 = a1.lng * radians,
|
||||||
φ0 = a[0][1] * radians,
|
φ2 = b0.lng * radians,
|
||||||
φ1 = a[1][1] * radians,
|
φ3 = b1.lng * radians,
|
||||||
φ2 = b[0][1] * radians,
|
|
||||||
φ3 = b[1][1] * radians,
|
|
||||||
t;
|
t;
|
||||||
|
|
||||||
// Ensure λ0 ≤ λ1 and λ2 ≤ λ3.
|
|
||||||
if (δλ0 < 0) t = λ0, λ0 = λ1, λ1 = t, t = φ0, φ0 = φ1, φ1 = t;
|
|
||||||
if (δλ1 < 0) t = λ2, λ2 = λ3, λ3 = t, t = φ2, φ2 = φ3, φ3 = t;
|
|
||||||
|
|
||||||
// Check if longitude ranges overlap.
|
// Check if longitude ranges overlap.
|
||||||
// TODO handle antimeridian crossings.
|
// TODO handle antimeridian crossings.
|
||||||
if (!sλ0 && !sλ1 && (λ0 > λ3 || λ2 > λ1)) return;
|
if (!sλ0 && !sλ1 && (λ0 > λ3 || λ2 > λ1)) return;
|
||||||
|
|
||||||
// Check for polar endpoints.
|
// Check for polar endpoints.
|
||||||
if (Math.abs(Math.abs(φ0) - PI / 2) < ε) λ0 = λ1, aδλ0 = δλ0 = 0, sλ0 = false;
|
if (Math.abs(Math.abs(φ0) - PI / 2) < near_0) λ0 = λ1, δλ0 = 0, sλ0 = false;
|
||||||
if (Math.abs(Math.abs(φ1) - PI / 2) < ε) λ1 = λ0, aδλ0 = δλ0 = 0, sλ0 = false;
|
if (Math.abs(Math.abs(φ1) - PI / 2) < near_0) λ1 = λ0, δλ0 = 0, sλ0 = false;
|
||||||
if (Math.abs(Math.abs(φ2) - PI / 2) < ε) λ2 = λ3, aδλ1 = δλ1 = 0, sλ1 = false;
|
if (Math.abs(Math.abs(φ2) - PI / 2) < near_0) λ2 = λ3, δλ1 = 0, sλ1 = false;
|
||||||
if (Math.abs(Math.abs(φ3) - PI / 2) < ε) λ3 = λ2, aδλ1 = δλ1 = 0, sλ1 = false;
|
if (Math.abs(Math.abs(φ3) - PI / 2) < near_0) λ3 = λ2, δλ1 = 0, sλ1 = false;
|
||||||
|
|
||||||
// Check for arcs along meridians.
|
// Check for arcs along meridians.
|
||||||
var m0 = aδλ0 < ε || Math.abs(aδλ0 - 180) < ε,
|
var m0 = δλ0 < near_0 || Math.abs(δλ0 - 180) < near_0,
|
||||||
m1 = aδλ1 < ε || Math.abs(aδλ1 - 180) < ε;
|
m1 = δλ1 < near_0 || Math.abs(δλ1 - 180) < near_0;
|
||||||
|
|
||||||
λ0 *= radians, λ1 *= radians, λ2 *= radians, λ3 *= radians;
|
λ0 *= radians, λ1 *= radians, λ2 *= radians, λ3 *= radians;
|
||||||
|
|
||||||
@ -100,19 +105,25 @@ function intersect(a, b) {
|
|||||||
Ny = n0z * n1x - n0x * n1z,
|
Ny = n0z * n1x - n0x * n1z,
|
||||||
Nz = n0x * n1y - n0y * n1x;
|
Nz = n0x * n1y - n0y * n1x;
|
||||||
|
|
||||||
if (length(Nx, Ny, Nz) < ε) return;
|
if (length(Nx, Ny, Nz) < near_0) return;
|
||||||
|
|
||||||
var λ = Math.atan2(Ny, Nx);
|
var λ = Math.atan2(Ny, Nx);
|
||||||
if ((sλ0 ^ (λ0 <= λ && λ <= λ1) || m0 && Math.abs(λ - λ0) < ε) && (sλ1 ^ (λ2 <= λ && λ <= λ3) || m1 && Math.abs(λ - λ2) < ε) || (Nz = -Nz,
|
if ( (sλ0 ^ (λ0 <= λ && λ <= λ1) || m0 && Math.abs(λ - λ0) < near_0) && (sλ1 ^ (λ2 <= λ && λ <= λ3) || m1 && Math.abs(λ - λ2) < near_0) || (Nz = -Nz,
|
||||||
(sλ0 ^ (λ0 <= (λ = (λ + 2 * PI) % (2 * PI) - PI) && λ <= λ1) || m0 && Math.abs(λ - λ0) < ε) && (sλ1 ^ (λ2 <= λ && λ <= λ3) || m1 && Math.abs(λ - λ2) < ε))) {
|
(sλ0 ^ (λ0 <= (λ = (λ + 2 * PI) % (2 * PI) - PI) && λ <= λ1) || m0 && Math.abs(λ - λ0) < near_0) && (sλ1 ^ (λ2 <= λ && λ <= λ3) || m1 && Math.abs(λ - λ2) < near_0))) {
|
||||||
|
|
||||||
var φ = Math.asin(Nz / length(Nx, Ny, Nz));
|
var φ = Math.asin(Nz / length(Nx, Ny, Nz));
|
||||||
|
|
||||||
if (m0 || m1) {
|
if (m0 || m1) {
|
||||||
if (m1) φ0 = φ2, φ1 = φ3, λ0 = λ2, λ1 = λ3, aδλ0 = aδλ1;
|
if (m1) φ0 = φ2, φ1 = φ3, λ0 = λ2, λ1 = λ3, δλ0 = δλ1;
|
||||||
if (aδλ0 > ε) return φ0 + φ1 > 0 ^ φ < (Math.abs(λ - λ0) < ε ? φ0 : φ1) ? [λ / radians, φ / radians] : null;
|
|
||||||
|
if (δλ0 > near_0)
|
||||||
|
return (φ0 + φ1 > 0 ^ φ < (Math.abs(λ - λ0) < near_0 ? φ0 : φ1)) ? [λ / radians, φ / radians] : null;
|
||||||
|
|
||||||
// Ensure φ0 ≤ φ1.
|
// Ensure φ0 ≤ φ1.
|
||||||
if (φ1 < φ0) t = φ0, φ0 = φ1, φ1 = t;
|
if (φ1 < φ0) t = φ0, φ0 = φ1, φ1 = t;
|
||||||
return Math.abs(λ - (m0 ? λ0 : λ2)) < ε && φ0 <= φ && φ <= φ1 ? [λ / radians, φ / radians] : null;
|
return (Math.abs(λ - (m0 ? λ0 : λ2)) < near_0 && φ0 <= φ && φ <= φ1) ? [λ / radians, φ / radians] : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return [λ / radians, φ / radians];
|
return [λ / radians, φ / radians];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -121,38 +132,25 @@ function length(x, y, z) {
|
|||||||
return Math.sqrt(x * x + y * y + z * z);
|
return Math.sqrt(x * x + y * y + z * z);
|
||||||
}
|
}
|
||||||
|
|
||||||
window.plugin.crossLinks.testPolyLine = function (polyline, link) {
|
/*
|
||||||
var a= [[link[0].lat,link[0].lng],[link[1].lat,link[1].lng]];
|
smallCircleIntersect
|
||||||
for (var i=0;i<polyline.length-1;++i) {
|
idea:
|
||||||
|
- build the plane of the small circle: normalvector (center) and p=center+radian // E:n1*x+n2*y+n3*z=n1*p1+n2*p2+n3*p3
|
||||||
|
- calc distance to both points // d=(n1*x+n2*y+n3*z- (n1*p1+n2*p2+n3*p3)) / length(n)
|
||||||
|
- both >0 = inside ; one >0 = collision
|
||||||
|
*/
|
||||||
|
|
||||||
var b= [[polyline[i].lat,polyline[i].lng],[polyline[i+1].lat,polyline[i+1].lng]];
|
window.plugin.crossLinks.testPolyLine = function (polyline, link,closed) {
|
||||||
if (intersect(a,b)) return true;
|
|
||||||
|
var a = link.getLatLngs();
|
||||||
|
var b = polyline.getLatLngs();
|
||||||
|
|
||||||
|
for (var i=0;i<b.length-1;++i) {
|
||||||
|
if (greatCircleArcIntersect(a[0],a[1],b[i],b[i+1])) return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
if (closed) {
|
||||||
}
|
if (greatCircleArcIntersect(a[0],a[1],b[b.length],b[0])) return true;
|
||||||
|
|
||||||
function isPointInPolygon(poly, point) {
|
|
||||||
// src: http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
|
|
||||||
var c = false;
|
|
||||||
var p2 = poly[0];
|
|
||||||
for (var i = 1; i < poly.length; ++i) {
|
|
||||||
var p1 = poly[i];
|
|
||||||
if ( ((p1.lng > point.lng) != (p2.lng>point.lng)) &&
|
|
||||||
(point.lat < (p2.lat - p1.lat)*(point.lng-p1.lng) / (p2.lng-p1.lng) + p1.lat))
|
|
||||||
c = !c;
|
|
||||||
|
|
||||||
p2 = p1;
|
|
||||||
}
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
|
|
||||||
window.plugin.crossLinks.testPolygons = function (polygons, link) {
|
|
||||||
|
|
||||||
var linkline= L.geodesicConvertLines(link._latlngs,0);
|
|
||||||
|
|
||||||
for (var pidx=0;pidx<polygons.length;++pidx) {
|
|
||||||
if (isPointInPolygon(polygons[pidx],linkline[0])) return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@ -161,61 +159,38 @@ window.plugin.crossLinks.testPolygons = function (polygons, link) {
|
|||||||
window.plugin.crossLinks.onLinkAdded = function (data) {
|
window.plugin.crossLinks.onLinkAdded = function (data) {
|
||||||
if (window.plugin.crossLinks.disabled) return;
|
if (window.plugin.crossLinks.disabled) return;
|
||||||
|
|
||||||
var link = data.link;
|
plugin.crossLinks.testLink(data.link);
|
||||||
|
|
||||||
window.plugin.drawTools.drawnItems.eachLayer( function(layer) {
|
|
||||||
if (layer instanceof L.GeodesicPolygon) {
|
|
||||||
latlngs = layer.getLatLngs();
|
|
||||||
latlngs.push(latlngs[0]);
|
|
||||||
if (window.plugin.crossLinks.testPolyLine(latlngs, link.getLatLngs())) {
|
|
||||||
plugin.crossLinks.showLink(link.getLatLngs());
|
|
||||||
}
|
|
||||||
// TODO: rework inside-polygons
|
|
||||||
/*
|
|
||||||
var polyline= [drawline];
|
|
||||||
} else if (window.plugin.crossLinks.testPolygons([drawline], link)) {
|
|
||||||
link.setStyle (window.plugin.crossLinks.STYLE_INSIDEPOLY);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
} else if (layer instanceof L.GeodesicPolyline) {
|
|
||||||
|
|
||||||
if (window.plugin.crossLinks.testPolyLine(layer.getLatLngs(), link.getLatLngs())) {
|
|
||||||
plugin.crossLinks.showLink(link.getLatLngs());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
window.plugin.crossLinks.checkAllLinks = function() {
|
window.plugin.crossLinks.checkAllLinks = function() {
|
||||||
console.debug("Cross-Links: checking links");
|
|
||||||
if (window.plugin.crossLinks.disabled) return;
|
if (window.plugin.crossLinks.disabled) return;
|
||||||
|
|
||||||
|
console.debug("Cross-Links: checking all links");
|
||||||
plugin.crossLinks.linkLayer.clearLayers();
|
plugin.crossLinks.linkLayer.clearLayers();
|
||||||
|
|
||||||
// check all links
|
|
||||||
$.each(window.links, function(guid, link) {
|
$.each(window.links, function(guid, link) {
|
||||||
|
plugin.crossLinks.testLink(link);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
window.plugin.crossLinks.testLink = function (link) {
|
||||||
window.plugin.drawTools.drawnItems.eachLayer( function(layer) {
|
window.plugin.drawTools.drawnItems.eachLayer( function(layer) {
|
||||||
if (layer instanceof L.GeodesicPolyline) {
|
if (layer instanceof L.GeodesicPolygon) {
|
||||||
if (window.plugin.crossLinks.testPolyLine(layer.getLatLngs(), link.getLatLngs())) {
|
if (window.plugin.crossLinks.testPolyLine(layer, link,true)) {
|
||||||
plugin.crossLinks.showLink(link.getLatLngs());
|
plugin.crossLinks.showLink(link);
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
} else if (layer instanceof L.GeodesicPolygon) {
|
} else if (layer instanceof L.GeodesicPolyline) {
|
||||||
latlngs = layer.getLatLngs();
|
if (window.plugin.crossLinks.testPolyLine(layer, link)) {
|
||||||
latlngs.push(latlngs[0]);
|
plugin.crossLinks.showLink(link);
|
||||||
if (window.plugin.crossLinks.testPolyLine(latlngs, link.getLatLngs())) {
|
|
||||||
plugin.crossLinks.showLink(link.getLatLngs());
|
|
||||||
}
|
}
|
||||||
// TODO: rework inside-polygons
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
window.plugin.crossLinks.showLink = function(latlngs) {
|
|
||||||
|
|
||||||
var poly = L.geodesicPolyline(latlngs, {
|
window.plugin.crossLinks.showLink = function(link) {
|
||||||
|
|
||||||
|
var poly = L.geodesicPolyline(link.getLatLngs(), {
|
||||||
color: '#f11',
|
color: '#f11',
|
||||||
opacity: 0.7,
|
opacity: 0.7,
|
||||||
weight: 4,
|
weight: 4,
|
||||||
@ -223,7 +198,7 @@ window.plugin.crossLinks.showLink = function(latlngs) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
poly.addTo(plugin.crossLinks.linkLayer);
|
poly.addTo(plugin.crossLinks.linkLayer);
|
||||||
};
|
}
|
||||||
|
|
||||||
window.plugin.crossLinks.onMapDataRefreshEnd = function () {
|
window.plugin.crossLinks.onMapDataRefreshEnd = function () {
|
||||||
if (window.plugin.crossLinks.reorderLinkLayer) {
|
if (window.plugin.crossLinks.reorderLinkLayer) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user