MayaFlux 0.2.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
EigenInsertion.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "NDData.hpp"
4#include <Eigen/Dense>
5
6namespace MayaFlux::Kakshya {
7
8/**
9 * @enum MatrixInterpretation
10 * @brief How to interpret Eigen matrix rows/columns
11 */
12enum class MatrixInterpretation : uint8_t {
13 SCALAR, ///< Single row → scalar values
14 COMPLEX, ///< 2 rows → complex (row 0 = real, row 1 = imag)
15 VEC2, ///< 2 rows → glm::vec2
16 VEC3, ///< 3 rows → glm::vec3
17 VEC4, ///< 4 rows → glm::vec4
18 MAT4, ///< 16 rows → glm::mat4 (flattened)
19 AUTO ///< Infer from row count
20};
21
22/**
23 * @class EigenInsertion
24 * @brief Type-erased writer for converting Eigen types to DataVariant
25 *
26 * Companion to EigenAccess for write operations.
27 * Provides semantic conversion from Eigen to NDData.
28 *
29 * Design principle: Matrix structure determines output type.
30 * User can override interpretation for ambiguous cases.
31 */
32class MAYAFLUX_API EigenInsertion {
33public:
35 : m_variant(variant)
36 {
37 }
38
39 /**
40 * @brief Insert Eigen vector as scalar data
41 * @param vec Source vector
42 * @param preserve_precision If true, use double; if false, convert to float
43 */
44 void insert_vector(
45 const Eigen::VectorXd& vec,
46 bool preserve_precision = true);
47
48 /**
49 * @brief Insert Eigen matrix with automatic interpretation
50 * @param matrix Source matrix (columns are data points)
51 * @param interpretation How to interpret matrix structure
52 * @param preserve_precision If true, use double for scalars
53 *
54 * AUTO interpretation rules:
55 * - 1 row → scalar (double or float based on preserve_precision)
56 * - 2 rows → complex<double> or glm::vec2 (use interpretation to specify)
57 * - 3 rows → glm::vec3
58 * - 4 rows → glm::vec4
59 * - 16 rows → glm::mat4
60 * - Other → error (must specify interpretation)
61 */
62 void insert_matrix(
63 const Eigen::MatrixXd& matrix,
64 MatrixInterpretation interpretation = MatrixInterpretation::AUTO,
65 bool preserve_precision = true);
66
67 /**
68 * @brief Clear variant data
69 */
70 void clear();
71
72private:
74
75 static constexpr Eigen::Index get_expected_rows(MatrixInterpretation interp);
76
77 void validate_matrix_dimensions(
78 const Eigen::MatrixXd& matrix,
79 MatrixInterpretation interpretation) const;
80};
81
82/**
83 * @brief Convenience function for direct conversion
84 * @param matrix Input Eigen matrix
85 * @param interpretation How to interpret structure
86 * @return DataVariant representation
87 */
89 const Eigen::MatrixXd& matrix,
91{
93 EigenInsertion insertion(variant);
94 insertion.insert_matrix(matrix, interpretation);
95 return variant;
96}
97
98/**
99 * @brief Convenience function for vector conversion
100 * @param vec Input Eigen vector
101 * @return DataVariant representation (vector<double>)
102 */
103inline Kakshya::DataVariant from_eigen_vector(const Eigen::VectorXd& vec)
104{
105 Kakshya::DataVariant variant;
106 EigenInsertion insertion(variant);
107 insertion.insert_vector(vec);
108 return variant;
109}
110
111} // namespace MayaFlux::Kakshya
EigenInsertion(Kakshya::DataVariant &variant)
void insert_vector(const Eigen::VectorXd &vec, bool preserve_precision=true)
Insert Eigen vector as scalar data.
void insert_matrix(const Eigen::MatrixXd &matrix, MatrixInterpretation interpretation=MatrixInterpretation::AUTO, bool preserve_precision=true)
Insert Eigen matrix with automatic interpretation.
Type-erased writer for converting Eigen types to DataVariant.
Kakshya::DataVariant from_eigen_matrix(const Eigen::MatrixXd &matrix, MatrixInterpretation interpretation=MatrixInterpretation::AUTO)
Convenience function for direct conversion.
Kakshya::DataVariant from_eigen_vector(const Eigen::VectorXd &vec)
Convenience function for vector conversion.
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:73
MatrixInterpretation
How to interpret Eigen matrix rows/columns.
@ MAT4
16 rows → glm::mat4 (flattened)
@ SCALAR
Single row → scalar values.
@ COMPLEX
2 rows → complex (row 0 = real, row 1 = imag)