|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r74147 - trunk/boost/geometry/util
From: barend.gehrels_at_[hidden]
Date: 2011-08-30 06:46:35
Author: barendgehrels
Date: 2011-08-30 06:46:34 EDT (Tue, 30 Aug 2011)
New Revision: 74147
URL: http://svn.boost.org/trac/boost/changeset/74147
Log:
Added basic support for Boost.Rational
Added:
trunk/boost/geometry/util/rational.hpp (contents, props changed)
Added: trunk/boost/geometry/util/rational.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/geometry/util/rational.hpp 2011-08-30 06:46:34 EDT (Tue, 30 Aug 2011)
@@ -0,0 +1,149 @@
+// Boost.Geometry (aka GGL, Generic Geometry Library)
+
+// Copyright (c) 2011-2011 Barend Gehrels, Amsterdam, the Netherlands.
+// Copyright (c) 2011-2011 Bruno Lalande, Paris, France.
+// Copyright (c) 2011-2011 Mateusz Loskot, London, UK.
+
+// Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
+// (geolib/GGL), copyright (c) 1995-2010 Geodan, 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_RATIONAL_HPP
+#define BOOST_GEOMETRY_UTIL_RATIONAL_HPP
+
+#include <boost/rational.hpp>
+#include <boost/numeric/conversion/bounds.hpp>
+
+#include <boost/geometry/util/coordinate_cast.hpp>
+#include <boost/geometry/util/select_most_precise.hpp>
+
+
+namespace boost{ namespace geometry
+{
+
+
+// Specialize for Boost.Geometry's coordinate cast
+// (from string to coordinate type)
+namespace detail
+{
+
+template <typename T>
+struct coordinate_cast<rational<T> >
+{
+ static inline rational<T> apply(std::string const& source)
+ {
+ // Note: decimal comma is not (yet) supported, it does (and should) not
+ // occur in a WKT, where points are comma separated.
+ std::string::size_type const p = source.find(".");
+ if (p == std::string::npos)
+ {
+ return rational<T>(atol(source.c_str()));
+ }
+
+ std::string const natural_part = source.substr(0, p);
+ std::string const fraction = source.substr(p + 1);
+
+ T const nat = atol(natural_part.c_str());
+ T const nom = atol(fraction.c_str());
+ T den = 1;
+ for (std::string::size_type i = 0; i < fraction.length(); i++)
+ {
+ den *= 10;
+ }
+
+ return rational<T>(nat) + rational<T>(nom, den);
+ }
+};
+
+} // namespace detail
+
+// Specialize for Boost.Geometry's select_most_precise
+template <typename T1, typename T2>
+struct select_most_precise<boost::rational<T1>, boost::rational<T2> >
+{
+ typedef typename boost::rational
+ <
+ typename select_most_precise<T1, T2>::type
+ > type;
+};
+
+template <typename T>
+struct select_most_precise<boost::rational<T>, double>
+{
+ typedef typename boost::rational<T> type;
+};
+
+
+}} // namespace boost::geometry
+
+
+// Specializes boost::rational to boost::numeric::bounds
+namespace boost { namespace numeric
+{
+
+template<class T>
+struct bounds<rational<T> >
+{
+ static inline rational<T> lowest()
+ {
+ return rational<T>(bounds<T>::lowest(), 1);
+ }
+ static inline rational<T> highest()
+ {
+ return rational<T>(bounds<T>::highest(), 1);
+ }
+};
+
+}} // namespace boost::numeric
+
+
+// Support for boost::numeric_cast to int and to double (necessary for SVG-mapper)
+namespace boost { namespace numeric
+{
+
+template
+<
+ typename T,
+ typename Traits,
+ typename OverflowHandler,
+ typename Float2IntRounder,
+ typename RawConverter,
+ typename UserRangeChecker
+>
+struct converter<int, rational<T>, Traits, OverflowHandler, Float2IntRounder, RawConverter, UserRangeChecker>
+{
+ static inline int convert(rational<T> const& arg)
+ {
+ double v = arg.numerator();
+ v /= arg.denominator();
+ return int(v);
+ }
+};
+
+template
+<
+ typename T,
+ typename Traits,
+ typename OverflowHandler,
+ typename Float2IntRounder,
+ typename RawConverter,
+ typename UserRangeChecker
+>
+struct converter<double, rational<T>, Traits, OverflowHandler, Float2IntRounder, RawConverter, UserRangeChecker>
+{
+ static inline double convert(rational<T> const& arg)
+ {
+ double v = arg.numerator();
+ v /= arg.denominator();
+ return v;
+ }
+};
+
+
+}}
+
+
+#endif // BOOST_GEOMETRY_UTIL_RATIONAL_HPP
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk