MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
DataSpec.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "Eigen/Core"
4
7
8namespace MayaFlux::Kakshya {
9class CompositeArray;
10class CompositeContainer;
11}
12
13namespace MayaFlux::Yantra {
14
15// =============================================================================
16// VariantVector concept
17// =============================================================================
18
19template <typename T>
20concept VariantVector = requires {
21 typename T::value_type;
22 requires std::same_as<T, std::vector<typename T::value_type>>;
23 requires std::constructible_from<Kakshya::DataVariant, typename T::value_type>;
24};
25
26// =============================================================================
27// Eigen matrix trait
28// =============================================================================
29
30template <typename T>
31struct is_eigen_matrix : std::false_type { };
32
33template <typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
34struct is_eigen_matrix<Eigen::Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols>> : std::true_type { };
35
36template <typename T>
38
39/** @brief An owning array of schema-bearing Composite elements. */
40template <typename T>
41concept CompositeArrayData = std::same_as<T, Kakshya::CompositeArray>;
42
43/** @brief Shared ownership of a CompositeContainer. */
44template <typename T>
45concept CompositeContainerData = std::same_as<T, std::shared_ptr<Kakshya::CompositeContainer>>;
46
47/** @brief Composite data that retains its field layout and presence information. */
48template <typename T>
50
51// =============================================================================
52// ComputeData concept
53// =============================================================================
54
55/**
56 * @concept ComputeData
57 * @brief Universal concept for types that can flow through the computation pipeline.
58 *
59 * Valid types:
60 * - Kakshya::DataVariant
61 * - std::vector<Kakshya::DataVariant>
62 * - std::shared_ptr<Kakshya::SignalSourceContainer>
63 * - Kakshya::CompositeArray
64 * - std::shared_ptr<Kakshya::CompositeContainer>
65 * - Kakshya::Region
66 * - Kakshya::RegionGroup
67 * - std::vector<Kakshya::RegionSegment>
68 * - Any Eigen matrix type (any scalar)
69 * - Any VariantVector (std::vector<T> where T is constructible into DataVariant)
70 * - Any type constructible from Kakshya::DataVariant
71 */
72// clang-format off
73template <typename T>
74concept ComputeData =
76 std::same_as<T, Kakshya::DataVariant> ||
77 std::same_as<T, std::vector<Kakshya::DataVariant>> ||
78 std::same_as<T, std::shared_ptr<Kakshya::SignalSourceContainer>> ||
79 std::same_as<T, Kakshya::Region> ||
80 std::same_as<T, Kakshya::RegionGroup> ||
81 std::same_as<T, std::vector<Kakshya::RegionSegment>> ||
82 std::is_base_of_v<Eigen::MatrixBase<T>, T> ||
84 std::constructible_from<Kakshya::DataVariant, T>;
85// clang-format on
86
87// =============================================================================
88// Structural concepts
89// =============================================================================
90
91/**
92 * @concept RegionLike
93 * @brief Types that represent spatial or temporal markers requiring a container to resolve data.
94 */
95template <typename T>
96concept RegionLike = std::is_same_v<T, Kakshya::Region> || std::is_same_v<T, Kakshya::RegionGroup> || std::is_same_v<T, std::vector<Kakshya::RegionSegment>>;
97
98/**
99 * @concept MultiVariant
100 * @brief Types that yield multiple data channels on extraction.
101 */
102template <typename T>
103concept MultiVariant = std::is_same_v<T, std::vector<Kakshya::DataVariant>> || std::is_same_v<T, std::shared_ptr<Kakshya::SignalSourceContainer>> || RegionLike<T>;
104
105/**
106 * @concept RequiresContainer
107 * @brief Types that need an associated SignalSourceContainer to extract data.
108 */
109template <typename T>
111
112/**
113 * @concept EigenMatrixLike
114 * @brief Any Eigen matrix type, regardless of scalar type.
115 *
116 * The previous definition gated on Scalar == double, rejecting MatrixXf,
117 * Matrix3f, VectorXf, and all other non-double Eigen types. Widening to
118 * double for algorithm input is the algorithm's concern, not the concept's.
119 */
120template <typename T>
121concept EigenMatrixLike = is_eigen_matrix_v<T>;
122
123/**
124 * @concept SingleVariant
125 * @brief Single data source: one DataVariant, a column Eigen vector of any
126 * scalar type, or any type constructible from DataVariant that is not
127 * multi-variant or region-like.
128 */
129template <typename T>
130concept SingleVariant = std::is_same_v<T, Kakshya::DataVariant>
131 || (is_eigen_matrix_v<T> && T::ColsAtCompileTime == 1)
132 || (std::constructible_from<Kakshya::DataVariant, T>
133 && !std::is_same_v<T, std::vector<Kakshya::DataVariant>>
134 && !std::is_same_v<T, std::shared_ptr<Kakshya::SignalSourceContainer>>
135 && !RegionLike<T>
136 && !is_eigen_matrix_v<T>);
137
138// =============================================================================
139// extraction_traits_d -- algorithm (double) extraction path
140//
141// The _d suffix is intentional: these traits describe extraction for Kinesis
142// algorithm input, which always operates on double-precision spans. All
143// existing callers (analyzers, sorters, transformers) use this path unchanged.
144// =============================================================================
145
146/**
147 * @struct extraction_traits_d
148 * @brief Compile-time traits describing how to extract double-precision data
149 * from a given ComputeData type for use by Kinesis algorithms.
150 *
151 * result_type is always span<double> (single) or vector<span<double>> (multi).
152 * Widening from native types is performed at the extraction site.
153 */
154template <typename T>
156 static constexpr bool is_multi_variant = false;
157 static constexpr bool requires_container = false;
158 static constexpr bool is_region_like = false;
159 using result_type = std::span<double>;
161};
162
163template <>
164struct extraction_traits_d<Kakshya::DataVariant> {
165 static constexpr bool is_multi_variant = false;
166 static constexpr bool requires_container = false;
167 static constexpr bool is_region_like = false;
168 using result_type = std::span<double>;
170};
171
172template <>
173struct extraction_traits_d<std::vector<Kakshya::DataVariant>> {
174 static constexpr bool is_multi_variant = true;
175 static constexpr bool requires_container = false;
176 static constexpr bool is_region_like = false;
177 using result_type = std::vector<std::span<double>>;
178 using variant_result_type = std::vector<Kakshya::DataVariant>;
179};
180
181template <>
182struct extraction_traits_d<std::shared_ptr<Kakshya::SignalSourceContainer>> {
183 static constexpr bool is_multi_variant = true;
184 static constexpr bool requires_container = false;
185 static constexpr bool is_region_like = false;
186 using result_type = std::vector<std::span<double>>;
187 using variant_result_type = std::vector<Kakshya::DataVariant>;
188};
189
190template <>
191struct extraction_traits_d<Kakshya::Region> {
192 static constexpr bool is_multi_variant = true;
193 static constexpr bool requires_container = true;
194 static constexpr bool is_region_like = true;
195 using result_type = std::vector<std::span<double>>;
196 using variant_result_type = std::vector<Kakshya::DataVariant>;
197};
198
199template <>
200struct extraction_traits_d<Kakshya::RegionGroup> {
201 static constexpr bool is_multi_variant = true;
202 static constexpr bool requires_container = true;
203 static constexpr bool is_region_like = true;
204 using result_type = std::vector<std::span<double>>;
205 using variant_result_type = std::vector<Kakshya::DataVariant>;
206};
207
208template <>
209struct extraction_traits_d<std::vector<Kakshya::RegionSegment>> {
210 static constexpr bool is_multi_variant = true;
211 static constexpr bool requires_container = true;
212 static constexpr bool is_region_like = true;
213 using result_type = std::vector<std::span<double>>;
214 using variant_result_type = std::vector<Kakshya::DataVariant>;
215};
216
217/** @brief Composite data has no implicit double or DataVariant extraction. */
218template <CompositeData T>
220 static constexpr bool is_multi_variant = false;
221 static constexpr bool requires_container = false;
222 static constexpr bool is_region_like = false;
223 using result_type = void;
225};
226
227/**
228 * @brief extraction_traits_d for any Eigen matrix type.
229 *
230 * Covers MatrixXd, MatrixXf, VectorXf, Matrix3f, etc. result_type remains
231 * vector<span<double>> because Kinesis algorithms require double input;
232 * widening from T::Scalar happens at the extraction site.
233 */
234template <typename T>
235 requires is_eigen_matrix_v<T>
236struct extraction_traits_d<T> {
237 static constexpr bool is_multi_variant = true;
238 static constexpr bool requires_container = false;
239 static constexpr bool is_region_like = false;
240 using result_type = std::vector<std::span<double>>;
241 using variant_result_type = std::vector<Kakshya::DataVariant>;
242};
243
244// =============================================================================
245// extraction_traits -- native-type aware companion to extraction_traits_d
246//
247// Carries the same structural flags but also exposes:
248// native_element_type -- the scalar the source actually stores.
249// native_result_type -- zero-copy span in native type.
250// algorithm_result_type -- double span for Kinesis algorithm input.
251//
252// Use this when staging GPU buffers, roundtripping containers, or any path
253// that should not force conversion to double.
254// =============================================================================
255
256namespace detail {
257
258 template <typename T>
260 using type = double;
261 };
262
263 template <>
264 struct native_element<std::vector<Kakshya::DataVariant>> {
265 using type = void; ///< Heterogeneous -- visit the variant at runtime.
266 };
267
268 template <>
269 struct native_element<std::shared_ptr<Kakshya::SignalSourceContainer>> {
270 using type = void; ///< Runtime-determined via value_element_type().
271 };
272
273 template <typename T>
274 requires is_eigen_matrix_v<T>
275 struct native_element<T> {
276 using type = typename T::Scalar;
277 };
278
279 template <typename T>
280 requires RegionLike<T>
281 struct native_element<T> {
282 using type = double;
283 };
284
285} // namespace detail
286
287/**
288 * @struct extraction_traits
289 * @brief Native-type aware extraction traits.
290 *
291 * extraction_traits_d and its aliases are unchanged; this struct is purely
292 * additive. Callers that need native access (GPU staging, container
293 * roundtrips) use extraction_traits<T> or the native_result_t / native_element_t
294 * convenience aliases.
295 */
296template <typename T>
301
304
305 using native_result_type = std::conditional_t<
306 std::is_same_v<native_element_type, void>,
308 std::conditional_t<
310 std::vector<std::span<native_element_type>>,
311 std::span<native_element_type>>>;
312};
313
314template <>
315struct extraction_traits<Kakshya::DataVariant> {
316 static constexpr bool is_multi_variant = false;
317 static constexpr bool requires_container = false;
318 static constexpr bool is_region_like = false;
320 using algorithm_result_type = std::span<double>;
322};
323
324template <>
325struct extraction_traits<std::shared_ptr<Kakshya::SignalSourceContainer>> {
326 static constexpr bool is_multi_variant = true;
327 static constexpr bool requires_container = false;
328 static constexpr bool is_region_like = false;
330 using algorithm_result_type = std::vector<std::span<double>>;
332};
333
334/** @brief Declare borrowed CompositeArray as the native result type. */
335template <CompositeData T>
337 static constexpr bool is_multi_variant = false;
338 static constexpr bool requires_container = false;
339 static constexpr bool is_region_like = false;
343};
344
345template <typename T>
346 requires is_eigen_matrix_v<T>
347struct extraction_traits<T> {
348 static constexpr bool is_multi_variant = true;
349 static constexpr bool requires_container = false;
350 static constexpr bool is_region_like = false;
351 using native_element_type = typename T::Scalar;
352 using algorithm_result_type = std::vector<std::span<double>>;
353 using native_result_type = std::vector<std::span<native_element_type>>;
354};
355
356// =============================================================================
357// enable_if aliases -- SFINAE helpers
358// =============================================================================
359
360template <typename T>
361using enable_if_single_variant_t = std::enable_if_t<SingleVariant<T>>;
362
363template <typename T>
364using enable_if_multi_variant_t = std::enable_if_t<extraction_traits_d<T>::is_multi_variant>;
365
366template <typename T>
367using enable_if_region_like_t = std::enable_if_t<extraction_traits_d<T>::is_region_like>;
368
369template <typename T>
370using enable_if_multi_no_container_t = std::enable_if_t<
372
373template <typename T>
374using enable_if_multi_with_container_t = std::enable_if_t<
376
377template <typename T>
378using enable_if_eigen_matrix_t = std::enable_if_t<is_eigen_matrix_v<T>>;
379
380// =============================================================================
381// Type aliases
382// =============================================================================
383
384/// Algorithm (double) result type for T -- for Kinesis callers.
385template <typename T>
387
388/// Variant result type for T.
389template <typename T>
391
392/// Native result type for T -- zero-copy in the source's own scalar type.
393template <typename T>
395
396/// Native element type for T.
397template <typename T>
399
400} // namespace MayaFlux::Yantra
Owns a sequence of values that share a CompositeLayout.
An owning array of schema-bearing Composite elements.
Definition DataSpec.hpp:41
Shared ownership of a CompositeContainer.
Definition DataSpec.hpp:45
Composite data that retains its field layout and presence information.
Definition DataSpec.hpp:49
Universal concept for types that can flow through the computation pipeline.
Definition DataSpec.hpp:74
Any Eigen matrix type, regardless of scalar type.
Definition DataSpec.hpp:121
Types that yield multiple data channels on extraction.
Definition DataSpec.hpp:103
Types that represent spatial or temporal markers requiring a container to resolve data.
Definition DataSpec.hpp:96
Types that need an associated SignalSourceContainer to extract data.
Definition DataSpec.hpp:110
Single data source: one DataVariant, a column Eigen vector of any scalar type, or any type constructi...
Definition DataSpec.hpp:130
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:70
typename extraction_traits< T >::native_result_type native_result_t
Native result type for T – zero-copy in the source's own scalar type.
Definition DataSpec.hpp:394
std::enable_if_t< extraction_traits_d< T >::is_region_like > enable_if_region_like_t
Definition DataSpec.hpp:367
std::enable_if_t< is_eigen_matrix_v< T > > enable_if_eigen_matrix_t
Definition DataSpec.hpp:378
std::enable_if_t< extraction_traits_d< T >::is_multi_variant > enable_if_multi_variant_t
Definition DataSpec.hpp:364
typename extraction_traits< T >::native_element_type native_element_t
Native element type for T.
Definition DataSpec.hpp:398
typename extraction_traits_d< T >::result_type extraction_result_t
Algorithm (double) result type for T – for Kinesis callers.
Definition DataSpec.hpp:386
std::enable_if_t< extraction_traits_d< T >::is_multi_variant &&!extraction_traits_d< T >::requires_container > enable_if_multi_no_container_t
Definition DataSpec.hpp:371
constexpr bool is_eigen_matrix_v
Definition DataSpec.hpp:37
std::enable_if_t< SingleVariant< T > > enable_if_single_variant_t
Definition DataSpec.hpp:361
typename extraction_traits_d< T >::variant_result_type variant_result_t
Variant result type for T.
Definition DataSpec.hpp:390
std::enable_if_t< extraction_traits_d< T >::is_multi_variant &&extraction_traits_d< T >::requires_container > enable_if_multi_with_container_t
Definition DataSpec.hpp:375
std::vector< Kakshya::DataVariant > variant_result_type
Definition DataSpec.hpp:205
std::vector< Kakshya::DataVariant > variant_result_type
Definition DataSpec.hpp:196
std::vector< std::span< double > > result_type
Definition DataSpec.hpp:195
static constexpr bool requires_container
Definition DataSpec.hpp:157
Kakshya::DataVariant variant_result_type
Definition DataSpec.hpp:160
static constexpr bool is_region_like
Definition DataSpec.hpp:158
static constexpr bool is_multi_variant
Definition DataSpec.hpp:156
Compile-time traits describing how to extract double-precision data from a given ComputeData type for...
Definition DataSpec.hpp:155
static constexpr bool requires_container
Definition DataSpec.hpp:299
static constexpr bool is_multi_variant
Definition DataSpec.hpp:298
static constexpr bool is_region_like
Definition DataSpec.hpp:300
typename detail::native_element< T >::type native_element_type
Definition DataSpec.hpp:302
std::conditional_t< std::is_same_v< native_element_type, void >, algorithm_result_type, std::conditional_t< extraction_traits_d< T >::is_multi_variant, std::vector< std::span< native_element_type > >, std::span< native_element_type > > > native_result_type
Definition DataSpec.hpp:311
typename extraction_traits_d< T >::result_type algorithm_result_type
Definition DataSpec.hpp:303
Native-type aware extraction traits.
Definition DataSpec.hpp:297