VTK/C++ - 将自定义 PolyData 上的标量值着色为自定义颜色
VTK/C++ - coloring points to scalar values on custom PolyData to custom colors
我想在 C++/VTK 中用均匀分布的点在球体上绘制 angular 值。因此,我想用选定的颜色为自定义 VTKPolyData 上的点上色。作为起点,我使用了
https://kitware.github.io/vtk-examples/site/Cxx/Visualization/ScalarBarActor/
这会创建一个相似的图片。
我成功地创建了我想要的自定义 PolyData 并计算了 Points 的值(都是正双精度值,大部分小于 1.0),但是颜色与我设置的值不符:
//create evenly spaced out angular coordinates Phi and Theta ...
...
//make a sphere object
sphere = vtkPolyData::New();
vtkPoints* spherepoints = vtkPoints::New();
//create Points on Phi and Theta to (x,y,z)
...spherepoints->InsertNextPoint(P);...
//Add points to PolyData
sphere->SetPoints(spherepoints);
sphere->Allocate();
//Create Connectivity in PolyData by
...
vtkIdList* indices = vtkIdList::New();
indices->Allocate(3);
indices->InsertId(0, integer0);
indices->InsertId(1, integer1);
indices->InsertId(2, integer2);
sphere->InsertNextCell(VTK_TRIANGLE, indices);
...
vtkFloatArray* scalars = vtkFloatArray::New();
//for all points calculate the local value
for (int i = 0; ...; i++) {
double I = f(i);
scalars->InsertTuple1(i, I);
}
// add scalars
sphere->GetPointData()->SetScalars(scalars);
//set a custom threshold
double threshold = 1e-4;
// build a lookuptable (low values: blue, high: red)
vtkLookupTable* lut = vtkLookupTable::New();
lut->SetIndexedLookup(false);
lut->Build();
lut->SetTableValue(0.01 * threshold, 0.0, 0.0, 1.0, 0.1);
lut->SetTableValue(0.1 * threshold, 1.0, 1.0, 1.0, 0.3);
lut->SetTableValue(1 * threshold, 1.0, 1.0, 0.0, 0.5);
lut->SetTableValue(10 * threshold,1.0, 0.0, 0.0, 0.7);
lut->SetTableRange(0, 10* threshold);
//build mapper
vtkNew<vtkPolyDataMapper> mapper;
mapper->SetInputData(sphere);
mapper->ScalarVisibilityOn();
mapper->SetLookupTable(lut);
mapper->SetScalarModeToUsePointData();
mapper->SetColorModeToMapScalars();
//create actor
this->sphereactor = vtkSmartPointer<vtkActor>::New();
sphereactor->SetMapper(mapper);
sphereactor->VisibilityOn();
我也尝试过对标量仅使用整数值(例如通过映射到 0-1000),仅索引我的颜色 (0-3),使用更多自定义插值色点或使用 SetTableRange 或 SetValueRange 命令但没有成功.
我每次得到的图片大多是一种或两种颜色,它们在某种程度上与标量对应或不对应,我能理解。用标量值给表面着色的正确方法是什么?
如果你想为标量范围设置颜色,创建一个 vtkColorTransferFunction
对象,配置它,然后构建。例如 VTK – Set Scalar Range And Show Different Colors
如果你想为每个标量值设置颜色,那将是不同的风格。
例如,Color The Model By Point Scalars And Cell Scalars
我想在 C++/VTK 中用均匀分布的点在球体上绘制 angular 值。因此,我想用选定的颜色为自定义 VTKPolyData 上的点上色。作为起点,我使用了 https://kitware.github.io/vtk-examples/site/Cxx/Visualization/ScalarBarActor/ 这会创建一个相似的图片。
我成功地创建了我想要的自定义 PolyData 并计算了 Points 的值(都是正双精度值,大部分小于 1.0),但是颜色与我设置的值不符:
//create evenly spaced out angular coordinates Phi and Theta ...
...
//make a sphere object
sphere = vtkPolyData::New();
vtkPoints* spherepoints = vtkPoints::New();
//create Points on Phi and Theta to (x,y,z)
...spherepoints->InsertNextPoint(P);...
//Add points to PolyData
sphere->SetPoints(spherepoints);
sphere->Allocate();
//Create Connectivity in PolyData by
...
vtkIdList* indices = vtkIdList::New();
indices->Allocate(3);
indices->InsertId(0, integer0);
indices->InsertId(1, integer1);
indices->InsertId(2, integer2);
sphere->InsertNextCell(VTK_TRIANGLE, indices);
...
vtkFloatArray* scalars = vtkFloatArray::New();
//for all points calculate the local value
for (int i = 0; ...; i++) {
double I = f(i);
scalars->InsertTuple1(i, I);
}
// add scalars
sphere->GetPointData()->SetScalars(scalars);
//set a custom threshold
double threshold = 1e-4;
// build a lookuptable (low values: blue, high: red)
vtkLookupTable* lut = vtkLookupTable::New();
lut->SetIndexedLookup(false);
lut->Build();
lut->SetTableValue(0.01 * threshold, 0.0, 0.0, 1.0, 0.1);
lut->SetTableValue(0.1 * threshold, 1.0, 1.0, 1.0, 0.3);
lut->SetTableValue(1 * threshold, 1.0, 1.0, 0.0, 0.5);
lut->SetTableValue(10 * threshold,1.0, 0.0, 0.0, 0.7);
lut->SetTableRange(0, 10* threshold);
//build mapper
vtkNew<vtkPolyDataMapper> mapper;
mapper->SetInputData(sphere);
mapper->ScalarVisibilityOn();
mapper->SetLookupTable(lut);
mapper->SetScalarModeToUsePointData();
mapper->SetColorModeToMapScalars();
//create actor
this->sphereactor = vtkSmartPointer<vtkActor>::New();
sphereactor->SetMapper(mapper);
sphereactor->VisibilityOn();
我也尝试过对标量仅使用整数值(例如通过映射到 0-1000),仅索引我的颜色 (0-3),使用更多自定义插值色点或使用 SetTableRange 或 SetValueRange 命令但没有成功. 我每次得到的图片大多是一种或两种颜色,它们在某种程度上与标量对应或不对应,我能理解。用标量值给表面着色的正确方法是什么?
如果你想为标量范围设置颜色,创建一个 vtkColorTransferFunction 对象,配置它,然后构建。例如 VTK – Set Scalar Range And Show Different Colors
如果你想为每个标量值设置颜色,那将是不同的风格。 例如,Color The Model By Point Scalars And Cell Scalars