-
Notifications
You must be signed in to change notification settings - Fork 208
/
CurveProcessor.ts
146 lines (136 loc) · 5.36 KB
/
CurveProcessor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Curve
*/
import { AnyCurve } from "./CurveTypes";
import { BagOfCurves, CurveCollection } from "./CurveCollection";
import { CurvePrimitive } from "./CurvePrimitive";
import { Loop } from "./Loop";
import { ParityRegion } from "./ParityRegion";
import { Path } from "./Path";
import { UnionRegion } from "./UnionRegion";
/* eslint-disable @typescript-eslint/naming-convention, no-empty */
/** base class for detailed traversal of curve artifacts.
* * This recurses to children in the quickest way (no records of path)
* * Use the RecursiveCurveProcessorWithStack to record the path along the visit.
* @public
*/
export abstract class RecursiveCurveProcessor {
protected constructor() {
}
/** process error content */
public announceUnexpected(_data: AnyCurve, _indexInParent: number) { }
/** process a leaf primitive. */
public announceCurvePrimitive(_data: CurvePrimitive, _indexInParent = -1): void { }
/** announce a path (recurse to children) */
public announcePath(data: Path, _indexInParent: number = -1): void {
let i = 0;
for (const curve of data.children)
this.announceCurvePrimitive(curve, i++);
}
/** announce a loop (recurse to children) */
public announceLoop(data: Loop, _indexInParent: number = -1): void {
let i = 0;
for (const curve of data.children)
this.announceCurvePrimitive(curve, i++);
}
/** announce beginning or end of loops in a parity region */
public announceParityRegion(data: ParityRegion, _indexInParent: number = -1): void {
let i = 0;
for (const loop of data.children)
this.announceLoop(loop, i++);
}
/** announce beginning or end of a parity region */
public announceUnionRegion(data: UnionRegion, _indexInParent: number = -1): void {
let i = 0;
for (const child of data.children) {
child.announceToCurveProcessor(this, i++);
}
}
/** announce a bag of curves.
* * The default implementation visits each child and calls the appropriate dispatch to
* * `this.announceCurvePrimitive(child)`
* * `child.announceToCurveProcessor(this)`
*/
public announceBagOfCurves(data: BagOfCurves, _indexInParent: number = -1): void {
for (const child of data.children) {
if (child instanceof CurvePrimitive)
this.announceCurvePrimitive(child);
else
child.announceToCurveProcessor(this);
}
}
}
/** base class for detailed traversal of curve artifacts
* * During recursion, maintains a stack that shows complete path to each artifact.
* * Use the QuickRecursiveCurveProcessor to visit without recording the path.
* @public
*/
export abstract class RecursiveCurveProcessorWithStack extends RecursiveCurveProcessor {
/** Stack of curve collections that are "up the tree" from the current point of the traversal. */
protected _stack: CurveCollection[];
protected constructor() {
super();
this._stack = [];
}
/** Push `data` onto the stack so its status is available during processing of children.
* * Called when `data` is coming into scope.
*/
public enter(data: CurveCollection) { this._stack.push(data); }
/** Pop the stack
* * called when the top of the stack goes out of scope
*/
public leave(): CurveCollection | undefined { return this._stack.pop(); }
/** process error content */
public override announceUnexpected(_data: AnyCurve, _indexInParent: number) { }
/** process a leaf primitive. */
public override announceCurvePrimitive(_data: CurvePrimitive, _indexInParent = -1): void { }
/** announce a path (recurse to children) */
public override announcePath(data: Path, indexInParent: number = -1): void {
this.enter(data);
super.announcePath(data, indexInParent);
this.leave();
}
/** announce a loop (recurse to children) */
public override announceLoop(data: Loop, indexInParent: number = -1): void {
this.enter(data);
super.announceLoop(data, indexInParent);
this.leave();
}
/** announce beginning or end of loops in a parity region */
public override announceParityRegion(data: ParityRegion, _indexInParent: number = -1): void {
this.enter(data);
let i = 0;
for (const loop of data.children)
this.announceLoop(loop, i++);
this.leave();
}
/** announce beginning or end of a parity region */
public override announceUnionRegion(data: UnionRegion, indexInParent: number = -1): void {
this.enter(data);
super.announceUnionRegion(data, indexInParent);
this.leave();
}
/**
* Announce members of an unstructured collection.
* * push the collection reference on the stack
* * announce children
* * pop the stack
* @param data the collection
* @param _indexInParent index where the collection appears in its parent.
*/
public override announceBagOfCurves(data: BagOfCurves, _indexInParent: number = -1): void {
this.enter(data);
let i = 0;
for (const child of data.children) {
if (child instanceof CurvePrimitive)
this.announceCurvePrimitive(child, i++);
else
child.announceToCurveProcessor(this);
}
this.leave();
}
}