简体   繁体   English

如何浏览嵌套循环

[英]how to browse nested for loops

the objective is to draw lines with vtkLineSource from a xml file. 目的是使用xml文件中的vtkLineSource绘制线条。 so,I retrieve values ​​from a xml file into vectors, then I loop through the vectors in nested loops to retrieve the values ​​and pass them as arguments to the function of drawing. 因此,我从xml文件中将值检索到向量中,然后在嵌套循环中遍历向量以检索值并将它们作为参数传递给绘图功能。

here is my code 这是我的代码

for ( std::vector<double>::iterator i = tab_recupere_X1.begin();
                       i != tab_recupere_X1.end();
                       i++)
{   
    p0[0]= *i;
    std::cout << "p0[0]"<<p0[0]<<std::endl;



     for (std::vector<double>::iterator j = tab_recupere_Y1.begin();
                       j != tab_recupere_Y1.end();
                       j++)
   {
       p0[1] = *j;
       std::cout << "p0[1]"<<p0[1]<<std::endl;
      // break;

       for (std::vector<double>::iterator k = tab_recupere_X2.begin();
                       k != tab_recupere_X2.end();
                       k++)
         {
            p1[0] = *k;
            std::cout << "p1[0]"<<p1[0]<<std::endl;


            for (std::vector<double>::iterator p =  tab_recupere_Y2.begin();
                       p != tab_recupere_Y2.end();
                       p++)

           {


            p1[1] = *p;

            std::cout << "p1[1]"<<p1[1]<<std::endl;




            vtkSmartPointer<vtkLineSource> lineSource = 
                vtkSmartPointer<vtkLineSource>::New();
            lineSource->SetPoint1(p0);
            lineSource->SetPoint2(p1);
            lineSource->Update();

             vtkSmartPointer<vtkPolyDataMapper> mapper =             vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(lineSource->GetOutputPort());
vtkSmartPointer<vtkActor> actor = 
vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
actor->GetProperty()->SetLineWidth(4);

vtkSmartPointer<vtkRenderer> renderer = 
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow = 
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = 
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);

renderer->AddActor(actor);

renderWindow->Render();
renderWindowInteractor->Start();


     }



         }

  }

}

but the last loop runs indefinitely without stopping, and nothing has been drawn. 但是最后一个循环会无限期地运行而不会停止,因此未绘制任何内容。 I used the (break) but nothing has been improved. 我使用了(break),但没有任何改善。

My purpose is for each iteration, retrieve the values ​​of x and y and draw lines. 我的目的是进行每次迭代,检索x和y的值并画线。

Can you help me please! 你能帮我吗! thank you in advance. 先感谢您。

Alright, I think it is a bit hard to understand what is wrong in your code. 好的,我认为很难理解代码中的错误。 I can't see anything obvious (maybe someone else will) but here are a couple points to help you find out what is going on: 我看不到任何明显的东西(也许会有其他人看到),但是这里有几点可以帮助您了解正在发生的事情:

  • Have you tried to comment out all your functions in your last loop (with iterator p)? 您是否尝试过注释掉最后一个循环中的所有函数(使用迭代器p)? Is it working when you do so? 这样做是否有效?

  • You probably don't want to do any display in this big loop, you should build or draw everything then display only when finished. 您可能不想在这个大循环中进行任何显示,应该构建或绘制所有内容,然后仅在完成时显示。

  • Refactoring this code would be a good idea, you may know what you're doing now but how about in 6 months? 重构此代码是一个好主意,您可能知道自己现在正在做什么,但是六个月后会怎么样? I'm not even mentioning the fact that someone else seeing this code will probably either give up understanding it or rewrite the whole thing himself even if the bug fix would have been one character. 我什至没有提到这样一个事实,即即使该错误修复只是一个字符,其他人看到此代码也可能会放弃对它的理解或自己重写整个内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM