Introduction
Geometry package provides API to adapt various geometry data structures such as Boost.Polygon and Boost.Geometry and various specialized geomtric algorithms such as computing area of polygons, conversion of rectilinear polygon to rectangles, etc. It is developed in a generic way to be flexible to different data types either in STL or Boost.
Examples
Custom Geometric Classes
Define custom classes for points and rectangles and use Polygon2Rectangle to convert polygon to rectangles.
See documented version: test/geometry/test_p2r.cpp
#include <iostream>
#include <vector>
#include <list>
#include <set>
using std::cout;
using std::endl;
using std::vector;
using std::list;
using std::set;
{
};
template <>
{
typedef Point point_type;
typedef int coordinate_type;
static coordinate_type
get(
const point_type& point, orientation_2d orient)
{
if (orient == HORIZONTAL) return point.x();
else if (orient == VERTICAL) return point.y();
else assert(0);
}
static void set(point_type& point, orientation_2d orient, coordinate_type value)
{
if (orient == HORIZONTAL) return point.x(value);
else if (orient == VERTICAL) return point.y(value);
else assert(0);
}
static point_type
construct(coordinate_type x, coordinate_type y)
{
point_type p;
p.x(x); p.y(y);
return p;
}
};
}}
{
};
template <>
{
typedef Rectangle rectangle_type;
typedef int coordinate_type;
static coordinate_type
get(
const rectangle_type& rect, direction_2d dir)
{
switch (dir)
{
case LEFT: return rect.xl();
case BOTTOM: return rect.yl();
case RIGHT: return rect.xh();
case TOP: return rect.yh();
default: assert(0);
}
}
static void set(rectangle_type& rect, direction_2d dir, coordinate_type value)
{
switch (dir)
{
case LEFT: rect.xl(value); break;
case BOTTOM: rect.yl(value); break;
case RIGHT: rect.xh(value); break;
case TOP: rect.yh(value); break;
default: assert(0);
}
}
static rectangle_type
construct(coordinate_type xl, coordinate_type yl, coordinate_type xh, coordinate_type yh)
{
rectangle_type rect;
rect.xl(xl); rect.yl(yl);
rect.xh(xh); rect.yh(yh);
return rect;
}
};
}}
void test1(
string const& filename)
{
vector<Rectangle> vRect;
assert(p2r.read(filename));
assert(p2r());
p2r.print("p2r1.gp");
}
void test2(
string const& filename)
{
vector<Rectangle> vRect;
assert(p2r.read(filename));
assert(p2r());
p2r.print("p2r2.gp");
}
void test3(
string const& filename)
{
vector<Rectangle> vRect;
assert(p2r.read(filename));
assert(p2r());
p2r.print("p2r3.gp");
}
int main(
int argc,
char** argv)
{
if (argc > 1)
{
}
else cout << "at least 1 argument is required" << endl;
return 0;
}
a generic implementation of polygon-to-rectangle conversion
a class implement conversion from manhattan polygon to rectangle
namespace for Limbo.Geometry
@ HOR_VER_SLICING
horizontal/vertical slicing and choose rectangle with larger area every time
int value_type
coordinate type
value_type m_x
x coordinate
value_type const & y() const
access y coordinate
value_type const & x() const
access x coordinate
value_type m_y
y coordinate
value_type const & yh() const
access top coordinate
int value_type
coordinate type
value_type const & yl() const
access bottom coordinate
value_type const & xl() const
access left coordinate
value_type const & xh() const
access right coordinate
static coordinate_type get(const point_type &point, orientation_2d const &orient)
get coordinate from point
static point_type construct(coordinate_type const &x, coordinate_type const &y)
construct point from coordinates
static void set(point_type &point, orientation_2d const &orient, coordinate_type const &value)
set coordinate for point
type traits for rectangle
static coordinate_type get(rectangle_type const &rectangle, direction_2d const &direct)
get coordinate from rectangle
static void set(rectangle_type &rectangle, direction_2d const &direct, coordinate_type const &v)
set coordinate for rectangle
static rectangle_type construct(coordinate_type const &xl, coordinate_type const &yl, coordinate_type const &xh, coordinate_type const &yh)
construct rectangle from coordinates
void test2(string const &filename)
test std::list
void test3(string const &filename)
test std::set
void test1()
test function API
Compiling and running commands (assuming LIMBO_DIR is valid and limbo library has been properly installed)
g++ -o test_p2r test_p2r.cpp -I $LIMBO_DIR/include
./test_p2r benchmarks/polygon1.gp
Output drawn with gnuplot
Boost.Polygon API
Use point and rectangle types in Boost.Polygon.
See documented version: test/geometry/test_boostpolygonapi.cpp
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <boost/polygon/polygon.hpp>
using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::list;
using std::set;
namespace gtl = boost::polygon;
void test1(
string const& filename)
{
std::vector<gtl::rectangle_data<int> > vRect;
assert(p2r.read(filename));
assert(p2r());
p2r.print("p2r1.gp");
}
void test2(
string const& filename)
{
std::vector<gtl::rectangle_data<int> > vRect;
assert(p2r.read(filename));
assert(p2r());
p2r.print("p2r2.gp");
}
void test3(
string const& filename)
{
std::vector<gtl::rectangle_data<int> > vRect;
assert(p2r.read(filename));
assert(p2r());
p2r.print("p2r3.gp");
}
{
vector<gtl::point_data<int> > vPoint (8);
vPoint[0] = gtl::construct<gtl::point_data<int> >(0, 0);
vPoint[1] = gtl::construct<gtl::point_data<int> >(0, 10);
vPoint[2] = gtl::construct<gtl::point_data<int> >(10, 10);
vPoint[3] = gtl::construct<gtl::point_data<int> >(10, 20);
vPoint[4] = gtl::construct<gtl::point_data<int> >(20, 20);
vPoint[5] = gtl::construct<gtl::point_data<int> >(20, 10);
vPoint[6] = gtl::construct<gtl::point_data<int> >(30, 10);
vPoint[7] = gtl::construct<gtl::point_data<int> >(30, 0);
vector<gtl::rectangle_data<int> > vRectangle;
for (std::size_t i = 0; i != vRectangle.size(); ++i)
{
gtl::rectangle_data<int> const& rect = vRectangle[i];
cout << "(" << gtl::xl(rect) << ", " << gtl::yl(rect) << ", " << gtl::xh(rect) << ", " << gtl::yh(rect) << ")\n";
}
cout << "test 4 passed\n";
}
int main(
int argc,
char** argv)
{
if (argc > 1)
{
}
else cout << "at least 1 argument is required" << endl;
return 0;
}
Geometry traits for Boost.Polygon, include this file when you use Boost.Polygon geometric types.
this file extracts polygon-to-rectangle conversion for Boost.Polygon API.
bool polygon2RectangleBoost(std::vector< gtl::point_data< int > > const &vPoint, std::vector< gtl::rectangle_data< int > > &vRectangle)
this function takes a set of points describing a rectilinear polygon and decomposes it into rectangle...
sort helper if orient == HORIZONTAL, sort by left lower if orient == VERTICAL, sort by lower left
void test4()
test polygon-to-rectangle for array of points
Compiling and running commands (assuming LIMBO_DIR is valid and limbo library has been properly installed)
g++ -o test_boostpolygonapi test_boostpolygonapi.cpp -I $LIMBO_DIR/include -I $BOOST_DIR/include
./test_boostpolygonapi benchmarks/polygon2.gp
Output drawn with gnuplot
All Examples
Possible dependencies: Boost.
References