diff --git a/modules/brep/io/brepLoopsFormat.js b/modules/brep/io/brepLoopsFormat.ts similarity index 65% rename from modules/brep/io/brepLoopsFormat.js rename to modules/brep/io/brepLoopsFormat.ts index 2fcf7aab..ddc86dca 100644 --- a/modules/brep/io/brepLoopsFormat.js +++ b/modules/brep/io/brepLoopsFormat.ts @@ -1,7 +1,18 @@ +import {Loop} from "brep/topo/loop"; +import {Shell} from "brep/topo/shell"; +import {Vec3} from "math/vec"; +import {Vertex} from "brep/topo/vertex"; -export function toLoops(shell, presicion) { +export interface BREPLoopsFormat { + format: 'LOOPS'; + vertices: Vec3[]; + faces: number[][]; - const fl = presicion || (v => v); +} + +export function toLoops(shell: Shell, precisionFn: (number) => number): BREPLoopsFormat { + + const fl = precisionFn || (v => v); const vertices = []; for (let v of shell.vertices) { @@ -29,7 +40,7 @@ export function toLoops(shell, presicion) { }; } -function sortByXYZ(vertices) { +function sortByXYZ(vertices: Vertex[]) { vertices.sort((v1, v2) => { let c = v1.point.x - v2.point.x; if (c === 0) { diff --git a/modules/brep/topo/face.ts b/modules/brep/topo/face.ts index 2ab2e4a7..5c11e9e9 100644 --- a/modules/brep/topo/face.ts +++ b/modules/brep/topo/face.ts @@ -4,8 +4,9 @@ import PIP from '../../../web/app/cad/tess/pip'; import {veq} from "geom/tolerance"; import {isOnPositiveHalfPlaneFromVec} from "../operations/boolean"; import {BrepSurface} from "geom/surfaces/brepSurface"; -import {Shell} from "./shell"; +import {edgesGenerator, Shell} from "./shell"; import {ProductionInfo} from "../../../web/app/cad/craft/engine/productionInfo"; +import {HalfEdge} from "brep/topo/edge"; declare module './face' { @@ -35,14 +36,20 @@ export class Face extends TopoObject { innerLoops: Loop[]; private __2d: any; + loops = { + [Symbol.iterator]: () => loopsGenerator(this) + }; + edges = { + [Symbol.iterator]: () => halfEdgesGenerator(this) + }; + + constructor(surface: BrepSurface) { super(); this.surface = surface; this.shell = null; this.outerLoop = new Loop(this); this.innerLoops = []; - this.defineIterable('loops', () => loopsGenerator(this)); - this.defineIterable('edges', () => halfEdgesGenerator(this)); } get id(): string { @@ -153,7 +160,7 @@ export class Face extends TopoObject { } } -export function* loopsGenerator(face) { +export function* loopsGenerator(face): Generator { if (face.outerLoop !== null) { yield face.outerLoop; } @@ -162,7 +169,7 @@ export function* loopsGenerator(face) { } } -export function* halfEdgesGenerator(face) { +export function* halfEdgesGenerator(face): Generator { for (let loop of face.loops) { for (let halfEdge of loop.halfEdges) { yield halfEdge; diff --git a/modules/brep/topo/loop.ts b/modules/brep/topo/loop.ts index dc596d43..bfcd0c27 100644 --- a/modules/brep/topo/loop.ts +++ b/modules/brep/topo/loop.ts @@ -3,19 +3,21 @@ import {Face} from "./face"; import {BrepSurface} from "geom/surfaces/brepSurface"; import {HalfEdge} from "./edge"; import {findLowestLeftPoint} from "geom/euclidean"; +import {Vertex} from "brep/topo/vertex"; export class Loop extends TopoObject { face: Face; halfEdges: HalfEdge[]; - encloses: any; + + encloses = { + [Symbol.iterator]: () => enclosesGenerator(this.halfEdges) + }; constructor(face: Face) { super(); this.face = face; this.halfEdges = []; - this.encloses = undefined; - this.defineIterable('encloses', () => enclosesGenerator(this.halfEdges)); } isCCW(surface: BrepSurface) { @@ -68,7 +70,7 @@ export class Loop extends TopoObject { }; } -export function* enclosesGenerator(halfEdges) { +export function* enclosesGenerator(halfEdges): Generator<[HalfEdge, HalfEdge, Vertex]> { let length = halfEdges.length; for (let i = 0; i < halfEdges.length; i++) { let j = (i + 1) % length; diff --git a/modules/brep/topo/shell.ts b/modules/brep/topo/shell.ts index f63cb0fc..f9c0f6cd 100644 --- a/modules/brep/topo/shell.ts +++ b/modules/brep/topo/shell.ts @@ -1,20 +1,27 @@ import {TopoObject} from './topo-object' import {Face} from "./face"; import {Loop} from "./loop"; -import {Edge} from "./edge"; +import {Vertex} from "brep/topo/vertex"; +import {Edge} from "brep/topo/edge"; + export class Shell extends TopoObject { faces: Face[]; - edges: Edge[]; + + vertices = { + [Symbol.iterator]: () => verticesGenerator(this) + }; + + edges = { + [Symbol.iterator]: () => edgesGenerator(this.faces) + }; constructor() { super(); this.faces = []; - this.defineIterable('vertices', () => verticesGenerator(this)); - this.defineIterable('edges', () => edgesGenerator(this.faces)) } - + clone(): Shell { let edgeClones = new Map(); for (let e of this.edges) { @@ -48,7 +55,7 @@ export class Shell extends TopoObject { } } -export function* verticesGenerator(shell: Shell) { +export function* verticesGenerator(shell: Shell): Generator { const seen = new Set(); for (let face of shell.faces) { for (let edge of face.edges) { @@ -60,7 +67,7 @@ export function* verticesGenerator(shell: Shell) { } } -export function* edgesGenerator(faces: Face[]) { +export function* edgesGenerator(faces: Face[]): Generator { const visited = new Set(); for (let face of faces) { for (let halfEdge of face.edges) { diff --git a/modules/brep/topo/topo-object.ts b/modules/brep/topo/topo-object.ts index c0fff075..74d8d473 100644 --- a/modules/brep/topo/topo-object.ts +++ b/modules/brep/topo/topo-object.ts @@ -13,10 +13,6 @@ export class TopoObject { }); } - defineIterable(name, iteratorFactory) { - this[name] = {}; - this[name][Symbol.iterator] = iteratorFactory; - } } export type OperationTemporaryData = any;