// Boost.Geometry (aka GGL, Generic Geometry Library) test file // // Copyright Barend Gehrels 2007-2009, Geodan, Amsterdam, the Netherlands // Copyright Bruno Lalande 2008, 2009 // 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) //#define TEST_ARRAY #include #include #include #include #include #ifndef TEST_ARRAY #include #endif #include #include #include #include #include template void test_distance_result() { typedef typename boost::geometry::distance_result::type distance_type; #ifndef TEST_ARRAY P p1 = boost::geometry::make

(0, 0); P p2 = boost::geometry::make

(3, 0); P p3 = boost::geometry::make

(0, 4); #else P p1, p2, p3; boost::geometry::set<0>(p1, 0); boost::geometry::set<1>(p1, 0); boost::geometry::set<0>(p2, 3); boost::geometry::set<1>(p2, 0); boost::geometry::set<0>(p3, 0); boost::geometry::set<1>(p3, 4); #endif distance_type dr12 = boost::geometry::distance(p1, p2); distance_type dr13 = boost::geometry::distance(p1, p3); distance_type dr23 = boost::geometry::distance(p2, p3); BOOST_CHECK_CLOSE(double(dr12), 3.000, 0.001); BOOST_CHECK_CLOSE(double(dr13), 4.000, 0.001); BOOST_CHECK_CLOSE(double(dr23), 5.000, 0.001); // COMPILATION TESTS distance_type comparable = boost::geometry::make_distance_result(3); //BOOST_CHECK_CLOSE(comparable.value(), 9.000, 0.001); // Question: how to check if the implemented operator is used, and not the auto-conversion to double? if (comparable == dr12) {}; if (comparable < dr12) {}; if (comparable > dr12) {}; // Check streamability std::ostringstream s; s << comparable; // Check comparisons with plain double double d = 3.0; if (dr12 == d) {}; if (dr12 < d) {}; if (dr12 > d) {}; } template void test_distance_point() { P p1; boost::geometry::set<0>(p1, 1); boost::geometry::set<1>(p1, 1); P p2; boost::geometry::set<0>(p2, 2); boost::geometry::set<1>(p2, 2); double d = boost::geometry::distance(p1, p2); BOOST_CHECK_CLOSE(d, 1.4142135, 0.001); } template void test_distance_segment() { typedef typename boost::geometry::coordinate_type

::type coordinate_type; P s1; boost::geometry::set<0>(s1, 2); boost::geometry::set<1>(s1, 2); P s2; boost::geometry::set<0>(s2, 3); boost::geometry::set<1>(s2, 3); // Check points left, right, projected-left, projected-right, on segment P p1; boost::geometry::set<0>(p1, 0); boost::geometry::set<1>(p1, 0); P p2; boost::geometry::set<0>(p2, 4); boost::geometry::set<1>(p2, 4); P p3; boost::geometry::set<0>(p3, coordinate_type(2.4)); boost::geometry::set<1>(p3, coordinate_type(2.6)); P p4; boost::geometry::set<0>(p4, coordinate_type(2.6)); boost::geometry::set<1>(p4, coordinate_type(2.4)); P p5; boost::geometry::set<0>(p5, 2.5); boost::geometry::set<1>(p5, 2.5); const boost::geometry::segment seg(s1, s2); double d1 = boost::geometry::distance(p1, seg); BOOST_CHECK_CLOSE(d1, 2.8284271, 0.001); double d2 = boost::geometry::distance(p2, seg); BOOST_CHECK_CLOSE(d2, 1.4142135, 0.001); double d3 = boost::geometry::distance(p3, seg); BOOST_CHECK_CLOSE(d3, 0.141421, 0.001); double d4 = boost::geometry::distance(p4, seg); BOOST_CHECK_CLOSE(d4, 0.141421, 0.001); double d5 = boost::geometry::distance(p5, seg); BOOST_CHECK_CLOSE(d5, 0.0, 0.001); // Reverse case double dr1 = boost::geometry::distance(seg, p1); BOOST_CHECK_CLOSE(dr1, d1, 0.001); double dr2 = boost::geometry::distance(seg, p2); BOOST_CHECK_CLOSE(dr2, d2, 0.001); } template void test_distance_linestring() { typedef typename boost::geometry::coordinate_type

::type coordinate_type; boost::array points; boost::geometry::set<0>(points[0], 1); boost::geometry::set<1>(points[0], 1); boost::geometry::set<0>(points[1], 3); boost::geometry::set<1>(points[1], 3); #ifndef TEST_ARRAY P p = boost::geometry::make

(2, 1); #else P p; boost::geometry::set<0>(p, 2); boost::geometry::set<1>(p, 1); #endif double d = boost::geometry::distance(p, points); BOOST_CHECK_CLOSE(d, 0.70710678, 0.001); #ifndef TEST_ARRAY p = boost::geometry::make

(5, 5); #else boost::geometry::set<0>(p, 5); boost::geometry::set<1>(p, 5); #endif d = boost::geometry::distance(p, points); BOOST_CHECK_CLOSE(d, 2.828427, 0.001); boost::geometry::linestring

line; #ifndef TEST_ARRAY line.push_back(boost::geometry::make

(1,1)); line.push_back(boost::geometry::make

(2,2)); line.push_back(boost::geometry::make

(3,3)); #else { P lp; boost::geometry::set<0>(lp, 1); boost::geometry::set<1>(lp, 1); line.push_back(lp); boost::geometry::set<0>(lp, 2); boost::geometry::set<1>(lp, 2); line.push_back(lp); boost::geometry::set<0>(lp, 3); boost::geometry::set<1>(lp, 3); line.push_back(lp); } #endif #ifndef TEST_ARRAY p = boost::geometry::make

(5, 5); #else boost::geometry::set<0>(p, 5); boost::geometry::set<1>(p, 5); #endif d = boost::geometry::distance(p, line); BOOST_CHECK_CLOSE(d, 2.828427, 0.001); // Reverse case d = boost::geometry::distance(line, p); BOOST_CHECK_CLOSE(d, 2.828427, 0.001); } template void test_all() { test_distance_result

(); test_distance_point

(); test_distance_segment

(); #ifndef TEST_ARRAY test_distance_linestring

(); #endif } int test_main(int, char* []) { #ifdef TEST_ARRAY //test_all(); test_all(); test_all(); test_all(); // located here because of 3D #endif //test_all >(); test_all >(); test_all >(); test_all >(); return 0; }