TBTK
Need a break? Support the development by playing Polarity Puzzles
Interpreter.h
1 /* Copyright 2019 Kristofer Björnson
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /* The MIT License (MIT)
17  *
18  * Copyright (c) 2014 Benno Evers
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining a copy
21  * of this software and associated documentation files (the "Software"), to deal
22  * in the Software without restriction, including without limitation the rights
23  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
24  * copies of the Software, and to permit persons to whom the Software is
25  * furnished to do so, subject to the following conditions:
26  *
27  * The above copyright notice and this permission notice shall be included in all
28  * copies or substantial portions of the Software.
29  *
30  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36  * SOFTWARE. */
37 
39 #pragma once
40 
41 //Allow for inclusion in multiple translation units
42 #ifndef TBTK_NUMPY_INITIALIZING_TRANSLATION_UNIT
43 # define NO_IMPORT_ARRAY
44 #endif
45 #define PY_ARRAY_UNIQUE_SYMBOL TBTK_PY_ARRAY_API
46 #define PY_UFUNC_UNIQUE_SYMBOL TBTK_PY_UFUNC_API
47 
48 //The convention of putting third party libraries after TBTK and std libraries
49 //is here ignored. Pyhton.h has to be included first to avoid warning that
50 //_POSIX_C_SOURCE is redefined.
51 #include <Python.h>
52 
53 #include <vector>
54 #include <map>
55 #include <array>
56 #include <numeric>
57 #include <algorithm>
58 #include <stdexcept>
59 #include <iostream>
60 #include <cstdint> // <cstdint> requires c++11 support
61 #include <functional>
62 
63 #ifndef WITHOUT_NUMPY
64 # define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
65 # include <numpy/arrayobject.h>
66 
67 # ifdef WITH_OPENCV
68 # include <opencv2/opencv.hpp>
69 # endif // WITH_OPENCV
70 
71 /*
72  * A bunch of constants were removed in OpenCV 4 in favour of enum classes, so
73  * define the ones we need here.
74  */
75 # if CV_MAJOR_VERSION > 3
76 # define CV_BGR2RGB cv::COLOR_BGR2RGB
77 # define CV_BGRA2RGBA cv::COLOR_BGRA2RGBA
78 # endif
79 #endif // WITHOUT_NUMPY
80 
81 #if PY_MAJOR_VERSION >= 3
82 # define PyString_FromString PyUnicode_FromString
83 # define PyInt_FromLong PyLong_FromLong
84 # define PyString_FromString PyUnicode_FromString
85 # define PyInt_FromString PyLong_FromString
86 #endif
87 
88 namespace TBTK {
89 namespace Visualization {
90 namespace MatPlotLib {
91 namespace matplotlibcpp {
92 namespace detail {
93 
94 extern std::string s_backend;
95 
96 struct Interpreter {
97  PyObject *s_python_function_show;
98  PyObject *s_python_function_close;
99  PyObject *s_python_function_draw;
100  PyObject *s_python_function_pause;
101  PyObject *s_python_function_save;
102  PyObject *s_python_function_figure;
103  PyObject *s_python_function_fignum_exists;
104  PyObject *s_python_function_plot;
105  PyObject *s_python_function_quiver;
106  PyObject *s_python_function_semilogx;
107  PyObject *s_python_function_semilogy;
108  PyObject *s_python_function_loglog;
109  PyObject *s_python_function_fill;
110  PyObject *s_python_function_fill_between;
111  PyObject *s_python_function_hist;
112  PyObject *s_python_function_imshow;
113  PyObject *s_python_function_scatter;
114  PyObject *s_python_function_subplot;
115  PyObject *s_python_function_subplot2grid;
116  PyObject *s_python_function_legend;
117  PyObject *s_python_function_xlim;
118  PyObject *s_python_function_ion;
119  PyObject *s_python_function_ginput;
120  PyObject *s_python_function_ylim;
121  PyObject *s_python_function_title;
122  PyObject *s_python_function_axis;
123  PyObject *s_python_function_xlabel;
124  PyObject *s_python_function_ylabel;
125  PyObject *s_python_function_xticks;
126  PyObject *s_python_function_yticks;
127  PyObject *s_python_function_grid;
128  PyObject *s_python_function_clf;
129  PyObject *s_python_function_errorbar;
130  PyObject *s_python_function_annotate;
131  PyObject *s_python_function_tight_layout;
132  PyObject *s_python_colormap;
133  PyObject *s_python_empty_tuple;
134  PyObject *s_python_function_stem;
135  PyObject *s_python_function_xkcd;
136  PyObject *s_python_function_text;
137  PyObject *s_python_function_suptitle;
138  PyObject *s_python_function_bar;
139  PyObject *s_python_function_subplots_adjust;
140  //<Added>
141  PyObject *s_python_function_gcf;
142  PyObject *s_python_function_gca;
143  PyObject *mpl_toolkitsmod;
144  PyObject *axis3dmod;
145  //</Added>
146 
147  static Interpreter& get();
148 
149  void initializeMPLToolkits();
150 
151  PyObject* safe_import(PyObject* module, std::string fname);
152 private:
153 #ifndef WITHOUT_NUMPY
154 # if PY_MAJOR_VERSION >= 3
155  void *import_numpy();
156 # else
157  void import_numpy();
158 # endif
159 #endif
160  Interpreter();
161 
162  ~Interpreter();
163 };
164 
165 } // end namespace detail
166 } // end namespace matplotlibcpp
167 } // end namespace MatPlotLib
168 } // end namespace Visualization
169 } // end namespace TBTK
Definition: Boolean.h:32