[vtkusers] Picking and moving a vertex

Meisam Aliroteh meisam.aliroteh at gmail.com
Fri Jun 15 12:00:08 EDT 2007


Hi All,

Dean:
thanks for pointing out the widget classes. I remembered needing some sort
of transformation matrix to get point from one frame of referece to another
but didn't know how to do this in vtk and the classes you pointed out were
perfect example for it. Thanks again.
So now I have obtained the motion vector, but do you know how can I
constrain this motion to the view plain only. I guess what I need is to
project the vector onto the view plane and use the projection as the new
motion vector, is this right ?

Geoframer, Henrik:
You don't need to go through all that trouble to create many actors and then
try to get their center , etc. I used to do that until I found the better
way (use GeneratePointIdsOn() of the glygh class). Below is an example of
how to do it in c++:


#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkInteractorStyleTrackballCamera.h"
#include "vtkProperty.h"
#include "vtkPolyData.h"
#include "vtkCell.h"
#include "vtkCellData.h"
#include "vtkSphereSource.h"
#include "vtkGlyph3D.h"
#include "vtkRendererCollection.h"
#include "vtkCommand.h"
#include "vtkCellPicker.h"
#include "vtkPointData.h"
#include "vtkIdTypeArray.h"

class ActionHandler : public vtkCommand
{
    public:
    static ActionHandler *New(){ return new ActionHandler; }
    virtual void Execute(vtkObject *caller, unsigned long eventid, void
*unUsed)
    {
        vtkRenderWindowInteractor *iren = (vtkRenderWindowInteractor
*)caller;
        vtkRenderer *ren = (vtkRenderer
*)iren->GetRenderWindow()->GetRenderers()->GetItemAsObject(0);
        vtkCellPicker *cellPicker = (vtkCellPicker *)iren->GetPicker();
        int mouseX, mouseY;
        iren->GetEventPosition(mouseX, mouseY);

        int cellID = cellPicker->Pick(mouseX,mouseY,0,ren);
        if ( cellID == 0 || (cellID = cellPicker->GetCellId()) == -1 )
        {
            return; // nothing is picked
        }
        else
        {
            vtkActor *pickedActor = cellPicker->GetActor();
            vtkPolyData *pd = (vtkPolyData
*)pickedActor->GetMapper()->GetInput();
            vtkCell *pickedCell = pd->GetCell(cellID);
            int pnt1 = pickedCell->GetPointId(0); // get one of the cell
points
            vtkIdTypeArray *InpPntIds = (vtkIdTypeArray
*)pd->GetPointData()->GetArray("InputPointIds");
            if ( InpPntIds )
            {
                int id = (int)InpPntIds->GetTuple1(pnt1); // get the input
point corresponding to this cell point
                printf("Picked vertex %d\n", id);
            }
        }
    }
};

vtkPolyData *makeTempPoly()
{
    double pnts[4][3] = { {0,0,0}, {10,0,0}, {10,10,0}, {0,10,0} };
    vtkPolyData *pd = vtkPolyData::New();
    vtkPoints *points = vtkPoints::New();

    pd->SetPoints( points );
    pd->Allocate();
    for(int i=0; i<4; i++)
    {
        points->InsertPoint(i, pnts[i][0], pnts[i][1], pnts[i][2]);
        pd->InsertNextCell(VTK_VERTEX, 1, &i);
    }

    vtkIntArray *cell_scalars = vtkIntArray::New();
    cell_scalars->SetNumberOfComponents(1);
    for( int i=0; i<4; i++)
        cell_scalars->InsertNextTuple1(i);
    pd->GetCellData()->SetScalars(cell_scalars);

    return pd;
}

int main()
{
    // Create graphics stuff
    vtkRenderer *ren = vtkRenderer::New();
    ren->SetBackground(0.1,0.2,0.4);

    vtkRenderWindow *renWin = vtkRenderWindow::New();
    renWin->SetSize(800,600);
    renWin->AddRenderer(ren);

    vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
    iren->SetInteractorStyle( vtkInteractorStyleTrackballCamera::New() );
    iren->SetRenderWindow(renWin);

    vtkPolyData *pd1 = makeTempPoly();

    vtkSphereSource *src = vtkSphereSource::New();
    src->SetRadius(0.2);
    src->SetThetaResolution(12);
    src->SetPhiResolution(12);

    vtkGlyph3D *glyph = vtkGlyph3D::New();
    glyph->SetInput(pd1);
    glyph->SetSource(src->GetOutput());
    glyph->GeneratePointIdsOn();

    vtkPolyDataMapper *mapper1 = vtkPolyDataMapper::New();
    mapper1->SetInput( glyph->GetOutput() );
    mapper1->ScalarVisibilityOff();
    mapper1->GlobalImmediateModeRenderingOn();

    vtkActor *actor1 = vtkActor::New();
    actor1->SetMapper( mapper1 );
    actor1->GetProperty()->SetColor(1,0,0);
    actor1->GetProperty()->SetPointSize(4);

    vtkCellPicker *cellPicker = vtkCellPicker::New();
    cellPicker->SetTolerance(0.001);
    iren->SetPicker(cellPicker);

    ActionHandler *h = ActionHandler::New();
    iren->AddObserver(vtkCommand::LeftButtonPressEvent, h, 1);

    ren->AddActor(actor1);
    iren->Initialize();
    iren->Start();

    return 0;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20070615/bdf86058/attachment.htm>


More information about the vtkusers mailing list