1 //
2 // Copyright 2019-2020 Mateusz Loskot <mateusz at loskot dot net>
3 //
4 // Distributed under the Boost Software License, Version 1.0
5 // See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt
7 //
8 #include <boost/gil.hpp>
9 #include <boost/gil/extension/numeric/affine.hpp>
10 #include <boost/gil/extension/numeric/resample.hpp>
11 #include <boost/gil/extension/numeric/sampler.hpp>
12
13 #include <boost/core/lightweight_test.hpp>
14
15 #include <cmath>
16
17 namespace gil = boost::gil;
18
19 // FIXME: Remove when https://github.com/boostorg/core/issues/38 happens
20 #define BOOST_GIL_TEST_IS_CLOSE(a, b, epsilon) BOOST_TEST_LT(std::fabs((a) - (b)), (epsilon))
21
22 namespace {
23 constexpr double HALF_PI = 1.57079632679489661923;
24 }
25
test_matrix3x2_default_constructor()26 void test_matrix3x2_default_constructor()
27 {
28 gil::matrix3x2<int> m1;
29 BOOST_TEST_EQ(m1.a, 1);
30 BOOST_TEST_EQ(m1.b, 0);
31 BOOST_TEST_EQ(m1.c, 0);
32 BOOST_TEST_EQ(m1.d, 1);
33 BOOST_TEST_EQ(m1.e, 0);
34 BOOST_TEST_EQ(m1.f, 0);
35 }
36
test_matrix3x2_parameterized_constructor()37 void test_matrix3x2_parameterized_constructor()
38 {
39 gil::matrix3x2<int> m1(1, 2, 3, 4, 5, 6);
40 BOOST_TEST_EQ(m1.a, 1);
41 BOOST_TEST_EQ(m1.b, 2);
42 BOOST_TEST_EQ(m1.c, 3);
43 BOOST_TEST_EQ(m1.d, 4);
44 BOOST_TEST_EQ(m1.e, 5);
45 BOOST_TEST_EQ(m1.f, 6);
46 }
47
test_matrix3x2_copy_constructor()48 void test_matrix3x2_copy_constructor()
49 {
50 gil::matrix3x2<int> m1(1, 2, 3, 4, 5, 6);
51 gil::matrix3x2<int> m2(m1);
52 BOOST_TEST_EQ(m2.a, 1);
53 BOOST_TEST_EQ(m2.b, 2);
54 BOOST_TEST_EQ(m2.c, 3);
55 BOOST_TEST_EQ(m2.d, 4);
56 BOOST_TEST_EQ(m2.e, 5);
57 BOOST_TEST_EQ(m2.f, 6);
58 }
59
test_matrix3x2_assignment_operator()60 void test_matrix3x2_assignment_operator()
61 {
62 gil::matrix3x2<int> m1(1, 2, 3, 4, 5, 6);
63 gil::matrix3x2<int> m2;
64 m2 = m1;
65 BOOST_TEST_EQ(m2.a, 1);
66 BOOST_TEST_EQ(m2.b, 2);
67 BOOST_TEST_EQ(m2.c, 3);
68 BOOST_TEST_EQ(m2.d, 4);
69 BOOST_TEST_EQ(m2.e, 5);
70 BOOST_TEST_EQ(m2.f, 6);
71 }
72
test_matrix3x2_multiplication_assignment()73 void test_matrix3x2_multiplication_assignment()
74 {
75 gil::matrix3x2<int> m1;
76 gil::matrix3x2<int> m2;
77 m2 *= m1;
78 BOOST_TEST_EQ(m2.a, 1);
79 BOOST_TEST_EQ(m2.b, 0);
80 BOOST_TEST_EQ(m2.c, 0);
81 BOOST_TEST_EQ(m2.d, 1);
82 BOOST_TEST_EQ(m2.e, 0);
83 BOOST_TEST_EQ(m2.f, 0);
84
85 gil::matrix3x2<int> m3(0, 0, 0, 0, 0, 0);
86 m2 *= m3;
87 BOOST_TEST_EQ(m2.a, 0);
88 BOOST_TEST_EQ(m2.b, 0);
89 BOOST_TEST_EQ(m2.c, 0);
90 BOOST_TEST_EQ(m2.d, 0);
91 BOOST_TEST_EQ(m2.e, 0);
92 BOOST_TEST_EQ(m2.f, 0);
93 }
94
test_matrix3x2_matrix3x2_multiplication()95 void test_matrix3x2_matrix3x2_multiplication()
96 {
97 gil::matrix3x2<int> m1;
98 gil::matrix3x2<int> m2(0, 0, 0, 0, 0, 0);
99 gil::matrix3x2<int> m3;
100 m3 = m1 * m2;
101 BOOST_TEST_EQ(m3.a, 0);
102 BOOST_TEST_EQ(m3.b, 0);
103 BOOST_TEST_EQ(m3.c, 0);
104 BOOST_TEST_EQ(m3.d, 0);
105 BOOST_TEST_EQ(m3.e, 0);
106 BOOST_TEST_EQ(m3.f, 0);
107 }
108
test_matrix3x2_vector_multiplication()109 void test_matrix3x2_vector_multiplication()
110 {
111 gil::matrix3x2<int> m1;
112 gil::point<int> v1{2, 4};
113
114 gil::point<int> v2 = v1 * m1;
115 BOOST_TEST_EQ(v2.x, 2);
116 BOOST_TEST_EQ(v2.y, 4);
117
118 gil::point<int> v3 = gil::transform(m1, v1);
119 BOOST_TEST_EQ(v3.x, 2);
120 BOOST_TEST_EQ(v3.y, 4);
121 }
122
test_matrix3x2_get_rotate()123 void test_matrix3x2_get_rotate()
124 {
125 auto m1 = gil::matrix3x2<double>::get_rotate(HALF_PI);
126 BOOST_GIL_TEST_IS_CLOSE(m1.a, std::cos(HALF_PI), 0.03);
127 BOOST_TEST_EQ(m1.b, 1);
128 BOOST_TEST_EQ(m1.c, -1);
129 BOOST_GIL_TEST_IS_CLOSE(m1.d, std::cos(HALF_PI), 0.03);
130 BOOST_TEST_EQ(m1.e, 0);
131 BOOST_TEST_EQ(m1.f, 0);
132 }
133
test_matrix3x2_get_scale()134 void test_matrix3x2_get_scale()
135 {
136 gil::matrix3x2<int> m1;
137 m1 = gil::matrix3x2<int>::get_scale(2);
138 BOOST_TEST_EQ(m1.a, 2);
139 BOOST_TEST_EQ(m1.b, 0);
140 BOOST_TEST_EQ(m1.c, 0);
141 BOOST_TEST_EQ(m1.d, 2);
142 BOOST_TEST_EQ(m1.e, 0);
143 BOOST_TEST_EQ(m1.f, 0);
144 m1 = gil::matrix3x2<int>::get_scale(2, 4);
145 BOOST_TEST_EQ(m1.a, 2);
146 BOOST_TEST_EQ(m1.d, 4);
147 m1 = gil::matrix3x2<int>::get_scale(gil::point<int>{4, 8});
148 BOOST_TEST_EQ(m1.a, 4);
149 BOOST_TEST_EQ(m1.d, 8);
150 }
151
test_matrix3x2_get_translate()152 void test_matrix3x2_get_translate()
153 {
154 gil::matrix3x2<int> m1;
155 m1 = gil::matrix3x2<int>::get_translate(2, 4);
156 BOOST_TEST_EQ(m1.a, 1);
157 BOOST_TEST_EQ(m1.b, 0);
158 BOOST_TEST_EQ(m1.c, 0);
159 BOOST_TEST_EQ(m1.d, 1);
160 BOOST_TEST_EQ(m1.e, 2);
161 BOOST_TEST_EQ(m1.f, 4);
162 m1 = gil::matrix3x2<int>::get_translate(gil::point<int>{4, 8});
163 BOOST_TEST_EQ(m1.e, 4);
164 BOOST_TEST_EQ(m1.f, 8);
165 }
166
test_matrix3x2_transform()167 void test_matrix3x2_transform()
168 {
169 gil::matrix3x2<int> m1;
170 gil::point<int> v1{2, 4};
171 gil::point<int> v2 = gil::transform(m1, v1);
172 BOOST_TEST_EQ(v2.x, 2);
173 BOOST_TEST_EQ(v2.y, 4);
174 }
175
main()176 int main()
177 {
178 test_matrix3x2_default_constructor();
179 test_matrix3x2_parameterized_constructor();
180 test_matrix3x2_copy_constructor();
181 test_matrix3x2_assignment_operator();
182 test_matrix3x2_multiplication_assignment();
183 test_matrix3x2_matrix3x2_multiplication();
184 test_matrix3x2_vector_multiplication();
185 test_matrix3x2_get_rotate();
186 test_matrix3x2_get_scale();
187 test_matrix3x2_get_translate();
188 test_matrix3x2_transform();
189
190 return ::boost::report_errors();
191 }
192