Commit e6b692c5 authored by Dennis Gläser's avatar Dennis Gläser
Browse files

[python][point] add ctor from list

parent fbc428e6
......@@ -19,7 +19,9 @@
#ifndef FRACKIT_PYTHON_GEOMETRY_POINT_HH
#define FRACKIT_PYTHON_GEOMETRY_POINT_HH
#include <array>
#include <string>
#include <algorithm>
#include <pybind11/pybind11.h>
#include <pybind11/operators.h>
......@@ -36,13 +38,15 @@ namespace Detail {
template<class Base, class Impl>
void registerPointBase(py::module& module)
{
static constexpr int worldDim = Impl::worldDimension();
static_assert(Base::worldDimension() == worldDim, "dimension mismatch");
using ctype = typename Base::ctype;
using Vector = Frackit::Vector<ctype, Impl::worldDimension()>;
using Vector = Frackit::Vector<ctype, worldDim>;
// register class and define constructor
std::string className("PointBase_" + std::to_string(Base::worldDimension()));
// register class
std::string className("PointBase_" + std::to_string(worldDim));
py::class_<Base, Geometry> cls(module, className.c_str());
cls.def(py::init<>());
// member function
cls.def("name", &Base::name, "name of the geometry class");
......@@ -75,6 +79,24 @@ namespace Detail {
std::string className("Point_" + std::to_string(worldDim));
py::class_<Point, Base> cls(module, className.c_str());
cls.def(py::init<>());
cls.def(py::init( [] (py::list x) -> Point
{
// create array of matching size and fill with values
// from the list. Use zeros at the end if list is shorter.
std::array<ctype, worldDim> vals;
std::fill(vals.begin(), vals.end(), 0.0);
unsigned int minDim = std::min(worldDim, static_cast<int>(x.size()));
for (unsigned int i = 0; i < minDim; ++i)
vals[i] = x[i].template cast<ctype>();
if constexpr (worldDim == 1)
return {vals[0]};
else if constexpr (worldDim == 2)
return {vals[0], vals[1]};
else
return {vals[0], vals[1], vals[2]};
} ), "coordinates"_a);
if constexpr (worldDim == 1)
cls.def(py::init<ctype>(), "x"_a);
else if constexpr (worldDim == 2)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment