9#ifndef AVARA3D_MESH_INDEXACCESS_H
10#define AVARA3D_MESH_INDEXACCESS_H
20#include "a3d/Assert.h"
21#include "a3d/mesh/IndexFormats.h"
22#include "a3d/mesh/MeshElement.h"
23#include "a3d/mesh/PrimitiveTopology.h"
27 struct IndexStreamView {
28 const std::byte* base =
nullptr;
30 IndexFormat format = IndexFormat::None;
35 static std::optional<IndexStreamView> GetIndexStreamView(
const MeshElement& element);
36 static std::optional<IndexStreamView> GetIndexStreamView(
const MeshElement& element,
37 IndexFormat expectedFormat);
40 static uint32_t ReadIndexU32(
const IndexStreamView& v, uint32_t i);
43 static void ExpandToU32(
const IndexStreamView& v, std::vector<uint32_t>& out);
47 static void GetTrianglesU32(
const MeshElement& element, std::vector<uint32_t>& out);
51 static void ForEachTriangle(
const MeshElement& element, F&& fn) {
53 A3D_ASSERT(element.topology() == PrimitiveTopology::Triangles);
54 if (element.topology() != PrimitiveTopology::Triangles) {
58 auto vOpt = GetIndexStreamView(element);
62 A3D_ASSERT((v.count % 3u) == 0u);
63 if ((v.count % 3u) != 0u) {
67 for (uint32_t i = 0; (i + 2u) < v.count; i += 3u) {
68 const uint32_t a = ReadIndexU32(v, i + 0u);
69 const uint32_t b = ReadIndexU32(v, i + 1u);
70 const uint32_t c = ReadIndexU32(v, i + 2u);
77 const uint32_t vcount = element.vertexCount();
79 A3D_ASSERT((vcount % 3u) == 0u);
80 if ((vcount % 3u) != 0u) {
84 for (uint32_t i = 0; (i + 2u) < vcount; i += 3u) {
85 fn(i + 0u, i + 1u, i + 2u);