VTK渲染raw格式的三维点云数据,并纹理贴图(纹理映射)

#include
#include
#include
#include
#include
#include
#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);

#define RAW_FILE "E:\\1.raw"
#define BMP_File "E:\\PatMatch.bmp"
#define TEX_FILE "E:\\1000wMono8_3840_2748.raw"
#define TEX_WIDE 3840
#define TEX_HIGH 2748


//R,S,T轴是否翻转
#define R_FLIP 1
#define S_FLIP 1
#define T_FLIP 1
#define MAX_Point 1000000

typedef struct 3D_POINT__
{
	float fX;
	float fY;
	float fZ;
} 3D_POINT;

3D_POINT  3D_point[MAX_Point];
long  nPointNum = 0;

double X_angle = 0;
double Y_angle = 0;
double Z_angle = 0; //一般修改此参数来调整贴图方向


int main()
{

	//读取BMP
	vtkSmartPointerbmpReader = vtkSmartPointer::New();
	bmpReader->SetFileName(BMP_File);

    //读取缓存或网络的raw格式的纹理
	/*vtkSmartPointer raw_bmp = vtkSmartPointer::New();  //创建图像
	vtkSmartPointer info = vtkSmartPointer::New();
	raw_bmp->SetDimensions(TEX_WIDE,TEX_HIGH,1);      //指定维数
	raw_bmp->SetScalarType(VTK_UNSIGNED_CHAR,info);  //指定图像的像素数据类型为unsigned char 
	raw_bmp->SetNumberOfScalarComponents(1, info);  //每个像素值为一个标量
	raw_bmp->SetSpacing(1, 1,0);
	raw_bmp->AllocateScalars(info);    //分配内存

	unsigned char *ptr = (unsigned char*)raw_bmp->GetScalarPointer();
	
	ifstream fin_Tex(TEX_FILE, ios::binary | ios::in);
	if (!fin_Tex)
	{
		cout << "File open error!\n";
		return 1;
	}
	int i = 0;
	while (!fin_Tex.eof())
	{
		
		fin_Tex.read((char *)&pData[i],sizeof(char));
		i++;
	}
	fin_Tex.close();

	for (int j = 0; j < TEX_WIDE*TEX_HIGH; j++)
	{
		*ptr= (unsigned char)pData[j];
		ptr++;
		
	}

	*/

	/*
	//读取本地raw格式的纹理
	vtkSmartPointer rawReader = vtkSmartPointer::New();
	rawReader->SetFileName(TEX_FILE);
	rawReader->SetFileDimensionality(2);//设置显示图像的维数
	rawReader->SetDataScalarTypeToUnsignedChar();//将数据转换为unsigned char型
	rawReader->SetDataExtent(0, 3840, 0, 2784, 0,0);//需手动设置图像各维的大小
	rawReader->SetDataSpacing(1,1,0); //设置像素间间距
	//rawReader->SetDataOrigin(0.0, 0.0, 0.0);//设置基准点
	*/

	//在纹理上设置变换,进行缩放,旋转和平移纹理。
	vtkSmartPointer transform = vtkSmartPointer::New();
	//旋转
	//创建一个关于X、Y或Z轴的旋转矩阵,角度以度表示
	transform->RotateX(X_angle);
	transform->RotateY(Y_angle);
	transform->RotateZ(Z_angle); //一般调整此参数来调整贴图方向

	//纹理
	vtkSmartPointertexture = vtkSmartPointer::New();
	texture->SetInputConnection(bmpReader->GetOutputPort());
    //texture->SetInputConnection(rawReader->GetOutputPort());
    //texture->SetInputData(raw_bmp);
	texture->InterpolateOn();
	texture->SetTransform(transform);

	
	vtkSmartPointer  points = vtkSmartPointer ::New();  //点
	vtkSmartPointer  cells = vtkSmartPointer ::New(); //顶点,用于焦点渲染

	vtkIdType idtype;

	//读取raw的二进制数据
	ifstream fin(RAW_FILE, ios::binary | ios::in);
	if (!fin)
	{
		cout << "File open error!\n";
		return 1;
	}

	while (!fin.eof())    
	{
		fin.read((char *)&3D_point[nPointNum], sizeof(3D_POINT)); 
		idtype = points->InsertNextPoint(3D_point[nPointNum].fX, 3D_point[nPointNum].fY, 3D_point[nPointNum].fZ);
		cells->InsertNextCell(1, &idtype);
		nPointNum++;
	}
	fin.close();

	

	//设置polyData的内容
	vtkSmartPointer  polyData = vtkSmartPointer ::New();
	polyData->SetPoints(points);
	polyData->SetVerts(cells);

	//将二维纹理空间映射到一个简单的三维物体表面,例如球面、圆柱面等
	//这里将 2D 纹理坐标生成映射输入的纹理数据集点至一个2D平面上

	vtkSmartPointer textureMap =
	vtkSmartPointer::New();
	
	textureMap->SetInputData(polyData);



	//用来变换纹理坐标,包括缩放、旋转和平移。
	vtkSmartPointer transformTextureCoords =
		vtkSmartPointer::New();
	transformTextureCoords->SetInputConnection(textureMap->GetOutputPort());


	//翻转
	transformTextureCoords->SetFlipR(R_FLIP);
	transformTextureCoords->SetFlipS(S_FLIP);
	transformTextureCoords->SetFlipT(T_FLIP);
	transformTextureCoords->SetOrigin(0.5,0.5,0.5); //设置原点
	transformTextureCoords->SetScale(1, 1, 0); //贴图大小


	//将polyData转换为几何图元
	vtkSmartPointer  mapper = vtkSmartPointer ::New();
	mapper->SetInputConnection(transformTextureCoords->GetOutputPort());

	//可视化表达
	vtkSmartPointer  actor = vtkSmartPointer ::New();
	actor->SetMapper(mapper);
	actor->SetTexture(texture);
	
	//负责管理场景的渲染过程
	vtkSmartPointer  renderer = vtkSmartPointer::New();
	renderer->AddActor(actor);

	//连接操作系统与VTK的渲染引擎
	vtkSmartPointer  renWin = vtkSmartPointer::New();
	renWin->AddRenderer(renderer);
	renWin->SetSize(800, 800);

	//交互
	vtkSmartPointer  iren = vtkSmartPointer::New();
	iren->SetRenderWindow(renWin);

	//交互器样式中的“相机”,用来旋转放大缩小
	vtkSmartPointer  style = vtkSmartPointer ::New();
	iren->SetInteractorStyle(style);


	iren->Initialize();
	iren->Start();
	return 0;

}

RAW文件里是三维的二进制坐标X,Y,Z

 

参考文献:

fstream读写二进制文件

VTK教程系列导航

VTK实现纹理贴图

VTK 简单点云数据显示绘制

VTK点云数据显示

VTK笔记——纹理映射

VTK纹理映射之vtkTransformTextureCoods

VTK纹理映射及原理

图形处理之纹理映射vtkTextureMapToCylinder

纹理贴图(立方体、平面(vtkPlaneSource)、球(vtkSphereSource)、圆柱体(vtkCylinderSource))

你可能感兴趣的:(VTK渲染raw格式的三维点云数据,并纹理贴图(纹理映射))