C ++向量在递归函数中丢失数据

时间:2012-02-17 15:55:34

标签: c++ stl recursion vector

我是C ++的新手,我正在尝试实现一个TriangleDynamic对象,它可以使用一个名为splitTriangleProject的函数递归地分割自身。它将自身分成四个较小的TriangleDynamic对象(并将新三角形的顶点投影到具有给定半径和原点的球体上,但我相信这是在该点旁边),将新创建的三角形推入一个向量中,该向量是原始对象的成员数据。向量成员数据称为subTriangles。然后每个subTriangle都会调用splitTriangleProject函数,直到发生某个“分割”级别。

我遇到的问题是只有第一级分裂实际上正在推送到子三角形矢量。我确信这个问题与超出范围的向量有关,或者TriangleDynamic可能超出范围。我想有一些指针解决方案。如果有人可以提供帮助,我们将不胜感激。

这是我的TriangleDynamic声明:

class TriangleDynamic
{
public:
    TriangleDynamic(const Point &P1, const Point &P2, const Point &P3);
    TriangleDynamic();
    ~TriangleDynamic(){}
    void printTriangle();
    void splitTriangleProject( int currentLevel, int maxLevel, const Point &org, double radius);
    void init();
    bool operator<(const TriangleDynamic&);
    bool operator>(const TriangleDynamic&);
    Line edge1;
    Line edge2;
    Line edge3;

    Point p1;
    Point p2;
    Point p3;

    Vect normal;

    bool lowestLevel;

    vector<TriangleDynamic> subTriangles;
    static int numTriangles;
    int triangleId;
};

int TriangleDynamic::numTriangles = 0;

和构造函数:

// constructor for the TriangleDynamic object
TriangleDynamic::TriangleDynamic(const Point &P1, const Point &P2, const Point &P3)
{
    p1 = P1;
    p2 = P2;
    p3 = P3;
    init();
}

TriangleDynamic::TriangleDynamic()
{
    p1 = Point(0,0,0);
    p2 = Point(0,0,1);
    p3 = Point(0,1,0);
    init();
}

void TriangleDynamic::init()
{
    edge1 = Line(p1,p2);
    edge2 = Line(p2,p3);
    edge3 = Line(p3,p1);

    Vect U = p2.minus( p1);
    Vect V = p3.minus(p1);

    normal = U.cross(V);

    lowestLevel = true;
    triangleId = numTriangles + 1;
    numTriangles = triangleId;
}

这是我的splitTriangleProject函数:

void TriangleDynamic::splitTriangleProject(int currentLevel, int maxLevel, const Point &org, double radius)
{
    if ( currentLevel < maxLevel)
    {
        lowestLevel = false;
        Point worldOrigin = Point(0,0,0);
        double edge1MidMag = (edge1.midpoint - org).distance(worldOrigin) ;
        double edge2MidMag = (edge2.midpoint - org).distance(worldOrigin) ;
        double edge3MidMag = (edge3.midpoint - org).distance(worldOrigin) ;

        Point newEdge1Mid = (((edge1.midpoint) * radius )/ edge1MidMag) + org;
        Point newEdge2Mid = (((edge2.midpoint) * radius )/ edge2MidMag) + org;
        Point newEdge3Mid = (((edge3.midpoint) * radius )/ edge3MidMag) + org;

        TriangleDynamic t1(p1 , newEdge1Mid , newEdge3Mid);
        subTriangles.push_back(t1);

        TriangleDynamic t2(newEdge1Mid, p2, newEdge2Mid);
        subTriangles.push_back(t2);

        TriangleDynamic t3(newEdge3Mid, newEdge2Mid, p3);
        subTriangles.push_back(t3);

        TriangleDynamic t4(newEdge1Mid, newEdge2Mid, newEdge3Mid);
        subTriangles.push_back(t4);

        t1.splitTriangleProject( currentLevel + 1, maxLevel, org, radius);
        t2.splitTriangleProject( currentLevel + 1, maxLevel, org, radius);
        t3.splitTriangleProject( currentLevel + 1, maxLevel, org, radius); 
        t4.splitTriangleProject( currentLevel + 1, maxLevel, org, radius);
    }
}

这是一个在完成时递归打印triangleDynamic的函数:

void TriangleDynamic::printTriangle()
{
    cout<< "p1";
    p1.printPoint();
    cout << "\np2";
    p2.printPoint();
    cout << "\np3";
    p3.printPoint();
    cout << "\n\n";

    if(!lowestLevel)
    {
        int ctr;
        for (ctr=0; ctr<=subTriangles.size()-1; ctr++)
        {
            cout << "subTriangle\n";
            subTriangles[ctr].printTriangle();
        }
    }
}

以下是我在主要内容中的称呼方式:

int main()
{
    TriangleDynamic t = TriangleDynamic();
    t.splitTriangleProject(0,3,Point(), 1);
    t.printTriangle();
    cin.get();
    return 0;
}

我想我此时发布的内容过多。感谢任何帮助,提前谢谢。

3 个答案:

答案 0 :(得分:1)

问题就在这里

    TriangleDynamic t1(p1 , newEdge1Mid , newEdge3Mid);
    subTriangles.push_back(t1);

    TriangleDynamic t2(newEdge1Mid, p2, newEdge2Mid);
    subTriangles.push_back(t2);

    TriangleDynamic t3(newEdge3Mid, newEdge2Mid, p3);
    subTriangles.push_back(t3);

    TriangleDynamic t4(newEdge1Mid, newEdge2Mid, newEdge3Mid);
    subTriangles.push_back(t4);

    t1.splitTriangleProject( currentLevel + 1, maxLevel, org, radius);
    t2.splitTriangleProject( currentLevel + 1, maxLevel, org, radius);
    t3.splitTriangleProject( currentLevel + 1, maxLevel, org, radius); 
    t4.splitTriangleProject( currentLevel + 1, maxLevel, org, radius);

注意在调用splitTrangleProject之前如何将三角形的副本推入subTriangles向量。

因此,向量中的三角形没有调用splitTriangleProject。

将回退移动到代码的末尾,它应该可以工作。

    TriangleDynamic t1(p1 , newEdge1Mid , newEdge3Mid);


    TriangleDynamic t2(newEdge1Mid, p2, newEdge2Mid);


    TriangleDynamic t3(newEdge3Mid, newEdge2Mid, p3);


    TriangleDynamic t4(newEdge1Mid, newEdge2Mid, newEdge3Mid);


    t1.splitTriangleProject( currentLevel + 1, maxLevel, org, radius);
    t2.splitTriangleProject( currentLevel + 1, maxLevel, org, radius);
    t3.splitTriangleProject( currentLevel + 1, maxLevel, org, radius); 
    t4.splitTriangleProject( currentLevel + 1, maxLevel, org, radius);

    subTriangles.push_back(t3);
    subTriangles.push_back(t2);
    subTriangles.push_back(t1);
    subTriangles.push_back(t4);

(另外,另一方面,如果这段代码开始变慢,可能会大大加快C ++ 11 std :: move。除非必须,否则不要进入指针。)

答案 1 :(得分:0)

当你将三角形推回向量时,它会推动三角形的副本。然后你在t1到t4递归,但是这个递归不会影响已经被推入向量的内容,因为它是一个副本。

答案 2 :(得分:0)

在计算splitTriangleProject()中的子三角形之后,您需要检索它们并添加到类自己的列表(subTriangles)。 像这样:

void splitTriangleProject( int currentLevel, int maxLevel, const Point &org, double radius)
{
// your code above

subTriangles.insert(subTriangles.begin(), t1.subTriangles.begin(), t1.subTriangles.end());
subTriangles.insert(subTriangles.begin(), t2.subTriangles.begin(), t2.subTriangles.end());
subTriangles.insert(subTriangles.begin(), t3.subTriangles.begin(), t3.subTriangles.end());
subTriangles.insert(subTriangles.begin(), t4.subTriangles.begin(), t4.subTriangles.end());

}

然后你需要添加一个额外的访问方法“const vector&amp; getSubTriangles()”。 (现在你似乎把所有成员变量分享为公共的,这不是一个好习惯)