-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpvjsonbbox.js
More file actions
144 lines (125 loc) · 5.29 KB
/
Copy pathpvjsonbbox.js
File metadata and controls
144 lines (125 loc) · 5.29 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
var fs = require('fs');
var matrix = require('./matrix.js');
if (process.argv.length < 3) {
console.log("usage : pvjsonbbox filename - list all the bounding boxes in file");
console.log(" pvjsonbbox filename path - list the bounding boxes for the specified instance");
console.log(" pvjsonbbox filename path anchor - list the specified anchor");
console.log(" - anchor is a 3-character patter that defines the position of the anchor point in each dimension xyz :-");
console.log(" - t = top");
console.log(" - c = center");
console.log(" - b = bottom");
console.log(" - so for example ctc = center(x), top(y), center(z)");
console.log(" - path can be '*' to show all specified anchor for all items");
process.exit(1);
}
var fi = process.argv[2];
var pvs = JSON.parse(fs.readFileSync(fi,'utf8'));
var pvj = new Object();
function traverse(node, position, path, isolate, anchor) {
var cbox = undefined;
if (node.children != undefined) {
cbox = new matrix.BBox();
node.children.forEach(function(kid) {
var child = pvs.components[kid.cidref];
var m = new matrix.Matrix4();
if (kid.location != undefined) {
if (kid.location.orientation != undefined)
m.RotateA(kid.location.orientation.matrix);
if (kid.location.position != undefined)
m.TranslateV(kid.location.position);
}
if (position != undefined)
m.Multiply(position.m);
var ech = path[path.length-1];
var idpath;
if (ech != '/')
idpath = path + "/" + kid.vid;
else
idpath = path + kid.vid;
cbox.Envelope(traverse(child, m, idpath, isolate, anchor));
//console.log(path+' '+cbox.ToString());
});
}
if (node.shape != undefined) {
if (node.bbox != undefined) {
var box = new matrix.BBox().Set(node.bbox.min, node.bbox.max);
var inst = new Object();
var pname = node.properties["name"];
inst.name = pname != undefined ? pname : "undefined";
if (anchor != undefined)
inst.anchors = new Object(); //Array();
if (isolate != undefined && isolate != path) {
// compute it (for parents) but don't log it
} else {
pvj[path] = inst;
}
if (position != undefined) {
var tbox = box.Transform(position);
if (anchor != undefined) {
// all the anchors (including centers)
tbox.EnumerateAll(function(c,v) {
//or just the corners
//tbox.EnumerateCorners(function(c,v) {
inst.anchors[c] = v.v;
}, anchor);
} else {
inst.min = tbox.min;
inst.max = tbox.max;
}
//console.log(path+' 1 '+tbox.ToString());
return tbox;
} else {
if (anchor != undefined) {
box.EnumerateAll(function(c,v) {
//box.EnumerateCorners(function(c,v) {
inst.anchors[c] = v.v;
}, anchor);
} else {
inst.min = box.min;
inst.max = box.max;
}
//console.log(path+' 2 '+tbox.ToString());
return box;
}
} else return undefined;
} else if (cbox != undefined) {
var inst = new Object();
var pname = node.properties["name"];
inst.name = pname != undefined ? pname : "undefined";
if (anchor != undefined)
inst.anchors = new Object(); //Array();
if (isolate != undefined && isolate != path) {
// compute it (for parents) but don't log it
} else {
pvj[path] = inst;
}
// note all the children are already transformed to our space
// so no we just use this box
if (anchor != undefined) {
cbox.EnumerateAll(function(c,v) {
//cbox.EnumerateCorners(function(c,v) {
inst.anchors[c] = v.v;
}, anchor);
} else {
inst.min = cbox.min;
inst.max = cbox.max;
}
//console.log(path+' 3 '+cbox.ToString());
return cbox;
} else
return undefined;
}
var iroot = pvs.components.length - 1;
var root = pvs.components[iroot];
var path = process.argv[3];
var anchor = process.argv[4];
if (path === '*') path = undefined;
traverse(root, undefined, "/", path, anchor);
var fo = process.argv[5];
if (fo === undefined)
console.log(JSON.stringify(pvj,null,'\t'));
else
fs.writeFile(fo, JSON.stringify(pvj), function(err){
if (err) throw err;
console.log('Done!');
})