Skip to content

Commit

Permalink
Make explicit conversions in axes, figure, colors, and common
Browse files Browse the repository at this point in the history
  • Loading branch information
alandefreitas committed Sep 2, 2020
1 parent 9267985 commit 1993634
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 43 deletions.
42 changes: 21 additions & 21 deletions source/matplot/core/axes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3631,7 +3631,7 @@ namespace matplot {
const double opacity = (1 - fc[0]);
const double new_opacity = opacity * 0.7;
const double new_alpha = 1 - new_opacity;
fc[0] = new_alpha;
fc[0] = static_cast<float>(new_alpha);
h->face_color(fc);
h->line_color(to_array("white"));
this->emplace_object(h);
Expand Down Expand Up @@ -3993,13 +3993,13 @@ namespace matplot {
if (w > h) {
double old_width = this->width();
double new_width = h / this->parent()->width();
this->width(new_width);
this->x_origin(this->x_origin() + (old_width - new_width) / 2.);
this->width(static_cast<float>(new_width));
this->x_origin(this->x_origin() + static_cast<float>(old_width - new_width) / 2.f);
} else if (h > w) {
double old_height = this->height();
double new_height = h / this->parent()->height();
this->height(new_height);
this->y_origin(this->y_origin() + (old_height - new_height) / 2.);
this->height(static_cast<float>(new_height));
this->y_origin(this->y_origin() + static_cast<float>(old_height - new_height) / 2.f);
}
}

Expand Down Expand Up @@ -4572,8 +4572,8 @@ namespace matplot {
double mesh_density) {
axes_silencer temp_silencer_{this};

std::vector<double> x = linspace(x_range[0], x_range[1], mesh_density);
std::vector<double> y = linspace(y_range[0], y_range[1], mesh_density);
std::vector<double> x = linspace(x_range[0], x_range[1], static_cast<size_t>(mesh_density));
std::vector<double> y = linspace(y_range[0], y_range[1], static_cast<size_t>(mesh_density));
auto [X, Y] = meshgrid(x, y);
auto Z = transform(X, Y, fn);
surface_handle l = this->mesh(X, Y, Z, {});
Expand All @@ -4590,8 +4590,8 @@ namespace matplot {
double mesh_density) {
axes_silencer temp_silencer_{this};

std::vector<double> u = linspace(u_range[0], u_range[1], mesh_density);
std::vector<double> v = linspace(v_range[0], v_range[1], mesh_density);
std::vector<double> u = linspace(u_range[0], u_range[1], static_cast<size_t>(mesh_density));
std::vector<double> v = linspace(v_range[0], v_range[1], static_cast<size_t>(mesh_density));
auto [U, V] = meshgrid(u, v);
auto X = transform(U, V, funx);
auto Y = transform(U, V, funy);
Expand All @@ -4607,7 +4607,7 @@ namespace matplot {
const std::array<double, 4> &xy_range,
double mesh_density) {
return this->fmesh(fn, {xy_range[0], xy_range[1]},
{xy_range[2], xy_range[3]}, mesh_density);
{xy_range[2], xy_range[3]}, static_cast<size_t>(mesh_density));
}

/// Lambda function mesh - Parametric
Expand All @@ -4618,15 +4618,15 @@ namespace matplot {
const std::array<double, 4> &uv_range,
double mesh_density) {
return this->fmesh(funx, funy, funz, {uv_range[0], uv_range[1]},
{uv_range[2], uv_range[3]}, mesh_density);
{uv_range[2], uv_range[3]}, static_cast<size_t>(mesh_density));
}

/// Function mesh
/// Grid / Both ranges in the same array size 2
surface_handle axes::fmesh(fcontour_function_type fn,
const std::array<double, 2> &xy_range,
double mesh_density) {
return this->fmesh(fn, xy_range, xy_range, mesh_density);
return this->fmesh(fn, xy_range, xy_range, static_cast<size_t>(mesh_density));
}

/// Function mesh
Expand All @@ -4636,7 +4636,7 @@ namespace matplot {
fcontour_function_type funz,
const std::array<double, 2> &uv_range,
double mesh_density) {
return this->fmesh(funx, funy, funz, uv_range, uv_range, mesh_density);
return this->fmesh(funx, funy, funz, uv_range, uv_range, static_cast<size_t>(mesh_density));
}

// z = f(x,y)
Expand All @@ -4649,8 +4649,8 @@ namespace matplot {
std::string line_spec, double mesh_density) {
axes_silencer temp_silencer_{this};

std::vector<double> x = linspace(x_range[0], x_range[1], mesh_density);
std::vector<double> y = linspace(y_range[0], y_range[1], mesh_density);
std::vector<double> x = linspace(x_range[0], x_range[1], static_cast<size_t>(mesh_density));
std::vector<double> y = linspace(y_range[0], y_range[1], static_cast<size_t>(mesh_density));
auto [X, Y] = meshgrid(x, y);
auto Z = transform(X, Y, fn);
surface_handle l = this->surf(X, Y, Z, {}, line_spec);
Expand All @@ -4668,8 +4668,8 @@ namespace matplot {
std::string line_spec, double mesh_density) {
axes_silencer temp_silencer_{this};

std::vector<double> u = linspace(u_range[0], u_range[1], mesh_density);
std::vector<double> v = linspace(v_range[0], v_range[1], mesh_density);
std::vector<double> u = linspace(u_range[0], u_range[1], static_cast<size_t>(mesh_density));
std::vector<double> v = linspace(v_range[0], v_range[1], static_cast<size_t>(mesh_density));
auto [U, V] = meshgrid(u, v);
auto X = transform(U, V, funx);
auto Y = transform(U, V, funy);
Expand All @@ -4685,7 +4685,7 @@ namespace matplot {
const std::array<double, 4> &xy_range,
std::string line_spec, double mesh_density) {
return this->fsurf(fn, {xy_range[0], xy_range[1]},
{xy_range[2], xy_range[3]}, line_spec, mesh_density);
{xy_range[2], xy_range[3]}, line_spec, static_cast<size_t>(mesh_density));
}

/// Parametric / Both ranges in the same array size 4
Expand All @@ -4695,15 +4695,15 @@ namespace matplot {
const std::array<double, 4> &uv_range,
std::string line_spec, double mesh_density) {
return this->fsurf(funx, funy, funz, {uv_range[0], uv_range[1]},
{uv_range[2], uv_range[3]}, line_spec, mesh_density);
{uv_range[2], uv_range[3]}, line_spec, static_cast<size_t>(mesh_density));
}

/// Function surf
/// Grid / Both ranges in the same array size 2
surface_handle axes::fsurf(fcontour_function_type fn,
const std::array<double, 2> &xy_range,
std::string line_spec, double mesh_density) {
return this->fsurf(fn, xy_range, xy_range, line_spec, mesh_density);
return this->fsurf(fn, xy_range, xy_range, line_spec, static_cast<size_t>(mesh_density));
}

/// Function surf
Expand All @@ -4714,7 +4714,7 @@ namespace matplot {
const std::array<double, 2> &uv_range,
std::string line_spec, double mesh_density) {
return this->fsurf(funx, funy, funz, uv_range, uv_range, line_spec,
mesh_density);
static_cast<size_t>(mesh_density));
}

/// Mesh - Core function
Expand Down
8 changes: 4 additions & 4 deletions source/matplot/core/figure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -797,12 +797,12 @@ namespace matplot {
return h;
}

const std::vector<std::shared_ptr<struct axes>> &figure::children() const {
const std::vector<std::shared_ptr<class axes>> &figure::children() const {
return children_;
}

void figure::children(
const std::vector<std::shared_ptr<struct axes>> &children) {
const std::vector<std::shared_ptr<class axes>> &children) {
children_ = children;
}

Expand Down Expand Up @@ -925,8 +925,8 @@ namespace matplot {
float subplot_height = (1.f - t_margin - b_margin) / n_rows;
for (size_t i = 0; i < n_rows; ++i) {
for (size_t j = 0; j < n_cols; ++j) {
axs[i][j]->width(subplot_width - 0.01);
axs[i][j]->height(subplot_height - 0.01);
axs[i][j]->width(subplot_width - 0.01f);
axs[i][j]->height(subplot_height - 0.01f);
axs[i][j]->x_origin(l_margin + subplot_width * j);
axs[i][j]->y_origin(1.f - t_margin - subplot_height -
subplot_height * i);
Expand Down
18 changes: 9 additions & 9 deletions source/matplot/util/colors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2829,22 +2829,22 @@ namespace matplot {
value /= max;
value *= cm.size() - 1;
// find positions in colormap we are interested
size_t first_position = std::max(0., floor(value));
size_t second_position = std::min(ceil(value), cm.size() - 1.);
size_t first_position = static_cast<size_t>(std::max(0., floor(value)));
size_t second_position = static_cast<size_t>(std::min(ceil(value), cm.size() - 1.));
// interpolation alpha
double amount_first_color = 1 - (value - floor(value));
double amount_second_color = value - floor(value);
// create color
auto &first_color = cm[first_position];
auto &second_color = cm[second_position];
std::array<float, 4> result;
std::array<float, 4> result{};
result[0] = 0.;
result[1] = amount_first_color * first_color[0] +
amount_second_color * second_color[0];
result[2] = amount_first_color * first_color[1] +
amount_second_color * second_color[1];
result[3] = amount_first_color * first_color[2] +
amount_second_color * second_color[2];
result[1] = static_cast<float>(amount_first_color) * first_color[0] +
static_cast<float>(amount_second_color) * second_color[0];
result[2] = static_cast<float>(amount_first_color) * first_color[1] +
static_cast<float>(amount_second_color) * second_color[1];
result[3] = static_cast<float>(amount_first_color) * first_color[2] +
static_cast<float>(amount_second_color) * second_color[2];
return result;
}

Expand Down
18 changes: 9 additions & 9 deletions source/matplot/util/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ namespace matplot {

std::vector<double> iota(double d1, double step, double d2) {
vector_1d r;
r.reserve((d2 - d1) / step);
r.reserve(static_cast<size_t>((d2 - d1) / step));
for (double i = d1; i <= d2; i += step) {
r.emplace_back(i);
}
Expand Down Expand Up @@ -587,8 +587,8 @@ namespace matplot {
image_channels_t img(n_channels,
image_channel_t(height, image_row_t(width, 0)));
for (size_t channel = 0; channel < n_channels; ++channel) {
for (size_t i = 0; i < cimg_image.height(); ++i) {
for (size_t j = 0; j < cimg_image.width(); ++j) {
for (int i = 0; i < cimg_image.height(); ++i) {
for (int j = 0; j < cimg_image.width(); ++j) {
img[channel][i][j] =
cimg_image.operator()(j, i, 0, channel);
}
Expand All @@ -604,8 +604,8 @@ namespace matplot {
const size_t height = A[0].size();
const size_t width = A[0][0].size();
for (size_t channel = 0; channel < n_channels; ++channel) {
for (size_t i = 0; i < cimg_image.height(); ++i) {
for (size_t j = 0; j < cimg_image.width(); ++j) {
for (int i = 0; i < cimg_image.height(); ++i) {
for (int j = 0; j < cimg_image.width(); ++j) {
cimg_image.operator()(j, i, 0, channel) = A[channel][i][j];
}
}
Expand Down Expand Up @@ -663,7 +663,7 @@ namespace matplot {
return image_channels_t{};
}
auto [h, w] = size(A[0]);
return imresize(A, h * scale, w * scale, m);
return imresize(A, static_cast<size_t>(h * scale), static_cast<size_t>(w * scale), m);
}

void imwrite(const image_channels_t &A, const std::string &filename) {
Expand All @@ -690,9 +690,9 @@ namespace matplot {
for (size_t j = 0; j < A[i].size(); ++j) {
color_array c =
colormap_interpolation(A[i][j], 0, 255, colormap);
img[0][i][j] = round(c[1] * 255);
img[1][i][j] = round(c[2] * 255);
img[2][i][j] = round(c[3] * 255);
img[0][i][j] = static_cast<unsigned char>(round(c[1] * 255));
img[1][i][j] = static_cast<unsigned char>(round(c[2] * 255));
img[2][i][j] = static_cast<unsigned char>(round(c[3] * 255));
}
}
return img;
Expand Down

0 comments on commit 1993634

Please sign in to comment.