c++ - 使用 assimp 加载网格,并检测其中的边界和非流形边

标签 c++ computational-geometry assimp

我正在尝试使用 assimp 加载网格以检测非流形和开放(边界)边缘。我正在使用从 assimp 中得到的顶点索引来建立顶点、边和面之间的关系。我有一个看起来像这样的脸类

#include "Face2.h"

Face2::Face2()
{
}

Face2::Face2(std::vector<unsigned int> indices)
{
    m_indices = indices;
}

bool Face2::containsEdge(const Edge2 &edge)
{
//    for(unsigned int i = 0; i < m_indices.size(); i++)
//    {
//        if(edge.getStartIndex() == m_indices[i]){
//            for(unsigned int i = 0; i < m_indices.size(); i++)
//            {
//                edge.getEndIndex() == m_indices[i]
//            }

//        }
//    }
    for(unsigned int i = 0; i < m_indices.size(); i++)
    {
        if(edge.getStartIndex() == m_indices[i])
        {
            for(unsigned int j = 0; j < m_indices.size(); j++)
            {
                if(edge.getEndIndex() == m_indices[j]) return true;
            }
        }
    }
    return false;
}

std::vector<unsigned int> Face2::getIndices() const
{
    return m_indices;
}

void Face2::setIndices(const std::vector<unsigned int> &indices)
{
    m_indices = indices;
}

我的边类基本上只是一个保存边的起始索引和结束索引的数据结构。 我的网格类包含一个 std::vector of faces 和 std::vector of edges,它们像这样填充:

for(unsigned int i = 0; i < m_indices.size()-1; i+=2)
    {
        m_edges2.push_back(Edge2(m_indices[i], m_indices[i]));
    }

    for(unsigned int i = 0; i < m_indices.size()-1; i+=3)
    {
        std::vector<unsigned int> faceData;
        faceData.push_back(m_indices[i]);
        faceData.push_back(m_indices[i+1]);
        faceData.push_back(m_indices[i+2]);
        m_faces2.push_back(Face2(faceData));
   }

我的主要功能有这段代码:

    std::cout << "Number of edges2: " << m->getEdges2().size() << std::endl;
    std::cout << "Number of faces2: " << m->getFaces2().size() << std::endl;
    std::cout << "finished " << std::endl;

    unsigned int nonManif = 0;
    unsigned int boundary = 0;

    for(unsigned int i = 0; i < m->getEdges2().size(); i++)
    {
        unsigned int edgeCount = 0;
        for(unsigned int j = 0; j < m->getFaces2().size(); j++)
        {
            if(m->getFaces2().at(j).containsEdge(m->getEdges2().at(i)))
            {
                edgeCount++;
            }
        }

        std::cout << "Edge#" << i << " occurences: " << edgeCount << std::endl;
        if(edgeCount > 2) nonManif++;
        else if(edgeCount < 2) boundary++;
    }

    std::cout << "Non-Manifold: " << nonManif << std::endl;
    std::cout << "Boundary: " << boundary << std::endl;

我从中得到了正确数量的边和面。 边界边的数量与我从 meshlab 获得的相同模型的数量有很大差异。并且非流形边的数量始终为 0。

他们是我做错了什么,还是他们有更好的方法?

最佳答案

我认为这里有一个错误:

m_edges2.push_back(Edge2(m_indices[i], m_indices[i]));

我想应该是:

m_edges2.push_back(Edge2(m_indices[i], m_indices[i + 1]));

解决这个问题可能会解决您的问题

在具有迭代上限的面创建周期中还有另一个错误,这可能会导致 UB。应该是:

i < m_indices.size()-2

关于c++ - 使用 assimp 加载网格,并检测其中的边界和非流形边,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35293877/

相关文章:

algorithm - 计算随机放在 table 上的卡片所覆盖的区域

iOS:在给定圆的情况下推导出轻敲点的角度

c++ - 使用 QT 的未定义引用 aiImportfile (assimp)

animation - 使用 Assimp 导入时确定骨骼长度

java - 为什么类对象只能在java中动态创建?

c++ - 使用 Z3 的 C++ api 创建长析取?

c++ - C++ 中直线与线段的交点

c++ - 使用多个着色器的最佳方式

c++ - 下面的代码应该为 4 和 0 的输入打印 120.000000。但它只显示 120。为什么?以及如何获得所需的输出?

c++ - 'sqrt' : ambiguous call to overloaded function . .\assimp\vector3.inl