UNPKG

@realsee/dnalogel

Version:
126 lines (125 loc) 3.74 kB
var c = Object.defineProperty; var f = (r, i, e) => i in r ? c(r, i, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[i] = e; var o = (r, i, e) => (f(r, typeof i != "symbol" ? i + "" : i, e), e); import a from "./line.js"; import m from "./point.js"; import { uuid as P } from "../../shared-utils/uuid.js"; import { Subscribe as g } from "@realsee/five"; import "../utils/line.js"; import "@realsee/five/line"; import "../utils/constants.js"; import "three"; import "../utils/dom/distanceItem.js"; import "../utils/dom/base.js"; import "../utils/isNDCPointInScreen.js"; import "../../shared-utils/three/centerPoint.js"; class M { constructor(i) { o(this, "id", P()); o(this, "lines", []); o(this, "visiblePanoIndexes"); o(this, "model"); o(this, "type", "polyline"); o(this, "hook", new g()); this.model = i.model; } get points() { const i = /* @__PURE__ */ new Set(); return this.lines.forEach((e) => { i.add(e.points[0]), i.add(e.points[1]); }), Array.from(i); } addLine(i) { if (this.includes(i)) { console.error(new Error("不能重复添加线段")); return; } const e = this.getLastPoint(), l = e && i.findAnotherPoint(e); if (e && !l) { console.error(new Error("传入线段无法与当前折线进行连接")); return; } this.lines.push(i), this.hook.emit("lineAdded", i); } removeLine(i) { if (this.lines.length === 0) return; if (!this.isLastLine(i)) { console.error(new Error("传入线段不是当前折线的最后一段")); return; } this.lines.pop() && this.hook.emit("lineRemoved", i); } isLastLine(i) { const e = this.getLastLine(); return e ? i.id === e.id : !1; } getLastLine() { return this.lines.slice(-1)[0]; } /** * @description: 获取连续线段的顶点数量 */ getPointLength() { return this.lines.length + 1; } clear() { const i = this.lines.slice(); this.lines.length = 0, i.reduceRight((e, l) => this.hook.emit("lineRemoved", l), void 0); } /** 是否产生重叠 */ overlapWith(i) { return i.type === "point" ? this.includes(i) : i.type === "line" ? i.points.some((e) => this.includes(e)) : i.type === "polyline" ? i.points.some((e) => this.includes(e)) : !0; } includes(i) { return i.type === "point" ? this.points.find(({ id: e }) => e === i.id) !== void 0 : i.type === "line" ? this.lines.find(({ id: e }) => e === i.id) !== void 0 : !1; } isEmpty() { return this.lines.length === 0; } getLastPoint() { return this.points.slice(-1)[0]; } toJson() { return { id: this.id, lines: this.lines.map((i) => i.toJson()), visiblePanoIndexes: this.visiblePanoIndexes }; } getPointByID(i) { return this.points.find(({ id: e }) => e === i); } fromJson(i) { this.clear(), this.id = i.id; const { getPoint: e } = u(), l = i.lines.map((t) => { const [d, p] = t.points, n = e(d), h = e(p); if (!n || !h) throw new Error("线上的点不存在"); const s = new a(n, h, this.model); return s.id = t.id, s.text = t.text, s; }); this.lines.push(...l), this.visiblePanoIndexes = i.visiblePanoIndexes; function u() { const t = /* @__PURE__ */ new Map(); function d(n) { const h = t.get(n.id); if (h) return h; const s = new m(n.position); return s.id = n.id, t.set(n.id, s), s; } function p() { return Array.from(t.values()); } return { getPoint: d, getAllPoints: p }; } return this; } replace(i) { this.id = i.id, this.clear(), this.lines.push(...i.lines); } } export { M as Polyline };