scene/prim.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | #pragma once | ||
| 3 | |||
| 4 | #include <cstdint> | ||
| 5 | #include <optional> | ||
| 6 | #include <string> | ||
| 7 | #include <string_view> | ||
| 8 | #include <type_traits> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | #include "chrono_batch_render/export.h" | ||
| 12 | #include "chrono_batch_render/stage/schema/ids.h" | ||
| 13 | #include "chrono_batch_render/stage/schema/schema.h" | ||
| 14 | #include "chrono_batch_render/stage/views/attribute.h" | ||
| 15 | #include "chrono_batch_render/stage/views/view.h" | ||
| 16 | #include "chrono_batch_render/util/span.h" | ||
| 17 | |||
| 18 | namespace chrono::batch_render { | ||
| 19 | |||
| 20 | class Scene; | ||
| 21 | |||
| 22 | struct Connection { | ||
| 23 | std::string input; | ||
| 24 | std::string source; | ||
| 25 | std::string output; | ||
| 26 | }; | ||
| 27 | |||
| 28 | class CHRONO_BATCH_RENDER_API Prim { | ||
| 29 | public: | ||
| 30 | 31 | Prim() = default; | |
| 31 | |||
| 32 | 48076 | Prim(Scene& scene, uint32_t index, uint32_t generation) | |
| 33 | 48076 | : m_scene(&scene), m_index(index), m_generation(generation) {} | |
| 34 | |||
| 35 | bool Valid() const; | ||
| 36 | |||
| 37 | 57921 | uint32_t Index() const { | |
| 38 | 57912 | return m_index; | |
| 39 | } | ||
| 40 | |||
| 41 | ✗ | uint32_t Generation() const { | |
| 42 | ✗ | return m_generation; | |
| 43 | } | ||
| 44 | |||
| 45 | SchemaId Schema() const; | ||
| 46 | uint32_t World() const; | ||
| 47 | bool IsSession() const; | ||
| 48 | std::string Path() const; | ||
| 49 | std::string_view Name() const; | ||
| 50 | std::optional<Prim> Parent() const; | ||
| 51 | std::vector<Prim> Children() const; | ||
| 52 | std::vector<Connection> Connections() const; | ||
| 53 | void Connect(std::string_view input, const Prim& source, std::string_view output) const; | ||
| 54 | void Disconnect(std::string_view input) const; | ||
| 55 | bool IsA(SchemaId schema) const; | ||
| 56 | bool HasApi(SchemaId api) const; | ||
| 57 | std::vector<std::string> Instances(SchemaId api) const; | ||
| 58 | |||
| 59 | 2338 | template <class T> bool IsA() const { | |
| 60 | 2338 | return IsA(T::kSchema); | |
| 61 | } | ||
| 62 | |||
| 63 | 8 | template <class T> bool HasApi() const { | |
| 64 | 8 | return HasApi(T::kSchema); | |
| 65 | } | ||
| 66 | |||
| 67 | 1 | template <class T> std::vector<std::string> Instances() const { | |
| 68 | 1 | return Instances(T::kSchema); | |
| 69 | } | ||
| 70 | |||
| 71 | 2 | template <class T> T Get(Field<T> field, std::string_view instance) const { | |
| 72 | 4 | return Read(Single(field.owner, instance), field); | |
| 73 | } | ||
| 74 | |||
| 75 | template <class T> | ||
| 76 | 4 | void Set(Field<T> field, std::string_view instance, | |
| 77 | const typename Identity<T>::Type& value) const { | ||
| 78 | 4 | Write(Single(field.owner, instance), field, value); | |
| 79 | 4 | } | |
| 80 | |||
| 81 | Scene& GetScene() const; | ||
| 82 | |||
| 83 | 18 | template <class T> T Get(std::string_view field) const { | |
| 84 | 35 | return Get<T>(Single().Column(field)); | |
| 85 | } | ||
| 86 | |||
| 87 | 41 | template <class T> void Set(std::string_view field, const T& value) const { | |
| 88 | 41 | Set(Single().Column(field), value); | |
| 89 | 41 | } | |
| 90 | |||
| 91 | 12441 | template <class T> T Get(Field<T> field) const { | |
| 92 | 24879 | return Read(Single(), field); | |
| 93 | } | ||
| 94 | |||
| 95 | 1789 | template <class T> void Set(Field<T> field, const typename Identity<T>::Type& value) const { | |
| 96 | 1789 | Write(Single(), field, value); | |
| 97 | 1788 | } | |
| 98 | |||
| 99 | 3 | bool operator==(const Prim& other) const { | |
| 100 | 3 | return m_scene == other.m_scene && m_index == other.m_index && | |
| 101 | m_generation == other.m_generation; | ||
| 102 | } | ||
| 103 | |||
| 104 | bool operator!=(const Prim& other) const { | ||
| 105 | return !(*this == other); | ||
| 106 | } | ||
| 107 | |||
| 108 | protected: | ||
| 109 | void CheckSchema(SchemaId schema) const; | ||
| 110 | |||
| 111 | private: | ||
| 112 | 18 | template <class T> T Get(ColumnRef column) const { | |
| 113 | 52 | return Single().Get<T>(column).Get().front(); | |
| 114 | } | ||
| 115 | |||
| 116 | 41 | template <class T> void Set(ColumnRef column, const T& value) const { | |
| 117 | 82 | Single().Get<T>(column).Set(value); | |
| 118 | 41 | } | |
| 119 | |||
| 120 | 12442 | template <class T> T Read(const View& view, Field<T> field) const { | |
| 121 | 12442 | const ColumnRef column = field.Column(); | |
| 122 | if constexpr (std::is_same_v<T, std::string>) { | ||
| 123 | 1444 | return std::string(Text(view.Get<Token>(column).Get().front())); | |
| 124 | } else if constexpr (std::is_same_v<T, std::vector<std::string>>) { | ||
| 125 | 1335 | const UntypedAttribute values = view.Get(column); | |
| 126 | 1335 | return column.column->type == ColumnType::TokenArray ? values.GetTokens(0) | |
| 127 | 1335 | : values.GetTargets(0); | |
| 128 | 1335 | } else if constexpr (std::is_same_v<T, std::vector<Primvar>>) { | |
| 129 | 6 | return view.Get(column).GetPrimvars(0); | |
| 130 | } else if constexpr (HasItems<T>::value) { | ||
| 131 | 73 | return view.GetArray<typename T::value_type>(column).Get(0); | |
| 132 | } else { | ||
| 133 | 21114 | return view.Get<T>(column).Get().front(); | |
| 134 | } | ||
| 135 | } | ||
| 136 | |||
| 137 | template <class T> | ||
| 138 | 1793 | void Write(const View& view, Field<T> field, const typename Identity<T>::Type& value) const { | |
| 139 | 1793 | const ColumnRef column = field.Column(); | |
| 140 | if constexpr (std::is_same_v<T, std::string>) { | ||
| 141 | 35 | view.Get<Token>(column).Set(Intern(value)); | |
| 142 | } else if constexpr (std::is_same_v<T, std::vector<std::string>>) { | ||
| 143 | 806 | UntypedAttribute values = view.Get(column); | |
| 144 | 806 | if (column.column->type == ColumnType::TokenArray) { | |
| 145 | 522 | values.SetTokens(0, value); | |
| 146 | } else { | ||
| 147 | 284 | values.SetTargets(0, value); | |
| 148 | } | ||
| 149 | 806 | } else if constexpr (std::is_same_v<T, std::vector<Primvar>>) { | |
| 150 | 9 | view.Get(column).SetPrimvars(0, value); | |
| 151 | } else if constexpr (HasItems<T>::value) { | ||
| 152 | 162 | view.GetArray<typename T::value_type>(column).Set( | |
| 153 | 0, Span<const typename T::value_type>(value)); | ||
| 154 | } else { | ||
| 155 | 781 | view.Get<T>(column).Set(value); | |
| 156 | } | ||
| 157 | 1792 | } | |
| 158 | |||
| 159 | void Check() const; | ||
| 160 | View Single() const; | ||
| 161 | View Single(SchemaId api, std::string_view instance) const; | ||
| 162 | Token Intern(std::string_view text) const; | ||
| 163 | std::string_view Text(Token token) const; | ||
| 164 | |||
| 165 | Scene* m_scene = nullptr; | ||
| 166 | uint32_t m_index = kNoPrim; | ||
| 167 | uint32_t m_generation = 0; | ||
| 168 | }; | ||
| 169 | |||
| 170 | } | ||
| 171 |