2016-06-13 07:29:25 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* VCGLib o o *
|
|
|
|
* Visual and Computer Graphics Library o o *
|
|
|
|
* _ O _ *
|
|
|
|
* Copyright(C) 2004-2016 \/)\/ *
|
|
|
|
* 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. *
|
|
|
|
* *
|
|
|
|
****************************************************************************/
|
2012-07-02 18:41:28 +02:00
|
|
|
#ifndef VORONOI_ATLAS_H
|
|
|
|
#define VORONOI_ATLAS_H
|
2013-04-03 12:07:29 +02:00
|
|
|
|
2012-07-02 18:41:28 +02:00
|
|
|
#include<vcg/complex/algorithms/parametrization/poisson_solver.h>
|
|
|
|
#include<vcg/complex/algorithms/parametrization/uv_utils.h>
|
|
|
|
#include<vcg/complex/algorithms/parametrization/distortion.h>
|
2013-04-03 12:07:29 +02:00
|
|
|
#include<vcg/space/outline2_packer.h>
|
|
|
|
#include<vcg/space/rasterized_outline2_packer.h>
|
2012-07-02 18:41:28 +02:00
|
|
|
#include<vcg/complex/algorithms/update/texture.h>
|
|
|
|
#include<vcg/complex/algorithms/point_sampling.h>
|
2014-03-04 01:53:39 +01:00
|
|
|
#include<vcg/complex/algorithms/voronoi_processing.h>
|
2012-07-02 18:41:28 +02:00
|
|
|
|
2014-03-04 01:53:39 +01:00
|
|
|
//#include<wrap/qt/outline2_rasterizer.h>
|
2013-04-03 12:07:29 +02:00
|
|
|
|
2012-07-02 18:41:28 +02:00
|
|
|
namespace vcg {
|
|
|
|
namespace tri {
|
|
|
|
|
|
|
|
template <class MeshType>
|
|
|
|
class VoronoiAtlas
|
|
|
|
{
|
|
|
|
//private:
|
|
|
|
public:
|
|
|
|
class VoroEdge;
|
|
|
|
class VoroFace;
|
|
|
|
class VoroVertex;
|
|
|
|
struct VoroUsedTypes : public UsedTypes< Use<VoroVertex> ::template AsVertexType,
|
|
|
|
Use<VoroEdge> ::template AsEdgeType,
|
|
|
|
Use<VoroFace> ::template AsFaceType>{};
|
|
|
|
|
|
|
|
class VoroVertex : public Vertex< VoroUsedTypes, vertex::Coord3f, vertex::Normal3f, vertex::TexCoord2f, vertex::VFAdj , vertex::Qualityf, vertex::Color4b, vertex::BitFlags >{};
|
2016-02-02 16:10:49 +01:00
|
|
|
class VoroFace : public Face< VoroUsedTypes, face::VertexRef, face::BitFlags, face::FFAdj ,face::VFAdj , face::CurvatureDirf,face::WedgeTexCoord2f> {};
|
2012-07-02 18:41:28 +02:00
|
|
|
class VoroEdge : public Edge< VoroUsedTypes>{};
|
|
|
|
class VoroMesh : public tri::TriMesh< std::vector<VoroVertex>, std::vector<VoroFace> , std::vector<VoroEdge> > {};
|
|
|
|
|
|
|
|
typedef typename VoroMesh::FaceIterator FaceIterator;
|
|
|
|
typedef typename VoroMesh::VertexType VertexType;
|
|
|
|
typedef typename VoroMesh::FaceType FaceType;
|
|
|
|
|
|
|
|
static void CollectUVBorder(VoroMesh *rm, std::vector<Point2f> &uvBorder)
|
|
|
|
{
|
|
|
|
tri::UpdateTopology<VoroMesh>::FaceFace(*rm);
|
|
|
|
tri::UpdateFlags<VoroMesh>::FaceClearV(*rm);
|
|
|
|
for(FaceIterator fi=rm->face.begin();fi!=rm->face.end();++fi)
|
|
|
|
{
|
|
|
|
for(int j=0;j<3;++j)
|
|
|
|
if(face::IsBorder(*fi,j) && !(fi->IsV()))
|
|
|
|
{
|
|
|
|
face::Pos<FaceType> pp(&*fi,j,fi->V(j));
|
|
|
|
assert(pp.IsBorder());
|
|
|
|
face::Pos<FaceType> startPos = pp;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
uvBorder.push_back( pp.F()->WT(pp.VInd()).P() );
|
|
|
|
pp.F()->SetV();
|
|
|
|
pp.NextB();
|
|
|
|
} while(pp != startPos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// take a mesh and rescale its uv so that they are in the 0..1 range
|
|
|
|
static void RegularizeTexArea(VoroMesh &m)
|
|
|
|
{
|
|
|
|
float areaTex=0;
|
|
|
|
float areaGeo=0;
|
|
|
|
|
|
|
|
vcg::Box2f UVBox = tri::UV_Utils<VoroMesh>::PerWedgeUVBox(m);
|
|
|
|
for(FaceIterator fi=m.face.begin();fi!=m.face.end();++fi)
|
|
|
|
{
|
|
|
|
areaTex+= fabs((fi->WT(1).P() - fi->WT(0).P()) ^ (fi->WT(2).P() - fi->WT(0).P())) ;
|
|
|
|
areaGeo+= DoubleArea(*fi);
|
|
|
|
}
|
|
|
|
|
|
|
|
float ratio = sqrt(areaGeo/areaTex);
|
|
|
|
|
|
|
|
for(FaceIterator fi=m.face.begin();fi!=m.face.end();++fi)
|
|
|
|
{
|
|
|
|
for(int j=0;j<3;++j)
|
|
|
|
fi->WT(j).P() = (fi->WT(j).P()-UVBox.min) *ratio;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
2012-07-03 11:13:07 +02:00
|
|
|
struct VoronoiAtlasParam
|
|
|
|
{
|
|
|
|
VoronoiAtlasParam()
|
|
|
|
{
|
2013-04-03 12:07:29 +02:00
|
|
|
maxIterNum = 5;
|
2012-07-03 11:13:07 +02:00
|
|
|
sampleNum=10;
|
|
|
|
overlap=false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Stat
|
|
|
|
{
|
2012-07-03 12:21:41 +02:00
|
|
|
void clear() { iterNum=totalTime=unwrapTime=voronoiTime=samplingTime=0;}
|
2012-07-03 11:13:07 +02:00
|
|
|
int totalTime;
|
|
|
|
int unwrapTime;
|
|
|
|
int voronoiTime;
|
|
|
|
int samplingTime;
|
2012-07-03 12:21:41 +02:00
|
|
|
|
|
|
|
int regionNum;
|
|
|
|
int iterNum;
|
2012-07-03 11:13:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
int sampleNum;
|
|
|
|
bool overlap;
|
|
|
|
Stat vas;
|
2013-04-03 12:07:29 +02:00
|
|
|
int maxIterNum;
|
2017-07-27 15:59:09 +02:00
|
|
|
CallBackPos *cb=vcg::CErrCallBackPos;
|
2012-07-03 11:13:07 +02:00
|
|
|
};
|
|
|
|
|
2012-07-02 18:41:28 +02:00
|
|
|
// Main parametrization function:
|
|
|
|
// it takes a startMesh, copy it and
|
|
|
|
|
2012-07-03 11:13:07 +02:00
|
|
|
static void Build( MeshType &startMesh, MeshType ¶Mesh, VoronoiAtlasParam &pp)
|
2012-07-02 18:41:28 +02:00
|
|
|
{
|
2012-07-03 11:13:07 +02:00
|
|
|
pp.vas.clear();
|
|
|
|
int t0=clock();
|
2012-07-02 18:41:28 +02:00
|
|
|
VoroMesh m; // the mesh used for the processing is a copy of the passed one.
|
|
|
|
tri::Append<VoroMesh, MeshType>::Mesh(m, startMesh);
|
|
|
|
tri::Clean<VoroMesh>::RemoveUnreferencedVertex(m);
|
|
|
|
tri::Allocator<VoroMesh>::CompactVertexVector(m);
|
|
|
|
tri::Allocator<VoroMesh>::CompactFaceVector(m);
|
|
|
|
|
|
|
|
tri::UpdateBounding<VoroMesh>::Box(m);
|
|
|
|
std::vector<VoroMesh *> meshRegionVec;
|
|
|
|
std::vector< std::vector<Point2f> > uvBorders;
|
2012-07-03 11:13:07 +02:00
|
|
|
|
|
|
|
// Main processing loop
|
2012-07-02 18:41:28 +02:00
|
|
|
do
|
|
|
|
{
|
2014-03-04 01:53:39 +01:00
|
|
|
// qDebug("************ ITERATION %i sampling mesh of %i with %i ************",pp.vas.iterNum,m.fn,pp.sampleNum);
|
2012-07-03 11:13:07 +02:00
|
|
|
int st0=clock();
|
2012-07-02 18:41:28 +02:00
|
|
|
std::vector<Point3f> PoissonSamples;
|
|
|
|
float diskRadius=0;
|
2012-07-03 11:13:07 +02:00
|
|
|
tri::PoissonSampling(m,PoissonSamples,pp.sampleNum,diskRadius);
|
|
|
|
int st1=clock();
|
|
|
|
pp.vas.samplingTime+= st1-st0;
|
2017-07-27 15:59:09 +02:00
|
|
|
pp.cb(50,StrFormat("Sampling created a new mesh of %lu points\n",PoissonSamples.size()));
|
2013-10-10 12:36:39 +02:00
|
|
|
EuclideanDistance<VoroMesh> edFunc;
|
2012-07-02 18:41:28 +02:00
|
|
|
std::vector<VertexType *> seedVec;
|
|
|
|
tri::VoronoiProcessing<VoroMesh>::SeedToVertexConversion(m,PoissonSamples,seedVec);
|
|
|
|
tri::UpdateTopology<VoroMesh>::VertexFace(m);
|
2013-10-10 12:36:39 +02:00
|
|
|
tri::VoronoiProcessing<VoroMesh>::ComputePerVertexSources(m,seedVec,edFunc);
|
2012-07-02 18:41:28 +02:00
|
|
|
tri::VoronoiProcessing<VoroMesh>::FaceAssociateRegion(m);
|
2013-11-25 11:39:48 +01:00
|
|
|
tri::VoronoiProcessing<VoroMesh>::VoronoiColoring(m,true);
|
2012-07-02 18:41:28 +02:00
|
|
|
std::vector<VoroMesh *> badRegionVec;
|
2012-07-03 11:13:07 +02:00
|
|
|
int st2=clock();
|
|
|
|
pp.vas.voronoiTime+=st2-st1;
|
2012-07-02 18:41:28 +02:00
|
|
|
for(size_t i=0; i<seedVec.size();++i)
|
|
|
|
{
|
|
|
|
VoroMesh *rm = new VoroMesh();
|
|
|
|
int selCnt = tri::VoronoiProcessing<VoroMesh>::FaceSelectAssociateRegion(m,seedVec[i]);
|
2017-07-27 15:59:09 +02:00
|
|
|
pp.cb(50,StrFormat("Region %i of %i faces",i,selCnt));
|
2013-04-03 12:07:29 +02:00
|
|
|
if(selCnt==0) continue;
|
2012-07-02 18:41:28 +02:00
|
|
|
assert(selCnt>0);
|
2012-07-03 11:13:07 +02:00
|
|
|
if(pp.overlap){
|
2012-07-02 18:41:28 +02:00
|
|
|
tri::UpdateSelection<VoroMesh>::VertexFromFaceLoose(m);
|
|
|
|
tri::UpdateSelection<VoroMesh>::FaceFromVertexLoose(m);
|
|
|
|
}
|
|
|
|
tri::Append<VoroMesh,VoroMesh>::Mesh(*rm, m, true);
|
2012-07-03 11:13:07 +02:00
|
|
|
int tp0=clock();
|
2012-07-02 18:41:28 +02:00
|
|
|
tri::PoissonSolver<VoroMesh> PS(*rm);
|
2017-07-27 15:59:09 +02:00
|
|
|
tri::UpdateBounding<VoroMesh>::Box(*rm);
|
2017-06-16 11:33:35 +02:00
|
|
|
if(PS.IsFeasible())
|
2012-07-02 18:41:28 +02:00
|
|
|
{
|
|
|
|
PS.Init();
|
|
|
|
PS.FixDefaultVertices();
|
|
|
|
PS.SolvePoisson(false);
|
|
|
|
tri::UpdateTexture<VoroMesh>::WedgeTexFromVertexTex(*rm);
|
|
|
|
RegularizeTexArea(*rm);
|
|
|
|
|
|
|
|
std::vector<Point2f> uvBorder;
|
|
|
|
CollectUVBorder(rm,uvBorder);
|
|
|
|
meshRegionVec.push_back(rm);
|
|
|
|
uvBorders.push_back(uvBorder);
|
2018-01-04 05:43:57 +01:00
|
|
|
int foldedCnt = tri::Distortion<VoroMesh,false>::FoldedNum(*rm);
|
2013-04-03 12:07:29 +02:00
|
|
|
if( foldedCnt > rm->fn/10)
|
|
|
|
{
|
|
|
|
badRegionVec.push_back(rm);
|
2014-03-04 01:53:39 +01:00
|
|
|
// qDebug("-- region %i Parametrized but with %i fold on %i!",i,foldedCnt,rm->fn);
|
2013-04-03 12:07:29 +02:00
|
|
|
}
|
2014-03-04 01:53:39 +01:00
|
|
|
// else qDebug("-- region %i Parametrized!",i);
|
2013-04-03 12:07:29 +02:00
|
|
|
|
2012-07-02 18:41:28 +02:00
|
|
|
} else
|
|
|
|
{
|
2014-03-04 01:53:39 +01:00
|
|
|
// qDebug("-- region %i is NOT homeomorphic to a disk\n",i);
|
2012-07-02 18:41:28 +02:00
|
|
|
badRegionVec.push_back(rm);
|
|
|
|
}
|
2012-07-03 11:13:07 +02:00
|
|
|
int tp1=clock();
|
|
|
|
pp.vas.unwrapTime +=tp1-tp0;
|
2012-07-03 12:21:41 +02:00
|
|
|
++pp.vas.iterNum;
|
2012-07-02 18:41:28 +02:00
|
|
|
}
|
2014-03-04 01:53:39 +01:00
|
|
|
// qDebug("\n -- Completed (%i bad regions) -- \n", badRegionVec.size());
|
2012-07-02 18:41:28 +02:00
|
|
|
VoroMesh *rm = new VoroMesh();
|
|
|
|
tri::VoronoiProcessing<VoroMesh>::FaceSelectAssociateRegion(m,0);
|
|
|
|
tri::Append<VoroMesh,VoroMesh>::Mesh(*rm, m, true);
|
|
|
|
|
|
|
|
if(rm->fn>0)
|
|
|
|
{
|
2014-03-04 01:53:39 +01:00
|
|
|
// qDebug("ACH - unreached faces %i fn\n",rm->fn);
|
2012-07-02 18:41:28 +02:00
|
|
|
badRegionVec.push_back(rm);
|
|
|
|
}
|
|
|
|
m.Clear();
|
2012-07-03 11:13:07 +02:00
|
|
|
pp.sampleNum = 10;
|
2012-07-02 18:41:28 +02:00
|
|
|
if(!badRegionVec.empty())
|
|
|
|
{
|
|
|
|
for(size_t i=0;i<badRegionVec.size();++i)
|
2013-04-03 12:07:29 +02:00
|
|
|
if(badRegionVec[i]->fn>50)
|
2012-07-02 18:41:28 +02:00
|
|
|
tri::Append<VoroMesh,VoroMesh>::Mesh(m, *badRegionVec[i], false);
|
|
|
|
|
|
|
|
tri::Clean<VoroMesh>::RemoveDuplicateFace(m);
|
|
|
|
tri::Clean<VoroMesh>::RemoveUnreferencedVertex(m);
|
|
|
|
tri::Allocator<VoroMesh>::CompactVertexVector(m);
|
|
|
|
tri::Allocator<VoroMesh>::CompactFaceVector(m);
|
|
|
|
}
|
|
|
|
} while (m.fn>0);
|
|
|
|
|
|
|
|
std::vector<Similarity2f> trVec;
|
|
|
|
Point2f finalSize;
|
2013-04-03 12:07:29 +02:00
|
|
|
//PolyPacker<float>::WritePolyVec(uvBorders,"borders.poly");
|
|
|
|
PolyPacker<float>::PackAsObjectOrientedRect(uvBorders,Point2i(1024,1024),trVec,finalSize);
|
2014-03-04 01:53:39 +01:00
|
|
|
// RasterizedOutline2Packer<float,QtOutline2Rasterizer>::Parameters prp;
|
|
|
|
// RasterizedOutline2Packer<float,QtOutline2Rasterizer>::Pack(uvBorders,Point2i(1024,1024),trVec,prp);
|
2012-07-02 18:41:28 +02:00
|
|
|
// loop again over all the patches
|
2012-07-03 12:21:41 +02:00
|
|
|
pp.vas.regionNum=meshRegionVec.size();
|
2012-07-02 18:41:28 +02:00
|
|
|
for(size_t i=0; i<meshRegionVec.size();++i)
|
|
|
|
{
|
|
|
|
VoroMesh *rm = meshRegionVec[i];
|
|
|
|
for(FaceIterator fi=rm->face.begin();fi!=rm->face.end();++fi)
|
|
|
|
{
|
|
|
|
for(int j=0;j<3;++j)
|
|
|
|
{
|
|
|
|
Point2f pp(fi->WT(j).U(),fi->WT(j).V());
|
|
|
|
Point2f newpp=trVec[i]*pp;
|
|
|
|
fi->WT(j).U()=newpp[0]/1024.0f;
|
|
|
|
fi->WT(j).V()=newpp[1]/1024.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tri::Append<MeshType,VoroMesh>::Mesh(paraMesh, *rm, false);
|
|
|
|
}
|
2012-07-03 11:13:07 +02:00
|
|
|
int t2=clock();
|
|
|
|
pp.vas.totalTime=t2-t0;
|
2012-07-02 18:41:28 +02:00
|
|
|
}
|
2012-07-03 11:13:07 +02:00
|
|
|
}; //end
|
2012-07-02 18:41:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
} // end namespace vcg
|
|
|
|
} // end namespace tri
|
|
|
|
|
|
|
|
|
|
|
|
#endif // VORONOI_ATLAS_H
|