简体   繁体   中英

how to browse nested for loops

the objective is to draw lines with vtkLineSource from a xml file. 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.

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.

My purpose is for each iteration, retrieve the values ​​of x and y and draw lines.

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)? 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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