自定义纹理映射Filter
利用VTK之自定义Filter类(https://blog.csdn.net/webzhuce/article/details/75905519)来写一个自己定义的纹理映射Filter。仿照vtkTextureMapToCylinder类,将 s设为0,t根据点投影到设置的vector上的位置求得,即(projectstion - minprojecttion) / (maxprojection - minprojecttion)。确保t在0和1之间。主要实现如下:
int vtkTextureMapToIrregularity::RequestData(vtkInformation *vtkNotUsed(request), vtkInformationVector **inputVector, vtkInformationVector *outputVector)
{
//get th info objects
vtkInformation* inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation* outInfo = outputVector->GetInformationObject(0);
//get the input and output
vtkDataSet *input = vtkDataSet::SafeDownCast(inInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkDataSet *output = vtkDataSet::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()));
// First, copy the input to the output as a starting point
output->CopyStructure(input);
vtkIdType numPts = input->GetNumberOfPoints();
vtkIdType ptId;
if (numPts == 0) {
vtkErrorMacro(<< "Can't generate texture coordinates without points");
return 1;
}
double minProjection = VTK_DOUBLE_MAX;
double maxProjection = VTK_DOUBLE_MIN;
for (ptId = 0; ptId < numPts; ptId++) {
double pos[3];
input->GetPoint(ptId, pos);
double projection = vtkMath::Dot(pos, this->Vector);
if (projection < minProjection)
minProjection = projection;
if (projection > maxProjection)
maxProjection = projection;
}
vtkFloatArray* newTCoords = vtkFloatArray::New();
newTCoords->SetNumberOfComponents(2);
for (ptId = 0; ptId < numPts; ptId++) {
double pos[3];
input->GetPoint(ptId, pos);
double projection = vtkMath::Dot(pos, this->Vector);
double t = (projection - minProjection) / (maxProjection - minProjection);
if (t > 1.0)
t = 1.0;
if (t < 0.0)
t = 0.0;
newTCoords->InsertNextTuple2(0, t);
}
// Update ourselves
output->GetPointData()->CopyTCoordsOff();
output->GetPointData()->PassData(input->GetPointData());
output->GetCellData()->PassData(input->GetCellData());
output->GetPointData()->SetTCoords(newTCoords);
newTCoords->Delete();
return 1;
}
示例演示
我们将一张牛身体的截图作为纹理图片,如下图所示。
和vtkTextureMapToCylinder类,我们自定义的类目的就是给数据的点设置纹理坐标,如下所示。
output->GetPointData()->SetTCoords(tCoords);