Browse Source

perf(cockpit): 标记投影每帧复用并补齐相机距离

标记/设施/构件先投影一次供聚合、深度与渲染共用;相机窗口投影补 distance,深度排序不再额外遍历。
ChenJZ 7 giờ trước cách đây
mục cha
commit
108e38b6eb

+ 1 - 0
src/modules/cockpit/services/cesiumEngine.js

@@ -2764,6 +2764,7 @@ export function createCameraEngine(viewer) {
             const rect = markerProjectionRect || canvasRectCached(viewer)
             markerWindowPosition.x = at.x + rect.left
             markerWindowPosition.y = at.y + rect.top
+            markerWindowPosition.distance = Cartesian3.distance(world, camera.positionWC)
             return markerWindowPosition
         },
 

+ 1 - 0
src/modules/cockpit/services/mapService.js

@@ -1110,6 +1110,7 @@ async function bootEngine(record) {
             if (!at) return null
             overlayWindowPosition.x = at.x - overlayHostOrigin.left
             overlayWindowPosition.y = at.y - overlayHostOrigin.top
+            overlayWindowPosition.distance = at.distance
             return overlayWindowPosition
         }
         record.cameraControl = createCameraControl({

+ 35 - 24
src/modules/cockpit/services/markerOverlay.js

@@ -1590,7 +1590,9 @@ export function syncMarkerOverlay({ markers, devices, aggregates, facilities, se
     const windowPositions = new Map()
     for (const marker of markers || []) {
         if (!marker || !Number.isFinite(marker.longitude) || !Number.isFinite(marker.latitude)) continue
+        const projectionStartedAt = overlayPerfEnabled ? performance.now() : 0
         const pos = projectToWindow(marker.longitude, marker.latitude, marker)
+        if (overlayPerfEnabled) markerProjectionMs += performance.now() - projectionStartedAt
         if (pos && Number.isFinite(pos.x) && Number.isFinite(pos.y)) windowPositions.set(`${marker.kind}|${marker.code}`, { x: pos.x, y: pos.y, distance: pos.distance })
     }
     // 设施/构件的投影也先算一次:聚合、深度排序、渲染共用同一份(不再每帧投两遍)。
@@ -1598,14 +1600,18 @@ export function syncMarkerOverlay({ markers, devices, aggregates, facilities, se
     for (const facility of facilities || []) {
         const key = facility.key || `facility-point|${facility.code}`
         if (!Number.isFinite(facility.longitude) || !Number.isFinite(facility.latitude)) continue
+        const projectionStartedAt = overlayPerfEnabled ? performance.now() : 0
         const pos = projectToWindow(facility.longitude, facility.latitude, facility)
+        if (overlayPerfEnabled) markerProjectionMs += performance.now() - projectionStartedAt
         if (pos && Number.isFinite(pos.x) && Number.isFinite(pos.y)) facilityPositions.set(key, { x: pos.x, y: pos.y, distance: pos.distance })
     }
     const componentPositions = new Map()
     for (const component of pipeComponentsVisible === false ? [] : (pipeComponents || [])) {
         const key = component.key || `pipe-component|${component.code}`
         if (!Number.isFinite(component.longitude) || !Number.isFinite(component.latitude)) continue
+        const projectionStartedAt = overlayPerfEnabled ? performance.now() : 0
         const pos = projectToWindow(component.longitude, component.latitude, component)
+        if (overlayPerfEnabled) markerProjectionMs += performance.now() - projectionStartedAt
         if (pos && Number.isFinite(pos.x) && Number.isFinite(pos.y)) componentPositions.set(key, { x: pos.x, y: pos.y, distance: pos.distance })
     }
     // 03 未关联设备 / 04 监测点 / 05 设施点:各自独立命名空间,同位置也不跨类合并。
@@ -1628,19 +1634,26 @@ export function syncMarkerOverlay({ markers, devices, aggregates, facilities, se
         if (marker.kind === 'unassociated-equipment') aggregateRows.push(row)
         else if (marker.kind === 'monitoring-point') pointAggregateRows.push(row)
     }
+    // 服务端桶覆盖的类别直接使用桶结果;本地聚合只为缺失类别回退。
+    const externalGroups = Array.isArray(displayGroups) ? displayGroups.filter((group) => group && group.count > 1) : []
+    const externalKinds = new Set(externalGroups.map((group) => group.memberKind))
     // 迟滞:带上上一帧的组,已进组成员要拉开「阈值 + 缓冲」才释放(防止边界闪烁)。
-    if (!aggregateRows.length) lastAggregateGroups = []
-    const aggregateResult = groupUnassociatedByScreen(aggregateRows, {
-        selectedCode: aggregateSelectedCode,
-        previousGroups: lastAggregateGroups,
-    })
+    if (!aggregateRows.length || externalKinds.has('unassociated-equipment')) lastAggregateGroups = []
+    const aggregateResult = externalKinds.has('unassociated-equipment')
+        ? { groups: [] }
+        : groupUnassociatedByScreen(aggregateRows, {
+            selectedCode: aggregateSelectedCode,
+            previousGroups: lastAggregateGroups,
+        })
     lastAggregateGroups = aggregateResult.groups
-    if (!pointAggregateRows.length) lastPointAggregateGroups = []
-    const pointAggregateResult = groupMonitoringPointsByScreen(pointAggregateRows, {
-        // 选中的监测点从聚合移出,单独显示并保持两级详情(选中态由详情 store 驱动)。
-        selectedCode: typeof selectedPointCode === 'string' ? selectedPointCode : '',
-        previousGroups: lastPointAggregateGroups,
-    })
+    if (!pointAggregateRows.length || externalKinds.has('monitoring-point')) lastPointAggregateGroups = []
+    const pointAggregateResult = externalKinds.has('monitoring-point')
+        ? { groups: [] }
+        : groupMonitoringPointsByScreen(pointAggregateRows, {
+            // 选中的监测点从聚合移出,单独显示并保持两级详情(选中态由详情 store 驱动)。
+            selectedCode: typeof selectedPointCode === 'string' ? selectedPointCode : '',
+            previousGroups: lastPointAggregateGroups,
+        })
     lastPointAggregateGroups = pointAggregateResult.groups
     // 05 设施点:独立命名空间;选中设施用完整 key 移出,保留单体标记与设施详情。
     const facilityAggregateRows = []
@@ -1664,11 +1677,13 @@ export function syncMarkerOverlay({ markers, devices, aggregates, facilities, se
             screen: pos,
         })
     }
-    if (!facilityAggregateRows.length) lastFacilityAggregateGroups = []
-    const facilityAggregateResult = groupFacilityPointsByScreen(facilityAggregateRows, {
-        selectedCode: typeof selectedFacilityKey === 'string' ? selectedFacilityKey : '',
-        previousGroups: lastFacilityAggregateGroups,
-    })
+    if (!facilityAggregateRows.length || externalKinds.has('facility-point')) lastFacilityAggregateGroups = []
+    const facilityAggregateResult = externalKinds.has('facility-point')
+        ? { groups: [] }
+        : groupFacilityPointsByScreen(facilityAggregateRows, {
+            selectedCode: typeof selectedFacilityKey === 'string' ? selectedFacilityKey : '',
+            previousGroups: lastFacilityAggregateGroups,
+        })
     lastFacilityAggregateGroups = facilityAggregateResult.groups
     let badgeGroups = [
         ...aggregateResult.groups,
@@ -1678,17 +1693,15 @@ export function syncMarkerOverlay({ markers, devices, aggregates, facilities, se
     const groupedMemberKeys = new Set()
     // 票 07:服务端桶可用时用它作为聚合入口;仍用本地对象投影来隐藏桶内成员,
     // 成员详情/游标另走真实成员页。桶不可用时 displayGroups 为空,完全保留旧聚合。
-    const externalGroups = Array.isArray(displayGroups) ? displayGroups.filter((group) => group && group.count > 1) : []
     if (externalGroups.length) {
-        const externalKinds = new Set(externalGroups.map((group) => group.memberKind))
         const localForMissingClass = [
             ...aggregateResult.groups,
             ...pointAggregateResult.groups,
+            ...facilityAggregateResult.groups,
         ].filter((group) => group.count > 1 && !externalKinds.has(group.memberKind))
         badgeGroups = [
             ...externalGroups,
             ...localForMissingClass,
-            ...facilityAggregateResult.groups.filter((group) => group.count > 1),
         ]
         for (const marker of markers || []) {
             if (!marker) continue
@@ -1755,9 +1768,7 @@ export function syncMarkerOverlay({ markers, devices, aggregates, facilities, se
         if (groupedMemberKeys.has(key)) { hidePooledNode(key); continue }
         if (!Number.isFinite(marker.longitude) || !Number.isFinite(marker.latitude)) { hidePooledNode(key); continue }
 
-        const projectionStartedAt = overlayPerfEnabled ? performance.now() : 0
-        const windowPos = projectToWindow(marker.longitude, marker.latitude, marker)
-        if (overlayPerfEnabled) markerProjectionMs += performance.now() - projectionStartedAt
+        const windowPos = windowPositions.get(key)
         if (!windowPos || !Number.isFinite(windowPos.x) || !Number.isFinite(windowPos.y)) { hidePooledNode(key); continue }
         if (!withinOverlayMargin(windowPos.x, windowPos.y, hostRect.width, hostRect.height)) {
             hidePooledNode(key)
@@ -2011,7 +2022,7 @@ export function syncMarkerOverlay({ markers, devices, aggregates, facilities, se
         const key = facility.key || `facility-point|${facility.code}`
         if (groupedMemberKeys.has(key)) { hidePooledNode(key); continue }
         if (!Number.isFinite(facility.longitude) || !Number.isFinite(facility.latitude)) { hidePooledNode(key); continue }
-        const windowPos = projectToWindow(facility.longitude, facility.latitude, facility)
+        const windowPos = facilityPositions.get(key)
         if (!windowPos || !Number.isFinite(windowPos.x) || !Number.isFinite(windowPos.y)) { hidePooledNode(key); continue }
         if (!withinOverlayMargin(windowPos.x, windowPos.y, hostRect.width, hostRect.height)) {
             hidePooledNode(key)
@@ -2077,7 +2088,7 @@ export function syncMarkerOverlay({ markers, devices, aggregates, facilities, se
     for (const component of pipeComponentsVisible === false ? [] : (pipeComponents || [])) {
         const key = component.key || `pipe-component|${component.code}`
         if (!Number.isFinite(component.longitude) || !Number.isFinite(component.latitude)) { hidePooledNode(key); continue }
-        const windowPos = projectToWindow(component.longitude, component.latitude, component)
+        const windowPos = componentPositions.get(key)
         if (!windowPos || !Number.isFinite(windowPos.x) || !Number.isFinite(windowPos.y)) { hidePooledNode(key); continue }
         if (!withinOverlayMargin(windowPos.x, windowPos.y, hostRect.width, hostRect.height)) {
             hidePooledNode(key)

+ 15 - 4
tests/markerOverlay.test.js

@@ -24,12 +24,13 @@ function makeMarkers(count) {
         tone: 'lv4',
         longitude: 110 + index / 10000,
         latitude: 28 + index / 10000,
+        distance: 1000 - index,
         screenX: 10 + (index % 30) * 30,
         screenY: 10 + Math.floor(index / 30) * 28,
     }))
 }
 
-function makeFrame(markers, position) {
+function makeFrame(markers, position, projectToWindow) {
     return {
         markers,
         devices: [],
@@ -52,11 +53,11 @@ function makeFrame(markers, position) {
         deviceEffectOf: () => '',
         facilityEffectOf: () => '',
         selectedCode: '',
-        projectToWindow: (_longitude, _latitude, marker) => {
+        projectToWindow: projectToWindow || ((_longitude, _latitude, marker) => {
             position.x = marker.screenX
             position.y = marker.screenY
             return position
-        },
+        }),
     }
 }
 
@@ -70,7 +71,16 @@ test('preserves all markers through 100/500/1000 point updates and reuses projec
     const rows = makeMarkers(1000)
     const coordinatesBefore = rows.map(({ longitude, latitude }) => [longitude, latitude])
     for (const count of [100, 500, 1000]) {
-        overlay.syncMarkerOverlay(makeFrame(rows.slice(0, count), position))
+        const projectionCounts = new Map()
+        overlay.syncMarkerOverlay(makeFrame(rows.slice(0, count), position, (_longitude, _latitude, marker) => {
+            projectionCounts.set(marker.code, (projectionCounts.get(marker.code) || 0) + 1)
+            position.x = marker.screenX
+            position.y = marker.screenY
+            position.distance = marker.distance
+            return position
+        }))
+        assert.equal(projectionCounts.size, count)
+        assert.ok([...projectionCounts.values()].every((calls) => calls === 1), '每个标记每帧只投影一次')
         assert.equal(overlay.overlayPoolSize(), count)
         const visible = [...host.querySelectorAll('.marker-overlay-node')]
             .filter((node) => node.style.display === 'block')
@@ -81,6 +91,7 @@ test('preserves all markers through 100/500/1000 point updates and reuses projec
     const second = host.querySelector('[data-marker-overlay-key="monitoring-point|point-1"]')
     assert.equal(first.style.transform, 'translate(10px, 10px)')
     assert.equal(second.style.transform, 'translate(40px, 10px)')
+    assert.ok(Number(first.style.zIndex) > Number(second.style.zIndex), '更近的标记应绘制在更远标记上方')
 
     overlay.syncMarkerOverlay(makeFrame(rows.slice(0, 100), position))
     assert.equal(overlay.overlayPoolSize(), 1000)