mirror of
https://github.com/boostorg/geometry.git
synced 2025-05-11 13:34:10 +00:00
341 lines
9.9 KiB
C++
341 lines
9.9 KiB
C++
// Boost.Geometry (aka GGL, Generic Geometry Library)
|
|
// Unit Test
|
|
|
|
// Copyright (c) 2014, Oracle and/or its affiliates.
|
|
|
|
// Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
|
|
|
|
// Licensed under the Boost Software License version 1.0.
|
|
// http://www.boost.org/users/license.html
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#ifndef BOOST_TEST_MODULE
|
|
#define BOOST_TEST_MODULE test_point_iterator
|
|
#endif
|
|
|
|
#include <boost/test/included/unit_test.hpp>
|
|
|
|
#include <boost/geometry/geometries/geometries.hpp>
|
|
#include <boost/geometry/algorithms/equals.hpp>
|
|
#include <boost/geometry/multi/geometries/multi_geometries.hpp>
|
|
#include <boost/geometry/io/wkt/read.hpp>
|
|
#include <boost/geometry/io/wkt/write.hpp>
|
|
#include <boost/geometry/io/dsv/write.hpp>
|
|
#include <boost/geometry/multi/io/dsv/write.hpp>
|
|
#include <boost/geometry/multi/io/wkt/write.hpp>
|
|
#include <boost/geometry/multi/io/wkt/read.hpp>
|
|
|
|
#include <boost/geometry/core/point_iterator.hpp>
|
|
|
|
|
|
namespace bg = ::boost::geometry;
|
|
|
|
typedef bg::model::point<double, 2, bg::cs::cartesian> point_type;
|
|
typedef bg::model::linestring<point_type> linestring_type;
|
|
typedef bg::model::polygon<point_type, false, false> polygon_type; //ccw, open
|
|
|
|
// multi geometries
|
|
typedef bg::model::multi_point<point_type> multi_point_type;
|
|
typedef bg::model::multi_linestring<linestring_type> multi_linestring_type;
|
|
typedef bg::model::multi_polygon<polygon_type> multi_polygon_type;
|
|
|
|
|
|
template <typename Geometry>
|
|
Geometry from_wkt(std::string const& wkt)
|
|
{
|
|
Geometry res;
|
|
boost::geometry::read_wkt(wkt, res);
|
|
return res;
|
|
}
|
|
|
|
|
|
struct equals
|
|
{
|
|
template <typename Iterator>
|
|
static inline std::size_t number_of_elements(Iterator begin,
|
|
Iterator end)
|
|
{
|
|
std::size_t num_elems(0);
|
|
for (Iterator it = begin; it != end; ++it)
|
|
{
|
|
++num_elems;
|
|
}
|
|
return num_elems;
|
|
}
|
|
|
|
template <typename Iterator1, typename Iterator2>
|
|
static inline bool apply(Iterator1 begin1, Iterator1 end1,
|
|
Iterator2 begin2, Iterator2 end2)
|
|
{
|
|
std::size_t num_points1 = number_of_elements(begin1, end1);
|
|
std::size_t num_points2 = number_of_elements(begin2, end2);
|
|
|
|
if ( num_points1 != num_points2 )
|
|
{
|
|
return false;
|
|
}
|
|
|
|
Iterator1 it1 = begin1;
|
|
Iterator2 it2 = begin2;
|
|
for (; it1 != end1; ++it1, ++it2)
|
|
{
|
|
if ( !bg::equals(*it1, *it2) )
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
};
|
|
|
|
template <typename Geometry, typename PointRange>
|
|
struct test_point_iterator_of_geometry
|
|
{
|
|
template <typename G>
|
|
static inline void base_test(G& geometry,
|
|
PointRange const& point_range,
|
|
std::string const& header)
|
|
{
|
|
typedef typename bg::point_iterator_type
|
|
<
|
|
G
|
|
>::type point_iterator_type;
|
|
|
|
point_iterator_type begin = bg::points_begin(geometry);
|
|
point_iterator_type end = bg::points_end(geometry);
|
|
|
|
BOOST_CHECK( equals::apply(begin, end,
|
|
boost::begin(point_range),
|
|
boost::end(point_range))
|
|
);
|
|
|
|
#ifdef GEOMETRY_TEST_DEBUG
|
|
std::cout << header << " geometry: " << bg::wkt(geometry) << std::endl;
|
|
std::cout << "point range: (";
|
|
for (point_iterator_type pit = begin; pit != end; ++pit)
|
|
{
|
|
std::cout << " " << bg::dsv(*pit);
|
|
}
|
|
std::cout << " )" << std::endl;
|
|
std::cout << "expected point range: " << bg::wkt(point_range)
|
|
<< std::endl;
|
|
#endif
|
|
}
|
|
|
|
static inline void apply(Geometry geometry, PointRange const& point_range)
|
|
{
|
|
base_test<Geometry>(geometry, point_range, "non-const");
|
|
|
|
#ifdef GEOMETRY_TEST_DEBUG
|
|
std::cout << std::endl;
|
|
#endif
|
|
|
|
base_test<Geometry const>(geometry, point_range, "const");
|
|
|
|
#ifdef GEOMETRY_TEST_DEBUG
|
|
std::cout << std::endl << std::endl;
|
|
#endif
|
|
}
|
|
};
|
|
|
|
|
|
//======================================================================
|
|
//======================================================================
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE( test_linestring_point_iterator )
|
|
{
|
|
#ifdef GEOMETRY_TEST_DEBUG
|
|
std::cout << "*** LINESTRING ***" << std::endl;
|
|
#endif
|
|
|
|
typedef multi_point_type MP;
|
|
typedef linestring_type L;
|
|
|
|
typedef test_point_iterator_of_geometry<L, MP> tester;
|
|
|
|
tester::apply(from_wkt<L>("LINESTRING()"),
|
|
from_wkt<MP>("MULTIPOINT()")
|
|
);
|
|
|
|
tester::apply(from_wkt<L>("LINESTRING(3 3,4 4,5 5)"),
|
|
from_wkt<MP>("MULTIPOINT(3 3,4 4,5 5)")
|
|
);
|
|
|
|
#ifdef GEOMETRY_TEST_DEBUG
|
|
std::cout << std::endl << std::endl << std::endl;
|
|
#endif
|
|
}
|
|
|
|
|
|
//======================================================================
|
|
//======================================================================
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE( test_polygon_point_iterator )
|
|
{
|
|
#ifdef GEOMETRY_TEST_DEBUG
|
|
std::cout << "*** POLYGON ***" << std::endl;
|
|
#endif
|
|
|
|
typedef multi_point_type MP;
|
|
typedef polygon_type P;
|
|
|
|
typedef test_point_iterator_of_geometry<P, MP> tester;
|
|
|
|
tester::apply(from_wkt<P>("POLYGON()"),
|
|
from_wkt<MP>("MULTIPOINT()")
|
|
);
|
|
|
|
tester::apply(from_wkt<P>("POLYGON(())"),
|
|
from_wkt<MP>("MULTIPOINT()")
|
|
);
|
|
|
|
tester::apply(from_wkt<P>("POLYGON((3 3,4 4,5 5),(),(),(),\
|
|
(6 6,7 7,8 8),(),(),(9 9),())"),
|
|
from_wkt<MP>("MULTIPOINT(3 3,4 4,5 5,6 6,7 7,8 8,9 9)")
|
|
);
|
|
|
|
tester::apply(from_wkt<P>("POLYGON((),(3 3,4 4,5 5),(),(),\
|
|
(6 6,7 7,8 8),(),(),(9 9),())"),
|
|
from_wkt<MP>("MULTIPOINT(3 3,4 4,5 5,6 6,7 7,8 8,9 9)")
|
|
);
|
|
|
|
#ifdef GEOMETRY_TEST_DEBUG
|
|
std::cout << std::endl << std::endl;
|
|
#endif
|
|
}
|
|
|
|
|
|
//======================================================================
|
|
//======================================================================
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE( test_multipoint_point_iterator )
|
|
{
|
|
#ifdef GEOMETRY_TEST_DEBUG
|
|
std::cout << "*** MULTIPOINT ***" << std::endl;
|
|
#endif
|
|
|
|
typedef multi_point_type MP;
|
|
|
|
typedef test_point_iterator_of_geometry<MP, MP> tester;
|
|
|
|
tester::apply(from_wkt<MP>("MULTIPOINT()"),
|
|
from_wkt<MP>("MULTIPOINT()")
|
|
);
|
|
|
|
tester::apply(from_wkt<MP>("MULTIPOINT(3 3,4 4,5 5)"),
|
|
from_wkt<MP>("MULTIPOINT(3 3,4 4,5 5)")
|
|
);
|
|
|
|
#ifdef GEOMETRY_TEST_DEBUG
|
|
std::cout << std::endl << std::endl << std::endl;
|
|
#endif
|
|
}
|
|
|
|
|
|
//======================================================================
|
|
//======================================================================
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE( test_multilinestring_point_iterator )
|
|
{
|
|
#ifdef GEOMETRY_TEST_DEBUG
|
|
std::cout << "*** MULTILINESTRING ***" << std::endl;
|
|
#endif
|
|
|
|
typedef multi_point_type MP;
|
|
typedef linestring_type L;
|
|
typedef multi_linestring_type ML;
|
|
|
|
typedef test_point_iterator_of_geometry<ML, MP> tester;
|
|
|
|
tester::apply(from_wkt<ML>("MULTILINESTRING()"),
|
|
from_wkt<MP>("MULTIPOINT()")
|
|
);
|
|
|
|
tester::apply(from_wkt<ML>("MULTILINESTRING(())"),
|
|
from_wkt<MP>("MULTIPOINT()")
|
|
);
|
|
|
|
tester::apply(from_wkt<ML>("MULTILINESTRING((),(),())"),
|
|
from_wkt<MP>("MULTIPOINT()")
|
|
);
|
|
|
|
tester::apply(from_wkt<ML>("MULTILINESTRING((),(),(0 0,1 1,2 2),(),(),\
|
|
(3 3,4 4,5 5),(),(6 6),(),(),())"),
|
|
from_wkt<MP>("MULTIPOINT(0 0,1 1,2 2,3 3,4 4,5 5,6 6)")
|
|
);
|
|
|
|
#ifdef GEOMETRY_TEST_DEBUG
|
|
std::cout << std::endl << std::endl;
|
|
#endif
|
|
}
|
|
|
|
|
|
//======================================================================
|
|
//======================================================================
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE( test_multipolygon_point_iterator )
|
|
{
|
|
#ifdef GEOMETRY_TEST_DEBUG
|
|
std::cout << "*** MULTIPOLYGON ***" << std::endl;
|
|
#endif
|
|
|
|
typedef multi_point_type MP;
|
|
typedef multi_polygon_type MPL;
|
|
|
|
typedef test_point_iterator_of_geometry<MPL, MP> tester;
|
|
|
|
tester::apply(from_wkt<MPL>("MULTIPOLYGON()"),
|
|
from_wkt<MP>("MULTIPOINT()")
|
|
);
|
|
|
|
tester::apply(from_wkt<MPL>("MULTIPOLYGON( () )"),
|
|
from_wkt<MP>("MULTIPOINT()")
|
|
);
|
|
|
|
tester::apply(from_wkt<MPL>("MULTIPOLYGON( (()) )"),
|
|
from_wkt<MP>("MULTIPOINT()")
|
|
);
|
|
|
|
tester::apply(from_wkt<MPL>("MULTIPOLYGON( ((),()) )"),
|
|
from_wkt<MP>("MULTIPOINT()")
|
|
);
|
|
|
|
tester::apply(from_wkt<MPL>("MULTIPOLYGON(\
|
|
((3 3,4 4,5 5),(),(),(),\
|
|
(6 6,7 7,8 8),(),(),(9 9),())\
|
|
\
|
|
((),(1 1,2 2,10 10),(),(),(),\
|
|
(11 11,12 12),(),(),(13 13),())\
|
|
)"),
|
|
from_wkt<MP>("MULTIPOINT(3 3,4 4,5 5,6 6,7 7,8 8,9 9,\
|
|
1 1,2 2,10 10,11 11,12 12,13 13)")
|
|
);
|
|
|
|
tester::apply(from_wkt<MPL>("MULTIPOLYGON(\
|
|
(),\
|
|
\
|
|
((3 3,4 4,5 5),(),(),(),\
|
|
(6 6,7 7,8 8),(),(),(9 9),())\
|
|
\
|
|
((),(1 1,2 2,10 10),(),(),(),\
|
|
(11 11,12 12),(),(),(13 13),())\
|
|
\
|
|
((),(),()),\
|
|
)"),
|
|
from_wkt<MP>("MULTIPOINT(3 3,4 4,5 5,6 6,7 7,8 8,9 9,\
|
|
1 1,2 2,10 10,11 11,12 12,13 13)")
|
|
);
|
|
|
|
#ifdef GEOMETRY_TEST_DEBUG
|
|
std::cout << std::endl << std::endl;
|
|
#endif
|
|
}
|
|
|