Skip to content

Commit

Permalink
[msvc] Fix warnings, mainly casting to smaller types (maplibre#270)
Browse files Browse the repository at this point in the history
If the checks by GitHub Actions checks are met, then it seems good to go.  Thanks for supporting FOSS4G!
  • Loading branch information
ntadej authored and keith committed Jul 22, 2022
1 parent 2cdbc6f commit fb2b296
Show file tree
Hide file tree
Showing 121 changed files with 645 additions and 459 deletions.
2 changes: 1 addition & 1 deletion expression-test/expression_test_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ Value toValue(const Compiled& compiled) {
optional<Value> toValue(const expression::Value& exprValue) {
return exprValue.match(
[](const Color& c) -> optional<Value> {
std::vector<Value> color { double(c.r), double(c.g), double(c.b), double(c.a) };
std::vector<Value> color { static_cast<double>(c.r), static_cast<double>(c.g), static_cast<double>(c.b), static_cast<double>(c.a) };
return {Value{std::move(color)}};
},
[](const expression::Formatted& formatted) -> optional<Value> { return {formatted.toObject()}; },
Expand Down
11 changes: 11 additions & 0 deletions include/mbgl/style/expression/distance.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
#pragma once

#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4244)
#pragma warning(disable: 4267)
#endif

#include <mapbox/cheap_ruler.hpp>

#ifdef _MSC_VER
#pragma warning(pop)
#endif

#include <mbgl/style/expression/expression.hpp>
#include <mbgl/util/geojson.hpp>

Expand Down
4 changes: 2 additions & 2 deletions include/mbgl/style/position.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class Position {
void calculateCartesian() {
// We abstract "north"/"up" (compass-wise) to be 0° when really this is 90° (π/2): we
// correct for that here
const float _a = (azimuthal + 90) * util::DEG2RAD;
const float _p = polar * util::DEG2RAD;
const float _a = (azimuthal + 90) * util::DEG2RAD_F;
const float _p = polar * util::DEG2RAD_F;

x = radial * std::cos(_a) * std::sin(_p);
y = radial * std::sin(_a) * std::sin(_p);
Expand Down
2 changes: 1 addition & 1 deletion include/mbgl/style/sources/custom_geometry_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class CustomGeometrySource final : public Source {
public:
struct TileOptions {
double tolerance = 0.375;
uint16_t tileSize = util::tileSize;
uint16_t tileSize = util::tileSize_I;
uint16_t buffer = 128;
bool clip = false;
bool wrap = false;
Expand Down
2 changes: 1 addition & 1 deletion include/mbgl/style/sources/geojson_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct GeoJSONOptions {
// GeoJSON-VT options
uint8_t minzoom = 0;
uint8_t maxzoom = 18;
uint16_t tileSize = util::tileSize;
uint16_t tileSize = util::tileSize_I;
uint16_t buffer = 128;
double tolerance = 0.375;
bool lineMetrics = false;
Expand Down
2 changes: 1 addition & 1 deletion include/mbgl/tile/tile_id.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ inline OverscaledTileID UnwrappedTileID::overscaleTo(const uint8_t overscaledZ)
}

inline float UnwrappedTileID::pixelsToTileUnits(const float pixelValue, const float zoom) const {
return pixelValue * (static_cast<float>(util::EXTENT) / (static_cast<float>(util::tileSize) * std::pow(2.f, zoom - canonical.z)));
return pixelValue * (static_cast<float>(util::EXTENT) / (static_cast<float>(util::tileSize_D) * std::pow(2.f, zoom - canonical.z)));
}

} // namespace mbgl
Expand Down
9 changes: 6 additions & 3 deletions include/mbgl/util/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ namespace mbgl {

namespace util {

constexpr double tileSize = 512;
constexpr double tileSize_D = 512;
constexpr uint16_t tileSize_I = 512;

/*
* The maximum extent of a feature that can be safely stored in the buffer.
Expand All @@ -26,8 +27,10 @@ constexpr double tileSize = 512;
*/
constexpr int32_t EXTENT = 8192;

constexpr double DEG2RAD = M_PI / 180.0;
constexpr double RAD2DEG = 180.0 / M_PI;
constexpr double DEG2RAD_D = M_PI / 180.0;
constexpr double RAD2DEG_D = 180.0 / M_PI;
constexpr float DEG2RAD_F = static_cast<float>(M_PI) / 180.0F;
constexpr float RAD2DEG_F = 180.0F / static_cast<float>(M_PI);
constexpr double M2PI = M_PI * 2;
constexpr double EARTH_RADIUS_M = 6378137;
constexpr double LATITUDE_MAX = 85.051128779806604;
Expand Down
6 changes: 3 additions & 3 deletions include/mbgl/util/feature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class Feature : public GeoJSONFeature {

template <class T>
optional<T> numericValue(const Value& value) {
return value.match([](uint64_t t) { return optional<T>(t); },
[](int64_t t) { return optional<T>(t); },
[](double t) { return optional<T>(t); },
return value.match([](uint64_t t) { return optional<T>(static_cast<T>(t)); },
[](int64_t t) { return optional<T>(static_cast<T>(t)); },
[](double t) { return optional<T>(static_cast<T>(t)); },
[](const auto&) { return optional<T>(); });
}

Expand Down
2 changes: 1 addition & 1 deletion include/mbgl/util/geometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ using Geometry = mapbox::geometry::geometry<T>;

template <class S, class T>
Point<S> convertPoint(const Point<T>& p) {
return Point<S>(p.x, p.y);
return Point<S>(static_cast<S>(p.x), static_cast<S>(p.y));
}

struct ToFeatureType {
Expand Down
42 changes: 41 additions & 1 deletion include/mbgl/util/interpolate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ T interpolate(const T& a, const T& b, const double t) {
return Interpolator<T>()(a, b, t);
}

template <typename T>
T interpolate(const T& a, const T& b, const float t) {
return Interpolator<T>()(a, b, t);
}

template <class T, class Enabled>
struct Interpolator {
Expand All @@ -33,6 +37,17 @@ struct Interpolator {
}
};

template <>
struct Interpolator<float> {
float operator()(const float& a, const float& b, const float t) const {
return a * (1.0f - t) + b * t;
}

float operator()(const float& a, const float& b, const double t) const {
return static_cast<float>(a * (1.0 - t) + b * t);
}
};

template <class T, std::size_t N>
struct Interpolator<std::array<T, N>> {
private:
Expand All @@ -49,6 +64,22 @@ struct Interpolator<std::array<T, N>> {
}
};

template <std::size_t N>
struct Interpolator<std::array<float, N>> {
private:
using Array = std::array<float, N>;

template <std::size_t... I>
Array operator()(const Array& a, const Array& b, const float t, std::index_sequence<I...>) {
return {{ interpolate(a[I], b[I], t)... }};
}

public:
Array operator()(const Array& a, const Array& b, const float t) {
return operator()(a, b, t, std::make_index_sequence<N>());
}
};


// In order to accept Array<Number, N> as an output value for Curve
// expressions, we need to have an interpolatable std::vector type.
Expand Down Expand Up @@ -82,7 +113,7 @@ struct Interpolator<std::vector<style::expression::Value>> {
template <>
struct Interpolator<style::Position> {
public:
style::Position operator()(const style::Position& a, const style::Position& b, const double t) {
style::Position operator()(const style::Position& a, const style::Position& b, const float t) {
auto pos = style::Position();
auto interpolated = interpolate(a.getCartesian(), b.getCartesian(), t);
pos.setCartesian(interpolated);
Expand All @@ -93,6 +124,15 @@ struct Interpolator<style::Position> {
template <>
struct Interpolator<Color> {
public:
Color operator()(const Color& a, const Color& b, const float t) {
return {
interpolate(a.r, b.r, t),
interpolate(a.g, b.g, t),
interpolate(a.b, b.b, t),
interpolate(a.a, b.a, t)
};
}

Color operator()(const Color& a, const Color& b, const double t) {
return {
interpolate(a.r, b.r, t),
Expand Down
16 changes: 8 additions & 8 deletions include/mbgl/util/projection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,32 @@ class Projection {
public:
// Map pixel width at given scale.
static double worldSize(double scale) {
return scale * util::tileSize;
return scale * util::tileSize_D;
}

static double getMetersPerPixelAtLatitude(double lat, double zoom) {
const double constrainedZoom = util::clamp(zoom, util::MIN_ZOOM, util::MAX_ZOOM);
const double constrainedScale = std::pow(2.0, constrainedZoom);
const double constrainedLatitude = util::clamp(lat, -util::LATITUDE_MAX, util::LATITUDE_MAX);
return std::cos(constrainedLatitude * util::DEG2RAD) * util::M2PI * util::EARTH_RADIUS_M / worldSize(constrainedScale);
return std::cos(constrainedLatitude * util::DEG2RAD_D) * util::M2PI * util::EARTH_RADIUS_M / worldSize(constrainedScale);
}

static ProjectedMeters projectedMetersForLatLng(const LatLng& latLng) {
const double constrainedLatitude = util::clamp(latLng.latitude(), -util::LATITUDE_MAX, util::LATITUDE_MAX);
const double constrainedLongitude = util::clamp(latLng.longitude(), -util::LONGITUDE_MAX, util::LONGITUDE_MAX);

const double m = 1 - 1e-15;
const double f = util::clamp(std::sin(util::DEG2RAD * constrainedLatitude), -m, m);
const double f = util::clamp(std::sin(util::DEG2RAD_D * constrainedLatitude), -m, m);

const double easting = util::EARTH_RADIUS_M * constrainedLongitude * util::DEG2RAD;
const double easting = util::EARTH_RADIUS_M * constrainedLongitude * util::DEG2RAD_D;
const double northing = 0.5 * util::EARTH_RADIUS_M * std::log((1 + f) / (1 - f));

return {northing, easting};
}

static LatLng latLngForProjectedMeters(const ProjectedMeters& projectedMeters) {
double latitude = (2 * std::atan(std::exp(projectedMeters.northing() / util::EARTH_RADIUS_M)) - (M_PI / 2.0)) * util::RAD2DEG;
double longitude = projectedMeters.easting() * util::RAD2DEG / util::EARTH_RADIUS_M;
double latitude = (2 * std::atan(std::exp(projectedMeters.northing() / util::EARTH_RADIUS_M)) - (M_PI / 2.0)) * util::RAD2DEG_D;
double longitude = projectedMeters.easting() * util::RAD2DEG_D / util::EARTH_RADIUS_M;

latitude = util::clamp(latitude, -util::LATITUDE_MAX, util::LATITUDE_MAX);
longitude = util::clamp(longitude, -util::LONGITUDE_MAX, util::LONGITUDE_MAX);
Expand All @@ -86,7 +86,7 @@ class Projection {
static LatLng unproject(const Point<double>& p, double scale, LatLng::WrapMode wrapMode = LatLng::Unwrapped) {
auto p2 = p * util::DEGREES_MAX / worldSize(scale);
return LatLng {
util::DEGREES_MAX / M_PI * std::atan(std::exp((util::LONGITUDE_MAX - p2.y) * util::DEG2RAD)) - 90.0,
util::DEGREES_MAX / M_PI * std::atan(std::exp((util::LONGITUDE_MAX - p2.y) * util::DEG2RAD_D)) - 90.0,
p2.x - util::LONGITUDE_MAX,
wrapMode
};
Expand All @@ -97,7 +97,7 @@ class Projection {
const double latitude = util::clamp(latLng.latitude(), -util::LATITUDE_MAX, util::LATITUDE_MAX);
return Point<double> {
util::LONGITUDE_MAX + latLng.longitude(),
util::LONGITUDE_MAX - util::RAD2DEG * std::log(std::tan(M_PI / 4 + latitude * M_PI / util::DEGREES_MAX))
util::LONGITUDE_MAX - util::RAD2DEG_D * std::log(std::tan(M_PI / 4 + latitude * M_PI / util::DEGREES_MAX))
} * (worldSize / util::DEGREES_MAX);
}
};
Expand Down
9 changes: 9 additions & 0 deletions include/mbgl/util/variant.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
#pragma once

#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4244)
#endif

#include <mapbox/variant.hpp>

#ifdef _MSC_VER
#pragma warning(pop)
#endif

namespace mbgl {

template <typename... T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ void NativeMapView::addAnnotationIcon(JNIEnv& env, const jni::String& symbol, ji

jni::GetArrayRegion(env, *jpixels, 0, size, reinterpret_cast<jbyte*>(premultipliedImage.data.get()));
map->addAnnotationImage(std::make_unique<mbgl::style::Image>(
symbolName, std::move(premultipliedImage), float(scale)));
symbolName, std::move(premultipliedImage), static_cast<float>(scale)));
}

void NativeMapView::removeAnnotationIcon(JNIEnv& env, const jni::String& symbol) {
Expand Down Expand Up @@ -1097,7 +1097,7 @@ void NativeMapView::addImage(JNIEnv& env, const jni::String& name, const jni::Ob
map->getStyle().addImage(std::make_unique<mbgl::style::Image>(
jni::Make<std::string>(env, name),
std::move(premultipliedImage),
float(scale),
static_cast<float>(scale),
sdf)
);
}
Expand Down
2 changes: 1 addition & 1 deletion platform/default/src/mbgl/storage/offline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ OfflineRegionDefinition decodeOfflineRegionDefinition(const std::string& region)
std::string styleURL { doc["style_url"].GetString(), doc["style_url"].GetStringLength() };
double minZoom = doc["min_zoom"].GetDouble();
double maxZoom = doc.HasMember("max_zoom") ? doc["max_zoom"].GetDouble() : INFINITY;
float pixelRatio = doc["pixel_ratio"].GetDouble();
auto pixelRatio = static_cast<float>(doc["pixel_ratio"].GetDouble());
bool includeIdeographs = doc.HasMember("include_ideographs") ? doc["include_ideographs"].GetBool() : true;

if (doc.HasMember("bounds")) {
Expand Down
6 changes: 3 additions & 3 deletions platform/default/src/mbgl/storage/offline_download.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ uint64_t tileCount(const OfflineRegionDefinition& definition, style::SourceType
const Range<uint8_t> clampedZoomRange =
definition.match([&](auto& reg) { return coveringZoomRange(reg, type, tileSize, zoomRange); });

unsigned long result = 0;;
uint64_t result{};
for (uint8_t z = clampedZoomRange.min; z <= clampedZoomRange.max; z++) {
result += definition.match(
[&](const OfflineTilePyramidRegionDefinition& reg){ return util::tileCount(reg.bounds, z); },
Expand Down Expand Up @@ -174,7 +174,7 @@ OfflineRegionStatus OfflineDownload::getStatus() const {
switch (type) {
case SourceType::Vector: {
const auto& vectorSource = *source->as<VectorSource>();
handleTiledSource(vectorSource.getURLOrTileset(), util::tileSize);
handleTiledSource(vectorSource.getURLOrTileset(), util::tileSize_I);
break;
}

Expand Down Expand Up @@ -284,7 +284,7 @@ void OfflineDownload::activateDownload() {
switch (type) {
case SourceType::Vector: {
const auto& vectorSource = *source->as<VectorSource>();
handleTiledSource(vectorSource.getURLOrTileset(), util::tileSize);
handleTiledSource(vectorSource.getURLOrTileset(), util::tileSize_I);
break;
}

Expand Down
Loading

0 comments on commit fb2b296

Please sign in to comment.