[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 |
|
---|
| 24 | using std::string;
|
---|
| 25 |
|
---|
[15] | 26 | namespace flair {
|
---|
| 27 | namespace core {
|
---|
| 28 | /*! \class cvmatrixElement
|
---|
| 29 | */
|
---|
| 30 | class cvmatrixElement : public IODataElement {
|
---|
| 31 | public:
|
---|
| 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()) {
|
---|
[214] | 39 | matrix->Err("index (%i,%i) out of bound, max (%i,%i)\n", row, col,
|
---|
[15] | 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] | 80 | private:
|
---|
| 81 | const cvmatrix *matrix;
|
---|
| 82 | uint32_t row, col;
|
---|
| 83 | };
|
---|
[2] | 84 |
|
---|
[15] | 85 | cvmatrix::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] | 99 | cvmatrix::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] | 113 | cvmatrix::~cvmatrix() { delete pimpl_; }
|
---|
[2] | 114 |
|
---|
[15] | 115 | IODataElement *cvmatrix::Element(uint32_t row, uint32_t col) const {
|
---|
| 116 | return new cvmatrixElement(this, Name(row, col), row, col);
|
---|
[2] | 117 | }
|
---|
| 118 |
|
---|
[15] | 119 | IODataElement *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 |
|
---|
| 130 | float 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) {
|
---|
[214] | 135 | Warn("index (%i,%i) out of bound, max (%i,%i)\n", row, col,
|
---|
[15] | 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 |
|
---|
| 147 | float 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) {
|
---|
[214] | 150 | Warn("index (%i,%i) out of bound, max (%i,%i)\n", row, col,
|
---|
[15] | 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] | 158 | void 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) {
|
---|
[214] | 161 | Warn("index (%i,%i) out of bound, max (%i,%i)\n", row, col,
|
---|
[15] | 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] | 170 | void 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) {
|
---|
[214] | 173 | Warn("index (%i,%i) out of bound, max (%i,%i)\n", row, col,
|
---|
[15] | 174 | pimpl_->mat->rows - 1, pimpl_->mat->cols - 1);
|
---|
| 175 | } else {
|
---|
| 176 | cvSetReal2D(pimpl_->mat, row, col, value);
|
---|
| 177 | }
|
---|
[2] | 178 | }
|
---|
| 179 |
|
---|
[15] | 180 | CvMat *cvmatrix::getCvMat(void) const { return pimpl_->mat; }
|
---|
[2] | 181 |
|
---|
[15] | 182 | void 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] | 189 | uint32_t cvmatrix::Rows(void) const { return pimpl_->mat->rows; }
|
---|
[2] | 190 |
|
---|
[15] | 191 | uint32_t cvmatrix::Cols(void) const { return pimpl_->mat->cols; }
|
---|
[2] | 192 |
|
---|
| 193 | string 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
|
---|