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()) {
|
---|
37 | matrix->Err("index (%i,%i) out of bound, max (%i,%i)\n", row, col,
|
---|
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 {
|
---|
55 | if (typeid(matrix->GetDataType().GetElementDataType()) ==
|
---|
56 | typeid(FloatType)) {
|
---|
57 | float value = matrix->Value(row, col);
|
---|
58 | memcpy(dst, &value, sizeof(value));
|
---|
59 | } else if (typeid(matrix->GetDataType().GetElementDataType()) ==
|
---|
60 | typeid(SignedIntegerType)) {
|
---|
61 | switch (matrix->GetDataType().GetElementDataType().GetSize()) {
|
---|
62 | case 1: {
|
---|
63 | int8_t int8Value = matrix->Value(row, col);
|
---|
64 | memcpy(dst, &int8Value, 1);
|
---|
65 | } break;
|
---|
66 | case 2: {
|
---|
67 | int16_t int16Value = matrix->Value(row, col);
|
---|
68 | memcpy(dst, &int16Value, 2);
|
---|
69 | } break;
|
---|
70 | }
|
---|
71 | } else if (typeid(matrix->GetDataType().GetElementDataType()) ==
|
---|
72 | typeid(UnsignedIntegerType)) {
|
---|
73 | switch (matrix->GetDataType().GetElementDataType().GetSize()) {
|
---|
74 | case 1: {
|
---|
75 | uint8_t uint8Value = matrix->Value(row, col);
|
---|
76 | memcpy(dst, &uint8Value, 1);
|
---|
77 | } break;
|
---|
78 | case 2: {
|
---|
79 | uint16_t uint16Value = matrix->Value(row, col);
|
---|
80 | memcpy(dst, &uint16Value, 2);
|
---|
81 | } break;
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | DataType const &GetDataType(void) const {
|
---|
87 | return matrix->GetDataType().GetElementDataType();
|
---|
88 | }
|
---|
89 |
|
---|
90 | private:
|
---|
91 | const Matrix *matrix;
|
---|
92 | uint32_t row, col;
|
---|
93 | };
|
---|
94 |
|
---|
95 | Matrix::Matrix(const Object *parent, uint32_t rows, uint32_t cols,
|
---|
96 | ScalarType const &elementDataType, string name, uint32_t n)
|
---|
97 | : io_data(parent, name, n), dataType(rows, cols, elementDataType) {
|
---|
98 | pimpl_ = new Matrix_impl(this, rows, cols, elementDataType, n);
|
---|
99 |
|
---|
100 | for (uint32_t i = 0; i < rows; i++) {
|
---|
101 | for (uint32_t j = 0; j < cols; j++) {
|
---|
102 | AppendLogDescription(pimpl_->descriptor->ElementName(i, j),
|
---|
103 | elementDataType);
|
---|
104 | SetValue(i, j, 0);
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | Matrix::Matrix(const Object *parent, const MatrixDescriptor *descriptor,
|
---|
110 | ScalarType const &elementDataType, string name, uint32_t n)
|
---|
111 | : io_data(parent, name, n),
|
---|
112 | dataType(descriptor->Rows(), descriptor->Cols(), elementDataType) {
|
---|
113 | pimpl_ = new Matrix_impl(this, descriptor, elementDataType, n);
|
---|
114 |
|
---|
115 | for (uint32_t i = 0; i < descriptor->Rows(); i++) {
|
---|
116 | for (uint32_t j = 0; j < descriptor->Cols(); j++) {
|
---|
117 | AppendLogDescription(descriptor->ElementName(i, j), elementDataType);
|
---|
118 | SetValue(i, j, 0);
|
---|
119 | }
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | Matrix::~Matrix() { delete pimpl_; }
|
---|
124 |
|
---|
125 | IODataElement *Matrix::Element(uint32_t row, uint32_t col) const {
|
---|
126 | return new MatrixElement(this, Name(row, col), row, col);
|
---|
127 | }
|
---|
128 |
|
---|
129 | IODataElement *Matrix::Element(uint32_t index) const {
|
---|
130 | if (Rows() == 1) {
|
---|
131 | return new MatrixElement(this, Name(0, index), 0, index);
|
---|
132 | } else if (Cols() == 1) {
|
---|
133 | return new MatrixElement(this, Name(index, 0), index, 0);
|
---|
134 | } else {
|
---|
135 | Err("matrix is not 1D\n");
|
---|
136 | return nullptr;
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | float Matrix::Value(uint32_t row, uint32_t col) const {
|
---|
141 | float value;
|
---|
142 |
|
---|
143 | if (row >= (uint32_t)pimpl_->descriptor->Rows() ||
|
---|
144 | col >= (uint32_t)pimpl_->descriptor->Cols()) {
|
---|
145 | Warn("index (%i,%i) out of bound, max (%i,%i)\n", row, col,
|
---|
146 | pimpl_->descriptor->Rows() - 1, pimpl_->descriptor->Cols() - 1);
|
---|
147 | return 0;
|
---|
148 | }
|
---|
149 |
|
---|
150 | GetMutex();
|
---|
151 | value = pimpl_->ValueNoMutex(row, col);
|
---|
152 | ReleaseMutex();
|
---|
153 |
|
---|
154 | return value;
|
---|
155 | }
|
---|
156 |
|
---|
157 | float Matrix::ValueNoMutex(uint32_t row, uint32_t col) const {
|
---|
158 | if (row >= (uint32_t)pimpl_->descriptor->Rows() ||
|
---|
159 | col >= (uint32_t)pimpl_->descriptor->Cols()) {
|
---|
160 | Warn("index (%i,%i) out of bound, max (%i,%i)\n", row, col,
|
---|
161 | pimpl_->descriptor->Rows() - 1, pimpl_->descriptor->Cols() - 1);
|
---|
162 | return 0;
|
---|
163 | }
|
---|
164 |
|
---|
165 | return pimpl_->ValueNoMutex(row, col);
|
---|
166 | }
|
---|
167 |
|
---|
168 | void Matrix::SetValue(uint32_t row, uint32_t col, float value) {
|
---|
169 | if (row >= (uint32_t)pimpl_->descriptor->Rows() ||
|
---|
170 | col >= (uint32_t)pimpl_->descriptor->Cols()) {
|
---|
171 | Warn("index (%i,%i) out of bound, max (%i,%i)\n", row, col,
|
---|
172 | pimpl_->descriptor->Rows() - 1, pimpl_->descriptor->Cols() - 1);
|
---|
173 | } else {
|
---|
174 | GetMutex();
|
---|
175 | pimpl_->SetValueNoMutex(row, col,value);
|
---|
176 | ReleaseMutex();
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | void Matrix::SetValueNoMutex(uint32_t row, uint32_t col, float value) {
|
---|
181 | if (row >= (uint32_t)pimpl_->descriptor->Rows() ||
|
---|
182 | col >= (uint32_t)pimpl_->descriptor->Cols()) {
|
---|
183 | Warn("index (%i,%i) out of bound, max (%i,%i)\n", row, col,
|
---|
184 | pimpl_->descriptor->Rows() - 1, pimpl_->descriptor->Cols() - 1);
|
---|
185 | } else {
|
---|
186 | pimpl_->SetValueNoMutex(row, col,value);
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | void Matrix::RawRead(char *dst) const {
|
---|
191 | GetMutex();
|
---|
192 | memcpy(dst, pimpl_->datas, dataType.GetSize());
|
---|
193 | ReleaseMutex();
|
---|
194 | }
|
---|
195 |
|
---|
196 | void Matrix::RawWrite(char *src) {
|
---|
197 | GetMutex();
|
---|
198 | memcpy(pimpl_->datas, src,dataType.GetSize());
|
---|
199 | ReleaseMutex();
|
---|
200 | }
|
---|
201 |
|
---|
202 | uint32_t Matrix::Rows(void) const { return pimpl_->descriptor->Rows(); }
|
---|
203 |
|
---|
204 | uint32_t Matrix::Cols(void) const { return pimpl_->descriptor->Cols(); }
|
---|
205 |
|
---|
206 | string Matrix::Name(uint32_t row, uint32_t col) const {
|
---|
207 | return pimpl_->descriptor->ElementName(row, col);
|
---|
208 | }
|
---|
209 |
|
---|
210 | } // end namespace core
|
---|
211 | } // end namespace flair
|
---|