/
api.ts
703 lines (576 loc) · 14.5 KB
/
api.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
import { ICopy, IObjectOf, IToHiccup } from "@thi.ng/api";
import { isNumber } from "@thi.ng/checks";
import { equiv } from "@thi.ng/equiv";
import { illegalState } from "@thi.ng/errors";
import {
add2,
add3,
maddN2,
ReadonlyVec,
set,
Vec
} from "@thi.ng/vectors";
import { arcPointAt, arcPointAtTheta } from "./internal/arc-point";
import { copyPoints } from "./internal/copy-points";
export const enum SegmentType {
MOVE,
LINE,
POLYLINE,
ARC,
CUBIC,
QUADRATIC,
CLOSE,
}
export const enum Type {
AABB = 1,
ARC,
CIRCLE,
CUBIC,
CUBIC3,
ELLIPSE,
GROUP,
LINE,
LINE3,
PATH,
POINTS,
POINTS3,
POLYGON,
POLYGON3,
POLYLINE,
POLYLINE3,
QUAD,
QUAD3,
QUADRATIC,
QUADRATIC3,
RECT,
SPHERE,
TRIANGLE,
TRIANGLE3,
RAY,
RAY3,
}
export const enum IntersectionType {
NONE,
PARALLEL,
COINCIDENT,
COINCIDENT_NO_INTERSECT,
INTERSECT,
INTERSECT_OUTSIDE,
}
export const DEFAULT_SAMPLES = 20;
export type Attribs = IObjectOf<any>;
export type Tessellator = (points: Vec[]) => Vec[][];
export type VecPair = [Vec, Vec];
export interface IShape extends
ICopy<IShape> {
readonly type: number | string;
attribs?: Attribs;
}
export interface AABBLike extends IShape {
pos: Vec;
size: Vec;
max(): Vec;
}
export interface IHiccupShape extends IShape, IToHiccup { }
export interface IHiccupPathSegment {
toHiccupPathSegments(): any[];
}
export interface IntersectionResult {
type: IntersectionType;
}
export interface LineIntersection extends IntersectionResult {
isec?: Vec;
det?: number;
alpha?: number;
beta?: number;
}
export interface PathSegment {
type: SegmentType;
point?: Vec;
geo?: IShape & IHiccupPathSegment;
}
export interface PCLike extends IShape {
points: Vec[];
}
export interface PCLikeConstructor {
new(pts: Vec[], attribs: Attribs): PCLike;
}
export interface SamplingOpts {
/**
* Number of points to sample & return. Defaults to the implementing
* type's `DEFAULT_RES` if neither this nor `theta` option is given
* (see `ArcSamplingOpts`).
*/
num: number;
/**
* Approximate desired distance between sampled result points. If
* given, takes priority over the `num` option, but the latter MIGHT
* be used as part of the sampling process (implementation
* specific). Note: For circles this value is interpreted as arc
* length, not cartesian distance (error will be proportional to the
* given value relative to the circle's radius).
*/
dist: number;
/**
* Currently only used by these types:
*
* - Arc
* - Circle
*
* Defines the target angle between sampled points. If greater than
* the actual range of the arc, only the two end points will be
* returned at most. This option is used to derive a `num` value and
* takes priority if `num` is given as well.
*
* This option is useful to adapt the sampling based on angular
* resolution, rather than a fixed number of samples.
*/
theta: number;
/**
* If `true`, the shape's end point will be included in the result
* array. The default setting for open geometries is `true`, for
* closed ones `false`. This option has no influence on any internal
* resolution calculation.
*
* For open geometry this option is useful to when re-sampling paths
* of consecutive segments, where the end points of each segment
* coincide with the start points of the next segment. For all but
* the last segment, this option should be `false` and so can be
* used to avoid duplicate vertices in the concatenated result.
*
* When sampling closed shapes, enabling this option will include an
* extra point (start), i.e. if the `num` option was given, results
* in `num+1` points.
*/
last: boolean;
}
export interface SubdivKernel {
fn: (pts: ReadonlyVec[], i: number, nump: number) => Vec[];
iter?: (pts: ReadonlyVec[]) => Iterable<ReadonlyVec>;
size: number;
}
export abstract class APC implements
PCLike {
points: Vec[];
attribs: Attribs;
constructor(points: Vec[], attribs?: Attribs) {
this.points = points;
this.attribs = attribs;
}
abstract get type(): number | string;
abstract copy(): IShape;
*[Symbol.iterator]() {
yield* this.points;
}
}
export class AABB implements
AABBLike {
pos: Vec;
size: Vec;
attribs: Attribs;
constructor(pos: Vec = [0, 0, 0], size: Vec = [1, 1, 1], attribs?: Attribs) {
this.pos = pos;
this.size = size;
this.attribs = attribs;
}
get type() {
return Type.AABB;
}
copy() {
return new AABB(set([], this.pos), set([], this.size), { ...this.attribs });
}
max() {
return add3([], this.pos, this.size);
}
}
export class Arc implements
IHiccupShape,
IHiccupPathSegment {
pos: Vec;
r: Vec;
start: number;
end: number;
axis: number;
xl: boolean;
cw: boolean;
attribs: Attribs;
constructor(
pos: Vec,
r: Vec,
axis: number,
start: number,
end: number,
xl = false,
cw = false,
attribs?: Attribs) {
this.pos = pos;
this.r = r;
this.axis = axis;
this.start = start;
this.end = end;
this.xl = xl;
this.cw = cw;
this.attribs = attribs;
}
get type() {
return Type.ARC;
}
copy() {
return new Arc(
set([], this.pos),
set([], this.r),
this.axis,
this.start,
this.end,
this.xl,
this.cw,
{ ...this.attribs }
);
}
equiv(o: any) {
return o instanceof Arc &&
equiv(this.pos, o.pos) &&
equiv(this.r, o.r) &&
this.start === o.start &&
this.end === o.end &&
this.axis === o.axis &&
this.xl === o.xl &&
this.cw && o.cw;
}
pointAt(t: number, out: Vec = []) {
return arcPointAt(this.pos, this.r, this.axis, this.start, this.end, t, out);
}
pointAtTheta(theta: number, out: Vec = []) {
return arcPointAtTheta(this.pos, this.r, this.axis, theta, out);
}
toHiccup() {
return ["path", this.attribs, [
["M", this.pointAt(0)],
...this.toHiccupPathSegments()
]];
}
toHiccupPathSegments() {
return [
[
"A",
this.r[0],
this.r[1],
this.axis,
this.xl,
this.cw,
this.pointAt(1)
]
];
}
}
export class Circle implements
IHiccupShape {
pos: Vec;
r: number;
attribs: Attribs;
constructor(pos: Vec = [0, 0], r = 1, attribs?: Attribs) {
this.pos = pos;
this.r = r;
this.attribs = attribs;
}
get type() {
return Type.CIRCLE;
}
copy() {
return new Circle(set([], this.pos), this.r, { ...this.attribs });
}
toHiccup() {
return ["circle", this.attribs, this.pos, this.r];
}
}
export class Cubic extends APC implements
IHiccupPathSegment {
get type() {
return Type.CUBIC;
}
copy() {
return new Cubic(copyPoints(this.points), { ...this.attribs });
}
toHiccup() {
return ["path", this.attribs,
[
["M", this.points[0]],
...this.toHiccupPathSegments()
]
];
}
toHiccupPathSegments() {
const pts = this.points;
return [["C", pts[1], pts[2], pts[3]]];
}
}
export class Ellipse implements
IHiccupShape {
pos: Vec;
r: Vec;
attribs: Attribs;
constructor(pos: Vec = [0, 0], r: number | Vec = [1, 1], attribs?: Attribs) {
this.pos = pos;
this.r = isNumber(r) ? [r, r] : r;
this.attribs = attribs;
}
get type() {
return Type.ELLIPSE;
}
copy() {
return new Ellipse(set([], this.pos), set([], this.r), { ...this.attribs });
}
toHiccup() {
return ["ellipse", this.attribs, this.pos, this.r];
}
}
export class Group implements
IHiccupShape {
children: IHiccupShape[];
attribs: Attribs;
constructor(children: IHiccupShape[], attribs?: Attribs) {
this.children = children;
this.attribs = attribs;
}
get type() {
return Type.GROUP;
}
*[Symbol.iterator]() {
yield* this.children;
}
copy() {
return new Group(
<IHiccupShape[]>this.children.map((c) => c.copy()),
{ ...this.attribs }
);
}
equiv(o: any) {
return o instanceof Group &&
equiv(this.children, o.children);
}
toHiccup() {
return ["g", this.attribs, ...this.children.map((x) => x.toHiccup())];
}
}
export class Line extends APC implements
IHiccupShape,
IHiccupPathSegment {
get type() {
return Type.LINE;
}
copy() {
return new Line(copyPoints(this.points), { ...this.attribs });
}
toHiccup() {
return ["line", this.attribs, this.points[0], this.points[1]];
}
toHiccupPathSegments() {
const [a, b] = this.points;
return [
a[0] === b[0] ?
["V", b[1]] :
a[1] === b[1] ?
["H", b[0]] :
["L", b]
];
}
}
export class Path implements
IHiccupShape {
segments: PathSegment[];
closed: boolean;
attribs: Attribs;
constructor(segments?: PathSegment[], attribs?: Attribs) {
this.segments = segments || [];
this.attribs = attribs;
this.closed = false;
}
get type() {
return Type.PATH;
}
*[Symbol.iterator]() {
yield* this.segments;
}
copy() {
const p = new Path([...this.segments], { ...this.attribs });
p.closed = this.closed;
return p;
}
equiv(o: any) {
return o instanceof Path &&
equiv(this.segments, o.segments);
}
add(s: PathSegment) {
if (this.closed) illegalState("path already closed");
this.segments.push(s);
}
toHiccup() {
let dest: any[] = [];
const segments = this.segments;
const n = segments.length;
if (n > 1) {
dest.push(["M", segments[0].point]);
for (let i = 1; i < n; i++) {
dest = dest.concat(segments[i].geo.toHiccupPathSegments());
}
if (this.closed) {
dest.push(["Z"]);
}
}
return ["path", this.attribs || {}, dest];
}
}
export class Points extends APC implements
IHiccupShape {
get type() {
return Type.POINTS;
}
copy() {
return new Points(copyPoints(this.points), { ...this.attribs });
}
toHiccup() {
return ["points", this.attribs, this.points];
}
}
export class Polygon extends APC implements
IHiccupShape {
get type() {
return Type.POLYGON;
}
copy() {
return new Polygon(copyPoints(this.points), { ...this.attribs });
}
toHiccup() {
return ["polygon", this.attribs, this.points];
}
}
export class Polyline extends APC implements
IHiccupShape,
IHiccupPathSegment {
get type() {
return Type.POLYLINE;
}
copy() {
return new Polyline(copyPoints(this.points), { ...this.attribs });
}
toHiccup() {
return ["polyline", { ...this.attribs, fill: "none" }, this.points];
}
toHiccupPathSegments() {
const res: any[] = [];
for (let pts = this.points, n = pts.length, i = 1; i < n; i++) {
res.push(["L", pts[i]]);
}
return res;
}
}
export class Quad extends APC implements
IHiccupShape {
get type() {
return Type.QUAD;
}
copy() {
return new Quad(copyPoints(this.points), { ...this.attribs });
}
toHiccup() {
return ["polygon", this.attribs, this.points];
}
}
export class Quadratic extends APC implements
IHiccupShape,
IHiccupPathSegment {
get type() {
return Type.QUADRATIC;
}
copy() {
return new Quadratic(copyPoints(this.points), { ...this.attribs });
}
toHiccup() {
return ["path", this.attribs,
[
["M", this.points[0]],
...this.toHiccupPathSegments()
]
];
}
toHiccupPathSegments() {
const pts = this.points;
return [["Q", pts[1], pts[2]]];
}
}
export class Ray implements
IHiccupShape {
pos: Vec;
dir: Vec;
attribs: Attribs;
constructor(pos: Vec, dir: Vec, attribs?: Attribs) {
this.pos = pos;
this.dir = dir;
this.attribs = attribs;
}
get type() {
return Type.RAY;
}
copy() {
return new Ray(set([], this.pos), set([], this.dir), { ...this.attribs });
}
toHiccup() {
return ["line", this.attribs, this.pos, maddN2([], this.pos, this.dir, 1e6)];
}
}
export class Rect implements
AABBLike,
IHiccupShape {
pos: Vec;
size: Vec;
attribs: Attribs;
constructor(pos: Vec = [0, 0], size: number | Vec = [1, 1], attribs?: Attribs) {
this.pos = pos;
this.size = isNumber(size) ? [size, size] : size;
this.attribs = attribs;
}
get type() {
return Type.RECT;
}
copy() {
return new Rect(set([], this.pos), set([], this.size), { ...this.attribs });
}
max() {
return add2([], this.pos, this.size);
}
toHiccup() {
return ["rect", this.attribs, this.pos, this.size[0], this.size[1]];
}
}
export class Sphere implements
IHiccupShape {
pos: Vec;
r: number;
attribs: Attribs;
constructor(pos: Vec = [0, 0, 0], r = 1, attribs?: Attribs) {
this.pos = pos;
this.r = r;
this.attribs = attribs;
}
get type() {
return Type.SPHERE;
}
copy() {
return new Sphere(set([], this.pos), this.r, { ...this.attribs });
}
toHiccup() {
return ["sphere", this.attribs, this.pos, this.r];
}
}
export class Triangle extends APC implements
IHiccupShape {
get type() {
return Type.TRIANGLE;
}
copy() {
return new Triangle(copyPoints(this.points), { ...this.attribs });
}
toHiccup() {
return ["polygon", this.attribs, this.points];
}
}