图形处理之纹理映射自定义Filter

自定义纹理映射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);

运行结果:

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值