2013-03-25 14:19:22 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* VCGLib o o *
|
|
|
|
* Visual and Computer Graphics Library o o *
|
|
|
|
* _ O _ *
|
2016-06-13 07:29:25 +02:00
|
|
|
* Copyright(C) 2004-2016 \/)\/ *
|
2013-03-25 14:19:22 +01:00
|
|
|
* 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. *
|
|
|
|
* *
|
|
|
|
****************************************************************************/
|
|
|
|
#include<vcg/complex/complex.h>
|
|
|
|
|
|
|
|
// input output
|
|
|
|
#include <wrap/io_trimesh/import_ply.h>
|
|
|
|
#include <wrap/io_trimesh/export_ply.h>
|
|
|
|
|
|
|
|
#include<vcg/complex/algorithms/outline_support.h>
|
|
|
|
|
2013-03-26 12:33:17 +01:00
|
|
|
#include <vcg/space/outline2_packer.h>
|
|
|
|
#include <wrap/qt/outline2_rasterizer.h>
|
|
|
|
|
|
|
|
#include <vcg/space/rasterized_outline2_packer.h>
|
|
|
|
#include <wrap/qt/Outline2ToQImage.h>
|
2017-06-13 15:46:19 +02:00
|
|
|
/*! \file trimesh_texture.cpp
|
|
|
|
\ingroup code_sample
|
|
|
|
|
|
|
|
\brief a small example about packing texture regions
|
|
|
|
|
|
|
|
It takes a sample mesh as input, computes the connected
|
|
|
|
components of its parametrization and pack them again using two different strategies
|
|
|
|
*/
|
2013-03-26 12:33:17 +01:00
|
|
|
|
|
|
|
using namespace vcg;
|
2013-03-25 14:19:22 +01:00
|
|
|
|
|
|
|
class MyEdge;
|
|
|
|
class MyFace;
|
|
|
|
class MyVertex;
|
|
|
|
struct MyUsedTypes : public UsedTypes< Use<MyVertex>::AsVertexType, Use<MyFace>::AsFaceType>{};
|
|
|
|
|
|
|
|
class MyVertex : public Vertex< MyUsedTypes, vertex::Coord3f, vertex::BitFlags >{};
|
|
|
|
class MyFace : public Face < MyUsedTypes, face::VertexRef, face::FFAdj, face::WedgeTexCoord2f, face::Mark, face::BitFlags > {};
|
|
|
|
class MyMesh : public tri::TriMesh< std::vector<MyVertex>, std::vector<MyFace > >{};
|
|
|
|
|
|
|
|
|
|
|
|
int main(int ,char ** )
|
|
|
|
{
|
|
|
|
MyMesh m,tm;
|
|
|
|
tri::io::ImporterPLY<MyMesh>::Open(m,"../../meshes/bunny10k_textured.ply");
|
|
|
|
|
2017-06-13 15:46:19 +02:00
|
|
|
// 1) Build a mesh with tex coords as coords.
|
|
|
|
|
|
|
|
for( auto &&f : m.face)
|
2013-03-25 14:19:22 +01:00
|
|
|
{
|
2017-06-13 15:46:19 +02:00
|
|
|
tri::Allocator<MyMesh>::AddFace(tm,
|
|
|
|
Point3f(f.WT(0).U(),f.WT(0).V(),0),
|
|
|
|
Point3f(f.WT(1).U(),f.WT(1).V(),0),
|
|
|
|
Point3f(f.WT(2).U(),f.WT(2).V(),0));
|
2013-03-25 14:19:22 +01:00
|
|
|
}
|
2017-06-13 15:46:19 +02:00
|
|
|
|
|
|
|
// 2) compute connected components (e.g. texture regions in an atlas)
|
2013-03-25 14:19:22 +01:00
|
|
|
tri::Clean<MyMesh>::RemoveDuplicateVertex(tm);
|
|
|
|
tri::UpdateTopology<MyMesh>::FaceFace(tm);
|
2017-06-13 15:46:19 +02:00
|
|
|
std::vector<std::pair<int,MyMesh::FacePointer> > fpVec;
|
2013-03-25 14:19:22 +01:00
|
|
|
tri::Clean<MyMesh>::ConnectedComponents(tm,fpVec);
|
2016-01-25 15:47:06 +01:00
|
|
|
printf("Mesh has %lu texture components\n",fpVec.size());
|
2013-03-25 14:19:22 +01:00
|
|
|
tri::io::ExporterPLY<MyMesh>::Save(tm,"out.ply");
|
|
|
|
std::vector< std::vector<Point2f> > outline2Vec;
|
2017-06-13 15:46:19 +02:00
|
|
|
|
|
|
|
// build the 2D outlines of each regions
|
2013-03-26 12:33:17 +01:00
|
|
|
for(size_t i=0; i<fpVec.size();++i)
|
|
|
|
{
|
|
|
|
tri::UpdateSelection<MyMesh>::FaceClear(tm);
|
|
|
|
fpVec[i].second->SetS();
|
|
|
|
tri::UpdateSelection<MyMesh>::FaceConnectedFF(tm);
|
|
|
|
tri::UpdateSelection<MyMesh>::VertexClear(tm);
|
|
|
|
tri::UpdateSelection<MyMesh>::VertexFromFaceLoose(tm);
|
|
|
|
|
|
|
|
MyMesh comp;
|
|
|
|
tri::Append<MyMesh,MyMesh>::Mesh(comp, tm, true);
|
|
|
|
|
|
|
|
std::vector< std::vector<Point3f> > outline3Vec;
|
|
|
|
tri::OutlineUtil<float>::ConvertMeshBoundaryToOutline3Vec(comp, outline3Vec);
|
|
|
|
std::vector< std::vector<Point2f> > compOutline2Vec;
|
|
|
|
tri::OutlineUtil<float>::ConvertOutline3VecToOutline2Vec(outline3Vec,compOutline2Vec);
|
|
|
|
int largestInd=tri::OutlineUtil<float>::LargestOutline2(compOutline2Vec);
|
|
|
|
if(tri::OutlineUtil<float>::Outline2Area(compOutline2Vec[largestInd])<0)
|
|
|
|
tri::OutlineUtil<float>::ReverseOutline2(compOutline2Vec[largestInd]);
|
|
|
|
|
|
|
|
outline2Vec.push_back(compOutline2Vec[largestInd]);
|
|
|
|
}
|
|
|
|
|
2016-01-25 15:47:06 +01:00
|
|
|
printf("Mesh has %lu texture components\n",outline2Vec.size());
|
2013-03-26 12:33:17 +01:00
|
|
|
|
|
|
|
Outline2Dumper::Param pp;
|
2013-03-25 14:19:22 +01:00
|
|
|
Similarity2f sim;
|
|
|
|
sim.sca=1024.0f;
|
|
|
|
std::vector<Similarity2f> trVec(outline2Vec.size(),sim);
|
2016-01-25 15:47:06 +01:00
|
|
|
printf("Mesh has %lu texture components\n",outline2Vec.size());
|
2017-06-13 15:46:19 +02:00
|
|
|
|
|
|
|
// Dump the original parametrization as a png
|
2013-03-26 12:33:17 +01:00
|
|
|
Outline2Dumper::dumpOutline2VecPNG("PrePack.png",outline2Vec,trVec,pp);
|
2013-03-25 14:19:22 +01:00
|
|
|
|
2017-06-13 15:46:19 +02:00
|
|
|
// Pack using Axis Aligned Rect
|
2013-03-26 12:33:17 +01:00
|
|
|
const Point2i containerSize(1024,1024);
|
2013-03-25 14:19:22 +01:00
|
|
|
Point2f finalSize(1024,1024);
|
|
|
|
PolyPacker<float>::PackAsAxisAlignedRect(outline2Vec,containerSize,trVec,finalSize);
|
|
|
|
|
2013-03-26 12:33:17 +01:00
|
|
|
Outline2Dumper::dumpOutline2VecPNG("PostPack.png",outline2Vec,trVec,pp);
|
|
|
|
|
2017-06-13 15:46:19 +02:00
|
|
|
// Pack using Oriented Rect
|
2013-03-26 12:33:17 +01:00
|
|
|
RasterizedOutline2Packer<float, QtOutline2Rasterizer>::Parameters packingParam;
|
|
|
|
packingParam.costFunction = RasterizedOutline2Packer<float, QtOutline2Rasterizer>::Parameters::LowestHorizon;
|
|
|
|
packingParam.doubleHorizon = true;
|
2019-01-31 14:28:24 +01:00
|
|
|
packingParam.innerHorizon = true;
|
|
|
|
packingParam.permutations = false;
|
2013-03-26 12:33:17 +01:00
|
|
|
packingParam.rotationNum = 16; //number of rasterizations in 90°
|
2019-01-31 14:28:24 +01:00
|
|
|
packingParam.gutterWidth = 2;
|
2013-03-26 12:33:17 +01:00
|
|
|
|
|
|
|
RasterizedOutline2Packer<float, QtOutline2Rasterizer>::Pack(outline2Vec,containerSize,trVec,packingParam);
|
|
|
|
Outline2Dumper::dumpOutline2VecPNG("PostPackRR.png",outline2Vec,trVec,pp);
|
|
|
|
|
2013-03-25 14:19:22 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|