[vtkusers] scenepicker not working

andyjk andrewkeeling at hotmail.com
Tue Apr 30 04:26:57 EDT 2019


I am trying to pick using scene picker but it doesn't seem to find a cell or
vertex, while proppicker does. 
How do I extract a pick position from scene picker? (Or even just a picked
cell)
Minimal example below:

#include <vtkVersion.h>
#include <vtkSmartPointer.h>
#include <vtkActor.h>
#include <vtkSphereSource.h>
#include <vtkRendererCollection.h>
#include <vtkCellArray.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkObjectFactory.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkPropPicker.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkPLYReader.h>
#include <vtkScenePicker.h>
#include <vtkSphereSource.h>


// Execute application.
int main(int, char *[])
{
	
	vtkSmartPointer<vtkSphereSource> sphereSource =
		vtkSmartPointer<vtkSphereSource>::New();
	sphereSource->Update();

	// Create a polydata object
	vtkPolyData* polydata = sphereSource->GetOutput();

	// Create a mapper
	vtkSmartPointer<vtkPolyDataMapper> mapper =
		vtkSmartPointer<vtkPolyDataMapper>::New();
#if VTK_MAJOR_VERSION <= 5
	mapper->SetInput(polydata);
#else
	mapper->SetInputData(polydata);
#endif

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

	std::cout << "Actor address: " << actor << std::endl;

	// A renderer and render window
	vtkSmartPointer<vtkRenderer> renderer =
		vtkSmartPointer<vtkRenderer>::New();
	vtkSmartPointer<vtkRenderWindow> renderWindow =
		vtkSmartPointer<vtkRenderWindow>::New();
	renderWindow->SetStencilCapable(1); // Needed for point picking on scene
picker
	renderWindow->AddRenderer(renderer);

	// An interactor
	vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
		vtkSmartPointer<vtkRenderWindowInteractor>::New();
	renderWindowInteractor->SetRenderWindow(renderWindow);

	// Here comes the scene picker stuff. [ Just 2 lines ]
	vtkScenePicker * scenepicker = vtkScenePicker::New();
	scenepicker->SetRenderer(renderer);
	scenepicker->EnableVertexPickingOn();
	

	// Add the actors to the scene
	renderer->AddActor(actor);
	renderer->SetBackground(0, 0, 1);

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


	// Now the picking
stuff....................................................
	int e[2] = { 175, 215 };
	
	// Pick from this location using standard prop picker (this works fine)
	vtkSmartPointer<vtkPropPicker>  picker =
		vtkSmartPointer<vtkPropPicker>::New();
	picker->Pick(e[0], e[1], 0, renderer);

	double* pos = picker->GetPickPosition();
	std::cout << "Pick position (world coordinates) is: "
		<< pos[0] << " " << pos[1]
		<< " " << pos[2] << std::endl;
		
	// Pick using scene picker (this fails)
	vtkIdType vertexId = scenepicker->GetVertexId(e);
	vtkIdType cellId = scenepicker->GetCellId(e);
	std::cout << "Scene pick vertex Id " << vertexId << std::endl;
	std::cout << "Scene pick cell Id " << cellId << std::endl;

	double world_point[3];
	if (vertexId != -1)
	{
		polydata->GetPoint(vertexId, world_point);
		std::cout << "Scene pick position is : " << world_point[0] << " " <<
world_point[1] << " " << world_point[2] << std::endl;;
	}

	renderWindowInteractor->Start();

	return EXIT_SUCCESS;
}



--
Sent from: http://vtk.1045678.n5.nabble.com/VTK-Users-f1224199.html


More information about the vtkusers mailing list