Hi,
I’m trying to use vtkSelectVisiblePoints on a vtkActor that has been moved through setUserMatrix, but I cannot manage to make it work as intended.
With the code below, I press the “space” key to obtain the number of visible points in the render.
When I move the camera with the mouse to make the actor partially or completely out of the field of view, the number of visible points is correct.
When I use the Left/Right keys to move the actor by changing its UserMatrix, the number of visible points do not change.
I feel like this is related to the zbuffer not being updated, despite the Render() call after changing the UserMatrix.
Thank you for your help,
S.
#include <vtkPolyData.h>
#include <vtkPointSource.h>
#include <vtkSphereSource.h>
#include <vtkSmartPointer.h>
#include <vtkSelectVisiblePoints.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkCamera.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkObjectFactory.h>
#include <vtkMatrix4x4.h>
class MyInteractorStyle : public vtkInteractorStyleTrackballCamera
{
public:
static MyInteractorStyle* New();
vtkTypeMacro(MyInteractorStyle, vtkInteractorStyleTrackballCamera);
virtual void OnKeyPress() override
{
std::string key = this->Interactor->GetKeySym();
if (key == "space")
{
this->filter->Update();
std::cout << this->filter->GetOutput()->GetNumberOfPoints() << " visible points." << std::endl;
}
if (key == "Left")
{
vtkMatrix4x4* mat = this->actor->GetUserMatrix();
mat->SetElement(0, 3, mat->GetElement(0, 3) - 0.1);
this->Interactor->GetRenderWindow()->Render();
}
if (key == "Right")
{
vtkMatrix4x4* mat = this->actor->GetUserMatrix();
mat->SetElement(0, 3, mat->GetElement(0, 3) + 0.1);
this->Interactor->GetRenderWindow()->Render();
}
vtkInteractorStyleTrackballCamera::OnKeyPress();
}
void SetFilter(vtkSmartPointer<vtkSelectVisiblePoints> vis) { this->filter = vis; }
void SetActor(vtkSmartPointer<vtkActor> act) { this->actor = act; }
private:
vtkSmartPointer<vtkSelectVisiblePoints> filter;
vtkSmartPointer<vtkActor> actor;
};
vtkStandardNewMacro(MyInteractorStyle);
int main(int, char* [])
{
vtkNew<vtkSphereSource> sphereSource;
sphereSource->SetCenter(0, 0, 0);
vtkNew<vtkPolyDataMapper> mapper;
mapper->SetInputConnection(sphereSource->GetOutputPort());
vtkNew<vtkActor> actor;
actor->SetMapper(mapper);
vtkNew<vtkMatrix4x4> mat;
actor->SetUserMatrix(mat);
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->SetSize(800, 800);
vtkNew<vtkRenderer> renderer;
renderWindow->AddRenderer(renderer);
renderer->AddActor(actor);
vtkNew<vtkSelectVisiblePoints> selectVisiblePoints;
selectVisiblePoints->SetInputConnection(sphereSource->GetOutputPort());
selectVisiblePoints->SetRenderer(renderer);
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
renderWindowInteractor->SetRenderWindow(renderWindow);
vtkNew<MyInteractorStyle> style;
style->SetFilter(selectVisiblePoints);
style->SetActor(actor);
renderWindowInteractor->SetInteractorStyle(style);
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}