123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- #ifndef BOOST_GEOMETRY_STRATEGIES_CARTESIAN_CENTROID_BASHEIN_DETMER_HPP
- #define BOOST_GEOMETRY_STRATEGIES_CARTESIAN_CENTROID_BASHEIN_DETMER_HPP
- #include <cstddef>
- #include <boost/math/special_functions/fpclassify.hpp>
- #include <boost/numeric/conversion/cast.hpp>
- #include <boost/geometry/arithmetic/determinant.hpp>
- #include <boost/geometry/core/coordinate_type.hpp>
- #include <boost/geometry/core/point_type.hpp>
- #include <boost/geometry/strategies/centroid.hpp>
- #include <boost/geometry/util/math.hpp>
- #include <boost/geometry/util/select_most_precise.hpp>
- namespace boost { namespace geometry
- {
- namespace strategy { namespace centroid
- {
- template
- <
- typename Ignored1 = void,
- typename Ignored2 = void,
- typename CalculationType = void
- >
- class bashein_detmer
- {
- private :
-
-
-
-
- template <typename GeometryPoint, typename ResultPoint>
- struct calculation_type
- : std::conditional
- <
- std::is_void<CalculationType>::value,
- typename select_most_precise
- <
- typename coordinate_type<GeometryPoint>::type,
- typename coordinate_type<ResultPoint>::type,
- double
- >::type,
- CalculationType
- >
- {};
-
- template <typename GeometryPoint, typename ResultPoint>
- class sums
- {
- typedef typename calculation_type<GeometryPoint, ResultPoint>::type calc_type;
- friend class bashein_detmer;
- std::size_t count;
- calc_type sum_a2;
- calc_type sum_x;
- calc_type sum_y;
- public :
- inline sums()
- : count(0)
- , sum_a2(calc_type())
- , sum_x(calc_type())
- , sum_y(calc_type())
- {}
- };
- public :
- template <typename GeometryPoint, typename ResultPoint>
- struct state_type
- {
- typedef sums<GeometryPoint, ResultPoint> type;
- };
- template <typename GeometryPoint, typename ResultPoint>
- static inline void apply(GeometryPoint const& p1, GeometryPoint const& p2,
- sums<GeometryPoint, ResultPoint>& state)
- {
-
- typedef typename calculation_type<GeometryPoint, ResultPoint>::type calc_type;
-
- calc_type const x1 = boost::numeric_cast<calc_type>(get<0>(p1));
- calc_type const y1 = boost::numeric_cast<calc_type>(get<1>(p1));
- calc_type const x2 = boost::numeric_cast<calc_type>(get<0>(p2));
- calc_type const y2 = boost::numeric_cast<calc_type>(get<1>(p2));
- calc_type const ai = geometry::detail::determinant<calc_type>(p1, p2);
- state.count++;
- state.sum_a2 += ai;
- state.sum_x += ai * (x1 + x2);
- state.sum_y += ai * (y1 + y2);
- }
- template <typename GeometryPoint, typename ResultPoint>
- static inline bool result(sums<GeometryPoint, ResultPoint> const& state,
- ResultPoint& centroid)
- {
- typedef typename calculation_type<GeometryPoint, ResultPoint>::type calc_type;
- calc_type const zero = calc_type();
- if (state.count > 0 && ! math::equals(state.sum_a2, zero))
- {
- calc_type const v3 = 3;
- calc_type const a3 = v3 * state.sum_a2;
- typedef typename geometry::coordinate_type
- <
- ResultPoint
- >::type coordinate_type;
-
- if (boost::math::isfinite(a3))
- {
-
-
-
- set<0>(centroid,
- boost::numeric_cast<coordinate_type>(state.sum_x / a3));
- set<1>(centroid,
- boost::numeric_cast<coordinate_type>(state.sum_y / a3));
- return true;
- }
- }
- return false;
- }
- };
- #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
- namespace services
- {
- template <typename Point, typename Geometry>
- struct default_strategy<cartesian_tag, areal_tag, 2, Point, Geometry>
- {
- typedef bashein_detmer
- <
- Point,
- typename point_type<Geometry>::type
- > type;
- };
- }
- #endif
- }}
- }}
- #endif
|