keep vertex to edges index outside of Vertex class

This commit is contained in:
Val Erastov 2018-06-17 23:02:39 -07:00
parent fae6aa3492
commit 69c08eec7a
3 changed files with 54 additions and 13 deletions

54
web/app/brep/edgeIndex.js Normal file
View file

@ -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;
}
}

View file

@ -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) {

View file

@ -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;
}
}