2020-12-16 16:54:41 +00:00
|
|
|
/*
|
2021-07-25 14:12:40 +00:00
|
|
|
* Copyright 2021 James Lyne
|
2020-12-16 16:54:41 +00:00
|
|
|
*
|
|
|
|
* Some portions of this file were taken from https://github.com/webbukkit/dynmap.
|
|
|
|
* These portions are Copyright 2020 Dynmap Contributors.
|
|
|
|
*
|
2021-07-25 14:12:40 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2020-12-16 16:54:41 +00:00
|
|
|
*
|
2021-07-25 14:12:40 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2020-12-16 16:54:41 +00:00
|
|
|
*
|
2021-07-25 14:12:40 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2020-12-16 16:54:41 +00:00
|
|
|
*/
|
|
|
|
|
2020-12-18 20:25:34 +00:00
|
|
|
import {LatLngExpression} from "leaflet";
|
2021-07-23 21:28:06 +00:00
|
|
|
import LiveAtlasPolyline from "@/leaflet/vector/LiveAtlasPolyline";
|
|
|
|
import LiveAtlasPolygon from "@/leaflet/vector/LiveAtlasPolygon";
|
2021-07-28 00:56:41 +00:00
|
|
|
import {Coordinate, LiveAtlasArea} from "@/index";
|
2021-07-29 02:06:05 +00:00
|
|
|
import {arePointsEqual, createPopup, isStyleEqual, tooltipOptions} from "@/util/paths";
|
2020-12-11 15:28:51 +00:00
|
|
|
|
2021-07-24 03:06:19 +00:00
|
|
|
export const createArea = (options: LiveAtlasArea, converter: Function): LiveAtlasPolyline | LiveAtlasPolygon => {
|
2020-12-11 15:28:51 +00:00
|
|
|
const outline = !options.style.fillOpacity || (options.style.fillOpacity <= 0),
|
2021-07-28 00:56:41 +00:00
|
|
|
points = options.points.map(projectPointsMapCallback, converter) as LatLngExpression[] | LatLngExpression[][],
|
2021-07-28 01:14:20 +00:00
|
|
|
area = outline ? new LiveAtlasPolyline(points, options) : new LiveAtlasPolygon(points, options);
|
2020-12-11 15:28:51 +00:00
|
|
|
|
2021-07-28 16:49:38 +00:00
|
|
|
if (options.popupContent) {
|
|
|
|
area.bindPopup(() => createPopup(options, 'AreaPopup'));
|
2020-12-11 15:28:51 +00:00
|
|
|
}
|
|
|
|
|
2021-07-29 02:06:05 +00:00
|
|
|
if (options.tooltipContent) {
|
|
|
|
area.bindTooltip(() => options.tooltipContent, tooltipOptions);
|
|
|
|
}
|
|
|
|
|
2020-12-11 15:28:51 +00:00
|
|
|
return area;
|
|
|
|
};
|
|
|
|
|
2021-07-24 03:06:19 +00:00
|
|
|
export const updateArea = (area: LiveAtlasPolyline | LiveAtlasPolygon | undefined, options: LiveAtlasArea, converter: Function): LiveAtlasPolyline | LiveAtlasPolygon => {
|
2020-12-11 15:28:51 +00:00
|
|
|
if (!area) {
|
|
|
|
return createArea(options, converter);
|
|
|
|
}
|
|
|
|
|
2021-07-28 00:56:41 +00:00
|
|
|
const points = options.points.map(projectPointsMapCallback, converter) as LatLngExpression[] | LatLngExpression[][],
|
|
|
|
oldPoints = area.getLatLngs();
|
|
|
|
|
2020-12-14 15:53:58 +00:00
|
|
|
let dirty = false;
|
|
|
|
|
|
|
|
//Avoid pointless setStyle() redrawing by checking if styles have actually changed
|
|
|
|
if(!isStyleEqual(area.options, options.style)) {
|
|
|
|
area.setStyle(options.style); //FIXME: Maybe override setStyle to add an option for not redrawing
|
|
|
|
dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!arePointsEqual(oldPoints.length === 1 ? oldPoints[0] : oldPoints, points)) {
|
|
|
|
area.setLatLngs(points);
|
|
|
|
dirty = true;
|
|
|
|
}
|
|
|
|
|
2021-01-26 14:42:31 +00:00
|
|
|
area.closePopup();
|
2020-12-11 15:28:51 +00:00
|
|
|
area.unbindPopup();
|
2021-07-28 16:49:38 +00:00
|
|
|
area.bindPopup(() => createPopup(options, 'AreaPopup'));
|
2020-12-14 15:53:58 +00:00
|
|
|
|
|
|
|
if(dirty) {
|
|
|
|
area.redraw();
|
|
|
|
}
|
2020-12-11 15:28:51 +00:00
|
|
|
|
|
|
|
return area;
|
|
|
|
};
|
|
|
|
|
2021-07-28 00:56:41 +00:00
|
|
|
const projectPointsMapCallback = function(this: Function, point: Coordinate | Coordinate[] | Coordinate[][]): LatLngExpression | LatLngExpression[] {
|
|
|
|
if(Array.isArray(point)) {
|
|
|
|
return point.map(projectPointsMapCallback, this) as LatLngExpression[];
|
|
|
|
} else {
|
|
|
|
// @ts-ignore
|
|
|
|
return this(point);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getPoints = (x: number[], y: [number, number], z: number[], outline: boolean): Coordinate[] | Coordinate[][] => {
|
|
|
|
if (x.length === 2) { /* Only 2 points */
|
|
|
|
if (y[0] === y[1]) {
|
|
|
|
return get2DBoxPoints(x, y, z, outline);
|
2020-12-11 15:28:51 +00:00
|
|
|
} else {
|
2021-07-28 00:56:41 +00:00
|
|
|
return get3DBoxPoints(x, y, z);
|
2020-12-11 15:28:51 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-07-28 00:56:41 +00:00
|
|
|
if (y[0] === y[1]) {
|
|
|
|
return get2DShapePoints(x, y, z, outline);
|
2020-12-11 15:28:51 +00:00
|
|
|
} else {
|
2021-07-28 00:56:41 +00:00
|
|
|
return get3DShapePoints(x, y, z);
|
2020-12-11 15:28:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-28 00:56:41 +00:00
|
|
|
export const get3DBoxPoints = (x: number[], y: [number, number], z: number[]): Coordinate[][] => {
|
|
|
|
const maxX = x[0],
|
|
|
|
minX = x[1],
|
|
|
|
maxY = y[0],
|
|
|
|
minY = y[1],
|
|
|
|
maxZ = z[0],
|
|
|
|
minZ = z[1];
|
2020-12-11 15:28:51 +00:00
|
|
|
|
|
|
|
return [
|
|
|
|
[
|
2021-07-28 00:56:41 +00:00
|
|
|
{x: minX, y: minY, z: minZ},
|
|
|
|
{x: maxX, y: minY, z: minZ},
|
|
|
|
{x: maxX, y: minY, z: maxZ},
|
|
|
|
{x: minX, y: minY, z: maxZ}
|
2020-12-11 15:28:51 +00:00
|
|
|
], [
|
2021-07-28 00:56:41 +00:00
|
|
|
{x: minX, y: maxY, z: minZ},
|
|
|
|
{x: maxX, y: maxY, z: minZ},
|
|
|
|
{x: maxX, y: maxY, z: maxZ},
|
|
|
|
{x: minX, y: maxY, z: maxZ}
|
2020-12-11 15:28:51 +00:00
|
|
|
], [
|
2021-07-28 00:56:41 +00:00
|
|
|
{x: minX, y: minY, z: minZ},
|
|
|
|
{x: minX, y: maxY, z: minZ},
|
|
|
|
{x: maxX, y: maxY, z: minZ},
|
|
|
|
{x: maxX, y: minY, z: minZ}
|
2020-12-11 15:28:51 +00:00
|
|
|
], [
|
2021-07-28 00:56:41 +00:00
|
|
|
{x: maxX, y: minY, z: minZ},
|
|
|
|
{x: maxX, y: maxY, z: minZ},
|
|
|
|
{x: maxX, y: maxY, z: maxZ},
|
|
|
|
{x: maxX, y: minY, z: maxZ}
|
2020-12-11 15:28:51 +00:00
|
|
|
], [
|
2021-07-28 00:56:41 +00:00
|
|
|
{x: minX, y: minY, z: maxZ},
|
|
|
|
{x: minX, y: maxY, z: maxZ},
|
|
|
|
{x: maxX, y: maxY, z: maxZ},
|
|
|
|
{x: maxX, y: minY, z: maxZ}
|
2020-12-11 15:28:51 +00:00
|
|
|
], [
|
2021-07-28 00:56:41 +00:00
|
|
|
{x: minX, y: minY, z: minZ},
|
|
|
|
{x: minX, y: maxY, z: minZ},
|
|
|
|
{x: minX, y: maxY, z: maxZ},
|
|
|
|
{x: minX, y: minY, z: maxZ}
|
2020-12-11 15:28:51 +00:00
|
|
|
]
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2021-07-28 00:56:41 +00:00
|
|
|
export const get2DBoxPoints = (x: number[], y: [number, number], z: number[], outline: boolean): Coordinate[] => {
|
|
|
|
const maxX = x[0],
|
|
|
|
minX = x[1],
|
|
|
|
minY = y[1],
|
|
|
|
maxZ = z[0],
|
|
|
|
minZ = z[1];
|
2020-12-11 15:28:51 +00:00
|
|
|
|
|
|
|
if (outline) {
|
|
|
|
return [
|
2021-07-28 00:56:41 +00:00
|
|
|
{x: minX, y: minY, z: minZ},
|
|
|
|
{x: maxX, y: minY, z: minZ},
|
|
|
|
{x: maxX, y: minY, z: maxZ},
|
|
|
|
{x: minX, y: minY, z: maxZ},
|
|
|
|
{x: minX, y: minY, z: minZ}
|
2020-12-11 15:28:51 +00:00
|
|
|
];
|
|
|
|
} else {
|
|
|
|
return [
|
2021-07-28 00:56:41 +00:00
|
|
|
{x: minX, y: minY, z: minZ},
|
|
|
|
{x: maxX, y: minY, z: minZ},
|
|
|
|
{x: maxX, y: minY, z: maxZ},
|
|
|
|
{x: minX, y: minY, z: maxZ}
|
2020-12-11 15:28:51 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-28 00:56:41 +00:00
|
|
|
export const get3DShapePoints = (x: number[], y: [number, number], z: number[]): Coordinate[][] => {
|
2020-12-11 15:28:51 +00:00
|
|
|
const toplist = [],
|
|
|
|
botlist = [],
|
|
|
|
polylist = [];
|
|
|
|
|
2021-07-28 00:56:41 +00:00
|
|
|
for (let i = 0; i < x.length; i++) {
|
|
|
|
toplist[i] = {x: x[i], y: y[0], z: z[i]};
|
|
|
|
botlist[i] = {x: x[i], y: y[1], z: z[i]};
|
2020-12-11 15:28:51 +00:00
|
|
|
}
|
|
|
|
|
2021-07-28 00:56:41 +00:00
|
|
|
for (let i = 0; i < x.length; i++) {
|
|
|
|
polylist[i] = [
|
|
|
|
toplist[i],
|
|
|
|
botlist[i],
|
|
|
|
botlist[(i + 1) % x.length],
|
|
|
|
toplist[(i + 1) % x.length],
|
|
|
|
];
|
2020-12-11 15:28:51 +00:00
|
|
|
}
|
|
|
|
|
2021-07-28 00:56:41 +00:00
|
|
|
polylist[x.length] = botlist;
|
|
|
|
polylist[x.length + 1] = toplist;
|
2020-12-11 15:28:51 +00:00
|
|
|
|
|
|
|
return polylist;
|
|
|
|
};
|
|
|
|
|
2021-07-28 00:56:41 +00:00
|
|
|
export const get2DShapePoints = (x: number[], y: [number, number], z: number[], outline: boolean): Coordinate[] => {
|
2020-12-11 15:28:51 +00:00
|
|
|
const points = [];
|
|
|
|
|
2021-07-28 00:56:41 +00:00
|
|
|
for (let i = 0; i < x.length; i++) {
|
|
|
|
points[i] = {x: x[i], y: y[1], z: z[i]};
|
2020-12-11 15:28:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (outline) {
|
|
|
|
points.push(points[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return points;
|
2021-07-28 00:56:41 +00:00
|
|
|
};
|