Skip to content

Commit 6bde380

Browse files
committed
Update generated function definitions
1 parent 01e333c commit 6bde380

File tree

3 files changed

+141
-45
lines changed

3 files changed

+141
-45
lines changed

examples/graph-builder.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Check types and run:
3+
* yarn tsc --module node16 --target es2022 --strict --verbatimModuleSyntax --noEmit examples/graph-builder.ts
4+
* yarn tsx examples/graph-builder.ts -t <token> -p <project>
5+
*/
6+
import fs from "fs"
7+
import type { OptionValues } from "commander"
8+
import { program } from "commander"
9+
import MetafoldClient from "../src/metafold.js"
10+
import {
11+
CSGIntersect,
12+
CSGUnion,
13+
CylinderPrimitive,
14+
EllipsoidPrimitive,
15+
GenerateSamplePoints,
16+
POINT_SOURCE,
17+
Redistance,
18+
SampleSurfaceLattice,
19+
Threshold,
20+
} from "../src/metafold.js"
21+
22+
const f = Threshold(
23+
Redistance(
24+
CSGUnion(
25+
CSGIntersect(
26+
SampleSurfaceLattice(POINT_SOURCE, {
27+
lattice_type: "Gyroid",
28+
scale: [0.1, 0.1, 0.1],
29+
}),
30+
EllipsoidPrimitive(POINT_SOURCE, { size: [2.0, 2.0, 2.0] }),
31+
),
32+
CylinderPrimitive(POINT_SOURCE, {
33+
size: [2.0, 2.0, 0.15],
34+
xform: [
35+
1.0, 0.0, 0.0, 0.0,
36+
0.0, 1.0, 0.0, 0.0,
37+
0.0, 0.0, 1.0, 0.0,
38+
0.0, 0.0, -0.95, 1.0,
39+
],
40+
}),
41+
0.1, // Smoothing
42+
),
43+
),
44+
{
45+
width: 0.04075,
46+
},
47+
)
48+
49+
async function exec(opts: OptionValues) {
50+
const token = opts.token ?? process.env.METAFOLD_ACCESS_TOKEN
51+
if (!token) {
52+
console.error("access token is required")
53+
process.exit(1)
54+
}
55+
56+
const metafold = new MetafoldClient(token, opts.project)
57+
58+
for (const res of [64, 128, 265]) {
59+
const source = GenerateSamplePoints({
60+
size: [2.0, 2.0, 2.0],
61+
offset: [-1.0, -1.0, -1.0],
62+
resolution: [res, res, res],
63+
})
64+
const graph = f.json(source)
65+
const index = graph.operators.findIndex(({ type }) => type === "GenerateSamplePoints")
66+
67+
console.log("Running export_triangle_mesh job...")
68+
const exportMesh = await metafold.jobs.run("export_triangle_mesh", {
69+
graph,
70+
point_source: index,
71+
file_type: "obj",
72+
})
73+
74+
console.log("Downloading generated mesh asset...")
75+
const fd = fs.openSync(`out-${res}.obj`, "a")
76+
try {
77+
const r = await metafold.assets.download(exportMesh.assets[0].id)
78+
for await (const chunk of r.data) {
79+
fs.appendFileSync(fd, chunk)
80+
}
81+
} finally {
82+
fs.closeSync(fd)
83+
}
84+
}
85+
}
86+
87+
async function main() {
88+
program
89+
.option("-t, --token <token>", "access token")
90+
.requiredOption("-p, --project <id>", "project id")
91+
.action(exec)
92+
93+
await program.parseAsync(process.argv)
94+
}
95+
96+
main()

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "metafold",
3-
"version": "0.3.0",
3+
"version": "0.3.1",
44
"description": "Metafold SDK for Node.js",
55
"main": "lib/metafold.js",
66
"type": "module",

src/func.ts

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
} from "./func-types.js"
3131

3232
/** Enum variants for CSG.operation. */
33-
export type CSG_Enum_operation = "Subtract" | "Union" | "Intersect"
33+
export type CSG_Enum_operation = "Union" | "Intersect" | "Subtract"
3434

3535
/** Optional parameters for the CSG operator. */
3636
export interface CSG_Parameters {
@@ -100,7 +100,7 @@ export function GenerateSamplePoints(
100100
}
101101

102102
/** Enum variants for GradeCellSize.shape_type. */
103-
export type GradeCellSize_Enum_shape_type = "Box" | "Ellipsoid" | "Plane" | "Cylinder"
103+
export type GradeCellSize_Enum_shape_type = "Cylinder" | "Box" | "Ellipsoid" | "Plane"
104104

105105
/** Optional parameters for the GradeCellSize operator. */
106106
export interface GradeCellSize_Parameters {
@@ -193,7 +193,7 @@ export function LoadSamplePoints(
193193
}
194194

195195
/** Enum variants for LoadVolume.component_type. */
196-
export type LoadVolume_Enum_component_type = "Integer" | "Vec2f" | "Vec3i" | "Vec3f" | "Vec4f" | "None" | "Byte" | "Float" | "Vec2i" | "Vec4i"
196+
export type LoadVolume_Enum_component_type = "None" | "Byte" | "Float" | "Vec3f" | "Vec4i" | "Vec4f" | "Integer" | "Vec2i" | "Vec2f" | "Vec3i"
197197

198198
/** Optional parameters for the LoadVolume operator. */
199199
export interface LoadVolume_Parameters {
@@ -264,7 +264,7 @@ export function Redistance(
264264
}
265265

266266
/** Enum variants for SampleBeam.node_type. */
267-
export type SampleBeam_Enum_node_type = "None" | "Sphere"
267+
export type SampleBeam_Enum_node_type = "Sphere" | "None"
268268

269269
/** Enum variants for SampleBeam.section_type. */
270270
export type SampleBeam_Enum_section_type = "Circle" | "Cross" | "Box"
@@ -297,7 +297,7 @@ export function SampleBeam(
297297
}
298298

299299
/** Enum variants for SampleBox.shape_type. */
300-
export type SampleBox_Enum_shape_type = "Box" | "Cylinder" | "Torus" | "Link" | "CappedCone" | "Ellipsoid" | "Capsule" | "BoxFrame" | "Plane"
300+
export type SampleBox_Enum_shape_type = "Cylinder" | "BoxFrame" | "Plane" | "Torus" | "Link" | "Box" | "Capsule" | "CappedCone" | "Ellipsoid"
301301

302302
/** Optional parameters for the SampleBox operator. */
303303
export interface SampleBox_Parameters {
@@ -344,10 +344,10 @@ export function SampleCustomShape(
344344
}
345345

346346
/** Enum variants for SampleLattice.node_type. */
347-
export type SampleLattice_Enum_node_type = "Sphere" | "None"
347+
export type SampleLattice_Enum_node_type = "None" | "Sphere"
348348

349349
/** Enum variants for SampleLattice.section_type. */
350-
export type SampleLattice_Enum_section_type = "Box" | "Circle" | "Cross"
350+
export type SampleLattice_Enum_section_type = "Circle" | "Cross" | "Box"
351351

352352
/** Optional parameters for the SampleLattice operator. */
353353
export interface SampleLattice_Parameters {
@@ -381,7 +381,7 @@ export function SampleLattice(
381381
}
382382

383383
/** Enum variants for SampleSurfaceLattice.lattice_type. */
384-
export type SampleSurfaceLattice_Enum_lattice_type = "SchwarzW" | "None" | "SchwarzD" | "I2Y" | "P" | "W" | "PM_Y" | "SchwarzPW" | "S" | "D" | "IWP" | "CP" | "CY" | "C_Y" | "SchwarzN" | "Gyroid" | "CI2Y" | "CD" | "CS" | "Y" | "FRD" | "SD1" | "F" | "Schwarz" | "CPM_Y"
384+
export type SampleSurfaceLattice_Enum_lattice_type = "CS" | "W" | "Y" | "SchwarzN" | "None" | "D" | "IWP" | "CP" | "SchwarzD" | "CI2Y" | "SchwarzW" | "SchwarzPW" | "C_Y" | "CPM_Y" | "Gyroid" | "I2Y" | "F" | "CY" | "CD" | "PM_Y" | "FRD" | "S" | "SD1" | "P" | "Schwarz"
385385

386386
/** Optional parameters for the SampleSurfaceLattice operator. */
387387
export interface SampleSurfaceLattice_Parameters {
@@ -485,14 +485,14 @@ export interface Threshold_Parameters {
485485
export function Threshold(
486486
samples: TypedFunc<FuncType.Float>,
487487
parameters?: Threshold_Parameters,
488-
): TypedFunc<FuncType.Float> {
489-
return new TypedFunc<FuncType.Float>(
488+
): TypedFunc<FuncType.Byte> {
489+
return new TypedFunc<FuncType.Byte>(
490490
"Threshold",
491491
{
492492
"Samples": samples,
493493
},
494494
parameters,
495-
FuncType.Float,
495+
FuncType.Byte,
496496
)
497497
}
498498

@@ -562,7 +562,7 @@ export function TransformSphericalCoords(
562562
}
563563

564564
/** Enum variants for TransformTwistCoords.axis. */
565-
export type TransformTwistCoords_Enum_axis = "Z" | "X" | "Y"
565+
export type TransformTwistCoords_Enum_axis = "Y" | "Z" | "X"
566566

567567
/** Optional parameters for the TransformTwistCoords operator. */
568568
export interface TransformTwistCoords_Parameters {
@@ -592,20 +592,28 @@ export function TransformTwistCoords(
592592
* ----------------------------------------------------------------------------
593593
*/
594594

595-
/** Helper to generate the SDF for a Box primitive. */
596-
export function BoxPrimitive(
595+
/** Helper to generate the SDF for a Cylinder primitive. */
596+
export function CylinderPrimitive(
597597
points: TypedFunc<FuncType.Vec3f> = POINT_SOURCE,
598598
parameters: Omit<SampleBox_Parameters, "shape_type">,
599599
): TypedFunc<FuncType.Float> {
600-
return SampleBox(points, { ...parameters, shape_type: "Box" })
600+
return SampleBox(points, { ...parameters, shape_type: "Cylinder" })
601601
}
602602

603-
/** Helper to generate the SDF for a Cylinder primitive. */
604-
export function CylinderPrimitive(
603+
/** Helper to generate the SDF for a BoxFrame primitive. */
604+
export function BoxFramePrimitive(
605605
points: TypedFunc<FuncType.Vec3f> = POINT_SOURCE,
606606
parameters: Omit<SampleBox_Parameters, "shape_type">,
607607
): TypedFunc<FuncType.Float> {
608-
return SampleBox(points, { ...parameters, shape_type: "Cylinder" })
608+
return SampleBox(points, { ...parameters, shape_type: "BoxFrame" })
609+
}
610+
611+
/** Helper to generate the SDF for a Plane primitive. */
612+
export function PlanePrimitive(
613+
points: TypedFunc<FuncType.Vec3f> = POINT_SOURCE,
614+
parameters: Omit<SampleBox_Parameters, "shape_type">,
615+
): TypedFunc<FuncType.Float> {
616+
return SampleBox(points, { ...parameters, shape_type: "Plane" })
609617
}
610618

611619
/** Helper to generate the SDF for a Torus primitive. */
@@ -624,20 +632,12 @@ export function LinkPrimitive(
624632
return SampleBox(points, { ...parameters, shape_type: "Link" })
625633
}
626634

627-
/** Helper to generate the SDF for a CappedCone primitive. */
628-
export function CappedConePrimitive(
629-
points: TypedFunc<FuncType.Vec3f> = POINT_SOURCE,
630-
parameters: Omit<SampleBox_Parameters, "shape_type">,
631-
): TypedFunc<FuncType.Float> {
632-
return SampleBox(points, { ...parameters, shape_type: "CappedCone" })
633-
}
634-
635-
/** Helper to generate the SDF for a Ellipsoid primitive. */
636-
export function EllipsoidPrimitive(
635+
/** Helper to generate the SDF for a Box primitive. */
636+
export function BoxPrimitive(
637637
points: TypedFunc<FuncType.Vec3f> = POINT_SOURCE,
638638
parameters: Omit<SampleBox_Parameters, "shape_type">,
639639
): TypedFunc<FuncType.Float> {
640-
return SampleBox(points, { ...parameters, shape_type: "Ellipsoid" })
640+
return SampleBox(points, { ...parameters, shape_type: "Box" })
641641
}
642642

643643
/** Helper to generate the SDF for a Capsule primitive. */
@@ -648,51 +648,51 @@ export function CapsulePrimitive(
648648
return SampleBox(points, { ...parameters, shape_type: "Capsule" })
649649
}
650650

651-
/** Helper to generate the SDF for a BoxFrame primitive. */
652-
export function BoxFramePrimitive(
651+
/** Helper to generate the SDF for a CappedCone primitive. */
652+
export function CappedConePrimitive(
653653
points: TypedFunc<FuncType.Vec3f> = POINT_SOURCE,
654654
parameters: Omit<SampleBox_Parameters, "shape_type">,
655655
): TypedFunc<FuncType.Float> {
656-
return SampleBox(points, { ...parameters, shape_type: "BoxFrame" })
656+
return SampleBox(points, { ...parameters, shape_type: "CappedCone" })
657657
}
658658

659-
/** Helper to generate the SDF for a Plane primitive. */
660-
export function PlanePrimitive(
659+
/** Helper to generate the SDF for a Ellipsoid primitive. */
660+
export function EllipsoidPrimitive(
661661
points: TypedFunc<FuncType.Vec3f> = POINT_SOURCE,
662662
parameters: Omit<SampleBox_Parameters, "shape_type">,
663663
): TypedFunc<FuncType.Float> {
664-
return SampleBox(points, { ...parameters, shape_type: "Plane" })
664+
return SampleBox(points, { ...parameters, shape_type: "Ellipsoid" })
665665
}
666666

667-
/** Helper to subtract two SDFs. */
668-
export function CSGSubtract(
667+
/** Helper to union two SDFs. */
668+
export function CSGUnion(
669669
a: TypedFunc<FuncType.Float>,
670670
b: TypedFunc<FuncType.Float>,
671671
smoothing?: number,
672672
): TypedFunc<FuncType.Float> {
673-
const params: CSG_Parameters = { operation: "Subtract" }
673+
const params: CSG_Parameters = { operation: "Union" }
674674
if (smoothing) { params.smoothing = smoothing }
675675
return CSG(a, b, params)
676676
}
677677

678-
/** Helper to union two SDFs. */
679-
export function CSGUnion(
678+
/** Helper to intersect two SDFs. */
679+
export function CSGIntersect(
680680
a: TypedFunc<FuncType.Float>,
681681
b: TypedFunc<FuncType.Float>,
682682
smoothing?: number,
683683
): TypedFunc<FuncType.Float> {
684-
const params: CSG_Parameters = { operation: "Union" }
684+
const params: CSG_Parameters = { operation: "Intersect" }
685685
if (smoothing) { params.smoothing = smoothing }
686686
return CSG(a, b, params)
687687
}
688688

689-
/** Helper to intersect two SDFs. */
690-
export function CSGIntersect(
689+
/** Helper to subtract two SDFs. */
690+
export function CSGSubtract(
691691
a: TypedFunc<FuncType.Float>,
692692
b: TypedFunc<FuncType.Float>,
693693
smoothing?: number,
694694
): TypedFunc<FuncType.Float> {
695-
const params: CSG_Parameters = { operation: "Intersect" }
695+
const params: CSG_Parameters = { operation: "Subtract" }
696696
if (smoothing) { params.smoothing = smoothing }
697697
return CSG(a, b, params)
698698
}

0 commit comments

Comments
 (0)