[vtkusers] Re : vtkHarswareSelector and vtkMultiBlockDataSet

R M mlokida at yahoo.fr
Mon Oct 17 09:52:22 EDT 2011


Here's a sample code adapted from http://www.vtk.org/Wiki/VTK/Examples/Cxx/Broken/Filtering/ExtractVisibleCells


Except my reader that make a vtkMultiBlockDataSet of vtkUnstructuredGrid the code is the same.

May be someone could tell me what's wrong with the selections I want to do.


#include <vtkSphereSource.h>
#include <vtkPolyData.h>
#include <vtkSmartPointer.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkHardwareSelector.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkObjectFactory.h>
#include <vtkRendererCollection.h>
#include <vtkDataSetMapper.h>
#include <vtkExtractSelection.h>
#include <vtkSelection.h>
#include <vtkProperty.h>
#include <vtkMultiBlockDataSet.h>
#include <vtkUnstructuredGrid.h>
#include <vtkGeometryFilter.h>
#include <vtkFieldData.h>
#include <vtkSelectionNode.h>
#include <vtkIdTypeArray.h>
#include <vtkDataSetAttributes.h>
#include <vtkInformation.h>

// Define interaction style
class KeyPressInteractorStyle : public vtkInteractorStyleTrackballCamera
{
public:
    static KeyPressInteractorStyle* New();
    vtkTypeMacro(KeyPressInteractorStyle, vtkInteractorStyleTrackballCamera);

    virtual void OnKeyPress()
    {
        // Get the keypress
        std::string key = this->Interactor->GetKeySym();

        // "s" for "s"elect
        if(key.compare("s") == 0)
        {
            vtkSmartPointer<vtkHardwareSelector> selector = vtkSmartPointer<vtkHardwareSelector>::New();
            selector->SetRenderer(this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer());
            int* temp = this->Interactor->GetRenderWindow()->GetSize();
            unsigned int windowSize[4];
            windowSize[0] = temp[2];
            windowSize[1] = temp[3];
            windowSize[2] = temp[0];
            windowSize[3] = temp[1];

            /*
            for(unsigned int i = 0; i < 4; i++)
            {
            windowSize[i] = temp[i];
            }
            */
            
            selector->SetArea(windowSize);
            selector->SetFieldAssociation(vtkDataObject::FIELD_ASSOCIATION_CELLS);
            vtkSelection* selection = selector->Select();
            
            vtkSmartPointer<vtkExtractSelection> extractSelection = vtkSmartPointer<vtkExtractSelection>::New();
            extractSelection->DebugOn();
            extractSelection->SetInput(0, mbds);
            extractSelection->SetInput(1, selection);
            extractSelection->Update();

            vtkMultiBlockDataSet *output = vtkMultiBlockDataSet::SafeDownCast( extractSelection->GetOutput() );

            for(int i = 0; i < output->GetNumberOfBlocks(); ++i)
            {
                vtkUnstructuredGrid * usg = vtkUnstructuredGrid::SafeDownCast( output->GetBlock(i) );

                // Add the selection to the renderer
                if(usg)
                {                
                    vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New();
                    mapper->SetInput(usg);
            
                    vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
                    actor->SetMapper(mapper);
                    actor->GetProperty()->SetColor(1,0,0);
                    this->Renderer->AddActor(actor);
                }            
            }
        }
        
        // Forward events
        vtkInteractorStyleTrackballCamera::OnKeyPress();
    }

    vtkMultiBlockDataSet * mbds;
    vtkRenderer* Renderer;

};
vtkStandardNewMacro(KeyPressInteractorStyle);

int main(int , char *[])
{
    
    MyMultiBlockReader mbReader;
    mbReader->Update();

    vtkSmartPointer<vtkMultiBlockDataSet> mbds = mbReader->GetOutput();
    
    // Actors array
    std::vector< vtkSmartPointer<vtkActor> > tabActor;

    // For all blocks
    for(unsigned int i = 0; i < mbds->GetNumberOfBlocks(); ++i)
    {
        // Block i
        vtkUnstructuredGrid *ptr_usg = vtkUnstructuredGrid::SafeDownCast( mbds->GetBlock(i) );

        // vtkUnstructuredGrid to vtkPolyData
        vtkSmartPointer<vtkGeometryFilter> geom = vtkSmartPointer<vtkGeometryFilter>::New();
        geom->SetInput(mbds->GetBlock(i));
        geom->Update();

        // polyData
        vtkSmartPointer<vtkPolyData> polyData =  vtkSmartPointer<vtkPolyData>::New();
        polyData->ShallowCopy( geom->GetOutput() );

        // Mapper
        vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
        mapper->SetInput(polyData);
        
        // Does not work with vtkHardwareSelector. Normal ?
        //vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New();
        //mapper->SetInput(ptr_usg);

        // Actor
        vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
        actor->SetMapper(mapper);

        actor->GetProperty()->EdgeVisibilityOn();

        tabActor.push_back(actor);
    }

    // Create a renderer, render window, and interactor
    vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(renderer);
    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);

    vtkSmartPointer<KeyPressInteractorStyle> style = vtkSmartPointer<KeyPressInteractorStyle>::New();
    style->Renderer = renderer;
    renderWindowInteractor->SetInteractorStyle(style);
    style->SetCurrentRenderer(renderer);
    style->mbds = mbds;

    // Add the actors to the scene
    for(unsigned int i = 0; i < tabActor.size();i++)
    {
        renderer->AddActor(tabActor[i]);
    }

    renderer->SetBackground(.3, .6, .3); // Background color green

    // Render and interact
    renderWindow->Render();
    renderWindowInteractor->Start();

    return EXIT_SUCCESS;
}



________________________________
De : R M <mlokida at yahoo.fr>
À : "vtkusers at vtk.org" <vtkusers at vtk.org>
Envoyé le : Lundi 17 Octobre 2011 13h35
Objet : [vtkusers] vtkHarswareSelector and vtkMultiBlockDataSet


Hello,

I want to select cells  or points in my application. The geometry is a vtkMultiBlockDataset  of vtkUnstructuredGrid.
I try to use the vtkHardwareSelector but I have a no good selection (no cells selection whereas visible, selection of unvisible cells)

So I must do something wrong but don't know what.

How should I manage the selection of visible cells and points on a vtkMultiBlockDataset  of vtkUnstructuredGrid ? 


I really don't know anymore how to manage this.

Any ideas wil be very very welcome.

Thank you.



_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Follow this link to subscribe/unsubscribe:
http://www.vtk.org/mailman/listinfo/vtkusers
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20111017/b93596f3/attachment.htm>


More information about the vtkusers mailing list