91 lines
3.1 KiB
TypeScript
91 lines
3.1 KiB
TypeScript
/**
|
|||
|
|
* 导航计算
|
||
|
|
* 根据当前遥测数据和航线信息,计算 HUD / Overlay 所需的导航指标
|
||
|
|
*/
|
||
|
|
import { useMemo } from 'react';
|
||
|
|
import type { BoatRouteListVO, BoatTelemetryData, BoatWaypointVO } from '@/types/boatRoute';
|
||
|
|
|
||
|
|
interface NextWaypoint {
|
||
|
|
name: string;
|
||
|
|
distance: number;
|
||
|
|
estimatedTime: number;
|
||
|
|
}
|
||
|
|
|
||
|
|
interface NavigationDataResult {
|
||
|
|
/** 当前激活航线的航点列表 */
|
||
|
|
activeWaypoints: BoatWaypointVO[];
|
||
|
|
/** 当前激活航线的基本信息 */
|
||
|
|
activeRouteInfo: BoatRouteListVO | null;
|
||
|
|
/** 下一航点信息 */
|
||
|
|
nextWaypoint: NextWaypoint | undefined;
|
||
|
|
/** 剩余距离(米) */
|
||
|
|
remainingDistance: number;
|
||
|
|
/** 预计剩余时间(秒) */
|
||
|
|
estimatedRemainingTime: number;
|
||
|
|
/** 进度百分比 0-100 */
|
||
|
|
progress: number;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function useNavigationData(
|
||
|
|
activeRouteId: string | null,
|
||
|
|
routes: BoatRouteListVO[],
|
||
|
|
routeWaypointsMap: Record<string, BoatWaypointVO[]>,
|
||
|
|
boatData: BoatTelemetryData | null,
|
||
|
|
currentWaypointIndex: number,
|
||
|
|
): NavigationDataResult {
|
||
|
|
const activeWaypoints = useMemo(() => {
|
||
|
|
if (!activeRouteId) return [];
|
||
|
|
return routeWaypointsMap[activeRouteId] || [];
|
||
|
|
}, [activeRouteId, routeWaypointsMap]);
|
||
|
|
|
||
|
|
const activeRouteInfo = useMemo(() => {
|
||
|
|
if (!activeRouteId) return null;
|
||
|
|
return routes.find((r) => r.routeId === activeRouteId) || null;
|
||
|
|
}, [activeRouteId, routes]);
|
||
|
|
|
||
|
|
const { nextWaypoint, remainingDistance, estimatedRemainingTime, progress } = useMemo(() => {
|
||
|
|
if (!activeWaypoints.length || !boatData) {
|
||
|
|
return { nextWaypoint: undefined, remainingDistance: 0, estimatedRemainingTime: 0, progress: 0 };
|
||
|
|
}
|
||
|
|
|
||
|
|
const wpIdx = boatData.currentWaypointIndex ?? currentWaypointIndex;
|
||
|
|
const nextIdx = Math.min(wpIdx + 1, activeWaypoints.length - 1);
|
||
|
|
const nextWp = activeWaypoints[nextIdx];
|
||
|
|
|
||
|
|
// Haversine 距离计算
|
||
|
|
const R = 6371000;
|
||
|
|
const dLat = ((nextWp?.latitude ?? 0) - (boatData.latitude ?? 0)) * Math.PI / 180;
|
||
|
|
const dLon = ((nextWp?.longitude ?? 0) - (boatData.longitude ?? 0)) * Math.PI / 180;
|
||
|
|
const a = Math.sin(dLat / 2) ** 2 +
|
||
|
|
Math.cos((boatData.latitude ?? 0) * Math.PI / 180) *
|
||
|
|
Math.cos((nextWp?.latitude ?? 0) * Math.PI / 180) *
|
||
|
|
Math.sin(dLon / 2) ** 2;
|
||
|
|
const distToNext = R * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
||
|
|
|
||
|
|
const totalDist = activeRouteInfo?.totalDistance ?? 1;
|
||
|
|
const prog = Math.min(100, Math.round((wpIdx / Math.max(1, activeWaypoints.length - 1)) * 100));
|
||
|
|
const remainDist = totalDist * (1 - prog / 100);
|
||
|
|
const estTime = boatData.speed > 0 ? remainDist / boatData.speed : 0;
|
||
|
|
|
||
|
|
return {
|
||
|
|
nextWaypoint: nextWp ? {
|
||
|
|
name: nextWp.pointName || `航点${nextIdx + 1}`,
|
||
|
|
distance: Math.round(distToNext),
|
||
|
|
estimatedTime: Math.round(boatData.speed > 0 ? distToNext / boatData.speed : 0),
|
||
|
|
} : undefined,
|
||
|
|
remainingDistance: Math.round(remainDist),
|
||
|
|
estimatedRemainingTime: Math.round(estTime),
|
||
|
|
progress: prog,
|
||
|
|
};
|
||
|
|
}, [activeWaypoints, boatData, currentWaypointIndex, activeRouteInfo]);
|
||
|
|
|
||
|
|
return {
|
||
|
|
activeWaypoints,
|
||
|
|
activeRouteInfo,
|
||
|
|
nextWaypoint,
|
||
|
|
remainingDistance,
|
||
|
|
estimatedRemainingTime,
|
||
|
|
progress,
|
||
|
|
};
|
||
|
|
}
|