-
Notifications
You must be signed in to change notification settings - Fork 105
/
lmm.h
257 lines (211 loc) · 7.48 KB
/
lmm.h
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
246
247
248
249
250
251
252
253
254
255
256
257
#pragma once
#include "common.h"
#include "vec.h"
#include "quat.h"
#include "array.h"
#include "nnet.h"
// This function uses the decompressor network
// to generate the pose of the character. It
// requires as input the feature values and latent
// values as well as a current root position and
// rotation.
void decompressor_evaluate(
slice1d<vec3> bone_positions,
slice1d<vec3> bone_velocities,
slice1d<quat> bone_rotations,
slice1d<vec3> bone_angular_velocities,
slice1d<bool> bone_contacts,
nnet_evaluation& evaluation,
const slice1d<float> features,
const slice1d<float> latent,
const vec3 root_position,
const quat root_rotation,
const nnet& nn,
const float dt = 1.0f / 60.0f)
{
slice1d<float> input_layer = evaluation.layers.front();
slice1d<float> output_layer = evaluation.layers.back();
// First copy feature values and latent variables to
// the input layer of the network
for (int i = 0; i < features.size; i++)
{
input_layer(i) = features(i);
}
for (int i = 0; i < latent.size; i++)
{
input_layer(features.size + i) = latent(i);
}
// Evaluate network
nnet_evaluate(evaluation, nn);
// Extract bone positions
int offset = 0;
for (int i = 0; i < bone_positions.size - 1; i++)
{
bone_positions(i + 1) = vec3(
output_layer(offset+i*3+0),
output_layer(offset+i*3+1),
output_layer(offset+i*3+2));
}
offset += (bone_positions.size - 1) * 3;
// Extract bone rotations, convert from 2-axis representation
for (int i = 0; i < bone_rotations.size - 1; i++)
{
bone_rotations(i + 1) = quat_from_xform_xy(
vec3(output_layer(offset+i*6+0),
output_layer(offset+i*6+2),
output_layer(offset+i*6+4)),
vec3(output_layer(offset+i*6+1),
output_layer(offset+i*6+3),
output_layer(offset+i*6+5)));
}
offset += (bone_rotations.size - 1) * 6;
// Extract bone velocities
for (int i = 0; i < bone_velocities.size - 1; i++)
{
bone_velocities(i + 1) = vec3(
output_layer(offset+i*3+0),
output_layer(offset+i*3+1),
output_layer(offset+i*3+2));
}
offset += (bone_velocities.size - 1) * 3;
// Extract bone angular velocities
for (int i = 0; i < bone_angular_velocities.size - 1; i++)
{
bone_angular_velocities(i + 1) = vec3(
output_layer(offset+i*3+0),
output_layer(offset+i*3+1),
output_layer(offset+i*3+2));
}
offset += (bone_angular_velocities.size - 1) * 3;
// Extract root velocities and put in world space
vec3 root_velocity = quat_mul_vec3(root_rotation, vec3(
output_layer(offset+0),
output_layer(offset+1),
output_layer(offset+2)));
vec3 root_angular_velocity = quat_mul_vec3(root_rotation, vec3(
output_layer(offset+3),
output_layer(offset+4),
output_layer(offset+5)));
offset += 6;
// Find new root position/rotation/velocities etc.
bone_positions(0) = dt * root_velocity + root_position;
bone_rotations(0) = quat_mul(quat_from_scaled_angle_axis(root_angular_velocity * dt), root_rotation);
bone_velocities(0) = root_velocity;
bone_angular_velocities(0) = root_angular_velocity;
// Extract bone contacts
if (bone_contacts.data != nullptr)
{
bone_contacts(0) = output_layer(offset+0) > 0.5f;
bone_contacts(1) = output_layer(offset+1) > 0.5f;
}
offset += 2;
// Check we got everything!
assert(offset == nn.output_mean.size);
}
// This function updates the feature and latent values
// using the stepper network and a given dt.
void stepper_evaluate(
slice1d<float> features,
slice1d<float> latent,
nnet_evaluation& evaluation,
const nnet& nn,
const float dt = 1.0f / 60.0f)
{
slice1d<float> input_layer = evaluation.layers.front();
slice1d<float> output_layer = evaluation.layers.back();
// Copy features and latents to input
for (int i = 0; i < features.size; i++)
{
input_layer(i) = features(i);
}
for (int i = 0; i < latent.size; i++)
{
input_layer(features.size + i) = latent(i);
}
// Evaluate network
nnet_evaluate(evaluation, nn);
// Update features and latents using result
for (int i = 0; i < features.size; i++)
{
features(i) += dt * output_layer(i);
}
for (int i = 0; i < latent.size; i++)
{
latent(i) += dt * output_layer(features.size + i);
}
}
// This function projects a set of feature values onto
// the nearest in the trained database, also outputting the
// associated latent values. It also produces the matching
// cost using the distance of the projection, and detects
// transitions for a given transition cost by measuring the
// distance between the projected result and the current
// feature values
void projector_evaluate(
bool& transition,
float& best_cost,
slice1d<float> proj_features,
slice1d<float> proj_latent,
nnet_evaluation& evaluation,
const slice1d<float> query,
const slice1d<float> features_offset,
const slice1d<float> features_scale,
const slice1d<float> curr_features,
const nnet& nn,
const float transition_cost = 0.0f)
{
slice1d<float> input_layer = evaluation.layers.front();
slice1d<float> output_layer = evaluation.layers.back();
// Copy query features to input
for (int i = 0; i < query.size; i++)
{
input_layer(i) = (query(i) - features_offset(i)) / features_scale(i);
}
// Evaluate network
nnet_evaluate(evaluation, nn);
// Copy projected features and latents from output
for (int i = 0; i < proj_features.size; i++)
{
proj_features(i) = output_layer(i);
}
for (int i = 0; i < proj_latent.size; i++)
{
proj_latent(i) = output_layer(proj_features.size + i);
}
// Compute the distance of the projection
best_cost = 0.0f;
for (int i = 0; i < proj_features.size; i++)
{
best_cost += squaref(query(i) - proj_features(i));
}
best_cost = sqrtf(best_cost);
// Compute the change in features from the current
float trns_dist_squared = 0.0f;
for (int i = 0; i < proj_features.size; i++)
{
trns_dist_squared += squaref(curr_features(i) - proj_features(i));
}
// If greater than the transition cost...
if (trns_dist_squared > squaref(transition_cost))
{
// transition and add the transition cost
transition = true;
best_cost += transition_cost;
}
else
{
// Don't transition and use current features as-is
transition = false;
for (int i = 0; i < proj_features.size; i++)
{
proj_features(i) = curr_features(i);
}
// Re-compute the projection cost
best_cost = 0.0f;
for (int i = 0; i < curr_features.size; i++)
{
best_cost += squaref(query(i) - curr_features(i));
}
best_cost = sqrtf(best_cost);
}
}