c++ - 使用数组指针索引数组

标签 c++ pointers

<分区>

我正尝试在 C++ 中为光线追踪器做一些半复杂的对象初始化。

以下代码用于通过数组向对象添加三角形面:

// "newTriangles" is a class; the object itself
// "faces" is an array of "triangle", a struct with three ints in it
// "nfaces" is an int of the number of faces which I use to initialize the array:
newTriangles->faces = new triangle[newTriangles->nFaces];

for (int i = 0; i < newTriangles->nFaces; ++i)
{
    // grab values
    newTriangles->faces[i].v1 = a;
    newTriangles->faces[i].v2 = b;
    newTriangles->faces[i].v3 = c;

    cout << i << ": " << newTriangles->faces[0].v1 << ", " << newTriangles->faces[0].v2 << ", " << newTriangles->faces[0].v3 << "\n";

    // Other code in this loop

    // Compute normal for this face
    coordinate v1 = {
        newTriangles->vertices[a].x,
        newTriangles->vertices[a].y,
        newTriangles->vertices[a].z };
    coordinate v2 = {
        newTriangles->vertices[b].x,
        newTriangles->vertices[b].y,
        newTriangles->vertices[b].z };
    coordinate v3 = {
        newTriangles->vertices[c].x,
        newTriangles->vertices[c].y,
        newTriangles->vertices[c].z };

    ray v1v2 = {
        v1.x - v2.x,
        v1.y - v2.y,
        v1.z - v2.z };
    ray v3v2 = {
        v3.x - v2.x,
        v3.y - v2.y,
        v3.z - v2.z };

    ray n = normalize(cross(v1v2, v3v2));
    newTriangles->fNormals[i].x = n.x;
    newTriangles->fNormals[i].y = n.y;
    newTriangles->fNormals[i].z = n.z;


    // This block here seems to be where the problem surfaces
    newTriangles->vNormals[i].x += n.x;
    newTriangles->vNormals[i].y += n.y;
    newTriangles->vNormals[i].z += n.z;

    double d = dot(newTriangles->fNormals[i], newTriangles->vertices[a]);
    newTriangles->d[i] = d;
}

我使用此 cout 命令来确保正确输入我的值。在这段代码中,我只打印出索引 0 处的第一个元素,看看它会发生什么。

这是我尝试用 48 个元素初始化数组时的一些示例输出:

0: 6, 7, 15 (this is correct)
1: 6, 7, 15
2: 6, 7, 15

... (repeats)

28: 6, 7, 15
29: -422365367, -1077858410, 15
30: -422365367, -1077858410, -58508617
31: -422365367, -1077858410, -58508617

... (repeats)

47: -422365367, -1077858410, -58508617

很明显,在我初始化数组的那个循环中,我的数据被破坏了。对我来说毫无意义。

最佳答案

假设您有网格压缩的标准设置:

  1. vertices N 3D 点列表。
  2. faces M 3 个索引点的列表,M 通常比 N 大得多(或者这个结构以需要额外的间接和实现起来很痛苦为代价没有任何好处。

此设置旨在压缩结构以共享所有相邻三角形之间的每个顶点。每个面将 3 个索引存储到 vertices 数组中,而不是 3 个 3D 点。

这里的想法是您有2 个不同大小的列表。当您混淆 for 循环索引或数组大小时,在 C 或 C++ 中实现此设置,尤其是使用动态分配的原始数组尤其痛苦。

让我们看看您的设置。

ray ptr = {
        vertices[faces[i].v1].x - o.x,
        vertices[faces[i].v1].y - o.y,
        vertices[faces[i].v1].z - o.z };

我假设这里的 faces 指向你在第一部分提到的 newTriangles->faces 数组,并且这个列表有 newTriangles->nFaces项目。我还假设此行位于某个循环内,如下所示。

for (int i = 0; i < newTriangles->nFaces; ++i)
{
    ray ptr = {
        vertices[faces[i].v1].x - o.x,
        vertices[faces[i].v1].y - o.y,
        vertices[faces[i].v1].z - o.z };
    // ...
}

你在你的帖子和评论中重复说这个数组被正确分配并且没有被篡改。当然可以。另一个名单呢?这个 vertices 数组从何而来? 那个列表包含多少个元素? 8? 16?我敢打赌是某个接近极限的数字导致了崩溃。

您的帖子没有包含足够的信息,我们无法找到您的问题。不过,我可以为您提供一份快速 list :

  1. 确保 i 的循环在 [0,nFaces) 范围内执行。
  2. 如果 faces[i].v1faces[i].v2faces[i].v3 不匹配 abc 在你的初始化中,你的代码的其他部分已经篡改了你的数组或者你没有正确传递函数参数,数组大小不匹配。
  3. 检查 vertices 是如何分配的,以及它包含多少元素。验证 faces[i].v1faces[i].v2faces[i].v3始终 小于 vertices 中的条目数(请记住,具有与 faces 相同数量的元素)。<

关于c++ - 使用数组指针索引数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5130052/

相关文章:

c++ - "expected initializer before ' < ' token"尝试模板成员特化

c++ - 有没有办法从 QML WorkerScript 运行 C++?

c - 使用递归反转c中的字符串,而不使用任何其他数组或指针

c++ - "this"是否也适配函数指针?

c - 我无法理解这个程序的输出

c++ - 是否可以编写任何 IDE 都无法解析的代码?

c++ - const int& 性能问题

c++ - 计算 vector 中的位

C++:返回类成员变量地址时,编译器强制为const *类型

unit-testing - cmocka malloc 测试 OOM 和 gcov