...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
/*
Copyright 2008 Intel Corporation
Use, modification and distribution are 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).
*/
#include <boost/polygon/polygon.hpp>
#include <cassert>
namespace gtl = boost::polygon;
using namespace boost::polygon::operators;
//lets make the body of main from point_usage.cpp
//a generic function parameterized by point type
template <typename Point>
void test_point() {
Â
 //constructing a gtl point
 Â
int x = 10;
 Â
int y = 20;
 Â
//Point pt(x, y);
 Â
Point pt = gtl::construct<Point>(x, y);
 Â
assert(gtl::x(pt) == 10);
 Â
assert(gtl::y(pt) == 20);
 Â
 Â
//a quick primer in isotropic point access
 Â
typedef gtl::orientation_2d O;
 Â
using gtl::HORIZONTAL;
 Â
using gtl::VERTICAL;
 Â
O o = HORIZONTAL;
 Â
assert(gtl::x(pt) == gtl::get(pt, o));
 Â
 Â
o = o.get_perpendicular();
 Â
assert(o == VERTICAL);
 Â
assert(gtl::y(pt) == gtl::get(pt, o));
 Â
 Â
gtl::set(pt, o, 30);
 Â
assert(gtl::y(pt) == 30);
 Â
 Â
//using some of the library functions
 Â
//Point pt2(10, 30);
 Â
Point pt2 = gtl::construct<Point>(10, 30);
 Â
assert(gtl::equivalence(pt, pt2));
 Â
 Â
gtl::transformation<int> tr(gtl::axis_transformation::SWAP_XY);
 Â
gtl::transform(pt, tr);
 Â
assert(gtl::equivalence(pt, gtl::construct<Point>(30, 10)));
 Â
 Â
gtl::transformation<int> tr2 = tr.inverse();
 Â
assert(tr == tr2); //SWAP_XY is its own inverse transform
 Â
 Â
gtl::transform(pt, tr2);
 Â
assert(gtl::equivalence(pt, pt2)); //the two points are equal again
 Â
 Â
gtl::move(pt, o, 10); //move pt 10 units in y
 Â
assert(gtl::euclidean_distance(pt, pt2) == 10.0f);
 Â
 Â
gtl::move(pt, o.get_perpendicular(), 10); //move pt 10 units in x
 Â
assert(gtl::manhattan_distance(pt, pt2) == 20);
}
 Â
//Now lets declare our own point type
//Bjarne says that if a class doesn't maintain an
//invariant just use a struct.
struct CPoint {
 Â
int x;
 Â
int y;
};
 Â
//There, nice a simple...but wait, it doesn't do anything
//how do we use it to do all the things a point needs to do?
 Â
 Â
//First we register it as a point with boost polygon
namespace boost {
namespace polygon {
  Â
template <>
 Â
struct geometry_concept<CPoint> { typedef point_concept type; };
Â
 Â
   //Then we specialize the gtl point traits for our point type
 Â
template <>
 Â
struct point_traits<CPoint> {
 Â
 Â
typedef int coordinate_type;
 Â
 Â
 Â
static inline coordinate_type get(const CPoint& point,
 Â
 Â
orientation_2d orient) {
 Â
 Â
 Â
if(orient == HORIZONTAL)
 Â
 Â
 Â
 Â
return point.x;
 Â
 Â
 Â
return point.y;
 Â
 Â
}
 Â
};
 Â
 Â
template <>
 Â
struct point_mutable_traits<CPoint> {
       typedef int coordinate_type;
 Â
 Â
static inline void set(CPoint& point, orientation_2d orient, int value) {
 Â
 Â
 Â
if(orient == HORIZONTAL)
 Â
 Â
 Â
 Â
point.x = value;
 Â
 Â
 Â
else
 Â
 Â
 Â
point.y = value;
 Â
 Â
}
 Â
 Â
static inline CPoint construct(int x_value, int y_value) {
 Â
 Â
 Â
CPoint retval;
 Â
 Â
 Â
retval.x = x_value;
 Â
 Â
 Â
retval.y = y_value;
 Â
 Â
 Â
return retval;
 Â
 Â
}
 Â
};
} }
 Â
//Now lets see if the CPoint works with the library functions
int main() {
 Â
test_point<CPoint>(); //yay! All your testing is done for you.
 Â
return 0;
}
 Â
//Now you know how to map a user type to the library point concept
//and how to write a generic function parameterized by point type
//using the library interfaces to access it.
 Â
Â
Copyright: | Copyright © Intel Corporation 2008-2010. |
---|---|
License: | Distributed under 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) |