c++ - OpenGL - 从外部文件绘制点
问题描述
AM 试图从 CSV 文件中绘制一些点。由于文件很大(> 2GB),将文件内容加载到向量会std::vector<std::vector<std::string> >parsedCsv
引发内存不足异常。
所以我想,不是将文件读取到向量然后绘制它,而是可以直接从 CSV 绘制它。我正在寻找下面的一些修改glVertex3f(x,y,z)
std::ifstream data("D:\\Files\\Dummy2.csv");
std::string line;
while (std::getline(data, line))
{
std::stringstream lineStream(line);
std::string cell;
std::vector<std::string> parsedRow;
while (std::getline(lineStream, cell, ','))
{
glBegin(GL_POINTS);
glColor3f(0.0f, 1.0f, 1.0f);
glVertex3f(----how to represent the points--?)
glEnd();
}
CSV 文件已经是所需的格式:
x1,y1,z1
x2,y2,z2
x3,y3,z3
-------
----
--
有什么建议么 ?
解决方案
您可以使用stof
将字符串值转换为浮点数。将单元格的数量推到vector
. 顶点顶点坐标的分量存储在 中vector
,可以通过 绘制glVertex3fv
:
std::ifstream data("D:\\Files\\Dummy2.csv");
std::string line;
while (std::getline(data, line))
{
std::stringstream lineStream(line);
std::string cell;
std::vector<float> parsedRow;
while (std::getline(lineStream, cell, ','))
parsedRow.push_back(std::stof(cell));
if (parsedRow.size() == 3)
{
glBegin(GL_POINTS);
glColor3f(0.0f, 1.0f, 1.0f);
glVertex3fv(parsedRow.data());
glEnd();
}
}
注意,如果stof
不能执行转换,则抛出无效参数异常。
推荐阅读
- telegram - tdlib 获取(另一个)用户的通知设置
- python - 使用二维数组作为参数在python中调用MATLAB函数
- ios - 团队未在 iTunes Connect 上显示
- php - 将数据从codeigniter中的多个表导出到excel
- ms-access - 循环中的 VBA DLookup
- ui-automation - 如何使用 REST API 在任何地方运行自动化作业?
- android - 如何提示通知侦听器的权限对话框
- python - 如何在 Python 中从文本文件创建文件夹
- javascript - 装饰器不支持 Angular 6 Prod 函数调用,但调用了“..Module”
- c# - 我如何打开表单并通过一个按钮将其关闭