MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
MeshData.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "MeshAccess.hpp"
4
5namespace MayaFlux::Kakshya {
6
7/**
8 * @struct MeshData
9 * @brief Owning CPU-side representation of a loaded or generated mesh.
10 *
11 * MeshData is the named owner of the two DataVariant streams that a mesh
12 * consists of: interleaved vertex bytes (vector<uint8_t>) and a flat
13 * uint32_t index array (vector<uint32_t>). It is the handoff type between
14 * IO (ModelReader) and GPU (MeshBuffer), and the input type for any future
15 * mesh container (procedural, Yantra-generated, or deformed).
16 *
17 * Relationship to the accessor layer:
18 * - MeshAccess is a non-owning view derived from MeshData via access().
19 * - MeshInsertion writes into MeshData's two variants directly.
20 * Neither accessor type changes when MeshData is introduced; they already
21 * operate on DataVariant references regardless of ownership.
22 *
23 * Submesh structure follows the established Region convention:
24 * a single RegionGroup named "submeshes" holds one Region per submesh,
25 * each carrying index_start, index_count, vertex_offset, name, and
26 * material_name as attributes.
27 *
28 * MeshData is not a container. It has no processing state, no read head,
29 * no processor chain. For per-cycle mesh mutation use MeshWriterNode.
30 * For offline or procedural generation feeding into MeshBuffer, construct
31 * a MeshData and pass it in.
32 */
33struct MAYAFLUX_API MeshData {
34 DataVariant vertex_variant; ///< vector<uint8_t>: interleaved vertex bytes
35 DataVariant index_variant; ///< vector<uint32_t>: triangle index list
37 std::optional<RegionGroup> submeshes;
38
39 // -------------------------------------------------------------------------
40 // Construction helpers
41 // -------------------------------------------------------------------------
42
43 /**
44 * @brief Construct an empty MeshData with the canonical 60-byte mesh layout.
45 *
46 * Both variants are initialised to empty vectors of the correct type so
47 * MeshInsertion can append into them without a type-check on first write.
48 */
49 static MeshData empty()
50 {
51 MeshData d;
52 d.vertex_variant = std::vector<uint8_t> {};
53 d.index_variant = std::vector<uint32_t> {};
54 d.layout = VertexLayout::for_meshes(60);
55 return d;
56 }
57
58 // -------------------------------------------------------------------------
59 // Validity
60 // -------------------------------------------------------------------------
61
62 /**
63 * @brief True when both variants are non-empty and layout stride is set.
64 */
65 [[nodiscard]] bool is_valid() const noexcept
66 {
67 const auto* vb = std::get_if<std::vector<uint8_t>>(&vertex_variant);
68 const auto* ib = std::get_if<std::vector<uint32_t>>(&index_variant);
69 return vb && !vb->empty()
70 && ib && !ib->empty()
71 && layout.stride_bytes > 0;
72 }
73
74 /**
75 * @brief Number of vertices derived from vertex byte count and layout stride.
76 * @return 0 if layout stride is zero or vertex_variant holds wrong type.
77 */
78 [[nodiscard]] uint32_t vertex_count() const noexcept
79 {
80 if (layout.stride_bytes == 0) {
81 return 0;
82 }
83 const auto* vb = std::get_if<std::vector<uint8_t>>(&vertex_variant);
84 if (!vb) {
85 return 0;
86 }
87 return static_cast<uint32_t>(vb->size() / layout.stride_bytes);
88 }
89
90 /**
91 * @brief Number of triangles (index count / 3).
92 */
93 [[nodiscard]] uint32_t face_count() const noexcept
94 {
95 const auto* ib = std::get_if<std::vector<uint32_t>>(&index_variant);
96 if (!ib) {
97 return 0;
98 }
99 return static_cast<uint32_t>(ib->size() / 3);
100 }
101
102 // -------------------------------------------------------------------------
103 // Accessor layer
104 // -------------------------------------------------------------------------
105
106 /**
107 * @brief Produce a non-owning MeshAccess view over this data.
108 *
109 * The returned MeshAccess borrows from this MeshData instance.
110 * MeshData must outlive any MeshAccess derived from it.
111 *
112 * Returns std::nullopt if is_valid() is false.
113 */
114 [[nodiscard]] std::optional<MeshAccess> access() const
115 {
116 if (!is_valid()) {
117 return std::nullopt;
118 }
119 return as_mesh_access(vertex_variant, index_variant, layout, submeshes);
120 }
121};
122
123} // namespace MayaFlux::Kakshya
std::optional< MeshAccess > as_mesh_access(const DataVariant &vertex_variant, const DataVariant &index_variant, const VertexLayout &layout, std::optional< RegionGroup > submeshes)
Construct a MeshAccess from two DataVariant instances.
std::variant< std::vector< double >, std::vector< float >, std::vector< uint8_t >, std::vector< uint16_t >, std::vector< uint32_t >, std::vector< std::complex< float > >, std::vector< std::complex< double > >, std::vector< glm::vec2 >, std::vector< glm::vec3 >, std::vector< glm::vec4 >, std::vector< glm::mat4 > > DataVariant
Multi-type data storage for different precision needs.
Definition NDData.hpp:76
uint32_t vertex_count() const noexcept
Number of vertices derived from vertex byte count and layout stride.
Definition MeshData.hpp:78
static MeshData empty()
Construct an empty MeshData with the canonical 60-byte mesh layout.
Definition MeshData.hpp:49
bool is_valid() const noexcept
True when both variants are non-empty and layout stride is set.
Definition MeshData.hpp:65
std::optional< RegionGroup > submeshes
Definition MeshData.hpp:37
DataVariant vertex_variant
vector<uint8_t>: interleaved vertex bytes
Definition MeshData.hpp:34
std::optional< MeshAccess > access() const
Produce a non-owning MeshAccess view over this data.
Definition MeshData.hpp:114
uint32_t face_count() const noexcept
Number of triangles (index count / 3).
Definition MeshData.hpp:93
DataVariant index_variant
vector<uint32_t>: triangle index list
Definition MeshData.hpp:35
Owning CPU-side representation of a loaded or generated mesh.
Definition MeshData.hpp:33
uint32_t stride_bytes
Total bytes per vertex (stride in Vulkan terms) e.g., 3 floats (position) + 3 floats (normal) = 24 by...
Complete description of vertex data layout in a buffer.