Algorithm 使用Assimp和OpenMesh进行网格简化

Algorithm 使用Assimp和OpenMesh进行网格简化,algorithm,computational-geometry,mesh,assimp,openmesh,Algorithm,Computational Geometry,Mesh,Assimp,Openmesh,几天前,我问了一个关于如何使用的问题。在软件中平滑obj和删除重复顶点解决了可能使边折叠工作的基本问题,我的意思是它工作,因为它可以通过MeshLab简化,如下所示: 在MeshLab中看起来不错,但我在我的引擎中使用了Assimp和OpenMesh。问题在于Assimp导入了指定的顶点和索引,这可能会让半边错过相反的一对(这称为非流形吗?)。 结果快照使用OpenMesh的二次抽取: 为了清楚地发现问题,我不进行抽取,直接解析OpenMesh数据结构。一切正常(我指的是没有抽取的结果)

几天前,我问了一个关于如何使用的问题。在软件中平滑obj和删除重复顶点解决了可能使边折叠工作的基本问题,我的意思是它工作,因为它可以通过MeshLab简化,如下所示:

在MeshLab中看起来不错,但我在我的引擎中使用了Assimp和OpenMesh。问题在于Assimp导入了指定的顶点和索引,这可能会让半边错过相反的一对(这称为非流形吗?)。 结果快照使用OpenMesh的二次抽取:

为了清楚地发现问题,我不进行抽取,直接解析OpenMesh数据结构。一切正常(我指的是没有抽取的结果)

我用来抽取网格的代码:

Loader::BasicData Loader::TestEdgeCollapse(float vertices[], int vertexLength, int indices[], int indexLength, float texCoords[], int texCoordLength, float normals[], int normalLength)
{
    // Mesh type
    typedef OpenMesh::TriMesh_ArrayKernelT<>   OPMesh;
    // Decimater type
    typedef OpenMesh::Decimater::DecimaterT< OPMesh > OPDecimater;
    // Decimation Module Handle type
    typedef OpenMesh::Decimater::ModQuadricT< OPMesh >::Handle HModQuadric;

    OPMesh mesh;
    std::vector<OPMesh::VertexHandle> vhandles;
    int iteration = 0;
    for (int i = 0; i < vertexLength; i += 3)
    {
        vhandles.push_back(mesh.add_vertex(OpenMesh::Vec3f(vertices[i], vertices[i + 1], vertices[i + 2])));
        if (texCoords != nullptr)
            mesh.set_texcoord2D(vhandles.back(),OpenMesh::Vec2f(texCoords[iteration * 2], texCoords[iteration * 2 + 1]));
        if (normals != nullptr)
            mesh.set_normal(vhandles.back(), OpenMesh::Vec3f(normals[i], normals[i + 1], normals[i + 2]));
        iteration++;
    }

    for (int i = 0; i < indexLength; i += 3)
        mesh.add_face(vhandles[indices[i]], vhandles[indices[i + 1]], vhandles[indices[i + 2]]);

    OPDecimater decimater(mesh);
    HModQuadric hModQuadric;
    decimater.add(hModQuadric);
    decimater.module(hModQuadric).unset_max_err();
    decimater.initialize();
    //decimater.decimate(); // without this, everything is fine as expect.
    mesh.garbage_collection();

    int verticesSize = mesh.n_vertices() * 3;
    float* newVertices = new float[verticesSize];
    int indicesSize = mesh.n_faces() * 3;
    int* newIndices = new int[indicesSize];
    float* newTexCoords = nullptr;
    int texCoordSize = mesh.n_vertices() * 2;
    if(mesh.has_vertex_texcoords2D())
        newTexCoords = new float[texCoordSize];
    float* newNormals = nullptr;
    int normalSize = mesh.n_vertices() * 3;
    if(mesh.has_vertex_normals())
        newNormals = new float[normalSize];

    Loader::BasicData data;

    int index = 0;
    for (v_it = mesh.vertices_begin(); v_it != mesh.vertices_end(); ++v_it)
    {
        OpenMesh::Vec3f &point = mesh.point(*v_it);
        newVertices[index * 3] = point[0];
        newVertices[index * 3 + 1] = point[1];
        newVertices[index * 3 + 2] = point[2];
        if (mesh.has_vertex_texcoords2D())
        {
            auto &tex = mesh.texcoord2D(*v_it);
            newTexCoords[index * 2] = tex[0];
            newTexCoords[index * 2 + 1] = tex[1];
        }
        if (mesh.has_vertex_normals())
        {
            auto &normal = mesh.normal(*v_it);
            newNormals[index * 3] = normal[0];
            newNormals[index * 3 + 1] = normal[1];
            newNormals[index * 3 + 2] = normal[2];
        }
        index++;
    }
    index = 0;

    for (f_it = mesh.faces_begin(); f_it != mesh.faces_end(); ++f_it)
        for (fv_it = mesh.fv_ccwiter(*f_it); fv_it.is_valid(); ++fv_it)
        {
            int id = fv_it->idx();
            newIndices[index] = id;
            index++;
        }

    data.Indices = newIndices;
    data.IndicesLength = indicesSize;
    data.Vertices = newVertices;
    data.VerticesLength = verticesSize;
    data.TexCoords = nullptr;
    data.TexCoordLength = -1;
    data.Normals = nullptr;
    data.NormalLength = -1;
    if (mesh.has_vertex_texcoords2D())
    {
        data.TexCoords = newTexCoords;
        data.TexCoordLength = texCoordSize;
    }
    if (mesh.has_vertex_normals())
    {
        data.Normals = newNormals;
        data.NormalLength = normalSize;
    }
    return data;
}
Loader::basicata Loader::TestEdgeCollapse(浮点顶点[],int-vertexLength,int-index[],int-indexLength,浮点texCoords[],int-texCoordLength,浮点法线[],int-normalLength)
{
//网格类型
typedef OpenMesh::TriMesh_ArrayKernelT OPMesh;
//分母型
typedef OpenMesh::Decimater::DecimaterOPDecimater;
//抽取模块句柄类型
typedef OpenMesh::Decimater::modquadract::Handle hmodquadrac;
OPMesh网;
std::向量vhandles;
int迭代=0;
对于(int i=0;iidx();
newindex[index]=id;
索引++;
}
数据。指数=新指数;
data.indicatesLength=指示大小;
data.Vertices=新顶点;
data.VerticesLength=verticesSize;
data.TexCoords=nullptr;
data.TexCoordLength=-1;
data.Normals=nullptr;
data.NormalLength=-1;
if(网格具有_顶点_texcoords2D())
{
data.TexCoords=newTexCoords;
data.TexCoordLength=texCoordSize;
}
if(网格具有_顶点_法线())
{
data.Normals=新法线;
data.NormalLength=normalSize;
}
返回数据;
}

还提供了我测试过的,以及由Assimp生成的,我从visual studio调试器中提取的,显示了一些索引找不到索引对的问题。

思考了几周后失败了,我想我需要一些学术/数学解决方案来自动生成这些抽取的网格,但现在我正试图找到一种简单的方法来实现这一点,我能做的就是改变在单个自定义对象(类obj)中加载多对象(file.obj)的结构,并在需要时切换对象。这样做的好处是我可以管理应该呈现的内容,而忽略任何算法问题

顺便说一下,我列出了一些阻碍我回到简单方式的障碍

  • Assimp唯一的索引和顶点,这是没有错的,但对于算法来说,没有办法使邻接半边结构适合这个
  • OpenMesh用于只读对象文件(*.obj),这可以在使用
    read_mesh
    函数时完成,但缺点是缺少文档示例,很难在我的引擎中使用
  • 为任何格式编写自定义三维模型导入器都很困难
  • 综上所述,有两种方法可以使细节层次在引擎中工作,一种是使用网格简化算法并进行更多的测试以确保质量,另一种是仅切换由3d软件制作的3d模型,它不是自动的,而是稳定的。我使用第二种方法,并在这里显示结果:)

    然而,这并不是我的问题的真正解决方案