| 1 |
//---------------------------------------------------------------------------- |
| 2 |
// Anti-Grain Geometry - Version 2.4 |
| 3 |
// Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com) |
| 4 |
// |
| 5 |
// Permission to copy, use, modify, sell and distribute this software |
| 6 |
// is granted provided this copyright notice appears in all copies. |
| 7 |
// This software is provided "as is" without express or implied |
| 8 |
// warranty, and with no claim as to its suitability for any purpose. |
| 9 |
// |
| 10 |
//---------------------------------------------------------------------------- |
| 11 |
// |
| 12 |
// The author gratefully acknowleges the support of David Turner, |
| 13 |
// Robert Wilhelm, and Werner Lemberg - the authors of the FreeType |
| 14 |
// libray - in producing this work. See http://www.freetype.org for details. |
| 15 |
// |
| 16 |
//---------------------------------------------------------------------------- |
| 17 |
// Contact: [email protected] |
| 18 |
// [email protected] |
| 19 |
// http://www.antigrain.com |
| 20 |
//---------------------------------------------------------------------------- |
| 21 |
// |
| 22 |
// Adaptation for 32-bit screen coordinates has been sponsored by |
| 23 |
// Liberty Technology Systems, Inc., visit http://lib-sys.com |
| 24 |
// |
| 25 |
// Liberty Technology Systems, Inc. is the provider of |
| 26 |
// PostScript and PDF technology for software developers. |
| 27 |
// |
| 28 |
//---------------------------------------------------------------------------- |
| 29 |
#ifndef AGG_RASTERIZER_CELLS_AA_INCLUDED |
| 30 |
#define AGG_RASTERIZER_CELLS_AA_INCLUDED |
| 31 |
|
| 32 |
#include <stdexcept> |
| 33 |
#include <string.h> |
| 34 |
#include <math.h> |
| 35 |
#include "agg_math.h" |
| 36 |
#include "agg_array.h" |
| 37 |
|
| 38 |
|
| 39 |
namespace agg |
| 40 |
{ |
| 41 |
|
| 42 |
//-----------------------------------------------------rasterizer_cells_aa |
| 43 |
// An internal class that implements the main rasterization algorithm. |
| 44 |
// Used in the rasterizer. Should not be used direcly. |
| 45 |
template<class Cell> class rasterizer_cells_aa |
| 46 |
{ |
| 47 |
enum cell_block_scale_e |
| 48 |
{ |
| 49 |
cell_block_shift = 12, |
| 50 |
cell_block_size = 1 << cell_block_shift, |
| 51 |
cell_block_mask = cell_block_size - 1, |
| 52 |
cell_block_pool = 256 |
| 53 |
}; |
| 54 |
|
| 55 |
struct sorted_y |
| 56 |
{ |
| 57 |
unsigned start; |
| 58 |
unsigned num; |
| 59 |
}; |
| 60 |
|
| 61 |
public: |
| 62 |
typedef Cell cell_type; |
| 63 |
typedef rasterizer_cells_aa<Cell> self_type; |
| 64 |
|
| 65 |
~rasterizer_cells_aa(); |
| 66 |
rasterizer_cells_aa(unsigned cell_block_limit=1024); |
| 67 |
|
| 68 |
void reset(); |
| 69 |
void style(const cell_type& style_cell); |
| 70 |
void line(int x1, int y1, int x2, int y2); |
| 71 |
|
| 72 |
int min_x() const { return m_min_x; } |
| 73 |
int min_y() const { return m_min_y; } |
| 74 |
int max_x() const { return m_max_x; } |
| 75 |
int max_y() const { return m_max_y; } |
| 76 |
|
| 77 |
void sort_cells(); |
| 78 |
|
| 79 |
unsigned total_cells() const |
| 80 |
{ |
| 81 |
return m_num_cells; |
| 82 |
} |
| 83 |
|
| 84 |
unsigned scanline_num_cells(unsigned y) const |
| 85 |
{ |
| 86 |
return m_sorted_y[y - m_min_y].num; |
| 87 |
} |
| 88 |
|
| 89 |
const cell_type* const* scanline_cells(unsigned y) const |
| 90 |
{ |
| 91 |
return m_sorted_cells.data() + m_sorted_y[y - m_min_y].start; |
| 92 |
} |
| 93 |
|
| 94 |
bool sorted() const { return m_sorted; } |
| 95 |
|
| 96 |
private: |
| 97 |
rasterizer_cells_aa(const self_type&); |
| 98 |
const self_type& operator = (const self_type&); |
| 99 |
|
| 100 |
void set_curr_cell(int x, int y); |
| 101 |
void add_curr_cell(); |
| 102 |
void render_hline(int ey, int x1, int y1, int x2, int y2); |
| 103 |
void allocate_block(); |
| 104 |
|
| 105 |
private: |
| 106 |
unsigned m_num_blocks; |
| 107 |
unsigned m_max_blocks; |
| 108 |
unsigned m_curr_block; |
| 109 |
unsigned m_num_cells; |
| 110 |
unsigned m_cell_block_limit; |
| 111 |
cell_type** m_cells; |
| 112 |
cell_type* m_curr_cell_ptr; |
| 113 |
pod_vector<cell_type*> m_sorted_cells; |
| 114 |
pod_vector<sorted_y> m_sorted_y; |
| 115 |
cell_type m_curr_cell; |
| 116 |
cell_type m_style_cell; |
| 117 |
int m_min_x; |
| 118 |
int m_min_y; |
| 119 |
int m_max_x; |
| 120 |
int m_max_y; |
| 121 |
bool m_sorted; |
| 122 |
}; |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
//------------------------------------------------------------------------ |
| 128 |
template<class Cell> |
| 129 |
rasterizer_cells_aa<Cell>::~rasterizer_cells_aa() |
| 130 |
{ |
| 131 |
if(m_num_blocks) |
| 132 |
{ |
| 133 |
cell_type** ptr = m_cells + m_num_blocks - 1; |
| 134 |
while(m_num_blocks--) |
| 135 |
{ |
| 136 |
pod_allocator<cell_type>::deallocate(*ptr, cell_block_size); |
| 137 |
ptr--; |
| 138 |
} |
| 139 |
pod_allocator<cell_type*>::deallocate(m_cells, m_max_blocks); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
//------------------------------------------------------------------------ |
| 144 |
template<class Cell> |
| 145 |
rasterizer_cells_aa<Cell>::rasterizer_cells_aa(unsigned cell_block_limit) : |
| 146 |
m_num_blocks(0), |
| 147 |
m_max_blocks(0), |
| 148 |
m_curr_block(0), |
| 149 |
m_num_cells(0), |
| 150 |
m_cell_block_limit(cell_block_limit), |
| 151 |
m_cells(0), |
| 152 |
m_curr_cell_ptr(0), |
| 153 |
m_sorted_cells(), |
| 154 |
m_sorted_y(), |
| 155 |
m_min_x(0x7FFFFFFF), |
| 156 |
m_min_y(0x7FFFFFFF), |
| 157 |
m_max_x(-0x7FFFFFFF), |
| 158 |
m_max_y(-0x7FFFFFFF), |
| 159 |
m_sorted(false) |
| 160 |
{ |
| 161 |
m_style_cell.initial(); |
| 162 |
m_curr_cell.initial(); |
| 163 |
} |
| 164 |
|
| 165 |
//------------------------------------------------------------------------ |
| 166 |
template<class Cell> |
| 167 |
void rasterizer_cells_aa<Cell>::reset() |
| 168 |
{ |
| 169 |
m_num_cells = 0; |
| 170 |
m_curr_block = 0; |
| 171 |
m_curr_cell.initial(); |
| 172 |
m_style_cell.initial(); |
| 173 |
m_sorted = false; |
| 174 |
m_min_x = 0x7FFFFFFF; |
| 175 |
m_min_y = 0x7FFFFFFF; |
| 176 |
m_max_x = -0x7FFFFFFF; |
| 177 |
m_max_y = -0x7FFFFFFF; |
| 178 |
} |
| 179 |
|
| 180 |
//------------------------------------------------------------------------ |
| 181 |
template<class Cell> |
| 182 |
AGG_INLINE void rasterizer_cells_aa<Cell>::add_curr_cell() |
| 183 |
{ |
| 184 |
if(m_curr_cell.area | m_curr_cell.cover) |
| 185 |
{ |
| 186 |
if((m_num_cells & cell_block_mask) == 0) |
| 187 |
{ |
| 188 |
if(m_num_blocks >= m_cell_block_limit) { |
| 189 |
throw std::overflow_error("Exceeded cell block limit"); |
| 190 |
} |
| 191 |
allocate_block(); |
| 192 |
} |
| 193 |
*m_curr_cell_ptr++ = m_curr_cell; |
| 194 |
++m_num_cells; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
//------------------------------------------------------------------------ |
| 199 |
template<class Cell> |
| 200 |
AGG_INLINE void rasterizer_cells_aa<Cell>::set_curr_cell(int x, int y) |
| 201 |
{ |
| 202 |
if(m_curr_cell.not_equal(x, y, m_style_cell)) |
| 203 |
{ |
| 204 |
add_curr_cell(); |
| 205 |
m_curr_cell.style(m_style_cell); |
| 206 |
m_curr_cell.x = x; |
| 207 |
m_curr_cell.y = y; |
| 208 |
m_curr_cell.cover = 0; |
| 209 |
m_curr_cell.area = 0; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
//------------------------------------------------------------------------ |
| 214 |
template<class Cell> |
| 215 |
AGG_INLINE void rasterizer_cells_aa<Cell>::render_hline(int ey, |
| 216 |
int x1, int y1, |
| 217 |
int x2, int y2) |
| 218 |
{ |
| 219 |
int ex1 = x1 >> poly_subpixel_shift; |
| 220 |
int ex2 = x2 >> poly_subpixel_shift; |
| 221 |
int fx1 = x1 & poly_subpixel_mask; |
| 222 |
int fx2 = x2 & poly_subpixel_mask; |
| 223 |
|
| 224 |
int delta, p, first, dx; |
| 225 |
int incr, lift, mod, rem; |
| 226 |
|
| 227 |
//trivial case. Happens often |
| 228 |
if(y1 == y2) |
| 229 |
{ |
| 230 |
set_curr_cell(ex2, ey); |
| 231 |
return; |
| 232 |
} |
| 233 |
|
| 234 |
//everything is located in a single cell. That is easy! |
| 235 |
if(ex1 == ex2) |
| 236 |
{ |
| 237 |
delta = y2 - y1; |
| 238 |
m_curr_cell.cover += delta; |
| 239 |
m_curr_cell.area += (fx1 + fx2) * delta; |
| 240 |
return; |
| 241 |
} |
| 242 |
|
| 243 |
//ok, we'll have to render a run of adjacent cells on the same |
| 244 |
//hline... |
| 245 |
p = (poly_subpixel_scale - fx1) * (y2 - y1); |
| 246 |
first = poly_subpixel_scale; |
| 247 |
incr = 1; |
| 248 |
|
| 249 |
dx = x2 - x1; |
| 250 |
|
| 251 |
if(dx < 0) |
| 252 |
{ |
| 253 |
p = fx1 * (y2 - y1); |
| 254 |
first = 0; |
| 255 |
incr = -1; |
| 256 |
dx = -dx; |
| 257 |
} |
| 258 |
|
| 259 |
delta = p / dx; |
| 260 |
mod = p % dx; |
| 261 |
|
| 262 |
if(mod < 0) |
| 263 |
{ |
| 264 |
delta--; |
| 265 |
mod += dx; |
| 266 |
} |
| 267 |
|
| 268 |
m_curr_cell.cover += delta; |
| 269 |
m_curr_cell.area += (fx1 + first) * delta; |
| 270 |
|
| 271 |
ex1 += incr; |
| 272 |
set_curr_cell(ex1, ey); |
| 273 |
y1 += delta; |
| 274 |
|
| 275 |
if(ex1 != ex2) |
| 276 |
{ |
| 277 |
p = poly_subpixel_scale * (y2 - y1 + delta); |
| 278 |
lift = p / dx; |
| 279 |
rem = p % dx; |
| 280 |
|
| 281 |
if (rem < 0) |
| 282 |
{ |
| 283 |
lift--; |
| 284 |
rem += dx; |
| 285 |
} |
| 286 |
|
| 287 |
mod -= dx; |
| 288 |
|
| 289 |
while (ex1 != ex2) |
| 290 |
{ |
| 291 |
delta = lift; |
| 292 |
mod += rem; |
| 293 |
if(mod >= 0) |
| 294 |
{ |
| 295 |
mod -= dx; |
| 296 |
delta++; |
| 297 |
} |
| 298 |
|
| 299 |
m_curr_cell.cover += delta; |
| 300 |
m_curr_cell.area += poly_subpixel_scale * delta; |
| 301 |
y1 += delta; |
| 302 |
ex1 += incr; |
| 303 |
set_curr_cell(ex1, ey); |
| 304 |
} |
| 305 |
} |
| 306 |
delta = y2 - y1; |
| 307 |
m_curr_cell.cover += delta; |
| 308 |
m_curr_cell.area += (fx2 + poly_subpixel_scale - first) * delta; |
| 309 |
} |
| 310 |
|
| 311 |
//------------------------------------------------------------------------ |
| 312 |
template<class Cell> |
| 313 |
AGG_INLINE void rasterizer_cells_aa<Cell>::style(const cell_type& style_cell) |
| 314 |
{ |
| 315 |
m_style_cell.style(style_cell); |
| 316 |
} |
| 317 |
|
| 318 |
//------------------------------------------------------------------------ |
| 319 |
template<class Cell> |
| 320 |
void rasterizer_cells_aa<Cell>::line(int x1, int y1, int x2, int y2) |
| 321 |
{ |
| 322 |
enum dx_limit_e { dx_limit = 16384 << poly_subpixel_shift }; |
| 323 |
|
| 324 |
int dx = x2 - x1; |
| 325 |
|
| 326 |
if(dx >= dx_limit || dx <= -dx_limit) |
| 327 |
{ |
| 328 |
// These are overflow safe versions of (x1 + x2) >> 1; divide each by 2 |
| 329 |
// first, then add 1 if both were odd. |
| 330 |
int cx = (x1 >> 1) + (x2 >> 1) + ((x1 & 1) & (x2 & 1)); |
| 331 |
int cy = (y1 >> 1) + (y2 >> 1) + ((y1 & 1) & (y2 & 1)); |
| 332 |
line(x1, y1, cx, cy); |
| 333 |
line(cx, cy, x2, y2); |
| 334 |
return; |
| 335 |
} |
| 336 |
|
| 337 |
int dy = y2 - y1; |
| 338 |
int ex1 = x1 >> poly_subpixel_shift; |
| 339 |
int ex2 = x2 >> poly_subpixel_shift; |
| 340 |
int ey1 = y1 >> poly_subpixel_shift; |
| 341 |
int ey2 = y2 >> poly_subpixel_shift; |
| 342 |
int fy1 = y1 & poly_subpixel_mask; |
| 343 |
int fy2 = y2 & poly_subpixel_mask; |
| 344 |
|
| 345 |
int x_from, x_to; |
| 346 |
int p, rem, mod, lift, delta, first, incr; |
| 347 |
|
| 348 |
if(ex1 < m_min_x) m_min_x = ex1; |
| 349 |
if(ex1 > m_max_x) m_max_x = ex1; |
| 350 |
if(ey1 < m_min_y) m_min_y = ey1; |
| 351 |
if(ey1 > m_max_y) m_max_y = ey1; |
| 352 |
if(ex2 < m_min_x) m_min_x = ex2; |
| 353 |
if(ex2 > m_max_x) m_max_x = ex2; |
| 354 |
if(ey2 < m_min_y) m_min_y = ey2; |
| 355 |
if(ey2 > m_max_y) m_max_y = ey2; |
| 356 |
|
| 357 |
set_curr_cell(ex1, ey1); |
| 358 |
|
| 359 |
//everything is on a single hline |
| 360 |
if(ey1 == ey2) |
| 361 |
{ |
| 362 |
render_hline(ey1, x1, fy1, x2, fy2); |
| 363 |
return; |
| 364 |
} |
| 365 |
|
| 366 |
//Vertical line - we have to calculate start and end cells, |
| 367 |
//and then - the common values of the area and coverage for |
| 368 |
//all cells of the line. We know exactly there's only one |
| 369 |
//cell, so, we don't have to call render_hline(). |
| 370 |
incr = 1; |
| 371 |
if(dx == 0) |
| 372 |
{ |
| 373 |
int ex = x1 >> poly_subpixel_shift; |
| 374 |
int two_fx = (x1 - (ex << poly_subpixel_shift)) << 1; |
| 375 |
int area; |
| 376 |
|
| 377 |
first = poly_subpixel_scale; |
| 378 |
if(dy < 0) |
| 379 |
{ |
| 380 |
first = 0; |
| 381 |
incr = -1; |
| 382 |
} |
| 383 |
|
| 384 |
x_from = x1; |
| 385 |
|
| 386 |
//render_hline(ey1, x_from, fy1, x_from, first); |
| 387 |
delta = first - fy1; |
| 388 |
m_curr_cell.cover += delta; |
| 389 |
m_curr_cell.area += two_fx * delta; |
| 390 |
|
| 391 |
ey1 += incr; |
| 392 |
set_curr_cell(ex, ey1); |
| 393 |
|
| 394 |
delta = first + first - poly_subpixel_scale; |
| 395 |
area = two_fx * delta; |
| 396 |
while(ey1 != ey2) |
| 397 |
{ |
| 398 |
//render_hline(ey1, x_from, poly_subpixel_scale - first, x_from, first); |
| 399 |
m_curr_cell.cover = delta; |
| 400 |
m_curr_cell.area = area; |
| 401 |
ey1 += incr; |
| 402 |
set_curr_cell(ex, ey1); |
| 403 |
} |
| 404 |
//render_hline(ey1, x_from, poly_subpixel_scale - first, x_from, fy2); |
| 405 |
delta = fy2 - poly_subpixel_scale + first; |
| 406 |
m_curr_cell.cover += delta; |
| 407 |
m_curr_cell.area += two_fx * delta; |
| 408 |
return; |
| 409 |
} |
| 410 |
|
| 411 |
//ok, we have to render several hlines |
| 412 |
p = (poly_subpixel_scale - fy1) * dx; |
| 413 |
first = poly_subpixel_scale; |
| 414 |
|
| 415 |
if(dy < 0) |
| 416 |
{ |
| 417 |
p = fy1 * dx; |
| 418 |
first = 0; |
| 419 |
incr = -1; |
| 420 |
dy = -dy; |
| 421 |
} |
| 422 |
|
| 423 |
delta = p / dy; |
| 424 |
mod = p % dy; |
| 425 |
|
| 426 |
if(mod < 0) |
| 427 |
{ |
| 428 |
delta--; |
| 429 |
mod += dy; |
| 430 |
} |
| 431 |
|
| 432 |
x_from = x1 + delta; |
| 433 |
render_hline(ey1, x1, fy1, x_from, first); |
| 434 |
|
| 435 |
ey1 += incr; |
| 436 |
set_curr_cell(x_from >> poly_subpixel_shift, ey1); |
| 437 |
|
| 438 |
if(ey1 != ey2) |
| 439 |
{ |
| 440 |
p = poly_subpixel_scale * dx; |
| 441 |
lift = p / dy; |
| 442 |
rem = p % dy; |
| 443 |
|
| 444 |
if(rem < 0) |
| 445 |
{ |
| 446 |
lift--; |
| 447 |
rem += dy; |
| 448 |
} |
| 449 |
mod -= dy; |
| 450 |
|
| 451 |
while(ey1 != ey2) |
| 452 |
{ |
| 453 |
delta = lift; |
| 454 |
mod += rem; |
| 455 |
if (mod >= 0) |
| 456 |
{ |
| 457 |
mod -= dy; |
| 458 |
delta++; |
| 459 |
} |
| 460 |
|
| 461 |
x_to = x_from + delta; |
| 462 |
render_hline(ey1, x_from, poly_subpixel_scale - first, x_to, first); |
| 463 |
x_from = x_to; |
| 464 |
|
| 465 |
ey1 += incr; |
| 466 |
set_curr_cell(x_from >> poly_subpixel_shift, ey1); |
| 467 |
} |
| 468 |
} |
| 469 |
render_hline(ey1, x_from, poly_subpixel_scale - first, x2, fy2); |
| 470 |
} |
| 471 |
|
| 472 |
//------------------------------------------------------------------------ |
| 473 |
template<class Cell> |
| 474 |
void rasterizer_cells_aa<Cell>::allocate_block() |
| 475 |
{ |
| 476 |
if(m_curr_block >= m_num_blocks) |
| 477 |
{ |
| 478 |
if(m_num_blocks >= m_max_blocks) |
| 479 |
{ |
| 480 |
cell_type** new_cells = |
| 481 |
pod_allocator<cell_type*>::allocate(m_max_blocks + |
| 482 |
cell_block_pool); |
| 483 |
|
| 484 |
if(m_cells) |
| 485 |
{ |
| 486 |
memcpy(new_cells, m_cells, m_max_blocks * sizeof(cell_type*)); |
| 487 |
pod_allocator<cell_type*>::deallocate(m_cells, m_max_blocks); |
| 488 |
} |
| 489 |
m_cells = new_cells; |
| 490 |
m_max_blocks += cell_block_pool; |
| 491 |
} |
| 492 |
|
| 493 |
m_cells[m_num_blocks++] = |
| 494 |
pod_allocator<cell_type>::allocate(cell_block_size); |
| 495 |
|
| 496 |
} |
| 497 |
m_curr_cell_ptr = m_cells[m_curr_block++]; |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
//------------------------------------------------------------------------ |
| 503 |
template <class T> static AGG_INLINE void swap_cells(T* a, T* b) |
| 504 |
{ |
| 505 |
T temp = *a; |
| 506 |
*a = *b; |
| 507 |
*b = temp; |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
//------------------------------------------------------------------------ |
| 512 |
enum |
| 513 |
{ |
| 514 |
qsort_threshold = 9 |
| 515 |
}; |
| 516 |
|
| 517 |
|
| 518 |
//------------------------------------------------------------------------ |
| 519 |
template<class Cell> |
| 520 |
void qsort_cells(Cell** start, unsigned num) |
| 521 |
{ |
| 522 |
Cell** stack[80]; |
| 523 |
Cell*** top; |
| 524 |
Cell** limit; |
| 525 |
Cell** base; |
| 526 |
|
| 527 |
limit = start + num; |
| 528 |
base = start; |
| 529 |
top = stack; |
| 530 |
|
| 531 |
for (;;) |
| 532 |
{ |
| 533 |
int len = int(limit - base); |
| 534 |
|
| 535 |
Cell** i; |
| 536 |
Cell** j; |
| 537 |
Cell** pivot; |
| 538 |
|
| 539 |
if(len > qsort_threshold) |
| 540 |
{ |
| 541 |
// we use base + len/2 as the pivot |
| 542 |
pivot = base + len / 2; |
| 543 |
swap_cells(base, pivot); |
| 544 |
|
| 545 |
i = base + 1; |
| 546 |
j = limit - 1; |
| 547 |
|
| 548 |
// now ensure that *i <= *base <= *j |
| 549 |
if((*j)->x < (*i)->x) |
| 550 |
{ |
| 551 |
swap_cells(i, j); |
| 552 |
} |
| 553 |
|
| 554 |
if((*base)->x < (*i)->x) |
| 555 |
{ |
| 556 |
swap_cells(base, i); |
| 557 |
} |
| 558 |
|
| 559 |
if((*j)->x < (*base)->x) |
| 560 |
{ |
| 561 |
swap_cells(base, j); |
| 562 |
} |
| 563 |
|
| 564 |
for(;;) |
| 565 |
{ |
| 566 |
int x = (*base)->x; |
| 567 |
do i++; while( (*i)->x < x ); |
| 568 |
do j--; while( x < (*j)->x ); |
| 569 |
|
| 570 |
if(i > j) |
| 571 |
{ |
| 572 |
break; |
| 573 |
} |
| 574 |
|
| 575 |
swap_cells(i, j); |
| 576 |
} |
| 577 |
|
| 578 |
swap_cells(base, j); |
| 579 |
|
| 580 |
// now, push the largest sub-array |
| 581 |
if(j - base > limit - i) |
| 582 |
{ |
| 583 |
top[0] = base; |
| 584 |
top[1] = j; |
| 585 |
base = i; |
| 586 |
} |
| 587 |
else |
| 588 |
{ |
| 589 |
top[0] = i; |
| 590 |
top[1] = limit; |
| 591 |
limit = j; |
| 592 |
} |
| 593 |
top += 2; |
| 594 |
} |
| 595 |
else |
| 596 |
{ |
| 597 |
// the sub-array is small, perform insertion sort |
| 598 |
j = base; |
| 599 |
i = j + 1; |
| 600 |
|
| 601 |
for(; i < limit; j = i, i++) |
| 602 |
{ |
| 603 |
for(; j[1]->x < (*j)->x; j--) |
| 604 |
{ |
| 605 |
swap_cells(j + 1, j); |
| 606 |
if (j == base) |
| 607 |
{ |
| 608 |
break; |
| 609 |
} |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
if(top > stack) |
| 614 |
{ |
| 615 |
top -= 2; |
| 616 |
base = top[0]; |
| 617 |
limit = top[1]; |
| 618 |
} |
| 619 |
else |
| 620 |
{ |
| 621 |
break; |
| 622 |
} |
| 623 |
} |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
|
| 628 |
//------------------------------------------------------------------------ |
| 629 |
template<class Cell> |
| 630 |
void rasterizer_cells_aa<Cell>::sort_cells() |
| 631 |
{ |
| 632 |
if(m_sorted) return; //Perform sort only the first time. |
| 633 |
|
| 634 |
add_curr_cell(); |
| 635 |
m_curr_cell.x = 0x7FFFFFFF; |
| 636 |
m_curr_cell.y = 0x7FFFFFFF; |
| 637 |
m_curr_cell.cover = 0; |
| 638 |
m_curr_cell.area = 0; |
| 639 |
|
| 640 |
if(m_num_cells == 0) return; |
| 641 |
|
| 642 |
// DBG: Check to see if min/max works well. |
| 643 |
//for(unsigned nc = 0; nc < m_num_cells; nc++) |
| 644 |
//{ |
| 645 |
// cell_type* cell = m_cells[nc >> cell_block_shift] + (nc & cell_block_mask); |
| 646 |
// if(cell->x < m_min_x || |
| 647 |
// cell->y < m_min_y || |
| 648 |
// cell->x > m_max_x || |
| 649 |
// cell->y > m_max_y) |
| 650 |
// { |
| 651 |
// cell = cell; // Breakpoint here |
| 652 |
// } |
| 653 |
//} |
| 654 |
// Allocate the array of cell pointers |
| 655 |
m_sorted_cells.allocate(m_num_cells, 16); |
| 656 |
|
| 657 |
// Allocate and zero the Y array |
| 658 |
m_sorted_y.allocate(m_max_y - m_min_y + 1, 16); |
| 659 |
m_sorted_y.zero(); |
| 660 |
|
| 661 |
// Create the Y-histogram (count the numbers of cells for each Y) |
| 662 |
cell_type** block_ptr = m_cells; |
| 663 |
cell_type* cell_ptr; |
| 664 |
unsigned nb = m_num_cells; |
| 665 |
unsigned i; |
| 666 |
while(nb) |
| 667 |
{ |
| 668 |
cell_ptr = *block_ptr++; |
| 669 |
i = (nb > cell_block_size) ? cell_block_size : nb; |
| 670 |
nb -= i; |
| 671 |
while(i--) |
| 672 |
{ |
| 673 |
m_sorted_y[cell_ptr->y - m_min_y].start++; |
| 674 |
++cell_ptr; |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
// Convert the Y-histogram into the array of starting indexes |
| 679 |
unsigned start = 0; |
| 680 |
for(i = 0; i < m_sorted_y.size(); i++) |
| 681 |
{ |
| 682 |
unsigned v = m_sorted_y[i].start; |
| 683 |
m_sorted_y[i].start = start; |
| 684 |
start += v; |
| 685 |
} |
| 686 |
|
| 687 |
// Fill the cell pointer array sorted by Y |
| 688 |
block_ptr = m_cells; |
| 689 |
nb = m_num_cells; |
| 690 |
while(nb) |
| 691 |
{ |
| 692 |
cell_ptr = *block_ptr++; |
| 693 |
i = (nb > cell_block_size) ? cell_block_size : nb; |
| 694 |
nb -= i; |
| 695 |
while(i--) |
| 696 |
{ |
| 697 |
sorted_y& curr_y = m_sorted_y[cell_ptr->y - m_min_y]; |
| 698 |
m_sorted_cells[curr_y.start + curr_y.num] = cell_ptr; |
| 699 |
++curr_y.num; |
| 700 |
++cell_ptr; |
| 701 |
} |
| 702 |
} |
| 703 |
|
| 704 |
// Finally arrange the X-arrays |
| 705 |
for(i = 0; i < m_sorted_y.size(); i++) |
| 706 |
{ |
| 707 |
const sorted_y& curr_y = m_sorted_y[i]; |
| 708 |
if(curr_y.num) |
| 709 |
{ |
| 710 |
qsort_cells(m_sorted_cells.data() + curr_y.start, curr_y.num); |
| 711 |
} |
| 712 |
} |
| 713 |
m_sorted = true; |
| 714 |
} |
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
//------------------------------------------------------scanline_hit_test |
| 719 |
class scanline_hit_test |
| 720 |
{ |
| 721 |
public: |
| 722 |
scanline_hit_test(int x) : m_x(x), m_hit(false) {} |
| 723 |
|
| 724 |
void reset_spans() {} |
| 725 |
void finalize(int) {} |
| 726 |
void add_cell(int x, int) |
| 727 |
{ |
| 728 |
if(m_x == x) m_hit = true; |
| 729 |
} |
| 730 |
void add_span(int x, int len, int) |
| 731 |
{ |
| 732 |
if(m_x >= x && m_x < x+len) m_hit = true; |
| 733 |
} |
| 734 |
unsigned num_spans() const { return 1; } |
| 735 |
bool hit() const { return m_hit; } |
| 736 |
|
| 737 |
private: |
| 738 |
int m_x; |
| 739 |
bool m_hit; |
| 740 |
}; |
| 741 |
|
| 742 |
|
| 743 |
} |
| 744 |
|
| 745 |
#endif |