VCG Library
trimesh_texture.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004-2016 \/)\/ *
6 * Visual Computing Lab /\/| *
7 * ISTI - Italian National Research Council | *
8 * \ *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20 * for more details. *
21 * *
22 ****************************************************************************/
23 #include<vcg/complex/complex.h>
24 
25 // input output
26 #include <wrap/io_trimesh/import_ply.h>
27 #include <wrap/io_trimesh/export_ply.h>
28 
29 #include<vcg/complex/algorithms/outline_support.h>
30 
31 #include <vcg/space/outline2_packer.h>
32 #include <wrap/qt/outline2_rasterizer.h>
33 
34 #include <vcg/space/rasterized_outline2_packer.h>
35 #include <wrap/qt/Outline2ToQImage.h>
45 using namespace vcg;
46 
47 class MyEdge;
48 class MyFace;
49 class MyVertex;
50 struct MyUsedTypes : public UsedTypes< Use<MyVertex>::AsVertexType, Use<MyFace>::AsFaceType>{};
51 
52 class MyVertex : public Vertex< MyUsedTypes, vertex::Coord3f, vertex::BitFlags >{};
53 class MyFace : public Face < MyUsedTypes, face::VertexRef, face::FFAdj, face::WedgeTexCoord2f, face::Mark, face::BitFlags > {};
54 class MyMesh : public tri::TriMesh< std::vector<MyVertex>, std::vector<MyFace > >{};
55 
56 
57 int main(int ,char ** )
58 {
59  MyMesh m,tm;
60  tri::io::ImporterPLY<MyMesh>::Open(m,"../../meshes/bunny10k_textured.ply");
61 
62  // 1) Build a mesh with tex coords as coords.
63 
64  for( auto &&f : m.face)
65  {
67  Point3f(f.WT(0).U(),f.WT(0).V(),0),
68  Point3f(f.WT(1).U(),f.WT(1).V(),0),
69  Point3f(f.WT(2).U(),f.WT(2).V(),0));
70  }
71 
72  // 2) compute connected components (e.g. texture regions in an atlas)
75  std::vector<std::pair<int,MyMesh::FacePointer> > fpVec;
77  printf("Mesh has %lu texture components\n",fpVec.size());
78  tri::io::ExporterPLY<MyMesh>::Save(tm,"out.ply");
79  std::vector< std::vector<Point2f> > outline2Vec;
80 
81  // build the 2D outlines of each regions
82  for(size_t i=0; i<fpVec.size();++i)
83  {
85  fpVec[i].second->SetS();
89 
90  MyMesh comp;
91  tri::Append<MyMesh,MyMesh>::Mesh(comp, tm, true);
92 
93  std::vector< std::vector<Point3f> > outline3Vec;
94  tri::OutlineUtil<float>::ConvertMeshBoundaryToOutline3Vec(comp, outline3Vec);
95  std::vector< std::vector<Point2f> > compOutline2Vec;
96  tri::OutlineUtil<float>::ConvertOutline3VecToOutline2Vec(outline3Vec,compOutline2Vec);
97  int largestInd=tri::OutlineUtil<float>::LargestOutline2(compOutline2Vec);
98  if(tri::OutlineUtil<float>::Outline2Area(compOutline2Vec[largestInd])<0)
99  tri::OutlineUtil<float>::ReverseOutline2(compOutline2Vec[largestInd]);
100 
101  outline2Vec.push_back(compOutline2Vec[largestInd]);
102  }
103 
104  printf("Mesh has %lu texture components\n",outline2Vec.size());
105 
106  Outline2Dumper::Param pp;
107  Similarity2f sim;
108  sim.sca=1024.0f;
109  std::vector<Similarity2f> trVec(outline2Vec.size(),sim);
110  printf("Mesh has %lu texture components\n",outline2Vec.size());
111 
112  // Dump the original parametrization as a png
113  Outline2Dumper::dumpOutline2VecPNG("PrePack.png",outline2Vec,trVec,pp);
114 
115  // Pack using Axis Aligned Rect
116  const Point2i containerSize(1024,1024);
117  Point2f finalSize(1024,1024);
118  PolyPacker<float>::PackAsAxisAlignedRect(outline2Vec,containerSize,trVec,finalSize);
119 
120  Outline2Dumper::dumpOutline2VecPNG("PostPack.png",outline2Vec,trVec,pp);
121 
122  // Pack using Oriented Rect
123  RasterizedOutline2Packer<float, QtOutline2Rasterizer>::Parameters packingParam;
124  packingParam.costFunction = RasterizedOutline2Packer<float, QtOutline2Rasterizer>::Parameters::LowestHorizon;
125  packingParam.doubleHorizon = true;
126  packingParam.cellSize = 4;
127  packingParam.rotationNum = 16; //number of rasterizations in 90°
128 
129  RasterizedOutline2Packer<float, QtOutline2Rasterizer>::Pack(outline2Vec,containerSize,trVec,packingParam);
130  Outline2Dumper::dumpOutline2VecPNG("PostPackRR.png",outline2Vec,trVec,pp);
131 
132  return 0;
133 }
134