Debian Codegraph @

src/utilities/aslMATLABCasters.cxx

raw
1 /*
2 * Advanced Simulation Library <http://asl.org.il>
3 *
4 * Copyright 2015 Avtech Scientific <http://avtechscientific.com>
5 *
6 *
7 * This file is part of Advanced Simulation Library (ASL).
8 *
9 * ASL is free software: you can redistribute it and/or modify it
10 * under the terms of the GNU Affero General Public License as
11 * published by the Free Software Foundation, version 3 of the License.
12 *
13 * ASL is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with ASL. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23
24 #include "aslMATLABCasters.h"
25
26 #include <data/aslBlocks.h>
27 #include <data/aslProbe.h>
28 #include <data/aslDataWrapper.h>
29 #include <acl/acl.h>
30 #include <acl/DataTypes/aclArray.h>
31 #include <math/aslVectors.h>
32
33 #include <matio.h>
34
35 template <typename T> asl::AVec<T> castMATLABVector(const asl::AVec<T> &a)
36 {
37 unsigned int n(nD(a));
38 asl::AVec<T> b(n>2?n:2);
39 if(n == 1)
40 b[0] = a[0]; b[1] = 1;
41 if (n >= 2)
42 b = a;
43 return b;
44 }
45
46 template <typename T> class MATLABTypes{};
47 template <> class MATLABTypes<double>{public:
48 const static int c=MAT_C_DOUBLE,
49 t=MAT_T_DOUBLE;
50 };
51 template <> class MATLABTypes<float>{public:
52 const static int c=MAT_C_SINGLE,
53 t=MAT_T_SINGLE;
54 };
55 template <> class MATLABTypes<cl_int>{public:
56 const static int c=MAT_C_INT32,
57 t=MAT_C_INT32;
58 };
59
60
61
62 matiocpp::Var::~Var(){
63 if (var->mem_conserve && freeArray)
64 switch (var->data_type)
65 {
66 case MAT_T_DOUBLE: delete[] ((double*)var->data); break;
67 case MAT_T_SINGLE: delete[] ((float*)var->data); break;
68 case MAT_T_INT32: delete[] ((cl_int*)var->data); break;
69 }
70 Mat_VarFree(var);
71 }
72
73 template<typename T> matvar_t* matiocpp::Var::init(T *d, const asl::AVec<int> & size, const std::string &name)
74 {
75 asl::AVec<size_t> msize(castMATLABVector(size));
76 return Mat_VarCreate(name.c_str(),
77 MATLABTypes<T>::c,
78 MATLABTypes<T>::t,
79 nD(msize),
80 &msize[0],
81 d,
82 0);
83 }
84
85 template<typename T> matiocpp::Var::Var(T *d, const asl::AVec<int> & size, const std::string &name, bool freeArr):
86 var(init(d,size,name)),
87 freeArray(freeArr)
88 {
89 }
90
91 template<typename T> matiocpp::Var::Var(T *d, unsigned int size, const std::string &name, bool freeArr):
92 var(init(d,asl::makeAVec((int)size),name)),
93 freeArray(freeArr)
94 {
95 }
96
97 template<typename T> matiocpp::Var::Var(std::vector<T> & d, const std::string &name):
98 var(init(&d.front(),asl::makeAVec((int)d.size()),name)),
99 freeArray(false)
100 {
101 }
102
103
104 namespace asl
105 {
106
107 SPMatVar castMATLABCellArray(acl::Element source, const std::string &name)
108 {
109 if (!isMemBlock(source))
110 errorMessage ("Error (castMATLABCellArray): the given element is not a MemBlock type");
111
112 if (source->getTypeID() == acl::TYPE_DOUBLE)
113 {
114 double* d=new double[source->getSize()];
115 copy(source,d);
116 return SPMatVar(new matiocpp::Var(d,source->getSize(),name));
117 }
118 if (source->getTypeID() == acl::TYPE_FLOAT)
119 {
120 float* d=new float[source->getSize()];
121 copy(source,d);
122 return SPMatVar(new matiocpp::Var(d,source->getSize(),name));
123 }
124 if (source->getTypeID() == acl::TYPE_INT)
125 {
126 int* d=new int[source->getSize()];
127 copy(source,d);
128 return SPMatVar(new matiocpp::Var(d,source->getSize(),name));
129 }
130 return SPMatVar();
131 }
132
133 SPMatVar castMATLABCellArray(acl::Element source, const AVec<int> & size, const std::string &name)
134 {
135 if (!isMemBlock(source))
136 errorMessage ("Error (castMATLABCellArray): the given element is not a MemBlock type");
137
138 if (source->getTypeID() == acl::TYPE_DOUBLE)
139 {
140 double* d = new double[source->getSize()];
141 copy(source, d);
142 return SPMatVar(new matiocpp::Var(d,size,name));
143 }
144 if (source->getTypeID() == acl::TYPE_FLOAT)
145 {
146 float* d = new float[source->getSize()];
147 copy(source, d);
148 return SPMatVar(new matiocpp::Var(d,size,name));
149 }
150 if (source->getTypeID() == acl::TYPE_INT)
151 {
152 int* d = new int[source->getSize()];
153 copy(source, d);
154 return SPMatVar(new matiocpp::Var(d,size,name));
155 }
156 return SPMatVar();
157 }
158
159
160 SPMatVar castMATLABCellArray(const AbstractData & d, const std::vector<std::string> &names)
161 {
162 const Block &b(d.getBlock());
163 bool bnames(names.size()>0);
164
165 if (d.getDContainer().size()==1)
166 return castMATLABCellArray(d.getDContainer()[0],b.getSize(),bnames?names[0]:"");
167 // else
168 // for (unsigned int i(1); i < ; ++i)
169 // image->GetPointData()->AddArray(castVTKDataArray(d.getDContainer()[i],bnames?names[i]:""));
170 return SPMatVar();
171 }
172
173 SPMatVar castMATLABCellArray(Probe &p, unsigned int component, const std::string &name)
174 {
175 return SPMatVar(new matiocpp::Var(p.getComponent(component),name));
176 }
177
178
179 }// asl