-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotter.h
More file actions
73 lines (53 loc) · 1.55 KB
/
plotter.h
File metadata and controls
73 lines (53 loc) · 1.55 KB
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
#ifndef PYBIND_PLOT_PLOTTER_H
#define PYBIND_PLOT_PLOTTER_H
#include <cmath> // std::exp, std::cos
#include <iostream>
#include <vector>
#include <pybind11/embed.h> // py::scoped_interpreter
#include <pybind11/stl.h> // bindings from C++ STL containers to Python types
namespace py = pybind11;
class Plotter{
public:
Plotter(Plotter & other) = delete;
void operator=(const Plotter& ) = delete;
static Plotter * GetInstance();
template<class DataType>
void plot(const std::vector<DataType>& signal){
// Start the Python interpreter
py::scoped_interpreter guard{};
using namespace py::literals;
// Save the necessary local variables in a Python dict
py::dict locals = py::dict{
"signal"_a = signal,
};
// Execute Python code, using the variables saved in `locals`
py::exec(R"(
import matplotlib.pyplot as plt
plt.plot(signal)
plt.show()
)",
py::globals(), locals);
std::cout << "Exiting..." << std::endl;
};
protected:
Plotter(){
}
private:
static Plotter* _instance;
};
Plotter* Plotter::_instance = nullptr;;
/**
* Static methods should be defined outside the class.
*/
Plotter *Plotter::GetInstance()
{
/**
* This is a safer way to create an instance. instance = new Singleton is
* dangeruous in case two instance threads wants to access at the same time
*/
if(_instance==nullptr){
_instance = new Plotter();
}
return _instance;
}
#endif //PYBIND_PLOT_PLOTTER_H