Skip to content

Commit

Permalink
add ExtrudeFromSlices
Browse files Browse the repository at this point in the history
  • Loading branch information
seveibar committed Oct 4, 2024
1 parent 6b44b6d commit de48bdf
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 2 deletions.
56 changes: 56 additions & 0 deletions examples/extrude-from-slices.fixture.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { ExtrudeFromSlices } from "../lib"
import { JsCadFixture } from "../lib/components/jscad-fixture"
import jscad from "@jscad/modeling"

const { bezier } = jscad.curves
const { circle, line, polygon, rectangle, roundedRectangle, star } =
jscad.primitives
const { extrudeLinear, extrudeRotate, extrudeFromSlices, slice } =
jscad.extrusions
const { mat4 } = jscad.maths

const baseSlice = slice.fromPoints([
[2, 2],
[-2, 2],
[-2, -2],
[2, -2],
])

const xCurve = bezier.create([1, 1.8, 0.4, 1])
const yCurve = bezier.create([1, 1.8, 0.5])

export default () => (
<JsCadFixture zAxisUp showGrid>
<ExtrudeFromSlices
numberOfSlices={10}
capStart={true}
capEnd={true}
close={true}
repair={true}
baseSlice={baseSlice}
callback={function (progress, count, base) {
let newslice = slice.transform(
mat4.fromTranslation(mat4.create(), [0, 0, 10 * progress]),
baseSlice,
)
newslice = slice.transform(
mat4.fromScaling(mat4.create(), [
bezier.valueAt(progress, xCurve) as any,
bezier.valueAt(progress, yCurve) as any,
1,
]),
newslice,
)

// Rotate the slice 90 degrees along the path
const rotationAngle = (Math.PI / 2) * progress // 90 degrees in radians
newslice = slice.transform(
mat4.fromXRotation(mat4.create(), rotationAngle),
newslice,
)

return newslice
}}
/>
</JsCadFixture>
)
14 changes: 14 additions & 0 deletions lib/create-host-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
CylinderEllipticProps,
CylinderProps,
EllipsoidProps,
ExtrudeFromSlicesProps,
ExtrudeHelicalProps,
ExtrudeLinearProps,
ExtrudeRectangularProps,
Expand All @@ -21,6 +22,7 @@ import type {
RectangleProps,
RoundedCuboidProps,
RoundedCylinderProps,
Slice,
SphereProps,
TorusProps,
UnionProps,
Expand Down Expand Up @@ -191,6 +193,18 @@ export function createHostConfig(jscad: JSCADModule) {

return extrudedGeometry
}

case "extrudeFromSlices": {
const { baseSlice, ...extrudeProps } = props as ExtrudeFromSlicesProps

const extrudedGeometry = jscad.extrusions.extrudeFromSlices(
extrudeProps,
baseSlice as Slice,
)

return extrudedGeometry
}

case "project": {
const { children, ...projectProps } = props as ProjectProps

Expand Down
10 changes: 9 additions & 1 deletion lib/intrinsic-jsx.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,19 @@ declare global {
cylinderElliptic: FN.CylinderEllipticProps & CommonProps
jscadPolygon: FN.PolygonProps & CommonProps
colorize: FN.ColorizeProps & CommonProps
extrudeFromSlices: {
numberOfSlices?: number
capStart?: boolean
capEnd?: boolean
close?: boolean
repair?: boolean
baseSlice?: FN.Slice
callback?: (progress: number, count: number, base: Slice) => Slice
} & CommonProps
sphere: FN.SphereProps & CommonProps
cuboid: FN.CuboidProps & CommonProps
ellipsoid: FN.EllipsoidProps & CommonProps
cylinder: FN.CylinderProps & CommonProps
cube: FN.CubeProps & CommonProps
torus: FN.TorusProps & CommonProps
custom: FN.CustomProps & CommonProps
union: FN.UnionProps & CommonProps
Expand Down
43 changes: 43 additions & 0 deletions lib/jscad-fns/extrude-from-slices.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { withColorProp } from "lib/wrappers/with-color-prop"
import { withOffsetProp } from "lib/wrappers/with-offset-prop"
import type { Point3 } from "./translate"

export type Slice = {
edges: Array<[Point3, Point3]>
}

export type ExtrudeFromSlicesProps = {
numberOfSlices?: number
capStart?: boolean
capEnd?: boolean
close?: boolean
repair?: boolean
baseSlice: Slice
callback?: (progress: number, count: number, base: Slice) => Slice
}

const ExtrudeFromSlicesBase = ({
numberOfSlices,
capStart,
capEnd,
close,
repair,
baseSlice,
callback,
}: ExtrudeFromSlicesProps) => {
return (
<extrudeFromSlices
numberOfSlices={numberOfSlices}
capStart={capStart}
capEnd={capEnd}
close={close}
repair={repair}
baseSlice={baseSlice}
callback={callback}
></extrudeFromSlices>
)
}

export const ExtrudeFromSlices = withOffsetProp(
withColorProp(ExtrudeFromSlicesBase),
)
1 change: 1 addition & 0 deletions lib/jscad-fns/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ export * from "./sphere"
export * from "./torus"
export * from "./translate"
export * from "./union"
export * from "./extrude-from-slices"
13 changes: 12 additions & 1 deletion lib/jscad-primitives.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Point3 } from "./jscad-fns"
import type { Point3, Slice } from "./jscad-fns"

// Define a type for the JSCAD module structure we expect
export interface JSCADModule {
Expand Down Expand Up @@ -113,6 +113,17 @@ export interface JSCADModule {
},
geometry: any,
) => any
extrudeFromSlices: (
options: {
numberOfSlices?: number
capStart?: boolean
capEnd?: boolean
close?: boolean
repair?: boolean
callback?: (progress: number, count: number, base: Slice) => Slice
},
baseSlice: Slice,
) => any
}
colors: {
colorize: (options: [number, number, number], geometry: any) => any
Expand Down

0 comments on commit de48bdf

Please sign in to comment.