TU-Programmieren_2/exercise5/TriangleMesh.hpp
2025-04-09 10:22:44 +02:00

75 lines
2.7 KiB
C++

/// @file
/// @brief TriangleMesh: class definition and member function declarations
#pragma once
// hpp
#include <array> // std::array
#include <tuple> // std::tuple
#include <vector> // std::vector
#include <unordered_set> // std::unordered_set
#include <filesystem> // std::filesystem
namespace ex5 {
///@brief Two-dimensional triangle mesh
class TriangleMesh {
public:
///@brief Vertex coordinate
using Vec2d = std::array<double, 2>;
///@brief Index triplet
using Vec3i = std::array<size_t, 3>;
///@brief Axis-aligned bounding box described using the bottom left and upper right coordinate
using BBox = std::tuple<Vec2d, Vec2d>;
private:
std::vector<Vec2d> vertices; ///< sequence of vertices
std::vector<Vec3i> triangles; ///< sequence of index triplets each indexing the three corner vertices of a triangle
public:
/// @brief Generates a regular triangular mesh pattern inside an axis-aligned bounding box
/// @param box Axis-aligned bounding box of the generated mesh
/// @param h Lower bound for the edge length of the generate triangles
TriangleMesh(BBox box, double h);
/// @brief Calculates the current axis-aligned bounding box
/// @return Bounding box of all triangles/vertices in the mesh
BBox bbox() const;
/// @brief Read-only access to the vertices of the mesh
/// @return Reference to the underlying sequence of vertices
const std::vector<Vec2d>& getVertices() const;
/// @brief Read-only access to triangles of the mesh
/// @return Reference to the underlying sequence of triangles
const std::vector<Vec3i>& getTriangles() const;
/// @brief Prints the sequences of vertices and triangles to the console
void print() const;
/// @brief Generate SVG image
/// @param filepath Filename for the generated output
BBox save(std::filesystem::path filepath) const;
/// @brief Evaluates the invariants of the class
/// @return false, if any of the invariants is violated, true otherwise
/// Invariants:
/// - each triangle references valid indices
/// - each triangle references three distinct corners
/// - edges are not occupied more than twice (no hidden edges)
/// - no unreferenced vertices are present
bool check_invariants() const;
/// @brief Removes a set of vertices from the mesh:
/// - all triangles connected to the removed vertices are removed
/// - all unused vertices potentially originating from the triangle removal are removed
/// @param indices Vertex indices to be removed
/// @return Total number of removed vertices and total number of removed triangles
std::tuple<size_t, size_t> remove_vertices(std::unordered_set<size_t> indices);
};
} // namespace mesh