first version of Dual meshing with sample application

This commit is contained in:
nico 2017-01-23 17:22:44 +01:00
parent 84913a7d54
commit b482aa3cbe
3 changed files with 335 additions and 0 deletions

View File

@ -0,0 +1,121 @@
/****************************************************************************
* 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. *
* *
****************************************************************************/
#include <vcg/complex/complex.h>
/*include the algorithms for updating: */
#include <vcg/complex/algorithms/update/topology.h>
#include <vcg/complex/algorithms/update/bounding.h>
#include <vcg/complex/algorithms/update/normal.h>
#include <vcg/complex/algorithms/clean.h>
#include <vcg/complex/algorithms/create/platonic.h>
#include <wrap/io_trimesh/export_obj.h>
#include <wrap/io_trimesh/import_obj.h>
#include <vcg/complex/algorithms/dual_meshing.h>
#include <vcg/complex/algorithms/polygon_support.h>
using namespace vcg;
using namespace std;
// forward declarations
class TFace;
class TVertex;
struct TUsedTypes: public vcg::UsedTypes< vcg::Use<TVertex>::AsVertexType, vcg::Use<TFace>::AsFaceType >{};
/* Definition of a mesh of triangles
*/
class TVertex : public Vertex< TUsedTypes,
vertex::BitFlags,
vertex::Coord3f,
vertex::Normal3f,
vertex::Mark >{};
class TFace : public Face< TUsedTypes,
face::VertexRef, // three pointers to vertices
face::Normal3f, // normal
face::BitFlags, // flags
face::FFAdj // three pointers to adjacent faces
> {};
/* the mesh is a container of vertices and a container of faces */
class TMesh : public vcg::tri::TriMesh< vector<TVertex>, vector<TFace> > {};
/* Definition of a mesh of polygons that also supports half-edges
*/
class PFace;
class PVertex;
struct PUsedTypes: public vcg::UsedTypes<vcg::Use<PVertex> ::AsVertexType,
vcg::Use<PFace> ::AsFaceType>{};
class PVertex:public vcg::Vertex< PUsedTypes,
vcg::vertex::Coord3f,
vcg::vertex::Normal3f,
vcg::vertex::Mark,
vcg::vertex::BitFlags>{} ;
class PFace:public vcg::Face<
PUsedTypes
,vcg::face::PolyInfo // this is necessary if you use component in vcg/simplex/face/component_polygon.h
// It says "this class is a polygon and the memory for its components (e.g. pointer to its vertices
// will be allocated dynamically")
,vcg::face::PFVAdj // Pointer to the vertices (just like FVAdj )
,vcg::face::PFVAdj
,vcg::face::PFFAdj // Pointer to edge-adjacent face (just like FFAdj )
,vcg::face::BitFlags // bit flags
,vcg::face::Normal3f // normal
> {};
class PMesh: public
vcg::tri::TriMesh<
std::vector<PVertex>, // the vector of vertices
std::vector<PFace > // the vector of faces
>{};
TMesh primalT;
PMesh primal,dual;
int main(int argc, char *argv[])
{
(void)argc;
(void)argv;
vcg::tri::Sphere<TMesh>(primalT,2);
vcg::tri::PolygonSupport<TMesh,PMesh>::ImportFromTriMesh(primal,primalT);
vcg::tri::DualMeshing<PMesh>::MakeDual(primal,dual);
vcg::tri::io::ExporterOBJ<PMesh>::Save(dual,"./dual.obj",vcg::tri::io::Mask::IOM_BITPOLYGONAL);
vcg::tri::DualMeshing<PMesh>::MakeDual(dual,primal);
vcg::tri::io::ExporterOBJ<PMesh>::Save(primal,"./dual_dual.obj",vcg::tri::io::Mask::IOM_BITPOLYGONAL);
}

View File

@ -0,0 +1,7 @@
\macx: QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.9
QMAKE_MAC_SDK = macosx10.9
CONFIG += c++11
include(../common.pri)
TARGET = polygonmesh_base
SOURCES += polygonmesh_dual.cpp ../../../wrap/ply/plylib.cpp

View File

@ -0,0 +1,207 @@
/****************************************************************************
* 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. *
* *
****************************************************************************/
#ifndef __VCGLIB_DUAL_MESH
#define __VCGLIB_DUAL_MESH
#include <vcg/complex/complex.h>
#include <vcg/complex/algorithms/update/topology.h>
#include <vcg/simplex/face/pos.h>
#include <vcg/complex/algorithms/clean.h>
namespace vcg {
namespace tri {
template <class PolyMeshType>
class DualMeshing
{
typedef typename PolyMeshType::VertexType VertexType;
typedef typename PolyMeshType::FaceType FaceType;
typedef typename PolyMeshType::CoordType CoordType;
typedef typename PolyMeshType::ScalarType ScalarType;
static void ComposeFace(VertexType &startV,
FaceType &startF,
std::map<std::pair<int,int>, int> &EdgeMap,
const std::vector<int> &FaceMap,
const PolyMeshType &primal,
std::vector<int> &vertSeq)
{
vcg::face::Pos<FaceType> startP(&startF,&startV);
//get the star of pos
std::vector<vcg::face::Pos<FaceType> > posVec;
vcg::face::VFOrderedStarFF(startP,posVec);
for (size_t i=0;i<posVec.size();i++)
{
FaceType *f=posVec[i].F();
int indexF=vcg::tri::Index(primal,f);
int indexV=FaceMap[indexF];
vertSeq.push_back(indexV);
}
if (startV.IsB())
{
vcg::face::Pos<FaceType> firstPos=posVec[0];
firstPos.FlipE();
assert(firstPos.IsBorder());
int indexVt0=vcg::tri::Index(primal,firstPos.V());
int indexVt1=vcg::tri::Index(primal,firstPos.VFlip());
std::pair<int,int> key(std::min(indexVt0,indexVt1),
std::max(indexVt0,indexVt1));
assert(EdgeMap.count(key)>0);
int indexV0=EdgeMap[key];
vcg::face::Pos<FaceType> lastPos=posVec.back();
assert(lastPos.IsBorder());
indexVt0=vcg::tri::Index(primal,lastPos.V());
indexVt1=vcg::tri::Index(primal,lastPos.VFlip());
key=std::pair<int,int> (std::min(indexVt0,indexVt1),
std::max(indexVt0,indexVt1));
assert(EdgeMap.count(key)>0);
int indexV1=EdgeMap[key];
vertSeq.push_back(indexV1);
vertSeq.push_back(indexV0);
}
}
static void CreateBorderEdgeVert(PolyMeshType &primal,
PolyMeshType &dual,
std::map<std::pair<int,int>, int> &VertMap)
{
VertMap.clear();
vcg::tri::UpdateFlags<PolyMeshType>::VertexClearB(primal);
for (size_t i=0;i<primal.face.size();i++)
for (int j=0;j<primal.face[i].VN();j++)
{
int edge_size=primal.face[i].VN();
FaceType *nextF=primal.face[i].cFFp(j);
if (nextF!=&primal.face[i])continue;
VertexType *v0=primal.face[i].V(j);
VertexType *v1=primal.face[i].V((j+1)%edge_size);
v0->SetB();
v1->SetB();
int V0Index=vcg::tri::Index(primal,v0);
int V1Index=vcg::tri::Index(primal,v1);
CoordType pos=(v0->P()+v1->P())/2;
vcg::tri::Allocator<PolyMeshType>::AddVertex(dual,pos);
std::pair<int,int> key(std::min(V0Index,V1Index),
std::max(V0Index,V1Index));
VertMap[key]=dual.vert.size()-1;
}
}
static void CreateFaceVert(PolyMeshType &primal,
PolyMeshType &dual,
std::vector<int> &VertMap,
std::vector<int> &VertFace,
bool snapBorder)
{
VertMap.clear();
VertMap.resize(primal.face.size(),-1);
VertFace.clear();
VertFace.resize(primal.vert.size(),-1);
for (size_t i=0;i<primal.face.size();i++)
{
CoordType pos(0,0,0);
int num=0;
if (snapBorder)//search for border edge
{
std::vector<CoordType> BorderPos;
for (int j=0;j<primal.face[i].VN();j++)
{
if (!primal.face[i].V(j)->IsB())continue;
pos+=primal.face[i].P(j);
num++;
}
if (num>0)
pos/=num;
}
if (num==0)
{
for (int j=0;j<primal.face[i].VN();j++)
{
pos+=primal.face[i].V(j)->P();
int indexV=vcg::tri::Index(primal,primal.face[i].V(j));
if (VertFace[indexV]!=-1)continue;
VertFace[indexV]=i;
}
pos/=(ScalarType)primal.face[i].VN();
}
vcg::tri::Allocator<PolyMeshType>::AddVertex(dual,pos);
VertMap[i]=dual.vert.size()-1;
}
}
public:
static void MakeDual(PolyMeshType &primal,
PolyMeshType &dual,
bool snapBorder=true)
{
dual.Clear();
vcg::tri::RequirePolygonalMesh(primal);
vcg::tri::RequirePolygonalMesh(dual);
vcg::tri::RequireFFAdjacency(primal);
vcg::tri::UpdateTopology<PolyMeshType>::FaceFace(primal);
std::map<std::pair<int,int>, int> VertEdgeMap;
CreateBorderEdgeVert(primal,dual,VertEdgeMap);
std::vector<int> VertFaceMap,VertFace;
CreateFaceVert(primal,dual,VertFaceMap,VertFace,snapBorder);
for (size_t i=0;i<primal.vert.size();i++)
{
if ((snapBorder)&&(primal.vert[i].IsB()))continue;
FaceType *firstF=&primal.face[VertFace[i]];
std::vector<int> VertSeq;
ComposeFace(primal.vert[i],*firstF,VertEdgeMap,VertFaceMap,primal,VertSeq);
vcg::tri::Allocator<PolyMeshType>::AddFaces(dual,1);
dual.face.back().Alloc(VertSeq.size());
for (size_t j=0;j<VertSeq.size();j++)
dual.face.back().V(j)=&dual.vert[VertSeq[j]];
}
vcg::tri::Clean<PolyMeshType>::RemoveUnreferencedVertex(dual);
}
};
}
}
#endif // __VCGLIB_DUAL_MESH