[213] | 1 | // %flair:license{
|
---|
| 2 | // This file is part of the Flair framework distributed under the
|
---|
| 3 | // CECILL-C License, Version 1.0.
|
---|
| 4 | // %flair:license}
|
---|
| 5 | // created: 2018/01/30
|
---|
| 6 | // filename: Matrix.cpp
|
---|
| 7 | //
|
---|
| 8 | // author: Guillaume Sanahuja
|
---|
| 9 | // Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
| 10 | //
|
---|
| 11 | // version: $Id: $
|
---|
| 12 | //
|
---|
| 13 | // purpose: Class defining a matrix
|
---|
| 14 | //
|
---|
| 15 | /*********************************************************************/
|
---|
| 16 |
|
---|
| 17 | #include "Matrix.h"
|
---|
| 18 | #include "Matrix_impl.h"
|
---|
| 19 | #include <typeinfo>
|
---|
| 20 | #include <string.h>
|
---|
| 21 |
|
---|
| 22 | using std::string;
|
---|
| 23 |
|
---|
| 24 | namespace flair {
|
---|
| 25 | namespace core {
|
---|
| 26 | /*! \class MatrixElement
|
---|
| 27 | */
|
---|
| 28 | class MatrixElement : public IODataElement {
|
---|
| 29 | public:
|
---|
| 30 | MatrixElement(const Matrix *matrix, string name, uint32_t row,
|
---|
| 31 | uint32_t col)
|
---|
| 32 | : IODataElement(matrix, name) {
|
---|
| 33 | this->matrix = matrix;
|
---|
| 34 | this->row = row;
|
---|
| 35 | this->col = col;
|
---|
| 36 | if (row >= matrix->Rows() || col >= matrix->Cols()) {
|
---|
[214] | 37 | matrix->Err("index (%i,%i) out of bound, max (%i,%i)\n", row, col,
|
---|
[213] | 38 | matrix->Rows() - 1, matrix->Cols() - 1);
|
---|
| 39 | size = 0;
|
---|
| 40 | } else {
|
---|
| 41 | try {
|
---|
| 42 | ScalarType const &scalarType = dynamic_cast<ScalarType const &>(
|
---|
| 43 | matrix->GetDataType().GetElementDataType());
|
---|
| 44 | size = scalarType.GetSize();
|
---|
| 45 | } catch (std::bad_cast e) {
|
---|
| 46 | matrix->Err("type not handled\n");
|
---|
| 47 | size = 0;
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | ~MatrixElement() {}
|
---|
| 53 |
|
---|
| 54 | void CopyData(char *dst) const {
|
---|
[439] | 55 | if (typeid(matrix->GetDataType().GetElementDataType()) == typeid(FloatType)) {
|
---|
[213] | 56 | float value = matrix->Value(row, col);
|
---|
| 57 | memcpy(dst, &value, sizeof(value));
|
---|
[439] | 58 | } else if (typeid(matrix->GetDataType().GetElementDataType()) == typeid(SignedIntegerType)) {
|
---|
[213] | 59 | switch (matrix->GetDataType().GetElementDataType().GetSize()) {
|
---|
| 60 | case 1: {
|
---|
| 61 | int8_t int8Value = matrix->Value(row, col);
|
---|
| 62 | memcpy(dst, &int8Value, 1);
|
---|
| 63 | } break;
|
---|
| 64 | case 2: {
|
---|
| 65 | int16_t int16Value = matrix->Value(row, col);
|
---|
| 66 | memcpy(dst, &int16Value, 2);
|
---|
| 67 | } break;
|
---|
| 68 | }
|
---|
[439] | 69 | } else if (typeid(matrix->GetDataType().GetElementDataType()) == typeid(UnsignedIntegerType)) {
|
---|
[214] | 70 | switch (matrix->GetDataType().GetElementDataType().GetSize()) {
|
---|
| 71 | case 1: {
|
---|
| 72 | uint8_t uint8Value = matrix->Value(row, col);
|
---|
| 73 | memcpy(dst, &uint8Value, 1);
|
---|
| 74 | } break;
|
---|
| 75 | case 2: {
|
---|
| 76 | uint16_t uint16Value = matrix->Value(row, col);
|
---|
| 77 | memcpy(dst, &uint16Value, 2);
|
---|
| 78 | } break;
|
---|
| 79 | }
|
---|
[213] | 80 | }
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | DataType const &GetDataType(void) const {
|
---|
| 84 | return matrix->GetDataType().GetElementDataType();
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | private:
|
---|
| 88 | const Matrix *matrix;
|
---|
| 89 | uint32_t row, col;
|
---|
| 90 | };
|
---|
| 91 |
|
---|
| 92 | Matrix::Matrix(const Object *parent, uint32_t rows, uint32_t cols,
|
---|
| 93 | ScalarType const &elementDataType, string name, uint32_t n)
|
---|
| 94 | : io_data(parent, name, n), dataType(rows, cols, elementDataType) {
|
---|
| 95 | pimpl_ = new Matrix_impl(this, rows, cols, elementDataType, n);
|
---|
| 96 |
|
---|
| 97 | for (uint32_t i = 0; i < rows; i++) {
|
---|
| 98 | for (uint32_t j = 0; j < cols; j++) {
|
---|
[439] | 99 | AppendLogDescription(pimpl_->descriptor->ElementName(i, j),elementDataType);
|
---|
[213] | 100 | SetValue(i, j, 0);
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[318] | 105 | Matrix::Matrix(const Object *parent, const MatrixDescriptor *descriptor,
|
---|
[213] | 106 | ScalarType const &elementDataType, string name, uint32_t n)
|
---|
| 107 | : io_data(parent, name, n),
|
---|
| 108 | dataType(descriptor->Rows(), descriptor->Cols(), elementDataType) {
|
---|
| 109 | pimpl_ = new Matrix_impl(this, descriptor, elementDataType, n);
|
---|
| 110 |
|
---|
| 111 | for (uint32_t i = 0; i < descriptor->Rows(); i++) {
|
---|
| 112 | for (uint32_t j = 0; j < descriptor->Cols(); j++) {
|
---|
| 113 | AppendLogDescription(descriptor->ElementName(i, j), elementDataType);
|
---|
| 114 | SetValue(i, j, 0);
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | Matrix::~Matrix() { delete pimpl_; }
|
---|
| 120 |
|
---|
| 121 | IODataElement *Matrix::Element(uint32_t row, uint32_t col) const {
|
---|
| 122 | return new MatrixElement(this, Name(row, col), row, col);
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | IODataElement *Matrix::Element(uint32_t index) const {
|
---|
| 126 | if (Rows() == 1) {
|
---|
| 127 | return new MatrixElement(this, Name(0, index), 0, index);
|
---|
| 128 | } else if (Cols() == 1) {
|
---|
| 129 | return new MatrixElement(this, Name(index, 0), index, 0);
|
---|
| 130 | } else {
|
---|
| 131 | Err("matrix is not 1D\n");
|
---|
| 132 | return nullptr;
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | float Matrix::Value(uint32_t row, uint32_t col) const {
|
---|
| 137 | float value;
|
---|
| 138 |
|
---|
[439] | 139 | if (row >= (uint32_t)pimpl_->descriptor->Rows() || col >= (uint32_t)pimpl_->descriptor->Cols()) {
|
---|
[214] | 140 | Warn("index (%i,%i) out of bound, max (%i,%i)\n", row, col,
|
---|
[213] | 141 | pimpl_->descriptor->Rows() - 1, pimpl_->descriptor->Cols() - 1);
|
---|
| 142 | return 0;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | GetMutex();
|
---|
[214] | 146 | value = pimpl_->ValueNoMutex(row, col);
|
---|
[213] | 147 | ReleaseMutex();
|
---|
| 148 |
|
---|
| 149 | return value;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | float Matrix::ValueNoMutex(uint32_t row, uint32_t col) const {
|
---|
[439] | 153 | if (row >= (uint32_t)pimpl_->descriptor->Rows() || col >= (uint32_t)pimpl_->descriptor->Cols()) {
|
---|
[214] | 154 | Warn("index (%i,%i) out of bound, max (%i,%i)\n", row, col,
|
---|
[213] | 155 | pimpl_->descriptor->Rows() - 1, pimpl_->descriptor->Cols() - 1);
|
---|
| 156 | return 0;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[214] | 159 | return pimpl_->ValueNoMutex(row, col);
|
---|
[213] | 160 | }
|
---|
| 161 |
|
---|
| 162 | void Matrix::SetValue(uint32_t row, uint32_t col, float value) {
|
---|
[439] | 163 | if (row >= (uint32_t)pimpl_->descriptor->Rows() || col >= (uint32_t)pimpl_->descriptor->Cols()) {
|
---|
[214] | 164 | Warn("index (%i,%i) out of bound, max (%i,%i)\n", row, col,
|
---|
[213] | 165 | pimpl_->descriptor->Rows() - 1, pimpl_->descriptor->Cols() - 1);
|
---|
| 166 | } else {
|
---|
| 167 | GetMutex();
|
---|
[214] | 168 | pimpl_->SetValueNoMutex(row, col,value);
|
---|
[213] | 169 | ReleaseMutex();
|
---|
| 170 | }
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | void Matrix::SetValueNoMutex(uint32_t row, uint32_t col, float value) {
|
---|
[439] | 174 | if (row >= (uint32_t)pimpl_->descriptor->Rows() || col >= (uint32_t)pimpl_->descriptor->Cols()) {
|
---|
[214] | 175 | Warn("index (%i,%i) out of bound, max (%i,%i)\n", row, col,
|
---|
[213] | 176 | pimpl_->descriptor->Rows() - 1, pimpl_->descriptor->Cols() - 1);
|
---|
| 177 | } else {
|
---|
[214] | 178 | pimpl_->SetValueNoMutex(row, col,value);
|
---|
[213] | 179 | }
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[252] | 182 | void Matrix::RawRead(char *dst) const {
|
---|
[213] | 183 | GetMutex();
|
---|
| 184 | memcpy(dst, pimpl_->datas, dataType.GetSize());
|
---|
| 185 | ReleaseMutex();
|
---|
| 186 | }
|
---|
| 187 |
|
---|
[252] | 188 | void Matrix::RawWrite(char *src) {
|
---|
| 189 | GetMutex();
|
---|
| 190 | memcpy(pimpl_->datas, src,dataType.GetSize());
|
---|
| 191 | ReleaseMutex();
|
---|
| 192 | }
|
---|
| 193 |
|
---|
[213] | 194 | uint32_t Matrix::Rows(void) const { return pimpl_->descriptor->Rows(); }
|
---|
| 195 |
|
---|
| 196 | uint32_t Matrix::Cols(void) const { return pimpl_->descriptor->Cols(); }
|
---|
| 197 |
|
---|
| 198 | string Matrix::Name(uint32_t row, uint32_t col) const {
|
---|
| 199 | return pimpl_->descriptor->ElementName(row, col);
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | } // end namespace core
|
---|
| 203 | } // end namespace flair
|
---|