实例变量列表中的对象没有被保留?指针问题?

时间:2015-04-13 00:12:29

标签: c++

我有一个全局变量vector<BezierPatch*> listOfBezierPatches,我从命令行解析函数填充。然后,我有一系列方法填充BezierPatch的两个实例变量列表:

// final list of subdivided triangles, ready to feed to OpenGL display system
std::vector<Triangle*> listOfTriangles;

// list of differential geometries (i.e. points) that we are evaluating the given patch at
std::vector<DifferentialGeometry*> listOfDifferentialGeometries;

main.cpp中:

void parseBezierFile(string filename) {
    ...
    BezierPatch* currentBezierPatch = new BezierPatch();
    while (getline(file, str)) {
        ...
        vector<Eigen::Vector3f> bezierCurve;
        bezierCurve.push_back(vector_from_command_line);
        ...
        currentBezierPatch->addCurve(bezierCurve);
        ...
        listOfBezierPatches.push_back(currentBezierPatch);            
    }

    perform_subdivision();    
}

void perform_subdivision() {
    // Iterate through each of the Bezier patches...
    for (std::vector<BezierPatch*>::size_type i = 0; i < listOfBezierPatches.size(); i++) {
        BezierPatch currentBezierPatch = *(listOfBezierPatches[i]);
        currentBezierPatch.performUniformSubdivision(subdivisionParameter);
    }
}

BezierPatch.h:

void performUniformSubdivision(float stepSize) {
        int numberOfSteps = 1.0 / stepSize;
        for (int u = 0; u < numberOfSteps; u++) {
            for (int v = 0; v < numberOfSteps; v++) {
                listOfDifferentialGeometries.push_back(evaluateDifferentialGeometry(u * stepSize, v * stepSize));
            }
        }

        // Iterate through all of our differential geometries, but do NOT touch the right-most column and the bottom-most row
        for (int u = 0; u < (numberOfSteps - 1); u++) {
            for (int v = 0; v < (numberOfSteps - 1); v++) {
                // (u, v) represents the index in the above grid that we're triangulating
                // This index represents the TOP LEFT corner of the 4-point rectangle that is described above

                // Index of listOfDifferentialGeometries that corresponds with position (u, v)
                int differentialGeometrixIndex = (u * numberOfSteps) + v;

                // Construct tri-1
                listOfTriangles.push_back(new Triangle(
                        *listOfDifferentialGeometries[differentialGeometrixIndex], // top left
                        *listOfDifferentialGeometries[differentialGeometrixIndex + numberOfSteps], // move one unit right
                        *listOfDifferentialGeometries[differentialGeometrixIndex + 1])); // move one unit down from top left

                // Construct tri-2
                listOfTriangles.push_back(new Triangle(
                        *listOfDifferentialGeometries[differentialGeometrixIndex + numberOfSteps], // top right
                        *listOfDifferentialGeometries[differentialGeometrixIndex + 1], // bottom left
                        *listOfDifferentialGeometries[differentialGeometrixIndex + numberOfSteps + 1])); // bottom right
            }
        }

        // We should have (numberOfSteps - 1) * (numberOfSteps - 1) * 2 triangles
    }

DifferentialGeometry* evaluateDifferentialGeometry(float u, float v) {
    ...
    return new DifferentialGeometry(finalVCurve.point, normal, Eigen::Vector2f(u, v));
}

问题:

在运行上述所有代码之后,如果我遍历listOfBezierPatches中的BezierPatches,listOfTriangleslistOfDifferentialGeometries都是空的。我知道这显然与本地指针/变量有关,但我似乎无法弄明白。以下是发生问题的perform_subdivision方法的详细介绍:

void perform_subdivision(bool adaptive_subdivision) {
    // Iterate through each of the Bezier patches...
    for (std::vector<BezierPatch*>::size_type i = 0; i < listOfBezierPatches.size(); i++) {
        BezierPatch currentBezierPatch = *(listOfBezierPatches[i]);
        currentBezierPatch.performUniformSubdivision(subdivisionParameter);

            // ***** TESTING: This successfully prints out our differential geometries ***** //
            // Iterate through Triangles in the current Bezier patch
            for (std::vector<DifferentialGeometry>::size_type j = 0; j < currentBezierPatch.listOfDifferentialGeometries.size(); j++) {
                DifferentialGeometry* currentDifferentialGeometry = currentBezierPatch.listOfDifferentialGeometries[j];
                cout << "    DifferentialGeometry " << (j + 1) << ":\n";
                Eigen::Vector3f currentPosition = currentDifferentialGeometry->position;
                cout << "      (" << currentPosition.x() << " , " << currentPosition.y() << " , " << currentPosition.z() << ")\n";
            }

            // This does NOT successfully print out our differential geometries...
            printDifferentialGeometriesInBezierPatches();    
        }
    }    
}

其中printDifferentialGeometriesInBezierPatches()定义为:

//****************************************************
// function that prints all differential geometries in every Bezier Patch
//***************************************************
void printDifferentialGeometriesInBezierPatches() {
    if (debug) {
        // Iterate through Bezier Patches
        for (std::vector<BezierPatch*>::size_type i = 0; i < listOfBezierPatches.size(); i++) {
            cout << "  Bezier patch " << (i + 1) << ":\n\n";

            // Iterate through Triangles in the current Bezier patch
            for (std::vector<DifferentialGeometry>::size_type j = 0; i < listOfBezierPatches[i]->listOfDifferentialGeometries.size(); j++) {
                DifferentialGeometry* currentDifferentialGeometry = listOfBezierPatches[i]->listOfDifferentialGeometries[j];
                cout << "    DifferentialGeometry " << (j + 1) << ":\n";
                Eigen::Vector3f currentPosition = currentDifferentialGeometry->position;
                cout << "      (" << currentPosition.x() << " , " << currentPosition.y() << " , " << currentPosition.z() << ")\n";
            }    
        }
    }
}

任何建议都将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

也许你的问题出现在perform_subdivision

的下一行
BezierPatch currentBezierPatch = *(listOfBezierPatches[i]);

对于每个元素,您创建一个在循环结束时删除的副本

void perform_subdivision() {
    // Iterate through each of the Bezier patches...
    for (std::vector<BezierPatch*>::size_type i = 0; i < listOfBezierPatches.size(); i++) {
        // ! Here  you create a copy of i-th element
        BezierPatch currentBezierPatch = *(listOfBezierPatches[i]);
        // populate its memebers
        currentBezierPatch.performUniformSubdivision(subdivisionParameter);
    }
}

我想这是正确的

void perform_subdivision() {
    // Iterate through each of the Bezier patches...
    for (std::vector<BezierPatch*>::size_type i = 0; i < listOfBezierPatches.size(); i++) {
        BezierPatch* currentBezierPatch = listOfBezierPatches[i];
        currentBezierPatch->performUniformSubdivision(subdivisionParameter);
    }
}