-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph-builder.ts
96 lines (87 loc) · 2.41 KB
/
graph-builder.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
/*
* Check types and run:
* yarn tsc --module node16 --target es2022 --strict --verbatimModuleSyntax --noEmit examples/graph-builder.ts
* yarn tsx examples/graph-builder.ts -t <token> -p <project>
*/
import fs from "fs"
import type { OptionValues } from "commander"
import { program } from "commander"
import MetafoldClient from "../src/metafold.js"
import {
CSGIntersect,
CSGUnion,
CylinderPrimitive,
EllipsoidPrimitive,
GenerateSamplePoints,
POINT_SOURCE,
Redistance,
SampleSurfaceLattice,
Threshold,
} from "../src/metafold.js"
const f = Threshold(
Redistance(
CSGUnion(
CSGIntersect(
SampleSurfaceLattice(POINT_SOURCE, {
lattice_type: "Gyroid",
scale: [0.1, 0.1, 0.1],
}),
EllipsoidPrimitive(POINT_SOURCE, { size: [2.0, 2.0, 2.0] }),
),
CylinderPrimitive(POINT_SOURCE, {
size: [2.0, 2.0, 0.15],
xform: [
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, -0.95, 1.0,
],
}),
0.1, // Smoothing
),
),
{
width: 0.04075,
},
)
async function exec(opts: OptionValues) {
const token = opts.token ?? process.env.METAFOLD_ACCESS_TOKEN
if (!token) {
console.error("access token is required")
process.exit(1)
}
const metafold = new MetafoldClient(token, opts.project)
for (const res of [64, 128, 265]) {
const source = GenerateSamplePoints({
size: [2.0, 2.0, 2.0],
offset: [-1.0, -1.0, -1.0],
resolution: [res, res, res],
})
const graph = f.json(source)
const index = graph.operators.findIndex(({ type }) => type === "GenerateSamplePoints")
console.log("Running export_triangle_mesh job...")
const exportMesh = await metafold.jobs.run("export_triangle_mesh", {
graph,
point_source: index,
file_type: "obj",
})
console.log("Downloading generated mesh asset...")
const fd = fs.openSync(`out-${res}.obj`, "a")
try {
const r = await metafold.assets.download(exportMesh.assets[0].id)
for await (const chunk of r.data) {
fs.appendFileSync(fd, chunk)
}
} finally {
fs.closeSync(fd)
}
}
}
async function main() {
program
.option("-t, --token <token>", "access token")
.requiredOption("-p, --project <id>", "project id")
.action(exec)
await program.parseAsync(process.argv)
}
main()