Merge pull request #2839 from mapnik/static_strategy

Changed from numeric casts to static casts in strategies
This commit is contained in:
Blake Thompson 2015-05-26 16:19:13 -05:00
commit 847a9f0d45
4 changed files with 11 additions and 143 deletions

View file

@ -155,28 +155,8 @@ struct scale_strategy
using p2_type = typename boost::geometry::coordinate_type<P2>::type; using p2_type = typename boost::geometry::coordinate_type<P2>::type;
double x = (boost::geometry::get<0>(p1) * scale_) + offset_; double x = (boost::geometry::get<0>(p1) * scale_) + offset_;
double y = (boost::geometry::get<1>(p1) * scale_) + offset_; double y = (boost::geometry::get<1>(p1) * scale_) + offset_;
try { boost::geometry::set<0>(p2, static_cast<p2_type>(x));
boost::geometry::set<0>(p2, boost::numeric_cast<p2_type>(x)); boost::geometry::set<1>(p2, static_cast<p2_type>(y));
}
catch(boost::numeric::negative_overflow&)
{
boost::geometry::set<0>(p2, std::numeric_limits<p2_type>::min());
}
catch(boost::numeric::positive_overflow&)
{
boost::geometry::set<0>(p2, std::numeric_limits<p2_type>::max());
}
try {
boost::geometry::set<1>(p2, boost::numeric_cast<p2_type>(y));
}
catch(boost::numeric::negative_overflow&)
{
boost::geometry::set<1>(p2, std::numeric_limits<p2_type>::min());
}
catch(boost::numeric::positive_overflow&)
{
boost::geometry::set<1>(p2, std::numeric_limits<p2_type>::max());
}
return true; return true;
} }
@ -205,28 +185,8 @@ struct scale_rounding_strategy
using p2_type = typename boost::geometry::coordinate_type<P2>::type; using p2_type = typename boost::geometry::coordinate_type<P2>::type;
double x = (boost::geometry::get<0>(p1) * scale_) + offset_; double x = (boost::geometry::get<0>(p1) * scale_) + offset_;
double y = (boost::geometry::get<1>(p1) * scale_) + offset_; double y = (boost::geometry::get<1>(p1) * scale_) + offset_;
try { boost::geometry::set<0>(p2, static_cast<p2_type>(std::round(x)));
boost::geometry::set<0>(p2, util::rounding_cast<p2_type>(x)); boost::geometry::set<1>(p2, static_cast<p2_type>(std::round(y)));
}
catch(boost::numeric::negative_overflow&)
{
boost::geometry::set<0>(p2, std::numeric_limits<p2_type>::min());
}
catch(boost::numeric::positive_overflow&)
{
boost::geometry::set<0>(p2, std::numeric_limits<p2_type>::max());
}
try {
boost::geometry::set<1>(p2, util::rounding_cast<p2_type>(y));
}
catch(boost::numeric::negative_overflow&)
{
boost::geometry::set<1>(p2, std::numeric_limits<p2_type>::min());
}
catch(boost::numeric::positive_overflow&)
{
boost::geometry::set<1>(p2, std::numeric_limits<p2_type>::max());
}
return true; return true;
} }

View file

@ -59,28 +59,8 @@ struct proj_strategy
double y = boost::geometry::get<1>(p1); double y = boost::geometry::get<1>(p1);
double z = 0.0; double z = 0.0;
if (!prj_trans_.forward(x, y, z)) return false; if (!prj_trans_.forward(x, y, z)) return false;
try { boost::geometry::set<0>(p2, static_cast<p2_type>(x));
boost::geometry::set<0>(p2, boost::numeric_cast<p2_type>(x)); boost::geometry::set<1>(p2, static_cast<p2_type>(y));
}
catch(boost::numeric::negative_overflow&)
{
boost::geometry::set<0>(p2, std::numeric_limits<p2_type>::min());
}
catch(boost::numeric::positive_overflow&)
{
boost::geometry::set<0>(p2, std::numeric_limits<p2_type>::max());
}
try {
boost::geometry::set<1>(p2, boost::numeric_cast<p2_type>(y));
}
catch(boost::numeric::negative_overflow&)
{
boost::geometry::set<1>(p2, std::numeric_limits<p2_type>::min());
}
catch(boost::numeric::positive_overflow&)
{
boost::geometry::set<1>(p2, std::numeric_limits<p2_type>::max());
}
return true; return true;
} }
@ -108,28 +88,8 @@ struct proj_backward_strategy
double y = boost::geometry::get<1>(p1); double y = boost::geometry::get<1>(p1);
double z = 0.0; double z = 0.0;
if (!prj_trans_.backward(x, y, z)) return false; if (!prj_trans_.backward(x, y, z)) return false;
try { boost::geometry::set<0>(p2, static_cast<p2_type>(x));
boost::geometry::set<0>(p2, boost::numeric_cast<p2_type>(x)); boost::geometry::set<1>(p2, static_cast<p2_type>(y));
}
catch(boost::numeric::negative_overflow&)
{
boost::geometry::set<0>(p2, std::numeric_limits<p2_type>::min());
}
catch(boost::numeric::positive_overflow&)
{
boost::geometry::set<0>(p2, std::numeric_limits<p2_type>::max());
}
try {
boost::geometry::set<1>(p2, boost::numeric_cast<p2_type>(y));
}
catch(boost::numeric::negative_overflow&)
{
boost::geometry::set<1>(p2, std::numeric_limits<p2_type>::min());
}
catch(boost::numeric::positive_overflow&)
{
boost::geometry::set<1>(p2, std::numeric_limits<p2_type>::max());
}
return true; return true;
} }

View file

@ -74,8 +74,8 @@ struct unview_strategy
double x = boost::geometry::get<0>(p1); double x = boost::geometry::get<0>(p1);
double y = boost::geometry::get<1>(p1); double y = boost::geometry::get<1>(p1);
tr_.backward(&x,&y); tr_.backward(&x,&y);
boost::geometry::set<0>(p2, boost::numeric_cast<coordinate_type>(x)); boost::geometry::set<0>(p2, static_cast<coordinate_type>(x));
boost::geometry::set<1>(p2, boost::numeric_cast<coordinate_type>(y)); boost::geometry::set<1>(p2, static_cast<coordinate_type>(y));
return true; return true;
} }

View file

@ -29,7 +29,6 @@ SECTION("proj and view strategy") {
point<double> p1(-97.553098,35.523105); point<double> p1(-97.553098,35.523105);
point<double> r1(-1.08596e+07, 4.2352e+06); point<double> r1(-1.08596e+07, 4.2352e+06);
point<double> p3 = transform<double>(p1, ps); point<double> p3 = transform<double>(p1, ps);
//std::cout << p3.x << " , " << p3.y << std::endl;
assert_g_equal(r1, p3); assert_g_equal(r1, p3);
} }
{ {
@ -37,7 +36,6 @@ SECTION("proj and view strategy") {
point<double> p1(-1.08596e+07, 4.2352e+06); point<double> p1(-1.08596e+07, 4.2352e+06);
point<double> r1(58.6287 , 100.945); point<double> r1(58.6287 , 100.945);
point<double> p3 = transform<double>(p1, vs); point<double> p3 = transform<double>(p1, vs);
//std::cout << p3.x << " , " << p3.y << std::endl;
assert_g_equal(r1, p3); assert_g_equal(r1, p3);
} }
@ -48,7 +46,6 @@ SECTION("proj and view strategy") {
using sg_type = strategy_group<mapnik::view_strategy>; using sg_type = strategy_group<mapnik::view_strategy>;
sg_type sg(vs); sg_type sg(vs);
point<double> p3 = transform<double>(p1, sg); point<double> p3 = transform<double>(p1, sg);
//std::cout << p3.x << " , " << p3.y << std::endl;
assert_g_equal(r1, p3); assert_g_equal(r1, p3);
} }
@ -59,7 +56,6 @@ SECTION("proj and view strategy") {
point<double> p1(-97.553098,35.523105); point<double> p1(-97.553098,35.523105);
point<double> r1(58.6287 , 100.945); point<double> r1(58.6287 , 100.945);
point<double> p3 = transform<double>(p1, sg); point<double> p3 = transform<double>(p1, sg);
//std::cout << p3.x << " , " << p3.y << std::endl;
assert_g_equal(r1, p3); assert_g_equal(r1, p3);
} }
{ {
@ -71,7 +67,6 @@ SECTION("proj and view strategy") {
geometry<double> p2 = transform<double>(p1, sg); geometry<double> p2 = transform<double>(p1, sg);
REQUIRE(p2.is<point<double> >()); REQUIRE(p2.is<point<double> >());
point<double> p3 = mapnik::util::get<point<double> >(p2); point<double> p3 = mapnik::util::get<point<double> >(p2);
//std::cout << p3.x << " , " << p3.y << std::endl;
assert_g_equal(r1, p3); assert_g_equal(r1, p3);
} }
{ {
@ -83,7 +78,6 @@ SECTION("proj and view strategy") {
geometry<std::int64_t> p2 = transform<std::int64_t>(p1, sg); geometry<std::int64_t> p2 = transform<std::int64_t>(p1, sg);
REQUIRE(p2.is<point<std::int64_t> >()); REQUIRE(p2.is<point<std::int64_t> >());
point<std::int64_t> p3 = mapnik::util::get<point<std::int64_t> >(p2); point<std::int64_t> p3 = mapnik::util::get<point<std::int64_t> >(p2);
//std::cout << p3.x << " , " << p3.y << std::endl;
assert_g_equal(r1, p3); assert_g_equal(r1, p3);
} }
{ {
@ -96,7 +90,6 @@ SECTION("proj and view strategy") {
geometry<std::int64_t> p2 = transform<std::int64_t>(p1, sg); geometry<std::int64_t> p2 = transform<std::int64_t>(p1, sg);
REQUIRE(p2.is<point<std::int64_t> >()); REQUIRE(p2.is<point<std::int64_t> >());
point<std::int64_t> p3 = mapnik::util::get<point<std::int64_t> >(p2); point<std::int64_t> p3 = mapnik::util::get<point<std::int64_t> >(p2);
//std::cout << p3.x << " , " << p3.y << std::endl;
assert_g_equal(r1, p3); assert_g_equal(r1, p3);
} }
{ {
@ -109,7 +102,6 @@ SECTION("proj and view strategy") {
geometry<double> p2 = transform<double>(p1, sg); geometry<double> p2 = transform<double>(p1, sg);
REQUIRE(p2.is<point<double> >()); REQUIRE(p2.is<point<double> >());
point<double> p3 = mapnik::util::get<point<double> >(p2); point<double> p3 = mapnik::util::get<point<double> >(p2);
//std::cout << p3.x << " , " << p3.y << std::endl;
assert_g_equal(r1, p3); assert_g_equal(r1, p3);
} }
{ {
@ -122,7 +114,6 @@ SECTION("proj and view strategy") {
geometry<std::int64_t> p2 = transform<std::int64_t>(p1, sg); geometry<std::int64_t> p2 = transform<std::int64_t>(p1, sg);
REQUIRE(p2.is<point<std::int64_t> >()); REQUIRE(p2.is<point<std::int64_t> >());
point<std::int64_t> p3 = mapnik::util::get<point<std::int64_t> >(p2); point<std::int64_t> p3 = mapnik::util::get<point<std::int64_t> >(p2);
//std::cout << p3.x << " , " << p3.y << std::endl;
assert_g_equal(r1, p3); assert_g_equal(r1, p3);
} }
{ {
@ -135,7 +126,6 @@ SECTION("proj and view strategy") {
geometry<double> p2 = transform<double>(p1, sg); geometry<double> p2 = transform<double>(p1, sg);
REQUIRE(p2.is<point<double> >()); REQUIRE(p2.is<point<double> >());
point<double> p3 = mapnik::util::get<point<double> >(p2); point<double> p3 = mapnik::util::get<point<double> >(p2);
//std::cout << p3.x << " , " << p3.y << std::endl;
assert_g_equal(r1, p3); assert_g_equal(r1, p3);
} }
@ -159,10 +149,9 @@ SECTION("scaling strategies - double to double") {
assert_g_equal(r, o); assert_g_equal(r, o);
} }
{ {
// Not the rounding doesn't apply because not casting to ints
scale_rounding_strategy ss(0.5, -2.0); scale_rounding_strategy ss(0.5, -2.0);
point<double> p(-90.3, 35.5); point<double> p(-90.3, 35.5);
point<double> r(-47.15, 15.75); point<double> r(-47.0, 16.0);
point<double> o = transform<double>(p, ss); point<double> o = transform<double>(p, ss);
assert_g_equal(r, o); assert_g_equal(r, o);
} }
@ -193,47 +182,6 @@ SECTION("scaling strategies - double to int64") {
point<std::int64_t> o = transform<std::int64_t>(p, ss); point<std::int64_t> o = transform<std::int64_t>(p, ss);
assert_g_equal(r, o); assert_g_equal(r, o);
} }
{
// Test underflow and overflow
std::int64_t min = std::numeric_limits<std::int64_t>::min();
std::int64_t max = std::numeric_limits<std::int64_t>::max();
scale_strategy ss(1.0E100);
point<double> p(-90.3, 35.5);
point<std::int64_t> r(min, max);
point<std::int64_t> o = transform<std::int64_t>(p, ss);
assert_g_equal(r, o);
}
{
// Test underflow and overflow
std::int64_t min = std::numeric_limits<std::int64_t>::min();
std::int64_t max = std::numeric_limits<std::int64_t>::max();
scale_rounding_strategy ss(1.0E100);
point<double> p(-90.3, 35.5);
point<std::int64_t> r(min, max);
point<std::int64_t> o = transform<std::int64_t>(p, ss);
assert_g_equal(r, o);
}
{
// Test overrflow and underflow
std::int64_t min = std::numeric_limits<std::int64_t>::min();
std::int64_t max = std::numeric_limits<std::int64_t>::max();
scale_strategy ss(1.0E100);
point<double> p(90.3, -35.5);
point<std::int64_t> r(max, min);
point<std::int64_t> o = transform<std::int64_t>(p, ss);
assert_g_equal(r, o);
}
{
// Test overflow and underflow
std::int64_t min = std::numeric_limits<std::int64_t>::min();
std::int64_t max = std::numeric_limits<std::int64_t>::max();
scale_rounding_strategy ss(1.0E100);
point<double> p(90.3, -35.5);
point<std::int64_t> r(max, min);
point<std::int64_t> o = transform<std::int64_t>(p, ss);
assert_g_equal(r, o);
}
} // END SECTION } // END SECTION
} // END TEST CASE } // END TEST CASE