Skip to content

Commit b8a3f10

Browse files
colormap and possible normalize
1 parent ff5c42a commit b8a3f10

File tree

4 files changed

+144
-60
lines changed

4 files changed

+144
-60
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,4 @@ if (BUILD_INSTALLER AND BUILD_PACKAGE)
128128

129129
# Always include CPack at last
130130
include(CPack)
131-
endif ()
131+
endif ()

source/matplot/axes_objects/vectors.cpp

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,30 @@ namespace matplot {
4848
x_data_(x_data), z_data_(z_data), u_data_(u_data), v_data_(v_data),
4949
w_data_(w_data) {}
5050

51+
vectors::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+
: axes_object(parent), line_spec_(this, line_spec), y_data_(y_data),
60+
x_data_(x_data), z_data_(z_data), u_data_(u_data), v_data_(v_data),
61+
w_data_(w_data), m_data_(m_data) {}
62+
5163
std::string vectors::plot_string() {
5264
maybe_update_line_spec();
5365
std::stringstream ss;
54-
ss << " '-' with vectors " +
55-
line_spec_.plot_string(
56-
line_spec::style_to_plot::plot_line_only, false);
66+
ss << " '-' with vectors";
67+
if (!line_spec_.user_color()) {
68+
ss << " linecolor palette ";
69+
} else {
70+
ss << " linecolor rgb \'" << to_string(line_spec_.color()) << "\'";
71+
}
72+
73+
ss << " linewidth " << line_spec_.line_width();
74+
5775
if (use_y2_) {
5876
ss << " xlim x1y2";
5977
}
@@ -79,6 +97,7 @@ namespace matplot {
7997
double u_value = u_data_.size() > i ? u_data_[i] : 0;
8098
double v_value = v_data_.size() > i ? v_data_[i] : 0;
8199
double w_value = w_data_.size() > i ? w_data_[i] : 0;
100+
double m_value = m_data_.size() > i ? m_data_[i] : 0;
82101

83102
bool is_end_of_series =
84103
!std::isfinite(x_value) || !std::isfinite(y_value) ||
@@ -97,6 +116,15 @@ namespace matplot {
97116
ss << " " << 0.0;
98117
}
99118

119+
if (normalize_ && m_value != 0) {
120+
double length = sqrt(u_value * u_value + v_value * v_value +
121+
w_value * w_value);
122+
double scale = length / m_value;
123+
u_value *= scale / length;
124+
v_value *= scale / length;
125+
w_value *= scale / length;
126+
}
127+
100128
ss << " " << u_value;
101129
ss << " " << v_value;
102130
if (is_3d()) {
@@ -105,6 +133,8 @@ namespace matplot {
105133
ss << " " << 0.0;
106134
}
107135

136+
ss << " " << m_value;
137+
108138
ss << "\n";
109139
}
110140
// if polar, gnuplot uses the vector origin to determine
@@ -123,13 +153,8 @@ namespace matplot {
123153
}
124154

125155
void vectors::maybe_update_line_spec() {
126-
if (line_spec_.has_line() && !line_spec_.user_color()) {
127-
// if user didn't set the color, get color from xlim
128-
auto c = parent_->get_color_and_bump();
129-
line_spec_.color(c);
130-
} else if (line_spec_.has_non_custom_marker() &&
131-
!line_spec_.user_color() &&
132-
!line_spec_.marker_user_color()) {
156+
if (line_spec_.has_non_custom_marker() && !line_spec_.user_color() &&
157+
!line_spec_.marker_user_color()) {
133158
auto c = parent_->get_color_and_bump();
134159
line_spec_.marker_color(c);
135160
}
@@ -399,4 +424,12 @@ namespace matplot {
399424
return *this;
400425
}
401426

402-
} // namespace matplot
427+
bool vectors::norm() const { return normalize_; }
428+
429+
class vectors &vectors::norm(bool normalize) {
430+
normalize_ = normalize;
431+
touch();
432+
return *this;
433+
}
434+
435+
} // namespace matplot

source/matplot/axes_objects/vectors.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ namespace matplot {
4848
const std::vector<double> &w_data,
4949
std::string_view line_spec = "");
5050

51+
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+
5160
/// If we receive an axes_handle, we can convert it to a raw
5261
/// pointer because there is no ownership involved here
5362
template <class... Args>
@@ -102,6 +111,9 @@ namespace matplot {
102111

103112
bool visible() const;
104113
class vectors &visible(bool visible);
114+
115+
bool norm() const;
116+
class vectors &norm(bool normalize);
105117

106118
public /* getters and setters bypassing the line_spec */:
107119
float line_width() const;
@@ -194,6 +206,9 @@ namespace matplot {
194206
std::vector<double> v_data_{};
195207
std::vector<double> w_data_{};
196208

209+
/// Vector magnitude
210+
std::vector<double> m_data_{};
211+
197212
/// Positions at which we want markers to appear
198213
std::vector<size_t> marker_indices_{};
199214
std::vector<float> marker_sizes_{};
@@ -213,7 +228,10 @@ namespace matplot {
213228

214229
/// True if visible
215230
bool visible_{true};
216-
};
231+
232+
/// True if vectors are normalized
233+
bool normalize_{false};
234+
};
217235
} // namespace matplot
218236

219237
#endif // MATPLOTPLUSPLUS_VECTORS_H

0 commit comments

Comments
 (0)