/
parameters.h
173 lines (141 loc) · 5.21 KB
/
parameters.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
#ifndef MULTILINEARRECONSTRUCTION_PARAMETERS_H
#define MULTILINEARRECONSTRUCTION_PARAMETERS_H
#include "glm/glm.hpp"
#include "mathutils.hpp"
struct CameraParameters {
CameraParameters() {}
CameraParameters(double fovy, double far, int image_width, int image_height)
: fovy(fovy), far(far) {
focal_length = 1.0 / tan(0.5 * fovy);
image_plane_center = glm::vec2(image_width * 0.5, image_height * 0.5);
image_size = glm::vec2(image_width, image_height);
}
static CameraParameters DefaultParameters(int image_width,
int image_height) {
const double fovy = deg2rad(15.0);
const double far = 1000.0;
return CameraParameters(fovy, far, image_width, image_height);
}
friend istream& operator>>(istream& is, CameraParameters& params);
friend ostream& operator<<(ostream& os, const CameraParameters& params);
double fovy;
double far;
double focal_length;
glm::dvec2 image_plane_center;
glm::dvec2 image_size;
};
inline istream& operator>>(istream& is, CameraParameters& params) {
is >> params.fovy >> params.far >> params.focal_length
>> params.image_plane_center.x >> params.image_plane_center.y
>> params.image_size.x >> params.image_size.y;
return is;
}
inline ostream& operator<<(ostream& os, const CameraParameters& params) {
os << params.fovy << "\n";
os << params.far << "\n";
os << params.focal_length << "\n";
os << params.image_plane_center.x << ' ' << params.image_plane_center.y << "\n";
os << params.image_size.x << ' ' << params.image_size.y;
return os;
}
struct ModelParameters {
static ModelParameters DefaultParameters(const MatrixXd& Uid,
const MatrixXd& Uexp) {
ModelParameters model_params;
// Make a neutral face
model_params.Wexp_FACS.resize(ModelParameters::nFACSDim);
model_params.Wexp_FACS(0) = 1.0;
for (int i = 1; i < ModelParameters::nFACSDim; ++i)
model_params.Wexp_FACS(i) = 0.0;
model_params.Wexp = model_params.Wexp_FACS.transpose() * Uexp;
// Use average identity
model_params.Wid = Uid.colwise().mean();
model_params.R = Vector3d(1e-3, 1e-3, 1e-3);
model_params.T = Vector3d(0, 0, -1.0);
model_params.vindices = VectorXi::Zero(ModelParameters::nVertices);
return model_params;
}
friend istream& operator>>(istream& is, ModelParameters& params);
friend ostream& operator<<(ostream& os, const ModelParameters& params);
static const int nFACSDim = 47;
static const int nVertices = 73;
VectorXd Wid; // identity weights
VectorXd Wexp, Wexp_FACS; // expression weights
Vector3d R; // rotation
Vector3d T; // translation
VectorXi vindices; // vertex indices
};
namespace {
template <typename T>
void write_vector(ostream& os, const Matrix<T, Dynamic, 1>& v) {
os << v.rows() << ' ';
for(int i=0;i<v.rows();++i) {
os << v(i) << ' ';
}
os << "\n";
}
template <typename T>
void read_vector(istream& is, Matrix<T, Dynamic, 1>& v) {
int nrows;
is >> nrows;
v.resize(nrows, 1);
for(int i=0;i<nrows;++i) is >> v(i);
}
}
inline istream& operator>>(istream& is, ModelParameters& params) {
read_vector(is, params.Wid);
read_vector(is, params.Wexp);
read_vector(is, params.Wexp_FACS);
is >> params.R(0) >> params.R(1) >> params.R(2);
is >> params.T(0) >> params.T(1) >> params.T(2);
read_vector(is, params.vindices);
return is;
}
inline ostream& operator<<(ostream& os, const ModelParameters& params) {
write_vector(os, params.Wid);
write_vector(os, params.Wexp);
write_vector(os, params.Wexp_FACS);
os << params.R(0) << ' ' << params.R(1) << ' ' << params.R(2) << "\n";
os << params.T(0) << ' ' << params.T(1) << ' ' << params.T(2) << "\n";
write_vector(os, params.vindices);
return os;
}
struct ReconstructionStats {
double max_error, min_error, avg_error;
friend istream& operator>>(istream& is, ReconstructionStats& stats);
friend ostream& operator<<(ostream& os, const ReconstructionStats& stats);
};
inline istream& operator>>(istream& is, ReconstructionStats& stats) {
is >> stats.avg_error >> stats.min_error >> stats.max_error;
return is;
}
inline ostream& operator<<(ostream& os, const ReconstructionStats& stats) {
os << stats.avg_error << ' ' << stats.min_error << ' ' << stats.max_error;
return os;
}
struct ReconstructionResult {
CameraParameters params_cam;
ModelParameters params_model;
ReconstructionStats stats;
};
template <typename Constraint>
struct ReconstructionParameters {
int imageWidth, imageHeight;
vector<Constraint> cons;
};
struct OptimizationParameters {
OptimizationParameters() : errorThreshold(1e-6), errorDiffThreshold(1e-6),
w_prior_id(100.0), w_prior_exp(100.0),
d_w_prior_id(10.0), d_w_prior_exp(10.0),
max_iters(3), num_initializations(1) {}
static OptimizationParameters Defaults() {
return OptimizationParameters();
}
double errorThreshold;
double errorDiffThreshold;
double w_prior_id, w_prior_exp, d_w_prior_id, d_w_prior_exp;
int max_iters;
int num_initializations;
double perturbation_range;
};
#endif //MULTILINEARRECONSTRUCTION_PARAMETERS_H