1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24
25 #include "pxr/pxr.h"
26 #include "pxr/base/gf/size2.h"
27 #include "pxr/base/tf/pyUtils.h"
28 #include "pxr/base/tf/wrapTypeHelpers.h"
29 #include "pxr/base/tf/pyContainerConversions.h"
30
31 #include <boost/python/class.hpp>
32 #include <boost/python/implicit.hpp>
33 #include <boost/python/operators.hpp>
34 #include <boost/python/return_arg.hpp>
35
36 #include <string>
37
38 using namespace boost::python;
39
40 using std::string;
41
42 PXR_NAMESPACE_USING_DIRECTIVE
43
44 namespace {
45
46 static int
normalizeIndex(int index)47 normalizeIndex(int index) {
48 return TfPyNormalizeIndex(index, 2, true /*throw error*/);
49 }
50
__len__(const GfSize2 & self)51 static int __len__(const GfSize2 &self) {
52 return 2;
53 }
54
__getitem__(const GfSize2 & self,int index)55 static size_t __getitem__(const GfSize2 &self, int index) {
56 index = normalizeIndex(index);
57 return self[index];
58 }
59
__setitem__(GfSize2 & self,int index,size_t value)60 static void __setitem__(GfSize2 &self, int index, size_t value) {
61 index = normalizeIndex(index);
62 self[index] = value;
63 }
64
__contains__(const GfSize2 & self,size_t value)65 static bool __contains__(const GfSize2 &self, size_t value) {
66 if (self[0] == value || self[1] == value)
67 return true;
68 return false;
69 }
70
71 #if PY_MAJOR_VERSION == 2
__truediv__(const GfSize2 & self,int value)72 static GfSize2 __truediv__(const GfSize2 &self, int value)
73 {
74 return self / value;
75 }
76
__itruediv__(GfSize2 & self,int value)77 static GfSize2 __itruediv__(GfSize2 &self, int value)
78 {
79 return self /= value;
80 }
81 #endif
82
_Repr(GfSize2 const & self)83 static string _Repr(GfSize2 const &self) {
84 return TF_PY_REPR_PREFIX + "Size2(" + TfPyRepr(self[0]) + ", " + TfPyRepr(self[1]) + ")";
85 }
86
87 } // anonymous namespace
88
wrapSize2()89 void wrapSize2()
90 {
91 typedef GfSize2 This;
92
93 static const int dimension = 2;
94
95 class_<This>( "Size2", "A 2D size class", init<>() )
96 .def(init<const This &>())
97 .def(init<const GfVec2i &>())
98 .def(init<size_t, size_t>())
99
100 .def( TfTypePythonClass() )
101
102 .def("Set", (GfSize2 &(This::*)(size_t, size_t))&This::Set, return_self<>())
103
104 .def_readonly("dimension", dimension)
105
106 .def("__len__", __len__)
107 .def("__getitem__", __getitem__)
108 .def("__setitem__", __setitem__)
109 .def("__contains__", __contains__)
110
111 .def( str(self) )
112 .def( self == self )
113 .def( self != self )
114 .def( self += self )
115 .def( self -= self )
116 .def( self *= int() )
117 .def( self /= int() )
118 .def( self + self )
119 .def( self - self )
120 .def( self * self )
121 .def( int() * self )
122 .def( self * int() )
123 .def( self / int() )
124
125 #if PY_MAJOR_VERSION == 2
126 // Needed only to support "from __future__ import division" in
127 // python 2. In python 3 builds boost::python adds this for us.
128 .def("__truediv__", __truediv__ )
129 .def("__itruediv__", __itruediv__ )
130 #endif
131
132 .def("__repr__", _Repr)
133
134 ;
135 to_python_converter<std::vector<This>,
136 TfPySequenceToPython<std::vector<This> > >();
137
138 // conversion operator
139 implicitly_convertible<This, GfVec2i>();
140
141 }
142