File: gtl_custom_point.cpp

package info (click to toggle)
boost1.83 1.83.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 545,632 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (120 lines) | stat: -rw-r--r-- 3,340 bytes parent folder | download | duplicates (14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
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.