File: polygon_point_test.cpp

package info (click to toggle)
boost1.74 1.74.0%2Bds1-21
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 463,588 kB
  • sloc: cpp: 3,338,117; xml: 131,293; python: 33,088; ansic: 14,292; asm: 4,038; sh: 3,353; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (205 lines) | stat: -rw-r--r-- 5,064 bytes parent folder | download | duplicates (15)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Boost.Polygon library polygon_point_test.cpp file

//          Copyright Andrii Sydorchuk 2012.
// Distributed under 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)

// See http://www.boost.org for updates, documentation, and revision history.

#include <boost/core/lightweight_test.hpp>
#include <boost/polygon/point_concept.hpp>
#include <boost/polygon/point_data.hpp>
#include <boost/polygon/point_traits.hpp>

using namespace boost::polygon;

void point_data_test()
{
  typedef point_data<int> point_type;

  point_type point1(1, 2);
  point_type point2;
  point2 = point1;
  BOOST_TEST_EQ(point1.x(), 1);
  BOOST_TEST_EQ(point1.y(), 2);
  BOOST_TEST_EQ(point2.x(), 1);
  BOOST_TEST_EQ(point2.y(), 2);
  BOOST_TEST(point1 == point2);
  BOOST_TEST(!(point1 != point2));
  BOOST_TEST(!(point1 < point2));
  BOOST_TEST(!(point1 > point2));
  BOOST_TEST(point1 <= point2);
  BOOST_TEST(point1 >= point2);

  point2.x(2);
  point2.y(1);
  BOOST_TEST_EQ(point2.x(), 2);
  BOOST_TEST_EQ(point2.y(), 1);
  BOOST_TEST(!(point1 == point2));
  BOOST_TEST(point1 != point2);
  BOOST_TEST(point1 < point2);
  BOOST_TEST(!(point1 > point2));
  BOOST_TEST(point1 <= point2);
  BOOST_TEST(!(point1 >= point2));

  point2.set(HORIZONTAL, 1);
  point2.set(VERTICAL, 2);
  BOOST_TEST(point1 == point2);
}

void point_traits_test()
{
  typedef point_data<int> point_type;

  point_type point = point_mutable_traits<point_type>::construct(1, 2);
  BOOST_TEST_EQ(point_traits<point_type>::get(point, HORIZONTAL), 1);
  BOOST_TEST_EQ(point_traits<point_type>::get(point, VERTICAL), 2);

  point_mutable_traits<point_type>::set(point, HORIZONTAL, 3);
  point_mutable_traits<point_type>::set(point, VERTICAL, 4);
  BOOST_TEST_EQ(point_traits<point_type>::get(point, HORIZONTAL), 3);
  BOOST_TEST_EQ(point_traits<point_type>::get(point, VERTICAL), 4);
}

template <typename T>
struct Point {
  T x;
  T y;
};

namespace boost {
namespace polygon {
  template <typename T>
  struct geometry_concept< Point<T> > {
    typedef point_concept type;
  };

  template <typename T>
  struct point_traits< Point<T> > {
    typedef T coordinate_type;

    static coordinate_type get(const Point<T>& point, orientation_2d orient) {
      return (orient == HORIZONTAL) ? point.x : point.y;
    }
  };

  template <typename T>
  struct point_mutable_traits< Point<T> > {
    typedef T coordinate_type;

    static void set(Point<T>& point, orientation_2d orient, T value) {
      (orient == HORIZONTAL) ? point.x = value : point.y = value;
    }

    static Point<T> construct(coordinate_type x, coordinate_type y) {
      Point<T> point;
      point.x = x;
      point.y = y;
      return point;
    }
  };
}  // polygon
}  // boost

void point_concept_test1()
{
  typedef Point<int> point_type;

  point_type point1 = construct<point_type>(1, 2);
  BOOST_TEST_EQ(point1.x, 1);
  BOOST_TEST_EQ(point1.y, 2);

  set(point1, HORIZONTAL, 3);
  set(point1, VERTICAL, 4);
  BOOST_TEST_EQ(get(point1, HORIZONTAL), 3);
  BOOST_TEST_EQ(get(point1, VERTICAL), 4);

  point_type point2;
  assign(point2, point1);
  BOOST_TEST(equivalence(point1, point2));

  x(point2, 1);
  y(point2, 2);
  BOOST_TEST_EQ(x(point2), 1);
  BOOST_TEST_EQ(y(point2), 2);
}

void point_concept_test2()
{
  typedef Point<int> point_type;

  point_type point1 = construct<point_type>(1, 2);
  point_type point2 = construct<point_type>(5, 5);
  BOOST_TEST_EQ(euclidean_distance(point1, point2, HORIZONTAL), 4);
  BOOST_TEST_EQ(euclidean_distance(point1, point2, VERTICAL), 3);
  BOOST_TEST_EQ(manhattan_distance(point1, point2), 7);
  BOOST_TEST_EQ(euclidean_distance(point1, point2), 5.0);
}

void point_concept_test3()
{
  typedef Point<int> point_type;

  point_type point = construct<point_type>(1, 2);
  point_type shift = construct<point_type>(4, 3);
  convolve(point, shift);
  BOOST_TEST_EQ(x(point), 5);
  BOOST_TEST_EQ(y(point), 5);

  deconvolve(point, shift);
  BOOST_TEST_EQ(x(point), 1);
  BOOST_TEST_EQ(y(point), 2);

  scale_up(point, 5);
  BOOST_TEST_EQ(x(point), 5);
  BOOST_TEST_EQ(y(point), 10);

  scale_down(point, 5);
  BOOST_TEST_EQ(x(point), 1);
  BOOST_TEST_EQ(y(point), 2);

  move(point, HORIZONTAL, 2);
  move(point, VERTICAL, 3);
  BOOST_TEST_EQ(x(point), 3);
  BOOST_TEST_EQ(y(point), 5);
}

template<typename T>
struct Transformer {
  void scale(T& x, T& y) const {
    x *= 2;
    y *= 2;
  }

  void transform(T& x, T& y) const {
    T tmp = x;
    x = y;
    y = tmp;
  }
};

void point_concept_test4()
{
  typedef Point<int> point_type;

  point_type point = construct<point_type>(1, 2);
  scale(point, Transformer<int>());
  BOOST_TEST_EQ(x(point), 2);
  BOOST_TEST_EQ(y(point), 4);

  transform(point, Transformer<int>());
  BOOST_TEST_EQ(x(point), 4);
  BOOST_TEST_EQ(y(point), 2);
}

int main()
{
    point_data_test();
    point_traits_test();
    point_concept_test1();
    point_concept_test2();
    point_concept_test3();
    point_concept_test4();
    return boost::report_errors();
}