[vtkusers] Why does not OnMouseMove() event work?

pnt1614 minpu.code at gmail.com
Thu Mar 29 06:58:23 EDT 2018


I follow an example
(https://www.vtk.org/Wiki/VTK/Examples/Cxx/Picking/HighlightPickedActor) to
highlight a picked actor. Now I want to extend it by overriding the
OnMouseMoveEvent() function with the following code. But a hovered actor's
color is not changed.

class MouseInteractorHighLightActor : public
vtkInteractorStyleTrackballCamera
{
public:
	static MouseInteractorHighLightActor* New();
	vtkTypeMacro(MouseInteractorHighLightActor,
vtkInteractorStyleTrackballCamera);

	MouseInteractorHighLightActor()
	{
		LastPickedActor = NULL;
		LastPickedProperty = vtkProperty::New();
	}
	virtual ~MouseInteractorHighLightActor()
	{
		LastPickedProperty->Delete();
	}
	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());

		// If we picked something before, reset its property
		if (this->LastPickedActor)
		{
			this->LastPickedActor->GetProperty()->DeepCopy(this->LastPickedProperty);
		}
		this->LastPickedActor = picker->GetActor();
		if (this->LastPickedActor)
		{
			// Save the property of the picked actor so that we can
			// restore it next time
			this->LastPickedProperty->DeepCopy(this->LastPickedActor->GetProperty());
			// Highlight the picked actor by changing its properties
			this->LastPickedActor->GetProperty()->SetColor(1.0, 0.0, 0.0);
			this->LastPickedActor->GetProperty()->SetDiffuse(1.0);
			this->LastPickedActor->GetProperty()->SetSpecular(0.0);
		}

		// Forward events
		vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
	}

private:
	vtkActor    *LastPickedActor;
	vtkProperty *LastPickedProperty;
};

vtkStandardNewMacro(MouseInteractorHighLightActor);


//....
int numberOfSpheres = 10;

// A renderer and render window
vtkSmartPointer<vtkRenderer> renderer =
m_vtkWindow->GetRenderers()->GetFirstRenderer();
m_vtkWindow->AddRenderer(renderer);

// An interactor
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
m_vtkWindow->GetInteractor();

// Set the custom type to use for interaction.
vtkSmartPointer<MouseInteractorHighLightActor> style =
vtkSmartPointer<MouseInteractorHighLightActor>::New();
style->SetDefaultRenderer(renderer);

renderWindowInteractor->SetInteractorStyle(style);

for (int i = 0; i < numberOfSpheres; ++i)
{
	vtkSmartPointer<vtkSphereSource> source =
	vtkSmartPointer<vtkSphereSource>::New();
	double x, y, z, radius;
	x = vtkMath::Random(-5, 5);
	y = vtkMath::Random(-5, 5);
	z = vtkMath::Random(-5, 5);
	radius = vtkMath::Random(.5, 1.0);
	source->SetRadius(radius);
	source->SetCenter(x, y, z);
	source->SetPhiResolution(11);
	source->SetThetaResolution(21);
	vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
	mapper->SetInputConnection(source->GetOutputPort());
	vtkSmartPointer<vtkActor> actor =
	vtkSmartPointer<vtkActor>::New();
	actor->SetMapper(mapper);
	double r, g, b;
	r = vtkMath::Random(.4, 1.0);
	g = vtkMath::Random(.4, 1.0);
	b = vtkMath::Random(.4, 1.0);
	actor->GetProperty()->SetDiffuseColor(r, g, b);
	actor->GetProperty()->SetDiffuse(.8);
	actor->GetProperty()->SetSpecular(.5);
	actor->GetProperty()->SetSpecularColor(1.0, 1.0, 1.0);
	actor->GetProperty()->SetSpecularPower(30.0);
	renderer->AddActor(actor);
}

renderer->SetBackground(.3, .4, .5);

// Render and interact
m_vtkWindow->Render();
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();

Is there anyone experienced such a problem? Please, help me.



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


More information about the vtkusers mailing list