渲染多个vtkpolydata轮廓

时间:2014-06-13 15:32:18

标签: 3d vtk

我正在尝试制作一些轮廓的3D模型。

为此我使用vtkAppendFilter来附加vtkPolyData(这是我的轮廓)。 然后在新的vtkPolyData实例中获取输出。

我放入了一个映射器,然后是一个演员...等等。但出于某种原因,

当我渲染它时,我什么也看不见。只是一个黑屏。

如果有人知道我哪里错了。请告诉我。

谢谢。

以下是代码:

vtkSmartPointer<vtkAppendPolyData> appendFilter = vtkSmartPointer<vtkAppendPolyData>::New();
double z=0;
//Does the job to append them
for(int i=0; i < this->mAllContoursRepresentations.size();i++){
    for(int j=0; j< this->mAllContoursRepresentations.at(i).size();j++){
        // z. is the number of the image
        z = this->mAllContoursRepresentations.at(i).at(j).first;
        //contour to copy
        vtkPolyData* pld = this->mAllContoursRepresentations.at(i).at(j).second->GetContourRepresentationAsPolyData();
        //contour to paste
        vtkSmartPointer<vtkPolyData> vtp = vtkSmartPointer<vtkPolyData>::New();
        //copy contour and paste it in another one
        vtp->DeepCopy(pld);
        vtkPoints* vtpPoints = vtp->GetPoints();
        vtkPoints* pldPoints = pld->GetPoints();
        vtpPoints->SetNumberOfPoints(pld->GetNumberOfPoints());
        for(int k=0;k<vtp->GetNumberOfPoints();k++){
            double toPixel[3];
            double points[3];
            pldPoints->GetPoint(k,points);
            vtkInteractorObserver::ComputeWorldToDisplay(Activity::GetActivity().GetDefaultRendererVisit(),points[0],points[1],points[2],toPixel);
            points[2]= z;
            points[1]=toPixel[1];
            points[0]=toPixel[0];
            vtpPoints->SetPoint(k,points);
        }
        //append contour to the filter
        appendFilter->AddInputData(vtp);
    }
}
appendFilter->Update();

vtkSmartPointer<vtkPolyDataMapper> contoursMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
contoursMapper->SetInputData(contours);
contoursMapper->ScalarVisibilityOff();
vtkSmartPointer<vtkActor> contoursActor = vtkSmartPointer<vtkActor>::New();
contoursActor->SetMapper(contoursMapper);
contoursActor->GetProperty()->SetRepresentationToWireframe();
contoursActor->GetProperty()->SetColor(1,0,1);
vtkSmartPointer<vtkVRMLExporter> exporter = vtkSmartPointer<vtkVRMLExporter>::New();
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
vtkSmartPointer<vtkRenderWindowInteractor> interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderer->AddActor(contoursActor);
renderer->ResetCamera();
renderWindow->AddRenderer(renderer);
interactor->SetRenderWindow(renderWindow);  
renderWindow->Render();
exporter->SetFileName("C:\\Users\\stagiaire\\Desktop\\toto.wrl");
exporter->SetRenderWindow(renderWindow);
exporter->Write();
interactor->Initialize();
interactor->Start();

1 个答案:

答案 0 :(得分:0)

感谢Google,我找到了一种帮助自己的方法。

基本上我正在做的是存储我的积分 在向量中为了构建一个新的vtkpolydata结构(这是一个轮廓 - &gt; simples行)并将其附加到vtkAppendPolyData中。然后使用常规指令,我渲染它。

以下是代码,如果有人需要帮助(随意问):

for(int i=0; i < this->mAllContoursRepresentations.size();i++){
        for(int j=0; j< this->mAllContoursRepresentations.at(i).size();j++){
            //contour to copy
            vtkPolyData* pld = this->mAllContoursRepresentations.at(i).at(j).second->GetContourRepresentationAsPolyData();

            // z. is the number of the image
            z = this->mAllContoursRepresentations.at(i).at(j).first;

            vtkPoints* pldPoints = pld->GetPoints();
            int numberPoints= pld->GetNumberOfPoints();

            for(int k=0;k<numberPoints;k++){
                pldPoints->GetPoint(k,toWorld);

                vtkInteractorObserver::ComputeWorldToDisplay(Activity::GetActivity().GetDefaultRendererVisit(),
                    toWorld[0],toWorld[1],toWorld[2],toPixel);


                qDebug()<< "Points Of contours - Image ";
                qDebug() << z;
                qDebug() << "\n";
                qDebug() << toPixel[0]; 
                qDebug() << toPixel[1];
                qDebug() << z;

                double *pointToSave =new double[3];
                pointToSave[0] =  toPixel[0];
                pointToSave[1] =  toPixel[1];
                pointToSave[2] =  z;

                pointsContours.push_back(pointToSave);  
            }
            pld = NULL;
            pldPoints= NULL;
        }

    }


    delete[] toPixel;
    delete[] toWorld;
    this->Stop();

    //create vtkAppendsPolyData in order to append contours together
    vtkSmartPointer<vtkAppendPolyData> appendFilter = vtkSmartPointer<vtkAppendPolyData>::New();
    int idPoint;
    idPoint=0;
    while(idPoint < pointsContours.size()){

        vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
        unsigned int k=0;       
        for(;idPoint < pointsContours.size()-1;idPoint++){
            if(pointsContours.at(idPoint)[2]==pointsContours.at(idPoint+1)[2]){
                points->InsertNextPoint(pointsContours.at(idPoint));
                k++;                                    
            }
            else 
                break;
        } 

        if(idPoint < pointsContours.size()){
            points->InsertNextPoint(pointsContours.at(idPoint));
            k++;            
            idPoint++;      
        }


        vtkSmartPointer<vtkPolyLine> polyline = vtkSmartPointer<vtkPolyLine>::New();
        polyline->GetPointIds()->SetNumberOfIds(k);
        for(unsigned int j=0; j<k;j++)
            polyline->GetPointIds()->SetId(j,j);

        // Create a cell array to store the lines in and add the lines to it
        vtkSmartPointer<vtkCellArray> cells = 
            vtkSmartPointer<vtkCellArray>::New();
        cells->InsertNextCell(polyline);

        vtkSmartPointer<vtkPolyData> polyData = 
            vtkSmartPointer<vtkPolyData>::New();

        // Add the points to the dataset
        polyData->SetPoints(points);

        // Add the lines to the dataset
        polyData->SetLines(cells);  

        appendFilter->AddInputData(polyData);
    }

    pointsContours.clear();
    appendFilter->Update();


    vtkSmartPointer<vtkPolyDataMapper> contoursMapper = vtkSmartPointer<vtkPolyDataMapper>::New();

    contoursMapper->SetInputData(appendFilter->GetOutput());
    contoursMapper->ScalarVisibilityOff();

    vtkSmartPointer<vtkActor> contoursActor = vtkSmartPointer<vtkActor>::New();
    contoursActor->SetMapper(contoursMapper);
    contoursActor->GetProperty()->SetRepresentationToWireframe();
    contoursActor->GetProperty()->SetColor(0,0,0);


    vtkSmartPointer<vtkVRMLExporter> exporter = vtkSmartPointer<vtkVRMLExporter>::New();
    vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
    vtkSmartPointer<vtkRenderWindowInteractor> interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New();


    renderer->AddActor(contoursActor);
    renderer->ResetCamera();
    renderWindow->AddRenderer(renderer);
    renderer->SetBackground(1,1,1); // Background color
    interactor->SetRenderWindow(renderWindow);
    interactor->Initialize();   
    renderWindow->Render();
    interactor->Start();

此示例也可能对您有所帮助。 http://www.vtk.org/Wiki/VTK/Examples/Cxx/GeometricObjects/PolyLine