-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathfunctions.ts
772 lines (698 loc) · 18.2 KB
/
functions.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
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
/* [Imports] */
import { primitives } from '@jscad/modeling';
import { colorize as colorSolid } from '@jscad/modeling/src/colors';
import {
measureBoundingBox,
type BoundingBox
} from '@jscad/modeling/src/measurements';
import {
intersect as _intersect,
subtract as _subtract,
union as _union
} from '@jscad/modeling/src/operations/booleans';
import { extrudeLinear } from '@jscad/modeling/src/operations/extrusions';
import { serialize } from '@jscad/stl-serializer';
import {
head,
list,
tail,
type List,
is_list
} from 'js-slang/dist/stdlib/list';
import save from 'save-file';
import { degreesToRadians, hexToColor } from '../../common/utilities';
import { Core } from './core';
import type { Solid } from './jscad/types';
import {
Group,
Shape,
type Operable,
type RenderGroup,
centerPrimitive
} from './utilities';
/* [Main] */
/* NOTE
These functions involving calls (not merely types) to js-slang make this file
only usable in bundles. DO NOT import this file in tabs or the build will
fail. Something about the node modules that building them involves causes
esbuild to attempt but fail to include Node-specific APIs (eg fs, os, https)
in the output that's meant for a browser environment (you can't use those in
the browser since they are Node-only). This is why we keep these functions
here instead of in utilities.ts.
When a user passes in a List, we convert it to arrays here so that the rest of
the underlying code is free to operate with arrays.
*/
export function listToArray(l: List): Operable[] {
const operables: Operable[] = [];
while (l !== null) {
const operable: Operable = head(l);
operables.push(operable);
l = tail(l);
}
return operables;
}
export function arrayToList(array: Operable[]): List {
return list(...array);
}
/* [Exports] */
// [Variables - Colors]
/**
* A hex color code for black (#000000).
*
* @category Colors
*/
export const black: string = '#000000';
/**
* A hex color code for dark blue (#0000AA).
*
* @category Colors
*/
export const navy: string = '#0000AA';
/**
* A hex color code for green (#00AA00).
*
* @category Colors
*/
export const green: string = '#00AA00';
/**
* A hex color code for dark cyan (#00AAAA).
*
* @category Colors
*/
export const teal: string = '#00AAAA';
/**
* A hex color code for dark red (#AA0000).
*
* @category Colors
*/
export const crimson: string = '#AA0000';
/**
* A hex color code for purple (#AA00AA).
*
* @category Colors
*/
export const purple: string = '#AA00AA';
/**
* A hex color code for orange (#FFAA00).
*
* @category Colors
*/
export const orange: string = '#FFAA00';
/**
* A hex color code for light gray (#AAAAAA).
*
* @category Colors
*/
export const silver: string = '#AAAAAA';
/**
* A hex color code for dark gray (#555555).
*
* @category Colors
*/
export const gray: string = '#555555';
/**
* A hex color code for blue (#5555FF).
*
* @category Colors
*/
export const blue: string = '#5555FF';
/**
* A hex color code for light green (#55FF55).
*
* @category Colors
*/
export const lime: string = '#55FF55';
/**
* A hex color code for cyan (#55FFFF).
*
* @category Colors
*/
export const cyan: string = '#55FFFF';
/**
* A hex color code for light red (#FF5555).
*
* @category Colors
*/
export const rose: string = '#FF5555';
/**
* A hex color code for pink (#FF55FF).
*
* @category Colors
*/
export const pink: string = '#FF55FF';
/**
* A hex color code for yellow (#FFFF55).
*
* @category Colors
*/
export const yellow: string = '#FFFF55';
/**
* A hex color code for white (#FFFFFF).
*
* @category Colors
*/
export const white: string = '#FFFFFF';
// [Functions - Primitives]
/**
* An empty Shape.
*
* @category Primitives
*/
export function empty_shape(): Shape {
return new Shape();
}
/**
* Returns a cube Shape in the specified color.
*
* - Side length: 1
* - Center: (0.5, 0.5, 0.5)
*
* @param hex hex color code
*
* @category Primitives
*/
export function cube(hex: string): Shape {
const solid: Solid = primitives.cube({ size: 1 });
const shape: Shape = new Shape(colorSolid(hexToColor(hex), solid));
return centerPrimitive(shape);
}
/**
* Returns a rounded cube Shape in the specified color.
*
* - Side length: 1
* - Center: (0.5, 0.5, 0.5)
*
* @param hex hex color code
*
* @category Primitives
*/
export function rounded_cube(hex: string): Shape {
const solid: Solid = primitives.roundedCuboid({ size: [1, 1, 1] });
const shape: Shape = new Shape(colorSolid(hexToColor(hex), solid));
return centerPrimitive(shape);
}
/**
* Returns an upright cylinder Shape in the specified color.
*
* - Height: 1
* - Radius: 0.5
* - Center: (0.5, 0.5, 0.5)
*
* @param hex hex color code
*
* @category Primitives
*/
export function cylinder(hex: string): Shape {
const solid: Solid = primitives.cylinder({
height: 1,
radius: 0.5
});
const shape: Shape = new Shape(colorSolid(hexToColor(hex), solid));
return centerPrimitive(shape);
}
/**
* Returns a rounded, upright cylinder Shape in the specified color.
*
* - Height: 1
* - Radius: 0.5
* - Center: (0.5, 0.5, 0.5)
*
* @param hex hex color code
*
* @category Primitives
*/
export function rounded_cylinder(hex: string): Shape {
const solid: Solid = primitives.roundedCylinder({
height: 1,
radius: 0.5
});
const shape: Shape = new Shape(colorSolid(hexToColor(hex), solid));
return centerPrimitive(shape);
}
/**
* Returns a sphere Shape in the specified color.
*
* - Radius: 0.5
* - Center: (0.5, 0.5, 0.5)
*
* @param hex hex color code
*
* @category Primitives
*/
export function sphere(hex: string): Shape {
const solid: Solid = primitives.sphere({ radius: 0.5 });
const shape: Shape = new Shape(colorSolid(hexToColor(hex), solid));
return centerPrimitive(shape);
}
/**
* Returns a geodesic sphere Shape in the specified color.
*
* - Radius: 0.5
* - Center: Floating at (0.5, 0.5, 0.5)
*
* @param hex hex color code
*
* @category Primitives
*/
export function geodesic_sphere(hex: string): Shape {
const solid: Solid = primitives.geodesicSphere({ radius: 0.5 });
const shape: Shape = new Shape(colorSolid(hexToColor(hex), solid));
return centerPrimitive(shape);
}
/**
* Returns a square pyramid Shape in the specified color.
*
* - Height: 1
* - Base length: 1
* - Center: (0.5, 0.5, 0.5)
*
* @param hex hex color code
*
* @category Primitives
*/
export function pyramid(hex: string): Shape {
const pythagorasSide: number = Math.sqrt(2); // sqrt(1^2 + 1^2)
const radius = pythagorasSide / 2;
const solid: Solid = primitives.cylinderElliptic({
height: 1,
// Base starting radius
startRadius: [radius, radius],
// Radius by the time the top is reached
endRadius: [0, 0],
segments: 4
});
let shape: Shape = new Shape(colorSolid(hexToColor(hex), solid));
shape = rotate(shape, 0, 0, degreesToRadians(45)) as Shape;
return centerPrimitive(shape);
}
/**
* Returns a cone Shape in the specified color.
*
* - Height: 1
* - Radius: 0.5
* - Center: (0.5, 0.5, 0.5)
*
* @param hex hex color code
*
* @category Primitives
*/
export function cone(hex: string): Shape {
const solid: Solid = primitives.cylinderElliptic({
height: 1,
startRadius: [0.5, 0.5],
endRadius: [0, 0]
});
const shape: Shape = new Shape(colorSolid(hexToColor(hex), solid));
return centerPrimitive(shape);
}
/**
* Returns an upright triangular prism Shape in the specified color.
*
* - Height: 1
* - Side length: 1
* - Center: (0.5, 0.5, 0.5)
*
* @param hex hex color code
*
* @category Primitives
*/
export function prism(hex: string): Shape {
const solid: Solid = extrudeLinear({ height: 1 }, primitives.triangle());
let shape: Shape = new Shape(colorSolid(hexToColor(hex), solid));
shape = rotate(shape, 0, 0, degreesToRadians(-90)) as Shape;
return centerPrimitive(shape);
}
/**
* Returns an upright extruded star Shape in the specified color.
*
* - Height: 1
* - Center: (0.5, 0.5, 0.5)
*
* @param hex hex color code
*
* @category Primitives
*/
export function star(hex: string): Shape {
const solid: Solid = extrudeLinear(
{ height: 1 },
primitives.star({ outerRadius: 0.5 })
);
const shape: Shape = new Shape(colorSolid(hexToColor(hex), solid));
return centerPrimitive(shape);
}
/**
* Returns a torus (donut) Shape in the specified color.
*
* - Inner radius: 0.15 (ring is 0.3 thick)
* - Total radius: 0.5 (from the centre of the hole to "outside")
* - Center: Floating at (0.5, 0.5, 0.5)
*
* @param hex hex color code
*
* @category Primitives
*/
export function torus(hex: string): Shape {
const solid: Solid = primitives.torus({
innerRadius: 0.15,
outerRadius: 0.35
});
const shape: Shape = new Shape(colorSolid(hexToColor(hex), solid));
return centerPrimitive(shape);
}
// [Functions - Operations]
/**
* Returns the union of the two specified Shapes.
*
* @param first first Shape
* @param second second Shape
* @returns unioned Shape
*
* @category Operations
*/
export function union(first: Shape, second: Shape): Shape {
if (!is_shape(first) || !is_shape(second)) {
throw new Error('Failed to union, only Shapes can be operated on');
}
const solid: Solid = _union(first.solid, second.solid);
return new Shape(solid);
}
/**
* Subtracts the second Shape from the first Shape, returning the resultant
* Shape.
*
* @param target target Shape to be subtracted from
* @param subtractedShape Shape to remove from the first Shape
* @returns subtracted Shape
*
* @category Operations
*/
export function subtract(target: Shape, subtractedShape: Shape): Shape {
if (!is_shape(target) || !is_shape(subtractedShape)) {
throw new Error('Failed to subtract, only Shapes can be operated on');
}
const solid: Solid = _subtract(target.solid, subtractedShape.solid);
return new Shape(solid);
}
/**
* Returns the intersection of the two specified Shapes.
*
* @param first first Shape
* @param second second Shape
* @returns intersected Shape
*
* @category Operations
*/
export function intersect(first: Shape, second: Shape): Shape {
if (!is_shape(first) || !is_shape(second)) {
throw new Error('Failed to intersect, only Shapes can be operated on');
}
const solid: Solid = _intersect(first.solid, second.solid);
return new Shape(solid);
}
// [Functions - Transformations]
/**
* Translates (moves) the specified Operable in the x, y, and z directions using
* the specified offsets.
*
* @param operable Shape or Group
* @param xOffset x offset
* @param yOffset y offset
* @param zOffset z offset
* @returns translated Shape
*
* @category Transformations
*/
export function translate(
operable: Operable,
xOffset: number,
yOffset: number,
zOffset: number
): Operable {
return operable.translate([xOffset, yOffset, zOffset]);
}
/**
* Sequentially rotates the specified Operable about the x, y, and z axes using
* the specified angles, in radians (i.e. 2π represents 360°).
*
* The order of rotation is: x, y, then z axis. The order of rotation can affect
* the result, so you may wish to make multiple separate calls to rotate() if
* you require a specific order of rotation.
*
* @param operable Shape or Group
* @param xAngle x angle in radians
* @param yAngle y angle in radians
* @param zAngle z angle in radians
* @returns rotated Shape
*
* @category Transformations
*/
export function rotate(
operable: Operable,
xAngle: number,
yAngle: number,
zAngle: number
): Operable {
return operable.rotate([xAngle, yAngle, zAngle]);
}
/**
* Scales the specified Operable in the x, y, and z directions using the
* specified factors. Scaling is done about the origin (0, 0, 0).
*
* For example, a factor of 0.5 results in a smaller Shape, while a factor of 2
* results in a larger Shape. A factor of 1 results in the original Shape.
* Factors must be greater than 0.
*
* @param operable Shape or Group
* @param xFactor x scaling factor
* @param yFactor y scaling factor
* @param zFactor z scaling factor
* @returns scaled Shape
*
* @category Transformations
*/
export function scale(
operable: Operable,
xFactor: number,
yFactor: number,
zFactor: number
): Operable {
if (xFactor <= 0 || yFactor <= 0 || zFactor <= 0) {
// JSCAD library does not allow factors <= 0
throw new Error('Scaling factor must be greater than 0');
}
return operable.scale([xFactor, yFactor, zFactor]);
}
// [Functions - Utilities]
/**
* Groups the specified list of Operables together. Groups can contain a mix of
* Shapes and other nested Groups.
*
* Groups cannot be operated on, but can be transformed together. I.e. a call
* like `intersect(group_a, group_b)` is not allowed, but a call like
* `scale(group, 5, 5, 5)` is.
*
* @param operables list of Shapes and/or Groups
* @returns new Group
*
* @category Utilities
*/
export function group(operables: List): Group {
if (!is_list(operables)) {
throw new Error('Only lists of Operables can be grouped');
}
return new Group(listToArray(operables));
}
/**
* Ungroups the specified Group, returning the list of Shapes and/or nested
* Groups contained within.
*
* @param g Group to ungroup
* @returns ungrouped list of Shapes and/or Groups
*
* @category Utilities
*/
export function ungroup(g: Group): List {
if (!is_group(g)) {
throw new Error('Only Groups can be ungrouped');
}
return arrayToList(g.ungroup());
}
/**
* Checks if the given parameter is a Shape.
*
* @param parameter parameter to check
* @returns whether parameter is a Shape
*
* @category Utilities
*/
export function is_shape(parameter: unknown): boolean {
return parameter instanceof Shape;
}
/**
* Checks if the given parameter is a Group.
*
* @param parameter parameter to check
* @returns whether parameter is a Group
*
* @category Utilities
*/
export function is_group(parameter: unknown): boolean {
return parameter instanceof Group;
}
/**
* Returns a function of type (string, string) → number, for getting the
* specified Shape's bounding box coordinates.
*
* Its first parameter must be "x", "y", or "z", indicating the coordinate axis.
*
* Its second parameter must be "min" or "max", indicating the minimum or
* maximum bounding box coordinate respectively.
*
* For example, if a sphere of radius 0.5 is centred at (0.5, 0.5, 0.5), its
* minimum bounding coordinates will be (0, 0, 0), and its maximum bounding
* coordinates will be (1, 1, 1).
*
* ```js
* // Sample usage
* const getter_function = bounding_box(sphere(silver));
* display(getter_function("y", "max")); // Displays 1, the maximum y coordinate
* ```
*
* @param shape Shape to measure
* @returns bounding box getter function
*
* @category Utilities
*/
export function bounding_box(
shape: Shape
): (axis: string, minMax: string) => number {
const bounds: BoundingBox = measureBoundingBox(shape.solid);
return (axis: string, minMax: string): number => {
let j: number;
if (axis === 'x') j = 0;
else if (axis === 'y') j = 1;
else if (axis === 'z') j = 2;
else {
throw new Error(
`Bounding box getter function expected "x", "y", or "z" as first parameter, but got ${axis}`
);
}
let i: number;
if (minMax === 'min') i = 0;
else if (minMax === 'max') i = 1;
else {
throw new Error(
`Bounding box getter function expected "min" or "max" as second parameter, but got ${minMax}`
);
}
return bounds[i][j];
};
}
/**
* Returns a hex color code representing the specified RGB values.
*
* @param redValue red value of the color
* @param greenValue green value of the color
* @param blueValue blue value of the color
* @returns hex color code
*
* @category Utilities
*/
export function rgb(
redValue: number,
greenValue: number,
blueValue: number
): string {
if (
redValue < 0
|| redValue > 255
|| greenValue < 0
|| greenValue > 255
|| blueValue < 0
|| blueValue > 255
) {
throw new Error('RGB values must be between 0 and 255 (inclusive)');
}
return `#${redValue.toString(16)}${greenValue.toString(16)}${blueValue.toString(16)}`;
}
/**
* Exports the specified Shape as an STL file, downloaded to your device.
*
* The file can be used for purposes such as 3D printing.
*
* @param shape Shape to export
*
* @category Utilities
*/
export async function download_shape_stl(shape: Shape): Promise<void> {
if (!is_shape(shape)) {
throw new Error('Failed to export, only Shapes can be converted to STL');
}
await save(
new Blob(serialize({ binary: true }, shape.solid)),
'Source Academy CSG Shape.stl'
);
}
// [Functions - Rendering]
/**
* Renders the specified Operable.
*
* @param operable Shape or Group to render
*
* @category Rendering
*/
export function render(operable: Operable): RenderGroup {
if (!(operable instanceof Shape || operable instanceof Group)) {
throw new Error('Only Operables can be rendered');
}
operable.store();
// Trigger a new render group for use with subsequent renders.
// Render group is returned for REPL text only; do not document
return Core.getRenderGroupManager()
.nextRenderGroup();
}
/**
* Renders the specified Operable, along with a grid.
*
* @param operable Shape or Group to render
*
* @category Rendering
*/
export function render_grid(operable: Operable): RenderGroup {
if (!(operable instanceof Shape || operable instanceof Group)) {
throw new Error('Only Operables can be rendered');
}
operable.store();
return Core.getRenderGroupManager()
.nextRenderGroup(true);
}
/**
* Renders the specified Operable, along with z, y, and z axes.
*
* @param operable Shape or Group to render
*
* @category Rendering
*/
export function render_axes(operable: Operable): RenderGroup {
if (!(operable instanceof Shape || operable instanceof Group)) {
throw new Error('Only Operables can be rendered');
}
operable.store();
return Core.getRenderGroupManager()
.nextRenderGroup(undefined, true);
}
/**
* Renders the specified Operable, along with both a grid and axes.
*
* @param operable Shape or Group to render
*
* @category Rendering
*/
export function render_grid_axes(operable: Operable): RenderGroup {
if (!(operable instanceof Shape || operable instanceof Group)) {
throw new Error('Only Operables can be rendered');
}
operable.store();
return Core.getRenderGroupManager()
.nextRenderGroup(true, true);
}