vtkContourWidget 轮廓线 vtkPolyData


// To setup the ContourWidget and its representation:
#include 
#include 
#include 
// To create the geometry:
#include 
#include 
#include 
#include 
// Usual VTK pipeline elements:
#include 
#include 
#include 
#include 
#include 

int main(int argc, char *argv[])
{
	// Create the contour widget
	vtkSmartPointer contourWidget = vtkSmartPointer::New();

	// Override the default representation for the contour widget to customize its look
	vtkSmartPointer contourRepresentation =	vtkSmartPointer::New();
	contourRepresentation->GetLinesProperty()->SetColor(1, 0, 0); // Set color to red
	contourWidget->SetRepresentation(contourRepresentation);

	// Generate a set of points arranged in a circle
	int numPts = 5;
	vtkSmartPointer points = vtkSmartPointer::New();
	for (int i = 0; i < numPts; i++)
	{
		// Create numPts points evenly spread around a circumference of radius 0.1
		const double angle = 2.0*vtkMath::Pi()*i / numPts;
		std::cout << "ponit:" << static_cast(i) << " " << 0.1*cos(angle) << " " << 0.1*sin(angle) << " " << 0.0 << std::endl;
		points->InsertPoint(static_cast(i), 0.1*cos(angle), 0.1*sin(angle), 0.0);
	}

	// Create a cell array to connect the points into meaningful geometry
	vtkIdType* vertexIndices = new vtkIdType[numPts + 1];
	for (int i = 0; i < numPts; i++)
	{ 
		vertexIndices[i] = static_cast(i);
	}
	// Set the last vertex to 0; this means the last line segment will join the 19th point (vertices[19])
	// with the first one (vertices[0]), thus closing the circle.
	vertexIndices[numPts] = 0;
	for (int i = 0; i < numPts + 1; ++i)
	{
		std::cout << "line:" << vertexIndices[i] << " " << vertexIndices[i + 1] << endl;
	}
	vtkSmartPointer lines = vtkSmartPointer::New();
	lines->InsertNextCell(numPts + 1, vertexIndices);

	// Create polydata to hold the geometry just created, and populate it
	vtkSmartPointer polydata = vtkSmartPointer::New();
	polydata->SetPoints(points);
	polydata->SetLines(lines);

	// Create the renderer to visualize the scene
	vtkSmartPointer renderer = vtkSmartPointer::New();
	renderer->SetBackground(0.1, 0.2, 0.4);			// Set a dark blue background (default is black)

	// Create the GUI window to hold the rendered scene
	vtkSmartPointer renderWindow = vtkSmartPointer::New();
	renderWindow->AddRenderer(renderer);

	// Create the events manager for the renderer window
	vtkSmartPointer interactor = vtkSmartPointer::New();
	interactor->SetRenderWindow(renderWindow);

	// Use the "trackball camera" interactor style, rather than the default "joystick camera"
	vtkSmartPointer style = vtkSmartPointer::New();
	interactor->SetInteractorStyle(style);

	// Set up the contour widget within the visualization pipeline just assembled
	contourWidget->SetInteractor(interactor);
	contourWidget->On();							// Turn on the interactor observer
	contourWidget->Initialize(polydata);


	std::cout << "the number of nodes is " << contourRepresentation->GetNumberOfNodes() << std::endl;
	// you can get node world position of one node by GetNthNodeWorldPosition(pointID, double_pos[3]) 
	renderer->ResetCamera();						// Reposition camera to fit the scene elements

	interactor->Initialize();
	// Start the interaction
	interactor->Start();

	return EXIT_SUCCESS;
}

vtkContourWidget 轮廓线 vtkPolyData_第1张图片

你可能感兴趣的:(VTK/ITK)