source: flair-src/trunk/lib/FlairCore/src/cvmatrix.cpp@ 214

Last change on this file since 214 was 214, checked in by Sanahuja Guillaume, 4 years ago

matrix

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: 2012/03/21
6// filename: cvmatrix.cpp
7//
8// author: Guillaume Sanahuja
9// Copyright Heudiasyc UMR UTC/CNRS 7253
10//
11// version: $Id: $
12//
13// purpose: Class defining a matrix of kind CvMat
14//
15/*********************************************************************/
16
17#include "cvmatrix.h"
18#include "cvmatrix_impl.h"
19#include <cxcore.h>
20#include <typeinfo>
21
22#include <iostream>
23
24using std::string;
25
26namespace flair {
27namespace core {
28/*! \class cvmatrixElement
29*/
30class cvmatrixElement : public IODataElement {
31public:
32 cvmatrixElement(const cvmatrix *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, max (%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 ~cvmatrixElement() {}
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 cvmatrix *matrix;
82 uint32_t row, col;
83};
84
85cvmatrix::cvmatrix(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 cvmatrix_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
99cvmatrix::cvmatrix(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 cvmatrix_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
113cvmatrix::~cvmatrix() { delete pimpl_; }
114
115IODataElement *cvmatrix::Element(uint32_t row, uint32_t col) const {
116 return new cvmatrixElement(this, Name(row, col), row, col);
117}
118
119IODataElement *cvmatrix::Element(uint32_t index) const {
120 if (Rows() == 1) {
121 return new cvmatrixElement(this, Name(0, index), 0, index);
122 } else if (Cols() == 1) {
123 return new cvmatrixElement(this, Name(index, 0), index, 0);
124 } else {
125 Err("matrix is not 1D\n");
126 return nullptr;
127 }
128}
129
130float cvmatrix::Value(uint32_t row, uint32_t col) const {
131 float value;
132
133 if (row >= (uint32_t)pimpl_->mat->rows ||
134 col >= (uint32_t)pimpl_->mat->cols) {
135 Warn("index (%i,%i) out of bound, max (%i,%i)\n", row, col,
136 pimpl_->mat->rows - 1, pimpl_->mat->cols - 1);
137 return 0;
138 }
139
140 GetMutex();
141 value = cvGetReal2D(pimpl_->mat, row, col);
142 ReleaseMutex();
143
144 return value;
145}
146
147float cvmatrix::ValueNoMutex(uint32_t row, uint32_t col) const {
148 if (row >= (uint32_t)pimpl_->mat->rows ||
149 col >= (uint32_t)pimpl_->mat->cols) {
150 Warn("index (%i,%i) out of bound, max (%i,%i)\n", row, col,
151 pimpl_->mat->rows - 1, pimpl_->mat->cols - 1);
152 return 0;
153 }
154
155 return cvGetReal2D(pimpl_->mat, row, col);
156}
157
158void cvmatrix::SetValue(uint32_t row, uint32_t col, float value) {
159 if (row >= (uint32_t)pimpl_->mat->rows ||
160 col >= (uint32_t)pimpl_->mat->cols) {
161 Warn("index (%i,%i) out of bound, max (%i,%i)\n", row, col,
162 pimpl_->mat->rows - 1, pimpl_->mat->cols - 1);
163 } else {
164 GetMutex();
165 cvSetReal2D(pimpl_->mat, row, col, value);
166 ReleaseMutex();
167 }
168}
169
170void cvmatrix::SetValueNoMutex(uint32_t row, uint32_t col, float value) {
171 if (row >= (uint32_t)pimpl_->mat->rows ||
172 col >= (uint32_t)pimpl_->mat->cols) {
173 Warn("index (%i,%i) out of bound, max (%i,%i)\n", row, col,
174 pimpl_->mat->rows - 1, pimpl_->mat->cols - 1);
175 } else {
176 cvSetReal2D(pimpl_->mat, row, col, value);
177 }
178}
179
180CvMat *cvmatrix::getCvMat(void) const { return pimpl_->mat; }
181
182void cvmatrix::CopyDatas(char *dst) const {
183 GetMutex();
184 // printf("%f %x %i\n",cvGetReal2D(pimpl_->mat,0,0),dst,Size());
185 memcpy(dst, pimpl_->mat->data.ptr, dataType.GetSize());
186 ReleaseMutex();
187}
188
189uint32_t cvmatrix::Rows(void) const { return pimpl_->mat->rows; }
190
191uint32_t cvmatrix::Cols(void) const { return pimpl_->mat->cols; }
192
193string cvmatrix::Name(uint32_t row, uint32_t col) const {
194 return pimpl_->descriptor->ElementName(row, col);
195}
196
197} // end namespace core
198} // end namespace flair
Note: See TracBrowser for help on using the repository browser.