[vtkusers] How to obtain the coordinates (double and not int type) mouse picking over an image?
Georges
Siqueiros.jorge at outlook.com
Fri Apr 22 14:58:09 EDT 2016
Hi everybody, I'm new in vtk, this is my first vtk project. I'm working over
C++ in VisualStudio with vtk 5.10.1 version. I'm making a SDI-MFC aplication
and I'm trying to obtain the coordinates (image coordinates) of the left
mouse click event when this click is over an image. I've reviewed the
examples pickpixel, pickpixel2 and picking, but pickpixel2 gave me the
coordiantes in int type variable and I need the coordinates in double type
variable. I've implemented the picking example (link:
http://www.vtk.org/Wiki/VTK/Examples/Cxx/Interaction/Picking) but when I run
the app over my program I get an Assertion and I can´t find the error. I now
attach my programm. I appreciate your help in explaining how could I make
the picking detection event or find the program mistake.
*//
*************************************************************************
// FUNCTION TO LOAD AN IMAGE IN FORMATS *,jpg. *.bmp, *.tiff, *.png
//
**************************************************************************
void COSSysView::OssysLoadImagevtk(CString m_ImagePath)
{
//Create an image reader
vtkImageFileReader *ImageReader = vtkImageFileReader::New();
ImageReader->SetFileName(m_ImagePath);
ImageReader->Update();
// ###Create an image mapper
vtkImageMapper3D *imageMapper;
vtkImageSliceMapper *imageSMapper = vtkImageSliceMapper::New();
imageMapper = imageSMapper;
imageMapper->SetInputConnection(ImageReader->GetOutputPort());
// Create an actor
vtkImageActor *ImageActor = vtkImageActor::New();
ImageActor->SetInput(ImageReader->GetOutput());
ImageActor->SetPosition(0,0,0);
ImageActor->SetMapper(imageMapper);
GetRenderer()->RemoveAllViewProps(); // Remove all the actors
// Setup renderer
GetRenderer()->AddActor(ImageActor);
GetRenderer()->ResetCamera();
// Setup render window
GetRenderer()->GetRenderWindow()->Render();
// Setup render window interactor
vtkRenderWindowInteractor *renderWindowInteractor =
vtkRenderWindowInteractor::New();
MouseInteractorStyle2 *style = MouseInteractorStyle2::New();
renderWindowInteractor->SetInteractorStyle(style);
// Render and start interaction
renderWindowInteractor->SetRenderWindow(GetRenderer()->GetRenderWindow());
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();
}
*//
**************************************************************************
*//
*************************************************************************
// CLASS TO HANDLE MOUSE EVENT
//
**************************************************************************
class MouseInteractorStyle2 : public vtkInteractorStyleTrackballCamera
{
public:
static MouseInteractorStyle2* New();
vtkTypeMacro(MouseInteractorStyle2, vtkInteractorStyleTrackballCamera);
virtual void OnLeftButtonDown()
{
int* clickPos = this->GetInteractor()->GetEventPosition();
// Pick from this location.
vtkSmartPointer<vtkPropPicker> picker =
vtkSmartPointer<vtkPropPicker>::New();
picker->Pick(clickPos[0], clickPos[1], 0, this->GetDefaultRenderer());
// IN THIS PART THE ASSERTION APPEARS
double* pos = picker->GetPickPosition();
std::cout << "Pick position (world coordinates) is: "
<< pos[0] << " " << pos[1]
<< " " << pos[2] << std::endl;
std::cout << "Picked actor: " << picker->GetActor() << std::endl;
//Create a sphere
vtkSmartPointer<vtkSphereSource> sphereSource =
vtkSmartPointer<vtkSphereSource>::New();
sphereSource->SetCenter(pos[0], pos[1], pos[2]);
sphereSource->SetRadius(0.1);
//Create a mapper and actor
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(sphereSource->GetOutputPort());
vtkSmartPointer<vtkActor> actorsphere =
vtkSmartPointer<vtkActor>::New();
actorsphere->SetMapper(mapper);
this->GetDefaultRenderer()->AddActor(actorsphere);
// Forward events
vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
}
};
vtkStandardNewMacro(MouseInteractorStyle2);
--
View this message in context: http://vtk.1045678.n5.nabble.com/How-to-obtain-the-coordinates-double-and-not-int-type-mouse-picking-over-an-image-tp5737850.html
Sent from the VTK - Users mailing list archive at Nabble.com.
More information about the vtkusers
mailing list