feat: add complex junction selection markers

This commit is contained in:
que01 committed 2026-09-04 17:09:32 +08:00
1 parent 57248f1aa4
commit 38ea779d9b
18 files changed
+685 -48

No files matched your search

@@ -1,5 +1,5 @@
import { useMemo } from 'react';
import type { JunctionDescriptor } from './hotspots';
import { useEffect, useMemo, useState } from 'react';
import { candidateForJunction, type JunctionDescriptor } from './hotspots';
import { canEditJunction, type JunctionSession } from './workspace';
import { Button } from '../ui/button';
import { ParameterPanel } from '../components/ParameterPanel';
@@ -25,6 +25,9 @@ interface Props {
onSave: () => void;
editMode?: boolean;
onEnableEdit?: () => void;
onEnableComplex?: (nodeIds: string[]) => void;
onUpdateComplexNodes?: (nodeIds: string[]) => void;
onCandidateMarkersChange?: (markers: Array<{ coordinate: Coordinate; label: string; selected: boolean }>) => void;
handles?: EditHandle[];
onDraft?: (handle: EditHandle, value: number) => void;
onDraftGroup?: (handles: EditHandle[], value: number) => void;
@@ -52,6 +55,9 @@ export function JunctionWorkspace({
onSave,
editMode = false,
onEnableEdit,
onEnableComplex,
onUpdateComplexNodes,
onCandidateMarkersChange,
diagnostics = [],
facilitySummary = { crosswalks: 0, stopLines: 0, signals: 0 },
state,
@@ -72,6 +78,90 @@ export function JunctionWorkspace({
onUpdateSignal,
}: Props) {
const editable = useMemo(() => canEditJunction(descriptor), [descriptor]);
const complexEnabled = useMemo(() => {
const nodes = new Set(descriptor.nodeIds.map(String));
return Boolean(
state?.debug?.junctionClusters?.some(
(cluster) =>
(descriptor.ref.type === 'cluster' && cluster.id === descriptor.ref.id) ||
(cluster.nodeIds.length === nodes.size && cluster.nodeIds.every((nodeId) => nodes.has(String(nodeId)))),
),
);
}, [descriptor, state?.debug?.junctionClusters]);
const candidate = useMemo(() => {
return candidateForJunction(state, descriptor);
}, [descriptor, state]);
const configuredNodeIds = useMemo(
() =>
state?.debug?.junctionClusters?.find((cluster) => cluster.id === descriptor.ref.id)?.nodeIds.map(String) || [],
[descriptor.ref.id, state?.debug?.junctionClusters],
);
const [selectedCandidateNodes, setSelectedCandidateNodes] = useState<string[]>([]);
const [candidateNodePickerOpen, setCandidateNodePickerOpen] = useState(false);
const candidateNodes = useMemo(
() =>
(candidate?.nodeIds?.length
? candidate.nodeIds
: descriptor.nodeIds.length
? descriptor.nodeIds
: configuredNodeIds
).map(String),
[candidate?.nodeIds, configuredNodeIds, descriptor.nodeIds],
);
const activeCandidateNodes = selectedCandidateNodes.filter((nodeId) => candidateNodes.includes(nodeId)).length
? selectedCandidateNodes.filter((nodeId) => candidateNodes.includes(nodeId))
: candidateNodes;
const candidateRoadNames = useMemo(() => {
if (!state) return [];
const nodeIds = new Set((candidate?.nodeIds || descriptor.nodeIds).map(String));
return [
...new Set(
state.compiled.model.roads
.filter((road) => road.sourceNodeIds.some((nodeId) => nodeIds.has(String(nodeId))))
.map((road) => junctionRoadName(road, state.compiled.model.roads)),
),
];
}, [candidate, descriptor.nodeIds, state]);
const candidateNodeLabels = useMemo(() => {
if (!state) return [];
return candidateNodes.map((nodeId, index) => {
const names = [
...new Set(
state.compiled.model.roads
.filter((road) => road.sourceNodeIds.some((item) => String(item) === nodeId))
.map((road) => junctionRoadName(road, state.compiled.model.roads)),
),
];
return { nodeId, label: `路口点 ${index + 1}${names.length ? ` · ${names.join('、')}` : ''}` };
});
}, [candidate, candidateNodes, state]);
useEffect(() => {
if (!onCandidateMarkersChange || !state || !candidateNodePickerOpen) {
onCandidateMarkersChange?.([]);
return;
}
const selectedIds = new Set(activeCandidateNodes);
onCandidateMarkersChange(
candidateNodes.flatMap((nodeId, index) => {
const points = state.compiled.model.endpoints
.filter((endpoint) => String(endpoint.nodeId) === nodeId)
.map((endpoint) => endpoint.coordinate);
return points.length
? [
{
coordinate: [
points.reduce((sum, point) => sum + point[0], 0) / points.length,
points.reduce((sum, point) => sum + point[1], 0) / points.length,
] as Coordinate,
label: String(index + 1),
selected: selectedIds.has(nodeId),
},
]
: [];
}),
);
return () => onCandidateMarkersChange([]);
}, [activeCandidateNodes, candidateNodePickerOpen, candidateNodes, onCandidateMarkersChange, state]);
const facilities = useMemo(
() => (state ? junctionFacilities(state, descriptor.nodeIds, roads) : []),
[descriptor.nodeIds, roads, state],
@@ -84,11 +174,15 @@ export function JunctionWorkspace({
<span className="junction-workspace__eyebrow">JunctionTools</span>
</div>
<p>
{editable
? editMode
? '可编辑路口几何参数'
: '当前为浏览模式,开启编辑后可调整路口几何参数'
: '只读上下文:当前路口类型暂不支持编辑'}
{complexEnabled
? '当前按复杂路口渲染'
: descriptor.status === 'candidate'
? '当前按普通路口渲染,可切换为复杂路口'
: editable
? editMode
? '可编辑路口几何参数'
: '当前为浏览模式,开启编辑后可调整路口几何参数'
: '只读上下文:当前路口类型暂不支持编辑'}
</p>
</div>
<div className="junction-workspace__header-actions">
@@ -111,6 +205,74 @@ export function JunctionWorkspace({
</dd>
</dl>
<section className="junction-workspace__validation" aria-label="路口验证">
{candidate || complexEnabled ? (
<div className="junction-workspace__candidate" aria-label="复杂路口候选信息">
<strong>{complexEnabled ? '当前复杂路口组成' : '附近可合并节点'}</strong>
{candidate ? (
<span>
已根据当前路口附近路网动态检测到 {candidate.nodeCount} 个相邻节点,范围约 {candidate.diameterMeters}{' '}
米。
{candidateRoadNames.length ? ` 关联道路:${candidateRoadNames.join('、')}。` : ''} 建议核心半径{' '}
{candidate.coreRadiusMeters} 米。
</span>
) : (
<span>当前复杂路口包含 {candidateNodes.length} 个路口点。</span>
)}
{candidateNodes.length > 2 ? (
<>
<Button
type="button"
className="junction-workspace__candidate-toggle"
aria-expanded={candidateNodePickerOpen}
onClick={() => setCandidateNodePickerOpen((open) => !open)}
>
{candidateNodePickerOpen ? '收起路口点选择' : '调整参与路口点'}
</Button>
{candidateNodePickerOpen ? (
<fieldset className="junction-workspace__candidate-nodes">
<legend>参与合并的路口点</legend>
{candidateNodeLabels.map(({ nodeId, label }, index) => (
<label
key={nodeId}
className={activeCandidateNodes.includes(nodeId) ? 'is-included' : 'is-excluded'}
>
<input
type="checkbox"
checked={activeCandidateNodes.includes(nodeId)}
disabled={activeCandidateNodes.length <= 2 && activeCandidateNodes.includes(nodeId)}
onChange={(event) => {
const next = event.target.checked
? [...new Set([...activeCandidateNodes, nodeId])]
: activeCandidateNodes.filter((item) => item !== nodeId);
setSelectedCandidateNodes(next);
}}
/>
<span className="junction-workspace__candidate-node-badge" aria-hidden="true">
{index + 1}
</span>
<span className="junction-workspace__candidate-node-label">{label}</span>
</label>
))}
</fieldset>
) : null}
</>
) : null}
{!complexEnabled && onEnableComplex ? (
<Button
type="button"
disabled={activeCandidateNodes.length < 2}
onClick={() => onEnableComplex(activeCandidateNodes)}
>
启用复杂路口渲染
</Button>
) : null}
{complexEnabled && onUpdateComplexNodes ? (
<Button type="button" onClick={() => onUpdateComplexNodes(activeCandidateNodes)}>
应用路口点调整
</Button>
) : null}
</div>
) : null}
<h3>设施与验证</h3>
<p>
斑马线 {facilitySummary.crosswalks} · 停止线 {facilitySummary.stopLines} · 信号灯 {facilitySummary.signals}
@@ -224,6 +386,8 @@ export function JunctionWorkspace({
<p className="junction-workspace__browse-hint">
开启编辑模式后,这里会显示进口宽度、进口收口距离和角部圆角控件。
</p>
) : complexEnabled ? (
<p className="junction-workspace__browse-hint">复杂路口几何已启用;首版仅支持查看,不支持参数拖拽编辑。</p>
) : null}
</div>
</section>