VTK笔记-几何图形-点vtkPoints构成的空间曲线

vtkPoints

  vtkPoints表示三维点的集合。
  vtkPoints的数据模型是根据(点或单元)id访问的vx-vy-vz三元组数组。
  默认数据类型为VTK_FLOAT类型;
在这里插入图片描述

Set接口

  设置点个数

   void SetNumberOfPoints(vtkIdType numPoints);  
   vtkTypeBool Resize(vtkIdType numPoints);

  设置点数据

   void SetPoint(vtkIdType id, const float x[3]) VTK_EXPECTS(0 <= id && id < GetNumberOfPoints())
   {
     this->Data->SetTuple(id, x);
   }
   void SetPoint(vtkIdType id, const double x[3]) VTK_EXPECTS(0 <= id && id < GetNumberOfPoints())
   {
     this->Data->SetTuple(id, x);
   }
   void SetPoint(vtkIdType id, double x, double y, double z) VTK_EXPECTS(0 <= id && id < GetNumberOfPoints());  
   void InsertPoint(vtkIdType id, const float x[3]) VTK_EXPECTS(0 <= id)
   {
     this->Data->InsertTuple(id, x);
   }
   void InsertPoint(vtkIdType id, const double x[3]) VTK_EXPECTS(0 <= id)
   {
     this->Data->InsertTuple(id, x);
   }
   void InsertPoint(vtkIdType id, double x, double y, double z) VTK_EXPECTS(0 <= id);  
   void InsertPoints(vtkIdList* dstIds, vtkIdList* srcIds, vtkPoints* source)
   {
     this->Data->InsertTuples(dstIds, srcIds, source->Data);
   }  
   void InsertPoints(vtkIdType dstStart, vtkIdType n, vtkIdType srcStart, vtkPoints* source)
   {
     this->Data->InsertTuples(dstStart, n, srcStart, source->Data);
   }  
   vtkIdType InsertNextPoint(const float x[3]) { return this->Data->InsertNextTuple(x); }
   vtkIdType InsertNextPoint(const double x[3]) { return this->Data->InsertNextTuple(x); }
   vtkIdType InsertNextPoint(double x, double y, double z);

vtkPoints内部数据类型

   virtual int GetDataType() const;  
   virtual void SetDataType(int dataType);
   void SetDataTypeToBit() { this->SetDataType(VTK_BIT); }
   void SetDataTypeToChar() { this->SetDataType(VTK_CHAR); }
   void SetDataTypeToUnsignedChar() { this->SetDataType(VTK_UNSIGNED_CHAR); }
   void SetDataTypeToShort() { this->SetDataType(VTK_SHORT); }
   void SetDataTypeToUnsignedShort() { this->SetDataType(VTK_UNSIGNED_SHORT); }
   void SetDataTypeToInt() { this->SetDataType(VTK_INT); }
   void SetDataTypeToUnsignedInt() { this->SetDataType(VTK_UNSIGNED_INT); }
   void SetDataTypeToLong() { this->SetDataType(VTK_LONG); }
   void SetDataTypeToUnsignedLong() { this->SetDataType(VTK_UNSIGNED_LONG); }
   void SetDataTypeToFloat() { this->SetDataType(VTK_FLOAT); }
   void SetDataTypeToDouble() { this->SetDataType(VTK_DOUBLE); }

Get接口

  获取点集占用内存空间大小,获取指定ID的点坐标数据;

   unsigned long GetActualMemorySize();  
   vtkIdType GetNumberOfPoints() const { return this->Data->GetNumberOfTuples(); }  
   double* GetPoint(vtkIdType id) VTK_EXPECTS(0 <= id && id < GetNumberOfPoints()) VTK_SIZEHINT(3)
   {
     return this->Data->GetTuple(id);
   }  
   void GetPoint(vtkIdType id, double x[3]) VTK_EXPECTS(0 <= id && id < GetNumberOfPoints())VTK_SIZEHINT(3)
   {
     this->Data->GetTuple(id, x);
   }
   void GetPoints(vtkIdList* ptId, vtkPoints* outPoints);

  计算点集边界,xmin,xmax,ymin,ymax,zmin,zmax这6个数值。

   virtual void ComputeBounds();  
   double* GetBounds() VTK_SIZEHINT(6);  
   void GetBounds(double bounds[6]);

示例

读取一个点坐标文件,绘制线段

  1.points.txt文本中每一行记录了XYZ坐标,浮点型,每个数值之间使用空格分割;
  2.读取points.txt文件,拆分每行字符串,得到一个点的XYZ坐标值;
  3.生成vtkPolyData的几何数据vtkPoints(离散点集合);
  4.生成vtkPolyData的拓扑数据vtkCellArray(一组线段信息);
  5.进入渲染管线;

vtkPoints
vtkPolyData
vtkCellArray
vtkPolyDataMapper
vtkActor
vtkRenderer
vtkRenderWindow
vtkRenderWindowInteractor

  其中点数据(Point Data:vtkPoints)定义数据集vtkPolyData的几何结构,单元数据(Cell Data:)定义数据集vtkPolyData的拓扑结构;这样在空间离散的坐标点结合坐标点之间的组合关系,就能描述出多个几何片元信息,用于渲染绘制使用;

#include <vtkLine.h>
#include <vtkPoints.h>
#include <vtkCellArray.h>
#include <iostream>
#include <string>
#include <vector>

class Test_Point_Line {
public:
	static void Test() {
		vtkNew<vtkPoints> points;
		std::vector<std::string> point_datas = ReadPointsFile("G:\\Data\\points.txt");
		for (auto it = point_datas.begin(); it != point_datas.end(); ++it) {
			std::vector<std::string> arr_data = Split(*it, " ");
			double x = std::atof(arr_data[0].c_str());
			double y = std::atof(arr_data[1].c_str());
			double z = std::atof(arr_data[2].c_str());
			points->InsertNextPoint(x, y, z);
		}

		vtkNew<vtkCellArray> lines;
		lines->InsertNextCell(point_datas.size());
		for (unsigned long i = 0; i < point_datas.size(); ++i) {
			lines->InsertCellPoint(i);
		}
		vtkNew<vtkPolyData> polyData;
		polyData->SetPoints(points);
		polyData->SetLines(lines);

		vtkNew<vtkPolyDataMapper> polyMapper;
		polyMapper->SetInputData(polyData);

		vtkNew<vtkActor> polyActor;
		polyActor->SetMapper(polyMapper);

		vtkNew<vtkRenderer> ren1;
		ren1->AddActor(polyActor);
		ren1->SetBackground(0.1, 0.2, 0.4);

		vtkNew<vtkRenderWindow> renWin;
		renWin->AddRenderer(ren1);
		renWin->SetSize(300, 300);

		vtkNew<vtkRenderWindowInteractor> iren;
		iren->SetRenderWindow(renWin);
		iren->Initialize();
		iren->Start();
	}
	
	static std::vector<std::string> ReadPointsFile(const char *path) {
		ifstream in(path);		
		std::string line;
		std::vector<std::string> result;

		if (in)	{
			while (getline(in, line)){
				result.push_back(line);
			}
		}
		else{
			cout << "no such file" << endl;
		}
		return result;
	}

	static std::vector<std::string> Split(const std::string &str, const std::string &pattern) {
		std::vector<std::string> resVec;

		if (str.empty()){
			return resVec;
		}
		std::string strs = str + pattern;
		size_t pos = strs.find(pattern);
		size_t size = strs.size();

		while (pos != std::string::npos){
			resVec.push_back(strs.substr(0, pos));
			strs = strs.substr(pos + 1, size);
			pos = strs.find(pattern);
		}
		return resVec;
	}
};

在这里插入图片描述
当使用SetVerts函数指明输入拓扑结构为点集合时,将绘制出离散点在空间中的显示;

	vtkNew<vtkPolyData> polyData;
	polyData->SetPoints(points);
	polyData->SetVerts(lines);

在这里插入图片描述
  使用的points文件路径:https://download.csdn.net/download/liushao1031177/19134425

参考文献

1.vtkPoints Class Reference
2.vtkCellArray Class Reference

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黑山老妖的笔记本

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值