33 bool (*callback)(
int,
int,
void *),
42 if (callback(x1, y1, user_data) == 0) {
47 const int sign_x = (x2 > x1) ? 1 : -1;
48 const int sign_y = (y2 > y1) ? 1 : -1;
50 const int delta_x = (sign_x == 1) ? (x2 - x1) : (x1 - x2);
51 const int delta_y = (sign_y == 1) ? (y2 - y1) : (y1 - y2);
53 const int delta_x_step = delta_x * 2;
54 const int delta_y_step = delta_y * 2;
56 if (delta_x >= delta_y) {
58 int error = delta_y_step - delta_x;
62 if (
error || (sign_x == 1)) {
64 error -= delta_x_step;
71 error += delta_y_step;
73 if (callback(x1, y1, user_data) == 0) {
80 int error = delta_x_step - delta_y;
84 if (
error || (sign_y == 1)) {
86 error -= delta_y_step;
93 error += delta_x_step;
95 if (callback(x1, y1, user_data) == 0) {
125#define ORDERED_SWAP(ty, a, b) \
131#define ORDERED_SWAP_BY(ty, a, b, by) \
132 if ((a by) > (b by)) { \
137#define ORDER_VARS2(ty, a, b) \
139 ORDERED_SWAP(ty, a, b); \
143#define ORDER_VARS3_BY(ty, a, b, c, by) \
145 ORDERED_SWAP_BY(ty, b, c, by); \
146 ORDERED_SWAP_BY(ty, a, c, by); \
147 ORDERED_SWAP_BY(ty, a, b, by); \
165 const float inv_slope1,
166 const float inv_slope2,
167 void (*callback)(
int x,
int x_end,
int y,
void *),
170 float cur_x1 =
float(p[0]);
171 float cur_x2 = cur_x1;
173 const int min_y = p[1];
174 const int max_y_end = max_y + 1;
175 for (
int scanline_y = min_y; scanline_y != max_y_end; scanline_y += 1) {
176 callback(
int(cur_x1), 1 +
int(cur_x2), scanline_y, user_data);
177 cur_x1 += inv_slope1;
178 cur_x2 += inv_slope2;
191 const float inv_slope1,
192 const float inv_slope2,
193 void (*callback)(
int x,
int x_end,
int y,
void *),
196 float cur_x1 =
float(p[0]);
197 float cur_x2 = cur_x1;
199 const int max_y = p[1];
200 const int min_y_end = min_y - 1;
201 for (
int scanline_y = max_y; scanline_y != min_y_end; scanline_y -= 1) {
202 callback(
int(cur_x1), 1 +
int(cur_x2), scanline_y, user_data);
203 cur_x1 -= inv_slope1;
204 cur_x2 -= inv_slope2;
213 void (*callback)(
int x,
int x_end,
int y,
void *),
222 if (p2[1] == p3[1]) {
229 else if (p1[1] == p2[1]) {
244 const float inv_slope_p21 =
inv_slope(p2, p1);
245 const float inv_slope_p31 =
inv_slope(p3, p1);
246 const float inv_slope_p32 =
inv_slope(p3, p2);
248 float inv_slope1_max, inv_slope2_max;
249 float inv_slope2_min, inv_slope1_min;
251 if (inv_slope_p21 < inv_slope_p31) {
252 inv_slope1_max = inv_slope_p21;
253 inv_slope2_max = inv_slope_p31;
254 inv_slope2_min = inv_slope_p31;
255 inv_slope1_min = inv_slope_p32;
258 inv_slope1_max = inv_slope_p31;
259 inv_slope2_max = inv_slope_p21;
260 inv_slope2_min = inv_slope_p32;
261 inv_slope1_min = inv_slope_p31;
264 draw_tri_flat_max(p1, p2[1], inv_slope1_max, inv_slope2_max, callback, user_data);
275#undef ORDERED_SWAP_BY
288 const int (*
verts)[2] =
static_cast<const int (*)[2]
>(verts_p);
289 const int *a =
static_cast<const int *
>(a_p);
290 const int *
b =
static_cast<const int *
>(b_p);
291 const int *co_a =
verts[a[0]];
292 const int *co_b =
verts[
b[0]];
294 if (co_a[1] < co_b[1]) {
297 if (co_a[1] > co_b[1]) {
300 if (co_a[0] < co_b[0]) {
303 if (co_a[0] > co_b[0]) {
307 const int *co = co_a;
310 int ord = (((co_b[0] - co[0]) * (co_a[1] - co[1])) - ((co_a[0] - co[0]) * (co_b[1] - co[1])));
325 void (*callback)(
int x,
int x_end,
int y,
void *),
334 for (
int i_curr = 0, i_prev =
int(
verts.size() - 1); i_curr <
verts.size(); i_prev = i_curr++) {
335 const int *co_prev =
verts[i_prev];
336 const int *co_curr =
verts[i_curr];
338 if (co_prev[1] != co_curr[1]) {
340 if ((
min_ii(co_prev[1], co_curr[1]) >= ymax) || (
max_ii(co_prev[1], co_curr[1]) < ymin)) {
344 int *s = span_y[span_y_len++];
345 if (co_prev[1] < co_curr[1]) {
360 (
void *)
verts.data());
368 int span_y_index = 0;
369 if (span_y_len != 0 &&
verts[span_y[0][0]][1] < ymin) {
370 while ((span_y_index < span_y_len) && (
verts[span_y[span_y_index][0]][1] < ymin)) {
372 if (
verts[span_y[span_y_index][1]][1] >= ymin) {
373 NodeX *n = &node_x[node_x_len++];
374 n->span_y_index = span_y_index;
381 for (
int pixel_y = ymin; pixel_y < ymax; pixel_y++) {
382 bool is_sorted =
true;
383 bool do_remove =
false;
385 for (
int i = 0, x_ix_prev = INT_MIN;
i < node_x_len;
i++) {
386 NodeX *n = &node_x[
i];
387 const int *s = span_y[n->span_y_index];
388 const int *co_prev =
verts[s[0]];
389 const int *co_curr =
verts[s[1]];
391 BLI_assert(co_prev[1] < pixel_y && co_curr[1] >= pixel_y);
393 const double x = (co_prev[0] - co_curr[0]);
394 const double y = (co_prev[1] - co_curr[1]);
395 const double y_px = (pixel_y - co_curr[1]);
396 const int x_ix = int(
double(co_curr[0]) + ((y_px /
y) *
x));
399 if (is_sorted && (x_ix_prev > x_ix)) {
402 if (do_remove ==
false && co_curr[1] == pixel_y) {
409 if (is_sorted ==
false) {
411 const int node_x_end = node_x_len - 1;
412 while (
i < node_x_end) {
413 if (node_x[
i].
x > node_x[
i + 1].
x) {
414 SWAP(NodeX, node_x[
i], node_x[
i + 1]);
426 for (
int i = 0;
i < node_x_len;
i += 2) {
427 int x_src = node_x[
i].x;
428 int x_dst = node_x[
i + 1].x;
435 x_src = std::max(x_src, xmin);
436 x_dst = std::min(x_dst, xmax);
439 callback(x_src - xmin, x_dst - xmin, pixel_y - ymin, user_data);
446 if (do_remove ==
true) {
448 for (
int i_src = 0; i_src < node_x_len; i_src += 1) {
449 const int *s = span_y[node_x[i_src].span_y_index];
450 const int *co =
verts[s[1]];
451 if (co[1] != pixel_y) {
452 if (i_dst != i_src) {
454 node_x[i_dst].span_y_index = node_x[i_src].span_y_index;
463 while ((span_y_index < span_y_len) && (
verts[span_y[span_y_index][0]][1] == pixel_y)) {
468 NodeX *n = &node_x[node_x_len++];
469 n->span_y_index = span_y_index;
MINLINE int min_ii(int a, int b)
MINLINE int max_ii(int a, int b)
void BLI_qsort_r(void *a, size_t n, size_t es, BLI_sort_cmp_t cmp, void *thunk)
Read Guarded memory(de)allocation.
void BLI_bitmap_draw_2d_poly_v2i_n(const int xmin, const int ymin, const int xmax, const int ymax, const Span< int2 > verts, void(*callback)(int x, int x_end, int y, void *), void *user_data)
static int draw_poly_v2i_n__span_y_sort(const void *a_p, const void *b_p, void *verts_p)
static float inv_slope(const int a[2], const int b[2])
void BLI_bitmap_draw_2d_line_v2v2i(const int p1[2], const int p2[2], bool(*callback)(int, int, void *), void *user_data)
void BLI_bitmap_draw_2d_tri_v2i(const int p1[2], const int p2[2], const int p3[2], void(*callback)(int x, int x_end, int y, void *), void *user_data)
static void draw_tri_flat_max(const int p[2], const int max_y, const float inv_slope1, const float inv_slope2, void(*callback)(int x, int x_end, int y, void *), void *user_data)
static void draw_tri_flat_min(const int p[2], const int min_y, const float inv_slope1, const float inv_slope2, void(*callback)(int x, int x_end, int y, void *), void *user_data)
#define ORDER_VARS3_BY(ty, a, b, c, by)
#define ORDER_VARS2(ty, a, b)
void * MEM_malloc_arrayN(size_t len, size_t size, const char *str)
void MEM_freeN(void *vmemh)
static void error(const char *str)
VecBase< int32_t, 2 > int2