-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwavefront_scale.js
executable file
·52 lines (45 loc) · 1.27 KB
/
wavefront_scale.js
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
#!/usr/bin/env node
const [, scriptPath, ...args] = process.argv
if (args.length !== 3 || args.includes("--help")) {
console.info(`Usage:
.${scriptPath} <scaling factor> <input file name (wavefront obj)> <output file name (wavefront obj)>`)
return
}
const fs = require("fs")
const [scalingFactorString, inputFileName, outputFileName] = args
const scalingFactor = Number.parseFloat(scalingFactorString)
if (!Number.isFinite(scalingFactor)) {
console.error(
`Scaling factor must be a number, but instead was ${scalingFactorString}`
)
}
fs.readFile(inputFileName, "utf8", (error, data) => {
if (error) {
console.error(error.message)
} else {
const lines = data
.replace(/\r/g, "")
.split("\n")
.filter((line) => line)
fs.writeFile(
outputFileName,
lines
.map((line) => {
const [descriminator, x, y, z, w] = line.split(" ")
return [
descriminator,
Number.parseFloat(x) * scalingFactor,
Number.parseFloat(y) * scalingFactor,
Number.parseFloat(z) * scalingFactor,
Number.parseFloat(w),
].join(" ")
})
.join("\n"),
(error) => {
if (error) {
console.error(error.message)
}
}
)
}
})