Files
ConformalLabpp/code/include/mesh_io.hpp
user2595 704f42bbfd
Some checks failed
C++ Tests / test-fast (push) Has started running
C++ Tests / test-cgal (push) Has been cancelled
API Docs / doc-build (push) Has been cancelled
Doxygen → Codeberg Pages / publish (push) Has been cancelled
Markdown link check / check (push) Has been cancelled
Mirror to Codeberg / mirror (push) Has been cancelled
Merge pull request 'ci+quality: structural gates (CI: 3 new; local: 7 new + .clang-tidy)' (#18) from ci/structural-tests into main
2026-05-26 09:14:45 +00:00

67 lines
2.1 KiB
C++

#pragma once
// Copyright (c) 2024-2026 Tarik Moussa.
// SPDX-License-Identifier: MIT
// mesh_io.hpp
//
// Phase 4b — CGAL::IO wrappers for ConformalMesh.
//
// Reads and writes ConformalMesh (CGAL::Surface_mesh) in standard polygon-mesh
// formats using the CGAL Polygon Mesh I/O utilities (CGAL 5.4+).
//
// Supported formats (detected by file extension):
// .off — Object File Format (read + write)
// .obj — Wavefront OBJ (read + write)
// .ply — Polygon File Format (read + write)
//
// Usage:
// ConformalMesh mesh;
// if (!read_mesh("input.off", mesh)) throw std::runtime_error("read failed");
// // ... process mesh ...
// write_mesh("output.off", mesh);
//
// Note: property maps (lambda0, v_idx, etc.) are NOT serialised — they must
// be re-initialised with setup_*_maps() + compute_lambda0_from_mesh() after
// reading a file.
#include "conformal_mesh.hpp"
#include <CGAL/IO/polygon_mesh_io.h>
#include <string>
#include <stdexcept>
namespace conformallab {
/// Read a polygon mesh from `filename` into `mesh` (clears existing content).
/// Returns `true` on success, `false` on failure.
inline bool read_mesh(const std::string& filename, ConformalMesh& mesh)
{
mesh.clear();
return CGAL::IO::read_polygon_mesh(filename, mesh);
}
/// Write `mesh` to `filename`. Returns `true` on success.
inline bool write_mesh(const std::string& filename, const ConformalMesh& mesh)
{
return CGAL::IO::write_polygon_mesh(filename, mesh);
}
/// Throwing wrapper around `read_mesh`: returns the mesh by value
/// or throws `std::runtime_error` on read failure.
inline ConformalMesh load_mesh(const std::string& filename)
{
ConformalMesh mesh;
if (!read_mesh(filename, mesh))
throw std::runtime_error("conformallab: failed to read mesh from " + filename);
return mesh;
}
/// Throwing wrapper around `write_mesh`; throws `std::runtime_error` on
/// write failure.
inline void save_mesh(const std::string& filename, const ConformalMesh& mesh)
{
if (!write_mesh(filename, mesh))
throw std::runtime_error("conformallab: failed to write mesh to " + filename);
}
} // namespace conformallab