Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ceres_mesh_unwrapper.cpp « src « SLIM « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b0ef67628b5778664b5442f4da46fa0df1ac6ff7 (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//
//  ceres_mesh_unwrapper.cpp
//  Blender
//
//  Created by Aurel Gruber on 24.02.17.
//
//

#include "ceres_mesh_unwrapper.h"
#include "ceres/ceres.h"
#include <iostream>
#include <math.h>       /* cos */


using namespace std;

template <typename T>
T cross_2D(const Eigen::Matrix<T, 1, 2> &a, const Eigen::Matrix<T, 1, 2> &b){
	return -T(a(0) * b(1) - a(1) * b(0));
}

/**
 *find SVD according to http://scicomp.stackexchange.com/questions/8899/robust-algorithm-for-2x2-svd
 *	-> Alex Eftimiades' answer
 */
template <typename T>
void computeSingularValues(T j11, T j12, T j21, T j22, T& s1, T& s2) {

	T y1 = j12 + j21;
	T x1 = j11 - j22;
	T y2 = j12 - j21;
	T x2 = j11 + j22;

	T h1 = T(sqrt(y1*y1 + x1*x1));
	T h2 = T(sqrt(y2*y2 + x2*x2));

	s1 = (h1 + h2) / T(2.0);
	s2 = (h1 - h2) / T(2.0);

	//cout << "s1: " << s1 << endl;
	//cout << "s2: " << s2 << endl;

}

template <typename T>
void findJacobian(const double* orig_v2D_A, const double* orig_v2D_B, const double* orig_v2D_C,
				  T* new_v2D_A, T* new_v2D_B, T* new_v2D_C,
				  T& j11, T& j12, T& j21, T& j22) {
	/*

	 To find J we can use the points B and C:

	 J = j11	j12
	 j21	j22

	 |j11  j12|   |b1|   |b'1|
	 JB = B'	 ->  |		  | * |  | = |   |
	 |j21  j22|   |b2|   |b'2|

	 -->

	 |j11  j12|   |c1|   |c'1|
	 JC = C'	 ->  |        | * |  | = |   |
	 |j21  j22|   |c2|   |c'2|


	 We know, that c2 is zero, due to the nature of the embedding of the original triangle in 2d

	 j11 * b1 + j12 * b2 = b'1
	 j21 * b1 + j22 * b2 = b'2
	 j11 * c1 = c'1
	 j21 * c1 = c'2

	 Thereofore:

	 j11 = c'1 / c1
	 j21 = c'2 / c1

	 j12 = ( b'1 - j11*b1 ) / b2
	 j22 = ( b'2 - j21*b1 ) / b2

	 */

	j11 = new_v2D_C[0] / T(orig_v2D_C[0]);
	j21 = new_v2D_C[1] / T(orig_v2D_C[0]);

	j12 = ( new_v2D_B[0] - j11 * T(orig_v2D_B[0]) ) / T(orig_v2D_B[1]);
	j22 = ( new_v2D_B[1] - j21 * T(orig_v2D_B[0]) ) / T(orig_v2D_B[1]);
}

void map_3D_triangles_to_2D_undistorded(
										const Eigen::Vector3d &v3DA,
										const Eigen::Vector3d &v3DB,
										const Eigen::Vector3d &v3DC,
										Eigen::Vector2d &v2DAembedded,
										Eigen::Vector2d &v2DBembedded,
										Eigen::Vector2d &v2DCembedded
										) {
	/*
	     b
	 A ______ B
	   \    /
	  a \  / c
	     \/
	     C

	 We place A on 0,0
	 A = 0,0

	 We place C on the x axis
	 C = ||C-A||, 0

	 We compute B via angle

	 b = B - A
	 a = C - A

	 phi =  arccos(a*b)/|a|*|b|

	 B = cos(phi)*b, sin(phi)*b
	 */

	Eigen::Vector3d xUnitVector = Eigen::Vector3d::UnitX();
	Eigen::Vector3d yUnitVector = Eigen::Vector3d::UnitY();

	//translate to origin
	Eigen::Vector3d v3DAembedded;
	v3DAembedded<< 0,0,0;

	Eigen::Vector3d v3DBembedded = v3DB - v3DA;
	Eigen::Vector3d v3DCembedded = v3DC - v3DA;

	// rotate triangle, s.t. C is on x axis
	double angleC_X = std::acos( (v3DCembedded.dot(xUnitVector)) / v3DCembedded.norm() );
	Eigen::Vector3d axis = v3DCembedded.cross(xUnitVector);

	if (axis.norm() > 0) {
		axis.normalize();
		Eigen::AngleAxisd rotationY(angleC_X, axis);

		v3DCembedded = rotationY * v3DCembedded;
		v3DBembedded = rotationY * v3DBembedded;
	}


	// rotate triangle, such that B is in x-y plane
	Eigen::Vector3d v3DBembeddedProjectedZYPlane;
	v3DBembeddedProjectedZYPlane << 0, v3DBembedded(1), v3DBembedded(2);
	double angleB_XY = std::acos( (v3DBembeddedProjectedZYPlane.dot(yUnitVector)) / v3DBembeddedProjectedZYPlane.norm() );
	axis = v3DBembeddedProjectedZYPlane.cross(yUnitVector);

	if (axis.norm() > 0) {

		axis.normalize();
		Eigen::AngleAxisd rotationX(angleB_XY, axis);

		v3DBembedded = rotationX * v3DBembedded;
	}

	v2DAembedded << v3DAembedded(0), v3DAembedded(1);
	v2DBembedded << v3DBembedded(0), v3DBembedded(1);
	v2DCembedded << v3DCembedded(0), v3DCembedded(1);
}

struct DistortionResidual {
	DistortionResidual(const double* v2D_A, const double* v2D_B, const double* v2D_C)
	: orig_v2D_A(v2D_A), orig_v2D_B(v2D_B), orig_v2D_C(v2D_C) {}
	template <typename T>
	bool operator()(
					const T* const new_v2D_A_offset,
					const T* const new_v2D_B_offset,
					const T* const new_v2D_C_offset,
					T* residuals) const {


		/*
		 The Jacobian J represents the mapping from the embedded, undistorted triangles to the distorted triangles of the current iterate:

            A ______ B         A'__________ B'
              \    /	  J		|	    .
               \  /		-->     |.  ^
                \/				C'
                 C

		 Given an ortogonal frame per triangle, we can express any point in this local coordinatesystem. We first place the triangle of the current iterate at the origin:
		 */

		T new_v2D_A[2];
		T new_v2D_B[2];
		T new_v2D_C[2];

		new_v2D_A[0] = T(0);
		new_v2D_A[1] = T(0);
		new_v2D_B[0] = new_v2D_B_offset[0] - new_v2D_A_offset[0];
		new_v2D_B[1] = new_v2D_B_offset[1] - new_v2D_A_offset[1];
		new_v2D_C[0] = new_v2D_C_offset[0] - new_v2D_A_offset[0];
		new_v2D_C[1] = new_v2D_C_offset[1] - new_v2D_A_offset[1];

		/*
		 The unwrapping can be characterised as a linear function w.r.t. the undistorted triangle. Given a point p1 "on" the undistorted triangle, it must hold that

		 J*p1 = q1

		 where q1 is the position of the point after the mapping. Ideally, no distortion is induced. This means J == R, that is some rotation matrix.

		 That means, the singular values of J should all be 1.
		 */

		T j11, j12, j21, j22;
		findJacobian(orig_v2D_A, orig_v2D_B, orig_v2D_C,
					 new_v2D_A, new_v2D_B, new_v2D_C,
					 j11, j12, j21, j22);

		/*
			Given J, we can find the singular values s1, s2:
		 */

		T s1, s2;
		computeSingularValues(j11, j12, j21, j22, s1, s2);


		const Eigen::Map<const Eigen::Matrix<T, 1, 2> > ev1(new_v2D_A_offset);
		const Eigen::Map<const Eigen::Matrix<T, 1, 2> > ev2(new_v2D_B_offset);
		const Eigen::Map<const Eigen::Matrix<T, 1, 2> > ev3(new_v2D_C_offset);

		Eigen::Matrix<T, 1, 2> e21 = ev1 - ev2;
		Eigen::Matrix<T, 1, 2> e23 = ev3 - ev2;

		T area_2 = cross_2D(e21, e23);

		if (area_2 < T(0.0)){
			cout << "area negative! " << endl;
			return false;
		}

		residuals[0] = s1;
		residuals[1] = s2;
		residuals[2] = T(1.0) / s1;
		residuals[3] = T(1.0) / s2;

		return true;
	}
	const double* orig_v2D_A;
	const double* orig_v2D_B;
	const double* orig_v2D_C;
};

bool solve_map_with_ceres(const Eigen::MatrixXd &Vertex3D, const Eigen::MatrixXi &FaceIndices, Eigen::MatrixXd &UV, int nIterations){
	int num_faces = FaceIndices.rows();

	Eigen::Matrix<double, Eigen::Dynamic, 2, Eigen::RowMajor> vertices2d = UV;
	Eigen::Matrix<double, Eigen::Dynamic, 3, Eigen::RowMajor> vertices3d = Vertex3D;
	Eigen::Matrix<double, Eigen::Dynamic, 2, Eigen::RowMajor> vertices2DEmbedded(num_faces*3, 2);

	ceres::Problem problem;
	for (int i = 0; i < num_faces; ++i) {

		// computed undistorted embedding
		Eigen::Vector2d vertex2DAEmbedded;
		Eigen::Vector2d vertex2DBEmbedded;
		Eigen::Vector2d vertex2DCEmbedded;

		map_3D_triangles_to_2D_undistorded(vertices3d.row(FaceIndices(i, 0)),
										   vertices3d.row(FaceIndices(i, 1)),
										   vertices3d.row(FaceIndices(i, 2)),
										   vertex2DAEmbedded,
										   vertex2DBEmbedded,
										   vertex2DCEmbedded);

		vertices2DEmbedded.row(3*i) = vertex2DAEmbedded;
		vertices2DEmbedded.row(3*i+1) = vertex2DBEmbedded;
		vertices2DEmbedded.row(3*i+2) = vertex2DCEmbedded;

		problem.AddResidualBlock(
			new ceres::AutoDiffCostFunction<DistortionResidual, 4, 2, 2, 2>(
				new DistortionResidual(
						&vertices2DEmbedded(3*i, 0),
						&vertices2DEmbedded(3*i+1, 0),
						&vertices2DEmbedded(3*i+2, 0)
				)
			),
			NULL,
			&vertices2d(FaceIndices(i, 0), 0),
			&vertices2d(FaceIndices(i, 1), 0),
			&vertices2d(FaceIndices(i, 2), 0));
	}

	// Make Ceres automatically detect the bundle structure. Note that the
	// standard solver, SPARSE_NORMAL_CHOLESKY, also works fine but it is slower
	// for standard bundle adjustment problems.
	ceres::Solver::Options options;
	options.max_num_iterations = nIterations;
	options.linear_solver_type = ceres::CGNR;
	options.sparse_linear_algebra_library_type = ceres::EIGEN_SPARSE;
	options.minimizer_progress_to_stdout = true;
	ceres::Solver::Summary summary;
	ceres::Solve(options, &problem, &summary);
	std::cout << summary.FullReport() << "\n";
	
	UV = vertices2d;
	return true;
}