You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
405 lines
13 KiB
405 lines
13 KiB
import * as THREE from 'three'
|
|
import type Viewport from '@/core/engine/Viewport'
|
|
import type BaseRenderer from '@/core/base/BaseRenderer'
|
|
import { getRenderer } from './ModuleManager'
|
|
import { getLineId, parseLineId } from '@/core/ModelUtils'
|
|
|
|
/**
|
|
* 实体管理器
|
|
* 缓存所有 数据(ItemJson)和他们的关系, 以及渲染对象 THREE.Object3D
|
|
* 数据更新的流程是:
|
|
* 1. 状态管理器 StateManager 发出数据被修改的请求, 这些请求通常只有 ItemJson 数据
|
|
* 2. 实体管理器 (EntityManager) 需要计算所有相关的点和线的变化. 在 endUpdate 时需要根据 center / in / out 关系, 计算出: 点的创建 / 点的更新 / 点的删除 / 线的创建 / 线的更新 / 线的删除
|
|
* 3. 通知对应的渲染器 (Renderer) 进行点和线的创建 / 更新 / 删除
|
|
*/
|
|
export default class EntityManager {
|
|
viewport: Viewport
|
|
|
|
// 所有数据点的实体
|
|
private readonly entities = new Map<string, ItemJson>()
|
|
|
|
// 关系索引
|
|
private readonly relationIndex = new Map<string, Relation>()
|
|
|
|
// 所有 THREEJS "点"对象, 检索值是"点实体"的 id, 值是 THREE.Object3D 数组
|
|
private readonly objects = new Map<string, THREE.Object3D[]>()
|
|
|
|
// 所有 THREEJS "线"对象, 检索值是"线实体"的 id, 取值方式是 {type}${startId}${endId}, 值是 THREE.Object3D 数组
|
|
private readonly lines = new Map<string, THREE.Object3D[]>()
|
|
|
|
// 差量渲染器
|
|
private readonly diffRenderer = new Map<string, BaseRenderer>()
|
|
// 线差量记录
|
|
private readonly lineDiffs = {
|
|
create: new Map<string, LineDiffItem>(),
|
|
update: new Map<string, LineDiffItem>(),
|
|
delete: new Map<string, LineDiffItem>()
|
|
}
|
|
isUpdating = false
|
|
|
|
init(viewport: Viewport) {
|
|
this.viewport = viewport
|
|
}
|
|
|
|
/**
|
|
* 批量更新开始
|
|
*/
|
|
beginEntityUpdate(): void {
|
|
this.isUpdating = true
|
|
this.viewport.beginViewUpdate()
|
|
this.diffRenderer.clear()
|
|
this.lineDiffs.create.clear()
|
|
this.lineDiffs.update.clear()
|
|
this.lineDiffs.delete.clear()
|
|
}
|
|
|
|
/**
|
|
* 创建或更新一个实体, 这个点的 center[] / in[] / out[] 关联的点, 可能都要对应进行关联
|
|
*/
|
|
createOrUpdateEntity(entityRaw: ItemJson, option: EntityCudOption = {}): void {
|
|
if (!entityRaw?.id) {
|
|
throw new Error('Entity must have an id')
|
|
}
|
|
const entity = _.cloneDeep(entityRaw) as ItemJson
|
|
|
|
// 找到这个数据的渲染器
|
|
const renderer = this.getDiffRenderer(entity.t)
|
|
|
|
const originEntity = this.entities.get(entity.id)
|
|
this.entities.set(entity.id, entity)
|
|
|
|
// 更新关系网
|
|
this.updateRelations(entity, originEntity)
|
|
|
|
// 判断坐标是否变化
|
|
|
|
if (originEntity?.tf && entity?.tf && !_.isEqual(originEntity.tf[0], entity.tf[0])) {
|
|
// 点的坐标发生变化, 要通知所有关联线更新
|
|
const relations = this.relationIndex.get(entity.id)
|
|
if (!relations) return
|
|
|
|
for (const type of (['center', 'in', 'out'] as LinkType[])) {
|
|
const relatedIds = relations[type]
|
|
if (!relatedIds) continue
|
|
|
|
for (const relatedId of relatedIds) {
|
|
const lineId = getLineId(entity.id, relatedId, type)
|
|
this.lineDiffs.update.set(lineId, { startId: entity.id, endId: relatedId, type })
|
|
|
|
// 如果是双向线(比如 center),也要反向加一次
|
|
if (type === 'center') {
|
|
this.lineDiffs.update.set(lineId, { startId: relatedId, endId: entity.id, type })
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (typeof originEntity === 'undefined') {
|
|
renderer.createPoint(entity, option)
|
|
|
|
} else {
|
|
option.originEntity = _.cloneDeep(originEntity)
|
|
renderer.updatePoint(entity, option)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 删除实体, 与这个点有关的所有线都要删除, 与这个点有关系的点,都要对应的 center[] / in[] / out[] 都要断绝关系
|
|
*/
|
|
deleteEntity(id: string, option: EntityCudOption = {}): void {
|
|
const entity = this.entities.get(id)
|
|
if (!entity) return
|
|
|
|
option.originEntity = _.cloneDeep(entity)
|
|
|
|
this.entities.delete(id)
|
|
this.removeRelations(id)
|
|
this.getDiffRenderer(entity.t).deletePoint(id, option)
|
|
}
|
|
|
|
/**
|
|
* 批量更新结束, 结束后会触发视窗的渲染
|
|
* 这个方法最重要的是进行连线逻辑的处理
|
|
* - 如果进行了添加, 那么这个点的 center[] / in[] / out[] 关联的点, 都要对应进行关联
|
|
* - 如果进行了删除, 与这个点有关的所有线都要删除, 与这个点有关系的点,都要对应的 center[] / in[] / out[] 都要断绝关系
|
|
* - 如果进行了更新, 如果改了颜色/位置, 则需要在UI上进行对应修改,如果改了关系,需要与关联的节点批量调整
|
|
* 将影响到的所有数据, 都变成一个修改集合, 统一调用对应单元类型渲染器(BaseRenderer)的 createPoint / deletePoint / updatePoint / createLine / updateLine / deleteLine 方法
|
|
*/
|
|
endEntityUpdate(): void {
|
|
for (const [itemTypeName, renderer] of this.diffRenderer.entries()) {
|
|
for (const [lineId, lineDiffItem] of this.lineDiffs.create.entries()) {
|
|
const start = this.entities.get(lineDiffItem.startId)
|
|
const end = this.entities.get(lineDiffItem.endId)
|
|
if (start.t !== itemTypeName) {
|
|
// 只通知起点对应的渲染器
|
|
continue
|
|
}
|
|
renderer.createLine(start, end, lineDiffItem.type)
|
|
}
|
|
|
|
for (const [lineId, lineDiffItem] of this.lineDiffs.update.entries()) {
|
|
const start = this.entities.get(lineDiffItem.startId)
|
|
const end = this.entities.get(lineDiffItem.endId)
|
|
if (start.t !== itemTypeName) {
|
|
// 只通知起点对应的渲染器
|
|
continue
|
|
}
|
|
renderer.updateLine(start, end, lineDiffItem.type)
|
|
}
|
|
|
|
for (const [lineId, lineDiffItem] of this.lineDiffs.delete.entries()) {
|
|
const start = this.entities.get(lineDiffItem.startId)
|
|
const end = this.entities.get(lineDiffItem.endId)
|
|
if (start.t !== itemTypeName) {
|
|
// 只通知起点对应的渲染器
|
|
continue
|
|
}
|
|
renderer.deleteLine(start, end, lineDiffItem.type)
|
|
}
|
|
|
|
renderer.endRendererUpdate()
|
|
}
|
|
this.viewport.endViewUpdate()
|
|
this.isUpdating = false
|
|
}
|
|
|
|
/**
|
|
* 更新关系关系网, 计算出差值, 可以临时放在 diffRenderer 中, 等待 commitUpdate 时统一处理
|
|
*/
|
|
private updateRelations(entity: ItemJson, originEntity?: ItemJson): void {
|
|
const { id, dt } = entity
|
|
if (!id || !dt) return
|
|
|
|
const oldCenter = new Set(originEntity?.dt?.center || [])
|
|
const oldIn = new Set(originEntity?.dt?.in || [])
|
|
const oldOut = new Set(originEntity?.dt?.out || [])
|
|
|
|
const newCenter = new Set(dt.center || [])
|
|
const newIn = new Set(dt.in || [])
|
|
const newOut = new Set(dt.out || [])
|
|
|
|
// 更新正向关系
|
|
const relations = this.relationIndex.get(id) || new Relation()
|
|
relations.center = newCenter
|
|
relations.input = newIn
|
|
relations.output = newOut
|
|
this.relationIndex.set(id, relations)
|
|
|
|
// 更新反向关系
|
|
this.updateReverseRelations(id, oldCenter, newCenter, 'center')
|
|
this.updateReverseRelations(id, oldIn, newIn, 'out') // 入边的反向是出边
|
|
this.updateReverseRelations(id, oldOut, newOut, 'in') // 出边的反向是入边
|
|
|
|
// 更新线差量
|
|
this.calculateLineDiffs(id, oldCenter, newCenter, 'center')
|
|
this.calculateLineDiffs(id, oldIn, newIn, 'in')
|
|
this.calculateLineDiffs(id, oldOut, newOut, 'out')
|
|
}
|
|
|
|
private updateReverseRelations(id: string, oldIds: Set<string>, newIds: Set<string>, relationType: LinkType) {
|
|
// 移除旧关系
|
|
for (const relatedId of oldIds) {
|
|
if (!newIds.has(relatedId)) {
|
|
const rev = this.relationIndex.get(relatedId)
|
|
rev.delete(relationType, id)
|
|
}
|
|
}
|
|
|
|
// 添加新关系
|
|
for (const relatedId of newIds) {
|
|
if (!oldIds.has(relatedId)) {
|
|
let rev = this.relationIndex.get(relatedId)
|
|
if (!rev) {
|
|
rev = new Relation()
|
|
this.relationIndex.set(relatedId, rev)
|
|
}
|
|
rev.add(relationType, id)
|
|
}
|
|
}
|
|
}
|
|
|
|
private calculateLineDiffs(id: string, oldIds: Set<string>, newIds: Set<string>, lineType: LinkType) {
|
|
// 删除被移除的线
|
|
for (const relatedId of oldIds) {
|
|
if (!newIds.has(relatedId)) {
|
|
const lineId = getLineId(id, relatedId, lineType)
|
|
this.lineDiffs.delete.set(lineId, { startId: id, endId: relatedId, type: lineType })
|
|
}
|
|
}
|
|
|
|
// 新增新增的线
|
|
for (const relatedId of newIds) {
|
|
if (!oldIds.has(relatedId)) {
|
|
const lineId = getLineId(id, relatedId, lineType)
|
|
this.lineDiffs.create.set(lineId, { startId: id, endId: relatedId, type: lineType })
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 重命名一个点
|
|
* 注意, 不能在更新时刻改名. 所有的关系节点都应该改名
|
|
*/
|
|
renamePoint(newId: string, originId: string) {
|
|
if (this.isUpdating) {
|
|
throw new Error('Cannot rename point during update')
|
|
}
|
|
const entity = this.entities.get(originId)
|
|
if (!entity) {
|
|
throw new Error(`Entity with id ${originId} does not exist`)
|
|
}
|
|
if (this.entities.has(newId)) {
|
|
throw new Error(`Entity with id ${newId} already exists`)
|
|
}
|
|
entity.id = newId
|
|
this.entities.set(newId, entity)
|
|
this.entities.delete(originId)
|
|
this.objects.set(newId, this.objects.get(originId) || [])
|
|
this.objects.delete(originId)
|
|
|
|
// 更新关系索引
|
|
const relations = this.relationIndex.get(originId)
|
|
if (relations) {
|
|
this.relationIndex.delete(originId)
|
|
|
|
// 更新所有关系中的 id
|
|
relations.center.forEach((relatedId) => {
|
|
const rev = this.relationIndex.get(relatedId)
|
|
if (rev && rev.delete('center', originId)) {
|
|
rev.add('center', newId)
|
|
}
|
|
})
|
|
relations.input.forEach((relatedId) => {
|
|
const rev = this.relationIndex.get(relatedId)
|
|
if (rev && rev.delete('out', originId)) {
|
|
rev.add('out', newId)
|
|
}
|
|
})
|
|
relations.output.forEach((relatedId) => {
|
|
const rev = this.relationIndex.get(relatedId)
|
|
if (rev && rev.delete('in', originId)) {
|
|
rev.add('in', newId)
|
|
}
|
|
})
|
|
|
|
this.relationIndex.set(newId, relations)
|
|
}
|
|
|
|
// 更新所有线段数据
|
|
for (const [lineId, lineObjects] of this.lines.entries()) {
|
|
const [type, startId, endId] = parseLineId(lineId)
|
|
if (startId === originId) {
|
|
const newLineId = getLineId(newId, endId, type)
|
|
this.lines.set(newLineId, lineObjects)
|
|
this.lines.delete(lineId)
|
|
|
|
} else if (endId === originId) {
|
|
const newLineId = getLineId(startId, newId, type)
|
|
this.lines.set(newLineId, lineObjects)
|
|
this.lines.delete(lineId)
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 删除关系关系网, 计算出差值, 可以临时放在 diffRenderer 中, 等待 commitUpdate 时统一处理
|
|
*/
|
|
private removeRelations(id: string): void {
|
|
const relations = this.relationIndex.get(id)
|
|
if (!relations) return
|
|
|
|
const removeLine = (relatedId: string, type: LinkType) => {
|
|
const lineId = getLineId(id, relatedId, type)
|
|
this.lineDiffs.delete.set(lineId, { startId: id, endId: relatedId, type })
|
|
}
|
|
|
|
relations.center.forEach((relatedId) => {
|
|
this.relationIndex.get(relatedId)?.center.delete(id)
|
|
removeLine(relatedId, 'center')
|
|
})
|
|
|
|
relations.input.forEach((relatedId) => {
|
|
this.relationIndex.get(relatedId)?.output.delete(id)
|
|
removeLine(relatedId, 'in')
|
|
})
|
|
|
|
relations.output.forEach((relatedId) => {
|
|
this.relationIndex.get(relatedId)?.input.delete(id)
|
|
removeLine(relatedId, 'out')
|
|
})
|
|
|
|
this.relationIndex.delete(id)
|
|
}
|
|
|
|
private getDiffRenderer(type: string) {
|
|
let renderer = this.diffRenderer.get(type)
|
|
if (typeof renderer === 'undefined') {
|
|
renderer = getRenderer(type)
|
|
renderer.beginRendererUpdate(this.viewport)
|
|
this.diffRenderer.set(type, renderer)
|
|
}
|
|
return renderer
|
|
}
|
|
|
|
deleteEntityOnly(id: string) {
|
|
return this.entities.delete(id)
|
|
}
|
|
|
|
findObjectsById(id: string) {
|
|
return this.objects.get(id) || []
|
|
}
|
|
|
|
deleteObjectsOnly(id: string) {
|
|
return this.objects.delete(id)
|
|
}
|
|
|
|
appendObject(id: string, points: THREE.Object3D[]) {
|
|
this.objects.set(id, points)
|
|
}
|
|
|
|
appendLineObject(id: string, lines: THREE.Object3D[]) {
|
|
this.lines.set(id, lines)
|
|
}
|
|
|
|
findLineObjectsById(lineId: string): THREE.Object3D[] {
|
|
return this.lines.get(lineId) || []
|
|
}
|
|
|
|
deleteLineObjectOnly(id: string) {
|
|
return this.lines.delete(id)
|
|
}
|
|
}
|
|
|
|
interface LineDiffItem {
|
|
startId: string
|
|
endId: string
|
|
type: LinkType
|
|
}
|
|
|
|
export class Relation {
|
|
center = new Set<string>()
|
|
input = new Set<string>()
|
|
output = new Set<string>()
|
|
|
|
add(type: LinkType, id: string) {
|
|
if (type === 'in')
|
|
return this.input.add(id)
|
|
else if (type === 'out')
|
|
return this.output.add(id)
|
|
else if (type === 'center')
|
|
return this.center.add(id)
|
|
else
|
|
throw new Error(`Unknown link type: ${type}`)
|
|
}
|
|
|
|
delete(type: LinkType, id: string) {
|
|
if (type === 'in')
|
|
return this.input.delete(id)
|
|
else if (type === 'out')
|
|
return this.output.delete(id)
|
|
else if (type === 'center')
|
|
return this.center.delete(id)
|
|
else
|
|
throw new Error(`Unknown link type: ${type}`)
|
|
}
|
|
}
|