Skip to content

Commit

Permalink
feat: Add empty shape primitive (#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
joeng03 authored Mar 14, 2024
1 parent 5d762b0 commit 122b3b8
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 97 deletions.
96 changes: 22 additions & 74 deletions src/bundles/csg/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
subtract as _subtract,
union as _union,
} from '@jscad/modeling/src/operations/booleans';
import { geom3 } from '@jscad/modeling/src/geometries';

Check warning on line 13 in src/bundles/csg/functions.ts

View workflow job for this annotation

GitHub Actions / deploy

'geom3' is defined but never used
import { extrudeLinear } from '@jscad/modeling/src/operations/extrusions';
import { serialize } from '@jscad/stl-serializer';
import {
Expand All @@ -32,8 +33,6 @@ import {
} from './utilities';
import { degreesToRadians } from '../../common/utilities.js';



/* [Main] */
/* NOTE
These functions involving calls (not merely types) to js-slang make this file
Expand Down Expand Up @@ -61,8 +60,6 @@ export function arrayToList(array: Operable[]): List {
return list(...array);
}



/* [Exports] */

// [Variables - Colors]
Expand Down Expand Up @@ -181,6 +178,15 @@ 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.
*
Expand All @@ -193,12 +199,7 @@ export const white: string = '#FFFFFF';
*/
export function cube(hex: string): Shape {
let solid: Solid = primitives.cube({ size: 1 });
let shape: Shape = new Shape(
colorSolid(
hexToColor(hex),
solid,
),
);
let shape: Shape = new Shape(colorSolid(hexToColor(hex), solid));
return centerPrimitive(shape);
}

Expand All @@ -214,12 +215,7 @@ export function cube(hex: string): Shape {
*/
export function rounded_cube(hex: string): Shape {
let solid: Solid = primitives.roundedCuboid({ size: [1, 1, 1] });
let shape: Shape = new Shape(
colorSolid(
hexToColor(hex),
solid,
),
);
let shape: Shape = new Shape(colorSolid(hexToColor(hex), solid));
return centerPrimitive(shape);
}

Expand All @@ -239,12 +235,7 @@ export function cylinder(hex: string): Shape {
height: 1,
radius: 0.5,
});
let shape: Shape = new Shape(
colorSolid(
hexToColor(hex),
solid,
),
);
let shape: Shape = new Shape(colorSolid(hexToColor(hex), solid));
return centerPrimitive(shape);
}

Expand All @@ -264,12 +255,7 @@ export function rounded_cylinder(hex: string): Shape {
height: 1,
radius: 0.5,
});
let shape: Shape = new Shape(
colorSolid(
hexToColor(hex),
solid,
),
);
let shape: Shape = new Shape(colorSolid(hexToColor(hex), solid));
return centerPrimitive(shape);
}

Expand All @@ -285,12 +271,7 @@ export function rounded_cylinder(hex: string): Shape {
*/
export function sphere(hex: string): Shape {
let solid: Solid = primitives.sphere({ radius: 0.5 });
let shape: Shape = new Shape(
colorSolid(
hexToColor(hex),
solid,
),
);
let shape: Shape = new Shape(colorSolid(hexToColor(hex), solid));
return centerPrimitive(shape);
}

Expand All @@ -306,12 +287,7 @@ export function sphere(hex: string): Shape {
*/
export function geodesic_sphere(hex: string): Shape {
let solid: Solid = primitives.geodesicSphere({ radius: 0.5 });
let shape: Shape = new Shape(
colorSolid(
hexToColor(hex),
solid,
),
);
let shape: Shape = new Shape(colorSolid(hexToColor(hex), solid));
return centerPrimitive(shape);
}

Expand All @@ -337,12 +313,7 @@ export function pyramid(hex: string): Shape {
endRadius: [0, 0],
segments: 4,
});
let shape: Shape = new Shape(
colorSolid(
hexToColor(hex),
solid,
),
);
let shape: Shape = new Shape(colorSolid(hexToColor(hex), solid));
shape = rotate(shape, 0, 0, degreesToRadians(45)) as Shape;
return centerPrimitive(shape);
}
Expand All @@ -364,12 +335,7 @@ export function cone(hex: string): Shape {
startRadius: [0.5, 0.5],
endRadius: [0, 0],
});
let shape: Shape = new Shape(
colorSolid(
hexToColor(hex),
solid,
),
);
let shape: Shape = new Shape(colorSolid(hexToColor(hex), solid));
return centerPrimitive(shape);
}

Expand All @@ -385,16 +351,8 @@ export function cone(hex: string): Shape {
* @category Primitives
*/
export function prism(hex: string): Shape {
let solid: Solid = extrudeLinear(
{ height: 1 },
primitives.triangle(),
);
let shape: Shape = new Shape(
colorSolid(
hexToColor(hex),
solid,
),
);
let 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);
}
Expand All @@ -414,12 +372,7 @@ export function star(hex: string): Shape {
{ height: 1 },
primitives.star({ outerRadius: 0.5 }),
);
let shape: Shape = new Shape(
colorSolid(
hexToColor(hex),
solid,
),
);
let shape: Shape = new Shape(colorSolid(hexToColor(hex), solid));
return centerPrimitive(shape);
}

Expand All @@ -439,12 +392,7 @@ export function torus(hex: string): Shape {
innerRadius: 0.15,
outerRadius: 0.35,
});
let shape: Shape = new Shape(
colorSolid(
hexToColor(hex),
solid,
),
);
let shape: Shape = new Shape(colorSolid(hexToColor(hex), solid));
return centerPrimitive(shape);
}

Expand Down
3 changes: 1 addition & 2 deletions src/bundles/csg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ context.moduleContexts.csg.state = moduleState;
// We initialise Core for the first time over on the bundles' end here
Core.initialize(moduleState);



/* [Exports] */
export {
// Colors
Expand All @@ -90,6 +88,7 @@ export {
white,

// Primitives
empty_shape,
cube,
rounded_cube,
cylinder,
Expand Down
20 changes: 5 additions & 15 deletions src/bundles/csg/utilities.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* [Imports] */
import {
import geom3, {
transform as _transform,
} from '@jscad/modeling/src/geometries/geom3';
import mat4, { type Mat4 } from '@jscad/modeling/src/maths/mat4';
Expand All @@ -13,8 +13,6 @@ import type { ReplResult } from '../../typings/type_helpers.js';
import { Core } from './core.js';
import type { AlphaColor, Color, Solid } from './jscad/types.js';



/* [Exports] */
export interface Operable {
applyTransforms: (newTransforms: Mat4) => Operable;
Expand All @@ -28,10 +26,7 @@ export interface Operable {
export class Group implements Operable, ReplResult {
children: Operable[];

constructor(
_children: Operable[],
public transforms: Mat4 = mat4.create(),
) {
constructor(_children: Operable[], public transforms: Mat4 = mat4.create()) {
// Duplicate the array to avoid modifying the original, maintaining
// stateless Operables for the user
this.children = [..._children];
Expand All @@ -45,10 +40,7 @@ export class Group implements Operable, ReplResult {
);

// Return a new object for statelessness
return new Group(
this.children,
appliedTransforms,
);
return new Group(this.children, appliedTransforms);
}

store(newTransforms: Mat4 = mat4.create()): void {
Expand Down Expand Up @@ -103,14 +95,12 @@ export class Group implements Operable, ReplResult {
ungroup(): Operable[] {
// Return all children, but we need to account for this Group's unresolved
// transforms by applying them to each child
return this.children.map(
(child: Operable) => child.applyTransforms(this.transforms),
);
return this.children.map((child: Operable) => child.applyTransforms(this.transforms));
}
}

export class Shape implements Operable, ReplResult {
constructor(public solid: Solid) {}
constructor(public solid: Solid = geom3.create()) {}

applyTransforms(newTransforms: Mat4): Operable {
// Return a new object for statelessness
Expand Down
8 changes: 4 additions & 4 deletions src/bundles/plotly/curve_functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function z_of(pt: Point): number {
* ```
*/
export function r_of(pt: Point): number {
return ( pt.color[0] ?? 0 ) * 255;
return (pt.color[0] ?? 0) * 255;
}

/**
Expand All @@ -62,7 +62,7 @@ export function r_of(pt: Point): number {
* ```
*/
export function g_of(pt: Point): number {
return ( pt.color[1] ?? 0 ) * 255;
return (pt.color[1] ?? 0) * 255;
}

/**
Expand All @@ -77,7 +77,7 @@ export function g_of(pt: Point): number {
* ```
*/
export function b_of(pt: Point): number {
return ( pt.color[2] ?? 0 ) * 255;
return (pt.color[2] ?? 0) * 255;
}
export function generatePlot(
type: string,
Expand Down Expand Up @@ -124,4 +124,4 @@ export function generatePlot(

function draw_new_curve(divId: string, data: Data, layout: Partial<Layout>) {
Plotly.react(divId, [data], layout);
}
}
2 changes: 1 addition & 1 deletion src/bundles/plotly/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ export {
draw_points_2d,
draw_points_3d,
draw_sound_2d,
} from './functions';
} from './functions';
2 changes: 1 addition & 1 deletion src/bundles/plotly/plotly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ export class Point implements ReplResult {
}

export type Wave = (...t: any) => number;
export type Sound = Pair<Wave, number>;
export type Sound = Pair<Wave, number>;

0 comments on commit 122b3b8

Please sign in to comment.