Skip to content

Commit 1c77b7a

Browse files
committed
Replace Mesh class with meshToNumpyArray function
1 parent 807ed64 commit 1c77b7a

File tree

5 files changed

+133
-242
lines changed

5 files changed

+133
-242
lines changed

pdal/PyArray.cpp

Lines changed: 0 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -89,35 +89,6 @@ std::string toString(PyObject *pname)
8989

9090
} // unnamed namespace
9191

92-
Array::Array(PointViewPtr view)
93-
{
94-
if (_import_array() < 0)
95-
throw pdal_error("Could not import numpy.core.multiarray.");
96-
97-
PyObject *dtype_dict = (PyObject*)buildNumpyDescription(view);
98-
if (!dtype_dict)
99-
throw pdal_error("Unable to build numpy dtype "
100-
"description dictionary");
101-
102-
PyArray_Descr *dtype = nullptr;
103-
if (PyArray_DescrConverter(dtype_dict, &dtype) == NPY_FAIL)
104-
throw pdal_error("Unable to build numpy dtype");
105-
Py_XDECREF(dtype_dict);
106-
107-
// This is a 1 x size array.
108-
npy_intp size = view->size();
109-
m_array = (PyArrayObject *)PyArray_NewFromDescr(&PyArray_Type, dtype,
110-
1, &size, 0, nullptr, NPY_ARRAY_CARRAY, nullptr);
111-
112-
// copy the data
113-
DimTypeList types = view->dimTypes();
114-
for (PointId idx = 0; idx < view->size(); idx++)
115-
{
116-
char *p = (char *)PyArray_GETPTR1(m_array, idx);
117-
view->getPackedPoint(types, idx, p);
118-
}
119-
}
120-
12192

12293
Array::Array(PyArrayObject* array) : m_array(array), m_rowMajor(true)
12394
{
@@ -190,63 +161,6 @@ Array::~Array()
190161
}
191162

192163

193-
PyObject* Array::buildNumpyDescription(PointViewPtr view) const
194-
{
195-
// Build up a numpy dtype dictionary
196-
//
197-
// {'formats': ['f8', 'f8', 'f8', 'u2', 'u1', 'u1', 'u1', 'u1', 'u1',
198-
// 'f4', 'u1', 'u2', 'f8', 'u2', 'u2', 'u2'],
199-
// 'names': ['X', 'Y', 'Z', 'Intensity', 'ReturnNumber',
200-
// 'NumberOfReturns', 'ScanDirectionFlag', 'EdgeOfFlightLine',
201-
// 'Classification', 'ScanAngleRank', 'UserData',
202-
// 'PointSourceId', 'GpsTime', 'Red', 'Green', 'Blue']}
203-
//
204-
205-
Dimension::IdList dims = view->dims();
206-
207-
PyObject* dict = PyDict_New();
208-
PyObject* sizes = PyList_New(dims.size());
209-
PyObject* formats = PyList_New(dims.size());
210-
PyObject* titles = PyList_New(dims.size());
211-
212-
for (size_t i = 0; i < dims.size(); ++i)
213-
{
214-
Dimension::Id id = dims[i];
215-
Dimension::Type t = view->dimType(id);
216-
npy_intp stride = view->dimSize(id);
217-
218-
std::string name = view->dimName(id);
219-
220-
std::string kind("i");
221-
Dimension::BaseType b = Dimension::base(t);
222-
if (b == Dimension::BaseType::Unsigned)
223-
kind = "u";
224-
else if (b == Dimension::BaseType::Signed)
225-
kind = "i";
226-
else if (b == Dimension::BaseType::Floating)
227-
kind = "f";
228-
else
229-
throw pdal_error("Unable to map kind '" + kind +
230-
"' to PDAL dimension type");
231-
232-
std::stringstream oss;
233-
oss << kind << stride;
234-
PyObject* pySize = PyLong_FromLong(stride);
235-
PyObject* pyTitle = PyUnicode_FromString(name.c_str());
236-
PyObject* pyFormat = PyUnicode_FromString(oss.str().c_str());
237-
238-
PyList_SetItem(sizes, i, pySize);
239-
PyList_SetItem(titles, i, pyTitle);
240-
PyList_SetItem(formats, i, pyFormat);
241-
}
242-
243-
PyDict_SetItemString(dict, "names", titles);
244-
PyDict_SetItemString(dict, "formats", formats);
245-
246-
return dict;
247-
}
248-
249-
250164
ArrayIter& Array::iterator()
251165
{
252166
ArrayIter *it = new ArrayIter(m_array);
@@ -255,88 +169,6 @@ ArrayIter& Array::iterator()
255169
}
256170

257171

258-
Mesh::Mesh(PointViewPtr view)
259-
{
260-
if (_import_array() < 0)
261-
throw pdal_error("Could not import numpy.core.multiarray.");
262-
263-
PyObject *dtype_dict = (PyObject*)buildNumpyDescription(view);
264-
if (!dtype_dict)
265-
throw pdal_error("Unable to build numpy dtype "
266-
"description dictionary");
267-
268-
PyArray_Descr *dtype = nullptr;
269-
if (PyArray_DescrConverter(dtype_dict, &dtype) == NPY_FAIL)
270-
throw pdal_error("Unable to build numpy dtype");
271-
Py_XDECREF(dtype_dict);
272-
273-
// This is a 1 x size array.
274-
TriangularMesh* mesh = view->mesh();
275-
npy_intp size = mesh ? mesh->size() : 0;
276-
m_mesh = (PyArrayObject *)PyArray_NewFromDescr(&PyArray_Type, dtype,
277-
1, &size, 0, nullptr, NPY_ARRAY_CARRAY, nullptr);
278-
279-
for (PointId idx = 0; idx < size; idx++)
280-
{
281-
char* p = (char *)PyArray_GETPTR1(m_mesh, idx);
282-
const Triangle& t = (*mesh)[idx];
283-
uint32_t a = (uint32_t)t.m_a;
284-
std::memcpy(p, &a, 4);
285-
uint32_t b = (uint32_t)t.m_b;
286-
std::memcpy(p + 4, &b, 4);
287-
uint32_t c = (uint32_t)t.m_c;
288-
std::memcpy(p + 8, &c, 4);
289-
}
290-
}
291-
292-
293-
Mesh::~Mesh()
294-
{
295-
if (m_mesh)
296-
Py_XDECREF((PyObject *)m_mesh);
297-
}
298-
299-
300-
PyObject* Mesh::buildNumpyDescription(PointViewPtr view) const
301-
{
302-
// Build up a numpy dtype dictionary
303-
//
304-
// {'formats': ['f8', 'f8', 'f8', 'u2', 'u1', 'u1', 'u1', 'u1', 'u1',
305-
// 'f4', 'u1', 'u2', 'f8', 'u2', 'u2', 'u2'],
306-
// 'names': ['X', 'Y', 'Z', 'Intensity', 'ReturnNumber',
307-
// 'NumberOfReturns', 'ScanDirectionFlag', 'EdgeOfFlightLine',
308-
// 'Classification', 'ScanAngleRank', 'UserData',
309-
// 'PointSourceId', 'GpsTime', 'Red', 'Green', 'Blue']}
310-
//
311-
312-
Dimension::IdList dims = view->dims();
313-
314-
PyObject* dict = PyDict_New();
315-
PyObject* formats = PyList_New(3);
316-
PyObject* titles = PyList_New(3);
317-
318-
PyList_SetItem(titles, 0, PyUnicode_FromString("A"));
319-
PyList_SetItem(formats, 0, PyUnicode_FromString("u4"));
320-
PyList_SetItem(titles, 1, PyUnicode_FromString("B"));
321-
PyList_SetItem(formats, 1, PyUnicode_FromString("u4"));
322-
PyList_SetItem(titles, 2, PyUnicode_FromString("C"));
323-
PyList_SetItem(formats, 2, PyUnicode_FromString("u4"));
324-
325-
326-
PyDict_SetItemString(dict, "names", titles);
327-
PyDict_SetItemString(dict, "formats", formats);
328-
329-
return dict;
330-
}
331-
332-
ArrayIter& Mesh::iterator()
333-
{
334-
ArrayIter *it = new ArrayIter(m_mesh);
335-
m_iterators.push_back(std::unique_ptr<ArrayIter>(it));
336-
return *it;
337-
}
338-
339-
340172
ArrayIter::ArrayIter(PyArrayObject* np_array)
341173
{
342174
m_iter = NpyIter_New(np_array,

pdal/PyArray.hpp

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -46,50 +46,21 @@ namespace python
4646
class ArrayIter;
4747

4848

49-
class PDAL_DLL Mesh
50-
{
51-
public:
52-
using Shape = std::array<size_t, 3>;
53-
54-
Mesh(PointViewPtr view);
55-
~Mesh();
56-
57-
PyArrayObject *getPythonArray() const { return m_mesh; }
58-
bool rowMajor() const { return m_rowMajor; }
59-
Shape shape() const { return m_shape; }
60-
ArrayIter& iterator();
61-
62-
private:
63-
inline PyObject* buildNumpyDescription(PointViewPtr view) const;
64-
65-
PyArrayObject* m_mesh;
66-
bool m_rowMajor;
67-
Shape m_shape {};
68-
std::vector<std::unique_ptr<ArrayIter>> m_iterators;
69-
};
70-
71-
7249
class PDAL_DLL Array
7350
{
7451
public:
7552
using Shape = std::array<size_t, 3>;
7653
using Fields = std::vector<MemoryViewReader::Field>;
7754

78-
// Create an array for reading data from PDAL.
79-
Array(PointViewPtr view);
80-
// Create an array for writing data to PDAL.
8155
Array(PyArrayObject* array);
8256
~Array();
8357

84-
PyArrayObject *getPythonArray() const { return m_array; }
8558
bool rowMajor() const { return m_rowMajor; };
8659
Shape shape() const { return m_shape; }
8760
const Fields& fields() const { return m_fields; };
8861
ArrayIter& iterator();
8962

9063
private:
91-
inline PyObject* buildNumpyDescription(PointViewPtr view) const;
92-
9364
PyArrayObject* m_array;
9465
Fields m_fields;
9566
bool m_rowMajor;

0 commit comments

Comments
 (0)