24 #ifndef PXR_BASE_VT_WRAP_ARRAY_H
25 #define PXR_BASE_VT_WRAP_ARRAY_H
29 #include "pxr/base/vt/array.h"
53 #include <hboost/preprocessor/punctuation/comma_if.hpp>
54 #include <hboost/preprocessor/repetition/repeat.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;
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
const &) {
130 template <
typename T,
typename S>
133 slice::range<T*>&
range,
size_t setSize,
bool tile =
false)
136 const size_t length = len(value);
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
const &) {
189 const size_t setSize = 1 + (range.stop - range.start) / range.step;
198 const size_t length = val.size();
201 if (!tile && length < setSize) {
203 (
"Not enough values to set slice. Expected %zu, got %zu.",
209 for (
size_t i = 0; i != setSize; range.start += range.step, ++i) {
210 *range.start = val[i %
length];
215 else if (extract<T>(value).check()) {
223 for (
size_t i = 0; i != setSize; range.start += range.step, ++i) {
229 else if (extract<list>(value).check()) {
230 setArraySlice(
self, extract<list>(value)(), range, setSize, tile);
234 else if (extract<tuple>(value).check()) {
235 setArraySlice(
self, extract<tuple>(value)(), range, setSize, tile);
245 template <
typename T>
250 if (idx != ellipsis) {
251 PyErr_SetString(PyExc_TypeError,
"unsupported index type");
252 throw_error_already_set();
257 template <
typename T>
265 template <
typename T>
276 template <
class T,
class... Ts>
278 return (std::is_same_v<T, Ts> || ...);
281 template <
class T,
class TypeList>
283 return Vt_IsAnySameImpl<T>(TypeList{});
292 long long,
unsigned long long>;
298 template <
typename T>
300 return std::isfinite(value);
303 return std::isfinite(static_cast<float>(value));
306 template <
typename T>
307 static void streamValue(std::ostringstream &
stream, T
const &
value) {
310 if constexpr(Vt_IsAnySame<T, Vt_OptimizedStreamIntegralTypes>()) {
328 Vt_ComputeEffectiveRankAndLastDimSize(
331 unsigned int rank = sd->
GetRank();
337 1, [](
size_t x,
size_t y) {
return x * y; });
339 size_t remainder = divisor ? sd->
totalSize % divisor : 0;
340 *lastDimSize = divisor ? sd->
totalSize / divisor : 0;
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>
398 unique_ptr<VtArray<T> > ret(
new VtArray<T>(len(values)));
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();
441 template <typename T>
442 static std::
string _VtStr(T const &self)
447 template <
typename T>
450 using namespace Vt_WrapArray;
453 typedef typename This::ElementType
Type;
455 string name = GetVtArrayName<This>();
457 string docStr =
TfStringPrintf(
"An array of type %s.", typeStr.c_str());
459 auto selfCls = class_<This>(
name.c_str(), docStr.c_str(), no_init)
460 .setattr(
"_isVtArray",
true)
463 .def(
"__init__", make_constructor(VtArray__init__<Type>),
465 "__init__(values)\n\n"
466 "values: a sequence (tuple, list, or another VtArray with "
467 "element type convertible to the new array's element type)\n\n"
469 .def(
"__init__", make_constructor(VtArray__init__2<Type>))
470 .def(init<unsigned int>())
472 .def(
"__getitem__", getitem_ellipsis<Type>)
473 .def(
"__getitem__", getitem_slice<Type>)
474 .def(
"__getitem__", getitem_index<Type>)
475 .def(
"__setitem__", setitem_ellipsis<Type>)
476 .def(
"__setitem__", setitem_slice<Type>)
477 .def(
"__setitem__", setitem_index<Type>)
480 .def(
"__iter__", iterator<This>())
482 .def(
"__repr__", __repr__<Type>)
485 .def(
"__str__", _VtStr<T>)
489 #ifdef NUMERIC_OPERATORS
490 #define ADDITION_OPERATOR
491 #define SUBTRACTION_OPERATOR
492 #define MULTIPLICATION_OPERATOR
493 #define DIVISION_OPERATOR
494 #define UNARY_NEG_OPERATOR
497 #ifdef ADDITION_OPERATOR
500 #ifdef SUBTRACTION_OPERATOR
503 #ifdef MULTIPLICATION_OPERATOR
506 #ifdef DIVISION_OPERATOR
512 #ifdef DOUBLE_MULT_OPERATOR
513 .def(
self *
double())
514 .def(
double() *
self)
516 #ifdef DOUBLE_DIV_OPERATOR
517 .def(
self /
double())
519 #ifdef UNARY_NEG_OPERATOR
525 #define WRITE(z, n, data) HBOOST_PP_COMMA_IF(n) data
526 #define VtCat_DEF(z, n, unused) \
527 def("Cat",(VtArray<Type> (*)( HBOOST_PP_REPEAT(n, WRITE, VtArray<Type> const &) ))VtCat<Type>);
541 implicitly_convertible<This, TfSpan<Type> >();
542 implicitly_convertible<This, TfSpan<const Type> >();
546 template <
typename T>
549 using namespace Vt_WrapArray;
552 typedef typename This::ElementType
Type;
554 def(
"AnyTrue", VtAnyTrue<Type>);
555 def(
"AllTrue", VtAllTrue<Type>);
563 template <
class Array>
567 typedef typename Array::ElementType ElemType;
569 if (PySequence_Check(obj.ptr())) {
570 Py_ssize_t len = PySequence_Length(obj.ptr());
572 ElemType *elem = result.data();
573 for (Py_ssize_t i = 0; i != len; ++i) {
574 hboost::python::handle<>
h(PySequence_ITEM(obj.ptr(), i));
576 if (PyErr_Occurred())
580 hboost::python::extract<ElemType> e(h.get());
586 }
else if (PyIter_Check(obj.ptr())) {
588 while (PyObject *item = PyIter_Next(obj.ptr())) {
589 hboost::python::handle<>
h(item);
591 if (PyErr_Occurred())
595 hboost::python::extract<ElemType> e(h.get());
598 result.push_back(e());
605 template <
class Array,
class Iter>
609 typedef typename Array::ElementType ElemType;
611 for (ElemType *e = result.data(); begin !=
end; ++
begin) {
628 }
else if (v.
IsHolding<std::vector<VtValue> >()) {
629 std::vector<VtValue>
const &vec = v.
UncheckedGet<std::vector<VtValue> >();
630 ret = Vt_ConvertFromRange<T>(vec.begin(), vec.end());
636 template <
class Elem>
640 VtValue::RegisterCast<TfPyObjWrapper, Array>(Vt_CastToArray<Array>);
641 VtValue::RegisterCast<std::vector<VtValue>, Array>(Vt_CastToArray<Array>);
644 #define VT_WRAP_ARRAY(unused, elem) \
645 VtWrapArray< VtArray< VT_TYPE(elem) > >();
646 #define VT_WRAP_COMPARISON(unused, elem) \
647 VtWrapComparisonFunctions< VtArray< VT_TYPE(elem) > >();
651 #endif // PXR_BASE_VT_WRAP_ARRAY_H
TF_API std::string TfStringPrintf(const char *fmt,...)
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
#define VTOPERATOR_WRAPDECLARE(op, lmethod, rmethod)
#define VTOPERATOR_WRAPDECLARE_BOOL(func)
T const & UncheckedGet() const &
OIIO_UTIL_API bool copy(string_view from, string_view to, std::string &err)
GLsizei const GLchar *const * string
GLsizei const GLfloat * value
#define VTOPERATOR_WRAP_BOOL(func, op)
ARCH_API std::string ArchGetDemangled(const std::string &typeName)
GLuint GLsizei GLsizei * length
#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.
TF_API void TfPyThrowValueError(const char *msg)
**But if you need a result
bool IsEmpty() const
Returns true iff this value is empty.
bool _IsFinite(T const &value)
object getitem_index(VtArray< T > const &self, int64_t idx)
VtValue Vt_ConvertFromRange(Iter begin, Iter end)
constexpr bool Vt_IsAnySame()
VtValue Vt_CastToArray(VtValue const &v)
std::string TfPyRepr(T const &t)
OIIO_FORCEINLINE bool extract(const vbool4 &a)
void setitem_slice(VtArray< T > &self, slice idx, object value)
SYS_FORCE_INLINE const X * cast(const InstancablePtr *o)
object getitem_slice(VtArray< T > const &self, slice idx)
OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER class IMF_EXPORT_TEMPLATE_TYPE Array
TfMetaList< short, unsigned short, int, unsigned int, long, unsigned long, long long, unsigned long long > Vt_OptimizedStreamIntegralTypes
VtArray< T > * VtArray__init__(object const &values)
std::string TfStringify(const T &v)
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)
GLuint const GLchar * name
VT_API string GetVtArrayName()
#define ARCH_PRAGMA_UNARY_MINUS_ON_UNSIGNED
void VtRegisterValueCastsFromPythonSequencesToArray()
Register casts with VtValue from python sequences to VtArray types.
VtArray< T > * VtArray__init__2(size_t size, object const &values)
GLfloat GLfloat GLfloat GLfloat h
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
unsigned int otherDims[NumOtherDims]
GLenum GLsizei GLsizei GLint * values
VtValue Vt_ConvertFromPySequenceOrIter(TfPyObjWrapper const &obj)
void VtWrapComparisonFunctions()
object getitem_ellipsis(VtArray< T > const &self, object idx)
void setitem_index(VtArray< T > &self, int64_t idx, object value)
#define PXR_NAMESPACE_CLOSE_SCOPE
TF_API int64_t TfPyNormalizeIndex(int64_t index, uint64_t size, bool throwError=false)
bool accumulate(const PointDataTreeT &points, const std::string &attribute, typename PromoteType< ValueT >::Highest &total, const FilterT &filter, typename PointDataTreeT::template ValueConverter< ResultTreeT >::Type *totalTree)
Evaluates the total value of a point attribute and returns whether the value is valid. Optionally constructs localised total value trees.
#define TF_PY_REPR_PREFIX
constexpr bool Vt_IsAnySameImpl(TfMetaList< Ts...>)
SIM_API const UT_StringHolder distance
#define VtCat_DEF(z, n, unused)
#define VTOPERATOR_WRAP(lmethod, rmethod)
#define VTOPERATOR_WRAP_NONCOMM(lmethod, rmethod)
PcpNodeRef_ChildrenIterator begin(const PcpNodeRef::child_const_range &r)
Support for range-based for loops for PcpNodeRef children ranges.