From b0bd3344adb9b7d4d5de4a59d0513b7db3315804 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 29 Apr 2023 12:47:45 +0200 Subject: [PATCH] Added serializable properties to Vector, Matrix, Tensor3. --- mlpp/lin_alg/mlpp_matrix.cpp | 49 +++++++++++++++++++++++++++++++++++ mlpp/lin_alg/mlpp_matrix.h | 3 +++ mlpp/lin_alg/mlpp_tensor3.cpp | 49 +++++++++++++++++++++++++++++++++++ mlpp/lin_alg/mlpp_tensor3.h | 3 +++ mlpp/lin_alg/mlpp_vector.cpp | 32 +++++++++++++++++++++-- mlpp/lin_alg/mlpp_vector.h | 3 +++ 6 files changed, 137 insertions(+), 2 deletions(-) diff --git a/mlpp/lin_alg/mlpp_matrix.cpp b/mlpp/lin_alg/mlpp_matrix.cpp index c143ea3..003a064 100644 --- a/mlpp/lin_alg/mlpp_matrix.cpp +++ b/mlpp/lin_alg/mlpp_matrix.cpp @@ -6,6 +6,51 @@ #include "../stat/stat.h" #include +Array MLPPMatrix::get_data() { + PoolRealArray pl; + + int ds = data_size(); + + if (ds) { + pl.resize(ds); + PoolRealArray::Write w = pl.write(); + real_t *dest = w.ptr(); + + for (int i = 0; i < ds; ++i) { + dest[i] = _data[i]; + } + } + + Array arr; + arr.push_back(size()); + arr.push_back(pl); + + return arr; +} +void MLPPMatrix::set_data(const Array &p_from) { + if (p_from.size() != 2) { + return; + } + + Size2i s = p_from[0]; + PoolRealArray pl = p_from[1]; + + int ds = s.x * s.y; + + if (ds != pl.size()) { + return; + } + + if (_size != s) { + resize(s); + } + + PoolRealArray::Read r = pl.read(); + for (int i = 0; i < ds; ++i) { + _data[i] = r[i]; + } +} + void MLPPMatrix::add_row(const Vector &p_row) { if (p_row.size() == 0) { return; @@ -2952,6 +2997,10 @@ MLPPMatrix::MLPPMatrix(const std::vector> &p_from) { } void MLPPMatrix::_bind_methods() { + ClassDB::bind_method(D_METHOD("get_data"), &MLPPMatrix::get_data); + ClassDB::bind_method(D_METHOD("set_data", "data"), &MLPPMatrix::set_data); + ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "data"), "set_data", "get_data"); + ClassDB::bind_method(D_METHOD("add_row", "row"), &MLPPMatrix::add_row_pool_vector); ClassDB::bind_method(D_METHOD("add_row_mlpp_vector", "row"), &MLPPMatrix::add_row_mlpp_vector); ClassDB::bind_method(D_METHOD("add_rows_mlpp_matrix", "other"), &MLPPMatrix::add_rows_mlpp_matrix); diff --git a/mlpp/lin_alg/mlpp_matrix.h b/mlpp/lin_alg/mlpp_matrix.h index c916fc7..4192ade 100644 --- a/mlpp/lin_alg/mlpp_matrix.h +++ b/mlpp/lin_alg/mlpp_matrix.h @@ -20,6 +20,9 @@ class MLPPMatrix : public Resource { GDCLASS(MLPPMatrix, Resource); public: + Array get_data(); + void set_data(const Array &p_from); + _FORCE_INLINE_ real_t *ptrw() { return _data; } diff --git a/mlpp/lin_alg/mlpp_tensor3.cpp b/mlpp/lin_alg/mlpp_tensor3.cpp index ecf418c..fdddde6 100644 --- a/mlpp/lin_alg/mlpp_tensor3.cpp +++ b/mlpp/lin_alg/mlpp_tensor3.cpp @@ -3,6 +3,51 @@ #include "core/io/image.h" +Array MLPPTensor3::get_data() { + PoolRealArray pl; + + int ds = data_size(); + + if (ds) { + pl.resize(ds); + PoolRealArray::Write w = pl.write(); + real_t *dest = w.ptr(); + + for (int i = 0; i < ds; ++i) { + dest[i] = _data[i]; + } + } + + Array arr; + arr.push_back(size()); + arr.push_back(pl); + + return arr; +} +void MLPPTensor3::set_data(const Array &p_from) { + if (p_from.size() != 2) { + return; + } + + Size3i s = p_from[0]; + PoolRealArray pl = p_from[1]; + + int ds = s.x * s.y * s.z; + + if (ds != pl.size()) { + return; + } + + if (_size != s) { + resize(s); + } + + PoolRealArray::Read r = pl.read(); + for (int i = 0; i < ds; ++i) { + _data[i] = r[i]; + } +} + void MLPPTensor3::add_z_slice(const Vector &p_row) { if (p_row.size() == 0) { return; @@ -2228,6 +2273,10 @@ MLPPTensor3::MLPPTensor3(const std::vector>> &p_ } void MLPPTensor3::_bind_methods() { + ClassDB::bind_method(D_METHOD("get_data"), &MLPPTensor3::get_data); + ClassDB::bind_method(D_METHOD("set_data", "data"), &MLPPTensor3::set_data); + ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "data"), "set_data", "get_data"); + ClassDB::bind_method(D_METHOD("add_z_slice_pool_vector", "row"), &MLPPTensor3::add_z_slice_pool_vector); ClassDB::bind_method(D_METHOD("add_z_slice_mlpp_vector", "row"), &MLPPTensor3::add_z_slice_mlpp_vector); ClassDB::bind_method(D_METHOD("add_z_slice_mlpp_matrix", "matrix"), &MLPPTensor3::add_z_slice_mlpp_matrix); diff --git a/mlpp/lin_alg/mlpp_tensor3.h b/mlpp/lin_alg/mlpp_tensor3.h index 53e8170..96b534e 100644 --- a/mlpp/lin_alg/mlpp_tensor3.h +++ b/mlpp/lin_alg/mlpp_tensor3.h @@ -21,6 +21,9 @@ class MLPPTensor3 : public Resource { GDCLASS(MLPPTensor3, Resource); public: + Array get_data(); + void set_data(const Array &p_from); + _FORCE_INLINE_ real_t *ptrw() { return _data; } diff --git a/mlpp/lin_alg/mlpp_vector.cpp b/mlpp/lin_alg/mlpp_vector.cpp index 3e113b6..c246fe8 100644 --- a/mlpp/lin_alg/mlpp_vector.cpp +++ b/mlpp/lin_alg/mlpp_vector.cpp @@ -3,6 +3,30 @@ #include "mlpp_matrix.h" +PoolRealArray MLPPVector::get_data() { + PoolRealArray pl; + if (size()) { + pl.resize(size()); + PoolRealArray::Write w = pl.write(); + real_t *dest = w.ptr(); + + for (int i = 0; i < size(); ++i) { + dest[i] = _data[i]; + } + } + return pl; +} +void MLPPVector::set_data(const PoolRealArray &p_from) { + if (_size != p_from.size()) { + resize(p_from.size()); + } + + PoolRealArray::Read r = p_from.read(); + for (int i = 0; i < _size; i++) { + _data[i] = r[i]; + } +} + void MLPPVector::push_back(real_t p_elem) { ++_size; @@ -166,7 +190,7 @@ PoolRealArray MLPPVector::to_pool_vector() const { PoolRealArray pl; if (size()) { pl.resize(size()); - typename PoolRealArray::Write w = pl.write(); + PoolRealArray::Write w = pl.write(); real_t *dest = w.ptr(); for (int i = 0; i < size(); ++i) { @@ -1293,7 +1317,7 @@ MLPPVector::MLPPVector(const PoolRealArray &p_from) { _data = NULL; resize(p_from.size()); - typename PoolRealArray::Read r = p_from.read(); + PoolRealArray::Read r = p_from.read(); for (int i = 0; i < _size; i++) { _data[i] = r[i]; } @@ -1331,6 +1355,10 @@ MLPPVector::MLPPVector(const std::vector &p_from) { } void MLPPVector::_bind_methods() { + ClassDB::bind_method(D_METHOD("get_data"), &MLPPVector::get_data); + ClassDB::bind_method(D_METHOD("set_data", "data"), &MLPPVector::set_data); + ADD_PROPERTY(PropertyInfo(Variant::POOL_REAL_ARRAY, "data"), "set_data", "get_data"); + ClassDB::bind_method(D_METHOD("push_back", "elem"), &MLPPVector::push_back); ClassDB::bind_method(D_METHOD("add_mlpp_vector", "other"), &MLPPVector::push_back); ClassDB::bind_method(D_METHOD("remove", "index"), &MLPPVector::remove); diff --git a/mlpp/lin_alg/mlpp_vector.h b/mlpp/lin_alg/mlpp_vector.h index 3dc1ce3..c3ee188 100644 --- a/mlpp/lin_alg/mlpp_vector.h +++ b/mlpp/lin_alg/mlpp_vector.h @@ -21,6 +21,9 @@ class MLPPVector : public Resource { GDCLASS(MLPPVector, Resource); public: + PoolRealArray get_data(); + void set_data(const PoolRealArray &p_from); + _FORCE_INLINE_ real_t *ptrw() { return _data; }