Ver código fonte

fix(cockpit): 深度层级改全局唯一序号并统一设备定位距离

深度闪烁根因:逐条算 z 会撞值,遮挡退回 DOM 顺序,徽章重建/池复用即闪。改为每帧收集全部可见对象,rankByDepth 全局排序分配唯一序号(近者大),距离在 0.5% 容差内沿用上一帧相对顺序;点位/设备/设施/构件/徽章/标签全部使用唯一序号,选中点固定 9000,面板 9999。定位:新增 locateTierFor,独立设备/关联设备/监测设备统一 device 档 150m(跳转最小距离),监测点 500m、聚合 300m+ 保持现状;显式 tier 优先。
ChenJZ 1 dia atrás
pai
commit
6bfe49afe0

+ 36 - 21
src/modules/cockpit/domain/pointAggregation.js

@@ -89,29 +89,44 @@ export function labelOccludedByAggregates(pos, centers, clearance = AGGREGATE_BA
 }
 
 /**
- * 深度排序的 z-index(近者在上,远者在下)。
+ * 按相机距离计算**全屏唯一**的深度序号(近者序号大,远者小)。
  *
- * 地图是 3D 的,但 DOM overlay 是 2D;用「图标到相机的距离」做画家算法:
- * 距离越小 z 越大。对数刻度覆盖 1km–5×10⁷m,避免近距离挤在一起、远距离全塌成一层。
- * 返回值 1..900,渲染层再按「图标 = 2z、标签 = 2z+1」交错,标签略高于自身图标、
- * 低于更近的图标 —— 这样聚合徽章也不再是绝对最高层。
+ * 为什么不再是「每条记录各自算 z + 迟滞」:两枚图标算出同一个 z 时,遮挡关系会退回
+ * DOM 顺序;徽章重建/池复用会改变 DOM 顺序,于是看起来仍在闪。这里改成**一帧一次全局排序**,
+ * 每个可见对象拿唯一序号,遮挡不再依赖 DOM 顺序。
+ *
+ * 距离在容差内(默认 0.5%,至少 1 米)时沿用上一帧的相对顺序(previousRanks),
+ * 于是相机微动不会让相近距离的对象互换;只有真正拉开才换层。
+ *
+ * @param {Array<{key:string,distance:number}>} entries 本帧可见对象(key 必须与渲染记录一致)
+ * @param {{previousRanks?: Map<string,number>, toleranceRatio?: number}} [options]
+ * @returns {Map<string, number>} key → 序号(1..N,越大越靠前)
  */
-export function depthZIndex(distance, { min = 1, max = 900 } = {}) {
-    const d = Number.isFinite(distance) && distance > 0 ? distance : 1e8
-    const lo = Math.log10(1000)
-    const hi = Math.log10(5e7)
-    const t = Math.min(1, Math.max(0, (Math.log10(d) - lo) / (hi - lo)))
-    return Math.round(max - (max - min) * t)
-}
-
-/** 深度层级迟滞(z 单位):两个图标距离接近时,不因逐帧微小变化交换层级。 */
-export const DEPTH_Z_HYSTERESIS = 8
-
-/** 稳定 z-index:变化不足阈值时保持原值;首次直接采用,新值非法时保持原值。 */
-export function stabilizeZIndex(previous, next, hysteresis = DEPTH_Z_HYSTERESIS) {
-    if (!Number.isFinite(next)) return Number.isFinite(previous) ? previous : 1
-    if (!Number.isFinite(previous)) return next
-    return Math.abs(next - previous) > hysteresis ? next : previous
+export function rankByDepth(entries, { previousRanks = new Map(), toleranceRatio = 0.005 } = {}) {
+    const list = (Array.isArray(entries) ? entries : []).filter((item) => item && item.key)
+    const sorted = list.slice().sort((a, b) => {
+        const da = Number.isFinite(a.distance) ? a.distance : Number.POSITIVE_INFINITY
+        const db = Number.isFinite(b.distance) ? b.distance : Number.POSITIVE_INFINITY
+        // 没有距离的对象一律沉底,且彼此按 key 稳定排序(避免 Infinity-Infinity = NaN 的比较)。
+        if (!Number.isFinite(da) || !Number.isFinite(db)) {
+            if (Number.isFinite(da)) return -1
+            if (Number.isFinite(db)) return 1
+            return String(a.key).localeCompare(String(b.key))
+        }
+        const tolerance = Math.max(1, Math.min(da, db) * toleranceRatio)
+        if (Number.isFinite(da) && Number.isFinite(db) && Math.abs(da - db) <= tolerance) {
+            // 近似同深:保持上一帧谁在前;没有历史才退回 key 的稳定顺序。
+            const pa = previousRanks.get(a.key)
+            const pb = previousRanks.get(b.key)
+            if (Number.isFinite(pa) && Number.isFinite(pb) && pa !== pb) return pb - pa
+            return String(a.key).localeCompare(String(b.key))
+        }
+        return da - db
+    })
+    const ranks = new Map()
+    const total = sorted.length
+    for (let index = 0; index < total; index += 1) ranks.set(sorted[index].key, total - index)
+    return ranks
 }
 
 /** 类型字的行业色语义:沿用管线行业色单源(燃气红 / 供水蓝 / 排水绿 / 窨井紫)。 */

+ 18 - 1
src/modules/cockpit/domain/pointLocate.js

@@ -1,9 +1,26 @@
-/** Monitoring-point locate preset (the device tier is consumed by the later linkage ticket). */
+/**
+ * 点位/设备定位预设。设备档 150m 是**跳转的最小距离**(手动缩放仍可更近):
+ * 独立设备与「监测点内关联设备」统一用它,避免点击设备后镜头反而拉远;点位档保持 500m。
+ */
 export const LOCATE_DEFAULTS = Object.freeze({
     point: Object.freeze({ heading: 0, pitch: -55, range: 500 }),
     device: Object.freeze({ heading: 0, pitch: -55, range: 150 }),
 })
 
+/**
+ * 定位档位:设备(关联/独立)统一走 device 档,监测点/设施走 point 档。
+ *
+ * 维护者 2026-11-25:独立设备与「监测点内关联设备」的拉近距离必须一致,
+ * 设备档 range 是**跳转的最小距离**(手动缩放仍可更近);点位与聚合缩放保持现状。
+ * 调用方显式传 tier 时以显式为准(例如成员列表/详情里的设备定位)。
+ */
+export function locateTierFor(object, explicitTier) {
+    if (explicitTier === 'point' || explicitTier === 'device') return explicitTier
+    const kind = object && object.kind
+    if (kind === 'unassociated-equipment' || kind === 'monitoring-device' || kind === 'associated-device') return 'device'
+    return 'point'
+}
+
 export function planLocateView(map = {}, tier = 'point') {
     const defaults = LOCATE_DEFAULTS[tier]
     if (!defaults) return null

+ 4 - 2
src/modules/cockpit/services/mapService.js

@@ -27,7 +27,7 @@ import { attachSceneFocus } from './sceneInputFocus.js'
 import { formatCameraState, INITIAL_PANORAMA, RETURN_FLIGHT_MS, SETTLE_EPSILON } from '../domain/camera.js'
 import { BASEMAP_STATE, basemapStatusText, planBasemap } from '../domain/basemap.js'
 import { isAtHome, planHome, planIntro } from '../domain/viewPresets.js'
-import { isStaleLocate, planLocateView, planTerrainLocateTarget, TERRAIN_LOCATE_REASON } from '../domain/pointLocate.js'
+import { isStaleLocate, locateTierFor, planLocateView, planTerrainLocateTarget, TERRAIN_LOCATE_REASON } from '../domain/pointLocate.js'
 import {
     planDeviceLead,
     planDeviceReactions,
@@ -446,7 +446,9 @@ async function runTerrainLocate(object, { tier = 'point', centerOnly = false, on
  * 点位/设备定位:准入只认后端 `renderable`(票 07 的唯一判据,不由前端重算范围)。
  */
 export async function locateMonitoringPoint(object, options = {}) {
-    return runTerrainLocate(object, options, (item) =>
+    // 独立设备与关联设备统一走 device 档(150m):设备详情不再比聚合点更远,避免点击后镜头拉远。
+    const tier = locateTierFor(object, options.tier)
+    return runTerrainLocate(object, { ...options, tier }, (item) =>
         isRenderable(item) && Number.isFinite(item.longitude) && Number.isFinite(item.latitude))
 }
 

+ 60 - 23
src/modules/cockpit/services/markerOverlay.js

@@ -49,7 +49,7 @@ import {
 // 票 83:几何读数(本体宽 / 绘制宽 / pad)与计划读数同一出口,走查不读 DOM 内部结构。
 import { markerGeometryDataset } from '../domain/markerPlan.js'
 // 本票:未关联设备的屏幕拥挤聚合(纯 domain;本层只负责投影/渲染/点击)。
-import { AGGREGATE_BADGE, AGGREGATE_KIND, aggregateBadge, canZoomAggregate, depthZIndex, groupMonitoringPointsByScreen, groupUnassociatedByScreen, labelOccludedByAggregates, planMemberList, stabilizeZIndex } from '../domain/pointAggregation.js'
+import { AGGREGATE_BADGE, AGGREGATE_KIND, aggregateBadge, canZoomAggregate, groupMonitoringPointsByScreen, groupUnassociatedByScreen, labelOccludedByAggregates, planMemberList, rankByDepth } from '../domain/pointAggregation.js'
 
 /**
  * 类名单源:注入的特效 CSS(下方模板串)与 DOM 构建(buildNode/mount)共用同一组常量。
@@ -907,11 +907,15 @@ function applyLabelState(record, visible, expanded) {
     if (record.labelBox.style.maxWidth !== wantedWidth) record.labelBox.style.maxWidth = wantedWidth
 }
 
-/** 深度 z 的迟滞出口:同一记录只有跨过缓冲才换层,避免相机微动时反复交换。 */
-function stableDepth(record, next) {
-    const stable = stabilizeZIndex(record.zIndex, next)
-    record.zIndex = stable
-    return stable
+/** 上一帧的全局深度序号:近似同深时用它保持相对顺序。 */
+let depthRanks = new Map()
+
+/** 聚合组的深度取组内最近成员(显示入口靠近它代表的最近真实对象)。 */
+function groupDistanceOf(group) {
+    return (group.members || []).reduce((min, member) => {
+        const value = member.screen && Number.isFinite(member.screen.distance) ? member.screen.distance : Number.POSITIVE_INFINITY
+        return Math.min(min, value)
+    }, Number.POSITIVE_INFINITY)
 }
 
 /** 标签跟随节点位移(与节点同一次 transform;不逐帧读布局)。 */
@@ -1128,12 +1132,8 @@ function renderAggregates(groups, tones) {
             aggregatePool.set(key, record)
             hostNode.appendChild(record.root)
         }
-        // 深度排序:聚合徽章参与同一套画家算法,不再绝对置顶(近的图标可压住远的徽章)。
-        const groupDistance = group.members.reduce((min, member) => {
-            const value = member.screen && Number.isFinite(member.screen.distance) ? member.screen.distance : 1e8
-            return Math.min(min, value)
-        }, Infinity)
-        const badgeZ = String(stableDepth(record, depthZIndex(groupDistance) * 2))
+        // 深度排序:徽章用本帧全局序号(组内最近成员的距离已参与排序),不再绝对置顶。
+        const badgeZ = String((depthRanks.get(group.groupKey) || 1) * 2)
         if (record.root.style.zIndex !== badgeZ) record.root.style.zIndex = badgeZ
         const transform = 'translate(' + group.screen.x + 'px, ' + group.screen.y + 'px) translate(-50%, -50%)'
         if (record.root.style.transform !== transform) record.root.style.transform = transform
@@ -1451,6 +1451,44 @@ export function syncMarkerOverlay({ markers, devices, aggregates, facilities, se
     const badgeCenters = badgeGroups.map((group) => group.screen)
     const labelBlockedByAggregate = (pos) => labelOccludedByAggregates(pos, badgeCenters)
 
+    // 深度排序(2026-11-25):先收集本帧全部可见对象,一次全局排序分配**唯一**序号;
+    // 不再逐条算 z(同 z 会退回 DOM 顺序,徽章重建就会闪)。距离近似时沿用上一帧顺序。
+    const facilityPositions = new Map()
+    for (const facility of facilities || []) {
+        const key = facility.key || `facility-point|${facility.code}`
+        if (!Number.isFinite(facility.longitude) || !Number.isFinite(facility.latitude)) continue
+        const pos = projectToWindow(facility.longitude, facility.latitude)
+        if (pos && Number.isFinite(pos.x) && Number.isFinite(pos.y)) facilityPositions.set(key, pos)
+    }
+    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 pos = projectToWindow(component.longitude, component.latitude)
+        if (pos && Number.isFinite(pos.x) && Number.isFinite(pos.y)) componentPositions.set(key, pos)
+    }
+    const depthEntries = []
+    for (const marker of markers || []) {
+        if (!marker) continue
+        const key = `${marker.kind}|${marker.code}`
+        if (groupedMemberKeys.has(key)) continue
+        const pos = windowPositions.get(key)
+        if (!pos) continue
+        depthEntries.push({ key, distance: pos.distance })
+    }
+    for (const device of devices || []) {
+        if (!device || !device.code) continue
+        depthEntries.push({ key: device.key || `device|${device.code}`, distance: device.distance })
+    }
+    for (const [key, pos] of facilityPositions.entries()) depthEntries.push({ key, distance: pos.distance })
+    for (const [key, pos] of componentPositions.entries()) depthEntries.push({ key, distance: pos.distance })
+    for (const row of aggregates || []) {
+        if (row && row.key) depthEntries.push({ key: row.key, distance: row.distance })
+    }
+    for (const group of badgeGroups) depthEntries.push({ key: group.groupKey, distance: groupDistanceOf(group) })
+    depthRanks = rankByDepth(depthEntries, { previousRanks: depthRanks })
+    const depthZOf = (key) => (depthRanks.get(key) || 1) * 2
+
     for (const marker of markers || []) {
         const key = `${marker.kind}|${marker.code}`
         // 聚合组成员不单独渲染(选中成员不在组里,仍会正常显示)。
@@ -1509,9 +1547,8 @@ export function syncMarkerOverlay({ markers, devices, aggregates, facilities, se
         // 深度排序:图标 = 2z,标签 = 2z+1(标签略高于自身图标,低于更近的图标)。
         // 04:已选中的监测点单独显示时置顶,且标签不被邻近聚合折叠 —— 否则「保持选择」只剩一个点。
         const selectedPoint = marker.kind === 'monitoring-point' && marker.code === selectedPointCode
-        // 901 > depthZIndex 上限 900:选中点连同标签压过所有徽章(图标 1802、标签 1803)。
-        // 其余图标走 stableDepth:距离接近时不交换层级(深度迟滞)。
-        const markerZNum = selectedPoint ? 1802 : stableDepth(record, depthZIndex(windowPos.distance) * 2)
+        // 选中点固定压过所有图标(9000/9001);其余用本帧全局唯一序号。
+        const markerZNum = selectedPoint ? 9000 : depthZOf(key)
         const markerZ = String(markerZNum)
         if (record.root.style.zIndex !== markerZ) record.root.style.zIndex = markerZ
         if (record.labelWrap) {
@@ -1610,7 +1647,7 @@ export function syncMarkerOverlay({ markers, devices, aggregates, facilities, se
         //   · ④ 设备被选择的中心设备:隐藏(与 ② 聚合被选择态同性质)。
         //   · 非聚合设备点(k=1 关联设备):**始终显示** —— 聚合规则不得外溢到它们。
         const devicePos = { x: device.x - hostOffset.x, y: device.y - hostOffset.y }
-        const deviceZNum = stableDepth(record, depthZIndex(device.distance) * 2)
+        const deviceZNum = depthZOf('device|' + device.code)
         const deviceZ = String(deviceZNum)
         if (record.root.style.zIndex !== deviceZ) record.root.style.zIndex = deviceZ
         if (record.labelWrap) {
@@ -1677,7 +1714,7 @@ export function syncMarkerOverlay({ markers, devices, aggregates, facilities, se
         // 票 76(四态):① 的 hover 预览与 ② 的固定态都隐藏聚合标签(让出设备环);
         // ③④ 中心已是设备点 → 整枚聚合节点让位(标签随之隐藏)。
         // 2026-11-24:标签与图标同宿主、按相机距离交错层级。
-        const aggZNum = stableDepth(record, depthZIndex(row.distance) * 2)
+        const aggZNum = depthZOf(row.key)
         const aggZ = String(aggZNum)
         if (record.root.style.zIndex !== aggZ) record.root.style.zIndex = aggZ
         if (record.labelWrap) {
@@ -1699,8 +1736,8 @@ export function syncMarkerOverlay({ markers, devices, aggregates, facilities, se
         const key = facility.key || `facility-point|${facility.code}`
         wanted.add(key)
         if (!Number.isFinite(facility.longitude) || !Number.isFinite(facility.latitude)) { hidePooledNode(key); continue }
-        const windowPos = projectToWindow(facility.longitude, facility.latitude)
-        if (!windowPos || !Number.isFinite(windowPos.x) || !Number.isFinite(windowPos.y)) { hidePooledNode(key); continue }
+        const windowPos = facilityPositions.get(key)
+        if (!windowPos) { hidePooledNode(key); continue }
 
         const marker = { ...facility, kind: 'facility-point' }
         const plan = facilityPlanOf(marker)
@@ -1738,7 +1775,7 @@ export function syncMarkerOverlay({ markers, devices, aggregates, facilities, se
         const transform = `translate(${windowPos.x}px, ${windowPos.y}px)`
         if (record.root.style.transform !== transform) record.root.style.transform = transform
         if (record.root.style.display !== 'block') record.root.style.display = 'block'
-        const facilityZNum = stableDepth(record, depthZIndex(windowPos.distance) * 2)
+        const facilityZNum = depthZOf(key)
         const facilityZ = String(facilityZNum)
         if (record.root.style.zIndex !== facilityZ) record.root.style.zIndex = facilityZ
         if (record.labelWrap) {
@@ -1761,8 +1798,8 @@ export function syncMarkerOverlay({ markers, devices, aggregates, facilities, se
         const key = component.key || `pipe-component|${component.code}`
         wanted.add(key)
         if (!Number.isFinite(component.longitude) || !Number.isFinite(component.latitude)) { hidePooledNode(key); continue }
-        const windowPos = projectToWindow(component.longitude, component.latitude)
-        if (!windowPos || !Number.isFinite(windowPos.x) || !Number.isFinite(windowPos.y)) { hidePooledNode(key); continue }
+        const windowPos = componentPositions.get(key)
+        if (!windowPos) { hidePooledNode(key); continue }
 
         const marker = { ...component, kind: 'pipe-component' }
         const plan = componentPlanOf(marker)
@@ -1791,7 +1828,7 @@ export function syncMarkerOverlay({ markers, devices, aggregates, facilities, se
         const transform = `translate(${windowPos.x}px, ${windowPos.y}px)`
         if (record.root.style.transform !== transform) record.root.style.transform = transform
         if (record.root.style.display !== 'block') record.root.style.display = 'block'
-        const componentZNum = stableDepth(record, depthZIndex(windowPos.distance) * 2)
+        const componentZNum = depthZOf(key)
         const componentZ = String(componentZNum)
         if (record.root.style.zIndex !== componentZ) record.root.style.zIndex = componentZ
         if (record.labelWrap) {