-
Notifications
You must be signed in to change notification settings - Fork 432
/
Script.cpp
300 lines (248 loc) · 8.36 KB
/
Script.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/******************************************************************************
* Copyright (c) 2017, Howard Butler (howard@hobu.co)
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following
* conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided
* with the distribution.
* * Neither the name of Hobu, Inc. or Flaxen Geo Consulting nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
****************************************************************************/
#include "Script.hpp"
namespace pdal
{
namespace mlang
{
std::ostream& operator << (std::ostream& os, Script const& script)
{
os << "source=[" << script.m_source.size() << " bytes], ";
os << std::endl;
return os;
}
std::string Script::getLogicalMask(mxArray* array, LogPtr log)
{
std::string output("");
mxClassID ml_id = mxGetClassID(array);
if (ml_id != mxSTRUCT_CLASS)
throw pdal::pdal_error("Selected array must be a Matlab struct array!");
for (int n = 0; n < mxGetNumberOfFields(array); ++n)
{
mxArray* f = mxGetFieldByNumber(array, 0, n);
if (mxIsLogical(f))
{
const char* name = mxGetFieldNameByNumber(array, n);
output = std::string(name);
}
}
return output;
}
PointLayoutPtr Script::getStructLayout(mxArray* array, LogPtr log)
{
PointLayoutPtr layout = PointLayoutPtr(new PointLayout());
std::vector<mxArray*> arrays;
mxClassID ml_id = mxGetClassID(array);
if (ml_id != mxSTRUCT_CLASS)
throw pdal::pdal_error("Selected array must be a Matlab struct array!");
int numFields = mxGetNumberOfFields(array);
if (!numFields)
throw pdal::pdal_error("Selected struct array must have fields!");
for (int i=0; i < numFields; ++i)
{
const char* fieldName = mxGetFieldNameByNumber(array, i);
mxArray* f = mxGetFieldByNumber(array, 0, i);
mxClassID mt = mxGetClassID(f);
Dimension::Type pt = Script::getPDALDataType(mt);
layout->registerOrAssignDim(fieldName, pt);
}
layout->finalize();
return layout;
}
void Script::getMatlabStruct(mxArray* array, PointViewPtr view, const Dimension::IdList& indims, LogPtr log)
{
std::vector<mxArray*> arrays;
std::vector<std::string> dimNames;
mxClassID ml_id = mxGetClassID(array);
if (ml_id != mxSTRUCT_CLASS)
throw pdal::pdal_error("Selected array must be a Matlab struct array!");
Dimension::IdList dims;
if (!indims.size())
dims = view->dims();
else
dims = indims;
for (auto d: dims)
{
std::string dimName = view->dimName(d);
mxArray* f = mxGetField(array, 0, dimName.c_str());
if (!f)
{
std::ostringstream oss;
oss << "No dimension named '" << dimName << "' exists on struct array.";
throw pdal::pdal_error(oss.str());
}
mwSize numElements = mxGetNumberOfElements(f);
if (numElements != view->size())
{
std::ostringstream oss;
oss << "Array shape is not the same as the PDAL PointView. ";
oss << "Matlab array is has '" << numElements << "' elements. ";
oss << "PointView has '" << view->size() << "' elements.";
throw pdal::pdal_error(oss.str());
}
mxClassID mt = mxGetClassID(f);
Dimension::Type pt = Script::getPDALDataType(mt);
char* p = (char*)mxGetData(f);
if (!p)
{
std::ostringstream oss;
oss << "Unable to fetch Matlab pointer to array for dimension '" << dimName << "'";
throw pdal::pdal_error(oss.str());
}
for (PointId i = 0; i < view->size(); ++i)
{
view->setField(d, pt, i, p);
p += view->dimSize(d);
}
}
}
mxArray* Script::setMatlabStruct(PointViewPtr view, const Dimension::IdList& indims, LogPtr log)
{
std::vector<mxArray*> arrays;
std::vector<std::string> dimNames;
Dimension::IdList dims;
if (!indims.size())
dims = view->dims();
else
dims = indims;
mwSize mdims[2] = {0, 0};
mdims[0] = view->size();
mdims[1] = 1;
std::stringstream dimensionsString;
for (auto d: dims)
{
std::string dimName = Dimension::name(d);
mxArray* array = mxCreateNumericArray( 2,
mdims,
(mxClassID)mlang::Script::getMatlabDataType(view->dimType(d)),
(mxComplexity)0);
arrays.push_back(array);
dimNames.push_back(dimName);
}
// Matlab is column-major
for (size_t j = 0; j < dims.size(); ++j)
{
Dimension::Id id = dims[j];
Dimension::Type t = view->dimType(id);
mxArray* array = arrays[j];
char* pointsPtr = (char*)mxGetData(array);
for (point_count_t i = 0; i < view->size(); ++i)
{
view->getField(pointsPtr, id, t, i);
pointsPtr += view->dimSize(id);
}
}
// Push the dimension names into a char**
auto convert = [](std::string& s)
{
return s.c_str();
};
std::vector<const char*> fieldNames;
std::transform(dimNames.begin(), dimNames.end(), std::back_inserter(fieldNames), convert);
// Going into a 1x1 struct
mdims[0] = 1;
mdims[1] = 1;
mxArray* s = mxCreateStructArray(2, mdims, dimNames.size(), fieldNames.data());
for (size_t j = 0; j < dims.size(); ++j)
{
std::string dimName = dimNames[j];
mxArray* array = arrays[j];
mxSetField(s, 0, dimName.c_str(), array);
}
return s;
}
Dimension::Type Script::getPDALDataType(mxClassID t)
{
using namespace Dimension;
switch (t)
{
case mxSINGLE_CLASS:
return Type::Float;
case mxDOUBLE_CLASS:
return Type::Double;
case mxINT8_CLASS:
return Type::Signed8;
case mxINT16_CLASS:
return Type::Signed16;
case mxINT32_CLASS:
return Type::Signed32;
case mxINT64_CLASS:
return Type::Signed64;
case mxUINT8_CLASS:
return Type::Unsigned8;
case mxUINT16_CLASS:
return Type::Unsigned16;
case mxUINT32_CLASS:
return Type::Unsigned32;
case mxUINT64_CLASS:
return Type::Unsigned64;
default:
return Type::None;
}
assert(0);
return Type::None;
}
int Script::getMatlabDataType(Dimension::Type t)
{
using namespace Dimension;
switch (t)
{
case Type::Float:
return mxSINGLE_CLASS;
case Type::Double:
return mxDOUBLE_CLASS;
case Type::Signed8:
return mxINT8_CLASS;
case Type::Signed16:
return mxINT16_CLASS;
case Type::Signed32:
return mxINT32_CLASS;
case Type::Signed64:
return mxINT64_CLASS;
case Type::Unsigned8:
return mxUINT8_CLASS;
case Type::Unsigned16:
return mxUINT16_CLASS;
case Type::Unsigned32:
return mxUINT32_CLASS;
case Type::Unsigned64:
return mxUINT64_CLASS;
default:
return -1;
}
assert(0);
return -1;
}
} //namespace plang
} //namespace pdal