gpt4 book ai didi

c++ - 使用 vtkGenericDataObjectReader 读入 .vtk 二进制文件

转载 作者:行者123 更新时间:2023-11-28 07:02:13 27 4
gpt4 key购买 nike

我正在尝试读取 C++ 中的遗留 .vtk 文件,并使用 vtkGenericDataObjectReader(用于分子动力学模拟)填充我的数据结构。我搜索了文档并合并了类似 SO 问题的答案,但我仍然误解了一些东西。这是文件。请原谅二进制文件,我认为它写得正确,但我不排除它是问题所在。

# vtk DataFile Version 3.0
vtk output
BINARY
DATASET POLYDATA
FIELD FieldData 2
TIME 1 1 double
\00\00\00\00\00\00\00\00CYCLE 1 1 int
\00\00\00\00POINTS 8 double
\BF\F0\ED\E4m\D3B2\BF\F0\ED\E4m\D3B2\BF\F0\ED\E4m\D3B2\BF\F0\ED\E4m\D3B2\BF\F0\ED\E4m \D3B2?\F0\ED\E4m\D3B2\BF\F0\ED\E4m\D3B2?\F0\ED\E4m\D3B2\BF\F0\ED\E4m\D3B2\BF\F0\ED\E4m\D3B2?\F0\ED\E4m\D3B2?\F0\ED\E4m\D3B2?\F0\ED\E4m\D3B2\BF\F0\ED\E4m\D3B2\BF\F0\ED\E4m\D3B2?\F0\ED\E4m\D3B2\BF\F0\ED\E4m\D3B2?\F0\ED\E4m\D3B2?\F0\ED\E4m\D3B2?\F0\ED\E4m\D3B2\BF\F0\ED\E4m\D3B2?\F0\ED\E4m\D3B2?\F0\ED\E4m\D3B2?\F0\ED\E4m\D3B2VERTICES 8 16
\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00POINT_DATA 8
SCALARS mass double
LOOKUP_TABLE default
@H\00\00\00\00\00\00@H\00\00\00\00\00\00@H\00\00\00\00\00\00@H\00\00\00\00\00\00@H\00\00\00\00\00\00@H\00\00\00\00\00\00@H\00\00\00\00\00\00@H\00\00\00\00\00\00VECTORS velocity double
\BF\C0\9E b\D8_\BFp\B4Mz\8B\BF\C1\A3|9?\B5\81`\FA\CAk?\C7N\A4ig\BF\94\E5R,\BE瀿\C5wSbK\8E?\98l?\E0\AFϿ\CC3\81\EE\F1n*\BF\C3\DA6\EArf?\B5Ж\CD\EF\99?\C1\F1,\9E\F3\CF?\99=Aɕm"?\B2\87l\89\96eU?\A9\E9cA\A4[?\BD\D6\FD\A2Ϳ\C5\E9r}\93\B1\BF\B8X:a\B86\A4?\BDB\CE\DBV֓\BF\A4\AAa,~,?Č7\CCR{?\BC\F4\99L\B7Y\BF\C3\CD W\E4v(?\BFOS\D4f\8B

这是我的代码。在尝试执行行 'int rv = reader->ReadPoints(ps, int(num_particles)); 时,它在来自/usr/lib/libvtkIO.so.5.8 的 vtkDataReader::ReadString(char*) 中出现段错误

vtkSmartPointer<vtkGenericDataObjectReader> reader = 
vtkSmartPointer<vtkGenericDataObjectReader>::New();
reader->SetFileName(in_rel_path.c_str());
reader->Update();

vtkPolyData* output = reader->GetPolyDataOutput();
vtkPointSet *ps = NULL;
size_t num_particles = output->GetNumberOfPoints();
int rv = reader->ReadPoints(ps, int(num_particles));

vtkPointData* pd = output->GetPointData();
vtkDoubleArray* vel_data = vtkDoubleArray::SafeDownCast(pd->GetVectors());
vtkDoubleArray* mass_data = vtkDoubleArray::SafeDownCast(pd->GetScalars());
vtkDoubleArray* time_data = vtkDoubleArray::SafeDownCast(pd->GetArray("TIME"));
vtkIntArray* cycle_data = vtkIntArray::SafeDownCast(pd->GetArray("CYCLE"));

tot_iters = cycle_data->GetValue(0);
particles.resize(0);
double* position = new double[3];
double* velocity = new double[3];
for( size_t i = 0; i < num_particles; i++ )
{
ps->GetPoint(int(i), position);
vel_data->GetTupleValue(int(i), velocity);
double pmass = mass_data->GetValue(int(i));
particles.push_back(Particle(vec3(position[0],position[1],position[2]),
vec3(velocity[0],velocity[1],velocity[2]),
pmass));
}
delete[] position;
delete[] velocity;

我承认我对VTK了解不多。如果有人可以帮助解释我做错了什么,或者有更好的方法来解决这个问题,我将不胜感激。

最佳答案

读取 PolyData 的最佳选择是使用 vtkPolyDataReader 类。但是如果你阅读类文档,他们会给出以下关于二进制文件的警告“在一个系统上编写的二进制文件在其他系统上可能无法读取。”

这是一个例子,取自读取 .vtk 文件的 VTK Wiki

#include <vtkPolyDataReader.h>
#include <vtkSmartPointer.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>

int main ( int argc, char *argv[] )
{
// Parse command line arguments
if(argc != 2)
{
std::cerr << "Usage: " << argv[0]
<< " Filename(.vtk)" << std::endl;
return EXIT_FAILURE;
}

std::string filename = argv[1];

// Read all the data from the file
vtkSmartPointer<vtkPolyDataReader> reader =
vtkSmartPointer<vtkPolyDataReader>::New();
reader->SetFileName(filename.c_str());
reader->Update();

// Visualize
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(reader->GetOutputPort());

vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);

vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);

renderer->AddActor(actor);
renderer->SetBackground(.3, .6, .3); // Background color green

renderWindow->Render();
renderWindowInteractor->Start();

return EXIT_SUCCESS;
}

关于c++ - 使用 vtkGenericDataObjectReader 读入 .vtk 二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22275586/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com