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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
|
///\file
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2020 John Wellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#ifndef ETL_BRESENHAM_LINE_INCLUDED
#define ETL_BRESENHAM_LINE_INCLUDED
#include "platform.h"
#include "iterator.h"
#include "static_assert.h"
#include "type_traits.h"
#include "utility.h"
#include <stdint.h>
namespace etl
{
//***************************************************************************
/// A pseudo-container that generates points on a line, using Bresenham's
/// line algorithm.
/// T is the type for the etl::coordinate_2d value type.
/// TWork is the internal working variable type. Default is int16_t.
//***************************************************************************
template <typename T, typename TWork = int16_t>
class bresenham_line
{
public:
//***************************************************
/// Standard container types.
//***************************************************
typedef etl::coordinate_2d<T> value_type;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef value_type* pointer;
typedef const value_type* const_pointer;
//***************************************************
/// Const Iterator
//***************************************************
class const_iterator : public etl::iterator<ETL_OR_STD::forward_iterator_tag, const value_type>
{
public:
friend class bresenham_line;
//***************************************************
/// Default constructor
//***************************************************
const_iterator()
: p_bresenham_line(ETL_NULLPTR)
{
}
//***************************************************
/// Copy constructor
//***************************************************
const_iterator(const const_iterator& other)
: p_bresenham_line(other.p_bresenham_line)
{
}
//***************************************************
/// Assignment operator
//***************************************************
const_iterator& operator =(const const_iterator& rhs)
{
p_bresenham_line = rhs.p_bresenham_line;
return *this;
}
//***************************************************
/// Pre-increment operator
//***************************************************
const_iterator& operator ++()
{
// Has the end of the series has been reached?
if (p_bresenham_line->get_coordinate() == p_bresenham_line->back())
{
// Mark it as an end iterator.
p_bresenham_line = ETL_NULLPTR;
}
else
{
p_bresenham_line->next();
}
return *this;
}
//***************************************************
/// De-reference operator
//***************************************************
value_type operator *() const
{
return p_bresenham_line->get_coordinate();
}
//***************************************************
/// Equality operator
//***************************************************
friend bool operator ==(const const_iterator& lhs, const const_iterator& rhs)
{
return lhs.p_bresenham_line == rhs.p_bresenham_line;
}
//***************************************************
/// Inequality operator
//***************************************************
friend bool operator !=(const const_iterator& lhs, const const_iterator& rhs)
{
return !(lhs == rhs);
}
private:
//***************************************************
/// Constructor for use by bresenham_line
//***************************************************
const_iterator(bresenham_line<T>* pb)
: p_bresenham_line(pb)
{
}
bresenham_line<T>* p_bresenham_line;
};
//***************************************************
/// Constructor.
//***************************************************
bresenham_line()
{
initialise(T(0), T(0), T(0), T(0));
}
//***************************************************
/// Constructor.
/// Supplied first and last coordinates
//***************************************************
bresenham_line(etl::coordinate_2d<T> first_, etl::coordinate_2d<T> last_)
{
initialise(first_.x, first_.y, last_.x, last_.y);
}
//***************************************************
/// Constructor.
/// Supplied first and last coordinates
//***************************************************
bresenham_line(T first_x, T first_y, T last_x, T last_y)
{
initialise(first_x, first_y, last_x, last_y);
}
//***************************************************
/// Resets the line.
/// Supplied first and last coordinates
//***************************************************
void reset(etl::coordinate_2d<T> first_, etl::coordinate_2d<T> last_)
{
initialise(first_.x, first_.y, last_.x, last_.y);
}
//***************************************************
/// Resets the line.
/// Supplied first and last coordinates
//***************************************************
void reset(T first_x, T first_y, T last_x, T last_y)
{
initialise(first_x, first_y, last_x, last_y);
}
//***************************************************
/// Get a const_iterator to the first coordinate.
/// Resets the Bresenham line.
//***************************************************
const_iterator begin()
{
coordinate = first;
return const_iterator(this);
}
//***************************************************
/// Get a const_iterator to one past the last coordinate.
//***************************************************
const_iterator end() const
{
return const_iterator();
}
//***************************************************
/// Get the first coordinate.
//***************************************************
const_reference front() const
{
return first;
}
//***************************************************
/// Get the last coordinate.
//***************************************************
const_reference back() const
{
return last;
}
//***************************************************
/// Get the size of the series.
//***************************************************
size_t size() const
{
if (y_is_major_axis())
{
return (dy / 2) + 1;
}
else
{
return (dx / 2) + 1;
}
}
//***************************************************
/// Equality operator
//***************************************************
friend bool operator ==(const bresenham_line& lhs, const bresenham_line& rhs)
{
return (lhs.front() == rhs.front()) && (lhs.back() == rhs.back());
}
//***************************************************
/// Inequality operator
//***************************************************
friend bool operator !=(const bresenham_line& lhs, const bresenham_line& rhs)
{
return !(lhs == rhs);
}
private:
//***************************************************
/// Get the current number of generated points.
//***************************************************
void initialise(T first_x, T first_y, T last_x, T last_y)
{
first = value_type(first_x, first_y);
last = value_type(last_x, last_y);
coordinate = first;
x_increment = (last_x < first_x) ? -1 : 1;
y_increment = (last_y < first_y) ? -1 : 1;
dx = (last_x < first_x) ? first_x - last_x : last_x - first_x;
dy = (last_y < first_y) ? first_y - last_y : last_y - first_y;
do_minor_increment = false;
if (y_is_major_axis())
{
dx *= 2;
balance = dx - dy;
dy *= 2;
}
else
{
dy *= 2;
balance = dy - dx;
dx *= 2;
}
}
//***************************************************
/// Returns true if Y is the major axis.
//***************************************************
bool y_is_major_axis() const
{
return dx < dy;
}
//***************************************************
/// Calculate the next point.
//***************************************************
void next()
{
if (y_is_major_axis())
{
// Y is major axis.
if (do_minor_increment)
{
coordinate.x = T(coordinate.x + x_increment);
balance -= dy;
}
coordinate.y = T(coordinate.y + y_increment);
balance += dx;
}
else
{
// X is major axis.
if (do_minor_increment)
{
coordinate.y = T(coordinate.y + y_increment);
balance -= dx;
}
coordinate.x = T(coordinate.x + x_increment);
balance += dy;
}
do_minor_increment = (balance >= 0);
}
//***************************************************
/// Get the current coordinate.
//***************************************************
value_type get_coordinate() const
{
return coordinate;
}
typedef TWork work_t;
value_type first;
value_type last;
value_type coordinate;
work_t x_increment;
work_t y_increment;
work_t dx;
work_t dy;
work_t balance;
bool do_minor_increment;
};
}
#endif
|