Added serializable properties to Vector, Matrix, Tensor3.

This commit is contained in:
Relintai 2023-04-29 12:47:45 +02:00
parent cd5d84733c
commit b0bd3344ad
6 changed files with 137 additions and 2 deletions

View File

@ -6,6 +6,51 @@
#include "../stat/stat.h"
#include <random>
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<real_t> &p_row) {
if (p_row.size() == 0) {
return;
@ -2952,6 +2997,10 @@ MLPPMatrix::MLPPMatrix(const std::vector<std::vector<real_t>> &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);

View File

@ -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;
}

View File

@ -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<real_t> &p_row) {
if (p_row.size() == 0) {
return;
@ -2228,6 +2273,10 @@ MLPPTensor3::MLPPTensor3(const std::vector<std::vector<std::vector<real_t>>> &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);

View File

@ -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;
}

View File

@ -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<real_t> &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);

View File

@ -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;
}