机器人状态界面 在线状态实时更新 i修复返航物和障碍物保存和新增 互斥 障碍物支持矩形 手柄拖拽
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import React, { useEffect, useRef, useState, useMemo, useImperativeHandle, forwardRef, useCallback } from 'react';
|
||||
import React, { useEffect, useRef, useState, useMemo, useImperativeHandle, forwardRef, useCallback } from 'react';
|
||||
import carImg from '../assets/images/carplane.png';
|
||||
import planeImg from '../assets/images/planeicon.png';
|
||||
import cameraImg from '../assets/images/camera.png';
|
||||
@@ -30,6 +30,7 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
|
||||
const navLineRef = useRef(null); // 完成点实时轨迹
|
||||
const markedPointsRef = useRef([]); // 标记点实体
|
||||
const obstacleCirclesRef = useRef([]); // 障碍物点圆形实体
|
||||
const markedObstacleHandlesRef = useRef([]); // 编辑中障碍物拖拽手柄
|
||||
const markedLineRef = useRef(null); // 标记点连线实体
|
||||
const lastMarkedPointsRef = useRef([]); // 上次渲染的标记点数据,用于增量更新
|
||||
const onMarkPointRef = useRef(onMarkPoint); // 总是指向最新的 onMarkPoint,避免闭包滞后
|
||||
@@ -124,6 +125,37 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
|
||||
);
|
||||
};
|
||||
|
||||
// 拖拽手柄时显示障碍物尺寸(长×宽 / 半径),跟随手柄位置
|
||||
const OBSTACLE_DRAG_TIP_ID = 'obstacle-drag-tip';
|
||||
|
||||
const updateObstacleDragTip = (viewer, position, text) => {
|
||||
if (!viewer || !position) return;
|
||||
const existing = viewer.entities.getById(OBSTACLE_DRAG_TIP_ID);
|
||||
if (existing) {
|
||||
existing.position = new Cesium.ConstantPositionProperty(position);
|
||||
if (existing.label?.text?.setValue) {
|
||||
existing.label.text.setValue(text);
|
||||
} else if (existing.label) {
|
||||
existing.label.text = text;
|
||||
}
|
||||
return;
|
||||
}
|
||||
viewer.entities.add({
|
||||
id: OBSTACLE_DRAG_TIP_ID,
|
||||
position,
|
||||
label: {
|
||||
text,
|
||||
font: 'bold 13px sans-serif',
|
||||
fillColor: Cesium.Color.WHITE,
|
||||
showBackground: true,
|
||||
backgroundColor: Cesium.Color.fromCssColorString('#ff4d4f').withAlpha(0.9),
|
||||
pixelOffset: new Cesium.Cartesian2(0, -18),
|
||||
disableDepthTestDistance: Number.POSITIVE_INFINITY,
|
||||
show: true,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
// 打印矩形障碍物信息(center 为 WGS84,size 为以 center 为中心的长宽,单位米)
|
||||
const logRectObstacleInfo = (obstacle, index, lon, lat, width, height) => {
|
||||
const info = {
|
||||
@@ -831,6 +863,7 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
|
||||
// 同时删除对应的 obstacle circle
|
||||
const circleId = `marked-obstacle-circle-${id ?? i}`;
|
||||
viewer.entities.removeById(circleId);
|
||||
//viewer.entities.removeById(`marked-obstacle-handle-${baseId}`);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -845,6 +878,14 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
|
||||
return true;
|
||||
});
|
||||
|
||||
markedObstacleHandlesRef.current = markedObstacleHandlesRef.current.filter(id => {
|
||||
if (!newPoints.some((p, i) => `marked-obstacle-handle-${p.id ?? i}` === id)) {
|
||||
viewer.entities.removeById(id);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
// 2) 新增或更新标记点 entity
|
||||
newPoints.forEach((p, i) => {
|
||||
const id = `marked-point-${p.id ?? i}`;
|
||||
@@ -882,31 +923,64 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
|
||||
markedPointsRef.current.push(id);
|
||||
}
|
||||
|
||||
// 障碍物点:创建/更新圆形区域(EllipseGraphics,单位是米)
|
||||
//if (isObstacle) {
|
||||
// const radius = Math.max(0.5, Number(p.radius) || 2.0);
|
||||
// const circleEntity = viewer.entities.getById(circleId);
|
||||
// if (circleEntity) {
|
||||
// circleEntity.position = position;
|
||||
// circleEntity.ellipse.semiMajorAxis = radius;
|
||||
// circleEntity.ellipse.semiMinorAxis = radius;
|
||||
// } else {
|
||||
// viewer.entities.add({
|
||||
// id: circleId,
|
||||
// position,
|
||||
// ellipse: {
|
||||
// semiMajorAxis: radius,
|
||||
// semiMinorAxis: radius,
|
||||
// material: new Cesium.Color(1.0, 0.3, 0.3, 0.25),
|
||||
// outline: true,
|
||||
// outlineColor: Cesium.Color.RED,
|
||||
// outlineWidth: 2,
|
||||
// height: 0,
|
||||
// },
|
||||
// });
|
||||
// obstacleCirclesRef.current.push(circleId);
|
||||
// }
|
||||
//}
|
||||
// 障碍物点:根据形状创建预览 + 拖拽手柄
|
||||
if (isObstacle) {
|
||||
const shapeType = p.shapeType === 'circle' ? 'circle' : 'rect';
|
||||
const sizeX = Math.max(0.1, Number(p.sizeX) || Number(p.size_x) || 2);
|
||||
const sizeY = Math.max(0.1, Number(p.sizeY) || Number(p.size_y) || 2);
|
||||
const radius = Math.max(0.1, Number(p.radius) || 2);
|
||||
const entity = viewer.entities.getById(circleId);
|
||||
if (shapeType === 'rect') {
|
||||
if (entity?.rectangle) {
|
||||
entity.rectangle.coordinates.setValue(getObstacleRectCoordinates(Number(p.lon), Number(p.lat), sizeX, sizeY));
|
||||
} else {
|
||||
viewer.entities.add({
|
||||
id: circleId, position,
|
||||
rectangle: {
|
||||
coordinates: getObstacleRectCoordinates(Number(p.lon), Number(p.lat), sizeX, sizeY),
|
||||
height: 0, heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
|
||||
material: new Cesium.Color(1.0, 0.3, 0.3, 0.25),
|
||||
outline: true, outlineColor: Cesium.Color.RED, outlineWidth: 2,
|
||||
},
|
||||
});
|
||||
}
|
||||
} else {
|
||||
if (entity?.ellipse) {
|
||||
entity.ellipse.semiMajorAxis.setValue(radius);
|
||||
entity.ellipse.semiMinorAxis.setValue(radius);
|
||||
} else {
|
||||
viewer.entities.add({
|
||||
id: circleId, position,
|
||||
ellipse: {
|
||||
semiMajorAxis: radius, semiMinorAxis: radius, height: 0,
|
||||
heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
|
||||
material: new Cesium.Color(1.0, 0.3, 0.3, 0.25),
|
||||
outline: true, outlineColor: Cesium.Color.RED, outlineWidth: 2,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
if (!obstacleCirclesRef.current.includes(circleId)) obstacleCirclesRef.current.push(circleId);
|
||||
|
||||
// 拖拽手柄
|
||||
const handleId = `marked-obstacle-handle-${p.id ?? i}`;
|
||||
const handlePos = shapeType === 'rect'
|
||||
? getObstacleRectHandlePosition(Number(p.lon), Number(p.lat), sizeX, sizeY)
|
||||
: getObstacleHandlePosition(Number(p.lon), Number(p.lat), radius);
|
||||
const existingHandle = viewer.entities.getById(handleId);
|
||||
if (existingHandle) {
|
||||
existingHandle.position = new Cesium.ConstantPositionProperty(handlePos);
|
||||
} else {
|
||||
viewer.entities.add({
|
||||
id: handleId,
|
||||
obstacleHandleData: { index: i, pointId: p.id, source: 'marked' },
|
||||
position: handlePos,
|
||||
point: { pixelSize: 13, color: Cesium.Color.WHITE, outlineColor: Cesium.Color.RED, outlineWidth: 3, disableDepthTestDistance: Number.POSITIVE_INFINITY },
|
||||
label: { text: '\u2194', font: 'bold 12px sans-serif', fillColor: Cesium.Color.RED, pixelOffset: new Cesium.Cartesian2(0, -1), disableDepthTestDistance: Number.POSITIVE_INFINITY },
|
||||
});
|
||||
if (!markedObstacleHandlesRef.current.includes(handleId)) markedObstacleHandlesRef.current.push(handleId);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// 3) 连线:只有坐标/数量真正变化时才重建
|
||||
@@ -1730,13 +1804,14 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
|
||||
outlineColor: Cesium.Color.WHITE,
|
||||
outlineWidth: 2,
|
||||
disableDepthTestDistance: Number.POSITIVE_INFINITY,
|
||||
show: true,
|
||||
},
|
||||
|
||||
// 名称与半径标签
|
||||
label: {
|
||||
text: isRect
|
||||
? `${obstacle?.alias || `障碍物点-${idx + 1}`} (${rectWidth.toFixed(2)}×${rectHeight.toFixed(2)}m)`
|
||||
: `${obstacle?.alias || `障碍物点-${idx + 1}`} (${radius}m)`,
|
||||
? `${obstacle?.alias || obstacle?.name || `障碍物点-${idx + 1}`} (${rectWidth.toFixed(2)}×${rectHeight.toFixed(2)}m)`
|
||||
: `${obstacle?.alias || obstacle?.name || `障碍物点-${idx + 1}`} (${radius}m)`,
|
||||
font: isHighlighted ? 'bold 14px sans-serif' : '12px sans-serif',
|
||||
fillColor: Cesium.Color.WHITE,
|
||||
showBackground: true,
|
||||
@@ -1747,6 +1822,7 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
|
||||
0,
|
||||
100000
|
||||
),
|
||||
show: true,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -1773,6 +1849,7 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
|
||||
outlineColor: Cesium.Color.RED,
|
||||
outlineWidth: 3,
|
||||
disableDepthTestDistance: Number.POSITIVE_INFINITY,
|
||||
show: true,
|
||||
},
|
||||
|
||||
label: {
|
||||
@@ -1781,6 +1858,7 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
|
||||
fillColor: Cesium.Color.RED,
|
||||
pixelOffset: new Cesium.Cartesian2(0, -1),
|
||||
disableDepthTestDistance: Number.POSITIVE_INFINITY,
|
||||
show: true,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -1788,7 +1866,7 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
|
||||
});
|
||||
|
||||
viewer.scene.requestRender();
|
||||
}, [stationOverlay, highlightReturnPointId, obstacleShapeSignature]);
|
||||
}, [stationOverlay, highlightReturnPointId, obstacleShapeSignature, activeType]);
|
||||
|
||||
|
||||
useEffect(() => {
|
||||
@@ -1827,48 +1905,42 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
|
||||
if (!handleData) return;
|
||||
|
||||
const index = Number(handleData.index);
|
||||
const obstacles = stationOverlay.tempObstaclePoints || [];
|
||||
const obstacle = obstacles[index];
|
||||
const handleSource = handleData.source || 'station';
|
||||
let lon, lat, radius, shapeType, rectWidth, rectHeight, entityId, handleId;
|
||||
|
||||
if (!obstacle) return;
|
||||
|
||||
const lon = Number(
|
||||
obstacle.position?.lng ?? obstacle.position?.lon
|
||||
);
|
||||
const lat = Number(obstacle.position?.lat);
|
||||
const radius = Number(obstacle.radius ?? 2);
|
||||
|
||||
if (
|
||||
!Number.isFinite(lon) ||
|
||||
!Number.isFinite(lat) ||
|
||||
!Number.isFinite(radius)
|
||||
) {
|
||||
return;
|
||||
if (handleSource === 'marked') {
|
||||
const obstacle = markedPoints[index];
|
||||
if (!obstacle) return;
|
||||
lon = Number(obstacle.lon);
|
||||
lat = Number(obstacle.lat);
|
||||
shapeType = obstacle.shapeType === 'circle' ? 'circle' : 'rect';
|
||||
radius = Math.max(0.1, Number(obstacle.radius) || 2);
|
||||
rectWidth = shapeType === 'rect' ? (Number(obstacle.sizeX) || Number(obstacle.size_x) || 2) : radius * 2;
|
||||
rectHeight = shapeType === 'rect' ? (Number(obstacle.sizeY) || Number(obstacle.size_y) || 2) : radius * 2;
|
||||
entityId = `marked-obstacle-circle-${obstacle.id ?? index}`;
|
||||
handleId = `marked-obstacle-handle-${obstacle.id ?? index}`;
|
||||
} else {
|
||||
const obstacles = stationOverlay.tempObstaclePoints || [];
|
||||
const obstacle = obstacles[index];
|
||||
if (!obstacle) return;
|
||||
lon = Number(obstacle.position?.lng ?? obstacle.position?.lon);
|
||||
lat = Number(obstacle.position?.lat);
|
||||
radius = Number(obstacle.radius ?? 2);
|
||||
const shapeConfig = obstacleShapes?.[index] || {};
|
||||
shapeType = shapeConfig.shapeType === 'rect' ? 'rect' : 'circle';
|
||||
rectWidth = Math.max(0.1, Number(shapeConfig.width) || radius * 2);
|
||||
rectHeight = Math.max(0.1, Number(shapeConfig.height) || radius * 2);
|
||||
entityId = `station-obstacle-point-${index}`;
|
||||
handleId = `station-obstacle-handle-${index}`;
|
||||
}
|
||||
|
||||
// 形状配置:圆形 / 矩形
|
||||
const shapeConfig = obstacleShapes?.[index] || {};
|
||||
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) || !Number.isFinite(lat) || !Number.isFinite(radius)) return;
|
||||
|
||||
draggingObstacleRef.current = {
|
||||
index,
|
||||
entityId: `station-obstacle-point-${index}`,
|
||||
handleId: `station-obstacle-handle-${index}`,
|
||||
index, source: handleSource, entityId, handleId,
|
||||
center: Cesium.Cartesian3.fromDegrees(lon, lat, 0),
|
||||
lon,
|
||||
lat,
|
||||
radius,
|
||||
shapeType: isRect ? 'rect' : 'circle',
|
||||
width: rectWidth,
|
||||
height: rectHeight,
|
||||
lon, lat, radius, shapeType,
|
||||
width: rectWidth, height: rectHeight,
|
||||
};
|
||||
|
||||
setIsDraggingObstacle(true);
|
||||
@@ -2000,24 +2072,23 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
|
||||
)
|
||||
);
|
||||
}
|
||||
const handlePos = getObstacleRectHandlePosition(
|
||||
dragging.lon,
|
||||
dragging.lat,
|
||||
width,
|
||||
height
|
||||
);
|
||||
if (rectHandle) {
|
||||
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)`
|
||||
);
|
||||
rectHandle.position = new Cesium.ConstantPositionProperty(handlePos);
|
||||
}
|
||||
|
||||
// 拖拽过程中实时显示长宽
|
||||
updateObstacleDragTip(
|
||||
viewer,
|
||||
handlePos,
|
||||
`${width.toFixed(2)} × ${height.toFixed(2)} m`
|
||||
);
|
||||
|
||||
viewer.scene.requestRender();
|
||||
return;
|
||||
}
|
||||
@@ -2044,24 +2115,38 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
|
||||
}
|
||||
|
||||
// 同步更新手柄位置
|
||||
const circleHandlePos = getObstacleHandlePosition(
|
||||
dragging.lon,
|
||||
dragging.lat,
|
||||
nextRadius
|
||||
);
|
||||
if (handle) {
|
||||
handle.position = new Cesium.ConstantPositionProperty(
|
||||
getObstacleHandlePosition(
|
||||
dragging.lon,
|
||||
dragging.lat,
|
||||
nextRadius
|
||||
)
|
||||
);
|
||||
handle.position = new Cesium.ConstantPositionProperty(circleHandlePos);
|
||||
}
|
||||
|
||||
// 同步更新圆圈标签
|
||||
if (circle?.label) {
|
||||
const obstacle =
|
||||
stationOverlay.tempObstaclePoints?.[dragging.index];
|
||||
// 拖拽过程中实时显示直径
|
||||
updateObstacleDragTip(
|
||||
viewer,
|
||||
circleHandlePos,
|
||||
`直径 ${(nextRadius * 2).toFixed(2)} m`
|
||||
);
|
||||
|
||||
circle.label.text.setValue(
|
||||
`${obstacle?.alias || `障碍物点-${dragging.index + 1}`} (${nextRadius.toFixed(2)}m)`
|
||||
);
|
||||
// 同步更新圆圈标签
|
||||
if (circle) {
|
||||
const obstacle = dragging.source === 'marked'
|
||||
? markedPoints?.[dragging.index]
|
||||
: stationOverlay.tempObstaclePoints?.[dragging.index];
|
||||
|
||||
const labelText = `${obstacle?.alias || obstacle?.name || `障碍物点-${dragging.index + 1}`} (${nextRadius.toFixed(2)}m)`;
|
||||
|
||||
if (circle.label) {
|
||||
if (circle.label.text && circle.label.text.setValue) {
|
||||
circle.label.text.setValue(labelText);
|
||||
} else {
|
||||
circle.label.text = labelText;
|
||||
}
|
||||
circle.label.show = new Cesium.ConstantProperty(true);
|
||||
}
|
||||
}
|
||||
|
||||
viewer.scene.requestRender();
|
||||
@@ -2084,22 +2169,27 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
|
||||
ctrl.enableRotate = true;
|
||||
ctrl.enableTranslate = true;
|
||||
ctrl.enableZoom = true;
|
||||
|
||||
// 移除拖拽尺寸提示
|
||||
const tip = viewer.entities.getById(OBSTACLE_DRAG_TIP_ID);
|
||||
if (tip) viewer.entities.remove(tip);
|
||||
}
|
||||
|
||||
// 矩形:打印信息并回传长宽给父组件(暂不走圆形保存逻辑)
|
||||
if (dragging.shapeType === 'rect') {
|
||||
const obstacle =
|
||||
stationOverlay.tempObstaclePoints?.[index];
|
||||
const obstacle = dragging.source === 'marked'
|
||||
? markedPoints?.[index]
|
||||
: 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,
|
||||
@@ -2120,7 +2210,7 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
|
||||
viewer.scene.screenSpaceCameraController.enableRotate = true;
|
||||
viewer.scene.screenSpaceCameraController.enableTranslate = true;
|
||||
};
|
||||
}, [stationOverlay]);
|
||||
}, [stationOverlay, markedPoints, activeType]);
|
||||
|
||||
// 点击已配置列表:定位到对应返航点/分区;返航点额外高亮
|
||||
useEffect(() => {
|
||||
|
||||
Reference in New Issue
Block a user