vcg.tri.SurfaceSampling是一个用于表面采样的C ++库,可以提供表面指定数量的均匀分布采样点的机制。
从网格表面均匀采样一组点。
template <class InputMeshType, class VertexType>
void sampleUniform(const InputMeshType &m_in, std::vector<VertexType> &m_out, const size_t n);
从网格表面最远的点开始采样,按照距离大小依次加入到集合中,直到点数达到指定数量。
template <class InputMeshType, class VertexType>
void sampleFarthest(const InputMeshType &m_in, std::vector<VertexType> &m_out, const size_t n);
vcg.tri.SurfaceSampling提供两个采样方式,即均匀采样和最远距离采样,通过参数n来指定输出点数目。
#include <vcg/complex/complex.h>
#include <vcg/complex/algorithms/clean.h>
#include <vcg/complex/algorithms/create/platonic.h>
#include <vcg/complex/algorithms/update/topology.h>
#include <vcg/simplex/vertexplus/base.h>
#include <vcg/simplex/face/base.h>
#include <vcg/space/index/grid_static_ptr.h>
#include <vcg/trianglesampling/surface_sampling.h>
#include <iostream>
using namespace vcg;
using namespace std;
class MyVertex: public VertexSimp2< MyVertex, vertex::Coord3f, vertex::Normal3f, vertex::VFAdj >{};class MyFace: public FaceSimp2< MyFace, face::VertexRef, face::FFAdj, face::Normal3f >{};
class MyMesh: public vcg::tri::TriMesh<vector<MyVertex>, vector<MyFace> > {};
int main()
{
MyMesh m;
tri::Tetrahedron(m);
tri::UpdateTopology<MyMesh>::FaceFace(m);
tri::UpdateTopology<MyMesh>::VertexFace(m);
int nVertex1 = m.vn;
tri::Clean<MyMesh>::RemoveDuplicateVertex(m,0.01);
tri::UpdateTopology<MyMesh>::FaceFace(m);
tri::UpdateTopology<MyMesh>::VertexFace(m);
int nVertex2 = m.vn;
size_t num_samples = 30;
vector<MyVertex> samples;
vcg::tri::SurfaceSampling<MyMesh>::sampleUniform(m, samples, num_samples);
std::cout << "Num samples: " << samples.size() << std::endl;
return 0;
}
示例代码创建一个简单的四面体和平面三角形,对三角形执行uniform采样,并将数量写入控制台输出。