2016-12-04 257 views
1

幾天前,我問如何使用edge collapse with Assimp。平滑的OBJ和刪除重複的頂點軟件sloved的基本問題,可以使崩邊的工作,我的意思是工作,因爲它可以通過MeshLab這樣被simplicated: simplified mesh使用Assimp和OpenMesh簡化網格

它看起來MeshLab不錯,但我則在使用Assimp和OpenMesh的引擎中執行此操作。問題是Assimp導入了指定的頂點和指數,這可能讓半邊框錯過了相反的對(這是否稱爲非流形?)。 結果快照使用OpenMesh的二次抽取:

Decimation by OpenMesh

要清除,以發現問題,我做沒有抽取和背部直接解析OpenMesh數據結構。一切工作都很好(我的意思是沒有抽取的結果)。

Without Decimation

,我用來抽取網格上的代碼:

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; 
} 

另外提供the tree obj我測試,並且the face data通過Assimp產生的,我從Visual Studio調試器取出,示出了問題是一些索引找不到索引對。

+0

你是否證實這不是因爲你沒有畫出「背面」?即缺失面部的法線指向樹內部。 –

回答

0

幾周想着這和失敗,我想我需要一些學術/數學解自動生成這些銳減目,但現在我試圖找到簡單的方法實現這一點,我的方式我能夠做的是改變用於在單個自定義對象(類obj)中加載多對象(file.obj)的結構,並且在需要時切換對象。這樣做的好處是我可以管理應該呈現的內容並忽略任何算法問題。順便說一下,我列出了一些讓我回到簡單方式的障礙。

  1. Assimp唯一索引和頂點,這沒什麼錯,但對於算法,沒有辦法使這個鄰接半邊結構。
  2. OpenMesh僅用於讀取目標文件(* .obj),這可以在使用read_mesh函數時完成,但缺點是缺少文檔示例,難以在我的引擎中使用。
  3. 爲任何格式編寫自定義3d模型導入程序都很困難。

總之,有兩種方法可以使細節級別發動機的工作,一個是使用網格簡化算法和更多的測試,以確保質量,另一種是隻需切換,通過3D軟件製作的3dmodel,它不是自動的,而是穩定的。我用的是第二種方法,我在這裏顯示的結果:)

this work!

然而,這不是我的問題真正的解決辦法,所以我不會指派我一個答案。