Open smudrak opened 1 year ago
If we choose to use OBJ, library rapidobj looks promising, it has simple interface, uses standard C++ types, and is very fast: https://aras-p.info/blog/2022/05/14/comparing-obj-parse-libraries/.
Simple example:
#include "rapidobj.hpp"
#include <iostream>
int main(int argc, char** argv)
{
if (argc != 2)
{
std::cerr << "Input file path expected\n";
return 1;
}
// Load the model
auto result = rapidobj::ParseFile(argv[1]);
if (result.error)
{
std::cerr << "Failed to load model: " << result.error.code.message() << "\n";
return 1;
}
const auto& mesh = result.attributes.positions;
// Iterate through the positions and print them
// Note: Positions are stored as a flat array of floats,
// with every 3 values representing the x, y, z components of a vertex position.
for (size_t i = 0; i < mesh.size(); i += 3)
{
std::cout << "Vertex " << (i / 3) << ": "
<< mesh[i] << ", "
<< mesh[i + 1] << ", "
<< mesh[i + 2] << "\n";
}
return 0;
}
Or we can try to stick with VTK:
#include <vtkSmartPointer.h>
#include <vtkOBJReader.h>
#include <vtkPolyData.h>
#include <vtkPoints.h>
int main(int argc, char *argv[])
{
if(argc != 2)
{
std::cerr << "Usage: " << argv[0] << " Filename(.obj)" << std::endl;
return 1;
}
// Create a VTK reader for OBJ files
vtkSmartPointer<vtkOBJReader> reader = vtkSmartPointer<vtkOBJReader>::New();
reader->SetFileName(argv[1]);
reader->Update(); // Load the .OBJ file
// Get the loaded geometry
vtkPolyData* polyData = reader->GetOutput();
// Access the vertices
vtkPoints* points = polyData->GetPoints();
// Iterate over and print each vertex
for(vtkIdType i = 0; i < points->GetNumberOfPoints(); i++)
{
double p[3];
points->GetPoint(i, p);
// p contains the x, y, z coordinates of the vertex
std::cout << "Vertex " << i << ": (" << p[0] << ", " << p[1] << ", " << p[2] << ")" << std::endl;
}
return 0;
}
A library (or a set of libraries) is needed in therion, that will be able to: