/**************************************************************************** * 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::Box(m); * vcg::tri::UpdateFlags::Clear(m); * vcg::tri::UpdateFlags::VertexBorderFromNone(m); * vcg::tri::OptimizeUV_ARAP(m); * */ #ifndef __VCG_IGL_ARAP_PARAMETRIZATION #define __VCG_IGL_ARAP_PARAMETRIZATION #include #include #include #include #include #include namespace vcg { namespace tri { template 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::GetTriMeshData(m, F, V); vcg::tri::MeshToMatrix::GetUVData(m, V_uv); b.resize(nFixed); bc.resize(nFixed,2); for (size_t i=0,k=0; i 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::Clear(m); int fixed0, fixed1 = -1; auto p = m.bbox.Center(); ScalarType maxDist = -1; for (size_t i=0; i maxDist) { fixed0 = i; maxDist = dist; } } maxDist = -1; p = m.vert[fixed0].cP(); for (size_t i=0; i 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 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