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