障碍物检测初始化
This commit is contained in:
@@ -16,7 +16,7 @@ import * as Cesium from 'cesium';
|
||||
|
||||
import 'cesium/Build/Cesium/Widgets/widgets.css';
|
||||
|
||||
const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine, finishPoint, drawRealTime, realtimePosition, realtimeType, pathPoints = [], devices = [], onCameraClick, focusLocation, points = [], markedPoints = [], onPathChange, from, onClearDraw, onMarkPoint, routeMode = '', polyPoints, mapLayer, activeType, overlayRefreshKey = 0, focusOverlay = null, onObstacleRadiusChange, obstacleShapes = [], onObstacleRectResize, siteMapInfoId }, ref) => {
|
||||
const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine, finishPoint, drawRealTime, realtimePosition, realtimeType, pathPoints = [], devices = [], onCameraClick, focusLocation, points = [], markedPoints = [], onPathChange, from, onClearDraw, onMarkPoint, routeMode = '', polyPoints, mapLayer, activeType, overlayRefreshKey = 0, focusOverlay = null, onObstacleRadiusChange, obstacleShapes = [], onObstacleRectResize, siteMapInfoId, detectReportInfo }, ref) => {
|
||||
const containerRef = useRef(null);
|
||||
const viewerRef = useRef(null);
|
||||
const tooltipRef = useRef(null);
|
||||
@@ -78,6 +78,8 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
|
||||
// try { viewer.camera.cancelFlight(); } catch (e) { /* 忽略取消异常 */ }
|
||||
// viewer.camera.flyTo(config);
|
||||
//};
|
||||
|
||||
|
||||
const resetFlightPriority = () => { lastFlightPriorityRef.current = -1; };
|
||||
const getObstacleHandlePosition = (
|
||||
lon,
|
||||
@@ -246,80 +248,12 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
|
||||
if (lastFlightPromiseRef.current) viewer.camera.cancelFlight();
|
||||
};
|
||||
}, []);
|
||||
|
||||
const flyToView = (viewer, priority, config, key = '') => {
|
||||
if (!viewer || viewer.isDestroyed()) return false;
|
||||
|
||||
const state = flightStateRef.current;
|
||||
const now = Date.now();
|
||||
|
||||
// 相同目标正在飞行,忽略重复请求
|
||||
if (
|
||||
state.flying &&
|
||||
state.priority === priority &&
|
||||
state.key === key &&
|
||||
now - (state.startedAt || 0) < 3000
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 仅在当前飞行尚未结束时执行优先级比较
|
||||
if (state.flying && priority < state.priority) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const token = ++state.token;
|
||||
|
||||
state.priority = priority;
|
||||
state.key = key;
|
||||
state.startedAt = now;
|
||||
state.flying = true;
|
||||
|
||||
try {
|
||||
viewer.camera.cancelFlight();
|
||||
|
||||
viewer.camera.flyTo({
|
||||
...config,
|
||||
|
||||
complete: () => {
|
||||
// 旧飞行的回调不能清除新飞行状态
|
||||
if (flightStateRef.current.token !== token) return;
|
||||
|
||||
flightStateRef.current.flying = false;
|
||||
flightStateRef.current.priority = -1;
|
||||
flightStateRef.current.key = null;
|
||||
|
||||
config.complete?.();
|
||||
},
|
||||
|
||||
cancel: () => {
|
||||
if (flightStateRef.current.token !== token) return;
|
||||
|
||||
flightStateRef.current.flying = false;
|
||||
flightStateRef.current.priority = -1;
|
||||
flightStateRef.current.key = null;
|
||||
|
||||
config.cancel?.();
|
||||
},
|
||||
});
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
if (flightStateRef.current.token === token) {
|
||||
flightStateRef.current.flying = false;
|
||||
flightStateRef.current.priority = -1;
|
||||
flightStateRef.current.key = null;
|
||||
}
|
||||
|
||||
console.error('地图飞行失败:', error);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
function updateLiveObstacleBoxes(
|
||||
viewer,
|
||||
message,
|
||||
) {
|
||||
console.log(message, "障碍物信息")
|
||||
return;
|
||||
const payload = message.data ?? message;
|
||||
const objects = payload.objects ?? [];
|
||||
|
||||
@@ -404,6 +338,88 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
const handleDetectFn = (res) => {
|
||||
console.log(res, "监测到障碍物")
|
||||
const viewer = viewerRef.current;
|
||||
updateLiveObstacleBoxes(viewer, res);
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
const flyToView = (viewer, priority, config, key = '') => {
|
||||
if (!viewer || viewer.isDestroyed()) return false;
|
||||
|
||||
const state = flightStateRef.current;
|
||||
const now = Date.now();
|
||||
|
||||
// 相同目标正在飞行,忽略重复请求
|
||||
if (
|
||||
state.flying &&
|
||||
state.priority === priority &&
|
||||
state.key === key &&
|
||||
now - (state.startedAt || 0) < 3000
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 仅在当前飞行尚未结束时执行优先级比较
|
||||
if (state.flying && priority < state.priority) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const token = ++state.token;
|
||||
|
||||
state.priority = priority;
|
||||
state.key = key;
|
||||
state.startedAt = now;
|
||||
state.flying = true;
|
||||
|
||||
try {
|
||||
viewer.camera.cancelFlight();
|
||||
|
||||
viewer.camera.flyTo({
|
||||
...config,
|
||||
|
||||
complete: () => {
|
||||
// 旧飞行的回调不能清除新飞行状态
|
||||
if (flightStateRef.current.token !== token) return;
|
||||
|
||||
flightStateRef.current.flying = false;
|
||||
flightStateRef.current.priority = -1;
|
||||
flightStateRef.current.key = null;
|
||||
|
||||
config.complete?.();
|
||||
},
|
||||
|
||||
cancel: () => {
|
||||
if (flightStateRef.current.token !== token) return;
|
||||
|
||||
flightStateRef.current.flying = false;
|
||||
flightStateRef.current.priority = -1;
|
||||
flightStateRef.current.key = null;
|
||||
|
||||
config.cancel?.();
|
||||
},
|
||||
});
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
if (flightStateRef.current.token === token) {
|
||||
flightStateRef.current.flying = false;
|
||||
flightStateRef.current.priority = -1;
|
||||
flightStateRef.current.key = null;
|
||||
}
|
||||
|
||||
console.error('地图飞行失败:', error);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
const clearDrawingPath = () => {
|
||||
const viewer = viewerRef.current;
|
||||
if (!viewer) return;
|
||||
@@ -1626,11 +1642,11 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
|
||||
);
|
||||
const lat = Number(obstacle?.position?.lat);
|
||||
const radius = Number(obstacle?.radius ?? 2);
|
||||
// 形状配置:圆形走原有半径逻辑,矩形按长宽渲染
|
||||
const shapeConfig = obstacleShapes?.[idx] || {};
|
||||
const isRect = shapeConfig.shapeType === 'rect';
|
||||
const rectWidth = Math.max(0.1, Number(shapeConfig.width) || radius * 2);
|
||||
const rectHeight = Math.max(0.1, Number(shapeConfig.height) || radius * 2);
|
||||
// 形状配置:圆形走原有半径逻辑,矩形按长宽渲染
|
||||
const shapeConfig = obstacleShapes?.[idx] || {};
|
||||
const isRect = shapeConfig.shapeType === 'rect';
|
||||
const rectWidth = Math.max(0.1, Number(shapeConfig.width) || radius * 2);
|
||||
const rectHeight = Math.max(0.1, Number(shapeConfig.height) || radius * 2);
|
||||
|
||||
if (
|
||||
!Number.isFinite(lon) ||
|
||||
@@ -1704,7 +1720,7 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
|
||||
outlineColor: redColor,
|
||||
outlineWidth: isHighlighted ? 4 : 2,
|
||||
heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
|
||||
}
|
||||
}
|
||||
: undefined,
|
||||
|
||||
// 圆心标记
|
||||
@@ -1834,12 +1850,12 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
|
||||
const shapeConfig = obstacleShapes?.[index] || {};
|
||||
const isRect = shapeConfig.shapeType === 'rect';
|
||||
const rectWidth = Math.max(
|
||||
0.1,
|
||||
Number(shapeConfig.width) || radius * 2
|
||||
0.1,
|
||||
Number(shapeConfig.width) || radius * 2
|
||||
);
|
||||
const rectHeight = Math.max(
|
||||
0.1,
|
||||
Number(shapeConfig.height) || radius * 2
|
||||
0.1,
|
||||
Number(shapeConfig.height) || radius * 2
|
||||
);
|
||||
|
||||
draggingObstacleRef.current = {
|
||||
@@ -1952,20 +1968,20 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
|
||||
const mouseLat = Cesium.Math.toDegrees(carto.latitude);
|
||||
|
||||
const width = Math.max(
|
||||
0.2,
|
||||
2 *
|
||||
Cesium.Cartesian3.distance(
|
||||
dragging.center,
|
||||
Cesium.Cartesian3.fromDegrees(mouseLon, dragging.lat, 0)
|
||||
)
|
||||
0.2,
|
||||
2 *
|
||||
Cesium.Cartesian3.distance(
|
||||
dragging.center,
|
||||
Cesium.Cartesian3.fromDegrees(mouseLon, dragging.lat, 0)
|
||||
)
|
||||
);
|
||||
const height = Math.max(
|
||||
0.2,
|
||||
2 *
|
||||
Cesium.Cartesian3.distance(
|
||||
dragging.center,
|
||||
Cesium.Cartesian3.fromDegrees(dragging.lon, mouseLat, 0)
|
||||
)
|
||||
0.2,
|
||||
2 *
|
||||
Cesium.Cartesian3.distance(
|
||||
dragging.center,
|
||||
Cesium.Cartesian3.fromDegrees(dragging.lon, mouseLat, 0)
|
||||
)
|
||||
);
|
||||
|
||||
dragging.width = width;
|
||||
@@ -1975,31 +1991,31 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
|
||||
const rectHandle = viewer.entities.getById(dragging.handleId);
|
||||
|
||||
if (rectEntity?.rectangle) {
|
||||
rectEntity.rectangle.coordinates.setValue(
|
||||
getObstacleRectCoordinates(
|
||||
dragging.lon,
|
||||
dragging.lat,
|
||||
width,
|
||||
height
|
||||
)
|
||||
);
|
||||
rectEntity.rectangle.coordinates.setValue(
|
||||
getObstacleRectCoordinates(
|
||||
dragging.lon,
|
||||
dragging.lat,
|
||||
width,
|
||||
height
|
||||
)
|
||||
);
|
||||
}
|
||||
if (rectHandle) {
|
||||
rectHandle.position = new Cesium.ConstantPositionProperty(
|
||||
getObstacleRectHandlePosition(
|
||||
dragging.lon,
|
||||
dragging.lat,
|
||||
width,
|
||||
height
|
||||
)
|
||||
);
|
||||
rectHandle.position = new Cesium.ConstantPositionProperty(
|
||||
getObstacleRectHandlePosition(
|
||||
dragging.lon,
|
||||
dragging.lat,
|
||||
width,
|
||||
height
|
||||
)
|
||||
);
|
||||
}
|
||||
if (rectEntity?.label) {
|
||||
const obstacle =
|
||||
stationOverlay.tempObstaclePoints?.[dragging.index];
|
||||
rectEntity.label.text.setValue(
|
||||
`${obstacle?.alias || `障碍物点-${dragging.index + 1}`} (${width.toFixed(2)}×${height.toFixed(2)}m)`
|
||||
);
|
||||
const obstacle =
|
||||
stationOverlay.tempObstaclePoints?.[dragging.index];
|
||||
rectEntity.label.text.setValue(
|
||||
`${obstacle?.alias || `障碍物点-${dragging.index + 1}`} (${width.toFixed(2)}×${height.toFixed(2)}m)`
|
||||
);
|
||||
}
|
||||
|
||||
viewer.scene.requestRender();
|
||||
@@ -2064,34 +2080,34 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
|
||||
setIsDraggingObstacle(false);
|
||||
|
||||
if (viewer) {
|
||||
const ctrl = viewer.scene.screenSpaceCameraController;
|
||||
ctrl.enableRotate = true;
|
||||
ctrl.enableTranslate = true;
|
||||
ctrl.enableZoom = true;
|
||||
const ctrl = viewer.scene.screenSpaceCameraController;
|
||||
ctrl.enableRotate = true;
|
||||
ctrl.enableTranslate = true;
|
||||
ctrl.enableZoom = true;
|
||||
}
|
||||
|
||||
// 矩形:打印信息并回传长宽给父组件(暂不走圆形保存逻辑)
|
||||
if (dragging.shapeType === 'rect') {
|
||||
const obstacle =
|
||||
stationOverlay.tempObstaclePoints?.[index];
|
||||
const { width, height } = dragging;
|
||||
const obstacle =
|
||||
stationOverlay.tempObstaclePoints?.[index];
|
||||
const { width, height } = dragging;
|
||||
|
||||
logRectObstacleInfo(
|
||||
obstacle,
|
||||
index,
|
||||
dragging.lon,
|
||||
dragging.lat,
|
||||
width,
|
||||
height
|
||||
);
|
||||
logRectObstacleInfo(
|
||||
obstacle,
|
||||
index,
|
||||
dragging.lon,
|
||||
dragging.lat,
|
||||
width,
|
||||
height
|
||||
);
|
||||
|
||||
onObstacleRectResize?.(
|
||||
index,
|
||||
Number(width.toFixed(2)),
|
||||
Number(height.toFixed(2))
|
||||
);
|
||||
viewer?.scene.requestRender();
|
||||
return;
|
||||
onObstacleRectResize?.(
|
||||
index,
|
||||
Number(width.toFixed(2)),
|
||||
Number(height.toFixed(2))
|
||||
);
|
||||
viewer?.scene.requestRender();
|
||||
return;
|
||||
}
|
||||
|
||||
// 通知父组件更新列表
|
||||
@@ -3553,6 +3569,9 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
|
||||
resolve(dataUrl);
|
||||
});
|
||||
}, []);
|
||||
useEffect(() => {
|
||||
handleDetectFn(detectReportInfo)
|
||||
}, [detectReportInfo])
|
||||
|
||||
// 暴露 ref 方法
|
||||
useImperativeHandle(ref, () => ({
|
||||
|
||||
Reference in New Issue
Block a user