Limbo 3.5.4
Loading...
Searching...
No Matches
Limbo.Geometry

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;
using namespace limbo::geometry;
struct Point
{
typedef int value_type;
value_type const& x() const {return m_x;}
value_type const& y() const {return m_y;}
void x(value_type v) {m_x = v;}
void y(value_type v) {m_y = v;}
//bool operator==(Point const& rhs) {return x() == rhs.x() && y() == rhs.y();}
};
namespace limbo { namespace geometry {
template <>
struct point_traits<Point>
{
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;
}
};
}}
struct Rectangle
{
typedef int value_type;
value_type const& xl() const {return m_xl;}
value_type const& yl() const {return m_yl;}
value_type const& xh() const {return m_xh;}
value_type const& yh() const {return m_yh;}
void xl(value_type v) {m_xl = v;}
void yl(value_type v) {m_yl = v;}
void xh(value_type v) {m_xh = v;}
void yh(value_type v) {m_yh = v;}
};
namespace limbo { namespace geometry {
template <>
struct rectangle_traits<Rectangle>
{
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;
Polygon2Rectangle<vector<Point>, vector<Rectangle> > p2r (vRect, HOR_VER_SLICING);
assert(p2r.read(filename));
assert(p2r());
p2r.print("p2r1.gp");
}
void test2(string const& filename)
{
vector<Rectangle> vRect;
Polygon2Rectangle<list<Point>, vector<Rectangle> > p2r (vRect, HOR_VER_SLICING);
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)
{
test1(argv[1]);
test2(argv[1]);
test3(argv[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
Definition Geometry.h:21
@ HOR_VER_SLICING
horizontal/vertical slicing and choose rectangle with larger area every time
Definition Geometry.h:43
namespace for Limbo
a custom point class
Definition test_p2r.cpp:23
int value_type
coordinate type
Definition test_p2r.cpp:25
value_type m_x
x coordinate
Definition test_p2r.cpp:27
value_type const & y() const
access y coordinate
Definition test_p2r.cpp:39
value_type const & x() const
access x coordinate
Definition test_p2r.cpp:34
value_type m_y
y coordinate
Definition test_p2r.cpp:29
a custom rectangle class
Definition test_p2r.cpp:111
value_type m_yl
bottom
Definition test_p2r.cpp:118
value_type m_yh
top
Definition test_p2r.cpp:122
value_type const & yh() const
access top coordinate
Definition test_p2r.cpp:143
int value_type
coordinate type
Definition test_p2r.cpp:113
value_type m_xh
right
Definition test_p2r.cpp:120
value_type const & yl() const
access bottom coordinate
Definition test_p2r.cpp:133
value_type const & xl() const
access left coordinate
Definition test_p2r.cpp:128
value_type const & xh() const
access right coordinate
Definition test_p2r.cpp:138
value_type m_xl
left
Definition test_p2r.cpp:116
type traits for point
Definition Geometry.h:196
static coordinate_type get(const point_type &point, orientation_2d const &orient)
get coordinate from point
Definition Geometry.h:206
static point_type construct(coordinate_type const &x, coordinate_type const &y)
construct point from coordinates
Definition Geometry.h:218
static void set(point_type &point, orientation_2d const &orient, coordinate_type const &value)
set coordinate for point
Definition Geometry.h:212
type traits for rectangle
Definition Geometry.h:227
static coordinate_type get(rectangle_type const &rectangle, direction_2d const &direct)
get coordinate from rectangle
Definition Geometry.h:237
static void set(rectangle_type &rectangle, direction_2d const &direct, coordinate_type const &v)
set coordinate for rectangle
Definition Geometry.h:243
static rectangle_type construct(coordinate_type const &xl, coordinate_type const &yl, coordinate_type const &xh, coordinate_type const &yh)
construct rectangle from coordinates
Definition Geometry.h:248
int main()
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;
namespace lg = limbo::geometry;
void test1(string const& filename)
{
std::vector<gtl::rectangle_data<int> > vRect;
lg::Polygon2Rectangle<vector<gtl::point_data<int> >, vector<gtl::rectangle_data<int> > > p2r (vRect, lg::HOR_VER_SLICING);
assert(p2r.read(filename));
assert(p2r());
p2r.print("p2r1.gp");
}
void test2(string const& filename)
{
std::vector<gtl::rectangle_data<int> > vRect;
lg::Polygon2Rectangle<list<gtl::point_data<int> >, vector<gtl::rectangle_data<int> > > p2r (vRect, lg::HOR_VER_SLICING);
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");
}
void test4()
{
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);
//vPoint.push_back(gtl::construct<gtl::point_data<int> >(0, 0));
vector<gtl::rectangle_data<int> > vRectangle;
assert(lg::polygon2RectangleBoost(vPoint, 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)
{
test4();
if (argc > 1)
{
test1(argv[1]);
test2(argv[1]);
test3(argv[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