// utils.js - Utility functions for coordinate conversion import * as THREE from 'three'; import { CONFIG } from './constants.js'; // Convert latitude/longitude to 3D vector export function latLonToVector3(lat, lon, radius = CONFIG.earthRadius) { const phi = (90 - lat) * (Math.PI / 180); const theta = (lon + 180) * (Math.PI / 180); const x = -(radius * Math.sin(phi) * Math.cos(theta)); const z = radius * Math.sin(phi) * Math.sin(theta); const y = radius * Math.cos(phi); return new THREE.Vector3(x, y, z); } // Convert 3D vector to latitude/longitude export function vector3ToLatLon(vector) { const radius = Math.sqrt(vector.x * vector.x + vector.y * vector.y + vector.z * vector.z); const lat = 90 - (Math.acos(vector.y / radius) * 180 / Math.PI); const lon = (Math.atan2(vector.z, -vector.x) * 180 / Math.PI) - 180; return { lat: parseFloat(lat.toFixed(4)), lon: parseFloat(lon.toFixed(4)), alt: radius - CONFIG.earthRadius }; } // Convert screen coordinates to Earth surface 3D coordinates export function screenToEarthCoords(x, y, camera, earth) { const raycaster = new THREE.Raycaster(); const mouse = new THREE.Vector2( (x / window.innerWidth) * 2 - 1, -(y / window.innerHeight) * 2 + 1 ); raycaster.setFromCamera(mouse, camera); const intersects = raycaster.intersectObject(earth); if (intersects.length > 0) { return intersects[0].point; } return null; } // Calculate simplified distance between two points export function calculateDistance(lat1, lon1, lat2, lon2) { const dx = lon2 - lon1; const dy = lat2 - lat1; return Math.sqrt(dx * dx + dy * dy); }