| 1 |
// |
| 2 |
// SPDX-License-Identifier: BSD-3-Clause |
| 3 |
// Copyright Contributors to the OpenEXR Project. |
| 4 |
// |
| 5 |
|
| 6 |
// clang-format off |
| 7 |
|
| 8 |
#include <Python.h> |
| 9 |
#define BOOST_BIND_GLOBAL_PLACEHOLDERS |
| 10 |
#include <boost/python.hpp> |
| 11 |
#include <boost/python/make_constructor.hpp> |
| 12 |
#include <boost/format.hpp> |
| 13 |
#include "PyImathFun.h" |
| 14 |
#include "PyImathFunOperators.h" |
| 15 |
#include "PyImathDecorators.h" |
| 16 |
#include "PyImathExport.h" |
| 17 |
#include "PyImathAutovectorize.h" |
| 18 |
|
| 19 |
namespace PyImath { |
| 20 |
|
| 21 |
using namespace boost::python; |
| 22 |
|
| 23 |
namespace |
| 24 |
{ |
| 25 |
|
| 26 |
struct RegisterFloatDoubleOps |
| 27 |
{ |
| 28 |
template <typename T> |
| 29 |
void operator() (T) |
| 30 |
{ |
| 31 |
// nb: MSVC gets confused about which arg we want (it thinks it |
| 32 |
// might be boost::arg), so telling it which one explicitly here. |
| 33 |
typedef boost::python::arg arg; |
| 34 |
|
| 35 |
generate_bindings<abs_op<T>,boost::mpl::true_>( |
| 36 |
"abs", |
| 37 |
"return the absolute value of 'value'", |
| 38 |
(arg("value"))); |
| 39 |
|
| 40 |
generate_bindings<sign_op<T>,boost::mpl::true_>( |
| 41 |
"sign", |
| 42 |
"return 1 or -1 based on the sign of 'value'", |
| 43 |
(arg("value"))); |
| 44 |
|
| 45 |
generate_bindings<log_op<T>,boost::mpl::true_>( |
| 46 |
"log", |
| 47 |
"return the natural log of 'value'", |
| 48 |
(arg("value"))); |
| 49 |
|
| 50 |
generate_bindings<log10_op<T>,boost::mpl::true_>( |
| 51 |
"log10", |
| 52 |
"return the base 10 log of 'value'", |
| 53 |
(arg("value"))); |
| 54 |
|
| 55 |
generate_bindings<lerp_op<T>,boost::mpl::true_,boost::mpl::true_,boost::mpl::true_>( |
| 56 |
"lerp", |
| 57 |
"return the linear interpolation of 'a' to 'b' using parameter 't'", |
| 58 |
(arg("a"),arg("b"),arg("t"))); |
| 59 |
|
| 60 |
generate_bindings<lerpfactor_op<T>,boost::mpl::true_,boost::mpl::true_,boost::mpl::true_>( |
| 61 |
"lerpfactor", |
| 62 |
"return how far m is between a and b, that is return t such that\n" |
| 63 |
"if:\n" |
| 64 |
" t = lerpfactor(m, a, b);\n" |
| 65 |
"then:\n" |
| 66 |
" m = lerp(a, b, t);\n" |
| 67 |
"\n" |
| 68 |
"If a==b, return 0.\n", |
| 69 |
(arg("m"),arg("a"),arg("b"))); |
| 70 |
|
| 71 |
generate_bindings<clamp_op<T>,boost::mpl::true_,boost::mpl::true_,boost::mpl::true_>( |
| 72 |
"clamp", |
| 73 |
"return the value clamped to the range [low,high]", |
| 74 |
(arg("value"),arg("low"),arg("high"))); |
| 75 |
|
| 76 |
generate_bindings<floor_op<T>,boost::mpl::true_>( |
| 77 |
"floor", |
| 78 |
"return the closest integer less than or equal to 'value'", |
| 79 |
(arg("value"))); |
| 80 |
|
| 81 |
generate_bindings<ceil_op<T>,boost::mpl::true_>( |
| 82 |
"ceil", |
| 83 |
"return the closest integer greater than or equal to 'value'", |
| 84 |
(arg("value"))); |
| 85 |
|
| 86 |
generate_bindings<trunc_op<T>,boost::mpl::true_>( |
| 87 |
"trunc", |
| 88 |
"return the closest integer with magnitude less than or equal to 'value'", |
| 89 |
(arg("value"))); |
| 90 |
|
| 91 |
generate_bindings<rgb2hsv_op<T>,boost::mpl::true_>( |
| 92 |
"rgb2hsv", |
| 93 |
"return the hsv version of an rgb color", |
| 94 |
args("rgb")); |
| 95 |
|
| 96 |
generate_bindings<hsv2rgb_op<T>,boost::mpl::true_>( |
| 97 |
"hsv2rgb", |
| 98 |
"return the rgb version of an hsv color", |
| 99 |
args("hsv")); |
| 100 |
|
| 101 |
generate_bindings<sin_op<T>,boost::mpl::true_>( |
| 102 |
"sin", |
| 103 |
"return the sine of the angle theta", |
| 104 |
args("theta")); |
| 105 |
|
| 106 |
generate_bindings<cos_op<T>,boost::mpl::true_>( |
| 107 |
"cos", |
| 108 |
"return the cosine of the angle theta", |
| 109 |
args("theta")); |
| 110 |
|
| 111 |
generate_bindings<tan_op<T>,boost::mpl::true_>( |
| 112 |
"tan", |
| 113 |
"return the tangent of the angle theta", |
| 114 |
args("theta")); |
| 115 |
|
| 116 |
generate_bindings<asin_op<T>,boost::mpl::true_>( |
| 117 |
"asin", |
| 118 |
"return the arcsine of the value x", |
| 119 |
args("x")); |
| 120 |
|
| 121 |
generate_bindings<acos_op<T>,boost::mpl::true_>( |
| 122 |
"acos", |
| 123 |
"return the arccosine of the value x", |
| 124 |
args("x")); |
| 125 |
|
| 126 |
generate_bindings<atan_op<T>,boost::mpl::true_>( |
| 127 |
"atan", |
| 128 |
"return the arctangent of the value x", |
| 129 |
args("x")); |
| 130 |
|
| 131 |
generate_bindings<atan2_op<T>,boost::mpl::true_,boost::mpl::true_>( |
| 132 |
"atan2", |
| 133 |
"return the arctangent of the coordinate x,y - note the y " |
| 134 |
"is the first argument for consistency with libm ordering", |
| 135 |
args("y","x")); |
| 136 |
|
| 137 |
generate_bindings<sqrt_op<T>,boost::mpl::true_>( |
| 138 |
"sqrt", |
| 139 |
"return the square root of x", |
| 140 |
args("x")); |
| 141 |
|
| 142 |
generate_bindings<pow_op<T>,boost::mpl::true_,boost::mpl::true_>( |
| 143 |
"pow", |
| 144 |
"return x**y", |
| 145 |
args("x","y")); |
| 146 |
|
| 147 |
generate_bindings<exp_op<T>,boost::mpl::true_>( |
| 148 |
"exp", |
| 149 |
"return exp(x)", |
| 150 |
args("x")); |
| 151 |
|
| 152 |
generate_bindings<sinh_op<T>,boost::mpl::true_>( |
| 153 |
"sinh", |
| 154 |
"return sinh(x)", |
| 155 |
args("x")); |
| 156 |
|
| 157 |
generate_bindings<cosh_op<T>,boost::mpl::true_>( |
| 158 |
"cosh", |
| 159 |
"return cosh(x)", |
| 160 |
args("x")); |
| 161 |
|
| 162 |
def("cmp", IMATH_NAMESPACE::cmp<T>); |
| 163 |
def("cmpt", IMATH_NAMESPACE::cmpt<T>); |
| 164 |
def("iszero", IMATH_NAMESPACE::iszero<T>); |
| 165 |
def("equal", IMATH_NAMESPACE::equal<T, T, T>); |
| 166 |
} |
| 167 |
}; |
| 168 |
|
| 169 |
} // namespace |
| 170 |
|
| 171 |
void register_functions() |
| 172 |
{ |
| 173 |
// |
| 174 |
// Utility Functions |
| 175 |
// |
| 176 |
|
| 177 |
// nb: MSVC gets confused about which arg we want (it thinks it |
| 178 |
// might be boost::arg), so telling it which one explicitly here. |
| 179 |
typedef boost::python::arg arg; |
| 180 |
|
| 181 |
using fp_types = boost::mpl::vector<float, double>; |
| 182 |
boost::mpl::for_each<fp_types>(RegisterFloatDoubleOps()); |
| 183 |
|
| 184 |
generate_bindings<abs_op<int>,boost::mpl::true_>( |
| 185 |
"abs", |
| 186 |
"return the absolute value of 'value'", |
| 187 |
(arg("value"))); |
| 188 |
|
| 189 |
generate_bindings<sign_op<int>,boost::mpl::true_>( |
| 190 |
"sign", |
| 191 |
"return 1 or -1 based on the sign of 'value'", |
| 192 |
(arg("value"))); |
| 193 |
|
| 194 |
generate_bindings<clamp_op<int>,boost::mpl::true_,boost::mpl::true_,boost::mpl::true_>( |
| 195 |
"clamp", |
| 196 |
"return the value clamped to the range [low,high]", |
| 197 |
(arg("value"),arg("low"),arg("high"))); |
| 198 |
|
| 199 |
generate_bindings<divs_op,boost::mpl::true_,boost::mpl::true_>( |
| 200 |
"divs", |
| 201 |
"return x/y where the remainder has the same sign as x:\n" |
| 202 |
" divs(x,y) == (abs(x) / abs(y)) * (sign(x) * sign(y))\n", |
| 203 |
(arg("x"),arg("y"))); |
| 204 |
generate_bindings<mods_op,boost::mpl::true_,boost::mpl::true_>( |
| 205 |
"mods", |
| 206 |
"return x%y where the remainder has the same sign as x:\n" |
| 207 |
" mods(x,y) == x - y * divs(x,y)\n", |
| 208 |
(arg("x"),arg("y"))); |
| 209 |
|
| 210 |
generate_bindings<divp_op,boost::mpl::true_,boost::mpl::true_>( |
| 211 |
"divp", |
| 212 |
"return x/y where the remainder is always positive:\n" |
| 213 |
" divp(x,y) == floor (double(x) / double (y))\n", |
| 214 |
(arg("x"),arg("y"))); |
| 215 |
generate_bindings<modp_op,boost::mpl::true_,boost::mpl::true_>( |
| 216 |
"modp", |
| 217 |
"return x%y where the remainder is always positive:\n" |
| 218 |
" modp(x,y) == x - y * divp(x,y)\n", |
| 219 |
(arg("x"),arg("y"))); |
| 220 |
|
| 221 |
generate_bindings<bias_op,boost::mpl::true_,boost::mpl::true_>( |
| 222 |
"bias", |
| 223 |
"bias(x,b) is a gamma correction that remaps the unit interval such that bias(0.5, b) = b.", |
| 224 |
(arg("x"),arg("b"))); |
| 225 |
|
| 226 |
generate_bindings<gain_op,boost::mpl::true_,boost::mpl::true_>( |
| 227 |
"gain", |
| 228 |
"gain(x,g) is a gamma correction that remaps the unit interval with the property that gain(0.5, g) = 0.5.\n" |
| 229 |
"The gain function can be thought of as two scaled bias curves forming an 'S' shape in the unit interval.", |
| 230 |
(arg("x"),arg("g"))); |
| 231 |
|
| 232 |
// |
| 233 |
// Vectorized utility functions |
| 234 |
// |
| 235 |
generate_bindings<rotationXYZWithUpDir_op<float>,boost::mpl::true_,boost::mpl::true_,boost::mpl::true_>( |
| 236 |
"rotationXYZWithUpDir", |
| 237 |
"return the XYZ rotation vector that rotates 'fromDir' to 'toDir'" |
| 238 |
"using the up vector 'upDir'", |
| 239 |
args("fromDir","toDir","upDir")); |
| 240 |
} |
| 241 |
|
| 242 |
} // namespace PyImath |