Carve CSG c ++出现问题。结果中包含错误的顶点

时间:2018-06-21 22:15:28

标签: c++ geometry csg carve

你好,我对Carve有问题,但不确定是实施还是使用它。

这是孤立的代码,它显示我遇到的问题。本质上,我得到了错误的结果,或更具体地说,我得到的结果vertex_storage中的顶点不正确。该顶点也包含在面部索引中,因此似乎具有雕刻效果。我已经手动调试并绘制了所有面,因此确定不应该包含此顶点。

包含的不应突出显示的顶点。

enter image description here

生成的网格显然是错误的。更详细地检查它,vertex_storage中包含一个错误点,即(150,150,0)。

由此产生的结果三角形包括错误的顶点o.0

这是一个非常基本的差异操作,我已经阅读到雕刻有一些问题,但是我不会想到它会在2个基本网格物体(2个长方体)的情况下表现出来,这使我认为这是我的职责做错了。

这也是令人沮丧的,因为在调试(网格集0的)面6时(我假设已播种/ prng)我得到了相同的顶点,但几乎总是有不同的顺序。

这里是演示问题的来源...

#include <carve\mesh.hpp>
#include <carve\csg.hpp>
#include <carve\input.hpp>

#include <Windows.h>

struct MyVector3
{
    MyVector3(float x, float y, float z)
        : x_(x), y_(y), z_(z)
    {
    }

    float x_;
    float y_;
    float z_;
};

struct MyVertex
{
    MyVertex(unsigned int pointIndex)
        : p_(pointIndex)
    {
    }

    unsigned int p_;
    // n_, uv_ etc left out for covienience
};


struct MyTriangle
{
    MyTriangle(unsigned int a, unsigned int b, unsigned int c)
        : a_(a), b_(b), c_(c)
    {
    }

    unsigned int a_;
    unsigned int b_;
    unsigned int c_;
};

struct MyMesh
{
    std::vector<MyVector3> points_;
    std::vector<MyTriangle> triangles_;
    std::vector<MyVertex> vertices_;
};


carve::mesh::MeshSet<3U>* GetCarveMesh(const MyMesh& originalMesh)
{
    carve::input::PolyhedronData data;

    for (unsigned int v = 0; v < originalMesh.vertices_.size(); ++v)
    {
        const MyVector3& point = originalMesh.points_[originalMesh.vertices_[v].p_];
        data.addVertex(carve::geom::VECTOR(point.x_, point.y_, point.z_));
    }

    for (unsigned int t = 0; t < originalMesh.triangles_.size(); t++)
    {
        const MyTriangle& tri = originalMesh.triangles_[t];
        const MyVertex& a = originalMesh.vertices_[tri.a_];
        const MyVertex& b = originalMesh.vertices_[tri.b_];
        const MyVertex& c = originalMesh.vertices_[tri.c_];

        data.addFace(a.p_, b.p_, c.p_);
    }

    return data.createMesh(carve::input::Options());
}

std::list<std::shared_ptr<MyMesh>> GetMyMesh(carve::mesh::MeshSet<3U>* carveMesh)
{
    std::list<std::shared_ptr<MyMesh>> result;

    unsigned int meshStart = 0;
    unsigned int meshEnd = carveMesh->meshes.size();

    for (unsigned int ms = meshStart; ms < meshEnd; ++ms)
    {
        // For each mesh, construct the faces and add the points as we do.
        result.push_back(std::shared_ptr<MyMesh>());

        {
            std::ostringstream oss;
            oss << "mesh " << ms << " has " << carveMesh->meshes[ms]->faces.size() << " faces.\n";

            OutputDebugString(oss.str().c_str());
        }

        unsigned int faceStart = 0;
        unsigned int faceEnd = carveMesh->meshes[ms]->faces.size();

        for (unsigned int f = faceStart; f < faceEnd; ++f)
        {
            //if (f == 10 || f == 11 || f == 6)
            //    continue;

            unsigned int vertexCount = carveMesh->meshes[ms]->faces[f]->n_edges;
            std::vector<MyVector3> points;
            points.reserve(vertexCount);

            std::ostringstream oss;
            oss << " - face " << f << "(" << vertexCount << ") :\n";


            // add each edge in this face... more than 3, then we need to tessellate
            carve::mesh::Edge<3>* currentEdge = carveMesh->meshes[ms]->faces[f]->edge;
            for (unsigned int e = 0; e < vertexCount; ++e)
            {
                MyVector3 point(static_cast<float>(currentEdge->vert->v[0]), static_cast<float>(currentEdge->vert->v[1]), static_cast<float>(currentEdge->vert->v[2]));
                points.push_back(point);

                oss << "[" << e << ":" << point.x_ << ", " << point.y_ << ", " << point.z_ << "]";
                oss << "\n";

                currentEdge = currentEdge->next;
            }

            OutputDebugString(oss.str().c_str());

            //  ... Other operations, tessellation etc

        }
    }
    return result;
}


int main(int argc, char** argv)
{

    // define myMeshA
    MyMesh myMeshA;
    myMeshA.points_.push_back(MyVector3(-150.0f, -150.0f, 0));
    myMeshA.points_.push_back(MyVector3(-150.0f, -150.0f, 2800.0f));
    myMeshA.points_.push_back(MyVector3(150.0f, 150.0f, 0));
    myMeshA.points_.push_back(MyVector3(150.0f, 150.0f, 2800.0f));
    myMeshA.points_.push_back(MyVector3(3430.0f, -150.0f, 0));
    myMeshA.points_.push_back(MyVector3(3430.0f, -150.0f, 2800.0f));
    myMeshA.points_.push_back(MyVector3(3730.0f, 150.0f, 0));
    myMeshA.points_.push_back(MyVector3(3730.0f, 150.0f, 2800.0f));

    myMeshA.vertices_.push_back(MyVertex(0));
    myMeshA.vertices_.push_back(MyVertex(1));
    myMeshA.vertices_.push_back(MyVertex(2));
    myMeshA.vertices_.push_back(MyVertex(3));
    myMeshA.vertices_.push_back(MyVertex(4));
    myMeshA.vertices_.push_back(MyVertex(5));
    myMeshA.vertices_.push_back(MyVertex(6));
    myMeshA.vertices_.push_back(MyVertex(7));

    myMeshA.triangles_.push_back(MyTriangle(0, 1, 3));
    myMeshA.triangles_.push_back(MyTriangle(3, 2, 0));
    myMeshA.triangles_.push_back(MyTriangle(2, 3, 7));
    myMeshA.triangles_.push_back(MyTriangle(7, 6, 2));
    myMeshA.triangles_.push_back(MyTriangle(6, 7, 5));
    myMeshA.triangles_.push_back(MyTriangle(5, 4, 6));
    myMeshA.triangles_.push_back(MyTriangle(4, 5, 1));
    myMeshA.triangles_.push_back(MyTriangle(1, 0, 4));
    myMeshA.triangles_.push_back(MyTriangle(2, 4, 0));
    myMeshA.triangles_.push_back(MyTriangle(4, 2, 6));
    myMeshA.triangles_.push_back(MyTriangle(3, 5, 1));
    myMeshA.triangles_.push_back(MyTriangle(5, 3, 7));

    // define myMeshB
    MyMesh myMeshB;
    myMeshB.points_.push_back(MyVector3(0, 0, 0));
    myMeshB.points_.push_back(MyVector3(0, 0, 2100.0f));
    myMeshB.points_.push_back(MyVector3(0, 300.0f, 0));
    myMeshB.points_.push_back(MyVector3(0, 300.0f, 2100.0f));
    myMeshB.points_.push_back(MyVector3(930.0f, 0, 0));
    myMeshB.points_.push_back(MyVector3(930.0f, 0, 2100.0f));
    myMeshB.points_.push_back(MyVector3(930.0f, 300.0f, 0));
    myMeshB.points_.push_back(MyVector3(930.0f, 300.0f, 2100.0f));

    myMeshB.vertices_.push_back(MyVertex(0));
    myMeshB.vertices_.push_back(MyVertex(1));
    myMeshB.vertices_.push_back(MyVertex(2));
    myMeshB.vertices_.push_back(MyVertex(3));
    myMeshB.vertices_.push_back(MyVertex(4));
    myMeshB.vertices_.push_back(MyVertex(5));
    myMeshB.vertices_.push_back(MyVertex(6));
    myMeshB.vertices_.push_back(MyVertex(7));

    myMeshB.triangles_.push_back(MyTriangle(0, 1, 3));
    myMeshB.triangles_.push_back(MyTriangle(3, 2, 0));
    myMeshB.triangles_.push_back(MyTriangle(2, 3, 7));
    myMeshB.triangles_.push_back(MyTriangle(7, 6, 2));
    myMeshB.triangles_.push_back(MyTriangle(6, 7, 5));
    myMeshB.triangles_.push_back(MyTriangle(5, 4, 6));
    myMeshB.triangles_.push_back(MyTriangle(4, 5, 1));
    myMeshB.triangles_.push_back(MyTriangle(1, 0, 4));
    myMeshB.triangles_.push_back(MyTriangle(2, 4, 0));
    myMeshB.triangles_.push_back(MyTriangle(4, 2, 6));
    myMeshB.triangles_.push_back(MyTriangle(3, 5, 1));
    myMeshB.triangles_.push_back(MyTriangle(5, 3, 7));


    std::unique_ptr<carve::mesh::MeshSet<3U>> meshA(GetCarveMesh(myMeshA));
    std::unique_ptr<carve::mesh::MeshSet<3U>> meshB(GetCarveMesh(myMeshB));

    carve::csg::CSG csg;
    std::unique_ptr<carve::mesh::MeshSet<3U>> carveResult(csg.compute(meshA.get(), meshB.get(), carve::csg::CSG::OP::A_MINUS_B));
    std::list<std::shared_ptr<MyMesh>> result = GetMyMesh(carveResult.get());

    return 0;
}

相关输出

00000020    8.55740356  [12284] mesh 0 has 14 faces.    
00000021    8.55764484  [12284]  - face 0(6) :  
00000022    8.55764484  [12284] [0:-150, -150, 0]   
00000023    8.55764484  [12284] [1:-150, -150, 2800]    
00000024    8.55764484  [12284] [2:150, 150, 2800]  
00000025    8.55764484  [12284] [3:75, 75, 2100]    
00000026    8.55764484  [12284] [4:0, 0, 2100]  
00000027    8.55764484  [12284] [5:0, 0, 1400]  
00000028    8.55826759  [12284]  - face 1(3) :  
00000029    8.55826759  [12284] [0:0, 0, 0] 
00000030    8.55826759  [12284] [1:-150, -150, 0]   
00000031    8.55826759  [12284] [2:0, 0, 1400]  
00000032    8.55878353  [12284]  - face 2(3) :  
00000033    8.55878353  [12284] [0:-150, -150, 2800]    
00000034    8.55878353  [12284] [1:-150, -150, 0]   
00000035    8.55878353  [12284] [2:3430, -150, 0]   
00000036    8.55926609  [12284]  - face 3(3) :  
00000037    8.55926609  [12284] [0:150, 150, 2800]  
00000038    8.55926609  [12284] [1:150, 150, 2100]  
00000039    8.55926609  [12284] [2:75, 75, 2100]    
00000040    8.55982780  [12284]  - face 4(5) :  
00000041    8.55982780  [12284] [0:930, 78.6585, 0] 
00000042    8.55982780  [12284] [1:3430, -150, 0]   
00000043    8.55982780  [12284] [2:-150, -150, 0]   
00000044    8.55982780  [12284] [3:0, 0, 0] 
00000045    8.55982780  [12284] [4:930, 0, 0]   
00000046    8.56040192  [12284]  - face 5(3) :  
00000047    8.56040192  [12284] [0:3430, -150, 0]   
00000048    8.56040192  [12284] [1:3430, -150, 2800]    
00000049    8.56040192  [12284] [2:-150, -150, 2800]    
00000050    8.56112003  [12284]  - face 6(13) : 
00000051    8.56112003  [12284] [0:930, 150, 2100]  
00000052    8.56112003  [12284] [1:465, 150, 2100]  
00000053    8.56112003  [12284] [2:150, 150, 2100]  
00000054    8.56112003  [12284] [3:150, 150, 2800]  
00000055    8.56112003  [12284] [4:3730, 150, 2800] 
00000056    8.56112003  [12284] [5:930, 150, 610.056]   
00000057    8.56112003  [12284] [6:930, 150, 1050]  
00000058    8.56112003  [12284] [7:930, 150, 2100]  
00000059    8.56112003  [12284] [8:930, 150, 1050]  
00000060    8.56112003  [12284] [9:930, 150, 610.056]   
00000061    8.56112003  [12284] [10:150, 150, 0]    
00000062    8.56112003  [12284] [11:150, 150, 2100] 
00000063    8.56112003  [12284] [12:465, 150, 2100] 
00000064    8.56189060  [12284]  - face 7(4) :  
00000065    8.56189060  [12284] [0:930, 150, 0] 
00000066    8.56189060  [12284] [1:3730, 150, 0]    
00000067    8.56189060  [12284] [2:3430, -150, 0]   
00000068    8.56189060  [12284] [3:930, 78.6585, 0] 
00000069    8.56240559  [12284]  - face 8(3) :  
00000070    8.56240559  [12284] [0:3430, -150, 2800]    
00000071    8.56240559  [12284] [1:3430, -150, 0]   
00000072    8.56240559  [12284] [2:3730, 150, 0]    
00000073    8.56300354  [12284]  - face 9(4) :  
00000074    8.56300354  [12284] [0:930, 150, 610.056]   
00000075    8.56300354  [12284] [1:3730, 150, 2800] 
00000076    8.56300354  [12284] [2:3730, 150, 0]    
00000077    8.56300354  [12284] [3:930, 150, 0] 
00000078    8.56469250  [12284]  - face 10(4) : 
00000079    8.56469250  [12284] [0:930, 150, 0] 
00000080    8.56469250  [12284] [1:465, 150, 0] 
00000081    8.56469250  [12284] [2:150, 150, 0] 
00000082    8.56469250  [12284] [3:930, 150, 610.056]   
00000083    8.56473637  [12284]  - face 11(5) : 
00000084    8.56473637  [12284] [0:150, 150, 2100]  
00000085    8.56473637  [12284] [1:150, 150, 0] 
00000086    8.56473637  [12284] [2:0, 0, 0] 
00000087    8.56473637  [12284] [3:0, 0, 1400]  
00000088    8.56473637  [12284] [4:75, 75, 2100]    
00000089    8.56530762  [12284]  - face 12(3) : 
00000090    8.56530762  [12284] [0:3730, 150, 0]    
00000091    8.56530762  [12284] [1:3730, 150, 2800] 
00000092    8.56530762  [12284] [2:3430, -150, 2800]    
00000093    8.56581879  [12284]  - face 13(3) : 
00000094    8.56581879  [12284] [0:75, 75, 2100]    
00000095    8.56581879  [12284] [1:0, 0, 1400]  
00000096    8.56581879  [12284] [2:0, 0, 2100]  
00000097    8.56697178  [12284] mesh 1 has 2 faces. 
00000098    8.56711102  [12284]  - face 0(3) :  
00000099    8.56711102  [12284] [0:150, 150, 2800]  
00000100    8.56711102  [12284] [1:3430, -150, 2800]    
00000101    8.56711102  [12284] [2:-150, -150, 2800]    
00000102    8.56761074  [12284]  - face 1(3) :  
00000103    8.56761074  [12284] [0:3430, -150, 2800]    
00000104    8.56761074  [12284] [1:150, 150, 2800]  
00000105    8.56761074  [12284] [2:3730, 150, 2800]

在面顶点的关联输出中。不应包含顶点(150,150,0),但正如您所见,它包含在3个面中...(面6、10和11)。这会导致不正确的细分。

这是怎么回事?是我使用雕刻的方式,还是雕刻本身存在问题。任何帮助将不胜感激:)

0 个答案:

没有答案