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

Last change on this file since 213 was 213, checked in by Sanahuja Guillaume, 6 years ago

thread stack size rework
add Matrix class

File size: 5.7 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
22//#include <iostream>
23
24using std::string;
25
26namespace flair {
27namespace core {
28/*! \class MatrixElement
29*/
30class MatrixElement : public IODataElement {
31public:
32 MatrixElement(const Matrix *matrix, string name, uint32_t row,
33 uint32_t col)
34 : IODataElement(matrix, name) {
35 this->matrix = matrix;
36 this->row = row;
37 this->col = col;
38 if (row >= matrix->Rows() || col >= matrix->Cols()) {
39 matrix->Err("index (%i,%i) out of bound (%i,%i)\n", row, col,
40 matrix->Rows() - 1, matrix->Cols() - 1);
41 size = 0;
42 } else {
43 try {
44 ScalarType const &scalarType = dynamic_cast<ScalarType const &>(
45 matrix->GetDataType().GetElementDataType());
46 size = scalarType.GetSize();
47 } catch (std::bad_cast e) {
48 matrix->Err("type not handled\n");
49 size = 0;
50 }
51 }
52 }
53
54 ~MatrixElement() {}
55
56 void CopyData(char *dst) const {
57 if (typeid(matrix->GetDataType().GetElementDataType()) ==
58 typeid(FloatType)) {
59 float value = matrix->Value(row, col);
60 memcpy(dst, &value, sizeof(value));
61 } else if (typeid(matrix->GetDataType().GetElementDataType()) ==
62 typeid(SignedIntegerType)) {
63 switch (matrix->GetDataType().GetElementDataType().GetSize()) {
64 case 1: {
65 int8_t int8Value = matrix->Value(row, col);
66 memcpy(dst, &int8Value, 1);
67 } break;
68 case 2: {
69 int16_t int16Value = matrix->Value(row, col);
70 memcpy(dst, &int16Value, 2);
71 } break;
72 }
73 }
74 }
75
76 DataType const &GetDataType(void) const {
77 return matrix->GetDataType().GetElementDataType();
78 }
79
80private:
81 const Matrix *matrix;
82 uint32_t row, col;
83};
84
85Matrix::Matrix(const Object *parent, uint32_t rows, uint32_t cols,
86 ScalarType const &elementDataType, string name, uint32_t n)
87 : io_data(parent, name, n), dataType(rows, cols, elementDataType) {
88 pimpl_ = new Matrix_impl(this, rows, cols, elementDataType, n);
89
90 for (uint32_t i = 0; i < rows; i++) {
91 for (uint32_t j = 0; j < cols; j++) {
92 AppendLogDescription(pimpl_->descriptor->ElementName(i, j),
93 elementDataType);
94 SetValue(i, j, 0);
95 }
96 }
97}
98
99Matrix::Matrix(const Object *parent, const cvmatrix_descriptor *descriptor,
100 ScalarType const &elementDataType, string name, uint32_t n)
101 : io_data(parent, name, n),
102 dataType(descriptor->Rows(), descriptor->Cols(), elementDataType) {
103 pimpl_ = new Matrix_impl(this, descriptor, elementDataType, n);
104
105 for (uint32_t i = 0; i < descriptor->Rows(); i++) {
106 for (uint32_t j = 0; j < descriptor->Cols(); j++) {
107 AppendLogDescription(descriptor->ElementName(i, j), elementDataType);
108 SetValue(i, j, 0);
109 }
110 }
111}
112
113Matrix::~Matrix() { delete pimpl_; }
114
115IODataElement *Matrix::Element(uint32_t row, uint32_t col) const {
116 return new MatrixElement(this, Name(row, col), row, col);
117}
118
119IODataElement *Matrix::Element(uint32_t index) const {
120 if (Rows() == 1) {
121 return new MatrixElement(this, Name(0, index), 0, index);
122 } else if (Cols() == 1) {
123 return new MatrixElement(this, Name(index, 0), index, 0);
124 } else {
125 Err("matrix is not 1D\n");
126 return nullptr;
127 }
128}
129
130float Matrix::Value(uint32_t row, uint32_t col) const {
131 float value;
132
133 if (row >= (uint32_t)pimpl_->descriptor->Rows() ||
134 col >= (uint32_t)pimpl_->descriptor->Cols()) {
135 Warn("index (%i,%i) out of bound (%i,%i)\n", row, col,
136 pimpl_->descriptor->Rows() - 1, pimpl_->descriptor->Cols() - 1);
137 return 0;
138 }
139
140 GetMutex();
141 value = pimpl_->datas[row][col];
142 ReleaseMutex();
143
144 return value;
145}
146
147float Matrix::ValueNoMutex(uint32_t row, uint32_t col) const {
148 if (row >= (uint32_t)pimpl_->descriptor->Rows() ||
149 col >= (uint32_t)pimpl_->descriptor->Cols()) {
150 Warn("index (%i,%i) out of bound (%i,%i)\n", row, col,
151 pimpl_->descriptor->Rows() - 1, pimpl_->descriptor->Cols() - 1);
152 return 0;
153 }
154
155 return pimpl_->datas[row][col];
156}
157
158void Matrix::SetValue(uint32_t row, uint32_t col, float value) {
159 if (row >= (uint32_t)pimpl_->descriptor->Rows() ||
160 col >= (uint32_t)pimpl_->descriptor->Cols()) {
161 Warn("index (%i,%i) out of bound (%i,%i)\n", row, col,
162 pimpl_->descriptor->Rows() - 1, pimpl_->descriptor->Cols() - 1);
163 } else {
164 GetMutex();
165 pimpl_->datas[row][col]=value;
166 ReleaseMutex();
167 }
168}
169
170void Matrix::SetValueNoMutex(uint32_t row, uint32_t col, float value) {
171 if (row >= (uint32_t)pimpl_->descriptor->Rows() ||
172 col >= (uint32_t)pimpl_->descriptor->Cols()) {
173 Warn("index (%i,%i) out of bound (%i,%i)\n", row, col,
174 pimpl_->descriptor->Rows() - 1, pimpl_->descriptor->Cols() - 1);
175 } else {
176 pimpl_->datas[row][col]=value;
177 }
178}
179
180void Matrix::CopyDatas(char *dst) const {
181 GetMutex();
182 // printf("%f %x %i\n",cvGetReal2D(pimpl_->mat,0,0),dst,Size());
183 memcpy(dst, pimpl_->datas, dataType.GetSize());
184 ReleaseMutex();
185}
186
187uint32_t Matrix::Rows(void) const { return pimpl_->descriptor->Rows(); }
188
189uint32_t Matrix::Cols(void) const { return pimpl_->descriptor->Cols(); }
190
191string Matrix::Name(uint32_t row, uint32_t col) const {
192 return pimpl_->descriptor->ElementName(row, col);
193}
194
195} // end namespace core
196} // end namespace flair
Note: See TracBrowser for help on using the repository browser.