| 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 "PyImath.h" |
| 13 |
#include "PyImathMathExc.h" |
| 14 |
#include "PyImathVec.h" |
| 15 |
#include "PyImathFrustum.h" |
| 16 |
#include "PyImathDecorators.h" |
| 17 |
#include "PyImathExport.h" |
| 18 |
|
| 19 |
namespace PyImath{ |
| 20 |
using namespace boost::python; |
| 21 |
using namespace IMATH_NAMESPACE; |
| 22 |
|
| 23 |
template <class T> struct FrustumName {static const char *value;}; |
| 24 |
template <> const char *FrustumName<float>::value = "Frustumf"; |
| 25 |
template <> const char *FrustumName<double>::value = "Frustumd"; |
| 26 |
|
| 27 |
template <class T> struct FrustumTestName {static const char *value;}; |
| 28 |
template <> const char *FrustumTestName<float>::value = "FrustumTestf"; |
| 29 |
template <> const char *FrustumTestName<double>::value = "FrustumTestd"; |
| 30 |
|
| 31 |
|
| 32 |
template <class T> |
| 33 |
static std::string Frustum_repr(const Frustum<T> &f) |
| 34 |
{ |
| 35 |
std::stringstream stream; |
| 36 |
stream << FrustumName<T>::value << "(" << f.nearPlane() << ", " << f.farPlane() << ", " |
| 37 |
<< f.left() << ", " << f.right() << ", " << f.top() << ", " |
| 38 |
<< f.bottom() << ", " << f.orthographic() << ")"; |
| 39 |
return stream.str(); |
| 40 |
} |
| 41 |
|
| 42 |
template <class T> |
| 43 |
static void |
| 44 |
modifyNearAndFar(Frustum<T> &f, T nearPlane, T farPlane) |
| 45 |
{ |
| 46 |
MATH_EXC_ON; |
| 47 |
f.modifyNearAndFar (nearPlane, farPlane); |
| 48 |
} |
| 49 |
|
| 50 |
template <class T> |
| 51 |
static T |
| 52 |
fovx(Frustum<T> &f) |
| 53 |
{ |
| 54 |
MATH_EXC_ON; |
| 55 |
return f.fovx(); |
| 56 |
} |
| 57 |
|
| 58 |
template <class T> |
| 59 |
static T |
| 60 |
fovy(Frustum<T> &f) |
| 61 |
{ |
| 62 |
MATH_EXC_ON; |
| 63 |
return f.fovy(); |
| 64 |
} |
| 65 |
|
| 66 |
template <class T> |
| 67 |
static T |
| 68 |
aspect(Frustum<T> &f) |
| 69 |
{ |
| 70 |
MATH_EXC_ON; |
| 71 |
return f.aspect(); |
| 72 |
} |
| 73 |
|
| 74 |
template <class T> |
| 75 |
static Matrix44<T> |
| 76 |
projectionMatrix(Frustum<T> &f) |
| 77 |
{ |
| 78 |
MATH_EXC_ON; |
| 79 |
return f.projectionMatrix(); |
| 80 |
} |
| 81 |
|
| 82 |
template <class T> |
| 83 |
static Frustum<T> |
| 84 |
window (Frustum<T> &f, T l, T r, T b, T t) |
| 85 |
{ |
| 86 |
MATH_EXC_ON; |
| 87 |
return f.window(l, r, b, t); |
| 88 |
} |
| 89 |
|
| 90 |
template <class T> |
| 91 |
static Line3<T> |
| 92 |
projectScreenToRay (Frustum<T> &f, const Vec2<T> &p) |
| 93 |
{ |
| 94 |
MATH_EXC_ON; |
| 95 |
return f.projectScreenToRay(p); |
| 96 |
} |
| 97 |
|
| 98 |
template <class T> |
| 99 |
static Line3<T> |
| 100 |
projectScreenToRayTuple(Frustum<T> &f, const tuple &t) |
| 101 |
{ |
| 102 |
MATH_EXC_ON; |
| 103 |
if(t.attr("__len__")() == 2) |
| 104 |
{ |
| 105 |
Vec2<T> point; |
| 106 |
point.x = extract<T>(t[0]); |
| 107 |
point.y = extract<T>(t[1]); |
| 108 |
return f.projectScreenToRay(point); |
| 109 |
} |
| 110 |
else |
| 111 |
throw std::invalid_argument ( "projectScreenToRay expects tuple of length 2"); |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
template <class T> |
| 116 |
static Vec2<T> |
| 117 |
projectPointToScreen (Frustum<T> &f, const Vec3<T> &p) |
| 118 |
{ |
| 119 |
MATH_EXC_ON; |
| 120 |
return f.projectPointToScreen(p); |
| 121 |
} |
| 122 |
|
| 123 |
template <class T> |
| 124 |
static Vec2<T> |
| 125 |
projectPointToScreenTuple(Frustum<T> &f, const tuple &t) |
| 126 |
{ |
| 127 |
MATH_EXC_ON; |
| 128 |
if(t.attr("__len__")() == 3) |
| 129 |
{ |
| 130 |
Vec3<T> point; |
| 131 |
point.x = extract<T>(t[0]); |
| 132 |
point.y = extract<T>(t[1]); |
| 133 |
point.z = extract<T>(t[2]); |
| 134 |
return f.projectPointToScreen(point); |
| 135 |
} |
| 136 |
else |
| 137 |
throw std::invalid_argument ( "projectPointToScreen expects tuple of length 3"); |
| 138 |
|
| 139 |
} |
| 140 |
|
| 141 |
template <class T> |
| 142 |
static Vec2<T> |
| 143 |
projectPointToScreenObj(Frustum<T> &f, const object &o) |
| 144 |
{ |
| 145 |
MATH_EXC_ON; |
| 146 |
Vec3<T> v; |
| 147 |
if (PyImath::V3<T>::convert (o.ptr(), &v)) |
| 148 |
return f.projectPointToScreen(v); |
| 149 |
else |
| 150 |
throw std::invalid_argument ( "projectPointToScreen expects tuple of length 3"); |
| 151 |
} |
| 152 |
|
| 153 |
template <class T> |
| 154 |
static T |
| 155 |
ZToDepth(Frustum<T> &f, long z, long min, long max) |
| 156 |
{ |
| 157 |
MATH_EXC_ON; |
| 158 |
return f.ZToDepth(z, min, max); |
| 159 |
} |
| 160 |
|
| 161 |
template <class T> |
| 162 |
static T |
| 163 |
normalizedZToDepth(Frustum<T> &f, T z) |
| 164 |
{ |
| 165 |
MATH_EXC_ON; |
| 166 |
return f.normalizedZToDepth(z); |
| 167 |
} |
| 168 |
|
| 169 |
template <class T> |
| 170 |
static long |
| 171 |
DepthToZ(Frustum<T> &f, T depth, long min, long max) |
| 172 |
{ |
| 173 |
MATH_EXC_ON; |
| 174 |
return f.DepthToZ(depth, min, max); |
| 175 |
} |
| 176 |
|
| 177 |
template <class T> |
| 178 |
static T |
| 179 |
worldRadius(Frustum<T> &f, const Vec3<T> &p, T radius) |
| 180 |
{ |
| 181 |
MATH_EXC_ON; |
| 182 |
return f.worldRadius(p, radius); |
| 183 |
} |
| 184 |
|
| 185 |
template <class T> |
| 186 |
static T |
| 187 |
worldRadiusTuple(Frustum<T> &f, const tuple &t, T radius) |
| 188 |
{ |
| 189 |
MATH_EXC_ON; |
| 190 |
if(t.attr("__len__")() == 3) |
| 191 |
{ |
| 192 |
Vec3<T> point; |
| 193 |
point.x = extract<T>(t[0]); |
| 194 |
point.y = extract<T>(t[1]); |
| 195 |
point.z = extract<T>(t[2]); |
| 196 |
return f.worldRadius(point, radius); |
| 197 |
} |
| 198 |
else |
| 199 |
throw std::invalid_argument ( "worldRadius expects tuple of length 3"); |
| 200 |
} |
| 201 |
|
| 202 |
template <class T> |
| 203 |
static T |
| 204 |
screenRadius(Frustum<T> &f, const Vec3<T> &p, T radius) |
| 205 |
{ |
| 206 |
MATH_EXC_ON; |
| 207 |
return f.screenRadius(p, radius); |
| 208 |
} |
| 209 |
|
| 210 |
template <class T> |
| 211 |
static T |
| 212 |
screenRadiusTuple(Frustum<T> &f, const tuple &t, T radius) |
| 213 |
{ |
| 214 |
MATH_EXC_ON; |
| 215 |
if(t.attr("__len__")() == 3) |
| 216 |
{ |
| 217 |
Vec3<T> point; |
| 218 |
point.x = extract<T>(t[0]); |
| 219 |
point.y = extract<T>(t[1]); |
| 220 |
point.z = extract<T>(t[2]); |
| 221 |
return f.screenRadius(point, radius); |
| 222 |
} |
| 223 |
else |
| 224 |
throw std::invalid_argument ("screenRadius expects tuple of length 3"); |
| 225 |
} |
| 226 |
|
| 227 |
template <class T> |
| 228 |
static void |
| 229 |
planes1(Frustum<T> &f, Plane3<T> *p) |
| 230 |
{ |
| 231 |
MATH_EXC_ON; |
| 232 |
f.planes(p); |
| 233 |
} |
| 234 |
|
| 235 |
template <class T> |
| 236 |
static void |
| 237 |
planes2(Frustum<T> &f, Plane3<T> *p, const Matrix44<T> &m) |
| 238 |
{ |
| 239 |
MATH_EXC_ON; |
| 240 |
f.planes(p, m); |
| 241 |
} |
| 242 |
|
| 243 |
template <class T> |
| 244 |
static tuple |
| 245 |
planes3(Frustum<T> &f, const Matrix44<T> &mat) |
| 246 |
{ |
| 247 |
MATH_EXC_ON; |
| 248 |
IMATH_NAMESPACE::Plane3<T> p[6]; |
| 249 |
f.planes(p,mat); |
| 250 |
|
| 251 |
tuple t = make_tuple(p[0],p[1],p[2],p[3],p[4],p[5]); |
| 252 |
|
| 253 |
return t; |
| 254 |
} |
| 255 |
|
| 256 |
template <class T> |
| 257 |
static tuple |
| 258 |
planes4(Frustum<T> &f) |
| 259 |
{ |
| 260 |
MATH_EXC_ON; |
| 261 |
IMATH_NAMESPACE::Plane3<T> p[6]; |
| 262 |
f.planes(p); |
| 263 |
|
| 264 |
tuple t = make_tuple(p[0],p[1],p[2],p[3],p[4],p[5]); |
| 265 |
|
| 266 |
return t; |
| 267 |
} |
| 268 |
|
| 269 |
template <class T> |
| 270 |
class_<Frustum<T> > |
| 271 |
register_Frustum() |
| 272 |
{ |
| 273 |
void (IMATH_NAMESPACE::Frustum<T>::*set1)(T,T,T,T,T,T,bool) = &IMATH_NAMESPACE::Frustum<T>::set; |
| 274 |
void (IMATH_NAMESPACE::Frustum<T>::*set2)(T,T,T,T,T) = &IMATH_NAMESPACE::Frustum<T>::set; |
| 275 |
const char *name = FrustumName<T>::value; |
| 276 |
|
| 277 |
class_< Frustum<T> > frustum_class(name,name,init<Frustum<T> >("copy construction")); |
| 278 |
frustum_class |
| 279 |
.def(init<>("Frustum() default construction")) |
| 280 |
.def(init<T,T,T,T,T,T,bool>("Frustum(nearPlane,farPlane,left,right,top,bottom,ortho) construction")) |
| 281 |
.def(init<T,T,T,T,T>("Frustum(nearPlane,farPlane,fovx,fovy,aspect) construction")) |
| 282 |
.def(self == self) // NOSONAR - suppress SonarCloud bug report. |
| 283 |
.def(self != self) // NOSONAR - suppress SonarCloud bug report. |
| 284 |
.def("__repr__",&Frustum_repr<T>) |
| 285 |
.def("set", set1, |
| 286 |
"F.set(nearPlane, farPlane, left, right, top, bottom, " |
| 287 |
"[ortho])\n" |
| 288 |
"F.set(nearPlane, farPlane, fovx, fovy, aspect) " |
| 289 |
" -- sets the entire state of " |
| 290 |
"frustum F as specified. Only one of " |
| 291 |
"fovx or fovy may be non-zero.") |
| 292 |
.def("set", set2) |
| 293 |
|
| 294 |
.def("modifyNearAndFar", &modifyNearAndFar<T>, |
| 295 |
"F.modifyNearAndFar(nearPlane, farPlane) -- modifies " |
| 296 |
"the already-valid frustum F as specified") |
| 297 |
|
| 298 |
.def("setOrthographic", &Frustum<T>::setOrthographic, |
| 299 |
"F.setOrthographic(b) -- modifies the " |
| 300 |
"already-valid frustum F to be orthographic " |
| 301 |
"or not") |
| 302 |
|
| 303 |
.def("nearPlane", &Frustum<T>::nearPlane, |
| 304 |
"F.nearPlane() -- returns the coordinate of the " |
| 305 |
"near clipping plane of frustum F") |
| 306 |
|
| 307 |
.def("farPlane", &Frustum<T>::farPlane, |
| 308 |
"F.farPlane() -- returns the coordinate of the " |
| 309 |
"far clipping plane of frustum F") |
| 310 |
|
| 311 |
// The following two functions provide backwards compatibility |
| 312 |
// with the previous API for this class. |
| 313 |
|
| 314 |
.def("near", &Frustum<T>::nearPlane, |
| 315 |
"F.near() -- returns the coordinate of the " |
| 316 |
"near clipping plane of frustum F") |
| 317 |
|
| 318 |
.def("far", &Frustum<T>::farPlane, |
| 319 |
"F.far() -- returns the coordinate of the " |
| 320 |
"far clipping plane of frustum F") |
| 321 |
|
| 322 |
.def("left", &Frustum<T>::left, |
| 323 |
"F.left() -- returns the left coordinate of " |
| 324 |
"the near clipping window of frustum F") |
| 325 |
|
| 326 |
.def("right", &Frustum<T>::right, |
| 327 |
"F.right() -- returns the right coordinate of " |
| 328 |
"the near clipping window of frustum F") |
| 329 |
|
| 330 |
.def("top", &Frustum<T>::top, |
| 331 |
"F.top() -- returns the top coordinate of " |
| 332 |
"the near clipping window of frustum F") |
| 333 |
|
| 334 |
.def("bottom", &Frustum<T>::bottom, |
| 335 |
"F.bottom() -- returns the bottom coordinate " |
| 336 |
"of the near clipping window of frustum F") |
| 337 |
|
| 338 |
.def("orthographic", &Frustum<T>::orthographic, |
| 339 |
"F.orthographic() -- returns whether frustum " |
| 340 |
"F is orthographic or not") |
| 341 |
|
| 342 |
.def("planes", planes1<T>, |
| 343 |
"F.planes([M]) -- returns a sequence of 6 " |
| 344 |
"Plane3s, the sides of the frustum F " |
| 345 |
"(top, right, bottom, left, nearPlane, farPlane), " |
| 346 |
"optionally transformed by the matrix M " |
| 347 |
"if specified") |
| 348 |
.def("planes", planes2<T>) |
| 349 |
.def("planes", planes3<T>) |
| 350 |
.def("planes", planes4<T>) |
| 351 |
|
| 352 |
.def("fovx", &fovx<T>, |
| 353 |
"F.fovx() -- derives and returns the " |
| 354 |
"x field of view (in radians) for frustum F") |
| 355 |
|
| 356 |
.def("fovy", &fovy<T>, |
| 357 |
"F.fovy() -- derives and returns the " |
| 358 |
"y field of view (in radians) for frustum F") |
| 359 |
|
| 360 |
.def("aspect", &aspect<T>, |
| 361 |
"F.aspect() -- derives and returns the " |
| 362 |
"aspect ratio for frustum F") |
| 363 |
|
| 364 |
.def("projectionMatrix", &projectionMatrix<T>, |
| 365 |
"F.projectionMatrix() -- derives and returns " |
| 366 |
"the projection matrix for frustum F") |
| 367 |
|
| 368 |
.def("window", &window<T>, |
| 369 |
"F.window(l,r,b,t) -- takes a rectangle in " |
| 370 |
"the screen space (i.e., -1 <= l <= r <= 1, " |
| 371 |
"-1 <= b <= t <= 1) of F and returns a new " |
| 372 |
"Frustum whose near clipping-plane window " |
| 373 |
"is that rectangle in local space") |
| 374 |
|
| 375 |
.def("projectScreenToRay", &projectScreenToRay<T>, |
| 376 |
"F.projectScreenToRay(V) -- returns a Line3 " |
| 377 |
"through V, a V2 point in screen space") |
| 378 |
|
| 379 |
.def("projectScreenToRay", &projectScreenToRayTuple<T>) |
| 380 |
|
| 381 |
.def("projectPointToScreen", &projectPointToScreen<T>, |
| 382 |
"F.projectPointToScreen(V) -- returns the " |
| 383 |
"projection of V3 V into screen space") |
| 384 |
|
| 385 |
.def("projectPointToScreen", &projectPointToScreenTuple<T>) |
| 386 |
|
| 387 |
.def("projectPointToScreen", &projectPointToScreenObj<T>) |
| 388 |
|
| 389 |
.def("ZToDepth", &ZToDepth<T>, |
| 390 |
"F.ZToDepth(z, zMin, zMax) -- returns the " |
| 391 |
"depth (Z in the local space of the " |
| 392 |
"frustum F) corresponding to z (a result of " |
| 393 |
"transformation by F's projection matrix) " |
| 394 |
"after normalizing z to be between zMin " |
| 395 |
"and zMax") |
| 396 |
|
| 397 |
.def("normalizedZToDepth", &normalizedZToDepth<T>, |
| 398 |
"F.normalizedZToDepth(z) -- returns the " |
| 399 |
"depth (Z in the local space of the " |
| 400 |
"frustum F) corresponding to z (a result of " |
| 401 |
"transformation by F's projection matrix), " |
| 402 |
"which is assumed to have been normalized " |
| 403 |
"to [-1, 1]") |
| 404 |
|
| 405 |
.def("DepthToZ", &DepthToZ<T>, |
| 406 |
"F.DepthToZ(depth, zMin, zMax) -- converts " |
| 407 |
"depth (Z in the local space of the frustum " |
| 408 |
"F) to z (a result of transformation by F's " |
| 409 |
"projection matrix) which is normalized to " |
| 410 |
"[zMin, zMax]") |
| 411 |
|
| 412 |
.def("worldRadius", &worldRadius<T>, |
| 413 |
"F.worldRadius(V, r) -- returns the radius " |
| 414 |
"in F's local space corresponding to the " |
| 415 |
"point V and radius r in screen space") |
| 416 |
|
| 417 |
.def("worldRadius", &worldRadiusTuple<T>) |
| 418 |
|
| 419 |
.def("screenRadius", &screenRadius<T>, |
| 420 |
"F.screenRadius(V, r) -- returns the radius " |
| 421 |
"in screen space corresponding to " |
| 422 |
"the point V and radius r in F's local " |
| 423 |
"space") |
| 424 |
|
| 425 |
.def("screenRadius", &screenRadiusTuple<T>) |
| 426 |
|
| 427 |
; |
| 428 |
|
| 429 |
decoratecopy(frustum_class); |
| 430 |
|
| 431 |
return frustum_class; |
| 432 |
} |
| 433 |
|
| 434 |
template <class T,class T2> |
| 435 |
struct IsVisibleTask : public Task |
| 436 |
{ |
| 437 |
const IMATH_NAMESPACE::FrustumTest<T>& frustumTest; |
| 438 |
const PyImath::FixedArray<T2>& points; |
| 439 |
PyImath::FixedArray<int>& results; |
| 440 |
|
| 441 |
IsVisibleTask(const IMATH_NAMESPACE::FrustumTest<T>& ft, const PyImath::FixedArray<T2> &p, PyImath::FixedArray<int> &r) |
| 442 |
: frustumTest(ft), points(p), results(r) {} |
| 443 |
|
| 444 |
void execute(size_t start, size_t end) |
| 445 |
{ |
| 446 |
for(size_t p = start; p < end; ++p) |
| 447 |
results[p] = frustumTest.isVisible(IMATH_NAMESPACE::Vec3<T>(points[p])); |
| 448 |
} |
| 449 |
}; |
| 450 |
|
| 451 |
template <class T,class T2> |
| 452 |
PyImath::FixedArray<int> |
| 453 |
frustumTest_isVisible(IMATH_NAMESPACE::FrustumTest<T>& ft, const PyImath::FixedArray<T2>& points) |
| 454 |
{ |
| 455 |
size_t numPoints = points.len(); |
| 456 |
PyImath::FixedArray<int> mask(numPoints); |
| 457 |
|
| 458 |
IsVisibleTask<T,T2> task(ft,points,mask); |
| 459 |
dispatchTask(task,numPoints); |
| 460 |
return mask; |
| 461 |
} |
| 462 |
|
| 463 |
template <class T> |
| 464 |
class_<FrustumTest<T> > |
| 465 |
register_FrustumTest() |
| 466 |
{ |
| 467 |
const char *name = FrustumTestName<T>::value; |
| 468 |
|
| 469 |
bool (FrustumTest<T>::*isVisibleS)(const Sphere3<T> &) const = &FrustumTest<T>::isVisible; |
| 470 |
bool (FrustumTest<T>::*isVisibleB)(const Box<Vec3<T> > &) const = &FrustumTest<T>::isVisible; |
| 471 |
bool (FrustumTest<T>::*isVisibleV)(const Vec3<T> &) const = &FrustumTest<T>::isVisible; |
| 472 |
bool (FrustumTest<T>::*completelyContainsS)(const Sphere3<T> &) const = &FrustumTest<T>::completelyContains; |
| 473 |
bool (FrustumTest<T>::*completelyContainsB)(const Box<Vec3<T> > &) const = &FrustumTest<T>::completelyContains; |
| 474 |
|
| 475 |
class_< FrustumTest<T> > frustumtest_class(name,name,init<const IMATH_NAMESPACE::Frustum<T>&,const IMATH_NAMESPACE::Matrix44<T>&>("create a frustum test object from a frustum and transform")); |
| 476 |
frustumtest_class |
| 477 |
.def("isVisible",isVisibleS) |
| 478 |
.def("isVisible",isVisibleB) |
| 479 |
.def("isVisible",isVisibleV) |
| 480 |
.def("isVisible",&frustumTest_isVisible<T,IMATH_NAMESPACE::V3f>) |
| 481 |
.def("completelyContains",completelyContainsS) |
| 482 |
.def("completelyContains",completelyContainsB) |
| 483 |
; |
| 484 |
|
| 485 |
decoratecopy(frustumtest_class); |
| 486 |
|
| 487 |
return frustumtest_class; |
| 488 |
} |
| 489 |
|
| 490 |
template PYIMATH_EXPORT class_<Frustum<float> > register_Frustum<float>(); |
| 491 |
template PYIMATH_EXPORT class_<Frustum<double> > register_Frustum<double>(); |
| 492 |
template PYIMATH_EXPORT class_<FrustumTest<float> > register_FrustumTest<float>(); |
| 493 |
template PYIMATH_EXPORT class_<FrustumTest<double> > register_FrustumTest<double>(); |
| 494 |
} |