Commit d2df398a authored by Alfakhori's avatar Alfakhori
Browse files

optimisation

parent 6d8f61ae
Pipeline #12467 passed with stage
in 26 seconds
This diff is collapsed.
......@@ -4,6 +4,18 @@ let userIcon;
let intervalId;
let modeMarkers = [];
// PERFORMANCE: Reuse one Vector3 instead of allocating a new
// Vector3 every time updateMap() runs.
let cameraPosition = new THREE.Vector3();
// PERFORMANCE: Cache the map window element once.
let mapWindow = document.getElementById("map-window");
// PERFORMANCE: Remember the last mapped camera position so Leaflet
// is updated only after the user has actually moved.
let lastMapX = null;
let lastMapZ = null;
// ============================================================
// Proposed plan overlay
// ============================================================
......@@ -24,476 +36,289 @@ let planVisible = false;
// Longitude = 8.010410216
const planBounds = [
[48.405858385, 7.997080070], // Southwest / bottom-left
[48.414966828, 8.010410216] // Northeast / top-right
[48.405858385, 7.99708007],
[48.414966828, 8.010410216],
];
const earthRadius = 6378137; // Erdradius in Metern (WGS84)
const earthRadius = 6378137;
// ============================================================
// Initialize map
// ============================================================
function init_map() {
const mapContainer = document.getElementById("map-container");
const mapContainer = document.getElementById("map-container");
if (mapContainer) {
if (!init) {
// ====================================================
// Create Leaflet map
// ====================================================
mymap = L.map(mapContainer);
// ====================================================
// OpenStreetMap Germany basemap
// ====================================================
var OpenStreetMap_DE = L.tileLayer(
'https://tile.openstreetmap.de/{z}/{x}/{y}.png',
{
//maxZoom: 20,
attribution:
'&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}
);
OpenStreetMap_DE.addTo(mymap);
// ====================================================
// User icon
// ====================================================
userIcon = L.icon({
iconUrl: 'assets/previewImages/map-user.png',
iconSize: [32, 32],
iconAnchor: [16, 32],
popupAnchor: [0, -32]
});
// ====================================================
// Initial map position
// ====================================================
//
// The map is centered on the proposed plan instead
// of the user's current GPS position.
//
// We use fitBounds() so the whole plan is visible.
//
mymap.fitBounds(planBounds, {
padding: [20, 20]
});
// ====================================================
// User position marker
// ====================================================
userIcon = L.marker(
[geoLocation.latitude, geoLocation.longitude],
{
icon: userIcon
}
)
.bindPopup('Ihre Position')
.addTo(mymap);
init = true;
}
// ========================================================
// Remove old model markers
// ========================================================
modeMarkers.forEach(marker => {
marker.remove();
});
// Clear marker array
modeMarkers = [];
// ========================================================
// Add currently placed models to map
// ========================================================
const models = getAllPlacedModels();
models.forEach(model => {
const { lat, lng } = threeToLeaflet(
model.position.x,
model.position.z
);
if (mapContainer) {
if (!init) {
mymap = L.map(mapContainer);
var OpenStreetMap_DE = L.tileLayer(
"https://tile.openstreetmap.de/{z}/{x}/{y}.png",
{
attribution:
'&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
},
);
OpenStreetMap_DE.addTo(mymap);
userIcon = L.icon({
iconUrl: "assets/previewImages/map-user.png",
iconSize: [32, 32],
iconAnchor: [16, 32],
popupAnchor: [0, -32],
});
mymap.fitBounds(planBounds, {
padding: [20, 20],
});
userIcon = L.marker([geoLocation.latitude, geoLocation.longitude], {
icon: userIcon,
})
.bindPopup("Ihre Position")
.addTo(mymap);
init = true;
}
const modelMarker = L.marker(
[lat, lng],
{
icon: L.icon({
iconUrl: model.modelConfig.image,
iconSize: [16, 16],
iconAnchor: [16, 32],
popupAnchor: [0, -32]
})
}
)
.addTo(mymap)
.bindPopup(
model.modelConfig?.name || "Unbenannt"
);
// ========================================================
// Remove old model markers
// ========================================================
modeMarkers.forEach((marker) => {
marker.remove();
});
modeMarkers.push(modelMarker);
});
modeMarkers = [];
// ========================================================
// Add currently placed models to map
// ========================================================
// ========================================================
// Add proposed plan
// ========================================================
const models = getAllPlacedModels();
addPlanOverlay();
models.forEach((model) => {
const { lat, lng } = threeToLeaflet(model.position.x, model.position.z);
const modelMarker = L.marker([lat, lng], {
icon: L.icon({
iconUrl: model.modelConfig.image,
iconSize: [16, 16],
iconAnchor: [16, 32],
popupAnchor: [0, -32],
}),
})
.addTo(mymap)
.bindPopup(model.modelConfig?.name || "Unbenannt");
// ========================================================
// Make sure Leaflet knows the map container dimensions
// ========================================================
modeMarkers.push(modelMarker);
});
setTimeout(() => {
// ========================================================
// Add proposed plan
// ========================================================
centerOnPlan();
addPlanOverlay();
}, 100);
// ========================================================
// Make sure Leaflet knows the map container dimensions
// ========================================================
setTimeout(() => {
centerOnPlan();
}, 100);
// ========================================================
// Update map position continuously
// ========================================================
// ========================================================
// Update map position continuously
// ========================================================
intervalId = setInterval(updateMap, 500);
// PERFORMANCE: Start only one map update interval.
if (!intervalId) {
intervalId = setInterval(updateMap, 500);
}
}
}
// ============================================================
// Center map on proposed plan
// ============================================================
function centerOnPlan() {
if (!mymap) {
console.error("Map ist noch nicht initialisiert.");
if (!mymap) {
console.error(
"Map ist noch nicht initialisiert."
);
return;
}
// Tell Leaflet to recalculate the map container size.
// This is important because map-window may initially
// be hidden when the map is initialized.
mymap.invalidateSize();
return;
}
mymap.invalidateSize();
// Center and zoom the map on the proposed plan.
mymap.fitBounds(
planBounds,
{
padding: [20, 20]
}
);
mymap.fitBounds(planBounds, {
padding: [20, 20],
});
}
// ============================================================
// Add georeferenced proposed plan
// ============================================================
//
// Original coordinate system:
// DHDN / Gauss-Krüger Zone 3
// EPSG:31467
//
// Image extent:
//
// Southwest:
// X = 3425822.00
// Y = 5363602.00
//
// Northeast:
// X = 3426822.00
// Y = 5364602.00
//
// Converted to WGS84:
//
// Southwest:
// Latitude = 48.405858385
// Longitude = 7.997080070
//
// Northeast:
// Latitude = 48.414966828
// Longitude = 8.010410216
//
// Image:
//
// assets/plans/proposed-plan.png
// ============================================================
function addPlanOverlay() {
if (!mymap) {
console.error("Map ist noch nicht initialisiert.");
if (!mymap) {
console.error(
"Map ist noch nicht initialisiert."
);
return;
}
// ========================================================
// If overlay already exists, show it
// ========================================================
if (planOverlay) {
planOverlay.addTo(mymap);
planVisible = true;
return;
}
return;
}
// ========================================================
// Create image overlay
// ========================================================
planOverlay = L.imageOverlay(
'assets/plans/proposed-plan.png',
planBounds,
{
// Transparency
opacity: 0.7,
if (planOverlay) {
planOverlay.addTo(mymap);
// The plan does not capture mouse/touch events
interactive: false
}
);
planVisible = true;
return;
}
// ========================================================
// Add overlay to map
// ========================================================
planOverlay = L.imageOverlay("assets/plans/proposed-plan.png", planBounds, {
opacity: 0.7,
interactive: false,
});
planOverlay.addTo(mymap);
planOverlay.addTo(mymap);
planVisible = true;
planVisible = true;
}
// ============================================================
// Update map
// ============================================================
function updateMap() {
// The interval is normally stopped by closeMap().
// This guard also handles cases where the map is hidden
// by another UI action.
if (mapWindow && mapWindow.style.display === "none" && intervalId) {
clearInterval(intervalId);
if (
document.getElementById("map-window").style.display === 'none'
&& intervalId
) {
clearInterval(intervalId);
intervalId = null;
return;
}
intervalId = null;
return;
}
let vec = new THREE.Vector3();
if (!camera || !userIcon) {
return;
}
const userPosition = camera.getWorldPosition(vec);
// PERFORMANCE: Reuse the same Vector3 object.
camera.getWorldPosition(cameraPosition);
// PERFORMANCE: Do not update Leaflet if the user
// has not moved.
if (
lastMapX !== null &&
Math.abs(cameraPosition.x - lastMapX) < 0.1 &&
Math.abs(cameraPosition.z - lastMapZ) < 0.1
) {
return;
}
const { lat, lng } = threeToLeaflet(
userPosition.x,
userPosition.z
);
lastMapX = cameraPosition.x;
lastMapZ = cameraPosition.z;
const { lat, lng } = threeToLeaflet(cameraPosition.x, cameraPosition.z);
if (userIcon) {
userIcon.setLatLng([
lat,
lng
]);
}
userIcon.setLatLng([lat, lng]);
}
// ============================================================
// Convert Three.js coordinates to Leaflet coordinates
// ============================================================
function threeToLeaflet(threeX, threeZ) {
if (!geoLocation) {
console.error("GeoLocation ist noch nicht verfügbar");
if (!geoLocation) {
console.error(
"GeoLocation ist noch nicht verfügbar"
);
return;
}
const originLat = geoLocation.latitude;
const originLng = geoLocation.longitude;
const deltaLat =
(threeZ / earthRadius) *
(180 / Math.PI);
return;
}
const deltaLng =
(
threeX /
(
earthRadius *
Math.cos(
Math.PI * originLat / 180
)
)
) *
(180 / Math.PI);
const originLat = geoLocation.latitude;
const originLng = geoLocation.longitude;
const globalLat =
originLat + deltaLat;
const deltaLat = (threeZ / earthRadius) * (180 / Math.PI);
const deltaLng =
(threeX / (earthRadius * Math.cos((Math.PI * originLat) / 180))) *
(180 / Math.PI);
const globalLng =
originLng + deltaLng;
const globalLat = originLat + deltaLat;
const globalLng = originLng + deltaLng;
return {
lat: globalLat,
lng: globalLng
};
return {
lat: globalLat,
lng: globalLng,
};
}
// ============================================================
// Convert Leaflet coordinates to Three.js coordinates
// ============================================================
function leafletToThree(lat, lng) {
if (!geoLocation) {
console.error("GeoLocation ist noch nicht verfügbar");
if (!geoLocation) {
console.error(
"GeoLocation ist noch nicht verfügbar"
);
return;
}
const originLat = geoLocation.latitude;
const originLng = geoLocation.longitude;
const deltaLat =
lat - originLat;
return;
}
const originLat = geoLocation.latitude;
const deltaLng =
lng - originLng;
const originLng = geoLocation.longitude;
const deltaLat = lat - originLat;
const threeZ =
deltaLat *
(Math.PI / 180) *
earthRadius;
const deltaLng = lng - originLng;
const threeZ = deltaLat * (Math.PI / 180) * earthRadius;
const threeX =
deltaLng *
(Math.PI / 180) *
earthRadius *
Math.cos(
Math.PI * originLat / 180
);
const threeX =
deltaLng *
(Math.PI / 180) *
earthRadius *
Math.cos((Math.PI * originLat) / 180);
return {
x: threeX,
z: threeZ
};
return {
x: threeX,
z: threeZ,
};
}
// ============================================================
// Round number
// ============================================================
function roundTo(n, digits) {
var negative = false;
var negative = false;
if (digits === undefined) {
digits = 0;
}
if (digits === undefined) {
digits = 0;
}
if (n < 0) {
negative = true;
n = n * -1;
}
if (n < 0) {
negative = true;
n = n * -1;
}
var multiplicator = Math.pow(10, digits);
var multiplicator =
Math.pow(10, digits);
n = parseFloat((n * multiplicator).toFixed(11));
n = (Math.round(n) / multiplicator).toFixed(digits);
n = parseFloat(
(n * multiplicator).toFixed(11)
);
if (negative) {
n = (n * -1).toFixed(digits);
}
n =
(
Math.round(n) /
multiplicator
).toFixed(digits);
if (negative) {
n =
(n * -1).toFixed(digits);
}
return parseFloat(n);
}
\ No newline at end of file
return parseFloat(n);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment