test_bresenham.cpp
Go to the documentation of this file.
1 // Copyright 2023 Ekumen, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <gtest/gtest.h>
16 
17 #include <vector>
18 
19 #include <Eigen/Core>
20 
21 #include <range/v3/range/conversion.hpp>
22 
24 
25 namespace beluga {
26 
27 TEST(Bresenham, MultiPassGuarantee) {
28  // See comparison between iterators in
29  // https://en.cppreference.com/w/cpp/iterator/forward_iterator
30  const auto algorithm = Bresenham2i{Bresenham2i::kStandard};
31 
32  const auto line = algorithm({0, 0}, {5, 5});
33  for (auto it1 = line.begin(), it2 = line.begin(); it1 != line.end(); ++it1, ++it2) {
34  ASSERT_EQ(it1, it2);
35  }
36 
37  // Test that iterating over the sequence using an independent
38  // iterator doesn't change what an existing iterator refers to.
39  auto it3 = line.begin();
40  ++it3;
41  auto it4 = it3;
42  const auto expected_value = *it3;
43  ++it4;
44  ASSERT_EQ(expected_value, *it3);
45 }
46 
47 TEST(Bresenham, Standard) {
48  auto algorithm = Bresenham2i{Bresenham2i::kStandard};
49 
50  {
51  const auto expected_trace = std::vector<Eigen::Vector2i>{{0, 0}};
52  const auto trace = algorithm({0, 0}, {0, 0}) | ranges::to<std::vector>;
53  EXPECT_EQ(trace, expected_trace);
54  }
55 
56  {
57  // +---+---+
58  // | | > |
59  // +---+---+
60  // | > | |
61  // +---+---+
62  const auto expected_trace = std::vector<Eigen::Vector2i>{{0, 0}, {1, 1}};
63  const auto trace = algorithm({0, 0}, {1, 1}) | ranges::to<std::vector>;
64  EXPECT_EQ(trace, expected_trace);
65  }
66 
67  {
68  // +---+---+
69  // | | < |
70  // +---+---+
71  // | < | |
72  // +---+---+
73  const auto expected_trace = std::vector<Eigen::Vector2i>{{1, 1}, {0, 0}};
74  const auto trace = algorithm({1, 1}, {0, 0}) | ranges::to<std::vector>;
75  EXPECT_EQ(trace, expected_trace);
76  }
77 
78  {
79  // +---+---+---+
80  // | | | > |
81  // +---+---+---+
82  // | > | > | |
83  // +---+---+---+
84  const auto expected_trace = std::vector<Eigen::Vector2i>{{0, 0}, {1, 0}, {2, 1}};
85  const auto trace = algorithm({0, 0}, {2, 1}) | ranges::to<std::vector>;
86  EXPECT_EQ(trace, expected_trace);
87  }
88 
89  {
90  // +---+---+---+
91  // | | < | < |
92  // +---+---+---+
93  // | < | | |
94  // +---+---+---+
95  const auto expected_trace = std::vector<Eigen::Vector2i>{{2, 1}, {1, 1}, {0, 0}};
96  const auto trace = algorithm({2, 1}, {0, 0}) | ranges::to<std::vector>;
97  EXPECT_EQ(trace, expected_trace);
98  }
99 
100  {
101  // +---+---+
102  // | v | |
103  // +---+---+
104  // | v | |
105  // +---+---+
106  // | v | |
107  // +---+---+
108  const auto expected_trace = std::vector<Eigen::Vector2i>{{0, 2}, {0, 1}, {0, 0}};
109  const auto trace = algorithm({0, 2}, {0, 0}) | ranges::to<std::vector>;
110  EXPECT_EQ(trace, expected_trace);
111  }
112 
113  {
114  // +---+---+---+---+
115  // | | | | < |
116  // +---+---+---+---+
117  // | | < | < | |
118  // +---+---+---+---+
119  // | < | | | |
120  // +---+---+---+---+
121  const auto expected_trace = std::vector<Eigen::Vector2i>{{3, 2}, {2, 1}, {1, 1}, {0, 0}};
122  const auto trace = algorithm({3, 2}, {0, 0}) | ranges::to<std::vector>;
123  EXPECT_EQ(trace, expected_trace);
124  }
125 }
126 
127 TEST(Bresenham, Modified) {
128  auto algorithm = Bresenham2i{Bresenham2i::kModified};
129 
130  {
131  const auto expected_trace = std::vector<Eigen::Vector2i>{{0, 0}};
132  const auto trace = algorithm({0, 0}, {0, 0}) | ranges::to<std::vector>;
133  EXPECT_EQ(trace, expected_trace);
134  }
135 
136  {
137  // +---+---+
138  // | > | > |
139  // +---+---+
140  // | > | > |
141  // +---+---+
142  const auto expected_trace = std::vector<Eigen::Vector2i>{{0, 0}, {1, 0}, {0, 1}, {1, 1}};
143  const auto trace = algorithm({0, 0}, {1, 1}) | ranges::to<std::vector>;
144  EXPECT_EQ(trace, expected_trace);
145  }
146 
147  {
148  // +---+---+
149  // | < | < |
150  // +---+---+
151  // | < | < |
152  // +---+---+
153  const auto expected_trace = std::vector<Eigen::Vector2i>{{1, 1}, {0, 1}, {1, 0}, {0, 0}};
154  const auto trace = algorithm({1, 1}, {0, 0}) | ranges::to<std::vector>;
155  EXPECT_EQ(trace, expected_trace);
156  }
157 
158  {
159  // +---+---+---+
160  // | | > | > |
161  // +---+---+---+
162  // | > | > | |
163  // +---+---+---+
164  const auto expected_trace = std::vector<Eigen::Vector2i>{{0, 0}, {1, 0}, {1, 1}, {2, 1}};
165  const auto trace = algorithm({0, 0}, {2, 1}) | ranges::to<std::vector>;
166  EXPECT_EQ(trace, expected_trace);
167  }
168 
169  {
170  // +---+---+---+
171  // | | < | < |
172  // +---+---+---+
173  // | < | < | |
174  // +---+---+---+
175  const auto expected_trace = std::vector<Eigen::Vector2i>{{2, 1}, {1, 1}, {1, 0}, {0, 0}};
176  const auto trace = algorithm({2, 1}, {0, 0}) | ranges::to<std::vector>;
177  EXPECT_EQ(trace, expected_trace);
178  }
179 
180  {
181  // +---+---+
182  // | v | |
183  // +---+---+
184  // | v | |
185  // +---+---+
186  // | v | |
187  // +---+---+
188  const auto expected_trace = std::vector<Eigen::Vector2i>{{0, 2}, {0, 1}, {0, 0}};
189  const auto trace = algorithm({0, 2}, {0, 0}) | ranges::to<std::vector>;
190  EXPECT_EQ(trace, expected_trace);
191  }
192 
193  {
194  // +---+---+---+---+
195  // | | | < | < |
196  // +---+---+---+---+
197  // | | < | < | |
198  // +---+---+---+---+
199  // | < | < | | |
200  // +---+---+---+---+
201  const auto expected_trace = std::vector<Eigen::Vector2i>{{3, 2}, {2, 2}, {2, 1}, {1, 1}, {1, 0}, {0, 0}};
202  const auto trace = algorithm({3, 2}, {0, 0}) | ranges::to<std::vector>;
203  EXPECT_EQ(trace, expected_trace);
204  }
205 }
206 
207 } // namespace beluga
beluga::Bresenham2i::kStandard
@ kStandard
Standard Bresenham's algorithm.
Definition: bresenham.hpp:38
bresenham.hpp
Implementation of Bresenham-like raytracing algorithms.
beluga::Bresenham2i
Bresenham's 2D line drawing algorithm, optimized for integer arithmetic.
Definition: bresenham.hpp:34
beluga::Bresenham2i::kModified
@ kModified
Definition: bresenham.hpp:39
beluga
The main Beluga namespace.
Definition: 3d_embedding.hpp:21
beluga::TEST
TEST(Bresenham, MultiPassGuarantee)
Definition: test_bresenham.cpp:27


beluga
Author(s):
autogenerated on Tue Jul 16 2024 02:59:53