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_impl.cpp
|
---|
7 | //
|
---|
8 | // author: Guillaume Sanahuja
|
---|
9 | // Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
10 | //
|
---|
11 | // version: $Id: $
|
---|
12 | //
|
---|
13 | // purpose: classe definissant le type de données Matrix
|
---|
14 | //
|
---|
15 | /*********************************************************************/
|
---|
16 |
|
---|
17 | #include "Matrix.h"
|
---|
18 | #include "Matrix_impl.h"
|
---|
19 | #include <typeinfo>
|
---|
20 | #include <sstream>
|
---|
21 |
|
---|
22 | using std::string;
|
---|
23 | using namespace flair::core;
|
---|
24 |
|
---|
25 | Matrix_impl::Matrix_impl(Matrix *self, int rows, int cols,
|
---|
26 | flair::core::ScalarType const &_elementDataType,
|
---|
27 | int n)
|
---|
28 | : elementDataType(_elementDataType) {
|
---|
29 | descriptor = new cvmatrix_descriptor(rows, cols);
|
---|
30 | Init(self, n);
|
---|
31 | }
|
---|
32 |
|
---|
33 | Matrix_impl::Matrix_impl(Matrix *self,
|
---|
34 | const cvmatrix_descriptor *inDescriptor,
|
---|
35 | flair::core::ScalarType const &_elementDataType,
|
---|
36 | int n)
|
---|
37 | : elementDataType(_elementDataType) {
|
---|
38 | descriptor = new cvmatrix_descriptor(inDescriptor->Rows(), inDescriptor->Cols());
|
---|
39 |
|
---|
40 | for (uint32_t i = 0; i < descriptor->Rows(); i++) {
|
---|
41 | for (uint32_t j = 0; j < descriptor->Cols(); j++) {
|
---|
42 | descriptor->SetElementName(i, j, inDescriptor->ElementName(i, j));
|
---|
43 | }
|
---|
44 | }
|
---|
45 | Init(self, n);
|
---|
46 | }
|
---|
47 |
|
---|
48 | void Matrix_impl::Init(Matrix *self, int n) {
|
---|
49 | this->self = self;
|
---|
50 |
|
---|
51 | datas = nullptr;
|
---|
52 | try {
|
---|
53 | ScalarType const &scalarType =
|
---|
54 | dynamic_cast<ScalarType const &>(elementDataType);
|
---|
55 | if (typeid(scalarType) == typeid(FloatType)) {
|
---|
56 | datas = (float **)malloc(descriptor->Rows() * sizeof(float*));
|
---|
57 | if(datas==NULL) {
|
---|
58 | self->Err("error allocating matrix\n");
|
---|
59 | return;
|
---|
60 | }
|
---|
61 | for(uint32_t i=0 ; i < descriptor->Rows() ; i++){
|
---|
62 | datas[i] = (float*)malloc(descriptor->Cols() * sizeof(float) );
|
---|
63 | if(datas[i]==NULL) {
|
---|
64 | self->Err("error allocating matrix\n");
|
---|
65 | for(uint32_t j = i-1 ; j >= 0 ; j--) free(datas[j]);
|
---|
66 | free(datas);
|
---|
67 | return;
|
---|
68 | }
|
---|
69 | }
|
---|
70 | /*
|
---|
71 | } else if (typeid(scalarType) == typeid(SignedIntegerType)) {
|
---|
72 | switch (elementDataType.GetSize()) {
|
---|
73 | case 1:
|
---|
74 | mat = cvCreateMat(descriptor->Rows(), descriptor->Cols(), CV_8SC1);
|
---|
75 | break;
|
---|
76 | case 2:
|
---|
77 | mat = cvCreateMat(descriptor->Rows(), descriptor->Cols(), CV_16SC1);
|
---|
78 | break;
|
---|
79 | default:
|
---|
80 | self->Err("unsupported integer scalar type\n");
|
---|
81 | }*/
|
---|
82 | } else {
|
---|
83 | self->Err("unsupported scalar type\n");
|
---|
84 | }
|
---|
85 | } catch (std::bad_cast e) {
|
---|
86 | self->Err("type is not a scalar\n");
|
---|
87 | }
|
---|
88 |
|
---|
89 | if (datas == nullptr)
|
---|
90 | self->Err("allocating matrix failed\n");
|
---|
91 | if (n > 1)
|
---|
92 | self->Warn("n>1 not supported\n");
|
---|
93 | }
|
---|
94 |
|
---|
95 | Matrix_impl::~Matrix_impl() {
|
---|
96 | for (uint32_t i = 0; i < descriptor->Rows();i++) {
|
---|
97 | free(datas[i]);
|
---|
98 | }
|
---|
99 | free(datas);
|
---|
100 | delete descriptor;
|
---|
101 | }
|
---|