PageRenderTime 38ms CodeModel.GetById 12ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/libs/geometry/test/strategies/pythagoras.cpp

http://hadesmem.googlecode.com/
C++ | 320 lines | 210 code | 78 blank | 32 comment | 4 complexity | 9a8fecdb4f3be3a2e1362590003d29dd 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 <geometry_test_common.hpp>
 17
 18#if defined(_MSC_VER)
 19#  pragma warning( disable : 4101 )
 20#endif
 21
 22#include <boost/timer.hpp>
 23
 24#include <boost/concept/requires.hpp>
 25#include <boost/concept_check.hpp>
 26
 27#include <boost/geometry/algorithms/assign.hpp>
 28#include <boost/geometry/strategies/cartesian/distance_pythagoras.hpp>
 29#include <boost/geometry/strategies/concepts/distance_concept.hpp>
 30
 31
 32#include <boost/geometry/geometries/point.hpp>
 33#include <boost/geometry/geometries/adapted/c_array.hpp>
 34#include <boost/geometry/geometries/adapted/boost_tuple.hpp>
 35
 36#include <test_common/test_point.hpp>
 37
 38#ifdef HAVE_TTMATH
 39#  include <boost/geometry/extensions/contrib/ttmath_stub.hpp>
 40#endif
 41
 42BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian)
 43BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)
 44
 45
 46template <typename P1, typename P2>
 47void test_null_distance_3d()
 48{
 49    P1 p1;
 50    bg::assign_values(p1, 1, 2, 3);
 51    P2 p2;
 52    bg::assign_values(p2, 1, 2, 3);
 53
 54    typedef bg::strategy::distance::pythagoras<P1, P2> pythagoras_type;
 55    typedef typename bg::strategy::distance::services::return_type<pythagoras_type>::type return_type;
 56
 57    pythagoras_type pythagoras;
 58    return_type result = pythagoras.apply(p1, p2);
 59
 60    BOOST_CHECK_EQUAL(result, return_type(0));
 61}
 62
 63template <typename P1, typename P2>
 64void test_axis_3d()
 65{
 66    P1 p1;
 67    bg::assign_values(p1, 0, 0, 0);
 68    P2 p2;
 69    bg::assign_values(p2, 1, 0, 0);
 70
 71    typedef bg::strategy::distance::pythagoras<P1, P2> pythagoras_type;
 72    typedef typename bg::strategy::distance::services::return_type<pythagoras_type>::type return_type;
 73
 74    pythagoras_type pythagoras;
 75
 76    return_type result = pythagoras.apply(p1, p2);
 77    BOOST_CHECK_EQUAL(result, return_type(1));
 78
 79    bg::assign_values(p2, 0, 1, 0);
 80    result = pythagoras.apply(p1, p2);
 81    BOOST_CHECK_EQUAL(result, return_type(1));
 82
 83    bg::assign_values(p2, 0, 0, 1);
 84    result = pythagoras.apply(p1, p2);
 85    BOOST_CHECK_CLOSE(result, return_type(1), 0.001);
 86}
 87
 88template <typename P1, typename P2>
 89void test_arbitrary_3d()
 90{
 91    P1 p1;
 92    bg::assign_values(p1, 1, 2, 3);
 93    P2 p2;
 94    bg::assign_values(p2, 9, 8, 7);
 95
 96    {
 97        typedef bg::strategy::distance::pythagoras<P1, P2> strategy_type;
 98        typedef typename bg::strategy::distance::services::return_type<strategy_type>::type return_type;
 99
100        strategy_type strategy;
101        return_type result = strategy.apply(p1, p2);
102        BOOST_CHECK_CLOSE(result, return_type(10.77032961427), 0.001);
103    }
104
105    {
106        // Check comparable distance
107        typedef bg::strategy::distance::comparable::pythagoras<P1, P2> strategy_type;
108        typedef typename bg::strategy::distance::services::return_type<strategy_type>::type return_type;
109
110        strategy_type strategy;
111        return_type result = strategy.apply(p1, p2);
112        BOOST_CHECK_EQUAL(result, return_type(116));
113    }
114}
115
116template <typename P1, typename P2, typename CalculationType>
117void test_services()
118{
119    namespace bgsd = bg::strategy::distance;
120    namespace services = bg::strategy::distance::services;
121
122    {
123
124        // Compile-check if there is a strategy for this type
125        typedef typename services::default_strategy<bg::point_tag, P1, P2>::type pythagoras_strategy_type;
126    }
127
128
129    P1 p1;
130    bg::assign_values(p1, 1, 2, 3);
131
132    P2 p2;
133    bg::assign_values(p2, 4, 5, 6);
134
135    double const sqr_expected = 3*3 + 3*3 + 3*3; // 27
136    double const expected = sqrt(sqr_expected); // sqrt(27)=5.1961524227
137
138    // 1: normal, calculate distance:
139
140    typedef bgsd::pythagoras<P1, P2, CalculationType> strategy_type;
141
142    BOOST_CONCEPT_ASSERT( (bg::concept::PointDistanceStrategy<strategy_type>) );
143
144    typedef typename bgsd::services::return_type<strategy_type>::type return_type;
145
146    strategy_type strategy;
147    return_type result = strategy.apply(p1, p2);
148    BOOST_CHECK_CLOSE(result, return_type(expected), 0.001);
149
150    // 2: "similar" to construct a similar strategy (similar but with other template-parameters) for, e.g., the reverse P2/P1
151    // 2a: similar_type:
152    typedef typename services::similar_type<strategy_type, P2, P1>::type similar_type;
153    // 2b: get_similar
154    similar_type similar = services::get_similar<strategy_type, P2, P1>::apply(strategy);
155
156    //result = similar.apply(p1, p2); // should NOT compile because p1/p2 should also be reversed here
157    result = similar.apply(p2, p1);
158    BOOST_CHECK_CLOSE(result, return_type(expected), 0.001);
159
160
161    // 3: "comparable" to construct a "comparable strategy" for P1/P2
162    //    a "comparable strategy" is a strategy which does not calculate the exact distance, but
163    //    which returns results which can be mutually compared (e.g. avoid sqrt)
164
165    // 3a: "comparable_type"
166    typedef typename services::comparable_type<strategy_type>::type comparable_type;
167
168    // 3b: "get_comparable"
169    comparable_type comparable = bgsd::services::get_comparable<strategy_type>::apply(strategy);
170
171    return_type c_result = comparable.apply(p1, p2);
172    BOOST_CHECK_CLOSE(c_result, return_type(sqr_expected), 0.001);
173
174    // 4: the comparable_type should have a distance_strategy_constructor as well,
175    //    knowing how to compare something with a fixed distance
176    return_type c_dist5 = services::result_from_distance<comparable_type>::apply(comparable, 5.0);
177    return_type c_dist6 = services::result_from_distance<comparable_type>::apply(comparable, 6.0);
178
179    // If this is the case:
180    BOOST_CHECK(c_dist5 < c_result && c_result < c_dist6);
181
182    // This should also be the case
183    return_type dist5 = services::result_from_distance<strategy_type>::apply(strategy, 5.0);
184    return_type dist6 = services::result_from_distance<strategy_type>::apply(strategy, 6.0);
185    BOOST_CHECK(dist5 < result && result < dist6);
186}
187
188
189template <typename CoordinateType, typename CalculationType, typename AssignType>
190void test_big_2d_with(AssignType const& x1, AssignType const& y1,
191                 AssignType const& x2, AssignType const& y2)
192{
193    typedef bg::model::point<CoordinateType, 2, bg::cs::cartesian> point_type;
194    typedef bg::strategy::distance::pythagoras
195        <
196            point_type,
197            point_type,
198            CalculationType
199        > pythagoras_type;
200
201    pythagoras_type pythagoras;
202    typedef typename bg::strategy::distance::services::return_type<pythagoras_type>::type return_type;
203
204
205    point_type p1, p2;
206    bg::assign_values(p1, x1, y1);
207    bg::assign_values(p2, x2, y2);
208    return_type d = pythagoras.apply(p1, p2);
209
210    /***
211    std::cout << typeid(CalculationType).name()
212        << " " << std::fixed << std::setprecision(20) << d
213        << std::endl << std::endl;
214    ***/
215
216
217    BOOST_CHECK_CLOSE(d, return_type(1076554.5485833955678294387789057), 0.001);
218}
219
220template <typename CoordinateType, typename CalculationType>
221void test_big_2d()
222{
223    test_big_2d_with<CoordinateType, CalculationType>
224        (123456.78900001, 234567.89100001,
225        987654.32100001, 876543.21900001);
226}
227
228template <typename CoordinateType, typename CalculationType>
229void test_big_2d_string()
230{
231    test_big_2d_with<CoordinateType, CalculationType>
232        ("123456.78900001", "234567.89100001",
233        "987654.32100001", "876543.21900001");
234}
235
236
237
238template <typename P1, typename P2>
239void test_all_3d()
240{
241    test_null_distance_3d<P1, P2>();
242    test_axis_3d<P1, P2>();
243    test_arbitrary_3d<P1, P2>();
244}
245
246template <typename P>
247void test_all_3d()
248{
249    test_all_3d<P, int[3]>();
250    test_all_3d<P, float[3]>();
251    test_all_3d<P, double[3]>();
252    test_all_3d<P, test::test_point>();
253    test_all_3d<P, bg::model::point<int, 3, bg::cs::cartesian> >();
254    test_all_3d<P, bg::model::point<float, 3, bg::cs::cartesian> >();
255    test_all_3d<P, bg::model::point<double, 3, bg::cs::cartesian> >();
256}
257
258template <typename P, typename Strategy>
259void time_compare_s(int const n)
260{
261    boost::timer t;
262    P p1, p2;
263    bg::assign_values(p1, 1, 1);
264    bg::assign_values(p2, 2, 2);
265    Strategy strategy;
266    typename bg::strategy::distance::services::return_type<Strategy>::type s = 0;
267    for (int i = 0; i < n; i++)
268    {
269        for (int j = 0; j < n; j++)
270        {
271            bg::set<0>(p2, bg::get<0>(p2) + 0.001);
272            s += strategy.apply(p1, p2);
273        }
274    }
275    std::cout << "s: " << s << " t: " << t.elapsed() << std::endl;
276}
277
278template <typename P>
279void time_compare(int const n)
280{
281    time_compare_s<P, bg::strategy::distance::pythagoras<P> >(n);
282    time_compare_s<P, bg::strategy::distance::comparable::pythagoras<P> >(n);
283}
284
285int test_main(int, char* [])
286{
287    test_all_3d<int[3]>();
288    test_all_3d<float[3]>();
289    test_all_3d<double[3]>();
290
291    test_all_3d<test::test_point>();
292
293    test_all_3d<bg::model::point<int, 3, bg::cs::cartesian> >();
294    test_all_3d<bg::model::point<float, 3, bg::cs::cartesian> >();
295    test_all_3d<bg::model::point<double, 3, bg::cs::cartesian> >();
296
297    test_big_2d<float, float>();
298    test_big_2d<double, double>();
299    test_big_2d<long double, long double>();
300    test_big_2d<float, long double>();
301
302    test_services<bg::model::point<float, 3, bg::cs::cartesian>, double[3], long double>();
303    test_services<double[3], test::test_point, float>();
304
305
306    time_compare<bg::model::point<double, 2, bg::cs::cartesian> >(10000);
307
308#if defined(HAVE_TTMATH)
309
310    typedef ttmath::Big<1,4> tt;
311    typedef bg::model::point<tt, 3, bg::cs::cartesian> tt_point;
312
313    //test_all_3d<tt[3]>();
314    test_all_3d<tt_point>();
315    test_all_3d<tt_point, tt_point>();
316    test_big_2d<tt, tt>();
317    test_big_2d_string<tt, tt>();
318#endif
319    return 0;
320}