Beatmup
binding_tools.hpp
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2020, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 
21 #include "geometry.h"
23 #include "nnets/model.h"
24 
25 #include <pybind11/pybind11.h>
26 #include <pybind11/numpy.h>
27 
28 namespace py = pybind11;
29 
30 namespace Beatmup {
31  namespace Python {
32 
33  template<typename T>
34  inline CustomPoint<T> toPoint(const py::tuple& tuple) {
35  if (tuple.size() != 2)
36  throw std::invalid_argument("Expected a tuple of 2 elements");
37  return CustomPoint<T>(tuple[0].cast<T>(), tuple[1].cast<T>());
38  }
39 
40  template<typename T>
41  inline CustomRectangle<T> toRectangle(const py::tuple& tuple) {
42  if (tuple.size() != 4)
43  throw std::invalid_argument("Expected a tuple of 4 elements");
44  return CustomRectangle<T>(tuple[0].cast<T>(), tuple[1].cast<T>(), tuple[2].cast<T>(), tuple[3].cast<T>());
45  }
46 
47 
48  template<typename T>
49  inline py::tuple toTuple(const CustomPoint<T>& point) {
50  return py::make_tuple(point.x, point.y);
51  }
52 
53  template<typename T>
54  inline py::tuple toTuple(const CustomRectangle<T>& rectangle) {
55  return py::make_tuple(rectangle.a.x, rectangle.a.y, rectangle.b.x, rectangle.b.y);
56  }
57 
58  template<typename T>
59  inline py::tuple toTuple(const CustomMatrix2<T>& matrix) {
60  return py::make_tuple(
61  py::make_tuple(matrix.getA11(), matrix.getA12()),
62  py::make_tuple(matrix.getA21(), matrix.getA22())
63  );
64  }
65 
66  inline py::tuple toTuple(const color3f& color) {
67  return py::make_tuple(color.r, color.g, color.b);
68  }
69 
70  inline py::tuple toTuple(const color4i& color) {
71  return py::make_tuple(color.r, color.g, color.b, color.a);
72  }
73 
74  inline pixfloat4 toPixfloat4(const py::tuple& tuple) {
75  if (tuple.size() != 4)
76  throw std::invalid_argument("Expected a tuple of 4 elements");
77  return pixfloat4(tuple[0].cast<float>(), tuple[1].cast<float>(), tuple[2].cast<float>(), tuple[3].cast<float>());
78  }
79 
80  inline color3f toColor3f(const py::tuple& tuple) {
81  if (tuple.size() != 3)
82  throw std::invalid_argument("Expected a tuple of 3 elements");
83  return color3f{ tuple[0].cast<float>(), tuple[1].cast<float>(), tuple[2].cast<float>() };
84  }
85 
86  inline color4f toColor4f(const py::tuple& tuple) {
87  if (tuple.size() != 4)
88  throw std::invalid_argument("Expected a tuple of 4 elements");
89  return color4f{ tuple[0].cast<float>(), tuple[1].cast<float>(), tuple[2].cast<float>(), tuple[3].cast<float>() };
90  }
91 
92  inline color4i toColor4i(const py::tuple& tuple) {
93  if (tuple.size() != 4)
94  throw std::invalid_argument("Expected a tuple of 4 elements");
95  return color4i{ tuple[0].cast<uint8_t>(), tuple[1].cast<uint8_t>(), tuple[2].cast<uint8_t>(), tuple[3].cast<uint8_t>() };
96  }
97 
98 
99  py::object getModelOutputDataByName(NNets::Model& model, const std::string& opName, int output);
100 
101  py::object getModelOutputDataByOp(NNets::Model& model, const NNets::AbstractOperation& operation, int output);
102 
103  }
104 }
2D affine transformation.
Definition: geometry.h:329
numeric getA21() const
Definition: geometry.h:344
numeric getA11() const
Definition: geometry.h:342
numeric getA12() const
Definition: geometry.h:343
numeric getA22() const
Definition: geometry.h:345
2D point class
Definition: geometry.h:38
2D rectangle class All the utilities assume that the rectangle is normalized, e.g.
Definition: geometry.h:129
CustomPoint< numeric > b
Definition: geometry.h:131
CustomPoint< numeric > a
Definition: geometry.h:131
Abstract neural net operation (layer).
Definition: operation.h:46
Neural net model.
Definition: model.h:92
py::tuple toTuple(const CustomPoint< T > &point)
color4f toColor4f(const py::tuple &tuple)
color4i toColor4i(const py::tuple &tuple)
pixfloat4 toPixfloat4(const py::tuple &tuple)
color3f toColor3f(const py::tuple &tuple)
py::object getModelOutputDataByOp(NNets::Model &model, const NNets::AbstractOperation &operation, int output)
CustomRectangle< T > toRectangle(const py::tuple &tuple)
py::object getModelOutputDataByName(NNets::Model &model, const std::string &opName, int output)
CustomPoint< T > toPoint(const py::tuple &tuple)
4-channel floating point arithmetic
JNIEnv jlong jobject jstring opName