blob: 0c6449f38678be93899c20ebe0850820a9ba0a3b (
plain)
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
<scxml datamodel="ecmascript">
<script src="http://uscxml.tk.informatik.tu-darmstadt.de/scripts/dump.js" />
<script>
//<![CDATA[
function clone(item) {
if (!item) { return item; } // null, undefined values check
var types = [ Number, String, Boolean ],
result;
// normalizing primitives if someone did new String('aaa'), or new Number('444');
types.forEach(function(type) {
if (item instanceof type) {
result = type( item );
}
});
if (typeof result == "undefined") {
if (Object.prototype.toString.call( item ) === "[object Array]") {
result = [];
item.forEach(function(child, index, array) {
result[index] = clone( child );
});
} else if (typeof item == "object") {
// testing that this is DOM
if (item.nodeType && typeof item.cloneNode == "function") {
var result = item.cloneNode( true );
} else if (!item.prototype) { // check that this is a literal
if (item instanceof Date) {
result = new Date(item);
} else {
// it is an object literal
result = {};
for (var i in item) {
result[i] = clone( item[i] );
}
}
} else {
// depending what you would like here,
// just keep the reference, or create new object
if (false && item.constructor) {
// would not advice to do that, reason? Read below
result = new item.constructor();
} else {
result = item;
}
}
} else {
result = item;
}
}
return result;
}
//]]>
</script>
<datamodel>
<data id="modelDir" />
<data id="requests">{}</data>
</datamodel>
<state id="start">
<onentry>
<log expr="modelDir" />
</onentry>
<invoke type="osgconvert" id="osgvonvert.frame">
<param name="threads" expr="1" />
<finalize>
<script>
requests[_event.data.context].job.renderedFrames++;
</script>
<!-- osgconverter will copy its send request for the reply so these are still available -->
<send target="#_ffmpeg" event="render.frame">
<param name="frame" expr="_event.data.content.bmp" />
<param name="context" expr="_event.data.context" />
</send>
<if cond="requests[_event.data.context].job.renderedFrames >= requests[_event.data.context].job.totalFrames">
<send target="#_ffmpeg" event="render.end">
<param name="context" expr="_event.data.context" />
</send>
</if>
</finalize>
</invoke>
<invoke type="ffmpeg" id="ffmpeg">
<finalize>
<script>
// dump(_event);
</script>
<send target="#_parent" event="render.done">
<param name="context" expr="_event.data.context" />
<param name="movie" expr="_event.data.movie" />
<param name="mimetype" expr="_event.data.mimetype" />
<param name="filename" expr="_event.data.filename" />
</send>
<script>
// free up job in requests
requests[_event.data.context] = undefined;
</script>
</finalize>
</invoke>
<state id ="idle">
<!-- <onentry>
<script>
print("Invokers:");
dump(_invokers['ffmpeg']);
</script>
</onentry> -->
<transition event="http.post" target="idle" cond="
_event.data.pathComponent.length == 2 &&
_event.data.pathComponent[1] === 'movie'">
<!--
Something to encode just arrived.
Interpolate pose information and send each frame to the osg converter.
The finalize of the osgconverter will send the images to ffmpeg whose
finalize will trigger the response.
-->
<script>
//<![CDATA[
dump(_event);
var pathDelim = ':'; // we need to flatten directories - this will seperate them in filenames
// store event
var job = {};
job.event = _event;
job.framesPerSec = 25;
job.lengthInSec = _event.data.content.data.movieLength;
job.format = _event.data.content.data.format;
job.height = _event.data.content.data.height;
job.width = _event.data.content.data.width;
job.keyFrames = _event.data.content.data.frames;
job.frames = [];
job.totalFrames = job.lengthInSec * job.framesPerSec
job.renderedFrames = 0;
// sanitize
if (job.height % 2)
job.height++;
if (job.width % 2)
job.width++;
// calculate overall relative length
var totalRelLength = 0;
for (var index in job.keyFrames) {
totalRelLength += job.keyFrames[index].relFrameLength;
}
var framesToRelLength = job.totalFrames / totalRelLength;
// create frames
for (var kfIndex = 0; job.keyFrames[kfIndex]; kfIndex++) {
var nextPose = false;
if (job.keyFrames[kfIndex + 1]) {
nextPose = job.keyFrames[kfIndex + 1].pose;
}
var keyFrame = job.keyFrames[kfIndex];
keyFrame.file = keyFrame.imagePath.substr(1);
keyFrame.modelFile = keyFrame.file + ".osgb";
keyFrame.modelFile = keyFrame.modelFile.replace(/\//g, pathDelim);
keyFrame.modelFile = modelDir + '/' + keyFrame.modelFile;
var currentPose = keyFrame.pose;
var thisFrames = Math.round(keyFrame.relFrameLength * framesToRelLength);
var startTransitionAt = Math.round((100 - keyFrame.relTransitionLength) * 0.01 * thisFrames);
var transitionFrames = thisFrames - startTransitionAt;
//print("---------------------\n");
//print("lengthInSec: " + job.lengthInSec + "\n");
//print("totalFrames: " + job.totalFrames + "\n");
//print("thisFrames for " + kfIndex + ": " + thisFrames + "\n");
//print("startTransitionAt for " + kfIndex + ": " + startTransitionAt + "\n");
//print("transitionFrames for " + kfIndex + ": " + transitionFrames + "\n");
for (var frameIndex = 0; frameIndex < thisFrames && job.frames.length <= job.totalFrames; frameIndex++) {
var frame = clone(keyFrame);
if (frameIndex > startTransitionAt && nextPose) {
var transitionProgress = (frameIndex - startTransitionAt) / transitionFrames;
frame.pose.pitch += (nextPose.pitch - frame.pose.pitch) * transitionProgress;
frame.pose.roll += (nextPose.roll - frame.pose.roll) * transitionProgress;
frame.pose.yaw += (nextPose.yaw - frame.pose.yaw) * transitionProgress;
frame.pose.x += (nextPose.x - frame.pose.x) * transitionProgress;
frame.pose.y += (nextPose.y - frame.pose.y) * transitionProgress;
frame.pose.z += (nextPose.z - frame.pose.z) * transitionProgress;
frame.pose.zoom += (nextPose.zoom - frame.pose.zoom) * transitionProgress;
//print("#########################\n");
//print("Transition at: " + frameIndex + " \ Progress: " + transitionProgress + "\n");
//print("Interpolated pose:\n")
//dump(frame.pose);
}
job.frames.push(frame);
}
}
job.totalFrames = job.frames.length;
requests[_event.origin] = {};
requests[_event.origin].job = job;
//]]>
</script>
<send target="#_ffmpeg" event="render.start" idlocation="requests[_event.origin].sendId">
<param name="context" expr="_event.origin" />
<param name="format" expr="requests[_event.origin].job.format" />
<param name="width" expr="requests[_event.origin].job.width" />
<param name="height" expr="requests[_event.origin].job.height" />
</send>
<foreach array="requests[_event.origin].job.frames" item="frame" index="index">
<send target="#_osgvonvert.frame">
<param name="format" expr="'bmp'" />
<!-- param name="dest" expr="'/Users/sradomski/Desktop/ctrl/' + index + '.bmp'" / -->
<param name="source" expr="frame.modelFile" />
<param name="pitch" expr="frame.pose.pitch" />
<param name="roll" expr="frame.pose.roll" />
<param name="yaw" expr="frame.pose.yaw" />
<param name="zoom" expr="frame.pose.zoom" />
<param name="x" expr="frame.pose.x" />
<param name="y" expr="frame.pose.y" />
<param name="z" expr="frame.pose.z" />
<param name="width" expr="requests[_event.origin].job.width" />
<param name="height" expr="requests[_event.origin].job.height" />
<param name="autorotate" expr="frame.pose.autorotate" />
<param name="context" expr="_event.origin" />
</send>
</foreach>
</transition>
<transition event="http.get" target="idle" cond="
_event.data.pathComponent.length == 3 &&
_event.data.pathComponent[1] === 'movie' &&
_event.data.pathComponent[2] === 'codecs'">
<log expr="_invokers['ffmpeg']" />
<send target="#_parent" event="send.codecs">
<param name="context" expr="_event.origin" />
<param name="codecs" expr="_invokers['ffmpeg']" />
</send>
</transition>
</state>
</state>
</scxml>
|