/////////////////////////////////////////////////////////////////////////// // // Copyright (c) 2007-2011, Industrial Light & Magic, a division of Lucas // Digital Ltd. LLC // // 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 Industrial Light & Magic 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. // /////////////////////////////////////////////////////////////////////////// #ifndef _PyImathFixedArray2D_h_ #define _PyImathFixedArray2D_h_ #include #include #include #include #include #include #include "PyImathFixedArray.h" #include "PyImathOperators.h" #include namespace PyImath { template class FixedArray2D { T * _ptr; IMATH_NAMESPACE::Vec2 _length; IMATH_NAMESPACE::Vec2 _stride; size_t _size; //flattened size of the array // this handle optionally stores a shared_array to allocated array data // so that everything is freed properly on exit. boost::any _handle; public: FixedArray2D(T *ptr, Py_ssize_t lengthX, Py_ssize_t lengthY, Py_ssize_t strideX = 1) : _ptr(ptr), _length(lengthX, lengthY), _stride(strideX, lengthX), _handle() { if (lengthX < 0 || lengthY < 0) throw IEX_NAMESPACE::LogicExc("Fixed array 2d lengths must be non-negative"); if (strideX <= 0) throw IEX_NAMESPACE::LogicExc("Fixed array 2d strides must be positive"); initializeSize(); //std::cout << "fixed array external construct" << std::endl; // nothing } FixedArray2D(T *ptr, Py_ssize_t lengthX, Py_ssize_t lengthY, Py_ssize_t strideX, Py_ssize_t strideY) : _ptr(ptr), _length(lengthX, lengthY), _stride(strideX, strideY), _handle() { if (lengthX < 0 || lengthY < 0) throw IEX_NAMESPACE::LogicExc("Fixed array 2d lengths must be non-negative"); if (strideX <= 0 || strideY < 0) throw IEX_NAMESPACE::LogicExc("Fixed array 2d strides must be positive"); initializeSize(); //std::cout << "fixed array external construct" << std::endl; // nothing } FixedArray2D(T *ptr, Py_ssize_t lengthX, Py_ssize_t lengthY, Py_ssize_t strideX, Py_ssize_t strideY, boost::any handle) : _ptr(ptr), _length(lengthX, lengthY), _stride(strideX, strideY), _handle(handle) { initializeSize(); //std::cout << "fixed array external construct with handle" << std::endl; // nothing } explicit FixedArray2D(Py_ssize_t lengthX, Py_ssize_t lengthY) : _ptr(0), _length(lengthX, lengthY), _stride(1, lengthX), _handle() { if (lengthX < 0 || lengthY < 0) throw IEX_NAMESPACE::LogicExc("Fixed array 2d lengths must be non-negative"); initializeSize(); T tmp = FixedArrayDefaultValue::value(); boost::shared_array a(new T[_size]); for (size_t i=0; i<_size; ++i) a[i] = tmp; _handle = a; _ptr = a.get(); } explicit FixedArray2D(const IMATH_NAMESPACE::V2i& length) : _ptr(0), _length(length), _stride(1, length.x), _handle() { if (length.x < 0 || length.y < 0) throw IEX_NAMESPACE::LogicExc("Fixed array 2d lengths must be non-negative"); initializeSize(); T tmp = FixedArrayDefaultValue::value(); boost::shared_array a(new T[_size]); for (size_t i=0; i<_size; ++i) a[i] = tmp; _handle = a; _ptr = a.get(); } FixedArray2D(const T &initialValue, Py_ssize_t lengthX, Py_ssize_t lengthY) : _ptr(0), _length(lengthX, lengthY), _stride(1, lengthX), _handle() { if (lengthX < 0 || lengthY < 0) throw IEX_NAMESPACE::LogicExc("Fixed array 2d lengths must be non-negative"); initializeSize(); boost::shared_array a(new T[_size]); for (size_t i=0; i<_size; ++i) a[i] = initialValue; _handle = a; _ptr = a.get(); } void initializeSize() { _size = _length.x*_length.y; } template explicit FixedArray2D(const FixedArray2D &other) : _ptr(0), _length(other.len()), _stride(1, other.len().x), _handle() { initializeSize(); boost::shared_array a(new T[_size]); size_t z = 0; for (size_t j = 0; j < _length.y; ++j) for (size_t i = 0; i < _length.x; ++i) a[z++] = T(other(i,j)); _handle = a; _ptr = a.get(); } FixedArray2D(const FixedArray2D &other) : _ptr(other._ptr), _length(other._length), _stride(other._stride), _size(other._size), _handle(other._handle) { //std::cout << "fixed array copy consturct construct" << std::endl; // nothing } const FixedArray2D & operator = (const FixedArray2D &other) { if (&other == this) return *this; //std::cout << "fixed array assign" << std::endl; _ptr = other._ptr; _length = other._length; _stride = other._stride; _handle = other._handle; _size = _length.x*_length.y; return *this; } ~FixedArray2D() { //std::cout << "fixed array delete" << std::endl; } const boost::any & handle() { return _handle; } size_t canonical_index(Py_ssize_t index, size_t length) const { if (index < 0) index += length; if (index >= length || index < 0) { PyErr_SetString(PyExc_IndexError, "Index out of range"); boost::python::throw_error_already_set(); } return index; } void extract_slice_indices(PyObject *index, size_t length, size_t &start, size_t &end, Py_ssize_t &step, size_t &slicelength) const { if (PySlice_Check(index)) { PySliceObject *slice = reinterpret_cast(index); Py_ssize_t s, e, sl; if (PySlice_GetIndicesEx(slice,length,&s,&e,&step,&sl) == -1) { boost::python::throw_error_already_set(); } if (s < 0 || e < 0 || sl < 0) { throw IEX_NAMESPACE::LogicExc("Slice extraction produced invalid start, end, or length indices"); } start = s; end = e; slicelength = sl; } else if (PyInt_Check(index)) { size_t i = canonical_index(PyInt_AsSsize_t(index), length); start = i; end = i+1; step = 1; slicelength = 1; } else { PyErr_SetString(PyExc_TypeError, "Object is not a slice"); boost::python::throw_error_already_set(); } //std::cout << "Slice indices are " << start << " " << end << " " << step << " " << slicelength << std::endl; } // return_internal_reference doesn't seem to work with non-class types typedef typename boost::mpl::if_,T&,T>::type get_type; // get_type getitem(Py_ssize_t index) const { return _ptr[canonical_index(index)*_stride]; } //FIXME: const does not work here with at least IMATH_NAMESPACE::Color4, why it works for V3fArray? get_type getitem(Py_ssize_t i, Py_ssize_t j) //const { return (*this)(canonical_index(i, _length.x), canonical_index(j, _length.y)); } //FIXME: anyway to seperate 2:3,4:5 from 2,4? we'd like to return int for the second one, and also 1d array for 2, 4:5 or 2:3, 4 FixedArray2D getslice(PyObject *index) const { if (PyTuple_Check(index) && PyTuple_Size(index) == 2) { size_t startx=0, endx=0, slicelengthx=0; size_t starty=0, endy=0, slicelengthy=0; Py_ssize_t stepx=0; Py_ssize_t stepy=0; extract_slice_indices(PyTuple_GetItem(index, 0),_length.x,startx,endx,stepx,slicelengthx); extract_slice_indices(PyTuple_GetItem(index, 1),_length.y,starty,endy,stepy,slicelengthy); FixedArray2D f(slicelengthx, slicelengthy); for (size_t j=0,z=0; j &mask) const { // size_t len = match_dimension(mask); // size_t slicelength = 0; // for (size_t i=0; i len = match_dimension(mask); FixedArray2D f(len); for (size_t j=0; j(index[0]); // Py_ssize_t j = boost::python::extract(index[1]); // (*this)(i,j) = data; // } void setitem_scalar(PyObject *index, const T &data) { if (!PyTuple_Check(index) || PyTuple_Size(index) != 2) { PyErr_SetString(PyExc_TypeError, "Slice syntax error"); boost::python::throw_error_already_set(); } size_t startx=0, endx=0, slicelengthx=0; size_t starty=0, endy=0, slicelengthy=0; Py_ssize_t stepx=0; Py_ssize_t stepy=0; extract_slice_indices(PyTuple_GetItem(index, 0),_length.x,startx,endx,stepx,slicelengthx); extract_slice_indices(PyTuple_GetItem(index, 1),_length.y,starty,endy,stepy,slicelengthy); for (size_t j=0; j &mask, const T &data) { IMATH_NAMESPACE::Vec2 len = match_dimension(mask); for (size_t j = 0; j < len.y; j++) for (size_t i=0; i(slicelengthx, slicelengthy)) { PyErr_SetString(PyExc_IndexError, "Dimensions of source do not match destination"); boost::python::throw_error_already_set(); } for (size_t i=0; i &mask, const FixedArray2D &data) { IMATH_NAMESPACE::Vec2 len = match_dimension(mask); if (data.len() == len) { for (size_t j = 0; j < len.y; j++) for (size_t i=0; i &mask, const FixedArray &data) { IMATH_NAMESPACE::Vec2 len = match_dimension(mask); if (data.len() == len.x*len.y) { for (size_t j = 0, z = 0; j < len.y; j++) for (size_t i=0; i &data) { //TODO:sanity check size_t startx=0, endx=0, slicelengthx=0; size_t starty=0, endy=0, slicelengthy=0; Py_ssize_t stepx=0; Py_ssize_t stepy=0; extract_slice_indices(PyTuple_GetItem(index, 0),_length.x,startx,endx,stepx,slicelengthx); extract_slice_indices(PyTuple_GetItem(index, 1),_length.y,starty,endy,stepy,slicelengthy); // we have a valid range of indices if (data.len() != slicelengthx*slicelengthy) { PyErr_SetString(PyExc_IndexError, "Dimensions of source data do not match destination"); boost::python::throw_error_already_set(); } for (size_t j=0, z=0; j len() const { return _length; } IMATH_NAMESPACE::Vec2 stride() const { return _stride; } T & operator () (size_t i, size_t j) { return _ptr[_stride.x*(j*_stride.y + i)]; } const T & operator () (size_t i, size_t j) const { return _ptr[_stride.x*(j*_stride.y + i)]; } size_t totalLen() const { return _size; } boost::python::tuple size() const { return boost::python::make_tuple(_length.x, _length.y); } static boost::python::class_ > register_(const char *name, const char *doc) { // a little tricky, but here we go - class types return internal references // but fundemental types just get copied. this typedef sets up the appropriate // call policy for each type. typedef typename boost::mpl::if_< boost::is_class, boost::python::return_internal_reference<>, boost::python::default_call_policies>::type call_policy; boost::python::class_ > c(name,doc, boost::python::init( "construct an array of the specified length initialized to the default value for the type")); c .def(boost::python::init &>("construct an array with the same values as the given array")) .def(boost::python::init("construct an array of the specified length initialized to the specified default value")) .def("__getitem__", &FixedArray2D::getslice) .def("__getitem__", &FixedArray2D::getslice_mask) // .def("__getitem__", &FixedArray2D::getitem, call_policy()) .def("item", &FixedArray2D::getitem, call_policy()) // .def("__setitem__", &FixedArray2D::setitem) .def("__setitem__", &FixedArray2D::setitem_scalar) .def("__setitem__", &FixedArray2D::setitem_scalar_mask) .def("__setitem__", &FixedArray2D::setitem_vector) .def("__setitem__", &FixedArray2D::setitem_vector_mask) .def("__setitem__", &FixedArray2D::setitem_array1d) .def("__setitem__", &FixedArray2D::setitem_array1d_mask) .def("__len__",&FixedArray2D::totalLen) .def("size",&FixedArray2D::size) .def("ifelse",&FixedArray2D::ifelse_scalar) .def("ifelse",&FixedArray2D::ifelse_vector) ; return c; } // template // size_t match_dimension(const FixedArray &a1) const // { // if (_length.x != a1.len()) { // PyErr_SetString(PyExc_IndexError, "Dimensions of source do not match destination"); // boost::python::throw_error_already_set(); // } // return _length.x; // } template IMATH_NAMESPACE::Vec2 match_dimension(const FixedArray2D &a1) const { if (len() != a1.len()) { PyErr_SetString(PyExc_IndexError, "Dimensions of source do not match destination"); boost::python::throw_error_already_set(); } return len(); } FixedArray2D ifelse_vector(const FixedArray2D &choice, const FixedArray2D &other) { IMATH_NAMESPACE::Vec2 len = match_dimension(choice); match_dimension(other); FixedArray2D tmp(len); // should use default construction but V3f doens't initialize for (size_t j = 0; j < len.y; ++j) for (size_t i = 0; i < len.x; ++i) tmp(i,j) = choice(i,j) ? (*this)(i,j) : other(i,j); return tmp; } FixedArray2D ifelse_scalar(const FixedArray2D &choice, const T &other) { IMATH_NAMESPACE::Vec2 len = match_dimension(choice); FixedArray2D tmp(len); // should use default construction but V3f doens't initialize for (size_t j = 0; j < len.y; ++j) for (size_t i = 0; i < len.x; ++i) tmp(i,j) = choice(i,j) ? (*this)(i,j) : other; return tmp; } }; // unary operation application template