forked from alandefreitas/matplotplusplus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_line.cpp
More file actions
189 lines (162 loc) 路 6.32 KB
/
Copy pathfunction_line.cpp
File metadata and controls
189 lines (162 loc) 路 6.32 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//
// Created by Alan Freitas on 10/07/20.
//
#include <matplot/axes_objects/function_line.h>
#include <matplot/util/common.h>
namespace matplot {
function_line::function_line(class axes_type *parent) : line(parent) {}
function_line::function_line(class axes_type *parent,
const function_type &function,
std::array<double, 2> x_range,
const std::string &line_spec)
: line(parent, {}, line_spec), fn_x_(function), t_range_(x_range) {}
function_line::function_line(class axes_type *parent,
const function_type &function_x,
const function_type &function_y,
std::array<double, 2> t_range,
const std::string &line_spec)
: line(parent, {}, line_spec), fn_x_(function_x), fn_y_(function_y),
t_range_(t_range) {}
function_line::function_line(class axes_type *parent,
const function_type &function_x,
const function_type &function_y,
const function_type &function_z,
std::array<double, 2> t_range,
const std::string &line_spec)
: line(parent, {}, line_spec), fn_x_(function_x), fn_y_(function_y),
fn_z_(function_z), t_range_(t_range) {}
std::string function_line::plot_string() {
make_sure_data_is_preprocessed();
return line::plot_string();
}
double function_line::xmax() {
if (fn_x_ && !fn_y_ && !polar_) {
return t_range_[1];
} else {
make_sure_data_is_preprocessed();
return line::xmax();
}
}
double function_line::xmin() {
if (fn_x_ && !fn_y_ && !polar_) {
return t_range_[0];
} else {
make_sure_data_is_preprocessed();
return line::xmin();
}
}
double function_line::ymin() {
make_sure_data_is_preprocessed();
return line::ymin();
}
double function_line::ymax() {
make_sure_data_is_preprocessed();
return line::ymax();
}
enum axes_object::axes_category function_line::axes_category() {
if (polar_) {
return axes_object::axes_category::polar;
} else if (!fn_z_) {
return axes_object::axes_category::two_dimensional;
} else {
return axes_object::axes_category::three_dimensional;
}
}
const std::function<double(double)> &function_line::fn() const {
return fn_x_;
}
class function_line &
function_line::fn(const std::function<double(double)> &fn) {
fn_x_ = fn;
touch();
return *this;
}
class function_line &function_line::tmin(double x) {
t_range_[0] = x;
touch();
return *this;
}
class function_line &function_line::tmax(double x) {
t_range_[1] = x;
touch();
return *this;
}
double function_line::tmin() { return t_range_[0]; }
double function_line::tmax() { return t_range_[1]; }
const std::array<double, 2> &function_line::t_range() const {
return t_range_;
}
class function_line &
function_line::t_range(const std::array<double, 2> &t_range) {
t_range_ = t_range;
touch();
return *this;
}
class function_line &function_line::t_range(double t_min, double t_max) {
return t_range({t_min, t_max});
}
void function_line::make_sure_data_is_preprocessed() {
if (fn_x_ && !fn_y_) {
bool mesh_is_ok =
automatic_mesh_density_ ||
(!automatic_mesh_density_ && x_data().size() == mesh_density_);
// put the t_data directly in x_data as they are equal anyway
if (!mesh_is_ok || x_data_.empty() ||
x_data_.front() != t_range_[0] ||
x_data_.back() != t_range_[1]) {
x_data_ =
linspace(t_range_[0], t_range_[1],
automatic_mesh_density_ ? 500 : mesh_density_);
y_data_ = transform(x_data_, fn_x_);
}
} else if ((fn_x_ && !fn_z_)) {
// proprocess t_data and use it to calculate x and y data
bool mesh_is_ok =
automatic_mesh_density_ ||
(!automatic_mesh_density_ && t_data_.size() == mesh_density_);
if (!mesh_is_ok || t_data_.empty() ||
t_data_.front() != t_range_[0] ||
t_data_.back() != t_range_[1]) {
t_data_ =
linspace(t_range_[0], t_range_[1],
automatic_mesh_density_ ? 500 : mesh_density_);
x_data_ = transform(t_data_, fn_x_);
y_data_ = transform(t_data_, fn_y_);
}
} else {
bool mesh_is_ok =
automatic_mesh_density_ ||
(!automatic_mesh_density_ && t_data_.size() == mesh_density_);
if (!mesh_is_ok || t_data_.empty() ||
t_data_.front() != t_range_[0] ||
t_data_.back() != t_range_[1]) {
t_data_ =
linspace(t_range_[0], t_range_[1],
automatic_mesh_density_ ? 500 : mesh_density_);
x_data_ = transform(t_data_, fn_x_);
y_data_ = transform(t_data_, fn_y_);
z_data_ = transform(t_data_, fn_z_);
}
}
}
size_t function_line::mesh_density() const { return mesh_density_; }
class function_line &function_line::mesh_density(size_t mesh_density) {
if (mesh_density != mesh_density_ || automatic_mesh_density_) {
mesh_density_ = mesh_density;
automatic_mesh_density_ = false;
touch();
}
return *this;
}
bool function_line::automatic_mesh_density() const {
return automatic_mesh_density_;
}
class function_line &
function_line::automatic_mesh_density(bool automatic_mesh_density) {
if (automatic_mesh_density_ == automatic_mesh_density) {
automatic_mesh_density_ = automatic_mesh_density;
touch();
}
return *this;
}
} // namespace matplot