File: arap_parametrization.h

package info (click to toggle)
meshlab 2020.09%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 45,124 kB
  • sloc: cpp: 400,238; ansic: 31,952; javascript: 1,578; sh: 387; yacc: 238; lex: 139; python: 86; makefile: 29
file content (215 lines) | stat: -rw-r--r-- 6,736 bytes parent folder | download | duplicates (3)
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
/****************************************************************************
* VCGLib                                                            o o     *
* Visual and Computer Graphics Library                            o     o   *
*                                                                _   O  _   *
* Copyright(C) 2017                                                \/)\/    *
* Visual Computing Lab                                            /\/|      *
* ISTI - Italian National Research Council                           |      *
*                                                                    \      *
* All rights reserved.                                                      *
*                                                                           *
* This program is free software; you can redistribute it and/or modify      *
* it under the terms of the GNU General Public License as published by      *
* the Free Software Foundation; either version 2 of the License, or         *
* (at your option) any later version.                                       *
*                                                                           *
* This program is distributed in the hope that it will be useful,           *
* but WITHOUT ANY WARRANTY; without even the implied warranty of            *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
* for more details.                                                         *
*                                                                           *
****************************************************************************/

/* Optimizes given UV-mapping with
 * [ARAP parametrization]
 * (minimizes area and angle distortions).
 *
 * Needs:
 * (-) per-vertex texture coords
 * (-) per-vertex flags to fix boundaries
 *     Fixed vertices are the flagged ones.
 *     By default: BORDER or SELECTED verts are fixed.
 *     (use fixedMask parameter to customize)
 *
 * Example of usage:
 *    MeshType m;
 *    ...
 *    vcg::tri::UpdateBounding<MeshType>::Box(m);
 *    vcg::tri::UpdateFlags<MeshType>::Clear(m);
 *    vcg::tri::UpdateFlags<MeshType>::VertexBorderFromNone(m);
 *    vcg::tri::OptimizeUV_ARAP(m);
 *
 */

#ifndef __VCG_IGL_ARAP_PARAMETRIZATION
#define __VCG_IGL_ARAP_PARAMETRIZATION

#include <cmath>

#include <igl/arap.h>
#include <vcg/complex/algorithms/mesh_to_matrix.h>
#include <vcg/complex/algorithms/clean.h>
#include <vcg/complex/algorithms/update/flag.h>
#include <wrap/igl/lscm_parametrization.h>

namespace vcg {
namespace tri {

template<class MeshType>
void OptimizeUV_ARAP(
        MeshType& m,
        unsigned int iterations = 100,
        unsigned int fixedMask = MeshType::VertexType::BORDER | MeshType::VertexType::SELECTED,
        bool generateInitialGuess = true)
{

	// check requirements
	vcg::tri::RequirePerVertexTexCoord(m);
	vcg::tri::RequirePerVertexFlags   (m);
	vcg::tri::RequireCompactness      (m);

	if (m.vert.size() <= 1 || m.face.size() == 0)
	{
		return;
	}

	// build fixed points data
	size_t nFixed = 0;
	if (fixedMask != 0)
	{
		for (size_t i=0; i<m.vert.size(); i++)
		{
			if (m.vert[i].Flags() & fixedMask) nFixed++;
		}
	}

	// all fixed, nothing to do? get out to avoid crashes
	if (nFixed == m.vert.size())
	{
		return;
	}

	if (generateInitialGuess)
	{
		// if not enough vertices are fixed, initialize manually fixed points
		// else initialize with the provided fixed values
		InitializeArapWithLSCM(m, (nFixed < 2) ? 0 : fixedMask);
	}

	Eigen::MatrixXd V;
	Eigen::MatrixXi F;
	Eigen::VectorXi b;
	Eigen::MatrixXd bc;
	Eigen::MatrixXd V_uv;
	vcg::tri::MeshToMatrix<MeshType>::GetTriMeshData(m, F, V);
	vcg::tri::MeshToMatrix<MeshType>::GetUVData(m, V_uv);

	b.resize(nFixed);
	bc.resize(nFixed,2);
	for (size_t i=0,k=0; i<m.vert.size(); i++)
	{
		if (m.vert[i].Flags() & fixedMask)
		{
			b(k) = i;
			bc(k,0) = m.vert[i].T().P()[0];
			bc(k,1) = m.vert[i].T().P()[1];
			k++;
		}
	}

	// Add dynamic regularization to avoid to specify boundary conditions
	::igl::ARAPData arap_data;
	arap_data.with_dynamics = true;
	arap_data.max_iter = iterations;

	// compute ARAP parametrization
	::igl::arap_precomputation(V, F, 2, b, arap_data);
	::igl::arap_solve(bc, arap_data, V_uv);

	// copy results back to mesh
	for (size_t i=0; i<m.vert.size(); i++)
	{
		m.vert[i].T().P()[0] = V_uv(i,0);
		m.vert[i].T().P()[1] = V_uv(i,1);
	}
}

template <class MeshType>
void InitializeArapWithLSCM(MeshType & m, unsigned int fixedMask = 0)
{
	typedef typename MeshType::ScalarType                          ScalarType;
	typedef typename MeshType::VertexType::TexCoordType::PointType TexPointType;
	typedef typename TexPointType::ScalarType                      TexScalarType;

	if (fixedMask == 0)
	{
		// automatically select 2 vertices to fix
		vcg::tri::UpdateFlags<MeshType>::Clear(m);

		int fixed0, fixed1 = -1;
		auto p = m.bbox.Center();
		ScalarType maxDist = -1;
		for (size_t i=0; i<m.vert.size(); i++)
		{
			// farthest point from the center
			const ScalarType dist =(m.vert[i].cP() - p).Norm();
			if (dist > maxDist)
			{
				fixed0 = i;
				maxDist = dist;
			}
		}
		maxDist = -1;
		p = m.vert[fixed0].cP();
		for (size_t i=0; i<m.vert.size(); i++)
		{
			// farthest point from the previous
			const ScalarType dist =(m.vert[i].cP() - p).Norm();
			if (dist > maxDist)
			{
				fixed1 = i;
				maxDist = dist;
			}
		}

		assert(fixed0 >= 0);
		assert(fixed1 >= 0);
		assert(fixed0 != fixed1);

		//then select them
		m.vert[fixed0].SetS();
		m.vert[fixed1].SetS();
		m.vert[fixed0].T().P() = TexPointType(0,0);
		m.vert[fixed1].T().P() = TexPointType(1,1);

		fixedMask = MeshType::VertexType::SELECTED;
	}

	vcg::tri::OptimizeUV_LSCM(m, fixedMask);

	// Rescale the parametrization to match the 3D area
	ScalarType meshArea2D = 0;
	ScalarType meshArea3D = 0;

	for (size_t i=0; i<m.face.size(); i++)
	{
		vcg::Triangle2<TexScalarType> t2(m.face[i].V(0)->T().P(),
		                                 m.face[i].V(1)->T().P(),
		                                 m.face[i].V(2)->T().P());
		meshArea2D += ScalarType(fabs(((t2.P(1) - t2.P(0)) ^ (t2.P(2) - t2.P(0)))/2));
		meshArea3D += vcg::DoubleArea(m.face[i])/2;
	}

	ScalarType scaleFact = std::sqrt(meshArea3D / meshArea2D);

	for (size_t i=0; i<m.vert.size(); i++)
	{
		TexPointType & UVCoord = m.vert[i].T().P();
		UVCoord *= scaleFact;
	}
}

}} // namespaces

#endif // __VCG_IGL_ARAP_PARAMETRIZATION