|
| 1 | +/****************************************************************************** |
| 2 | +* Copyright (c) 2021, Runette Software Ltd (www.runette.co.uk) |
| 3 | +* |
| 4 | +* All rights reserved. |
| 5 | +* |
| 6 | +* Redistribution and use in source and binary forms, with or without |
| 7 | +* modification, are permitted provided that the following |
| 8 | +* conditions are met: |
| 9 | +* |
| 10 | +* * Redistributions of source code must retain the above copyright |
| 11 | +* notice, this list of conditions and the following disclaimer. |
| 12 | +* * Redistributions in binary form must reproduce the above copyright |
| 13 | +* notice, this list of conditions and the following disclaimer in |
| 14 | +* the documentation and/or other materials provided |
| 15 | +* with the distribution. |
| 16 | +* * Neither the name of Hobu, Inc. or Flaxen Geo Consulting nor the |
| 17 | +* names of its contributors may be used to endorse or promote |
| 18 | +* products derived from this software without specific prior |
| 19 | +* written permission. |
| 20 | +* |
| 21 | +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 | +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 | +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 24 | +* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 25 | +* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 26 | +* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 27 | +* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 28 | +* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 29 | +* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 30 | +* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 31 | +* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
| 32 | +* OF SUCH DAMAGE. |
| 33 | +****************************************************************************/ |
| 34 | + |
| 35 | +#include "PyMesh.hpp" |
| 36 | + |
| 37 | +#include <numpy/arrayobject.h> |
| 38 | + |
| 39 | + |
| 40 | +namespace pdal |
| 41 | +{ |
| 42 | +namespace python |
| 43 | +{ |
| 44 | + |
| 45 | +// Create new empty mesh |
| 46 | +// |
| 47 | +Mesh::Mesh() : m_mesh(nullptr) |
| 48 | +{ |
| 49 | + hasMesh = false; |
| 50 | + if (_import_array() < 0) |
| 51 | + throw pdal_error("Could not import numpy.core.multiarray."); |
| 52 | +} |
| 53 | + |
| 54 | +Mesh::~Mesh() |
| 55 | +{ |
| 56 | + if (m_mesh) |
| 57 | + Py_XDECREF((PyObject *)m_mesh); |
| 58 | +} |
| 59 | + |
| 60 | +// Update from a PointView |
| 61 | +// |
| 62 | +void Mesh::update(PointViewPtr view) |
| 63 | +{ |
| 64 | + npy_intp size; |
| 65 | + |
| 66 | + if (m_mesh) |
| 67 | + Py_XDECREF((PyObject *)m_mesh); |
| 68 | + m_mesh = nullptr; // Just in case of an exception. |
| 69 | + |
| 70 | + TriangularMesh* mesh = view->mesh(); |
| 71 | + if (mesh) |
| 72 | + { |
| 73 | + hasMesh = true; |
| 74 | + size = mesh->size(); |
| 75 | + } else { |
| 76 | + hasMesh = false; |
| 77 | + size = 0; |
| 78 | + } |
| 79 | + |
| 80 | + PyObject *dtype_dict = (PyObject*)buildNumpyDescription(view); |
| 81 | + if (!dtype_dict) |
| 82 | + throw pdal_error("Unable to build numpy dtype " |
| 83 | + "description dictionary"); |
| 84 | + |
| 85 | + PyArray_Descr *dtype = nullptr; |
| 86 | + if (PyArray_DescrConverter(dtype_dict, &dtype) == NPY_FAIL) |
| 87 | + throw pdal_error("Unable to build numpy dtype"); |
| 88 | + Py_XDECREF(dtype_dict); |
| 89 | + |
| 90 | + // This is a 1 x size array. |
| 91 | + m_mesh = (PyArrayObject *)PyArray_NewFromDescr(&PyArray_Type, dtype, |
| 92 | + 1, &size, 0, nullptr, NPY_ARRAY_CARRAY, nullptr); |
| 93 | + |
| 94 | + for (PointId idx = 0; idx < size; idx++) |
| 95 | + { |
| 96 | + char* p = (char *)PyArray_GETPTR1(m_mesh, idx); |
| 97 | + const Triangle& t = (*mesh)[idx]; |
| 98 | + uint32_t a = (uint32_t)t.m_a; |
| 99 | + std::memcpy(p, &a, 4); |
| 100 | + uint32_t b = (uint32_t)t.m_b; |
| 101 | + std::memcpy(p + 4, &b, 4); |
| 102 | + uint32_t c = (uint32_t)t.m_c; |
| 103 | + std::memcpy(p + 8, &c, 4); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | +PyArrayObject *Mesh::getPythonArray() const |
| 110 | +{ |
| 111 | + return m_mesh; |
| 112 | +} |
| 113 | + |
| 114 | +PyObject* Mesh::buildNumpyDescription(PointViewPtr view) const |
| 115 | +{ |
| 116 | + // Build up a numpy dtype dictionary |
| 117 | + // |
| 118 | + // {'formats': ['f8', 'f8', 'f8', 'u2', 'u1', 'u1', 'u1', 'u1', 'u1', |
| 119 | + // 'f4', 'u1', 'u2', 'f8', 'u2', 'u2', 'u2'], |
| 120 | + // 'names': ['X', 'Y', 'Z', 'Intensity', 'ReturnNumber', |
| 121 | + // 'NumberOfReturns', 'ScanDirectionFlag', 'EdgeOfFlightLine', |
| 122 | + // 'Classification', 'ScanAngleRank', 'UserData', |
| 123 | + // 'PointSourceId', 'GpsTime', 'Red', 'Green', 'Blue']} |
| 124 | + // |
| 125 | + |
| 126 | + Dimension::IdList dims = view->dims(); |
| 127 | + |
| 128 | + PyObject* dict = PyDict_New(); |
| 129 | + PyObject* formats = PyList_New(3); |
| 130 | + PyObject* titles = PyList_New(3); |
| 131 | + |
| 132 | + PyList_SetItem(titles, 0, PyUnicode_FromString("A")); |
| 133 | + PyList_SetItem(formats, 0, PyUnicode_FromString("u4")); |
| 134 | + PyList_SetItem(titles, 1, PyUnicode_FromString("B")); |
| 135 | + PyList_SetItem(formats, 1, PyUnicode_FromString("u4")); |
| 136 | + PyList_SetItem(titles, 2, PyUnicode_FromString("C")); |
| 137 | + PyList_SetItem(formats, 2, PyUnicode_FromString("u4")); |
| 138 | + |
| 139 | + |
| 140 | + PyDict_SetItemString(dict, "names", titles); |
| 141 | + PyDict_SetItemString(dict, "formats", formats); |
| 142 | + |
| 143 | + return dict; |
| 144 | +} |
| 145 | + |
| 146 | +bool Mesh::rowMajor() const |
| 147 | +{ |
| 148 | + return m_rowMajor; |
| 149 | +} |
| 150 | + |
| 151 | +Mesh::Shape Mesh::shape() const |
| 152 | +{ |
| 153 | + return m_shape; |
| 154 | +} |
| 155 | + |
| 156 | + |
| 157 | +MeshIter& Mesh::iterator() |
| 158 | +{ |
| 159 | + MeshIter *it = new MeshIter(*this); |
| 160 | + m_iterators.push_back(std::unique_ptr<MeshIter>(it)); |
| 161 | + return *it; |
| 162 | +} |
| 163 | + |
| 164 | +MeshIter::MeshIter(Mesh& mesh) |
| 165 | +{ |
| 166 | + m_iter = NpyIter_New(mesh.getPythonArray(), |
| 167 | + NPY_ITER_EXTERNAL_LOOP | NPY_ITER_READONLY | NPY_ITER_REFS_OK, |
| 168 | + NPY_KEEPORDER, NPY_NO_CASTING, NULL); |
| 169 | + if (!m_iter) |
| 170 | + throw pdal_error("Unable to create numpy iterator."); |
| 171 | + |
| 172 | + char *itererr; |
| 173 | + m_iterNext = NpyIter_GetIterNext(m_iter, &itererr); |
| 174 | + if (!m_iterNext) |
| 175 | + { |
| 176 | + NpyIter_Deallocate(m_iter); |
| 177 | + throw pdal_error(std::string("Unable to create numpy iterator: ") + |
| 178 | + itererr); |
| 179 | + } |
| 180 | + m_data = NpyIter_GetDataPtrArray(m_iter); |
| 181 | + m_stride = NpyIter_GetInnerStrideArray(m_iter); |
| 182 | + m_size = NpyIter_GetInnerLoopSizePtr(m_iter); |
| 183 | + m_done = false; |
| 184 | +} |
| 185 | + |
| 186 | +MeshIter::~MeshIter() |
| 187 | +{ |
| 188 | + NpyIter_Deallocate(m_iter); |
| 189 | +} |
| 190 | + |
| 191 | +MeshIter& MeshIter::operator++() |
| 192 | +{ |
| 193 | + if (m_done) |
| 194 | + return *this; |
| 195 | + |
| 196 | + if (--(*m_size)) |
| 197 | + *m_data += *m_stride; |
| 198 | + else if (!m_iterNext(m_iter)) |
| 199 | + m_done = true; |
| 200 | + return *this; |
| 201 | +} |
| 202 | + |
| 203 | +MeshIter::operator bool () const |
| 204 | +{ |
| 205 | + return !m_done; |
| 206 | +} |
| 207 | + |
| 208 | +char * MeshIter::operator * () const |
| 209 | +{ |
| 210 | + return *m_data; |
| 211 | +} |
| 212 | + |
| 213 | + |
| 214 | +} // namespace python |
| 215 | +} // namespace pdal |
| 216 | + |
0 commit comments