-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBVH.js
More file actions
169 lines (141 loc) · 4.57 KB
/
BVH.js
File metadata and controls
169 lines (141 loc) · 4.57 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//---------------------------------------------------------------------------
// This file contains the scene graph built from a BVH file. You should
// not need to modify it. There are helpful comments that describe the
// data structures.
//---------------------------------------------------------------------------
var BVHListener = require('parser/BVHListener');
//------------------------------------------------------------
// Segment
//------------------------------------------------------------
Segment = function() {
// Name of the segment (e.g. RHipJoint).
this.name = null;
// Spatial offset from the parent segment.
this.offsets = [];
// Strings giving the channel names.
this.channels = [];
// Children segments.
this.children = [];
// Offset into the global channel array.
this.channelOffset = 0;
// To get the ith channel value for this segment and frame j:
// bvh.frames[j][segment.channelOffset + i]
}
Segment.prototype.myToString = function(offset) {
var ret = offset + "offsets ";
for (var i = 0; i < this.offsets.length; ++i) {
var o = this.offsets[i];
ret = ret.concat(o + " ");
}
ret = ret.concat("\n" + offset + "channels ");
for (var i = 0; i < this.channels.length; ++i) {
var o = this.channels[i];
ret = ret.concat(o + " ");
}
ret = ret.concat("\n" + offset + "channel offset = " + this.channelOffset);
for (var i = 0; i < this.children.length; ++i) {
var s = this.children[i];
ret = ret.concat("\n" + offset + s.name + "\n");
ret = ret.concat(s.myToString(offset + " "));
}
return ret;
}
//------------------------------------------------------------
// BVH
//------------------------------------------------------------
BVH = function() {
// Inherit default listener
BVHListener.BVHListener.call(this);
// Root segments. Other joints will be children of the roots.
this.roots = [];
// Number of frames
this.numFrames = 0;
// Length of time a frame should play. In seconds.
this.frameTime = 0;
// Frames.
this.frames = [];
// Used in constructing the scene graph.
this.numChannels = 0;
// Used in constructing the scene graph.
this.curSegment = null;
// Used in constructing the scene graph.
this.stack = new Array();
return this;
};
BVH.prototype = Object.create(BVHListener.BVHListener.prototype);
BVH.prototype.constructor = BVH;
BVH.prototype.toString = function() {
var ret = "";
for (var i = 0; i < this.roots.length; ++i) {
var r = this.roots[i];
ret = ret.concat(r.name + "\n");
ret = ret.concat(r.myToString(" ") + "\n");
}
ret = ret.concat("Num frames = " + this.numFrames + "\n");
ret = ret.concat("Frame time = " + this.frameTime + "\n");
ret = ret.concat("Num channels = " + this.numChannels + "\n");
for (var i = 0; i < this.frames.length; ++i) {
ret = ret.concat("Frame: " + this.frames[i] + "\n");
}
return ret;
}
BVH.prototype.enterSegment = function() {
var parent = this.curSegment;
var child = new Segment();
if (parent) {
this.stack.push(parent);
parent.children.push(child);
}
child.channelOffset = this.numChannels;
this.curSegment = child;
}
BVH.prototype.exitSegment = function() {
this.curSegment = this.stack.pop();
}
//------------------------------------------------------------
// Callbacks
//------------------------------------------------------------
BVH.prototype.enterRootSegment = function(ctx) {
this.enterSegment();
this.roots.push(this.curSegment);
};
BVH.prototype.exitRootSegment = function(ctx) {
this.exitSegment();
};
BVH.prototype.enterSegmentID = function(ctx) {
this.curSegment.name = ctx.getText();
};
BVH.prototype.enterOffset = function(ctx) {
this.curSegment.offsets.push(ctx.getText());
};
BVH.prototype.enterChannel = function(ctx) {
this.curSegment.channels.push(ctx.getText());
++this.numChannels;
};
BVH.prototype.enterJointSegment = function(ctx) {
this.enterSegment();
};
BVH.prototype.exitJointSegment = function(ctx) {
this.exitSegment();
};
BVH.prototype.enterEndSite = function(ctx) {
this.enterSegment();
this.curSegment.name = "End site";
};
BVH.prototype.exitEndSite = function(ctx) {
this.exitSegment();
};
BVH.prototype.enterNumFrames = function(ctx) {
this.numFrames = parseInt(ctx.getText());
};
BVH.prototype.enterFrameTime = function(ctx) {
this.frameTime = parseFloat(ctx.getText());
};
BVH.prototype.enterMotionData = function(ctx) {
var data = ctx.getText().trim().split(/\s+/);
var nc = this.numChannels;
var numFrames = data.length / nc;
for (var i = 0; i < numFrames; ++i) {
this.frames.push(data.slice(i*nc, (i+1)*nc));
}
};