HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PyImathBox.h
Go to the documentation of this file.
1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright Contributors to the OpenEXR Project.
4 //
5 
6 // clang-format off
7 
8 #ifndef _PyImathBox_h_
9 #define _PyImathBox_h_
10 
11 #include <Python.h>
12 #define HBOOST_BIND_GLOBAL_PLACEHOLDERS
13 #include <hboost/python.hpp>
14 #include <ImathBox.h>
15 #include "PyImathVec.h"
16 #include "PyImathFixedArray.h"
17 
18 namespace PyImath {
19 
20 template <class T> hboost::python::class_<IMATH_NAMESPACE::Box<T> > register_Box2();
21 template <class T> hboost::python::class_<IMATH_NAMESPACE::Box<T> > register_Box3();
22 
23 template <class T> hboost::python::class_<FixedArray<IMATH_NAMESPACE::Box<T> > > register_BoxArray();
24 
30 
36 
37 //
38 
39 // Other code in the Zeno code base assumes the existance of a class with the
40 // same name as the Imath class, and with static functions wrap() and
41 // convert() to produce a PyImath object from an Imath object and vice-versa,
42 // respectively. The class Boost generates from the Imath class does not
43 // have these properties, so we define a companion class here.
44 // The template argument, T, is the element type for the box (e.g., int,
45 // float).
46 
47 template <class T>
48 class Box2 {
49  public:
50  static PyObject * wrap (const IMATH_NAMESPACE::Box< IMATH_NAMESPACE::Vec2<T> > &b);
51  static int convert (PyObject *p, IMATH_NAMESPACE::Box< IMATH_NAMESPACE::Vec2<T> > *b);
52 };
53 
54 template <class T>
55 class Box3 {
56  public:
57  static PyObject * wrap (const IMATH_NAMESPACE::Box< IMATH_NAMESPACE::Vec3<T> > &b);
58  static int convert (PyObject *p, IMATH_NAMESPACE::Box< IMATH_NAMESPACE::Vec3<T> > *v);
59 };
60 
61 template <class T>
62 PyObject *
63 Box2<T>::wrap (const IMATH_NAMESPACE::Box< IMATH_NAMESPACE::Vec2<T> > &b)
64 {
65  typename hboost::python::return_by_value::apply < IMATH_NAMESPACE::Box< IMATH_NAMESPACE::Vec2<T> > >::type converter;
66  PyObject *p = converter (b);
67  return p;
68 }
69 
70 template <class T>
71 PyObject *
72 Box3<T>::wrap (const IMATH_NAMESPACE::Box< IMATH_NAMESPACE::Vec3<T> > &b)
73 {
74  typename hboost::python::return_by_value::apply < IMATH_NAMESPACE::Box< IMATH_NAMESPACE::Vec3<T> > >::type converter;
75  PyObject *p = converter (b);
76  return p;
77 }
78 
79 template <class T>
80 int
81 Box2<T>::convert (PyObject *p, IMATH_NAMESPACE::Box< IMATH_NAMESPACE::Vec2<T> > *v)
82 {
83  hboost::python::extract < IMATH_NAMESPACE::Box<IMATH_NAMESPACE::V2i> > extractorV2i (p);
84  if (extractorV2i.check())
85  {
86  IMATH_NAMESPACE::Box<IMATH_NAMESPACE::V2i> b = extractorV2i();
87  v->min = b.min;
88  v->max = b.max;
89  return 1;
90  }
91 
92  hboost::python::extract < IMATH_NAMESPACE::Box<IMATH_NAMESPACE::V2f> > extractorV2f (p);
93  if (extractorV2f.check())
94  {
95  IMATH_NAMESPACE::Box<IMATH_NAMESPACE::V2f> b = extractorV2f();
96  v->min = b.min;
97  v->max = b.max;
98  return 1;
99  }
100 
101  hboost::python::extract < IMATH_NAMESPACE::Box<IMATH_NAMESPACE::V2d> > extractorV2d (p);
102  if (extractorV2d.check())
103  {
104  IMATH_NAMESPACE::Box<IMATH_NAMESPACE::V2d> b = extractorV2d();
105  v->min = b.min;
106  v->max = b.max;
107  return 1;
108  }
109 
110  hboost::python::extract <hboost::python::tuple> extractorTuple (p);
111  if (extractorTuple.check())
112  {
113  hboost::python::tuple t = extractorTuple();
114  if (t.attr ("__len__") () == 2)
115  {
116  PyObject *minObj =
117  hboost::python::extract <hboost::python::object> (t[0])().ptr();
118  PyObject *maxObj =
119  hboost::python::extract <hboost::python::object> (t[1])().ptr();
120 
121  IMATH_NAMESPACE::Vec2<T> min, max;
122  if (! V2<T>::convert (minObj, &min))
123  return 0;
124  if (! V2<T>::convert (maxObj, &max))
125  return 0;
126 
127  v->min = min;
128  v->max = max;
129 
130  return 1;
131  }
132  }
133 
134  return 0;
135 }
136 
137 template <class T>
138 int
139 Box3<T>::convert (PyObject *p, IMATH_NAMESPACE::Box< IMATH_NAMESPACE::Vec3<T> > *v)
140 {
141  hboost::python::extract < IMATH_NAMESPACE::Box<IMATH_NAMESPACE::V3i> > extractorV3i (p);
142  if (extractorV3i.check())
143  {
144  IMATH_NAMESPACE::Box<IMATH_NAMESPACE::V3i> b = extractorV3i();
145  v->min = b.min;
146  v->max = b.max;
147  return 1;
148  }
149 
150  hboost::python::extract < IMATH_NAMESPACE::Box<IMATH_NAMESPACE::V3f> > extractorV3f (p);
151  if (extractorV3f.check())
152  {
153  IMATH_NAMESPACE::Box<IMATH_NAMESPACE::V3f> b = extractorV3f();
154  v->min = b.min;
155  v->max = b.max;
156  return 1;
157  }
158 
159  hboost::python::extract < IMATH_NAMESPACE::Box<IMATH_NAMESPACE::V3d> > extractorV3d (p);
160  if (extractorV3d.check())
161  {
162  IMATH_NAMESPACE::Box<IMATH_NAMESPACE::V3d> b = extractorV3d();
163  v->min = b.min;
164  v->max = b.max;
165  return 1;
166  }
167 
168  hboost::python::extract <hboost::python::tuple> extractorTuple (p);
169  if (extractorTuple.check())
170  {
171  hboost::python::tuple t = extractorTuple();
172  if (t.attr ("__len__") () == 2)
173  {
174  PyObject *minObj =
175  hboost::python::extract <hboost::python::object> (t[0])().ptr();
176  PyObject *maxObj =
177  hboost::python::extract <hboost::python::object> (t[1])().ptr();
178 
179  IMATH_NAMESPACE::Vec3<T> min, max;
180  if (! V3<T>::convert (minObj, &min))
181  return 0;
182  if (! V3<T>::convert (maxObj, &max))
183  return 0;
184 
185  v->min = min;
186  v->max = max;
187 
188  return 1;
189  }
190  }
191 
192  return 0;
193 }
194 
195 typedef Box2<int> Box2i;
199 
200 typedef Box3<int> Box3i;
204 
205 }
206 
207 #endif
Box2< double > Box2d
Definition: PyImathBox.h:198
static PyObject * wrap(const IMATH_NAMESPACE::Box< IMATH_NAMESPACE::Vec2< T > > &b)
Definition: PyImathBox.h:63
FixedArray< IMATH_NAMESPACE::Box2f > Box2fArray
Definition: PyImathBox.h:28
FixedArray< IMATH_NAMESPACE::Box3f > Box3fArray
Definition: PyImathBox.h:34
const GLdouble * v
Definition: glcorearb.h:837
FixedArray< IMATH_NAMESPACE::Box2d > Box2dArray
Definition: PyImathBox.h:29
Box2< int > Box2i
Definition: PyImathBox.h:195
Box3< float > Box3f
Definition: PyImathBox.h:202
ImageBuf OIIO_API min(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
FixedArray< IMATH_NAMESPACE::Box3s > Box3sArray
Definition: PyImathBox.h:31
Box3< int64_t > Box3i64
Definition: PyImathBox.h:201
FixedArray< IMATH_NAMESPACE::Box2s > Box2sArray
Definition: PyImathBox.h:25
FixedArray< IMATH_NAMESPACE::Box3d > Box3dArray
Definition: PyImathBox.h:35
FixedArray< IMATH_NAMESPACE::Box2i > Box2iArray
Definition: PyImathBox.h:26
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
FixedArray< IMATH_NAMESPACE::Box3i64 > Box3i64Array
Definition: PyImathBox.h:33
FixedArray< IMATH_NAMESPACE::Box3i > Box3iArray
Definition: PyImathBox.h:32
Box2< int64_t > Box2i64
Definition: PyImathBox.h:196
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
FixedArray< IMATH_NAMESPACE::Box2i64 > Box2i64Array
Definition: PyImathBox.h:27
GLdouble t
Definition: glad.h:2397
Box3< double > Box3d
Definition: PyImathBox.h:203
hboost::python::class_< FixedArray< IMATH_NAMESPACE::Box< T > > > register_BoxArray()
UT_BoxT< FT > Box
Definition: UT_RTreeImpl.h:674
hboost::python::class_< IMATH_NAMESPACE::Box< T > > register_Box2()
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
Box3< int > Box3i
Definition: PyImathBox.h:200
static PyObject * wrap(const IMATH_NAMESPACE::Box< IMATH_NAMESPACE::Vec3< T > > &b)
Definition: PyImathBox.h:72
static int convert(PyObject *p, IMATH_NAMESPACE::Box< IMATH_NAMESPACE::Vec3< T > > *v)
Definition: PyImathBox.h:139
hboost::python::class_< IMATH_NAMESPACE::Box< T > > register_Box3()
Box2< float > Box2f
Definition: PyImathBox.h:197
static int convert(PyObject *p, IMATH_NAMESPACE::Box< IMATH_NAMESPACE::Vec2< T > > *b)
Definition: PyImathBox.h:81