| 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/format.hpp> |
| 12 |
#include <boost/python/make_constructor.hpp> |
| 13 |
#include "PyImath.h" |
| 14 |
#include "PyImathMathExc.h" |
| 15 |
#include "PyImathFixedArray.h" |
| 16 |
#include "PyImathRandom.h" |
| 17 |
#include "PyImathDecorators.h" |
| 18 |
|
| 19 |
namespace PyImath{ |
| 20 |
using namespace boost::python; |
| 21 |
|
| 22 |
template <class Rand, class T> |
| 23 |
static T |
| 24 |
nextf2 (Rand &rand, T min, T max) |
| 25 |
{ |
| 26 |
MATH_EXC_ON; |
| 27 |
return rand.nextf(min, max); |
| 28 |
} |
| 29 |
|
| 30 |
template <class Rand> |
| 31 |
static float |
| 32 |
nextGauss (Rand &rand) |
| 33 |
{ |
| 34 |
MATH_EXC_ON; |
| 35 |
return gaussRand(rand); |
| 36 |
} |
| 37 |
|
| 38 |
template <class T, class Rand> |
| 39 |
static IMATH_NAMESPACE::Vec3<T> nextGaussSphere(Rand &rand, const IMATH_NAMESPACE::Vec3<T> &v) |
| 40 |
{ |
| 41 |
MATH_EXC_ON; |
| 42 |
return IMATH_NAMESPACE::gaussSphereRand<IMATH_NAMESPACE::Vec3<T>,Rand>(rand); |
| 43 |
} |
| 44 |
template <class T, class Rand> |
| 45 |
static IMATH_NAMESPACE::Vec2<T> nextGaussSphere(Rand &rand, const IMATH_NAMESPACE::Vec2<T> &v) |
| 46 |
{ |
| 47 |
MATH_EXC_ON; |
| 48 |
return IMATH_NAMESPACE::gaussSphereRand<IMATH_NAMESPACE::Vec2<T>,Rand>(rand); |
| 49 |
} |
| 50 |
|
| 51 |
template <class T, class Rand> |
| 52 |
static IMATH_NAMESPACE::Vec3<T> nextHollowSphere(Rand &rand, const IMATH_NAMESPACE::Vec3<T> &v) |
| 53 |
{ |
| 54 |
MATH_EXC_ON; |
| 55 |
return IMATH_NAMESPACE::hollowSphereRand<IMATH_NAMESPACE::Vec3<T>,Rand>(rand); |
| 56 |
} |
| 57 |
|
| 58 |
template <class T, class Rand> |
| 59 |
static IMATH_NAMESPACE::Vec2<T> nextHollowSphere(Rand &rand, const IMATH_NAMESPACE::Vec2<T> &v) |
| 60 |
{ |
| 61 |
MATH_EXC_ON; |
| 62 |
return IMATH_NAMESPACE::hollowSphereRand<IMATH_NAMESPACE::Vec2<T>,Rand>(rand); |
| 63 |
} |
| 64 |
|
| 65 |
template <class T, class Rand> |
| 66 |
static IMATH_NAMESPACE::Vec3<T> nextSolidSphere(Rand &rand, const IMATH_NAMESPACE::Vec3<T> &v) |
| 67 |
{ |
| 68 |
MATH_EXC_ON; |
| 69 |
return IMATH_NAMESPACE::solidSphereRand<IMATH_NAMESPACE::Vec3<T>,Rand>(rand); |
| 70 |
} |
| 71 |
|
| 72 |
template <class T, class Rand> |
| 73 |
static IMATH_NAMESPACE::Vec2<T> nextSolidSphere(Rand &rand, const IMATH_NAMESPACE::Vec2<T> &v) |
| 74 |
{ |
| 75 |
MATH_EXC_ON; |
| 76 |
return IMATH_NAMESPACE::solidSphereRand<IMATH_NAMESPACE::Vec2<T>,Rand>(rand); |
| 77 |
} |
| 78 |
|
| 79 |
template <class Rand> |
| 80 |
static Rand *Rand_constructor1(unsigned long int seed) |
| 81 |
{ |
| 82 |
return new Rand(seed); |
| 83 |
} |
| 84 |
|
| 85 |
template <class Rand> |
| 86 |
static Rand *Rand_constructor2(Rand rand) |
| 87 |
{ |
| 88 |
Rand *r = new Rand(); |
| 89 |
*r = rand; |
| 90 |
|
| 91 |
return r; |
| 92 |
} |
| 93 |
|
| 94 |
template <class T, class Rand> |
| 95 |
static PyImath::FixedArray<IMATH_NAMESPACE::Vec3<T> > |
| 96 |
hollowSphereRand(Rand &rand, int num) |
| 97 |
{ |
| 98 |
MATH_EXC_ON; |
| 99 |
PyImath::FixedArray<IMATH_NAMESPACE::Vec3<T> > retval(num); |
| 100 |
for (int i=0; i<num; ++i) { |
| 101 |
retval[i] = IMATH_NAMESPACE::hollowSphereRand<IMATH_NAMESPACE::Vec3<T>,Rand>(rand); |
| 102 |
} |
| 103 |
return retval; |
| 104 |
} |
| 105 |
|
| 106 |
template <class T, class Rand> |
| 107 |
static PyImath::FixedArray<IMATH_NAMESPACE::Vec3<T> > |
| 108 |
solidSphereRand(Rand &rand, int num) |
| 109 |
{ |
| 110 |
MATH_EXC_ON; |
| 111 |
PyImath::FixedArray<IMATH_NAMESPACE::Vec3<T> > retval(num); |
| 112 |
for (int i=0; i<num; ++i) { |
| 113 |
retval[i] = IMATH_NAMESPACE::solidSphereRand<IMATH_NAMESPACE::Vec3<T>,Rand>(rand); |
| 114 |
} |
| 115 |
return retval; |
| 116 |
} |
| 117 |
|
| 118 |
PYIMATH_EXPORT |
| 119 |
class_<IMATH_NAMESPACE::Rand32> |
| 120 |
register_Rand32() |
| 121 |
{ |
| 122 |
float (IMATH_NAMESPACE::Rand32::*nextf1)(void) = &IMATH_NAMESPACE::Rand32::nextf; |
| 123 |
|
| 124 |
IMATH_NAMESPACE::Vec3<float> (*nextGaussSphere1)(IMATH_NAMESPACE::Rand32 &, const IMATH_NAMESPACE::Vec3<float> &v) = &nextGaussSphere<float,IMATH_NAMESPACE::Rand32>; |
| 125 |
IMATH_NAMESPACE::Vec3<double> (*nextGaussSphere2)(IMATH_NAMESPACE::Rand32 &, const IMATH_NAMESPACE::Vec3<double> &v) = &nextGaussSphere<double,IMATH_NAMESPACE::Rand32>; |
| 126 |
IMATH_NAMESPACE::Vec2<float> (*nextGaussSphere3)(IMATH_NAMESPACE::Rand32 &, const IMATH_NAMESPACE::Vec2<float> &v) = &nextGaussSphere<float,IMATH_NAMESPACE::Rand32>; |
| 127 |
IMATH_NAMESPACE::Vec2<double> (*nextGaussSphere4)(IMATH_NAMESPACE::Rand32 &, const IMATH_NAMESPACE::Vec2<double> &v) = &nextGaussSphere<double,IMATH_NAMESPACE::Rand32>; |
| 128 |
|
| 129 |
IMATH_NAMESPACE::Vec3<float> (*nextHollowSphere1)(IMATH_NAMESPACE::Rand32 &, const IMATH_NAMESPACE::Vec3<float> &v) = &nextHollowSphere<float,IMATH_NAMESPACE::Rand32>; |
| 130 |
IMATH_NAMESPACE::Vec3<double> (*nextHollowSphere2)(IMATH_NAMESPACE::Rand32 &, const IMATH_NAMESPACE::Vec3<double> &v) = &nextHollowSphere<double,IMATH_NAMESPACE::Rand32>; |
| 131 |
IMATH_NAMESPACE::Vec2<float> (*nextHollowSphere3)(IMATH_NAMESPACE::Rand32 &, const IMATH_NAMESPACE::Vec2<float> &v) = &nextHollowSphere<float,IMATH_NAMESPACE::Rand32>; |
| 132 |
IMATH_NAMESPACE::Vec2<double> (*nextHollowSphere4)(IMATH_NAMESPACE::Rand32 &, const IMATH_NAMESPACE::Vec2<double> &v) = &nextHollowSphere<double,IMATH_NAMESPACE::Rand32>; |
| 133 |
|
| 134 |
IMATH_NAMESPACE::Vec3<float> (*nextSolidSphere1)(IMATH_NAMESPACE::Rand32 &, const IMATH_NAMESPACE::Vec3<float> &v) = &nextSolidSphere<float,IMATH_NAMESPACE::Rand32>; |
| 135 |
IMATH_NAMESPACE::Vec3<double> (*nextSolidSphere2)(IMATH_NAMESPACE::Rand32 &, const IMATH_NAMESPACE::Vec3<double> &v) = &nextSolidSphere<double,IMATH_NAMESPACE::Rand32>; |
| 136 |
IMATH_NAMESPACE::Vec2<float> (*nextSolidSphere3)(IMATH_NAMESPACE::Rand32 &, const IMATH_NAMESPACE::Vec2<float> &v) = &nextSolidSphere<float,IMATH_NAMESPACE::Rand32>; |
| 137 |
IMATH_NAMESPACE::Vec2<double> (*nextSolidSphere4)(IMATH_NAMESPACE::Rand32 &, const IMATH_NAMESPACE::Vec2<double> &v) = &nextSolidSphere<double,IMATH_NAMESPACE::Rand32>; |
| 138 |
|
| 139 |
class_< IMATH_NAMESPACE::Rand32 > rand32_class("Rand32"); |
| 140 |
rand32_class |
| 141 |
.def(init<>("default construction")) |
| 142 |
.def("__init__", make_constructor(Rand_constructor1<IMATH_NAMESPACE::Rand32>)) |
| 143 |
.def("__init__", make_constructor(Rand_constructor2<IMATH_NAMESPACE::Rand32>)) |
| 144 |
.def("init", &IMATH_NAMESPACE::Rand32::init, |
| 145 |
"r.init(i) -- initialize with integer " |
| 146 |
"seed i") |
| 147 |
|
| 148 |
.def("nexti", &IMATH_NAMESPACE::Rand32::nexti, |
| 149 |
"r.nexti() -- return the next integer " |
| 150 |
"value in the uniformly-distributed " |
| 151 |
"sequence") |
| 152 |
.def("nextf", nextf1, |
| 153 |
"r.nextf() -- return the next floating-point " |
| 154 |
"value in the uniformly-distributed " |
| 155 |
"sequence\n" |
| 156 |
|
| 157 |
"r.nextf(float, float) -- return the next floating-point " |
| 158 |
"value in the uniformly-distributed " |
| 159 |
"sequence") |
| 160 |
.def("nextf", &nextf2 <IMATH_NAMESPACE::Rand32, float>) |
| 161 |
|
| 162 |
.def("nextb", &IMATH_NAMESPACE::Rand32::nextb, |
| 163 |
"r.nextb() -- return the next boolean " |
| 164 |
"value in the uniformly-distributed " |
| 165 |
"sequence") |
| 166 |
|
| 167 |
.def("nextGauss", &nextGauss<IMATH_NAMESPACE::Rand32>, |
| 168 |
"r.nextGauss() -- returns the next " |
| 169 |
"floating-point value in the normally " |
| 170 |
"(Gaussian) distributed sequence") |
| 171 |
|
| 172 |
.def("nextGaussSphere", nextGaussSphere1, |
| 173 |
"r.nextGaussSphere(v) -- returns the next " |
| 174 |
"point whose distance from the origin " |
| 175 |
"has a normal (Gaussian) distribution with " |
| 176 |
"mean 0 and variance 1. The vector " |
| 177 |
"argument, v, specifies the dimension " |
| 178 |
"and number type.") |
| 179 |
.def("nextGaussSphere", nextGaussSphere2) |
| 180 |
.def("nextGaussSphere", nextGaussSphere3) |
| 181 |
.def("nextGaussSphere", nextGaussSphere4) |
| 182 |
|
| 183 |
.def("nextHollowSphere", nextHollowSphere1, |
| 184 |
"r.nextHollowSphere(v) -- return the next " |
| 185 |
"point uniformly distributed on the surface " |
| 186 |
"of a sphere of radius 1 centered at the " |
| 187 |
"origin. The vector argument, v, specifies " |
| 188 |
"the dimension and number type.") |
| 189 |
.def("nextHollowSphere", nextHollowSphere2) |
| 190 |
.def("nextHollowSphere", nextHollowSphere3) |
| 191 |
.def("nextHollowSphere", nextHollowSphere4) |
| 192 |
|
| 193 |
.def("nextSolidSphere", nextSolidSphere1, |
| 194 |
"r.nextSolidSphere(v) -- return the next " |
| 195 |
"point uniformly distributed in a sphere " |
| 196 |
"of radius 1 centered at the origin. The " |
| 197 |
"vector argument, v, specifies the " |
| 198 |
"dimension and number type.") |
| 199 |
.def("nextSolidSphere", nextSolidSphere2) |
| 200 |
.def("nextSolidSphere", nextSolidSphere3) |
| 201 |
.def("nextSolidSphere", nextSolidSphere4) |
| 202 |
; |
| 203 |
|
| 204 |
def("hollowSphereRand",&hollowSphereRand<float,IMATH_NAMESPACE::Rand32>,"hollowSphereRand(randObj,num) return XYZ vectors uniformly " |
| 205 |
"distributed across the surface of a sphere generated from the given Rand32 object", |
| 206 |
args("randObj","num")); |
| 207 |
|
| 208 |
def("solidSphereRand",&solidSphereRand<float,IMATH_NAMESPACE::Rand32>,"solidSphereRand(randObj,num) return XYZ vectors uniformly " |
| 209 |
"distributed through the volume of a sphere generated from the given Rand32 object", |
| 210 |
args("randObj","num")); |
| 211 |
|
| 212 |
decoratecopy(rand32_class); |
| 213 |
|
| 214 |
return rand32_class; |
| 215 |
} |
| 216 |
|
| 217 |
PYIMATH_EXPORT |
| 218 |
class_<IMATH_NAMESPACE::Rand48> |
| 219 |
register_Rand48() |
| 220 |
{ |
| 221 |
double (IMATH_NAMESPACE::Rand48::*nextf1)(void) = &IMATH_NAMESPACE::Rand48::nextf; |
| 222 |
|
| 223 |
IMATH_NAMESPACE::Vec3<float> (*nextGaussSphere1)(IMATH_NAMESPACE::Rand48 &, const IMATH_NAMESPACE::Vec3<float> &v) = &nextGaussSphere<float,IMATH_NAMESPACE::Rand48>; |
| 224 |
IMATH_NAMESPACE::Vec3<double> (*nextGaussSphere2)(IMATH_NAMESPACE::Rand48 &, const IMATH_NAMESPACE::Vec3<double> &v) = &nextGaussSphere<double,IMATH_NAMESPACE::Rand48>; |
| 225 |
IMATH_NAMESPACE::Vec2<float> (*nextGaussSphere3)(IMATH_NAMESPACE::Rand48&, const IMATH_NAMESPACE::Vec2<float> &v) = &nextGaussSphere<float,IMATH_NAMESPACE::Rand48>; |
| 226 |
IMATH_NAMESPACE::Vec2<double> (*nextGaussSphere4)(IMATH_NAMESPACE::Rand48 &, const IMATH_NAMESPACE::Vec2<double> &v) = &nextGaussSphere<double,IMATH_NAMESPACE::Rand48>; |
| 227 |
|
| 228 |
IMATH_NAMESPACE::Vec3<float> (*nextHollowSphere1)(IMATH_NAMESPACE::Rand48 &, const IMATH_NAMESPACE::Vec3<float> &v) = &nextHollowSphere<float,IMATH_NAMESPACE::Rand48>; |
| 229 |
IMATH_NAMESPACE::Vec3<double> (*nextHollowSphere2)(IMATH_NAMESPACE::Rand48 &, const IMATH_NAMESPACE::Vec3<double> &v) = &nextHollowSphere<double,IMATH_NAMESPACE::Rand48>; |
| 230 |
IMATH_NAMESPACE::Vec2<float> (*nextHollowSphere3)(IMATH_NAMESPACE::Rand48 &, const IMATH_NAMESPACE::Vec2<float> &v) = &nextHollowSphere<float,IMATH_NAMESPACE::Rand48>; |
| 231 |
IMATH_NAMESPACE::Vec2<double> (*nextHollowSphere4)(IMATH_NAMESPACE::Rand48 &, const IMATH_NAMESPACE::Vec2<double> &v) = &nextHollowSphere<double,IMATH_NAMESPACE::Rand48>; |
| 232 |
|
| 233 |
IMATH_NAMESPACE::Vec3<float> (*nextSolidSphere1)(IMATH_NAMESPACE::Rand48 &, const IMATH_NAMESPACE::Vec3<float> &v) = &nextSolidSphere<float,IMATH_NAMESPACE::Rand48>; |
| 234 |
IMATH_NAMESPACE::Vec3<double> (*nextSolidSphere2)(IMATH_NAMESPACE::Rand48 &, const IMATH_NAMESPACE::Vec3<double> &v) = &nextSolidSphere<double,IMATH_NAMESPACE::Rand48>; |
| 235 |
IMATH_NAMESPACE::Vec2<float> (*nextSolidSphere3)(IMATH_NAMESPACE::Rand48&, const IMATH_NAMESPACE::Vec2<float> &v) = &nextSolidSphere<float,IMATH_NAMESPACE::Rand48>; |
| 236 |
IMATH_NAMESPACE::Vec2<double> (*nextSolidSphere4)(IMATH_NAMESPACE::Rand48 &, const IMATH_NAMESPACE::Vec2<double> &v) = &nextSolidSphere<double,IMATH_NAMESPACE::Rand48>; |
| 237 |
|
| 238 |
class_< IMATH_NAMESPACE::Rand48 > rand48_class("Rand48"); |
| 239 |
rand48_class |
| 240 |
.def(init<>("default construction")) |
| 241 |
.def("__init__", make_constructor(Rand_constructor1<IMATH_NAMESPACE::Rand48>)) |
| 242 |
.def("__init__", make_constructor(Rand_constructor2<IMATH_NAMESPACE::Rand48>)) |
| 243 |
.def("init", &IMATH_NAMESPACE::Rand48::init, |
| 244 |
"r.init(i) -- initialize with integer " |
| 245 |
"seed i") |
| 246 |
|
| 247 |
.def("nexti", &IMATH_NAMESPACE::Rand48::nexti, |
| 248 |
"r.nexti() -- return the next integer " |
| 249 |
"value in the uniformly-distributed " |
| 250 |
"sequence") |
| 251 |
|
| 252 |
.def("nextf", nextf1, |
| 253 |
"r.nextf() -- return the next double " |
| 254 |
"value in the uniformly-distributed " |
| 255 |
"sequence\n" |
| 256 |
|
| 257 |
"r.nextf(double,double) -- return the next double " |
| 258 |
"value in the uniformly-distributed " |
| 259 |
"sequence") |
| 260 |
.def("nextf", &nextf2 <IMATH_NAMESPACE::Rand48, double>) |
| 261 |
|
| 262 |
.def("nextb", &IMATH_NAMESPACE::Rand48::nextb, |
| 263 |
"r.nextb() -- return the next boolean " |
| 264 |
"value in the uniformly-distributed " |
| 265 |
"sequence") |
| 266 |
|
| 267 |
.def("nextGauss", &nextGauss<IMATH_NAMESPACE::Rand48>, |
| 268 |
"r.nextGauss() -- returns the next " |
| 269 |
"floating-point value in the normally " |
| 270 |
"(Gaussian) distributed sequence") |
| 271 |
|
| 272 |
.def("nextGaussSphere", nextGaussSphere1, |
| 273 |
"r.nextGaussSphere(v) -- returns the next " |
| 274 |
"point whose distance from the origin " |
| 275 |
"has a normal (Gaussian) distribution with " |
| 276 |
"mean 0 and variance 1. The vector " |
| 277 |
"argument, v, specifies the dimension " |
| 278 |
"and number type.") |
| 279 |
.def("nextGaussSphere", nextGaussSphere2) |
| 280 |
.def("nextGaussSphere", nextGaussSphere3) |
| 281 |
.def("nextGaussSphere", nextGaussSphere4) |
| 282 |
|
| 283 |
.def("nextHollowSphere", nextHollowSphere1, |
| 284 |
"r.nextHollowSphere(v) -- return the next " |
| 285 |
"point uniformly distributed on the surface " |
| 286 |
"of a sphere of radius 1 centered at the " |
| 287 |
"origin. The vector argument, v, specifies " |
| 288 |
"the dimension and number type.") |
| 289 |
.def("nextHollowSphere", nextHollowSphere2) |
| 290 |
.def("nextHollowSphere", nextHollowSphere3) |
| 291 |
.def("nextHollowSphere", nextHollowSphere4) |
| 292 |
|
| 293 |
.def("nextSolidSphere", nextSolidSphere1, |
| 294 |
"r.nextSolidSphere(v) -- return the next " |
| 295 |
"point uniformly distributed in a sphere " |
| 296 |
"of radius 1 centered at the origin. The " |
| 297 |
"vector argument, v, specifies the " |
| 298 |
"dimension and number type.") |
| 299 |
.def("nextSolidSphere", nextSolidSphere2) |
| 300 |
.def("nextSolidSphere", nextSolidSphere3) |
| 301 |
.def("nextSolidSphere", nextSolidSphere4) |
| 302 |
; |
| 303 |
|
| 304 |
decoratecopy(rand48_class); |
| 305 |
|
| 306 |
return rand48_class; |
| 307 |
} |
| 308 |
|
| 309 |
// |
| 310 |
|
| 311 |
PyObject * |
| 312 |
Rand32::wrap (const IMATH_NAMESPACE::Rand32 &r) |
| 313 |
{ |
| 314 |
boost::python::return_by_value::apply <IMATH_NAMESPACE::Rand32>::type converter; |
| 315 |
PyObject *p = converter (r); |
| 316 |
return p; |
| 317 |
} |
| 318 |
|
| 319 |
PyObject * |
| 320 |
Rand48::wrap (const IMATH_NAMESPACE::Rand48 &r) |
| 321 |
{ |
| 322 |
boost::python::return_by_value::apply <IMATH_NAMESPACE::Rand48>::type converter; |
| 323 |
PyObject *p = converter (r); |
| 324 |
return p; |
| 325 |
} |
| 326 |
|
| 327 |
} //namespace PyIMath |