some more python Vec and Pnt arithmetic

This commit is contained in:
Christopher Lackner 2019-12-10 14:04:53 +01:00
parent a103896079
commit 79b1d22185
2 changed files with 39 additions and 12 deletions

View File

@ -179,6 +179,15 @@ namespace netgen
return result; return result;
} }
template<int D>
inline Vec<D> operator*(const Vec<D>& v, double d)
{
Vec<D> result;
for(auto i : Range(D))
result[i] = d*v[i];
return result;
}
inline double Cross2(const Vec<2>& v1, const Vec<2>& v2) inline double Cross2(const Vec<2>& v1, const Vec<2>& v2)
{ {
return v1[0] * v2[1] - v1[1] * v2[0]; return v1[0] * v2[1] - v1[1] * v2[0];

View File

@ -122,18 +122,21 @@ DLL_HEADER void ExportNetgenMeshing(py::module &m)
.def("__getitem__", [](Point<2>& self, int index) { return self[index]; }) .def("__getitem__", [](Point<2>& self, int index) { return self[index]; })
; ;
m.def ("Pnt", FunctionPointer m.def("Pnt", [](double x, double y, double z)
([](double x, double y, double z) { return global_trafo(Point<3>(x,y,z)); })); { return global_trafo(Point<3>(x,y,z)); });
m.def ("Pnt", FunctionPointer m.def("Pnt", [](double x, double y) { return Point<2>(x,y); });
([](double x, double y) { return Point<2>(x,y); })); m.def("Pnt", [](py::array_t<double> np_array)
{
/* int dim = np_array.size();
// duplicated functions ???? if(!(dim == 2 || dim == 3))
m.def ("Pnt", FunctionPointer throw Exception("Invalid dimension of input array!");
([](double x, double y, double z) { return Point<3>(x,y,z); })); if(dim == 2)
m.def ("Pnt", FunctionPointer return py::cast(Point<2>(np_array.at(0),
([](double x, double y) { return Point<2>(x,y); })); np_array.at(1)));
*/ return py::cast(global_trafo(Point<3>(np_array.at(0),
np_array.at(1),
np_array.at(2))));
});
py::class_<Vec<2>> (m, "Vec2d") py::class_<Vec<2>> (m, "Vec2d")
.def(py::init<double,double>()) .def(py::init<double,double>())
@ -143,6 +146,7 @@ DLL_HEADER void ExportNetgenMeshing(py::module &m)
.def(py::self-py::self) .def(py::self-py::self)
.def(-py::self) .def(-py::self)
.def(double()*py::self) .def(double()*py::self)
.def(py::self*double())
.def("Norm", &Vec<2>::Length) .def("Norm", &Vec<2>::Length)
.def("__getitem__", [](Vec<2>& vec, int index) { return vec[index]; }) .def("__getitem__", [](Vec<2>& vec, int index) { return vec[index]; })
.def("__len__", [](Vec<2>& /*unused*/) { return 2; }) .def("__len__", [](Vec<2>& /*unused*/) { return 2; })
@ -156,6 +160,7 @@ DLL_HEADER void ExportNetgenMeshing(py::module &m)
.def(py::self-py::self) .def(py::self-py::self)
.def(-py::self) .def(-py::self)
.def(double()*py::self) .def(double()*py::self)
.def(py::self*double())
.def("Norm", &Vec<3>::Length) .def("Norm", &Vec<3>::Length)
.def("__getitem__", [](Vec<3>& vec, int index) { return vec[index]; }) .def("__getitem__", [](Vec<3>& vec, int index) { return vec[index]; })
.def("__len__", [](Vec<3>& /*unused*/) { return 3; }) .def("__len__", [](Vec<3>& /*unused*/) { return 3; })
@ -163,6 +168,19 @@ DLL_HEADER void ExportNetgenMeshing(py::module &m)
m.def ("Vec", FunctionPointer m.def ("Vec", FunctionPointer
([] (double x, double y, double z) { return global_trafo(Vec<3>(x,y,z)); })); ([] (double x, double y, double z) { return global_trafo(Vec<3>(x,y,z)); }));
m.def("Vec", [](py::array_t<double> np_array)
{
int dim = np_array.size();
if(!(dim == 2 || dim == 3))
throw Exception("Invalid dimension of input array!");
if(dim == 2)
return py::cast(Vec<2>(np_array.at(0),
np_array.at(1)));
return py::cast(global_trafo(Vec<3>(np_array.at(0),
np_array.at(1),
np_array.at(2))));
});
m.def ("Vec", FunctionPointer m.def ("Vec", FunctionPointer
([] (double x, double y) { return Vec<2>(x,y); })); ([] (double x, double y) { return Vec<2>(x,y); }));