Skip to content

Commit dcd56a0

Browse files
slightly fix interface
1 parent 4a9402d commit dcd56a0

File tree

7 files changed

+171
-100
lines changed

7 files changed

+171
-100
lines changed

examples/vector_fields/quiver/quiver_4.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ int main() {
1212
contour(x, y, z);
1313
hold(on);
1414

15-
quiver(x, y, dx, dy);
15+
quiver(x, y, dx, dy, 1, "b");
1616
hold(off);
1717

1818
show();
1919
return 0;
20-
}
20+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
add_executable(example_quiver3_1 quiver3_1.cpp)
22
target_link_libraries(example_quiver3_1 PUBLIC matplot)
33

4-
4+
add_executable(example_quiver3_2 quiver3_2.cpp)
5+
target_link_libraries(example_quiver3_2 PUBLIC matplot)
56

examples/vector_fields/quiver3/quiver3_1.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,4 @@ matplot::vector_2d get_w() {
119119
{0.229416, 0.248069, 0.267261, 0.285714, 0.301511, 0.312348, 0.316228,
120120
0.312348, 0.301511, 0.285714, 0.267261, 0.248069, 0.229416}};
121121
return x;
122-
}
122+
}

source/matplot/axes_objects/vectors.cpp

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ namespace matplot {
3737
: axes_object(parent), line_spec_(this, line_spec), y_data_(y_data),
3838
x_data_(x_data), u_data_(u_data), v_data_(v_data) {}
3939

40+
vectors::vectors(class axes_type *parent, const std::vector<double> &x_data,
41+
const std::vector<double> &y_data,
42+
const std::vector<double> &u_data,
43+
const std::vector<double> &v_data,
44+
const std::vector<double> &m_data,
45+
std::string_view line_spec)
46+
: axes_object(parent), line_spec_(this, line_spec), y_data_(y_data),
47+
x_data_(x_data), u_data_(u_data), v_data_(v_data), m_data_(m_data) {}
48+
4049
vectors::vectors(class axes_type *parent, const std::vector<double> &x_data,
4150
const std::vector<double> &y_data,
4251
const std::vector<double> &z_data,
@@ -116,9 +125,9 @@ namespace matplot {
116125
ss << " " << 0.0;
117126
}
118127

119-
if (normalize_ && m_value != 0) {
128+
if (normalize_) {
120129
double mag = sqrt(u_value * u_value + v_value * v_value +
121-
w_value * w_value);
130+
w_value * w_value);
122131
u_value *= scale_ / mag;
123132
v_value *= scale_ / mag;
124133
w_value *= scale_ / mag;
@@ -131,8 +140,9 @@ namespace matplot {
131140
} else if (parent_->is_3d()) {
132141
ss << " " << 0.0;
133142
}
134-
135-
ss << " " << m_value;
143+
if (!m_data_.empty()) {
144+
ss << " " << m_value;
145+
}
136146

137147
ss << "\n";
138148
}
@@ -152,8 +162,12 @@ namespace matplot {
152162
}
153163

154164
void vectors::maybe_update_line_spec() {
155-
if (line_spec_.has_non_custom_marker() && !line_spec_.user_color() &&
156-
!line_spec_.marker_user_color()) {
165+
if (!line_spec_.user_color() && m_data_.empty()) {
166+
auto c = parent_->get_color_and_bump();
167+
line_spec_.color(c);
168+
} else if (line_spec_.has_non_custom_marker() &&
169+
!line_spec_.user_color() &&
170+
!line_spec_.marker_user_color()) {
157171
auto c = parent_->get_color_and_bump();
158172
line_spec_.marker_color(c);
159173
}
@@ -307,6 +321,14 @@ namespace matplot {
307321
return *this;
308322
}
309323

324+
const std::vector<double> &vectors::m_data() const { return m_data_; }
325+
326+
class vectors &vectors::m_data(const std::vector<double> &m_data) {
327+
m_data_ = m_data;
328+
touch();
329+
return *this;
330+
}
331+
310332
const std::vector<size_t> &vectors::marker_indices() const {
311333
return marker_indices_;
312334
}
@@ -432,11 +454,11 @@ namespace matplot {
432454
}
433455

434456
double vectors::scale() const { return scale_; }
435-
457+
436458
class vectors &vectors::scale(double scale) {
437-
scale_ = scale;
438-
touch();
439-
return *this;
459+
scale_ = scale;
460+
touch();
461+
return *this;
440462
}
441463

442464
} // namespace matplot

source/matplot/axes_objects/vectors.h

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ namespace matplot {
4040
const std::vector<double> &v_data,
4141
std::string_view line_spec = "");
4242

43+
vectors(class axes_type *parent, const std::vector<double> &x_data,
44+
const std::vector<double> &y_data,
45+
const std::vector<double> &u_data,
46+
const std::vector<double> &v_data,
47+
const std::vector<double> &m_data,
48+
std::string_view line_spec = "");
49+
4350
vectors(class axes_type *parent, const std::vector<double> &x_data,
4451
const std::vector<double> &y_data,
4552
const std::vector<double> &z_data,
@@ -49,18 +56,18 @@ namespace matplot {
4956
std::string_view line_spec = "");
5057

5158
vectors(class axes_type *parent, const std::vector<double> &x_data,
52-
const std::vector<double> &y_data,
53-
const std::vector<double> &z_data,
54-
const std::vector<double> &u_data,
55-
const std::vector<double> &v_data,
56-
const std::vector<double> &w_data,
57-
const std::vector<double> &m_data,
58-
std::string_view line_spec = "");
59+
const std::vector<double> &y_data,
60+
const std::vector<double> &z_data,
61+
const std::vector<double> &u_data,
62+
const std::vector<double> &v_data,
63+
const std::vector<double> &w_data,
64+
const std::vector<double> &m_data,
65+
std::string_view line_spec = "");
5966

6067
/// If we receive an axes_handle, we can convert it to a raw
6168
/// pointer because there is no ownership involved here
6269
template <class... Args>
63-
vectors(const axes_handle &parent, Args&&... args)
70+
vectors(const axes_handle &parent, Args &&...args)
6471
: vectors(parent.get(), std::forward<Args>(args)...) {}
6572

6673
virtual ~vectors() = default;
@@ -90,6 +97,9 @@ namespace matplot {
9097
const std::vector<double> &z_data() const;
9198
class vectors &z_data(const std::vector<double> &z_data);
9299

100+
const std::vector<double> &m_data() const;
101+
class vectors &m_data(const std::vector<double> &m_data);
102+
93103
const std::vector<size_t> &marker_indices() const;
94104
class vectors &
95105
marker_indices(const std::vector<size_t> &marker_indices);
@@ -111,7 +121,7 @@ namespace matplot {
111121

112122
bool visible() const;
113123
class vectors &visible(bool visible);
114-
124+
115125
bool normalize() const;
116126
class vectors &normalize(bool normalize);
117127

@@ -237,7 +247,7 @@ namespace matplot {
237247

238248
/// True if vectors are normalized
239249
bool normalize_{false};
240-
};
250+
};
241251
} // namespace matplot
242252

243253
#endif // MATPLOTPLUSPLUS_VECTORS_H

source/matplot/core/axes_type.cpp

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4525,7 +4525,8 @@ namespace matplot {
45254525
vectors_handle axes_type::quiver(const std::vector<double> &x,
45264526
const std::vector<double> &y,
45274527
const std::vector<double> &u,
4528-
const std::vector<double> &v, double scale,
4528+
const std::vector<double> &v,
4529+
const std::vector<double> &m, double scale,
45294530
std::string_view line_spec) {
45304531
axes_silencer temp_silencer_{this};
45314532

@@ -4560,10 +4561,10 @@ namespace matplot {
45604561
v, [&](double v) { return (v / v_max) * scale * y_max; })
45614562
: v;
45624563

4563-
vectors_handle l = std::make_shared<class vectors>(this, x, y, u_scaled,
4564-
v_scaled, line_spec);
4564+
vectors_handle l = std::make_shared<class vectors>(
4565+
this, x, y, u_scaled, v_scaled, m, line_spec);
4566+
l->scale(scale);
45654567
this->emplace_object(l);
4566-
45674568
return l;
45684569
}
45694570

@@ -4577,12 +4578,23 @@ namespace matplot {
45774578
scale, line_spec);
45784579
}
45794580

4581+
/// Quiver - x,y,u,v with magnitude
4582+
vectors_handle axes_type::quiver(const std::vector<double> &x,
4583+
const std::vector<double> &y,
4584+
const std::vector<double> &u,
4585+
const std::vector<double> &v, double scale,
4586+
std::string_view line_spec) {
4587+
return this->quiver(x, y, u, v, std::vector<double>{}, scale,
4588+
line_spec);
4589+
}
4590+
45804591
/// Quiver 3d - Core function
45814592
vectors_handle axes_type::quiver3(
45824593
const std::vector<double> &x, const std::vector<double> &y,
45834594
const std::vector<double> &z, const std::vector<double> &u,
45844595
const std::vector<double> &v, const std::vector<double> &w,
4585-
double scale, std::string_view line_spec) {
4596+
const std::vector<double> &m, double scale,
4597+
std::string_view line_spec) {
45864598
axes_silencer temp_silencer_{this};
45874599

45884600
auto x_copy = x;
@@ -4605,15 +4617,15 @@ namespace matplot {
46054617
double v_max = *std::max_element(v.begin(), v.end());
46064618
double y_max = ydiffmin != y_diff.end() ? *ydiffmin : v_max;
46074619

4608-
auto z_copy = y;
4620+
auto z_copy = z;
46094621
std::sort(z_copy.begin(), z_copy.end());
46104622
z_copy.resize(std::distance(z_copy.begin(),
46114623
std::unique(z_copy.begin(), z_copy.end())));
46124624
std::vector<double> z_diff(z_copy.size());
46134625
std::adjacent_difference(z_copy.begin(), z_copy.end(), z_diff.begin());
4614-
// auto zdiffmin = std::min_element(z_diff.begin() + 1, z_diff.end());
4615-
double w_max = *std::max_element(v.begin(), v.end());
4616-
double z_max = ydiffmin != z_diff.end() ? *ydiffmin : w_max;
4626+
auto zdiffmin = std::min_element(z_diff.begin() + 1, z_diff.end());
4627+
double w_max = *std::max_element(w.begin(), w.end());
4628+
double z_max = zdiffmin != z_diff.end() ? *zdiffmin : w_max;
46174629

46184630
auto u_scaled =
46194631
(scale != 0.)
@@ -4630,11 +4642,6 @@ namespace matplot {
46304642
? transform(
46314643
w, [&](double w) { return (w / w_max) * scale * z_max; })
46324644
: w;
4633-
std::vector<double> m;
4634-
for (int i = 0; i < u.size(); i++) {
4635-
double mag = sqrt((u[i] * u[i]) + (v[i] * v[i]) + (w[i] * w[i]));
4636-
m.emplace_back(mag);
4637-
}
46384645

46394646
vectors_handle l = std::make_shared<class vectors>(
46404647
this, x, y, z, u_scaled, v_scaled, w_scaled, m, line_spec);
@@ -4671,6 +4678,17 @@ namespace matplot {
46714678
return this->quiver3(xx, yy, z, u, v, w, scale, line_spec);
46724679
}
46734680

4681+
/// Quiver 3d - magnitude included
4682+
vectors_handle axes_type::quiver3(
4683+
const std::vector<double> &x, const std::vector<double> &y,
4684+
const std::vector<double> &z, const std::vector<double> &u,
4685+
const std::vector<double> &v, const std::vector<double> &w,
4686+
double scale, std::string_view line_spec) {
4687+
auto l = this->quiver3(x, y, z, u, v, w, std::vector<double>{}, scale,
4688+
line_spec);
4689+
return l;
4690+
}
4691+
46744692
/// Fence - Core function
46754693
surface_handle axes_type::fence(const std::vector<std::vector<double>> &X,
46764694
const std::vector<std::vector<double>> &Y,

0 commit comments

Comments
 (0)