PageRenderTime 45ms CodeModel.GetById 2ms app.highlight 39ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/libs/geometry/test/multi/algorithms/multi_distance.cpp

http://hadesmem.googlecode.com/
C++ | 150 lines | 103 code | 36 blank | 11 comment | 0 complexity | 28a7f0dab8340fb932166b6899f61862 MD5 | raw file
  1// Boost.Geometry (aka GGL, Generic Geometry Library)
  2//
  3// Copyright (c) 2007-2011 Barend Gehrels, Amsterdam, the Netherlands.
  4// Use, modification and distribution is subject to the Boost Software License,
  5// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6// http://www.boost.org/LICENSE_1_0.txt)
  7
  8#include <string>
  9
 10
 11#include <geometry_test_common.hpp>
 12
 13#include <boost/geometry/algorithms/distance.hpp>
 14#include <boost/geometry/domains/gis/io/wkt/read_wkt.hpp>
 15
 16#include <boost/geometry/strategies/strategies.hpp>
 17
 18
 19#include <boost/geometry/multi/algorithms/distance.hpp>
 20#include <boost/geometry/multi/geometries/multi_point.hpp>
 21#include <boost/geometry/multi/geometries/multi_linestring.hpp>
 22#include <boost/geometry/multi/geometries/multi_polygon.hpp>
 23#include <boost/geometry/domains/gis/io/wkt/read_wkt_multi.hpp>
 24
 25#include <boost/geometry/geometries/geometries.hpp>
 26#include <boost/geometry/geometries/point_xy.hpp>
 27#include <boost/geometry/geometries/adapted/c_array.hpp>
 28#include <boost/geometry/geometries/adapted/boost_tuple.hpp>
 29#include <test_common/test_point.hpp>
 30
 31BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian)
 32BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)
 33
 34
 35template <typename Geometry1, typename Geometry2>
 36void test_distance(std::string const& wkt1, std::string const& wkt2, double expected)
 37{
 38    Geometry1 g1;
 39    Geometry2 g2;
 40    bg::read_wkt(wkt1, g1);
 41    bg::read_wkt(wkt2, g2);
 42    typename bg::default_distance_result<Geometry1, Geometry2>::type d = bg::distance(g1, g2);
 43
 44    BOOST_CHECK_CLOSE(d, expected, 0.0001);
 45}
 46
 47template <typename Geometry1, typename Geometry2, typename Strategy>
 48void test_distance(Strategy const& strategy, std::string const& wkt1,
 49                   std::string const& wkt2, double expected)
 50{
 51    Geometry1 g1;
 52    Geometry2 g2;
 53    bg::read_wkt(wkt1, g1);
 54    bg::read_wkt(wkt2, g2);
 55    typename bg::default_distance_result<Geometry1, Geometry2>::type d = bg::distance(g1, g2, strategy);
 56
 57    BOOST_CHECK_CLOSE(d, expected, 0.0001);
 58}
 59
 60
 61template <typename P>
 62void test_2d()
 63{
 64    typedef bg::model::multi_point<P> mp;
 65    typedef bg::model::multi_linestring<bg::model::linestring<P> > ml;
 66    test_distance<P, P>("POINT(0 0)", "POINT(1 1)", sqrt(2.0));
 67    test_distance<P, mp>("POINT(0 0)", "MULTIPOINT((1 1),(1 0),(0 2))", 1.0);
 68    test_distance<mp, P>("MULTIPOINT((1 1),(1 0),(0 2))", "POINT(0 0)", 1.0);
 69    test_distance<mp, mp>("MULTIPOINT((1 1),(1 0),(0 2))", "MULTIPOINT((2 2),(2 3))", sqrt(2.0));
 70    test_distance<P, ml>("POINT(0 0)", "MULTILINESTRING((1 1,2 2),(1 0,2 0),(0 2,0 3))", 1.0);
 71    test_distance<ml, P>("MULTILINESTRING((1 1,2 2),(1 0,2 0),(0 2,0 3))", "POINT(0 0)", 1.0);
 72    test_distance<ml, mp>("MULTILINESTRING((1 1,2 2),(1 0,2 0),(0 2,0 3))", "MULTIPOINT((0 0),(1 1))", 0.0);
 73
 74    // Test with a strategy
 75    bg::strategy::distance::pythagoras<P, P> pyth;
 76    test_distance<P, P>(pyth, "POINT(0 0)", "POINT(1 1)", sqrt(2.0));
 77    test_distance<P, mp>(pyth, "POINT(0 0)", "MULTIPOINT((1 1),(1 0),(0 2))", 1.0);
 78    test_distance<mp, P>(pyth, "MULTIPOINT((1 1),(1 0),(0 2))", "POINT(0 0)", 1.0);
 79}
 80
 81
 82template <typename P>
 83void test_3d()
 84{
 85    typedef bg::model::multi_point<P> mp;
 86    test_distance<P, P>("POINT(0 0 0)", "POINT(1 1 1)", sqrt(3.0));
 87    test_distance<P, mp>("POINT(0 0 0)", "MULTIPOINT((1 1 1),(1 0 0),(0 1 2))", 1.0);
 88    test_distance<mp, mp>("MULTIPOINT((1 1 1),(1 0 0),(0 0 2))", "MULTIPOINT((2 2 2),(2 3 4))", sqrt(3.0));
 89}
 90
 91
 92template <typename P1, typename P2>
 93void test_mixed()
 94{
 95    typedef bg::model::multi_point<P1> mp1;
 96    typedef bg::model::multi_point<P2> mp2;
 97
 98    test_distance<P1, P2>("POINT(0 0)", "POINT(1 1)", sqrt(2.0));
 99
100    test_distance<P1, mp1>("POINT(0 0)", "MULTIPOINT((1 1),(1 0),(0 2))", 1.0);
101    test_distance<P1, mp2>("POINT(0 0)", "MULTIPOINT((1 1),(1 0),(0 2))", 1.0);
102    test_distance<P2, mp1>("POINT(0 0)", "MULTIPOINT((1 1),(1 0),(0 2))", 1.0);
103    test_distance<P2, mp2>("POINT(0 0)", "MULTIPOINT((1 1),(1 0),(0 2))", 1.0);
104
105    // Test automatic reversal
106    test_distance<mp1, P1>("MULTIPOINT((1 1),(1 0),(0 2))", "POINT(0 0)", 1.0);
107    test_distance<mp1, P2>("MULTIPOINT((1 1),(1 0),(0 2))", "POINT(0 0)", 1.0);
108    test_distance<mp2, P1>("MULTIPOINT((1 1),(1 0),(0 2))", "POINT(0 0)", 1.0);
109    test_distance<mp2, P2>("MULTIPOINT((1 1),(1 0),(0 2))", "POINT(0 0)", 1.0);
110
111    // Test multi-multi using different point types for each
112    test_distance<mp1, mp2>("MULTIPOINT((1 1),(1 0),(0 2))", "MULTIPOINT((2 2),(2 3))", sqrt(2.0));
113
114    // Test with a strategy
115    using namespace bg::strategy::distance;
116
117    test_distance<P1, P2>(pythagoras<P1, P2>(), "POINT(0 0)", "POINT(1 1)", sqrt(2.0));
118
119    test_distance<P1, mp1>(pythagoras<P1, P1>(), "POINT(0 0)", "MULTIPOINT((1 1),(1 0),(0 2))", 1.0);
120    test_distance<P1, mp2>(pythagoras<P1, P2>(), "POINT(0 0)", "MULTIPOINT((1 1),(1 0),(0 2))", 1.0);
121    test_distance<P2, mp1>(pythagoras<P2, P1>(), "POINT(0 0)", "MULTIPOINT((1 1),(1 0),(0 2))", 1.0);
122    test_distance<P2, mp2>(pythagoras<P2, P2>(), "POINT(0 0)", "MULTIPOINT((1 1),(1 0),(0 2))", 1.0);
123
124    // Most interesting: reversal AND a strategy (note that the stategy must be reversed automatically
125    test_distance<mp1, P1>(pythagoras<P1, P1>(), "MULTIPOINT((1 1),(1 0),(0 2))", "POINT(0 0)", 1.0);
126    test_distance<mp1, P2>(pythagoras<P1, P2>(), "MULTIPOINT((1 1),(1 0),(0 2))", "POINT(0 0)", 1.0);
127    test_distance<mp2, P1>(pythagoras<P2, P1>(), "MULTIPOINT((1 1),(1 0),(0 2))", "POINT(0 0)", 1.0);
128    test_distance<mp2, P2>(pythagoras<P2, P2>(), "MULTIPOINT((1 1),(1 0),(0 2))", "POINT(0 0)", 1.0);
129}
130
131
132
133int test_main( int , char* [] )
134{
135    test_2d<boost::tuple<float, float> >();
136    test_2d<bg::model::d2::point_xy<float> >();
137    test_2d<bg::model::d2::point_xy<double> >();
138
139    test_3d<boost::tuple<float, float, float> >();
140    test_3d<bg::model::point<double, 3, bg::cs::cartesian> >();
141
142    test_mixed<bg::model::d2::point_xy<float>, bg::model::d2::point_xy<double> >();
143
144#ifdef HAVE_TTMATH
145    test_2d<bg::model::d2::point_xy<ttmath_big> >();
146    test_mixed<bg::model::d2::point_xy<ttmath_big>, bg::model::d2::point_xy<double> >();
147#endif
148
149    return 0;
150}