source: flair-src/trunk/lib/FlairCore/src/Matrix.cpp@ 448

Last change on this file since 448 was 439, checked in by Sanahuja Guillaume, 3 years ago

refactor

File size: 6.1 KB
Line 
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
22using std::string;
23
24namespace flair {
25namespace core {
26/*! \class MatrixElement
27*/
28class MatrixElement : public IODataElement {
29public:
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()) == typeid(FloatType)) {
56 float value = matrix->Value(row, col);
57 memcpy(dst, &value, sizeof(value));
58 } else if (typeid(matrix->GetDataType().GetElementDataType()) == typeid(SignedIntegerType)) {
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 }
69 } else if (typeid(matrix->GetDataType().GetElementDataType()) == typeid(UnsignedIntegerType)) {
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 }
80 }
81 }
82
83 DataType const &GetDataType(void) const {
84 return matrix->GetDataType().GetElementDataType();
85 }
86
87private:
88 const Matrix *matrix;
89 uint32_t row, col;
90};
91
92Matrix::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++) {
99 AppendLogDescription(pimpl_->descriptor->ElementName(i, j),elementDataType);
100 SetValue(i, j, 0);
101 }
102 }
103}
104
105Matrix::Matrix(const Object *parent, const MatrixDescriptor *descriptor,
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
119Matrix::~Matrix() { delete pimpl_; }
120
121IODataElement *Matrix::Element(uint32_t row, uint32_t col) const {
122 return new MatrixElement(this, Name(row, col), row, col);
123}
124
125IODataElement *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
136float Matrix::Value(uint32_t row, uint32_t col) const {
137 float value;
138
139 if (row >= (uint32_t)pimpl_->descriptor->Rows() || col >= (uint32_t)pimpl_->descriptor->Cols()) {
140 Warn("index (%i,%i) out of bound, max (%i,%i)\n", row, col,
141 pimpl_->descriptor->Rows() - 1, pimpl_->descriptor->Cols() - 1);
142 return 0;
143 }
144
145 GetMutex();
146 value = pimpl_->ValueNoMutex(row, col);
147 ReleaseMutex();
148
149 return value;
150}
151
152float Matrix::ValueNoMutex(uint32_t row, uint32_t col) const {
153 if (row >= (uint32_t)pimpl_->descriptor->Rows() || col >= (uint32_t)pimpl_->descriptor->Cols()) {
154 Warn("index (%i,%i) out of bound, max (%i,%i)\n", row, col,
155 pimpl_->descriptor->Rows() - 1, pimpl_->descriptor->Cols() - 1);
156 return 0;
157 }
158
159 return pimpl_->ValueNoMutex(row, col);
160}
161
162void Matrix::SetValue(uint32_t row, uint32_t col, float value) {
163 if (row >= (uint32_t)pimpl_->descriptor->Rows() || col >= (uint32_t)pimpl_->descriptor->Cols()) {
164 Warn("index (%i,%i) out of bound, max (%i,%i)\n", row, col,
165 pimpl_->descriptor->Rows() - 1, pimpl_->descriptor->Cols() - 1);
166 } else {
167 GetMutex();
168 pimpl_->SetValueNoMutex(row, col,value);
169 ReleaseMutex();
170 }
171}
172
173void Matrix::SetValueNoMutex(uint32_t row, uint32_t col, float value) {
174 if (row >= (uint32_t)pimpl_->descriptor->Rows() || col >= (uint32_t)pimpl_->descriptor->Cols()) {
175 Warn("index (%i,%i) out of bound, max (%i,%i)\n", row, col,
176 pimpl_->descriptor->Rows() - 1, pimpl_->descriptor->Cols() - 1);
177 } else {
178 pimpl_->SetValueNoMutex(row, col,value);
179 }
180}
181
182void Matrix::RawRead(char *dst) const {
183 GetMutex();
184 memcpy(dst, pimpl_->datas, dataType.GetSize());
185 ReleaseMutex();
186}
187
188void Matrix::RawWrite(char *src) {
189 GetMutex();
190 memcpy(pimpl_->datas, src,dataType.GetSize());
191 ReleaseMutex();
192}
193
194uint32_t Matrix::Rows(void) const { return pimpl_->descriptor->Rows(); }
195
196uint32_t Matrix::Cols(void) const { return pimpl_->descriptor->Cols(); }
197
198string 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
Note: See TracBrowser for help on using the repository browser.