C++ VTK reading and saving data from several files - c++

I am new to VTK and my goal is to be able to store information from several files into a vector so then I can separately use filters on some files or show it on QVTKWidget.
My question is what type of vector should I create?
So far I have this:
class for Reader that are creating my reader and pushing it back:
template<class TReader> vtkDataSet *readVTKfile(std::string fileName)
{
vtkSmartPointer<TReader> reader =
vtkSmartPointer<TReader>::New();
reader->SetFileName(fileName.c_str());
reader->Update();
reader->GetOutput()->Register(reader);
return vtkDataSet::SafeDownCast(reader->GetOutput());
}
And here is the problem with how can I use it for my vector:
QStringList filenames = QFileDialog::getOpenFileNames(this, tr("Choose"), "", tr("Vtk files (*.vtk)"));
std::vector<std::string> inputFilenames(filenames.count());
if (!filenames.isEmpty())
{
for (int i = 0; i < filenames.count(); i++)
inputFilenames.push_back(filenames.at(i).toLocal8Bit().constData());
}
std::vector<vtkDataSet*> data(inputFilenames.size()); // what type it should be?
for (int i = 0; i < inputFilenames.size(); i++)
{
data[i] = readVTKfile<vtkGenericDataObjectReader>(inputFilenames[i]);
}
vtkSmartPointer<vtkPolyDataMapper> objectMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
//objectMapper->SetInputConnection(reader->GetOutputPort());
objectMapper->SetInputConnection(data[0]); // so then I can use it for example for showing?
vtkSmartPointer<vtkActor> objectActor =
vtkSmartPointer<vtkActor>::New();
objectActor->SetMapper(objectMapper);
// VTK Renderer
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(objectActor);
// VTK/Qt wedded
this->qvtkWidgetLeft->GetRenderWindow()->AddRenderer(renderer);

QStringList filenames = QFileDialog::getOpenFileNames(this, tr("Choose"), "", tr("Vtk files (*.vtk)"));
// use the range based for, and .toStdString if you wanna std::string from QString
std::vector<std::string> inputFilenames(filenames.count());
for(const auto& filename : filenames) {
inputFilenames.push_back(filename.toStdString());
}
// I'm not sure if this is the right type, but it's what the documentation uses
std::vector<vtkAlgorithmOutput*> data(inputFilenames.size());
for (int i = 0; i < inputFilenames.size(); i++)
{
data[i] = readVTKfile<vtkGenericDataObjectReader>(inputFilenames[i]);
}
vtkSmartPointer<vtkPolyDataMapper> objectMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
objectMapper->SetInputConnection(data[0]);
vtkSmartPointer<vtkActor> objectActor =
vtkSmartPointer<vtkActor>::New();
objectActor->SetMapper(objectMapper);
// VTK Renderer
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(objectActor);
// VTK/Qt wedded
this->qvtkWidgetLeft->GetRenderWindow()->AddRenderer(renderer);

Related

Why the model is not displayed in the center of screen?

I want to show a point cloud with VTK. I inherit a class named VTKWidget from QVTKOpenGLNativeWidget, its constructor is that:
VTKWidget::VTKWidget(QVTKOpenGLNativeWidget* parent)
:QVTKOpenGLNativeWidget(parent)
{
QSurfaceFormat::setDefaultFormat(QVTKOpenGLNativeWidget::defaultFormat());
_renderWindow = vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New();
_render = vtkSmartPointer<vtkRenderer>::New();
_actor = vtkSmartPointer<vtkActor>::New();
_points = vtkSmartPointer<vtkPoints>::New();
_mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
_polyData = vtkSmartPointer<vtkPolyData>::New();
_verts = vtkSmartPointer<vtkCellArray>::New();
_polyData->SetPoints(_points);
_polyData->SetVerts(_verts);
_mapper->SetInputData(_polyData);
_actor->SetMapper(_mapper);
_actor->GetProperty()->SetColor(1.0, 0.0, 0.0);
_actor->GetProperty()->SetPointSize(2.0f);
_render->AddActor(_actor);
double color[] = {0.5, 0.5, 0.5};
_render->SetBackground(color);
_renderWindow->AddRenderer(_render);
//setRenderWindow(_renderWindow);
}
Points will be modified in the function updateData, the updateData function is that:
void VTKWidget::updateData(const QVector<QVector<QVector3D>>& data)
{
setRenderWindow(_renderWindow);
_points->Reset();
long long w = data.size();
long long h = data[0].size();
_points->SetNumberOfPoints(w * h);
_polyData->Reset();
_verts->Reset();
for (int i = 0; i < data.size(); ++i)
{
for (int j = 0; j < data[0].size(); ++j)
{
vtkIdType pid;
pid = _points->InsertNextPoint(data[i][j].x(), data[i][j].y(), data[i][j].z());
_verts->InsertNextCell(1, &pid);
}
}
_polyData->Modified();
_mapper->Update();
}
The question is that if the code setRenderWindow (_renderWindow); is in the constructor, the point cloud will not show in the center of this widget, but will be if the code is in the function updateData.
If you want to see the whole point cloud at the end of updateData, you should use _renderer->ResetCamera().
(https://vtk.org/doc/nightly/html/classvtkRenderer.html#ae8055043e676defbbacff6f1ea65ad1e)
The fact that the location of setRenderWindow() impact the rendering is a side effect.

VTK: View doesn't update until after user interaction

TL;DR
I have a pipeline which reads an image and displays a mesh using VTK; upon changing the input to the image reader and updating the pipeline, the mesh doesn't update until I interact with the window.
The Long Version
I have a directory with a sequence of segmentation files (i.e., 3D image volumes where pixel values correspond to structures in a corresponding image) which show how a structure changes over time. I've written a utility in VTK which allows me to load the first image in the directory, visualize a label as a mesh, and "step" forwards or backwards using the arrow keys by changing the file name of the input image and updating the pipeline. This very nearly works--the only issue is that, after updating the pipeline, the meshes don't update until I interact with the window (simply clicking anywhere in the window causes the mesh to update, for example).
What I've Tried:
Calling Update() on the reader:
this->reader->Update();
Calling Modified() on the actors:
const auto actors = this->GetCurrentRenderer()->GetActors();
actors->InitTraversal();
for (vtkIdType i = 0; i < actors->GetNumberOfItems(); ++i)
{
actors->GetNextActor()->Modified();
}
Calling Render() on the current render window:
this->GetCurrentRenderer()->Render();
MCVE:
NOTE: the reader is updated in KeyPressInteractorStyle::UpdateReader().
// VTK
#include <vtkSmartPointer.h>
#include <vtkNIFTIImageReader.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkCamera.h>
#include <vtkDiscreteMarchingCubes.h>
#include <vtkProperty.h>
#include <vtkInteractorStyleTrackballCamera.h>
// Define interaction style
class KeyPressInteractorStyle : public vtkInteractorStyleTrackballCamera
{
public:
static KeyPressInteractorStyle* New();
vtkTypeMacro(KeyPressInteractorStyle, vtkInteractorStyleTrackballCamera);
virtual void OnKeyPress()
{
// Get the keypress
vtkRenderWindowInteractor *rwi = this->Interactor;
std::string key = rwi->GetKeySym();
// Output the key that was pressed
std::cout << "Pressed " << key << std::endl;
// Handle an arrow key
if(key == "Down" || key == "Right")
{
this->index += 1;
this->UpdateReader();
}
// Handle an arrow key
if(key == "Up" || key == "Left")
{
this->index -= 1;
this->UpdateReader();
}
// Forward events
vtkInteractorStyleTrackballCamera::OnKeyPress();
}
void UpdateReader()
{
std::cout << "Frame: " << this->index << std::endl;
const auto fn = this->directory + std::to_string(this->index) + ".nii.gz";
std::cout << fn << std::endl;
this->reader->SetFileName( fn.c_str() );
this->reader->Update();
const auto actors = this->GetCurrentRenderer()->GetActors();
actors->InitTraversal();
for (vtkIdType i = 0; i < actors->GetNumberOfItems(); ++i)
{
actors->GetNextActor()->Modified();
}
this->GetCurrentRenderer()->Render();
}
unsigned int index = 0;
std::string directory;
vtkSmartPointer<vtkNIFTIImageReader> reader;
};
vtkStandardNewMacro(KeyPressInteractorStyle);
int
main( int argc, char ** argv )
{
std::string dn = argv[1];
const auto renderer = vtkSmartPointer<vtkRenderer>::New();
unsigned int frameid = 0;
const auto reader = vtkSmartPointer<vtkNIFTIImageReader>::New();
reader->SetFileName( (dn + std::to_string(frameid) + ".nii.gz").c_str() );
const auto cubes = vtkSmartPointer<vtkDiscreteMarchingCubes>::New();
cubes->SetInputConnection( reader->GetOutputPort() );
cubes->SetValue( 0, 1 );
const auto mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection( cubes->GetOutputPort() );
mapper->ScalarVisibilityOff();
const auto actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper( mapper );
renderer->AddActor( actor );
const auto window = vtkSmartPointer<vtkRenderWindow>::New();
window->AddRenderer( renderer );
const auto interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
const auto style = vtkSmartPointer<KeyPressInteractorStyle>::New();
style->reader = reader;
style->directory = dn;
interactor->SetInteractorStyle( style );
style->SetCurrentRenderer( renderer );
interactor->SetRenderWindow( window );
window->Render();
interactor->Start();
return EXIT_SUCCESS;
}
You have actually not tried to call Render() on the current render window, you are calling it on the current renderer, which are two different things. As the documentation of vtkRenderer::Renderer() states,
CALLED BY vtkRenderWindow ONLY.
End-user pass your way and call vtkRenderWindow::Render(). Create an
image. This is a superclass method which will in turn call the
DeviceRender method of Subclasses of vtkRenderer.
So change this->GetCurrentRenderer()->Render(); to this->GetCurrentRenderer()->GetRenderWindow()->Render();

Vtk Qt scene with >50 (moving) actors

I am trying to implement (fairly) simple scene where I have ~50 cubes which are moving in certain directions. The position of the cubes changes 20 times per second.
My first shoot was adding and removing actors from the scene. This approach just doesn't scale. Whole scene lags and user is unable to move the camera.
void draw(vtkRenderer *renderer)
{
renderer->RemoveAllViewProps();
for(const Cube& cube : cubes_)
{
vtkSmartPointer<vtkCubeSource> cube_source = vtkSmartPointer<vtkCubeSource>::New();
cube_source->Update();
cube_source->SetXLength(cube.lengt());
cube_source->SetYLength(cube.width());
cube_source->SetZLength(cube.height());
vtkSmartPointer<vtkPolyDataMapper> poly_mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
poly_mapper->SetInputConnection(cube_source->GetOutputPort());
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(poly_mapper);
actor->SetPosition(cube.x(), cube.y(), cube.z());
renderer->AddActor(actor);
}
}
Second shot is a bit better. I have created "actor pool" where I reuse the actors and hide the ones which are not needed.
Still, moving camera is laggy and the rest of my UI (I have some additional widgets inside Vtk widget) seems to be laggy.
I couldn't find any relevant source for Vtk where scene is "dynamic". All examples preload all scene elements and further work with them. Can anyone tell me what am I doing wrong here?
this my visualization VTK example
.....
vtkSmartPointer<vtkRenderer> m_vtkRenderer;
vtkSmartPointer<MouseInteractor> m_mouseInteractor;
QVector<vtkActor* > m_parcellationActors;
QVector<vtkActor* > m_electrodesActors;
QVector<vtkFollower* > m_textActors;
QVector<vtkActor*> m_vectorFieldsActors;
QVector<vtkActor*> m_streamlinesActors;
......
void VisualizationWidget::create3DViewArea()
{
m_3DViewArea = new QStackedWidget(this);
m_vtkWidget = new QVTKWidget(m_3DViewArea);
m_vtkRenderer = vtkSmartPointer<vtkRenderer>::New();
m_vtkRenderer->SetBackground(0.4, 0.4, 0.4);
this->m_vtkWidget->GetRenderWindow()->AddRenderer(m_vtkRenderer);
m_mouseInteractor = vtkSmartPointer<MouseInteractor>::New();
m_mouseInteractor ->SetDefaultRenderer(m_vtkRenderer);
this->m_vtkWidget->GetRenderWindow()->GetInteractor()->SetInteractorStyle( m_mouseInteractor);
connect(m_mouseInteractor, &MouseInteractor::onActorSelected, this, &VisualizationWidget::onActorSelectedSlot);
m_vtkLoadingWidget = new LoadingWidget(m_3DViewArea);
m_vtkLoadingWidget->setIconPath(Icon::s_getTDCSLoadingGif);
m_3DViewArea->addWidget(m_vtkLoadingWidget);
m_3DViewArea->addWidget(m_vtkWidget);
}
void VisualizationWidget::setParcellationActors(QVector<vtkActor*> actors)
{
m_parcellationActors = actors;
for (int i = 0; i < m_parcellationActors.size(); ++i) {
m_vtkRenderer->AddActor(m_parcellationActors.at(i));
}
m_vtkWidget->update();
}
void VisualizationWidget::setElectrodesActors(QVector<vtkActor*> actors)
{
m_electrodesActors = actors;
for (int i = 0; i < m_electrodesActors.size(); ++i) {
m_vtkRenderer->AddActor(m_electrodesActors.at(i));
}
m_vtkWidget->update();
}
void VisualizationWidget::setElectrodesLabelsActors(QVector<vtkFollower*> actors)
{
m_textActors = actors;
for (int i = 0; i < m_textActors.size(); ++i) {
m_textActors.at(i)->SetCamera(m_vtkRenderer->GetActiveCamera());
m_vtkRenderer->AddActor(m_textActors.at(i));
}
m_vtkRenderer->ResetCamera();
m_vtkWidget->update();
}
void VisualizationWidget::setVectorFieldsActors(QVector<vtkActor*> actors)
{
for (int i = 0; i < m_vectorFieldsActors.size(); ++i) {
m_vtkRenderer->RemoveActor(m_vectorFieldsActors.at(i));
}
m_vectorFieldsActors = actors;
for (int i = 0; i < m_vectorFieldsActors.size(); ++i) {
changeActorOpacity(m_vectorFieldsActors[i], double(m_postProcResOpacSliders.at(i)->value()) / m_postProcResOpacSliders.at(i)->maximum());
m_vtkRenderer->AddActor(m_vectorFieldsActors.at(i));
}
m_vtkRenderer->ResetCamera();
m_vtkWidget->update();
}
void VisualizationWidget::setStreamlinesActors(QVector<vtkActor*> actors)
{
for (int i = 0; i < m_streamlinesActors.size(); ++i) {
m_vtkRenderer->RemoveActor(m_streamlinesActors.at(i));
}
m_streamlinesActors = actors;
for (int i = 0; i < m_streamlinesActors.size(); ++i) {
changeActorOpacity(m_streamlinesActors[i], double(m_streamLinesSlider->value()) / m_streamLinesSlider->maximum());
m_vtkRenderer->AddActor(m_streamlinesActors.at(i));
}
m_vtkRenderer->ResetCamera();
m_vtkWidget->update();
}
void VisualizationWidget::changeActorOpacity(vtkActor* actor, double opac)
{
actor->SetVisibility(opac > 0.05);
actor->GetMapper()->Modified();
actor->GetProperty()->SetOpacity(opac);
}
So afther some days of reaserch I managed to hack working solution:
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
// as many points as you like
points->InsertNextPoint(-25, 0, 0);
points->InsertNextPoint(-35, 0, 0);
vtkSmartPointer<vtkFloatArray> scales = vtkSmartPointer<vtkFloatArray>::New();
scales->SetNumberOfComponents(3);
// same as number of points
scales->InsertNextTuple3(1, 1., 8.);
scales->InsertNextTuple3(1., 1., 10.);
vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
polydata->SetPoints(points);
polydata->GetPointData()->SetVectors(scales);
vtkSmartPointer<vtkCubeSource> cubeSource = vtkSmartPointer<vtkCubeSource>::New();
vtkSmartPointer<vtkGlyph3D> glyph3D = vtkSmartPointer<vtkGlyph3D>::New();
glyph3D->OrientOff(); // disable orientation
glyph3D->SetScaleModeToScaleByVectorComponents(); // sacle along each axis
glyph3D->SetSourceConnection(cubeSource->GetOutputPort());
glyph3D->SetInputData(polydata);
glyph3D->Update();
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(glyph3D->GetOutputPort());
mapper->ScalarVisibilityOff(); // use color from actor
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->GetProperty()->SetColor(0, 1, 0); // this will change color for whole glyph
actor->SetMapper(mapper);
mapper->Update();
vtk_renderer->AddActor(actor);
Code from above will add as many cubes and you want using a single actor! (which was amazing performance boost in my case)
Further, if you want to update positions of the cubes, you just need to do following:
points->Reset();
points->InsertNextPoint(-25, 0, 0);
points->InsertNextPoint(-35, 0, 0);
scales->Reset();
scales->InsertNextTuple3(1, 1., 8.);
scales->InsertNextTuple3(1., 1., 10.);
polydata_->Modified();
// call render
(notice that I am not removing/adding actors to the scene which is another boost)

Extracting skin data from an FBX file

I need to convert animation data from Autodesk's FBX file format to one that is compatible with DirectX; specifically, I need to calculate the offset matrices for my skinned mesh. I have written a converter( which in this case converts .fbx to my own 'scene' format ) in which I would like to calculate an offset matrix for my mesh. Here is code:
//
// Skin
//
if(bHasDeformer)
{
// iterate deformers( TODO: ACCOUNT FOR MULTIPLE DEFORMERS )
for(int i = 0; i < ncDeformers && i < 1; ++i)
{
// skin
FbxSkin *pSkin = (FbxSkin*)pMesh->GetDeformer(i, FbxDeformer::eSkin);
if(pSkin == NULL)
continue;
// bone count
int ncBones = pSkin->GetClusterCount();
// iterate bones
for (int boneIndex = 0; boneIndex < ncBones; ++boneIndex)
{
// cluster
FbxCluster* cluster = pSkin->GetCluster(boneIndex);
// bone ref
FbxNode* pBone = cluster->GetLink();
// Get the bind pose
FbxAMatrix bindPoseMatrix, transformMatrix;
cluster->GetTransformMatrix(transformMatrix);
cluster->GetTransformLinkMatrix(bindPoseMatrix);
// decomposed transform components
vS = bindPoseMatrix.GetS();
vR = bindPoseMatrix.GetR();
vT = bindPoseMatrix.GetT();
int *pVertexIndices = cluster->GetControlPointIndices();
double *pVertexWeights = cluster->GetControlPointWeights();
// Iterate through all the vertices, which are affected by the bone
int ncVertexIndices = cluster->GetControlPointIndicesCount();
for (int iBoneVertexIndex = 0; iBoneVertexIndex < ncVertexIndices; iBoneVertexIndex++)
{
// vertex
int niVertex = pVertexIndices[iBoneVertexIndex];
// weight
float fWeight = (float)pVertexWeights[iBoneVertexIndex];
}
}
}
How do I convert the fbx transforms to a bone offset matrix?

Read in .vtk binary file using vtkGenericDataObjectReader

I'm trying to read a legacy .vtk file in c++ and populate my data structures using vtkGenericDataObjectReader (for a Molecular Dynamics simulation). I've searched through the documentation and incorporated answers from similar SO questions, but I'm still misunderstanding something. Here's the file. Forgive the binary, I think it's written correctly but I wouldn't rule out it as the problem.
# 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
This is my code. It segfaults in vtkDataReader::ReadString(char*) from /usr/lib/libvtkIO.so.5.8 while trying to execute the line 'int rv = reader->ReadPoints(ps, int(num_particles));'
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;
I admit I don't know much about VTK. If anyone can help explain what I'm doing wrong, or a better way to go about this, I'd really appreciate it.
The best option to read a PolyData is using the vtkPolyDataReader class.
But if you read the class documentation they give the following warning about BINARY files
"Binary files written on one system may not be readable on other systems."
Here is an example, taken from VTK Wiki that reads .vtk file
#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;
}