-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTreeCurve.js
More file actions
145 lines (128 loc) · 3.78 KB
/
TreeCurve.js
File metadata and controls
145 lines (128 loc) · 3.78 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
143
144
145
var RANDOM_SEGMENT_COMPONENT_MAX = 20.0;
var RANDOM_SEGMENT_COMPONENT_MIN = 3.0;
TreeCurve = function()
{
//this.root = new TreeCurveNode(x, y, z);
}
TreeCurve.random = function(segments)
{
var curve = new TreeCurve();
curve.root = new TreeCurveNode(0.0, 0.0, 0.0);
curve.root.addRandomTree(segments);
return curve;
}
TreeCurve.loadTree = function(url, curveCallback)
{
$.getJSON(url)
.success(function(data) {
var nodes = [];
// nodes by id
for(var nodeIndex in data) {
var node = data[nodeIndex];
node.parents = [];
nodes[node.id] = node;
}
// parents
for(var nodeIndex in data) {
var node = data[nodeIndex];
for(var childIdIndex in node.children)
{
var childId = node.children[childIdIndex];
nodes[childId].parents.push(node.id);
}
}
// find root
var noParents = [];
for(var nodeIndex in nodes) {
var node = nodes[nodeIndex];
if(node.parents.length == 0)
noParents.push(node);
}
console.log('Found ', noParents.length, ' root node(s).', noParents);
if(noParents.length != 1)
console.error('TreeMesh Error: There should be exactly', 1, 'root node in ', url, 'but there are', noParents.length);
else
{
var curve = new TreeCurve();
var fileNodeToTreeCurveTree = function(node)
{
//console.log('JSON node', node);
var newNode = new TreeCurveNode(node.pos[0], node.pos[1], node.pos[2]);
for(var c = 0; c < node.children.length; c++)
{
var child = nodes[node.children[c]];
newNode.children.push(fileNodeToTreeCurveTree(child));
}
return newNode;
}
curve.root = fileNodeToTreeCurveTree(noParents[0]);
console.log('loaded curve:', curve);
curveCallback(curve);
}
})
.error(function(data) {
console.log('.error', data);
});
}
TreeCurve.prototype.totalLength = function()
{
return this.root.totalLength();
}
TreeCurve.prototype.maxDepth = function()
{
return this.root.maxDepth();
}
TreeCurveNode = function(x, y, z)
{
this.x = x;
this.y = y;
this.z = z;
this.children = [];
}
TreeCurveNode.prototype.addChild = function(x, y, z)
{
var newNode = new TreeCurveNode( x, y, z );
this.children.push( newNode );
return this.children[this.children.length-1];
}
TreeCurveNode.prototype.addRandomTree = function(segments, depth)
{
depth = depth || 0;
var branches = Math.ceil(Math.min(segments, (Math.random() * 4)));
if(branches == 0)
return;
segments -= branches;
var segmentBudgetPerChild = Math.floor(segments / branches);
for(var b = 0; b < branches; b++)
{
var dx = 2 * (Math.random() - 0.5) * (RANDOM_SEGMENT_COMPONENT_MAX - RANDOM_SEGMENT_COMPONENT_MIN);
var dy = 2 * (Math.random() - 0.5) * (RANDOM_SEGMENT_COMPONENT_MAX - RANDOM_SEGMENT_COMPONENT_MIN);
var dz = 2 * (Math.random() - 0.5) * (RANDOM_SEGMENT_COMPONENT_MAX - RANDOM_SEGMENT_COMPONENT_MIN);
this.addChild(this.x + dx, this.y + dy, this.z + dz).addRandomTree(segmentBudgetPerChild, depth+1);
}
}
TreeCurveNode.prototype.to = function(other)
{
return new THREE.Vector3( other.x - this.x, other.y - this.y, other.z - this.z );
}
TreeCurveNode.prototype.distanceTo = function(other)
{
var dx = this.x - other.x;
var dy = this.y - other.y;
var dz = this.z - other.z;
return Math.sqrt( dx * dx + dy * dy + dz * dz );
}
TreeCurveNode.prototype.totalLength = function()
{
var _this = this;
return this.children
.map(function(child) { return _this.distanceTo(child) + child.totalLength() })
.reduce(function(a, b) { return a+b }, 0.0);
}
TreeCurveNode.prototype.maxDepth = function()
{
var _this = this;
return this.children
.map(function(child) { return _this.distanceTo(child) + child.maxDepth() })
.reduce(function(a, b) { return Math.max(a, b) }, 0.0);
}