VTK-模型的颜色映射

原文链接:https://blog.csdn.net/qq_37996632/article/details/106044996

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
 
#include  
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2);
VTK_MODULE_INIT(vtkRenderingFreeType);
 
int main(int, char* [])
{
	// 加载一个J20的STL模型
	vtkSmartPointer<vtkSTLReader> source = vtkSmartPointer<vtkSTLReader>::New();
	source->SetFileName("../data/tmp.stl");
	source->Update();
 
	int numPts = source->GetOutput()->GetPoints()->GetNumberOfPoints();					// 获取模型的顶点数量
	vtkSmartPointer<vtkFloatArray> scalars = vtkSmartPointer<vtkFloatArray>::New();		// 创建存储顶点属性的float数组
	scalars->SetNumberOfValues(numPts);
	for (int i = 0; i < numPts; ++i)		// 为属性数组中的每个元素设置标量值(这个标量值可以当作颜色值)
		scalars->SetValue(i, i);////这个地方点和标量产生联系(第一个参数是第几个点的意思第二个参数就是设定的标量值)
 
	vtkSmartPointer<vtkPolyData> poly = vtkSmartPointer<vtkPolyData>::New();
	poly->DeepCopy(source->GetOutput());
	poly->GetPointData()->SetScalars(scalars);
 
	// 创建颜色查找表
	vtkSmartPointer<vtkLookupTable> hueLut = vtkSmartPointer<vtkLookupTable>::New();
	hueLut->SetNumberOfColors(numPts);		// 指定颜色查找表中有多少种颜色
	hueLut->SetHueRange(0.6667, 0.0);	//蓝到红渐变
	hueLut->Build();
 
	vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
	mapper->SetInputData(poly);
	mapper->SetScalarRange(0, numPts);			// 设置标量值的范围(0-100)
	mapper->ScalarVisibilityOn();
	//mapper->SetColorModeToMapScalars();		// 无论变量数据是何种类型,该方法都通过查询表对标量数据进行映射
	mapper->SetColorModeToDefault();			// 默认的映射器行为,即把unsigned char类型的标量属性数据当作颜色值,不执行隐式。对于其他类型的标量数据,将通过查询表映射。
	mapper->SetLookupTable(hueLut);
 
	vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
	actor->SetMapper(mapper);
 
	vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
	renderer->GradientBackgroundOn();
	renderer->SetBackground(1, 1, 1);
	renderer->SetBackground2(1, 1, 1);
 
	vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
	renderWindow->AddRenderer(renderer);
 
	vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
	renderWindowInteractor->SetRenderWindow(renderWindow);
	vtkSmartPointer<vtkInteractorStyleTrackballCamera> style = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
	renderWindowInteractor->SetInteractorStyle(style);
 
	renderer->AddActor(actor);
	renderWindow->SetSize(600, 600);
	renderWindow->Render();
	renderWindowInteractor->Start();
 
	return 0;
}

VTK-模型的颜色映射_第1张图片

你可能感兴趣的:(VTK)