c++ - 从线列表中形成面

标签 c++

我得到了一个行列表beginendLines(使用每个点的索引)

line0 = (0,1)
line1 = (1,2)
line2 = (2,3)
line3 = (3,0)
line4 = (4,5)
line5 = (5,6)
line6 = (6,7)
line7 = (7,4)
line8 = (0,4)
line9 = (1,5)
line10 = (2,6)
line11 = (3,7)
line12 = (0,5)
line13 = (0,8)
line14 = (1,8)
line15 = (5,8)
line16 = (1,9)
line17 = (2,9)
line18 = (6,9)
line19 = (5,9)
line20 = (4,10)
line21 = (5,11)
line22 = (6,12)
line23 = (7,13)
line24 = (10,11)
line25 = (11,12)
line26 = (12,13)
line27 = (13,10)

现在我需要找到一种方法来弄清楚如何用这些创建面孔。到目前为止我尝试了什么:

int progress = 0; 
for (int i = 0; i< beginendLines.size(); i+=2){
    if (progress == 0){
        Face addface;
        addface.point_indexes.push_back(beginendLines[i]);
    }
    else{
        addface.point_indexes.push_back(beginendLines[i+1]);
        progress = 0;
    }
    if (i != beginendLines.size - 2){   
        if (beginendLines[i+2] != beginendLines[i+1]){
            progress = 1;
            figuur.faces.push_back(addface);
        }
    }
}

这种方法的问题在于每个 for 循环都会生成一张新面孔。同样由于 if 循环,有很多事情超出了范围。任何人都可以通过不同的方法或重组我的代码来帮助我吗?

Face 是一个类,包含一个 vector ,该 vector 具有创建面部的点的索引。 例如0,1,2,3

class Face{
public: 
    vector<int> point_indexes;
};

更清楚地了解我要完成的任务: figuur.faces.push_back(addface) 应该将人脸推送到 figuur.faces。一个面应该至少有 2 个点(1 行的起点和终点),只要下一行的起点等于上一行的终点,下一行的终点索引就应该添加到面中。

最佳答案

For循环问题

for (int i = 0; i< beginendLines.size(); i+=2)

问题是当 i == bigenendLines.size() - 1bigenendLines.size() % 2 == 0 当您尝试使用 beginendLines[ 访问位于 i+1 的项目时,您将越界i+1]

这是一种可能的解决方法:

for (int i = 0; i< beginendLines.size()-1; i+=2)

或者使用迭代器循环:

for(lineIterator = beginendLines.begin(); 
    lineIterator != beginendLines.end(); 
    lineIterator++)

如果您可以访问 C++11,那么您应该能够执行基于范围的 for 循环(不过我不确定这对这种情况是否是个好主意):

 for (const Line& line : beginendLines)

范围界定问题

您的下一个问题是您的 addface 的范围不正确。您在 if 语句中定义它,然后在 else 语句(以及之后的 if 语句)中使用它。

if (progress == 0){
    Face addface;
    addface.point_indexes.push_back(beginendLines[i]);
}
else{
    addface.point_indexes.push_back(beginendLines[i+1]); // HOW?
    progress = 0;
}

你应该在 if 语句之前的 for 循环中定义 addface,例如

for (int i = 0; i< beginendLines.size()-1; i+=2) 
{
    Face addface;
    Other stuff;
}

其他问题

我质疑您在 for 循环中使用的逻辑。主要是你要达到的目的,是否需要process,是否需要if (i != beginendLines.size - 2)

关于每次迭代创建对象的注意事项

如果您有一个面 vector ,并且希望在每次循环迭代时将面插入 vector ,那么很自然地,您必须在每次循环迭代中创建一个面。如果您不将 addface 推送到 vector ,那么我很确定它会超出范围并被销毁,因此您无需担心所有这些面在每次循环迭代时都会填满您的堆.

关于c++ - 从线列表中形成面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49290415/

相关文章:

c++ - 尽管没有错误,openCL内核仍返回垃圾值

c++ - 在 Windows 运行时组件中使用 WRL 获取文件夹路径返回空字符串

C++ STL Vector of lists 在列表末尾插入元素

c++ - 如何在不卡住窗口的情况下在 MFC 中连续运行函数?

c++ - 为什么不能按位和使用作用域枚举?

c++ - 从 C++ 模板参数包编译时间数组

c++ - 有没有办法访问 STL 容器适配器的底层容器?

c++ - 是否可以根据眼睛和嘴巴的位置确定面部的偏航、俯仰和滚动? (含图片)

c++ - 对象数组的选择排序

c++ - 尝试在注册表中创建值 - C++ - RegSetValueEx