File: bindings.i

package info (click to toggle)
python-cmake-build-extension 0.6.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 488 kB
  • sloc: python: 504; cpp: 70; sh: 13; makefile: 5
file content (76 lines) | stat: -rw-r--r-- 1,990 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Documentation: https://numpy.org/doc/stable/reference/swig.interface-file.html

%module(package="mathlib") mymath

%{
#define SWIG_FILE_WITH_INIT
#include "mymath.h"
%}

%naturalvar;

// Convert all exceptions to RuntimeError
%include "exception.i"
%exception {
  try {
    $action
  } catch (const std::exception& e) {
    SWIG_exception(SWIG_RuntimeError, e.what());
  }
}

// Convert std::vector <-> python containers
%include <std_vector.i>
%template(VectorD) std::vector<double>;

// Doxygen typemaps
%typemap(doctype) std::vector<double> "Iterable[float]";

// Necessary for type hinting
%pythonbegin %{
from typing import Iterable
%}

// Include numpy.i typemaps
%include "numpy.i"

// Initialize NumPy
%init %{
import_array();
%}

// Apply the NumPy typemaps to the following signatures
%apply (double* IN_ARRAY1, int DIM1) {(double* in_1, unsigned size_in_1)};
%apply (double* IN_ARRAY1, int DIM1) {(double* in_2, unsigned size_in_2)};
%apply (double** ARGOUTVIEWM_ARRAY1, int* DIM1) {(double** out_1, int* size_out_1)};

// Create manually additional functions to allow using directly NumPy types
// without the need to alter the original C++ library
%inline %{
namespace numpy {
    double dot_numpy(double* in_1, unsigned size_in_1,
                     double* in_2, unsigned size_in_2)
    {
        const std::vector<double> vector1(in_1, in_1 + size_in_1);
        const std::vector<double> vector2(in_2, in_2 + size_in_2);

        return mymath::dot(vector1, vector2);
    }

    void normalize_numpy(double* in_1, unsigned size_in_1,
                         double** out_1, int* size_out_1)
    {
        const std::vector<double> vector(in_1, in_1 + size_in_1);

        auto result = mymath::normalize(vector);

        *out_1 = static_cast<double*>(malloc(result.size() * sizeof(double)));
        std::copy(result.begin(), result.end(), *out_1);

        *size_out_1 = result.size();
    }
}
%}

// Include the header with the symbols to wrap
%include "mymath.h"