11 #ifndef CUBBYFLOW_PYTHON_PYBIND11_UTILS_HPP 12 #define CUBBYFLOW_PYTHON_PYBIND11_UTILS_HPP 17 #include <pybind11/pybind11.h> 18 #include <pybind11/stl.h> 26 if (tuple.size() == 2)
28 for (
size_t i = 0; i < 2; ++i)
30 ret[i] = tuple[i].cast<
size_t>();
35 throw std::invalid_argument(
"Invalid size.");
47 for (
size_t i = 0; i < 2; ++i)
49 ret[i] = list[i].cast<
size_t>();
54 throw std::invalid_argument(
"Invalid size.");
64 if (tuple.size() == 3)
66 for (
size_t i = 0; i < 3; ++i)
68 ret[i] = tuple[i].cast<
size_t>();
73 throw std::invalid_argument(
"Invalid size.");
85 for (
size_t i = 0; i < 3; ++i)
87 ret[i] = list[i].cast<
size_t>();
92 throw std::invalid_argument(
"Invalid size.");
100 return pybind11::make_tuple(size.x, size.y);
105 return pybind11::make_tuple(size.x, size.y, size.z);
108 template <
typename T,
size_t N>
113 if (tuple.size() == N)
115 for (
size_t i = 0; i < N; ++i)
117 ret[i] = tuple[i].cast<T>();
122 throw std::invalid_argument(
"Invalid size.");
128 template <
typename T,
size_t N>
133 if (list.size() == N)
135 for (
size_t i = 0; i < N; ++i)
137 ret[i] = list[i].cast<T>();
142 throw std::invalid_argument(
"Invalid size.");
148 template <
typename T>
153 for (
size_t i = 0; i < 4; ++i)
155 ret[i] = tuple[i].cast<T>();
161 template <
typename T>
166 for (
size_t i = 0; i < 4; ++i)
168 ret[i] = list[i].cast<T>();
176 return TupleToVector<float, 2>(tuple);
181 return TupleToVector<float, 2>(list);
186 return TupleToVector<float, 3>(tuple);
191 return TupleToVector<float, 3>(list);
196 return TupleToVector<float, 4>(tuple);
201 return TupleToVector<float, 4>(list);
206 return TupleToQuaternion<float>(tuple);
211 return TupleToQuaternion<float>(list);
216 return TupleToVector<double, 2>(tuple);
221 return TupleToVector<double, 2>(list);
226 return TupleToVector<double, 3>(tuple);
231 return TupleToVector<double, 3>(list);
236 return TupleToVector<double, 4>(tuple);
241 return TupleToVector<double, 4>(list);
244 template <
typename T>
247 return pybind11::make_tuple(vec.x, vec.y);
250 template <
typename T>
253 return pybind11::make_tuple(vec.x, vec.y, vec.z);
256 template <
typename T>
259 return pybind11::make_tuple(vec.x, vec.y, vec.z, vec.w);
264 return TupleToQuaternion<double>(tuple);
269 return TupleToQuaternion<double>(list);
274 if (pybind11::isinstance<Vector2UZ>(static_cast<pybind11::handle>(obj)))
279 if (pybind11::isinstance<pybind11::tuple>(
280 static_cast<pybind11::handle>(obj)))
285 if (pybind11::isinstance<pybind11::list>(
286 static_cast<pybind11::handle>(obj)))
291 throw std::invalid_argument(
"Cannot convert to Vector2UZ.");
296 if (pybind11::isinstance<Vector3UZ>(static_cast<pybind11::handle>(obj)))
301 if (pybind11::isinstance<pybind11::tuple>(
302 static_cast<pybind11::handle>(obj)))
307 if (pybind11::isinstance<pybind11::list>(
308 static_cast<pybind11::handle>(obj)))
313 throw std::invalid_argument(
"Cannot convert to Vector3UZ.");
318 if (pybind11::isinstance<Vector2F>(static_cast<pybind11::handle>(obj)))
323 if (pybind11::isinstance<pybind11::tuple>(
324 static_cast<pybind11::handle>(obj)))
329 if (pybind11::isinstance<pybind11::list>(
330 static_cast<pybind11::handle>(obj)))
335 throw std::invalid_argument(
"Cannot convert to Vector2F.");
340 if (pybind11::isinstance<Vector2D>(static_cast<pybind11::handle>(obj)))
345 if (pybind11::isinstance<pybind11::tuple>(
346 static_cast<pybind11::handle>(obj)))
351 if (pybind11::isinstance<pybind11::list>(
352 static_cast<pybind11::handle>(obj)))
357 throw std::invalid_argument(
"Cannot convert to Vector2D.");
362 if (pybind11::isinstance<Vector3F>(static_cast<pybind11::handle>(obj)))
367 if (pybind11::isinstance<pybind11::tuple>(
368 static_cast<pybind11::handle>(obj)))
373 if (pybind11::isinstance<pybind11::list>(
374 static_cast<pybind11::handle>(obj)))
379 throw std::invalid_argument(
"Cannot convert to Vector3F.");
384 if (pybind11::isinstance<Vector3D>(static_cast<pybind11::handle>(obj)))
389 if (pybind11::isinstance<pybind11::tuple>(
390 static_cast<pybind11::handle>(obj)))
395 if (pybind11::isinstance<pybind11::list>(
396 static_cast<pybind11::handle>(obj)))
401 throw std::invalid_argument(
"Cannot convert to Vector3D.");
406 if (pybind11::isinstance<Vector4F>(static_cast<pybind11::handle>(obj)))
411 if (pybind11::isinstance<pybind11::tuple>(
412 static_cast<pybind11::handle>(obj)))
417 if (pybind11::isinstance<pybind11::list>(
418 static_cast<pybind11::handle>(obj)))
423 throw std::invalid_argument(
"Cannot convert to Vector4F.");
428 if (pybind11::isinstance<Vector4D>(static_cast<pybind11::handle>(obj)))
433 if (pybind11::isinstance<pybind11::tuple>(
434 static_cast<pybind11::handle>(obj)))
439 if (pybind11::isinstance<pybind11::list>(
440 static_cast<pybind11::handle>(obj)))
445 throw std::invalid_argument(
"Cannot convert to Vector4D.");
450 if (pybind11::isinstance<QuaternionF>(static_cast<pybind11::handle>(obj)))
455 if (pybind11::isinstance<pybind11::tuple>(
456 static_cast<pybind11::handle>(obj)))
461 if (pybind11::isinstance<pybind11::list>(
462 static_cast<pybind11::handle>(obj)))
467 throw std::invalid_argument(
"Cannot convert to QuaternionF.");
472 if (pybind11::isinstance<QuaternionD>(static_cast<pybind11::handle>(obj)))
477 if (pybind11::isinstance<pybind11::tuple>(
478 static_cast<pybind11::handle>(obj)))
483 if (pybind11::isinstance<pybind11::list>(
484 static_cast<pybind11::handle>(obj)))
489 throw std::invalid_argument(
"Cannot convert to QuaternionD.");
497 if (args.size() <= 3)
514 throw std::invalid_argument(
"Too many arguments.");
518 if (kwargs.contains(
"resolution"))
522 if (kwargs.contains(
"gridSpacing"))
526 if (kwargs.contains(
"gridOrigin"))
530 if (kwargs.contains(
"domainSizeX"))
532 double domainSizeX = kwargs[
"domainSizeX"].cast<
double>();
533 gridSpacing.
Fill(domainSizeX / static_cast<double>(resolution.x));
542 if (args.size() <= 3)
559 throw std::invalid_argument(
"Too many arguments.");
563 if (kwargs.contains(
"resolution"))
567 if (kwargs.contains(
"gridSpacing"))
571 if (kwargs.contains(
"gridOrigin"))
575 if (kwargs.contains(
"domainSizeX"))
577 double domainSizeX = kwargs[
"domainSizeX"].cast<
double>();
578 gridSpacing.
Fill(domainSizeX / static_cast<double>(resolution.x));
583 #define CUBBYFLOW_PYTHON_MAKE_INDEX_FUNCTION2(Class, Func) \ 584 [](const Class& instance, pybind11::args args) { \ 585 if (args.size() == 1) \ 587 return instance.Func(ObjectToVector2UZ(args[0])); \ 591 throw std::invalid_argument("Invalid number of arguments."); \ 595 #define CUBBYFLOW_PYTHON_MAKE_INDEX_FUNCTION3(Class, Func) \ 596 [](const Class& instance, pybind11::args args) { \ 597 if (args.size() == 1) \ 599 return instance.Func(ObjectToVector3UZ(args[0])); \ 603 throw std::invalid_argument("Invalid number of arguments."); \ Quaternion< T > TupleToQuaternion(pybind11::tuple tuple)
Definition: pybind11Utils.hpp:149
Vector2UZ ObjectToVector2UZ(const pybind11::object &obj)
Definition: pybind11Utils.hpp:272
pybind11::tuple Vector2UZToTuple(const Vector2UZ &size)
Definition: pybind11Utils.hpp:98
Vector2UZ TupleToVector2UZ(pybind11::tuple tuple)
Definition: pybind11Utils.hpp:22
QuaternionD ObjectToQuaternionD(const pybind11::object &obj)
Definition: pybind11Utils.hpp:470
pybind11::tuple Vector3UZToTuple(const Vector3UZ &size)
Definition: pybind11Utils.hpp:103
Vector4D ObjectToVector4D(const pybind11::object &obj)
Definition: pybind11Utils.hpp:426
pybind11::tuple Vector4ToTuple(const Vector< T, 4 > &vec)
Definition: pybind11Utils.hpp:257
void ParseGridResizeParams(pybind11::args args, pybind11::kwargs kwargs, Vector2UZ &resolution, Vector2D &gridSpacing, Vector2D &gridOrigin)
Definition: pybind11Utils.hpp:492
QuaternionD TupleToQuaternionD(pybind11::tuple tuple)
Definition: pybind11Utils.hpp:262
Vector4F ObjectToVector4F(const pybind11::object &obj)
Definition: pybind11Utils.hpp:404
Vector3UZ TupleToVector3UZ(pybind11::tuple tuple)
Definition: pybind11Utils.hpp:60
pybind11::tuple Vector3ToTuple(const Vector< T, 3 > &vec)
Definition: pybind11Utils.hpp:251
void Fill(const T &val)
Definition: Matrix-Impl.hpp:226
Vector2D ObjectToVector2D(const pybind11::object &obj)
Definition: pybind11Utils.hpp:338
Vector3F ObjectToVector3F(const pybind11::object &obj)
Definition: pybind11Utils.hpp:360
Definition: Matrix.hpp:27
Definition: pybind11Utils.hpp:20
Vector3D TupleToVector3D(pybind11::tuple tuple)
Definition: pybind11Utils.hpp:224
Vector3UZ ObjectToVector3UZ(const pybind11::object &obj)
Definition: pybind11Utils.hpp:294
QuaternionF TupleToQuaternionF(pybind11::tuple tuple)
Definition: pybind11Utils.hpp:204
Vector3D ObjectToVector3D(const pybind11::object &obj)
Definition: pybind11Utils.hpp:382
Vector2F TupleToVector2F(pybind11::tuple tuple)
Definition: pybind11Utils.hpp:174
Vector2D TupleToVector2D(pybind11::tuple tuple)
Definition: pybind11Utils.hpp:214
Vector4F TupleToVector4F(pybind11::tuple tuple)
Definition: pybind11Utils.hpp:194
Vector3F TupleToVector3F(pybind11::tuple tuple)
Definition: pybind11Utils.hpp:184
Vector2F ObjectToVector2F(const pybind11::object &obj)
Definition: pybind11Utils.hpp:316
Vector4D TupleToVector4D(pybind11::tuple tuple)
Definition: pybind11Utils.hpp:234
QuaternionF ObjectToQuaternionF(const pybind11::object &obj)
Definition: pybind11Utils.hpp:448
Quaternion class defined as q = w + xi + yj + zk.
Definition: Quaternion.hpp:22
pybind11::tuple Vector2ToTuple(const Vector< T, 2 > &vec)
Definition: pybind11Utils.hpp:245
Vector< T, N > TupleToVector(pybind11::tuple tuple)
Definition: pybind11Utils.hpp:109