Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[util] move bounds to geometry::util #1254

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions include/boost/geometry/algorithms/detail/assign_values.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

#include <boost/concept/requires.hpp>
#include <boost/concept_check.hpp>
#include <boost/numeric/conversion/bounds.hpp>

#include <boost/geometry/algorithms/append.hpp>
#include <boost/geometry/algorithms/clear.hpp>
Expand All @@ -38,7 +37,7 @@
#include <boost/geometry/geometries/concepts/check.hpp>

#include <boost/geometry/util/algorithm.hpp>
#include <boost/geometry/util/is_inverse_spheroidal_coordinates.hpp>
#include <boost/geometry/util/bounds.hpp>
#include <boost/geometry/util/numeric_cast.hpp>


Expand Down Expand Up @@ -74,8 +73,8 @@ struct assign_inverse_box_or_segment
{
typedef typename coordinate_type<BoxOrSegment>::type coordinate_type;

coordinate_type const highest = geometry::bounds<coordinate_type>::highest();
coordinate_type const lowest = geometry::bounds<coordinate_type>::lowest();
coordinate_type const highest = util::bounds<coordinate_type>::highest();
coordinate_type const lowest = util::bounds<coordinate_type>::lowest();
detail::for_each_dimension<BoxOrSegment>([&](auto dimension)
{
set<0, dimension>(geometry, highest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@

#include <cstddef>

#include <boost/numeric/conversion/bounds.hpp>

#include <boost/geometry/core/access.hpp>
#include <boost/geometry/core/coordinate_dimension.hpp>
#include <boost/geometry/core/coordinate_type.hpp>
#include <boost/geometry/util/bounds.hpp>


namespace boost { namespace geometry
Expand Down Expand Up @@ -67,9 +66,9 @@ struct initialize

static inline void apply(Box& box,
coordinate_type min_value
= boost::numeric::bounds<coordinate_type>::highest(),
= util::bounds<coordinate_type>::highest(),
coordinate_type max_value
= boost::numeric::bounds<coordinate_type>::lowest())
= util::bounds<coordinate_type>::lowest())
{
initialize_loop
<
Expand Down
28 changes: 28 additions & 0 deletions include/boost/geometry/util/bounds.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Boost.Geometry

// Copyright (c) 2024 Barend Gehrels, Amsterdam, the Netherlands.

// Use, modification and distribution is subject to the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#ifndef BOOST_GEOMETRY_UTIL_BOUNDS_HPP
#define BOOST_GEOMETRY_UTIL_BOUNDS_HPP

#include <boost/numeric/conversion/bounds.hpp>

namespace boost { namespace geometry { namespace util
{

// Define a boost::geometry::util::bounds
// It might be specialized for other numeric types, for example Boost.Rational
template<class CT>
struct bounds
{
static CT lowest () { return boost::numeric::bounds<CT>::lowest(); }
static CT highest () { return boost::numeric::bounds<CT>::highest(); }
};

}}} // namespace boost::geometry::util

#endif // BOOST_GEOMETRY_UTIL_BOUNDS_HPP
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,20 @@
#include <boost/geometry/core/coordinate_type.hpp>
#include <boost/geometry/core/point_type.hpp>

#include <boost/geometry/util/bounds.hpp>
#include <boost/geometry/util/math.hpp>

namespace boost { namespace geometry
{

template<class CT>
struct bounds
{
static CT lowest () { return boost::numeric::bounds<CT>::lowest(); }
static CT highest () { return boost::numeric::bounds<CT>::highest(); }
};

template <typename Box>
bool is_inverse_spheroidal_coordinates(Box const& box)
{
typedef typename point_type<Box>::type point_type;
typedef typename coordinate_type<point_type>::type bound_type;

bound_type high = bounds<bound_type>::highest();
bound_type low = bounds<bound_type>::lowest();
bound_type const high = util::bounds<bound_type>::highest();
bound_type const low = util::bounds<bound_type>::lowest();

return (geometry::get<0, 0>(box) == high) &&
(geometry::get<0, 1>(box) == high) &&
Expand Down
13 changes: 1 addition & 12 deletions include/boost/geometry/util/numeric_cast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#define BOOST_GEOMETRY_UTIL_NUMERIC_CAST_HPP

#include <boost/numeric/conversion/cast.hpp>
#include <boost/rational.hpp>

namespace boost { namespace geometry { namespace util

Expand All @@ -30,21 +29,11 @@ struct numeric_caster
}
};

// Specialization for Boost.Rational
template <typename Target, typename T>
struct numeric_caster<Target, rational<T>>
{
static inline Target apply(rational<T> const& source)
{
return boost::rational_cast<Target>(source);
}
};
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was placed here in last commit, but it is more consistent with other approach if it is all in rational.hpp (it was there before as well)


} // namespace detail
#endif

// Calls either boost::numeric_cast, or functionality specific for Boost.Geometry
// such as rational_cast for Boost.Rational
// (such as rational_cast for Boost.Rational)
template <typename Target, typename Source>
inline Target numeric_cast(Source const& source)
{
Expand Down
32 changes: 26 additions & 6 deletions include/boost/geometry/util/rational.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
#ifndef BOOST_GEOMETRY_UTIL_RATIONAL_HPP
#define BOOST_GEOMETRY_UTIL_RATIONAL_HPP

// Contains specializations for Boost.Rational

#include <boost/rational.hpp>
#include <boost/numeric/conversion/bounds.hpp>

#include <boost/geometry/util/bounds.hpp>
#include <boost/geometry/util/coordinate_cast.hpp>
#include <boost/geometry/util/numeric_cast.hpp>
#include <boost/geometry/util/select_most_precise.hpp>
Expand Down Expand Up @@ -111,14 +113,30 @@ struct select_most_precise<boost::rational<T>, double>
typedef typename boost::rational<T> type;
};

namespace util
{

}} // namespace boost::geometry

#ifndef DOXYGEN_NO_DETAIL
namespace detail
{

// Specializes boost::rational to boost::numeric::bounds
namespace boost { namespace numeric
// Specialize numeric_caster, needed for geomery::util::numeric_cast, for Boost.Rational
// Without it, code using Boost.Rational does not compile
template <typename Target, typename T>
struct numeric_caster<Target, rational<T>>
{
static inline Target apply(rational<T> const& source)
{
return boost::rational_cast<Target>(source);
}
};

} // namespace detail
#endif


// Specializes geometry::util::bounds for Boost.Rational
// Without it, bounds contains (0,1) by default for Boost.Rational
template<class T>
struct bounds<rational<T> >
{
Expand All @@ -132,7 +150,9 @@ struct bounds<rational<T> >
}
};

}} // namespace boost::numeric
} // namespace util

}} // namespace boost::geometry


#endif // BOOST_GEOMETRY_UTIL_RATIONAL_HPP
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ if (NOT TARGET tests)
endif()

add_subdirectory(algorithms)
add_subdirectory(util)
1 change: 1 addition & 0 deletions test/algorithms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ add_subdirectory(area)
add_subdirectory(buffer)
add_subdirectory(convex_hull)
add_subdirectory(detail)
add_subdirectory(envelope_expand)
add_subdirectory(overlay)
add_subdirectory(relate)
add_subdirectory(set_operations)
Expand Down
23 changes: 23 additions & 0 deletions test/algorithms/envelope_expand/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Boost.Geometry
# Copyright (c) 2024 Barend Gehrels, Amsterdam, the Netherlands.
# Use, modification and distribution is subject to the Boost Software License,
# Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)

foreach(item IN ITEMS
envelope
envelope_multi
expand
expand_on_spheroid
)
boost_geometry_add_unit_test("algorithms" ${item})
endforeach()

if (NOT APPLE)
# The results of these tests vary considerably on Apple/Darwin/arm64 using clang
foreach(item IN ITEMS
envelope_on_spheroid
)
boost_geometry_add_unit_test("algorithms" ${item})
endforeach()
endif()
7 changes: 3 additions & 4 deletions test/algorithms/envelope_expand/envelope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include <boost/numeric/conversion/bounds.hpp>

#include "test_envelope.hpp"

#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/adapted/c_array.hpp>
#include <boost/geometry/geometries/adapted/boost_tuple.hpp>
#include <boost/geometry/geometries/adapted/std_pair_as_segment.hpp>
#include <boost/geometry/util/bounds.hpp>
#include <test_common/test_point.hpp>

BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian)
Expand Down Expand Up @@ -68,8 +67,8 @@ template <typename Geometry>
void test_empty_geometry(std::string const& wkt)
{
typedef typename bg::coordinate_type<Geometry>::type ct;
ct high_val = boost::numeric::bounds<ct>::highest();
ct low_val = boost::numeric::bounds<ct>::lowest();
ct const high_val = bg::util::bounds<ct>::highest();
ct const low_val = bg::util::bounds<ct>::lowest();

test_envelope<Geometry>(wkt, high_val, low_val, high_val, low_val);
}
Expand Down
7 changes: 3 additions & 4 deletions test/algorithms/envelope_expand/envelope_on_spheroid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
#include <iostream>
#include <string>

#include <boost/numeric/conversion/bounds.hpp>

#include <from_wkt.hpp>
#include <geometry_test_common.hpp>
#include "test_envelope_expand_on_spheroid.hpp"
Expand All @@ -38,6 +36,7 @@
#include <boost/geometry/index/detail/algorithms/is_valid.hpp>
#include <boost/geometry/io/dsv/write.hpp>
#include <boost/geometry/io/wkt/wkt.hpp>
#include <boost/geometry/util/bounds.hpp>
#include <boost/geometry/util/condition.hpp>
#include <boost/geometry/util/type_traits.hpp>

Expand Down Expand Up @@ -492,8 +491,8 @@ void test_empty_geometry(std::string const& case_id, std::string const& wkt)
typedef test_envelope_on_sphere_or_spheroid<Geometry, B> tester;

typedef typename bg::coordinate_type<Geometry>::type ct;
ct high_val = boost::numeric::bounds<ct>::highest();
ct low_val = boost::numeric::bounds<ct>::lowest();
ct const high_val = bg::util::bounds<ct>::highest();
ct const low_val = bg::util::bounds<ct>::lowest();

if (BOOST_GEOMETRY_CONDITION(dim == 2))
{
Expand Down
12 changes: 6 additions & 6 deletions test/geometries/box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ void test_construction()
check_box(b2, 1,2,5,3,4,6);

bg::model::box<P> b3 = bg::make_inverse<bg::model::box<P> >();
check_box(b3, boost::numeric::bounds<T>::highest(),
boost::numeric::bounds<T>::highest(),
boost::numeric::bounds<T>::highest(),
boost::numeric::bounds<T>::lowest(),
boost::numeric::bounds<T>::lowest(),
boost::numeric::bounds<T>::lowest());
check_box(b3, bg::util::bounds<T>::highest(),
bg::util::bounds<T>::highest(),
bg::util::bounds<T>::highest(),
bg::util::bounds<T>::lowest(),
bg::util::bounds<T>::lowest(),
bg::util::bounds<T>::lowest());
}

template <typename P>
Expand Down
24 changes: 24 additions & 0 deletions test/util/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Boost.Geometry
# Copyright (c) 2024 Barend Gehrels, Amsterdam, the Netherlands.
# Use, modification and distribution is subject to the Boost Software License,
# Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)

foreach(item IN ITEMS
algorithm
calculation_type
for_each_coordinate
math_abs
math_divide
math_equals
math_sqrt
math_normalize_spheroidal
promote_integral
range
rational
select_most_precise
tuples
write_dsv
)
boost_geometry_add_unit_test("util" ${item})
endforeach()
Loading
Loading