Skip to content
Snippets Groups Projects

De/Serialization for wavefront object files

Merged Ludwig Jaeck requested to merge serialization into main
All threads resolved!
2 files
+ 121
0
Compare changes
  • Side-by-side
  • Inline
Files
2
ff/Serialize.cpp 0 → 100644
+ 104
0
// ************************************************************************************************
//
// BornAgain: simulate and fit reflection and scattering
//
//! @file ff/Serialize.cpp
//! @brief Implements serializing and deserializing of topologies.
//!
//! @homepage https://jugit.fz-juelich.de/mlz/libformfactor
//! @license GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2022
//! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
// ************************************************************************************************
#include "ff/Serialize.h"
#include <fstream>
namespace {
std::string parse_topology_as_wavefront_obj(const ff::PolyhedralTopology& topology,
const std::vector<R3> vertices)
{
std::stringstream stream;
stream << "o 1"
<< "\n";
for (auto vertice : vertices) {
stream << "v"
<< " " << vertice.x() << " " << vertice.y() << " " << vertice.z() << "\n";
}
stream << "usemtl Default\n";
for (auto& poly : topology.faces) {
if (poly.vertexIndices.size() == 0)
continue;
stream << "f"
<< " ";
for (size_t i = 0; i < poly.vertexIndices.size(); i++) {
stream << poly.vertexIndices[i] + 1;
stream << (i == poly.vertexIndices.size() - 1 ? "\n" : " ");
}
}
return stream.str();
}
std::pair<ff::PolyhedralTopology, std::vector<R3>> parse_string_as_wavefront_obj(std::string data)
{
std::vector<R3> vertices;
ff::PolyhedralTopology topology;
std::string line;
std::stringstream datastream(data);
while (std::getline(datastream, line)) {
std::istringstream is(line);
if (line.at(0) == 'v' && line.at(1) == ' ') {
std::string data;
std::string tp;
double x, y, z;
std::getline(is, data, ' ');
is >> x >> y >> z;
vertices.push_back(R3(x, y, z));
}
if (line.at(0) == 'f') {
std::string data;
std::string tp;
std::string a, b, c;
std::vector<std::string> seglist;
std::vector<int> intlist;
while (std::getline(is, data, ' ')) {
seglist.push_back(data);
}
seglist.erase(seglist.begin());
for (auto seg : seglist) {
auto i = std::stoi(seg.substr(0, seg.find("/"))) - 1;
intlist.push_back(i);
}
topology.faces.push_back({intlist, false});
}
}
return {topology, vertices};
}
} // namespace
namespace ff::io {
void export_ff_to_obj(std::string fileName, const PolyhedralTopology& topology,
const std::vector<R3> vertices)
{
std::ofstream myfile;
myfile.open(fileName);
myfile << parse_topology_as_wavefront_obj(topology, vertices);
myfile.close();
}
std::pair<PolyhedralTopology, std::vector<R3>> import_obj_to_ff(std::string fileName)
{
std::pair<PolyhedralTopology, std::vector<R3>> tp;
std::ifstream file;
file.open(fileName);
tp = parse_string_as_wavefront_obj(
std::string((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>()));
file.close();
return tp;
}
} // namespace ff::io
Loading