From 69c08eec7a42e8c2c124cb9b694d6f6273117ea9 Mon Sep 17 00:00:00 2001 From: Val Erastov Date: Sun, 17 Jun 2018 23:02:39 -0700 Subject: [PATCH] keep vertex to edges index outside of Vertex class --- web/app/brep/edgeIndex.js | 54 +++++++++++++++++++++++++++++++++++++ web/app/brep/topo/edge.js | 3 --- web/app/brep/topo/vertex.js | 10 ------- 3 files changed, 54 insertions(+), 13 deletions(-) create mode 100644 web/app/brep/edgeIndex.js diff --git a/web/app/brep/edgeIndex.js b/web/app/brep/edgeIndex.js new file mode 100644 index 00000000..03ecddf0 --- /dev/null +++ b/web/app/brep/edgeIndex.js @@ -0,0 +1,54 @@ +import {Edge} from './topo/edge'; +import BrepCurve from './geom/curves/brepCurve'; + +export default class EdgeIndex { + + constructor() { + this.index = new Map(); + } + + addEdge(edge, tag) { + if (edge.halfEdge1) { + this.addHalfEdge(edge.halfEdge1, tag); + } + if (edge.halfEdge2) { + this.addHalfEdge(edge.halfEdge2, tag); + } + } + + addHalfEdge(he, tag) { + this._edgesForVertex(he.vertexA).add([he, tag]); + } + + _edgesForVertex(v) { + let edges = this.index.get(v); + if (!edges) { + edges = new Set(); + this.index.set(v, edges); + } + return edges; + } + + getHalfEdge(a, b, tag) { + let edges = this.index.get(a); + if (edges) { + for (let [he, _tag] of edges) { + if (he.vertexB === b && (tag === undefined || tag === _tag)) { + return he; + } + } + } + return null; + } + + getHalfEdgeOrCreate(a, b, curveCreate, invertedToCurve, tag) { + let he = this.getHalfEdge(a, b, tag); + if (he === null) { + let curve = curveCreate ? curveCreate() : BrepCurve.createLinearCurve(a.point, b.point); + const e = new Edge(curve, invertedToCurve?b:a, invertedToCurve?a:b); + he = invertedToCurve ? e.halfEdge2 : e.halfEdge1; + this.addEdge(e, tag); + } + return he; + } +} \ No newline at end of file diff --git a/web/app/brep/topo/edge.js b/web/app/brep/topo/edge.js index e3ebe1cf..1e01a669 100644 --- a/web/app/brep/topo/edge.js +++ b/web/app/brep/topo/edge.js @@ -1,6 +1,5 @@ import {TopoObject} from './topo-object' import {Vertex} from "./vertex"; -import {Point} from "../geom/point"; export class Edge extends TopoObject { @@ -9,8 +8,6 @@ export class Edge extends TopoObject { this.curve = curve; this.halfEdge1 = new HalfEdge(this, false, a, b); this.halfEdge2 = new HalfEdge(this, true, b, a); - a.edges.add(this.halfEdge1); - b.edges.add(this.halfEdge2); } static fromCurve(curve) { diff --git a/web/app/brep/topo/vertex.js b/web/app/brep/topo/vertex.js index afa21232..0d467738 100644 --- a/web/app/brep/topo/vertex.js +++ b/web/app/brep/topo/vertex.js @@ -5,15 +5,5 @@ export class Vertex extends TopoObject { constructor(point) { super(); this.point = point; - this.edges = new Set(); - } - - edgeFor(other) { - for (let e of this.edges) { - if (e.vertexB == other) { - return e; - } - } - return null; } } \ No newline at end of file