[vtkusers] Changing PolyData

Ricardo Seco VTK secolasUA at gmail.com
Tue Jan 2 13:50:49 EST 2007


Is it possible to change the polydata in a callback command? I have a 
callback for cell picking and after getting the picked cell id i want to 
eleminate that cell...

Best Regards
Ricardo Seco

// Editor 3D para manipulação de nuvem de pontos e modelos triangulados

#include "stdio.h"

#include "vtkActor.h"
#include "vtkRenderWindow.h"
#include "vtkRenderer.h"
#include "vtkRenderWindowInteractor.h"

#include "vtkProperty.h"
#include "vtkPointPicker.h"
#include "vtkCellPicker.h"
#include "vtkOpenGLActor.h"
#include "vtkCommand.h"
#include "vtkSphereSource.h"
#include "vtkPoints.h"
#include "vtkPointData.h"
#include "vtkPolyData.h"
#include "vtkCellArray.h"
#include "vtkPolyDataMapper.h"

/* IMPLEMENTAÇÂO DO OBSERVER PARA O CELL PICKER */

class vtkMyCallback : public vtkCommand
{
public:
       
        vtkPolyData *dataSet;
       
        vtkMyCallback::vtkMyCallback() { m_pvtkActorSelection = NULL; };
        static vtkMyCallback *New() { return new vtkMyCallback; }
        void SetSelectionActor(vtkActor* pvtkActorSelection) { 
m_pvtkActorSelection = pvtkActorSelection; };
        virtual void Execute(vtkObject *caller, unsigned long, void*)
        {
                vtkRenderWindowInteractor *iren = 
reinterpret_cast<vtkRenderWindowInteractor*>(caller);
                double coords[3] = { 0, 0, 0 };
                int pId;
                vtkCellPicker *picker = (vtkCellPicker *)iren->GetPicker();
                picker->GetPickPosition(coords);
                printf("Coords: x:%f y:%f z:%f \n 
",coords[0],coords[1],coords[2] );
                printf("Point ID: %d \n",picker->GetCellId());
                pId = picker->GetCellId();
                dataSet->DeleteCell(pId);               
                //iren->Render();
                //if (picker->GetPointId() != -1)
                //{
                //        if (m_pvtkActorSelection)
                    //          m_pvtkActorSelection->SetPosition(coords);
                    //    iren->Render();
                //}
        }
private:
        vtkActor* m_pvtkActorSelection;
 };
       

int main()
{
    // Inicializações
    int numPoints;
    int intData[4];
    FILE *fp;
    float fltData1,fltData2,fltData3;
    bool readinVerts;
    char str[100];
    vtkPoints *Points = vtkPoints::New();
    vtkCellArray *Vertices = vtkCellArray::New();
    vtkPolyData *dataSet = vtkPolyData::New();
    numPoints = 0;
    readinVerts = false;

    // Leitura dos dados do ficheiro
    if ((fp = fopen("ASCII_Pontos_Guimaraes.txt","r")) != NULL )
    {
        while (!feof(fp) && (!readinVerts))
        {
            if(fscanf(fp,"%f %f %f",&fltData1,&fltData2,&fltData3) == 0)
            {
                fscanf(fp,"%c",&str);
                readinVerts = true;
                printf("Leu o V!");
            }
            else
            {
                Points->InsertNextPoint(fltData1,fltData2,fltData3);
                Points->InsertPoint(numPoints,fltData1,fltData2,fltData3);
                Vertices->InsertNextCell(1,&numPoints);
                numPoints++;
            }
        }
        /*if (readinVerts)
        {
            Vertices->Initialize();
            Vertices->InitTraversal();
            while (!feof(fp))
            {
                fscanf(fp,"%d %d %d 
%d",&intData[0],&intData[1],&intData[2],&intData[3]);
                Vertices->InsertNextCell(3);
                Vertices->InsertCellPoint(intData[0]);
                Vertices->InsertCellPoint(intData[1]);
                Vertices->InsertCellPoint(intData[2]);
                printf("%d, %d, %d\n", intData[0], intData[1], intData[2]);
            }
        }*/
    }

    fclose(fp);

    // Definição da esfera utilizada no Picking para assinalar a célula 
escolhida
    vtkSphereSource *sphere=vtkSphereSource::New();
    sphere->SetThetaResolution(8);
    sphere->SetPhiResolution(8);
    sphere->SetRadius(5);
    vtkPolyDataMapper *sphereMapper = vtkPolyDataMapper::New();
    sphereMapper->SetInput(sphere->GetOutput());

    // Estabelecimento da esfera como sendo do tipo de actor
    vtkOpenGLActor *myPicker=vtkOpenGLActor::New();
    myPicker->GetProperty()->SetColor(1.0,0.0,0.0);
    myPicker->SetMapper(sphereMapper);

    // Transfere a informação dos pontos para o array de dados PolyData
    dataSet->SetPoints(Points);
    dataSet->SetVerts(Vertices);
   
    // Mapper and actor
    vtkPolyDataMapper *polyMapper = vtkPolyDataMapper::New();
    polyMapper->SetInput(dataSet);

    vtkActor *polyActor = vtkActor::New();
    polyActor->SetMapper(polyMapper);

    // Criaçao do renderer, render window, e interactor.
    vtkRenderer *ren1 = vtkRenderer::New();
    vtkRenderWindow *renWin = vtkRenderWindow::New();
    renWin->AddRenderer(ren1);

    vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();

    ren1->SetBackground(0,0,0);
    ren1->AddActor(polyActor);
     
    // Início da interacção
    iren->SetRenderWindow(renWin);
    renWin->SetSize(800,600);
   
    /* Definicao de um Cell Picker */
    vtkCellPicker *picker=vtkCellPicker::New();
    picker->SetTolerance(0.05);
    iren->SetPicker(picker);
    iren->Initialize();

    // Código de atribuição do Observer
    vtkMyCallback *callback = vtkMyCallback::New();
    callback->SetSelectionActor(myPicker);
    iren->AddObserver(vtkCommand::EndPickEvent, callback);

    iren->Start();
   
    Points->Delete();
    Vertices->Delete();
    dataSet->Delete();
    polyMapper->Delete();
    polyActor->Delete();

    return 0;

}



More information about the vtkusers mailing list