#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//不添加以下代码,不能运行
#ifndef INITIAL_OPENGL
#define INITIAL_OPENGL
#include
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle)
#endif
using namespace std;
//删除字符串中空格,制表符tab等无效字符
string Trim(string& str)
{
//str.find_first_not_of(" \t\r\n"),在字符串str中从索引0开始,返回首次不匹配"\t\r\n"的位置
str.erase(0, str.find_first_not_of(" \t\r\n"));
str.erase(str.find_last_not_of(" \t\r\n") + 1);
return str;
}
int main()
{
vtkSmartPointer
double x, y, z;
ifstream fin("D:\\QTData\\Vtk-new\\ceshi.csv"); //打开文件流操作-绝对路径
string line;
while (getline(fin, line)) //整行读取,换行符“\n”区分,遇到文件尾标志eof终止读取
{
//cout <<"原始字符串:"<< line << endl; //整行输出
istringstream sin(line); //将整行字符串line读入到字符串流istringstream中
vector
string field;
while (getline(sin, field, ',')) //将字符串流sin中的字符读入到field字符串中,以逗号为分隔符
{
fields.push_back(field); //将刚刚读取的字符串添加到向量fields中
}
string sx = Trim(fields\[0\]);
string sy = Trim(fields\[1\]);
string sz = Trim(fields\[2\]);
//字符串转换成double型数字
istringstream streamx, streamy, streamz;
streamx.str(sx), streamy.str(sy), streamz.str(sz);
streamx >> x, streamy >> y, streamz >> z;
points->InsertNextPoint(x, y, z); //新读取的数据赋予点的几何结构
}
fin.close(); //关闭文件
vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
polyData->SetPoints(points);
vtkSmartPointer<vtkVertexGlyphFilter> glyphFilter = vtkSmartPointer<vtkVertexGlyphFilter>::New();
#if VTK_MAJOR_VERSION <= 5
glyphFilter->SetInputConnection(polyData->GetProducerPort());
#else
glyphFilter->SetInputData(polyData);
#endif
glyphFilter->Update();
// Visualize
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(glyphFilter->GetOutputPort());
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
actor->GetProperty()->SetPointSize(4);
actor->GetProperty()->SetColor(0.0, 0.0, 0.0);
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(actor);
renderer->SetBackground(1.3, 1.6, 1.3); // Background color green
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT\_SUCCESS;
}
数据源csv格式。
输出成果:
注解:
1. 关于 istringstream 的使用方法:
1) 构造函数
istringstream::istringstream(string str);
streamx.str(); //获取字符串内容
streamx>>x; //转化为double。
2) 举例
#include
#include
#include
using namespace std;
int main(){
string s = "ab,cd,e,fg,h";
int n = s.size();
for (int i = 0; i < n; ++i){
if (s[i] == ','){
s[i] = ' ';
}
}
istringstream out(s);
string str;
while (out >> str){
cout << str <<' ';
}
cout << endl;
}
2. getline的用法
区分:cin 遇到空格,制表符等会停止读取空白字表符。
举例:
getline(cin, name, ‘,’);
本文引用:https://blog.csdn.net/u013232740/article/details/50830165
istringstream - C++参考 (cplusplus.com)
以上代码亲测实践。
手机扫一扫
移动阅读更方便
你可能感兴趣的文章