24 #ifndef PXR_BASE_VT_WRAP_ARRAY_H
25 #define PXR_BASE_VT_WRAP_ARRAY_H
29 #include "pxr/base/vt/array.h"
51 #include <hboost/preprocessor/facilities/empty.hpp>
52 #include <hboost/preprocessor/punctuation/comma_if.hpp>
53 #include <hboost/preprocessor/repetition/repeat.hpp>
54 #include <hboost/preprocessor/seq/for_each.hpp>
56 #include <hboost/python/class.hpp>
57 #include <hboost/python/copy_const_reference.hpp>
58 #include <hboost/python/def.hpp>
59 #include <hboost/python/detail/api_placeholder.hpp>
60 #include <hboost/python/extract.hpp>
61 #include <hboost/python/implicit.hpp>
62 #include <hboost/python/iterator.hpp>
63 #include <hboost/python/make_constructor.hpp>
64 #include <hboost/python/object.hpp>
65 #include <hboost/python/operators.hpp>
66 #include <hboost/python/return_arg.hpp>
67 #include <hboost/python/slice.hpp>
68 #include <hboost/python/type_id.hpp>
69 #include <hboost/python/overloads.hpp>
80 namespace Vt_WrapArray {
82 using namespace hboost::python;
84 using std::unique_ptr;
92 object ellipsis =
object(handle<>(borrowed(Py_Ellipsis)));
93 if (idx != ellipsis) {
94 PyErr_SetString(PyExc_TypeError,
"unsupported index type");
95 throw_error_already_set();
100 template <
typename T>
104 static const bool throwError =
true;
109 template <
typename T>
114 slice::range<typename VtArray<T>::const_iterator>
range =
115 idx.get_indices(
self.
begin(),
self.
end());
116 const size_t setSize = 1 + (range.stop - range.start) / range.step;
119 for (; range.start != range.stop; range.start += range.step, ++i) {
125 catch (std::invalid_argument) {
130 template <
typename T,
typename S>
133 slice::range<T*>&
range,
size_t setSize,
bool tile =
false)
139 if (!tile && length < setSize) {
141 (
"Not enough values to set slice. Expected %zu, got %zu.",
148 std::vector<T> extracted;
149 extract<std::vector<T> > vectorExtraction(value);
150 if (vectorExtraction.check()) {
151 std::vector<T> tmp = vectorExtraction();
155 extracted.reserve(length);
156 for (
size_t i = 0; i !=
length; ++i) {
157 extracted.push_back(extract<T>(value[i]));
163 if (range.step == 1 && length >= setSize) {
164 std::copy(extracted.begin(), extracted.begin() + setSize, range.start);
167 for (
size_t i = 0; i != setSize; range.start += range.step, ++i) {
168 *range.start = extracted[i %
length];
173 template <
typename T>
178 slice::range<T*>
range;
180 T*
data =
self.data();
181 range = idx.get_indices(data, data +
self.
size());
183 catch (std::invalid_argument) {
189 const size_t setSize = 1 + (range.stop - range.start) / range.step;
194 const size_t length = val.size();
197 if (!tile && length < setSize) {
199 (
"Not enough values to set slice. Expected %zu, got %zu.",
205 for (
size_t i = 0; i != setSize; range.start += range.step, ++i) {
206 *range.start = val[i %
length];
211 else if (extract<T>(value).check()) {
219 for (
size_t i = 0; i != setSize; range.start += range.step, ++i) {
225 else if (extract<list>(value).check()) {
226 setArraySlice(
self, extract<list>(value)(), range, setSize, tile);
230 else if (extract<tuple>(value).check()) {
231 setArraySlice(
self, extract<tuple>(value)(), range, setSize, tile);
241 template <
typename T>
245 object ellipsis =
object(handle<>(borrowed(Py_Ellipsis)));
246 if (idx != ellipsis) {
247 PyErr_SetString(PyExc_TypeError,
"unsupported index type");
248 throw_error_already_set();
253 template <
typename T>
257 static const bool tile =
true;
261 template <
typename T>
275 template <
typename T>
276 static void streamValue(std::ostringstream &
stream, T
const &
value) {
282 #define _OPTIMIZED_STREAM_INTEGRAL_TYPES \
292 #define MAKE_STREAM_FUNC(r, unused, type) \
294 streamValue(std::ostringstream &stream, type const &value) { \
298 #undef MAKE_STREAM_FUNC
299 #undef _OPTIMIZED_STREAM_INTEGRAL_TYPES
305 template <
typename T>
306 static bool _IsFinite(T
const &
value) {
307 return std::isfinite(value);
310 return std::isfinite(static_cast<float>(value));
314 #define MAKE_STREAM_FUNC(r, unused, elem) \
316 streamValue(std::ostringstream &stream, VT_TYPE(elem) const &value) { \
317 if (_IsFinite(value)) { \
320 stream << TfPyRepr(value); \
325 #undef MAKE_STREAM_FUNC
328 Vt_ComputeEffectiveRankAndLastDimSize(
337 1, [](
size_t x,
size_t y) {
return x * y; });
348 template <
typename T>
355 std::ostringstream
stream;
356 stream.precision(17);
358 for (
size_t i = 0; i <
self.size(); ++i) {
359 stream << (i ?
", " :
"");
360 streamValue(stream,
self[i]);
362 stream << (
self.size() == 1 ?
",)" :
")");
367 self.
size(), stream.str().c_str());
377 size_t lastDimSize = 0;
379 Vt_ComputeEffectiveRankAndLastDimSize(shapeData, &lastDimSize);
382 for (
size_t i = 0; i != rank-1; ++i) {
384 i ?
", %d" :
"%d", shapeData->otherDims[i]);
388 repr.c_str(), shapeStr.c_str());
394 template <
typename T>
402 static const bool tile =
true;
404 return ret.release();
406 template <
typename T>
410 unique_ptr<VtArray<T> > ret(
new VtArray<T>(size));
414 static const bool tile =
true;
417 return ret.release();
428 VTOPERATOR_WRAP_NONCOMM(/,__div__,__rdiv__)
429 VTOPERATOR_WRAP_NONCOMM(%,__mod__,__rmod__)
440 template <typename T>
441 static std::
string _VtStr(T
const &self)
446 template <
typename T>
449 using namespace Vt_WrapArray;
452 typedef typename This::ElementType
Type;
454 string name = GetVtArrayName<This>();
456 string docStr =
TfStringPrintf(
"An array of type %s.", typeStr.c_str());
458 class_<This>(
name.c_str(), docStr.c_str(), no_init)
459 .setattr(
"_isVtArray",
true)
462 .def(
"__init__", make_constructor(VtArray__init__<Type>),
464 "__init__(values)\n\n"
465 "values: a sequence (tuple, list, or another VtArray with "
466 "element type convertible to the new array's element type)\n\n"
468 .def(
"__init__", make_constructor(VtArray__init__2<Type>))
469 .def(init<unsigned int>())
471 .def(
"__getitem__", getitem_ellipsis<Type>)
472 .def(
"__getitem__", getitem_slice<Type>)
473 .def(
"__getitem__", getitem_index<Type>)
474 .def(
"__setitem__", setitem_ellipsis<Type>)
475 .def(
"__setitem__", setitem_slice<Type>)
476 .def(
"__setitem__", setitem_index<Type>)
479 .def(
"__iter__", iterator<This>())
481 .def(
"__repr__", __repr__<Type>)
484 .def(
"__str__", _VtStr<T>)
488 #ifdef NUMERIC_OPERATORS
489 #define ADDITION_OPERATOR
490 #define SUBTRACTION_OPERATOR
491 #define MULTIPLICATION_OPERATOR
492 #define DIVISION_OPERATOR
493 #define UNARY_NEG_OPERATOR
496 #ifdef ADDITION_OPERATOR
499 #ifdef SUBTRACTION_OPERATOR
502 #ifdef MULTIPLICATION_OPERATOR
505 #ifdef DIVISION_OPERATOR
511 #ifdef DOUBLE_MULT_OPERATOR
512 .def(
self *
double())
513 .def(
double() *
self)
515 #ifdef DOUBLE_DIV_OPERATOR
516 .def(
self /
double())
518 #ifdef UNARY_NEG_OPERATOR
524 #define WRITE(z, n, data) HBOOST_PP_COMMA_IF(n) data
525 #define VtCat_DEF(z, n, unused) \
526 def("Cat",(VtArray<Type> (*)( HBOOST_PP_REPEAT(n, WRITE, VtArray<Type> const &) ))VtCat<Type>);
534 implicitly_convertible<This, TfSpan<Type> >();
535 implicitly_convertible<This, TfSpan<const Type> >();
539 template <
typename T>
542 using namespace Vt_WrapArray;
545 typedef typename This::ElementType
Type;
547 def(
"AnyTrue", VtAnyTrue<Type>);
548 def(
"AllTrue", VtAllTrue<Type>);
556 template <
class Array>
560 typedef typename Array::ElementType ElemType;
562 if (PySequence_Check(obj.
ptr())) {
563 Py_ssize_t
len = PySequence_Length(obj.
ptr());
565 ElemType *elem = result.data();
566 for (Py_ssize_t i = 0; i !=
len; ++i) {
567 hboost::python::handle<>
h(PySequence_ITEM(obj.
ptr(), i));
569 if (PyErr_Occurred())
573 hboost::python::extract<ElemType> e(h.get());
583 template <
class Array,
class Iter>
587 typedef typename Array::ElementType ElemType;
589 for (ElemType *e = result.data(); begin !=
end; ++
begin) {
590 VtValue cast = VtValue::Cast<ElemType>(*begin);
606 }
else if (v.
IsHolding<std::vector<VtValue> >()) {
607 std::vector<VtValue>
const &vec = v.
UncheckedGet<std::vector<VtValue> >();
608 ret = Vt_ConvertFromRange<T>(vec.begin(), vec.end());
614 template <
class Elem>
618 VtValue::RegisterCast<TfPyObjWrapper, Array>(Vt_CastToArray<Array>);
619 VtValue::RegisterCast<std::vector<VtValue>, Array>(Vt_CastToArray<Array>);
622 #define VT_WRAP_ARRAY(r, unused, elem) \
623 VtWrapArray< VtArray< VT_TYPE(elem) > >();
624 #define VT_WRAP_COMPARISON(r, unused, elem) \
625 VtWrapComparisonFunctions< VtArray< VT_TYPE(elem) > >();
629 #endif // PXR_BASE_VT_WRAP_ARRAY_H
TF_API std::string TfStringPrintf(const char *fmt,...)
T const & UncheckedGet() const
#define VTOPERATOR_WRAPDECLARE(op, lmethod, rmethod)
#define VTOPERATOR_WRAPDECLARE_BOOL(func)
GLuint const GLchar * name
FMT_CONSTEXPR auto begin(const C &c) -> decltype(c.begin())
GLuint const GLfloat * val
#define VTOPERATOR_WRAP_BOOL(func, op)
GLint GLsizei const GLuint64 * values
ARCH_API std::string ArchGetDemangled(const std::string &typeName)
TF_API void TfPyThrowValueError(std::string const &msg)
#define VT_FUNCTIONS_MAX_ARGS
unsigned int GetRank() const
string __repr__(VtArray< T > const &self)
#define ARCH_PRAGMA_UNSAFE_USE_OF_BOOL
VtValue & Swap(VtValue &rhs) noexcept
Swap this with rhs.
bool IsEmpty() const
Returns true iff this value is empty.
VtValue Vt_ConvertFromRange(Iter begin, Iter end)
#define MAKE_STREAM_FUNC(r, unused, type)
VtValue Vt_CastToArray(VtValue const &v)
TF_API PyObject * ptr() const
std::string TfPyRepr(T const &t)
void setitem_slice(VtArray< T > &self, slice idx, object value)
object getitem_slice(VtArray< T > const &self, slice idx)
VtValue Vt_ConvertFromPySequence(TfPyObjWrapper const &obj)
GLint GLint GLint GLint GLint x
GLint GLint GLint GLint GLint GLint y
#define _OPTIMIZED_STREAM_INTEGRAL_TYPES
GLint GLenum GLsizei GLint GLsizei const void * data
bool extract(const vbool4 &a)
VtArray< T > * VtArray__init__(object const &values)
GLuint GLsizei GLsizei * length
GLsizei GLsizei GLfloat distance
void setArraySlice(VtArray< T > &self, S value, slice::range< T * > &range, size_t setSize, bool tile=false)
void setitem_ellipsis(VtArray< T > &self, object idx, object value)
GLfloat GLfloat GLfloat GLfloat h
VT_API string GetVtArrayName()
#define ARCH_PRAGMA_UNARY_MINUS_ON_UNSIGNED
#define VTOPERATOR_WRAP(op, lmethod, rmethod)
void VtRegisterValueCastsFromPythonSequencesToArray()
Register casts with VtValue from python sequences to VtArray types.
#define VT_FLOATING_POINT_BUILTIN_VALUE_TYPES
GLsizei const GLchar *const * string
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
unsigned int otherDims[NumOtherDims]
void setitem_index(VtArray< T > &self, int idx, object value)
#define VTOPERATOR_WRAP_NONCOMM(op, lmethod, rmethod)
void VtWrapComparisonFunctions()
object getitem_ellipsis(VtArray< T > const &self, object idx)
size_t *lastDimSize unsigned int rank
OIIO_API bool copy(string_view from, string_view to, std::string &err)
object getitem_index(VtArray< T > const &self, int idx)
#define PXR_NAMESPACE_CLOSE_SCOPE
INT64 INT64 INT64 remainder
TF_API int64_t TfPyNormalizeIndex(int64_t index, uint64_t size, bool throwError=false)
#define TF_PY_REPR_PREFIX
#define VtCat_DEF(z, n, unused)
VtArray< T > * VtArray__init__2(unsigned int size, object const &values)
GLsizei const GLfloat * value
HBOOST_PP_SEQ_FOR_EACH(MAKE_STREAM_FUNC,~, VT_FLOATING_POINT_BUILTIN_VALUE_TYPES) static unsigned int Vt_ComputeEffectiveRankAndLastDimSize(Vt_ShapeData const *sd