PageRenderTime 55ms CodeModel.GetById 10ms app.highlight 40ms RepoModel.GetById 2ms app.codeStats 0ms

/libs/geometry/test/strategies/projected_point.cpp

https://github.com/delalond/boost_1_54_0-bgq
C++ | 188 lines | 120 code | 43 blank | 25 comment | 0 complexity | 8d51a792c9ec6fdc4ea10fcd1265a709 MD5 | raw file
  1// Boost.Geometry (aka GGL, Generic Geometry Library)
  2// Unit Test
  3
  4// Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  5// Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  6// Copyright (c) 2009-2012 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 <geometry_test_common.hpp>
 17
 18#include <boost/geometry/strategies/cartesian/distance_projected_point.hpp>
 19#include <boost/geometry/strategies/concepts/distance_concept.hpp>
 20
 21#include <boost/geometry/io/wkt/read.hpp>
 22
 23
 24#include <boost/geometry/geometries/point.hpp>
 25#include <boost/geometry/geometries/adapted/c_array.hpp>
 26#include <boost/geometry/geometries/adapted/boost_tuple.hpp>
 27#include <test_common/test_point.hpp>
 28
 29#ifdef HAVE_TTMATH
 30#  include <boost/geometry/extensions/contrib/ttmath_stub.hpp>
 31#endif
 32
 33BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian)
 34BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)
 35
 36
 37template <typename P, typename PS, typename CalculationType>
 38void test_services()
 39{
 40    PS p1, p2;
 41    bg::assign_values(p1, 0, 0);
 42    bg::assign_values(p2, 0, 4);
 43
 44    P p;
 45    bg::assign_values(p, 2, 0);
 46
 47    CalculationType const sqr_expected = 4;
 48    CalculationType const expected = 2;
 49
 50
 51    namespace bgsd = bg::strategy::distance;
 52    namespace services = bg::strategy::distance::services;
 53    // 1: normal, calculate distance:
 54
 55    typedef bgsd::projected_point<P, PS, CalculationType> strategy_type;
 56
 57    BOOST_CONCEPT_ASSERT( (bg::concept::PointSegmentDistanceStrategy<strategy_type>) );
 58
 59    typedef typename services::return_type<strategy_type>::type return_type;
 60
 61    strategy_type strategy;
 62    return_type result = strategy.apply(p, p1, p2);
 63    BOOST_CHECK_CLOSE(result, return_type(expected), 0.001);
 64
 65    // 2: "similar" to construct a similar strategy (similar but with other template-parameters) for, e.g., the reverse P2/P1
 66    // 2a: similar_type:
 67    typedef typename services::similar_type<strategy_type, P, PS>::type similar_type;
 68    // 2b: get_similar
 69    similar_type similar = services::get_similar<strategy_type, P, PS>::apply(strategy);
 70
 71    result = similar.apply(p, p1, p2);
 72    BOOST_CHECK_CLOSE(result, return_type(expected), 0.001);
 73
 74
 75    // 3: "comparable" to construct a "comparable strategy" for P1/P2
 76    //    a "comparable strategy" is a strategy which does not calculate the exact distance, but
 77    //    which returns results which can be mutually compared (e.g. avoid sqrt)
 78
 79    // 3a: "comparable_type"
 80    typedef typename services::comparable_type<strategy_type>::type comparable_type;
 81
 82    // 3b: "get_comparable"
 83    comparable_type comparable = bgsd::services::get_comparable<strategy_type>::apply(strategy);
 84
 85    return_type c_result = comparable.apply(p, p1, p2);
 86    BOOST_CHECK_CLOSE(c_result, return_type(sqr_expected), 0.001);
 87}
 88
 89
 90template <typename P1, typename P2, typename T>
 91void test_all_2d(std::string const& wkt_p,
 92                 std::string const& wkt_sp1,
 93                 std::string const& wkt_sp2,
 94                 T expected_distance)
 95{
 96    P1 p;
 97    P2 sp1, sp2;
 98    bg::read_wkt(wkt_p, p);
 99    bg::read_wkt(wkt_sp1, sp1);
100    bg::read_wkt(wkt_sp2, sp2);
101
102    {
103        typedef bg::strategy::distance::projected_point
104            <
105                P1,
106                P2
107            > strategy_type;
108
109        BOOST_CONCEPT_ASSERT
110            (
111                (bg::concept::PointSegmentDistanceStrategy<strategy_type>)
112            );
113
114        strategy_type strategy;
115        typedef typename bg::strategy::distance::services::return_type<strategy_type>::type return_type;
116        return_type d = strategy.apply(p, sp1, sp2);
117        BOOST_CHECK_CLOSE(d, expected_distance, 0.001);
118    }
119
120    // Test combination with the comparable strategy
121    {
122        typedef bg::strategy::distance::projected_point
123            <
124                P1,
125                P2,
126                void,
127                bg::strategy::distance::comparable::pythagoras<P1, P2>
128            > strategy_type;
129        strategy_type strategy;
130        typedef typename bg::strategy::distance::services::return_type<strategy_type>::type return_type;
131        return_type d = strategy.apply(p, sp1, sp2);
132        T expected_squared_distance = expected_distance * expected_distance;
133        BOOST_CHECK_CLOSE(d, expected_squared_distance, 0.01);
134    }
135
136}
137
138template <typename P1, typename P2>
139void test_all_2d()
140{
141    test_all_2d<P1, P2>("POINT(1 1)", "POINT(0 0)", "POINT(2 3)", 0.27735203958327);
142    test_all_2d<P1, P2>("POINT(2 2)", "POINT(1 4)", "POINT(4 1)", 0.5 * sqrt(2.0));
143    test_all_2d<P1, P2>("POINT(6 1)", "POINT(1 4)", "POINT(4 1)", 2.0);
144    test_all_2d<P1, P2>("POINT(1 6)", "POINT(1 4)", "POINT(4 1)", 2.0);
145}
146
147template <typename P>
148void test_all_2d()
149{
150    //test_all_2d<P, int[2]>();
151    //test_all_2d<P, float[2]>();
152    //test_all_2d<P, double[2]>();
153    //test_all_2d<P, test::test_point>();
154    test_all_2d<P, bg::model::point<int, 2, bg::cs::cartesian> >();
155    test_all_2d<P, bg::model::point<float, 2, bg::cs::cartesian> >();
156    test_all_2d<P, bg::model::point<double, 2, bg::cs::cartesian> >();
157    test_all_2d<P, bg::model::point<long double, 2, bg::cs::cartesian> >();
158}
159
160int test_main(int, char* [])
161{
162    test_all_2d<int[2]>();
163    test_all_2d<float[2]>();
164    test_all_2d<double[2]>();
165    //test_all_2d<test::test_point>();
166
167    test_all_2d<bg::model::point<int, 2, bg::cs::cartesian> >();
168    test_all_2d<bg::model::point<float, 2, bg::cs::cartesian> >();
169    test_all_2d<bg::model::point<double, 2, bg::cs::cartesian> >();
170
171    test_services
172        <
173            bg::model::point<double, 2, bg::cs::cartesian>,
174            bg::model::point<float, 2, bg::cs::cartesian>,
175            long double
176        >();
177
178
179#if defined(HAVE_TTMATH)
180    test_all_2d
181        <
182            bg::model::point<ttmath_big, 2, bg::cs::cartesian>,
183            bg::model::point<ttmath_big, 2, bg::cs::cartesian>
184        >();
185#endif
186
187    return 0;
188}