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

Last change on this file since 13 was 2, checked in by Sanahuja Guillaume, 9 years ago

flaircore

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