mirror of
https://github.com/boostorg/geometry.git
synced 2025-05-09 15:14:02 +00:00
coordinate_cast of rational, bugfix and implemented parsing strings like 3/2 (how it is streamed)
[SVN r74375]
This commit is contained in:
parent
3bb7db6c75
commit
38321e2bb9
@ -33,28 +33,62 @@ namespace detail
|
||||
template <typename T>
|
||||
struct coordinate_cast<rational<T> >
|
||||
{
|
||||
static inline void split_parts(std::string const& source, std::string::size_type p,
|
||||
T& before, T& after, bool& negate, std::string::size_type& len)
|
||||
{
|
||||
std::string before_part = source.substr(0, p);
|
||||
std::string const after_part = source.substr(p + 1);
|
||||
|
||||
negate = false;
|
||||
|
||||
if (before_part.size() > 0 && before_part[0] == '-')
|
||||
{
|
||||
negate = true;
|
||||
before_part.erase(0, 1);
|
||||
}
|
||||
before = atol(before_part.c_str());
|
||||
after = atol(after_part.c_str());
|
||||
len = after_part.length();
|
||||
}
|
||||
|
||||
|
||||
static inline rational<T> apply(std::string const& source)
|
||||
{
|
||||
T before, after;
|
||||
bool negate;
|
||||
std::string::size_type len;
|
||||
|
||||
// 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(".");
|
||||
std::string::size_type p = source.find(".");
|
||||
if (p == std::string::npos)
|
||||
{
|
||||
return rational<T>(atol(source.c_str()));
|
||||
p = source.find("/");
|
||||
if (p == std::string::npos)
|
||||
{
|
||||
return rational<T>(atol(source.c_str()));
|
||||
}
|
||||
split_parts(source, p, before, after, negate, len);
|
||||
|
||||
return negate
|
||||
? -rational<T>(before, after)
|
||||
: rational<T>(before, after)
|
||||
;
|
||||
|
||||
}
|
||||
|
||||
std::string const natural_part = source.substr(0, p);
|
||||
std::string const fraction = source.substr(p + 1);
|
||||
split_parts(source, p, before, after, negate, len);
|
||||
|
||||
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++)
|
||||
for (std::string::size_type i = 0; i < len; i++)
|
||||
{
|
||||
den *= 10;
|
||||
}
|
||||
|
||||
return rational<T>(nat) + rational<T>(nom, den);
|
||||
return negate
|
||||
? -rational<T>(before) - rational<T>(after, den)
|
||||
: rational<T>(before) + rational<T>(after, den)
|
||||
;
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -11,6 +11,7 @@
|
||||
test-suite boost-geometry-util
|
||||
:
|
||||
[ run for_each_coordinate.cpp ]
|
||||
[ run rational.cpp ]
|
||||
[ run select_most_precise.cpp ]
|
||||
[ run write_dsv.cpp ]
|
||||
;
|
||||
|
61
test/util/rational.cpp
Normal file
61
test/util/rational.cpp
Normal file
@ -0,0 +1,61 @@
|
||||
// Boost.Geometry (aka GGL, Generic Geometry Library)
|
||||
// Unit Test
|
||||
|
||||
// Copyright (c) 2007-2011 Barend Gehrels, Amsterdam, the Netherlands.
|
||||
// Copyright (c) 2008-2011 Bruno Lalande, Paris, France.
|
||||
// Copyright (c) 2009-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)
|
||||
|
||||
|
||||
#include <geometry_test_common.hpp>
|
||||
|
||||
#include <boost/geometry/geometries/geometries.hpp>
|
||||
#include <boost/geometry/domains/gis/io/wkt/wkt.hpp>
|
||||
#include <boost/geometry/util/rational.hpp>
|
||||
|
||||
void test_coordinate_cast(std::string const& s, int expected_nom, int expected_denom)
|
||||
{
|
||||
boost::rational<int> a = bg::detail::coordinate_cast<boost::rational<int> >::apply(s);
|
||||
BOOST_CHECK_EQUAL(a.numerator(), expected_nom);
|
||||
BOOST_CHECK_EQUAL(a.denominator(), expected_denom);
|
||||
}
|
||||
|
||||
|
||||
void test_wkt(std::string const& wkt, std::string const expected_wkt)
|
||||
{
|
||||
bg::model::point<boost::rational<int>, 2, bg::cs::cartesian> p;
|
||||
bg::read_wkt(wkt, p);
|
||||
std::ostringstream out;
|
||||
out << bg::wkt(p);
|
||||
|
||||
BOOST_CHECK_EQUAL(out.str(), expected_wkt);
|
||||
}
|
||||
|
||||
int test_main(int, char* [])
|
||||
{
|
||||
test_coordinate_cast("0", 0, 1);
|
||||
test_coordinate_cast("1", 1, 1);
|
||||
test_coordinate_cast("-1", -1, 1);
|
||||
test_coordinate_cast("-0.5", -1, 2);
|
||||
test_coordinate_cast("-1.5", -3, 2);
|
||||
test_coordinate_cast("0.5", 1, 2);
|
||||
test_coordinate_cast("1.5", 3, 2);
|
||||
test_coordinate_cast("2.12345", 42469, 20000);
|
||||
test_coordinate_cast("1.", 1, 1);
|
||||
|
||||
test_coordinate_cast("3/2", 3, 2);
|
||||
test_coordinate_cast("-3/2", -3, 2);
|
||||
|
||||
test_wkt("POINT(1.5 2.75)", "POINT(3/2 11/4)");
|
||||
test_wkt("POINT(3/2 11/4)", "POINT(3/2 11/4)");
|
||||
test_wkt("POINT(-1.5 2.75)", "POINT(-3/2 11/4)");
|
||||
test_wkt("POINT(-3/2 11/4)", "POINT(-3/2 11/4)");
|
||||
|
||||
return 0;
|
||||
}
|
174
test/util/rational.vcproj
Normal file
174
test/util/rational.vcproj
Normal file
@ -0,0 +1,174 @@
|
||||
<?xml version="1.0" encoding="Windows-1252"?>
|
||||
<VisualStudioProject
|
||||
ProjectType="Visual C++"
|
||||
Version="8.00"
|
||||
Name="rational"
|
||||
ProjectGUID="{6ABF6324-C1DC-4687-9895-B4CE2B27446F}"
|
||||
RootNamespace="rational"
|
||||
Keyword="Win32Proj"
|
||||
>
|
||||
<Platforms>
|
||||
<Platform
|
||||
Name="Win32"
|
||||
/>
|
||||
</Platforms>
|
||||
<ToolFiles>
|
||||
</ToolFiles>
|
||||
<Configurations>
|
||||
<Configuration
|
||||
Name="Debug|Win32"
|
||||
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
|
||||
IntermediateDirectory="$(ConfigurationName)\rational"
|
||||
ConfigurationType="1"
|
||||
InheritedPropertySheets="..\boost.vsprops"
|
||||
CharacterSet="1"
|
||||
>
|
||||
<Tool
|
||||
Name="VCPreBuildEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCustomBuildTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXMLDataGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCWebServiceProxyGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCMIDLTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"
|
||||
Optimization="0"
|
||||
AdditionalIncludeDirectories="../../../..;.."
|
||||
PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
|
||||
ExceptionHandling="2"
|
||||
RuntimeLibrary="1"
|
||||
UsePrecompiledHeader="0"
|
||||
DebugInformationFormat="1"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCManagedResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPreLinkEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCLinkerTool"
|
||||
GenerateDebugInformation="true"
|
||||
SubSystem="1"
|
||||
TargetMachine="1"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCALinkTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCManifestTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXDCMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCBscMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCFxCopTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCAppVerifierTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCWebDeploymentTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPostBuildEventTool"
|
||||
/>
|
||||
</Configuration>
|
||||
<Configuration
|
||||
Name="Release|Win32"
|
||||
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
|
||||
IntermediateDirectory="$(ConfigurationName)\rational"
|
||||
ConfigurationType="1"
|
||||
InheritedPropertySheets="..\boost.vsprops"
|
||||
CharacterSet="1"
|
||||
WholeProgramOptimization="1"
|
||||
>
|
||||
<Tool
|
||||
Name="VCPreBuildEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCustomBuildTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXMLDataGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCWebServiceProxyGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCMIDLTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"
|
||||
AdditionalIncludeDirectories="../../../..;.."
|
||||
PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
|
||||
ExceptionHandling="2"
|
||||
UsePrecompiledHeader="0"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCManagedResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPreLinkEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCLinkerTool"
|
||||
SubSystem="1"
|
||||
OptimizeReferences="2"
|
||||
EnableCOMDATFolding="2"
|
||||
TargetMachine="1"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCALinkTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCManifestTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXDCMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCBscMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCFxCopTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCAppVerifierTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCWebDeploymentTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPostBuildEventTool"
|
||||
/>
|
||||
</Configuration>
|
||||
</Configurations>
|
||||
<References>
|
||||
</References>
|
||||
<Files>
|
||||
<File
|
||||
RelativePath=".\rational.cpp"
|
||||
>
|
||||
</File>
|
||||
</Files>
|
||||
<Globals>
|
||||
</Globals>
|
||||
</VisualStudioProject>
|
@ -8,6 +8,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "write_dsv", "write_dsv.vcpr
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "as_range", "as_range.vcproj", "{A36D8426-67EB-405C-B6E8-3FBB3374A59B}"
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "rational", "rational.vcproj", "{6ABF6324-C1DC-4687-9895-B4CE2B27446F}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Win32 = Debug|Win32
|
||||
@ -30,6 +32,10 @@ Global
|
||||
{A36D8426-67EB-405C-B6E8-3FBB3374A59B}.Debug|Win32.Build.0 = Debug|Win32
|
||||
{A36D8426-67EB-405C-B6E8-3FBB3374A59B}.Release|Win32.ActiveCfg = Release|Win32
|
||||
{A36D8426-67EB-405C-B6E8-3FBB3374A59B}.Release|Win32.Build.0 = Release|Win32
|
||||
{6ABF6324-C1DC-4687-9895-B4CE2B27446F}.Debug|Win32.ActiveCfg = Debug|Win32
|
||||
{6ABF6324-C1DC-4687-9895-B4CE2B27446F}.Debug|Win32.Build.0 = Debug|Win32
|
||||
{6ABF6324-C1DC-4687-9895-B4CE2B27446F}.Release|Win32.ActiveCfg = Release|Win32
|
||||
{6ABF6324-C1DC-4687-9895-B4CE2B27446F}.Release|Win32.Build.0 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
Loading…
x
Reference in New Issue
Block a user