Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,22 @@ int main() {
mapbox::util::apply_visitor(visitor,geom);
}
```

Geometry types with a custom point type:

```cpp
#include <mapbox/geometry/geometry.hpp>

// geometry.hpp provides a `geometry_t` template that supports any point type with a
// `coordinate_type` member.
struct my_point {
using coordinate_type = double;
double x;
double y;
double elevation;
};

using my_geometry = mapbox::geometry::geometry_t<my_point>;
using my_line_string = my_geometry::line_string_type;
using my_polygon = my_geometry::polygon_type;
// etc.
58 changes: 36 additions & 22 deletions include/mapbox/geometry/geometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,52 @@

namespace mapbox { namespace geometry {

template <typename T, template <typename...> class Cont = std::vector>
struct geometry_collection;

template <typename T>
using geometry_base = mapbox::util::variant<point<T>,
line_string<T>,
polygon<T>,
multi_point<T>,
multi_line_string<T>,
multi_polygon<T>,
geometry_collection<T>>;

template <typename T>
struct geometry : geometry_base<T>
template <typename Point, template <typename...> class Cont = std::vector>
struct geometry_collection_t;

template <typename Point>
using geometry_base_t = mapbox::util::variant<Point,
line_string_t<Point>,
polygon_t<Point>,
multi_point_t<Point>,
multi_line_string_t<Point>,
multi_polygon_t<Point>,
geometry_collection_t<Point>>;

template <typename Point>
struct geometry_t : geometry_base_t<Point>
{
using coordinate_type = T;
using geometry_base<T>::geometry_base;
using point_type = Point;
using line_string_type = line_string_t<Point>;
using polygon_type = polygon_t<Point>;
using multi_point_type = multi_point_t<Point>;
using multi_line_string_type = multi_line_string_t<Point>;
using multi_polygon_type = multi_polygon_t<Point>;
using geometry_collection_type = geometry_collection_t<Point>;
using coordinate_type = typename point_type::coordinate_type;
using geometry_base_t<Point>::geometry_base_t;

/*
* The default constructor would create a point geometry with default-constructed coordinates;
* i.e. (0, 0). Since this is not particularly useful, and could hide bugs, it is disabled.
*/
geometry() = delete;
geometry_t() = delete;
};

template <typename T, template <typename...> class Cont>
struct geometry_collection : Cont<geometry<T>>
template <class T>
using geometry = geometry_t<point<T>>;

template <typename Point, template <typename...> class Container>
struct geometry_collection_t : Container<geometry_t<Point>>
{
using coordinate_type = T;
using geometry_type = geometry<T>;
using container_type = Cont<geometry_type>;
using point_type = Point;
using coordinate_type = typename point_type::coordinate_type;
using geometry_type = geometry_t<Point>;
using container_type = Container<geometry_type>;
using container_type::container_type;
};

template <class T, template <typename...> class Container = std::vector>
using geometry_collection = geometry_collection_t<point<T>, Container>;

}}
13 changes: 8 additions & 5 deletions include/mapbox/geometry/line_string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@

namespace mapbox { namespace geometry {

template <typename T, template <typename...> class Cont = std::vector>
struct line_string : Cont<point<T> >
template <class Point, template <typename...> class Container = std::vector>
struct line_string_t : Container<Point>
{
using coordinate_type = T;
using point_type = point<T>;
using container_type = Cont<point_type>;
using point_type = Point;
using coordinate_type = typename point_type::coordinate_type;
using container_type = Container<point_type>;
using container_type::container_type;
};

template <class T, template <typename...> class Container = std::vector>
using line_string = line_string_t<point<T>, Container>;

}}
14 changes: 9 additions & 5 deletions include/mapbox/geometry/multi_line_string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@

namespace mapbox { namespace geometry {

template <typename T, template <typename...> class Cont = std::vector>
struct multi_line_string : Cont<line_string<T>>
template <typename Point, template <typename...> class Container = std::vector>
struct multi_line_string_t : Container<line_string_t<Point>>
{
using coordinate_type = T;
using line_string_type = line_string<T>;
using container_type = Cont<line_string_type>;
using point_type = Point;
using coordinate_type = typename point_type::coordinate_type;
using line_string_type = line_string_t<point_type>;
using container_type = Container<line_string_type>;
using container_type::container_type;
};

template <class T, template <typename...> class Container = std::vector>
using multi_line_string = multi_line_string_t<point<T>, Container>;

}}
13 changes: 8 additions & 5 deletions include/mapbox/geometry/multi_point.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@

namespace mapbox { namespace geometry {

template <typename T, template <typename...> class Cont = std::vector>
struct multi_point : Cont<point<T>>
template <class Point, template <typename...> class Container = std::vector>
struct multi_point_t : Container<Point>
{
using coordinate_type = T;
using point_type = point<T>;
using container_type = Cont<point_type>;
using point_type = Point;
using coordinate_type = typename point_type::coordinate_type;
using container_type = Container<point_type>;
using container_type::container_type;
};

template <class T, template <typename...> class Container = std::vector>
using multi_point = multi_point_t<point<T>, Container>;

}}
14 changes: 9 additions & 5 deletions include/mapbox/geometry/multi_polygon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@

namespace mapbox { namespace geometry {

template <typename T, template <typename...> class Cont = std::vector>
struct multi_polygon : Cont<polygon<T>>
template <typename Point, template <typename...> class Container = std::vector>
struct multi_polygon_t : Container<polygon_t<Point>>
{
using coordinate_type = T;
using polygon_type = polygon<T>;
using container_type = Cont<polygon_type>;
using point_type = Point;
using coordinate_type = typename point_type::coordinate_type;
using polygon_type = polygon_t<Point>;
using container_type = Container<polygon_type>;
using container_type::container_type;
};

template <class T, template <typename...> class Container = std::vector>
using multi_polygon = multi_polygon_t<point<T>, Container>;

}}
24 changes: 14 additions & 10 deletions include/mapbox/geometry/polygon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,26 @@

namespace mapbox { namespace geometry {

template <typename T, template <typename...> class Cont = std::vector>
struct linear_ring : Cont<point<T>>
template <typename Point, template <typename...> class Container = std::vector>
struct linear_ring_t : Container<Point>
{
using coordinate_type = T;
using point_type = point<T>;
using container_type = Cont<point_type>;
using point_type = Point;
using coordinate_type = typename point_type::coordinate_type;
using container_type = Container<point_type>;
using container_type::container_type;
};

template <typename T, template <typename...> class Cont = std::vector>
struct polygon : Cont<linear_ring<T>>
template <typename Point, template <typename...> class Container = std::vector>
struct polygon_t : Container<linear_ring_t<Point>>
{
using coordinate_type = T;
using linear_ring_type = linear_ring<T>;
using container_type = Cont<linear_ring_type>;
using point_type = Point;
using coordinate_type = typename point_type::coordinate_type;
using linear_ring_type = linear_ring_t<Point>;
using container_type = Container<linear_ring_type>;
using container_type::container_type;
};

template <class T, template <typename...> class Container = std::vector>
using polygon = polygon_t<point<T>, Container>;

}}
16 changes: 16 additions & 0 deletions tests/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,21 @@ static void testEnvelope() {
assert(envelope(geometry_collection<int>({point<int>(0, 0)})) == box<int>({0, 0}, {0, 0}));
}

static void testCustomPointType() {
struct point_3d {
using coordinate_type = double;
double x;
double y;
double z;
};

using custom_geometry = geometry_t<point_3d>;
using custom_polygon = custom_geometry::polygon_type;

custom_geometry g { custom_polygon({{{0, 1, 2}, {3, 4, 5}}}) };
assert(g.get<custom_polygon>()[0][0].x == 0);
}

int main() {
testPoint();
testMultiPoint();
Expand All @@ -207,5 +222,6 @@ int main() {

testForEachPoint();
testEnvelope();
testCustomPointType();
return 0;
}