PageRenderTime 22ms CodeModel.GetById 2ms app.highlight 16ms RepoModel.GetById 2ms app.codeStats 0ms

/Src/Dependencies/Boost/libs/geometry/test/algorithms/comparable_distance.cpp

http://hadesmem.googlecode.com/
C++ | 146 lines | 94 code | 38 blank | 14 comment | 0 complexity | c16114ac514a3a3452acacbc432b4bea MD5 | raw file
  1// Boost.Geometry (aka GGL, Generic Geometry Library)
  2// Unit Test
  3
  4// Copyright (c) 2007-2011 Barend Gehrels, Amsterdam, the Netherlands.
  5// Copyright (c) 2008-2011 Bruno Lalande, Paris, France.
  6// Copyright (c) 2009-2011 Mateusz Loskot, London, UK.
  7
  8// Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  9// (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
 10
 11// Use, modification and distribution is subject to the Boost Software License,
 12// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
 13// http://www.boost.org/LICENSE_1_0.txt)
 14
 15
 16#include <sstream>
 17
 18#include <boost/mpl/if.hpp>
 19#include <geometry_test_common.hpp>
 20
 21#include <boost/geometry/geometry.hpp>
 22
 23#include <boost/geometry/geometries/point_xy.hpp>
 24#include <boost/geometry/geometries/linestring.hpp>
 25
 26#include <boost/geometry/algorithms/comparable_distance.hpp>
 27#include <boost/geometry/strategies/strategies.hpp>
 28
 29
 30#include <boost/geometry/geometries/geometries.hpp>
 31
 32
 33template <typename P>
 34void test_distance_result()
 35{
 36    typedef typename bg::default_distance_result<P, P>::type distance_type;
 37
 38    P p1 = bg::make<P>(0, 0);
 39    P p2 = bg::make<P>(3, 0);
 40    P p3 = bg::make<P>(0, 4);
 41
 42    distance_type dr12 = bg::comparable_distance(p1, p2);
 43    distance_type dr13 = bg::comparable_distance(p1, p3);
 44    distance_type dr23 = bg::comparable_distance(p2, p3);
 45
 46    BOOST_CHECK_CLOSE(dr12, 9.000, 0.001);
 47    BOOST_CHECK_CLOSE(dr13, 16.000, 0.001);
 48    BOOST_CHECK_CLOSE(dr23, 25.000, 0.001);
 49
 50}
 51
 52template <typename P>
 53void test_distance_point()
 54{
 55    P p1;
 56    bg::set<0>(p1, 1);
 57    bg::set<1>(p1, 1);
 58
 59    P p2;
 60    bg::set<0>(p2, 2);
 61    bg::set<1>(p2, 2);
 62
 63    typename bg::coordinate_type<P>::type d = bg::comparable_distance(p1, p2);
 64    BOOST_CHECK_CLOSE(d, 2.0, 0.001);
 65}
 66
 67template <typename P>
 68void test_distance_segment()
 69{
 70    typedef typename bg::coordinate_type<P>::type coordinate_type;
 71
 72    P s1 = bg::make<P>(2, 2);
 73    P s2 = bg::make<P>(3, 3);
 74
 75    // Check points left, right, projected-left, projected-right, on segment
 76    P p1 = bg::make<P>(0, 0);
 77    P p2 = bg::make<P>(4, 4);
 78    P p3 = bg::make<P>(2.4, 2.6);
 79    P p4 = bg::make<P>(2.6, 2.4);
 80    P p5 = bg::make<P>(2.5, 2.5);
 81
 82    bg::model::referring_segment<P const> const seg(s1, s2);
 83
 84    coordinate_type d1 = bg::comparable_distance(p1, seg); BOOST_CHECK_CLOSE(d1, 8.0, 0.001);
 85    coordinate_type d2 = bg::comparable_distance(p2, seg); BOOST_CHECK_CLOSE(d2, 2.0, 0.001);
 86    coordinate_type d3 = bg::comparable_distance(p3, seg); BOOST_CHECK_CLOSE(d3, 0.02, 0.001);
 87    coordinate_type d4 = bg::comparable_distance(p4, seg); BOOST_CHECK_CLOSE(d4, 0.02, 0.001);
 88    coordinate_type d5 = bg::comparable_distance(p5, seg); BOOST_CHECK_CLOSE(d5, 0.0, 0.001);
 89
 90    // Reverse case
 91    coordinate_type dr1 = bg::comparable_distance(seg, p1); BOOST_CHECK_CLOSE(dr1, d1, 0.001);
 92    coordinate_type dr2 = bg::comparable_distance(seg, p2); BOOST_CHECK_CLOSE(dr2, d2, 0.001);
 93}
 94
 95template <typename P>
 96void test_distance_linestring()
 97{
 98    bg::model::linestring<P> points;
 99    points.push_back(bg::make<P>(1, 1));
100    points.push_back(bg::make<P>(3, 3));
101
102    P p = bg::make<P>(2, 1);
103
104    typename bg::coordinate_type<P>::type d = bg::comparable_distance(p, points);
105    BOOST_CHECK_CLOSE(d, 0.70710678, 0.001);
106
107    p = bg::make<P>(5, 5);
108    d = bg::comparable_distance(p, points);
109    BOOST_CHECK_CLOSE(d, 2.828427, 0.001);
110
111
112    bg::model::linestring<P> line;
113    line.push_back(bg::make<P>(1,1));
114    line.push_back(bg::make<P>(2,2));
115    line.push_back(bg::make<P>(3,3));
116
117    p = bg::make<P>(5, 5);
118
119    d = bg::comparable_distance(p, line);
120    BOOST_CHECK_CLOSE(d, 2.828427, 0.001);
121
122    // Reverse case
123    d = bg::comparable_distance(line, p);
124    BOOST_CHECK_CLOSE(d, 2.828427, 0.001);
125}
126
127template <typename P>
128void test_all()
129{
130    test_distance_result<P>();
131    test_distance_point<P>();
132    test_distance_segment<P>();
133    test_distance_linestring<P>();
134}
135
136int test_main(int, char* [])
137{
138    //test_all<bg::model::d2::point_xy<int> >();
139    test_all<bg::model::d2::point_xy<float> >();
140    test_all<bg::model::d2::point_xy<double> >();
141
142#ifdef HAVE_TTMATH
143    test_all<bg::model::d2::point_xy<ttmath_big> >();
144#endif
145    return 0;
146}