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

Last change on this file since 67 was 15, checked in by Bayard Gildas, 8 years ago

sources reformatted with flair-format-dir script

File size: 5.7 KB
RevLine 
[2]1// %flair:license{
[15]2// This file is part of the Flair framework distributed under the
3// CECILL-C License, Version 1.0.
[2]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
[15]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 (%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 }
[2]53
[15]54 ~cvmatrixElement() {}
[2]55
[15]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 }
[2]75
[15]76 DataType const &GetDataType(void) const {
77 return matrix->GetDataType().GetElementDataType();
78 }
[2]79
[15]80private:
81 const cvmatrix *matrix;
82 uint32_t row, col;
83};
[2]84
[15]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);
[2]89
[15]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);
[2]95 }
[15]96 }
[2]97}
98
[15]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);
[2]104
[15]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);
[2]109 }
[15]110 }
[2]111}
112
[15]113cvmatrix::~cvmatrix() { delete pimpl_; }
[2]114
[15]115IODataElement *cvmatrix::Element(uint32_t row, uint32_t col) const {
116 return new cvmatrixElement(this, Name(row, col), row, col);
[2]117}
118
[15]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 }
[2]128}
129
130float cvmatrix::Value(uint32_t row, uint32_t col) const {
[15]131 float value;
[2]132
[15]133 if (row >= (uint32_t)pimpl_->mat->rows ||
134 col >= (uint32_t)pimpl_->mat->cols) {
135 Warn("index (%i,%i) out of bound (%i,%i)\n", row, col,
136 pimpl_->mat->rows - 1, pimpl_->mat->cols - 1);
137 return 0;
138 }
[2]139
[15]140 GetMutex();
141 value = cvGetReal2D(pimpl_->mat, row, col);
142 ReleaseMutex();
[2]143
[15]144 return value;
[2]145}
146
147float cvmatrix::ValueNoMutex(uint32_t row, uint32_t col) const {
[15]148 if (row >= (uint32_t)pimpl_->mat->rows ||
149 col >= (uint32_t)pimpl_->mat->cols) {
150 Warn("index (%i,%i) out of bound (%i,%i)\n", row, col,
151 pimpl_->mat->rows - 1, pimpl_->mat->cols - 1);
152 return 0;
153 }
[2]154
[15]155 return cvGetReal2D(pimpl_->mat, row, col);
[2]156}
157
[15]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 (%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 }
[2]168}
169
[15]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 (%i,%i)\n", row, col,
174 pimpl_->mat->rows - 1, pimpl_->mat->cols - 1);
175 } else {
176 cvSetReal2D(pimpl_->mat, row, col, value);
177 }
[2]178}
179
[15]180CvMat *cvmatrix::getCvMat(void) const { return pimpl_->mat; }
[2]181
[15]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();
[2]187}
188
[15]189uint32_t cvmatrix::Rows(void) const { return pimpl_->mat->rows; }
[2]190
[15]191uint32_t cvmatrix::Cols(void) const { return pimpl_->mat->cols; }
[2]192
193string cvmatrix::Name(uint32_t row, uint32_t col) const {
[15]194 return pimpl_->descriptor->ElementName(row, col);
[2]195}
196
197} // end namespace core
198} // end namespace flair
Note: See TracBrowser for help on using the repository browser.