Limbo 3.5.4
Loading...
Searching...
No Matches
Geometry.h
Go to the documentation of this file.
1
8
9#ifndef _LIMBO_GEOMETRY_GEOMETRY_H
10#define _LIMBO_GEOMETRY_GEOMETRY_H
11
12#include <vector>
13#include <list>
14#include <ostream>
15
17namespace limbo
18{
20namespace geometry
21{
22
23using std::cout;
24using std::endl;
25using std::ifstream;
26using std::ofstream;
27using std::string;
28
31enum orientation_2d_enum
32{
33 HORIZONTAL = 0,
34 VERTICAL = 1
35};
36
40{
41 HORIZONTAL_SLICING = 1,
42 VERTICAL_SLICING = 2,
46};
47
51inline 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";
62 return "HOR_VER_SA_SLICING";
64 return "HOR_VER_AR_SLICING";
65 default:
66 return "UNKNOWN";
67 }
68}
69
72enum direction_2d
73{
74 LEFT = 0,
75 BOTTOM = 1,
76 RIGHT = 2,
77 TOP = 3
78};
79
83enum winding_direction
84{
85 CLOCKWISE = 0,
86 COUNTERCLOCKWISE = 1,
87 UNKNOWN_WINDING = 2
88};
89
93{
94 public:
99 orientation_2d(orientation_2d_enum const& ori)
100 : m_orient(ori) {}
101
103 inline orientation_2d(const orientation_2d& ori) : m_orient(ori.m_orient) {}
108 {m_orient = ori.m_orient; return * this; }
109
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 }
130
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:
141};
142
145template <typename T>
147
149template <>
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
165#define LIMBO_GEOMETRY_COORDINATE_TRAITS(T, AREA_T, UNSIGNED_AREA_T) \
166template <> \
167struct coordinate_traits<T> \
168{ \
169 typedef T coordinate_type; \
170 typedef AREA_T area_type; \
171 typedef AREA_T manhattan_area_type; \
172 typedef UNSIGNED_AREA_T unsigned_area_type; \
173 typedef AREA_T coordinate_difference; \
174 typedef AREA_T coordinate_distance; \
175};
176
177#ifdef __SIZEOF_INT128__
178LIMBO_GEOMETRY_COORDINATE_TRAITS(long, __int128_t, __uint128_t)
179LIMBO_GEOMETRY_COORDINATE_TRAITS(unsigned long, __int128_t, __uint128_t)
180LIMBO_GEOMETRY_COORDINATE_TRAITS(long long, __int128_t, __uint128_t)
181LIMBO_GEOMETRY_COORDINATE_TRAITS(unsigned long long, __int128_t, __uint128_t)
182#else
183LIMBO_GEOMETRY_COORDINATE_TRAITS(long, long long, unsigned long long)
184LIMBO_GEOMETRY_COORDINATE_TRAITS(unsigned long, long long, unsigned long long)
185LIMBO_GEOMETRY_COORDINATE_TRAITS(long long, long long, unsigned long long)
186LIMBO_GEOMETRY_COORDINATE_TRAITS(unsigned long long, long long, unsigned long long)
187#endif
188
189#undef LIMBO_GEOMETRY_COORDINATE_TRAITS
190
194template <typename PointType>
196{
198 typedef PointType point_type;
199 typedef typename point_type::coordinate_type coordinate_type;
201
206 static coordinate_type get(const point_type& point, orientation_2d const& orient)
207 {return point.get(orient);}
208
212 static void set(point_type& point, orientation_2d const& orient, coordinate_type const& value)
213 {point.set(orient, value);}
214
218 static point_type construct(coordinate_type const& x, coordinate_type const& y)
219 {return point_type(x, y);}
220};
221
225template <typename RectType>
227{
229 typedef RectType rectangle_type;
230 typedef typename rectangle_type::coordinate_type coordinate_type;
232
237 static inline coordinate_type get(rectangle_type const& rectangle, direction_2d const& direct)
238 {return rectangle.get(direct);}
239
243 static inline void set(rectangle_type& rectangle, direction_2d const& direct, coordinate_type const& v)
244 {rectangle.set(direct, v);}
245
248 static inline rectangle_type construct(coordinate_type const& xl, coordinate_type const& yl,
249 coordinate_type const& xh, coordinate_type const& yh)
250 {return rectangle_type(xl, yl, xh, yh);}
251};
252
253#if 0
255template <typename PolygonType>
256struct polygon_90_traits
257{
258 typedef PolygonType polygon_type;
259 typedef typename polygon_type::point_type point_type;
260 typedef typename point_type::coordinate_type coordinate_type;
261 typedef typename polygon_type::vertex_iterator_type vertex_iterator_type;
262
263 // Get the begin iterator
264 static inline vertex_iterator_type begin_vertex(polygon_type const& p)
265 {return p.begin_vertex();}
266
267 // Get the end iterator
268 static inline vertex_iterator_type end_vertex(polygon_type const& p)
269 {return p.end_vertex();}
270
271 // Get the winding direction of the polygon
272 static inline winding_direction winding(polygon_type const&)
273 {return UNKNOWN_WINDING;}
274};
275#endif
276
280template <typename ContainerType>
282{
284 typedef ContainerType container_type;
285 typedef typename container_type::value_type value_type;
286 typedef typename container_type::const_iterator const_iterator_type;
287 typedef typename container_type::iterator iterator_type;
289
291 static void insert(container_type& container, value_type const& v) {container.insert(v);}
293 static void insert(container_type& container, iterator_type it, value_type const& v) {container.insert(it, v);}
295 static void erase(container_type& container, value_type const& v) {container.erase(v);}
297 static void erase(container_type& container, iterator_type it) {container.erase(it);}
299 template <typename PointCompareType>
300 static container_type construct(PointCompareType const& comp)
301 {return container_type(comp);}
302};
303
306template <typename T>
307struct container_traits<std::vector<T> >
308{
310 typedef std::vector<T> container_type;
311 typedef typename container_type::value_type value_type;
312 typedef typename container_type::const_iterator const_iterator_type;
313 typedef typename container_type::iterator iterator_type;
315
317 static void insert(container_type& container, value_type const& v) {container.push_back(v);}
319 static void insert(container_type& container, iterator_type it, value_type const& v) {container.insert(it, v);}
321 static void erase(container_type& container, iterator_type it) {container.erase(it);}
324 template <typename PointCompareType>
325 static container_type construct(PointCompareType const&)
326 {return container_type();}
327};
328
330template <typename T>
331struct container_traits<std::list<T> >
332{
334 typedef std::list<T> container_type;
335 typedef typename container_type::value_type value_type;
336 typedef typename container_type::const_iterator const_iterator_type;
337 typedef typename container_type::iterator iterator_type;
339
341 static void insert(container_type& container, value_type const& v) {container.push_back(v);}
343 static void insert(container_type& container, iterator_type it, value_type const& v) {container.insert(it, v);}
345 static void erase(container_type& container, iterator_type it) {container.erase(it);}
348 template <typename PointCompareType>
349 static container_type construct(PointCompareType const&)
350 {return container_type();}
351};
352
355template <typename PointSet>
356inline typename coordinate_traits<typename point_traits<typename container_traits<PointSet>::value_type>::coordinate_type>::area_type
357area(PointSet const& vPoint)
358{
359 typedef typename container_traits<PointSet>::const_iterator_type const_iterator_type;
360 typedef typename container_traits<PointSet>::value_type point_type;
361 typedef typename point_traits<point_type>::coordinate_type coordinate_type;
362 typedef typename coordinate_traits<coordinate_type>::area_type area_type;
363
364 area_type a = 0;
365 for (const_iterator_type itCur = vPoint.begin(); itCur != vPoint.end(); ++itCur)
366 {
367 const_iterator_type itNext = itCur;
368 ++itNext;
369 if (itNext == vPoint.end()) itNext = vPoint.begin();
370
371 a += (area_type)(point_traits<point_type>::get(*itNext, HORIZONTAL) - point_traits<point_type>::get(*itCur, HORIZONTAL)) *
372 (area_type)(point_traits<point_type>::get(*itNext, VERTICAL) + point_traits<point_type>::get(*itCur, VERTICAL));
373 }
374 return a/2;
375}
376
377} // namespace geometry
378} // namespace limbo
379
380#endif
#define LIMBO_GEOMETRY_COORDINATE_TRAITS(T, AREA_T, UNSIGNED_AREA_T)
macro to define coordinate_traits for other integer types
Definition Geometry.h:165
a class denoting orientation of lines
Definition Geometry.h:93
int to_int() const
convert orientation to integer
Definition Geometry.h:119
bool operator!=(orientation_2d const &rhs) const
inequality operator
Definition Geometry.h:116
friend bool operator<(orientation_2d const &ori1, orientation_2d const &ori2)
less than operator
Definition Geometry.h:137
bool valid() const
check whether the orientation is valid
Definition Geometry.h:132
int m_orient
enum type of orientation in integer representation
Definition Geometry.h:140
bool operator==(orientation_2d const &rhs) const
equality operator
Definition Geometry.h:112
orientation_2d(const orientation_2d &ori)
copy constructor
Definition Geometry.h:103
void turn_90()
turn 90 degree
Definition Geometry.h:121
orientation_2d get_perpendicular() const
get perpendicular orientation
Definition Geometry.h:124
orientation_2d(orientation_2d_enum const &ori)
constructor
Definition Geometry.h:99
orientation_2d & operator=(const orientation_2d &ori)
assignment
Definition Geometry.h:107
namespace for Limbo.Geometry
Definition Geometry.h:21
std::string to_string(slicing_orientation_2d slicing_orient)
convert enum type of slicing orientation to string
Definition Geometry.h:51
coordinate_traits< typenamepoint_traits< typenamecontainer_traits< PointSet >::value_type >::coordinate_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:357
@ HOR_VER_SLICING
horizontal/vertical slicing and choose rectangle with larger area every time
Definition Geometry.h:43
@ HOR_VER_AR_SLICING
horizontal/vertical slicing and choose rectangle with better aspect ratio every time
Definition Geometry.h:45
@ HOR_VER_SA_SLICING
horizontal/vertical slicing and choose rectangle with smaller area every time
Definition Geometry.h:44
namespace for Limbo
static void insert(container_type &container, iterator_type it, value_type const &v)
insert value to container with hint of iterator
Definition Geometry.h:343
static container_type construct(PointCompareType const &)
a default construct function
Definition Geometry.h:349
static void insert(container_type &container, value_type const &v)
endnowarn
Definition Geometry.h:341
static void erase(container_type &container, iterator_type it)
erase an element from container with iterator
Definition Geometry.h:345
static container_type construct(PointCompareType const &)
a default construct function
Definition Geometry.h:325
static void insert(container_type &container, value_type const &v)
insert value to container
Definition Geometry.h:317
static void erase(container_type &container, iterator_type it)
erase an element from container with iterator
Definition Geometry.h:321
static void insert(container_type &container, iterator_type it, value_type const &v)
insert value to container with hint of iterator
Definition Geometry.h:319
type traits for containers such as vector, list, multiset
Definition Geometry.h:282
static void insert(container_type &container, value_type const &v)
insert value to container
Definition Geometry.h:291
static container_type construct(PointCompareType const &comp)
a default construct function is necessary, because std::set needs a compare function,...
Definition Geometry.h:300
static void insert(container_type &container, iterator_type it, value_type const &v)
insert value to container with hint of iterator
Definition Geometry.h:293
static void erase(container_type &container, value_type const &v)
erase value from iterator
Definition Geometry.h:295
static void erase(container_type &container, iterator_type it)
erase an element from container with iterator
Definition Geometry.h:297
type traits for coordinates
Definition Geometry.h:146
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