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.进入渲染管线;
其中点数据(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