Limbo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Geometry.h
Go to the documentation of this file.
1 
9 #ifndef _LIMBO_GEOMETRY_GEOMETRY_H
10 #define _LIMBO_GEOMETRY_GEOMETRY_H
11 
12 #include <vector>
13 #include <list>
14 #include <ostream>
15 
17 namespace limbo
18 {
20 namespace geometry
21 {
22 
23 using std::cout;
24 using std::endl;
25 using std::ifstream;
26 using std::ofstream;
27 using std::string;
28 
32 {
33  HORIZONTAL = 0,
34  VERTICAL = 1
35 };
36 
40 {
41  HORIZONTAL_SLICING = 1,
42  VERTICAL_SLICING = 2,
46 };
47 
51 inline std::string to_string(slicing_orientation_2d slicing_orient)
52 {
53  switch (slicing_orient)
54  {
55  case HORIZONTAL_SLICING:
56  return "HORIZONTAL_SLICING";
57  case VERTICAL_SLICING:
58  return "VERTICAL_SLICING";
59  case HOR_VER_SLICING:
60  return "HOR_VER_SLICING";
61  case HOR_VER_SA_SLICING:
62  return "HOR_VER_SA_SLICING";
63  case HOR_VER_AR_SLICING:
64  return "HOR_VER_AR_SLICING";
65  default:
66  return "UNKNOWN";
67  }
68 }
69 
73 {
74  LEFT = 0,
75  BOTTOM = 1,
76  RIGHT = 2,
77  TOP = 3
78 };
79 
84 {
85  CLOCKWISE = 0,
86  COUNTERCLOCKWISE = 1,
87  UNKNOWN_WINDING = 2
88 };
89 
93 {
94  public:
100  : m_orient(ori) {}
103  inline orientation_2d(const orientation_2d& ori) : m_orient(ori.m_orient) {}
108  {m_orient = ori.m_orient; return * this; }
112  inline bool operator==(orientation_2d const& rhs) const { return (m_orient == rhs.m_orient); }
116  inline bool operator!=(orientation_2d const& rhs) const { return (m_orient != rhs.m_orient); }
119  inline int to_int() const { return m_orient; }
121  inline void turn_90() { m_orient = m_orient^ 1; }
125  {
126  orientation_2d retval = *this;
127  retval.turn_90();
128  return retval;
129  }
132  inline bool valid() const {return m_orient == 0 || m_orient == 1;}
137  friend bool operator<(orientation_2d const& ori1, orientation_2d const& ori2)
138  {return ori1.m_orient < ori2.m_orient;}
139  protected:
140  int m_orient;
141 };
142 
145 template <typename T>
147 
149 template <>
150 struct coordinate_traits<int>
151 {
153  typedef int coordinate_type;
154  typedef long area_type;
155 
156  typedef long manhattan_area_type;
157  typedef unsigned long unsigned_area_type;
158  typedef long coordinate_difference;
159 
160  typedef long coordinate_distance;
162 };
163 
167 template <typename PointType>
169 {
171  typedef PointType point_type;
172  typedef typename point_type::coordinate_type coordinate_type;
174 
179  static coordinate_type get(const point_type& point, orientation_2d const& orient)
180  {return point.get(orient);}
185  static void set(point_type& point, orientation_2d const& orient, coordinate_type const& value)
186  {point.set(orient, value);}
191  static point_type construct(coordinate_type const& x, coordinate_type const& y)
192  {return point_type(x, y);}
193 };
194 
198 template <typename RectType>
200 {
202  typedef RectType rectangle_type;
203  typedef typename rectangle_type::coordinate_type coordinate_type;
205 
210  static inline coordinate_type get(rectangle_type const& rectangle, direction_2d const& direct)
211  {return rectangle.get(direct);}
216  static inline void set(rectangle_type& rectangle, direction_2d const& direct, coordinate_type const& v)
217  {rectangle.set(direct, v);}
221  static inline rectangle_type construct(coordinate_type const& xl, coordinate_type const& yl,
222  coordinate_type const& xh, coordinate_type const& yh)
223  {return rectangle_type(xl, yl, xh, yh);}
224 };
225 
226 #if 0
227 template <typename PolygonType>
229 struct polygon_90_traits
230 {
231  typedef PolygonType polygon_type;
232  typedef typename polygon_type::point_type point_type;
233  typedef typename point_type::coordinate_type coordinate_type;
234  typedef typename polygon_type::vertex_iterator_type vertex_iterator_type;
235 
236  // Get the begin iterator
237  static inline vertex_iterator_type begin_vertex(polygon_type const& p)
238  {return p.begin_vertex();}
239 
240  // Get the end iterator
241  static inline vertex_iterator_type end_vertex(polygon_type const& p)
242  {return p.end_vertex();}
243 
244  // Get the winding direction of the polygon
245  static inline winding_direction winding(polygon_type const&)
246  {return UNKNOWN_WINDING;}
247 };
248 #endif
249 
253 template <typename ContainerType>
255 {
257  typedef ContainerType container_type;
258  typedef typename container_type::value_type value_type;
259  typedef typename container_type::const_iterator const_iterator_type;
260  typedef typename container_type::iterator iterator_type;
262 
264  static void insert(container_type& container, value_type const& v) {container.insert(v);}
266  static void insert(container_type& container, iterator_type it, value_type const& v) {container.insert(it, v);}
268  static void erase(container_type& container, value_type const& v) {container.erase(v);}
270  static void erase(container_type& container, iterator_type it) {container.erase(it);}
272  template <typename PointCompareType>
273  static container_type construct(PointCompareType const& comp)
274  {return container_type(comp);}
275 };
276 
279 template <typename T>
280 struct container_traits<std::vector<T> >
281 {
283  typedef std::vector<T> container_type;
284  typedef typename container_type::value_type value_type;
285  typedef typename container_type::const_iterator const_iterator_type;
286  typedef typename container_type::iterator iterator_type;
288 
290  static void insert(container_type& container, value_type const& v) {container.push_back(v);}
292  static void insert(container_type& container, iterator_type it, value_type const& v) {container.insert(it, v);}
294  static void erase(container_type& container, iterator_type it) {container.erase(it);}
297  template <typename PointCompareType>
298  static container_type construct(PointCompareType const&)
299  {return container_type();}
300 };
303 template <typename T>
304 struct container_traits<std::list<T> >
305 {
307  typedef std::list<T> container_type;
308  typedef typename container_type::value_type value_type;
309  typedef typename container_type::const_iterator const_iterator_type;
310  typedef typename container_type::iterator iterator_type;
312 
314  static void insert(container_type& container, value_type const& v) {container.push_back(v);}
316  static void insert(container_type& container, iterator_type it, value_type const& v) {container.insert(it, v);}
318  static void erase(container_type& container, iterator_type it) {container.erase(it);}
321  template <typename PointCompareType>
322  static container_type construct(PointCompareType const&)
323  {return container_type();}
324 };
325 
328 template <typename PointSet>
329 inline coordinate_traits<typename point_traits<typename container_traits<PointSet>::value_type>::area_type>
330 area(PointSet const& vPoint)
331 {
332  typedef typename container_traits<PointSet>::const_iterator_type const_iterator_type;
333  typedef typename container_traits<PointSet>::value_type point_type;
334  typedef typename point_traits<point_type>::coordinate_type coordinate_type;
335  typedef typename coordinate_traits<coordinate_type>::area_type area_type;
336 
337  area_type a = 0;
338  for (const_iterator_type itCur = vPoint.begin(); itCur != vPoint.end(); ++itCur)
339  {
340  const_iterator_type itNext = itCur;
341  ++itNext;
342  if (itNext == vPoint.end()) itNext = vPoint.begin();
343 
344  a += (area_type)(point_traits<point_type>::x(*itNext) - point_traits<point_type>::x(*itCur)) *
345  (area_type)(point_traits<point_type>::y(*itNext) + point_traits<point_type>::y(*itCur));
346  }
347  return a/2;
348 }
349 
350 } // namespace geometry
351 } // namespace limbo
352 
353 #endif
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:221
static coordinate_type get(const point_type &point, orientation_2d const &orient)
get coordinate from point
Definition: Geometry.h:179
int to_int() const
convert orientation to integer
Definition: Geometry.h:119
static point_type construct(coordinate_type const &x, coordinate_type const &y)
construct point from coordinates
Definition: Geometry.h:191
static void set(rectangle_type &rectangle, direction_2d const &direct, coordinate_type const &v)
set coordinate for rectangle
Definition: Geometry.h:216
void turn_90()
turn 90 degree
Definition: Geometry.h:121
int m_orient
enum type of orientation in integer representation
Definition: Geometry.h:140
bool valid() const
check whether the orientation is valid
Definition: Geometry.h:132
static void insert(container_type &container, value_type const &v)
insert value to container
Definition: Geometry.h:290
static void set(point_type &point, orientation_2d const &orient, coordinate_type const &value)
set coordinate for point
Definition: Geometry.h:185
static void erase(container_type &container, iterator_type it)
erase an element from container with iterator
Definition: Geometry.h:270
direction_2d
direction type for rectangles
Definition: Geometry.h:72
horizontal/vertical slicing and choose rectangle with smaller area every time
Definition: Geometry.h:44
orientation_2d_enum
orientation type for lines
Definition: Geometry.h:31
static void insert(container_type &container, iterator_type it, value_type const &v)
insert value to container with hint of iterator
Definition: Geometry.h:292
static container_type construct(PointCompareType const &comp)
a default construct function is necessary, because std::set needs a compare function, but vector does not
Definition: Geometry.h:273
bool operator!=(orientation_2d const &rhs) const
inequality operator
Definition: Geometry.h:116
orientation_2d()
constructor
Definition: Geometry.h:96
bool operator==(orientation_2d const &rhs) const
equality operator
Definition: Geometry.h:112
slicing_orientation_2d
orientation type for slicing
Definition: Geometry.h:39
type traits for containers such as vector, list, multiset
Definition: Geometry.h:254
static void erase(container_type &container, iterator_type it)
erase an element from container with iterator
Definition: Geometry.h:294
winding_direction
winding direction type CLOCKWISE and COUNTERCLOCKWISE refers to winding direction of a polygon ...
Definition: Geometry.h:83
coordinate_traits< typename point_traits< typename container_traits< PointSet >::value_type >::area_type > area(PointSet const &vPoint)
calculate signed area of a polygon, the result is positive if its winding is CLOCKWISE ...
Definition: Geometry.h:330
static container_type construct(PointCompareType const &)
a default construct function
Definition: Geometry.h:298
static coordinate_type get(rectangle_type const &rectangle, direction_2d const &direct)
get coordinate from rectangle
Definition: Geometry.h:210
horizontal/vertical slicing and choose rectangle with better aspect ratio every time ...
Definition: Geometry.h:45
std::string to_string(slicing_orientation_2d slicing_orient)
convert enum type of slicing orientation to string
Definition: Geometry.h:51
orientation_2d get_perpendicular() const
get perpendicular orientation
Definition: Geometry.h:124
orientation_2d(const orientation_2d &ori)
copy constructor
Definition: Geometry.h:103
type traits for rectangle
Definition: Geometry.h:199
static void insert(container_type &container, iterator_type it, value_type const &v)
insert value to container with hint of iterator
Definition: Geometry.h:316
namespace for Limbo
Definition: GraphUtility.h:22
friend bool operator<(orientation_2d const &ori1, orientation_2d const &ori2)
less than operator
Definition: Geometry.h:137
type traits for coordinates
Definition: Geometry.h:146
static container_type construct(PointCompareType const &)
a default construct function
Definition: Geometry.h:322
static void erase(container_type &container, iterator_type it)
erase an element from container with iterator
Definition: Geometry.h:318
orientation_2d(orientation_2d_enum const &ori)
constructor
Definition: Geometry.h:99
static void insert(container_type &container, value_type const &v)
insert value to container
Definition: Geometry.h:264
static void insert(container_type &container, value_type const &v)
endnowarn
Definition: Geometry.h:314
horizontal/vertical slicing and choose rectangle with larger area every time
Definition: Geometry.h:43
type traits for point
Definition: Geometry.h:168
static void erase(container_type &container, value_type const &v)
erase value from iterator
Definition: Geometry.h:268
orientation_2d & operator=(const orientation_2d &ori)
assignment
Definition: Geometry.h:107
a class denoting orientation of lines
Definition: Geometry.h:92
static void insert(container_type &container, iterator_type it, value_type const &v)
insert value to container with hint of iterator
Definition: Geometry.h:266