[vtkusers] Getting Cell Data from an imported VRML model

Secolas UA secolasua at gmail.com
Sun Dec 3 10:59:30 EST 2006


Hello! I am trying to visualize the cell data from an imported VRML model,
and I honestly don't know where to begin.
Anyone has any ideas?

Compliments
Ricardo Seco

Application Code Below




/*****************************************************************************
                                                                    Main.cpp
*****************************************************************************/

#include "vtkVRMLImporter.h"
#include "vtkRenderWindow.h"
#include "vtkRenderer.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkActor.h"
#include "vtkOpenGLActor.h"
#include "vtkActorCollection.h"
#include "vtkCellPicker.h"
#include "vtkSphereSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkPolyData.h"
#include "vtkProperty.h"
#include "vtkCommand.h"
#include "stdio.h"

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

class vtkMyCallback : public vtkCommand
{
public:
        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);

                vtkCellPicker *picker = (vtkCellPicker *)iren->GetPicker();
                   if (picker->GetCellId() != -1)
                {
                        if (m_pvtkActorSelection)

m_pvtkActorSelection->SetPosition(picker->GetPickPosition());
                        iren->Render();
                }
        }
private:
        vtkActor* m_pvtkActorSelection;
};

int main( )
{
    // 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(0.05);
    vtkPolyDataMapper *sphereMapper = vtkPolyDataMapper::New();
    vtkPolyDataMapper *cellMapper = vtkPolyDataMapper::New();
    vtkPolyData *cellData = vtkPolyData::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);

    // Claro que não nos interessa que seja "pickable"

    myPicker->PickableOff();

    /* Importador de Ficheiros VRML */

    vtkVRMLImporter *importer=vtkVRMLImporter::New();
    importer->SetFileName("Model.wrl");
    importer->Read();
    vtkRenderer *ren1;
    ren1 = importer->GetRenderer();


    // Obtenção do número de actores que compõem a cena VRML

    int NumberOfActors = ren1->GetActors()->GetNumberOfItems();

    printf("O numero de actores que compoe a cena sao %d \n",
NumberOfActors);

    // Coleccao de Actores que
    // armazenará o conjunto de
    // actores da cena VRML

    vtkActorCollection *ActColection;

    // Objecto vtkActor que representará
    // cada actor

    vtkActor  *Act;

    // Array que armazenará as coordenadas
    // de cada Actor da cena VRML

    double bounds[6];

    ren1->SetBackground(0,0,1);

     // Contador de actores

    int auxiliar = 0;

    float volume[9];
    for (int i=0; i<=8; i++)
        volume[i]=0.0;
    float volumeTotal = 0.0;

    if (NumberOfActors >0)
    {
          // Obtenção dos actores da cena

        ActColection = ren1->GetActors();
        ActColection->InitTraversal();

        // Para cada iteração ...

        for (int i=0;i<NumberOfActors;i++)
        {
            auxiliar++;

            // Obter esse actor

            Act = ActColection->GetNextActor();

            if (Act!=NULL)
            {
                if (Act->GetMapper()!=NULL)
                {

printf("------------------------------------------------------------------
\n");

                    // Este código consoante o indice do actor
                    // que "engloba" o modelo torna-o ou não
                    // pickable. No caso de só existir um actor
                    // este é sempre pickable

                    if ((auxiliar==1) && (NumberOfActors>1))
                    {
                        Act->PickableOff();
                    }

                      ren1->AddActor(Act);

                    // Obtenção das coordenadas do actor corrente

                    Act->GetBounds(bounds);


printf("------------------------------------------------------------------
\n");

                    printf("Actor %d Tem dimensoes : \n Xmin: %f \n Xmax: %f
\n Ymin: %f \n Ymax: %f \n Zmin: %f \n Zmax: %f
\n",auxiliar,bounds[0],bounds[1],bounds[2],bounds[3],bounds[4],bounds[5]);

                    // Calculo do volume do Objecto (Aproximado)
                    volume[i] =
((bounds[1]-bounds[0])*(bounds[3]-bounds[2])*(bounds[5]-bounds[4]));

                    printf("Actor %d Tem Volume : %f
\n",auxiliar,volume[i]);


printf("------------------------------------------------------------------
\n");
                }
            }
        }

        //Calculo do volume "livre" da sala
        volumeTotal =
volume[0]-(volume[1]+volume[2]+volume[3]+volume[4]+volume[5]+volume[6]+volume[7]);
        printf("A Sala Tem Volume 'Livre' : %f \n",volumeTotal);

printf("------------------------------------------------------------------
\n");
    }

    /* Janela de Renderização */
    vtkRenderWindow *renWin = vtkRenderWindow::New();
    renWin->AddRenderer(ren1);
    renWin->SetSize(640,480);
    ren1->AddActor(myPicker);
    ren1->Render();

    /* Janela de Interacção */
    vtkRenderWindowInteractor *iren=vtkRenderWindowInteractor::New();
    iren->SetRenderWindow(renWin);

    /* Definicao de um Cell Picker */
    vtkCellPicker *picker=vtkCellPicker::New();
    picker->SetTolerance(0.01);
    iren->SetPicker(picker);
    iren->Initialize();

    // Código de atribuição do Observer

    vtkMyCallback *callback = vtkMyCallback::New();
    callback->SetSelectionActor(myPicker);
    iren->AddObserver(vtkCommand::EndPickEvent, callback);
    /* Inicio da renderização da Cena */
    iren->Start();


    ren1->Delete();
    picker->Delete();
    importer->Delete();
    renWin->Delete();
    return 0;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20061203/481b7548/attachment.htm>


More information about the vtkusers mailing list