vtk求三维图像两点之间的距离

#include"stdafx.h"
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include  
#include  
#include

// PointPickerInteractorStyle
class PointPickerInteractorStyle : public vtkInteractorStyleTrackballCamera
{
public:
	static PointPickerInteractorStyle* New();
	double picked[3];
	int pp2[2000][2];
	double pp[2000][3];
	long double value = 0;
	double value2 = 0;

	int i = 0, j = 0,x=0,y=0,x1=0,x2=0;
	vtkTypeMacro(PointPickerInteractorStyle, vtkInteractorStyleTrackballCamera);

	virtual void OnLeftButtonDown()
	{
		pp2[j][0] = this->Interactor->GetEventPosition()[0]; pp2[j][1] = this->Interactor->GetEventPosition()[1];
		std::cout << "Picking pixel: " << pp2[j][0] << " " << pp2[j][1] << std::endl;
		this->Interactor->GetPicker()->Pick(this->Interactor->GetEventPosition()[0], this->Interactor->GetEventPosition()[1],
			0,  // always zero.
			this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer());

		this->Interactor->GetPicker()->GetPickPosition(picked);
		pp[i][0] = picked[0]; pp[i][1] = picked[1]; pp[i][2] = picked[2];
		std::cout << "Picked value: " << pp[i][0] << " " << pp[i][1] << " " << pp[i][2] << std::endl;
		if (j >= 1)
		{
			value2 = sqrt((pp2[j - 1][0] - pp2[j][0])*(pp2[j - 1][0] - pp2[j][0]) + (pp2[j - 1][1] - pp2[j][1])*(pp2[j - 1][1] - pp2[j][1]));
			std::cout << "两点之间的像素距离是: " << value2 << endl;
		}
		if (i >= 1)
		{
			value = sqrt((pp[i - 1][0] - pp[i][0]) * (pp[i - 1][0] - pp[i][0]) + (pp[i - 1][0] - pp[i][1]) * (pp[i - 1][1] - pp[i][1]) + (pp[i - 1][2] - pp[i][2])* (pp[i - 1][2] - pp[i][2]));
			std::cout << "世界坐标下的两点之间的距离是:" << value << endl;
		}
		i++; j++;
		vtkSmartPointer sphereSource = vtkSmartPointer::New();
		sphereSource->Update();
		vtkSmartPointer mapper = vtkSmartPointer::New();
		mapper->SetInputConnection(sphereSource->GetOutputPort());
		vtkSmartPointer actor = vtkSmartPointer::New();
		actor->SetMapper(mapper);
		actor->SetPosition(picked);
		actor->SetScale(0.8);//设置显示点的大小
		actor->GetProperty()->SetColor(1.0, 0.0, 0.0);
		this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->AddActor(actor);
		vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
	}
};
vtkStandardNewMacro(PointPickerInteractorStyle);

int main(int, char *[])
{
	vtkSmartPointer input1 = vtkSmartPointer::New();
	vtkSmartPointer reader1 = vtkSmartPointer::New();
	reader1->SetFileName("test.stl");
	reader1->Update();
	input1->DeepCopy(reader1->GetOutput());
	// Create a mapper and actor
	vtkSmartPointer mapper = vtkSmartPointer::New();
	mapper->SetInputConnection(reader1->GetOutputPort());
	vtkSmartPointer actor = vtkSmartPointer::New();
	actor->SetMapper(mapper);
	// Create a renderer, render window, and interactor
	vtkSmartPointer renderer = vtkSmartPointer::New();
	vtkSmartPointer renderWindow = vtkSmartPointer::New();
	renderWindow->Render();
	renderWindow->SetWindowName("PointPicker");
	renderWindow->AddRenderer(renderer);
	vtkSmartPointer pointPicker = vtkSmartPointer::New();
	vtkSmartPointer renderWindowInteractor = vtkSmartPointer::New();
	renderWindowInteractor->SetPicker(pointPicker);
	renderWindowInteractor->SetRenderWindow(renderWindow);
	vtkSmartPointer style = vtkSmartPointer::New();
	renderWindowInteractor->SetInteractorStyle(style);
	// Add the actor to the scene
	renderer->AddActor(actor);
	renderer->SetBackground(1, 1, 1);

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

	return EXIT_SUCCESS;
}

 

你可能感兴趣的:(VTK开发)