This repository has been archived by the owner on Aug 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathcenter_scale_obj.js
77 lines (64 loc) · 2.11 KB
/
center_scale_obj.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
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
"use strict";
// centers and scales all *.obj and saves results as *.2.obj
// can also keep 3d viewers from jittering
const fs = require('fs');
const readline = require('readline');
const path = require('path');
const OBJ_DIR = './downloaded_files/obj';
for (let i of fs.readdirSync(OBJ_DIR)) {
i = path.resolve(OBJ_DIR, i);
if (!fs.statSync(i).isDirectory()) continue;
for (let j of fs.readdirSync(i)) {
j = path.resolve(i, j);
if (!/\.obj$/.test(j) || /\.2\.obj$/.test(j)) continue;
if (!fs.statSync(j).isFile()) continue;
scaleMoveObj(j, `${j.match(/(.*)\.obj$/)[1]}.2.obj`);
}
}
const SCALE = 10;
function scaleMoveObj(file_in, file_out) {
if (fs.existsSync(file_out)) {
fs.unlinkSync(file_out);
}
const io = readline.createInterface({
input: fs.createReadStream(file_in),
terminal: false,
});
let min_x = Infinity, max_x = -Infinity;
let min_y = Infinity, max_y = -Infinity;
let min_z = Infinity, max_z = -Infinity;
io.on('line', line => {
if (!/^v /.test(line))
return;
let [x, y, z] = line.split(' ').slice(1).map(parseFloat);
min_x = Math.min(x, min_x);
min_y = Math.min(y, min_y);
min_z = Math.min(z, min_z);
max_x = Math.max(x, max_x);
max_y = Math.max(y, max_y);
max_z = Math.max(z, max_z);
}).on('close', () => {
const center_x = (max_x + min_x) / 2;
const center_y = (max_y + min_y) / 2;
const center_z = (max_z + min_z) / 2;
const distance_x = Math.abs(max_x - min_x);
const distance_y = Math.abs(max_y - min_y);
const distance_z = Math.abs(max_z - min_z);
const max_distance = Math.max(distance_x, distance_y, distance_z);
const io = readline.createInterface({
input: fs.createReadStream(file_in),
output: fs.createWriteStream(file_out),
});
io.on('line', line => {
if (!/^v /.test(line))
return io.output.write(`${line}\n`);
let [x, y, z] = line.split(' ').slice(1).map(parseFloat);
x = (x - center_x) / max_distance * SCALE;
y = (y - center_y) / max_distance * SCALE;
z = (z - center_z) / max_distance * SCALE;
io.output.write(`v ${x} ${y} ${z}\n`);
}).on('close', () => {
console.error(`done. saved as ${file_out}`);
});
});
}