47#define USE_CONVEX_SKIP
126#ifdef USE_CONVEX_SKIP
186 return (d2[0] * d3[1]) - (d3[0] * d2[1]);
195# define KDNODE_UNSET ((uint32_t)-1)
205 tree->coords = coords;
207 tree->node_num = tot;
220 for (i = 0, node =
tree->nodes; i < coords_num; i++) {
223 node->index = indices[i].index;
236 const float (*coords)[2],
252 median = node_num / 2;
255 const float co = coords[nodes[
pos].index][axis];
260 while (coords[nodes[++i].index][axis] < co) {
262 while (coords[nodes[--j].index][axis] > co && j > neg) {
281 node = &nodes[median];
286 &nodes[median + 1], (node_num - (median + 1)), axis, coords, (median + 1) + ofs);
301 for (i = 0, node =
tree->nodes; i <
tree->node_num; i++, node++) {
306 tree->nodes[node->pos].parent = i;
311 tree->nodes_map[node->index] = i;
328 node = &
tree->nodes[node_index];
340 if (node_parent->
neg == node_index) {
349 node_index = node->parent;
360 const float *tri_coords[3],
361 const float tri_center[2],
365 const float *co =
tree->coords[node->index];
377 if (!
ELEM(node->index, tri_index[0], tri_index[1], tri_index[2])) {
384# define KDTREE2D_ISECT_TRI_RECURSE_NEG \
385 (((node->neg != KDNODE_UNSET) && (co[node->axis] >= bounds[node->axis].min)) && \
386 kdtree2d_isect_tri_recursive( \
387 tree, tri_index, tri_coords, tri_center, bounds, &tree->nodes[node->neg]))
388# define KDTREE2D_ISECT_TRI_RECURSE_POS \
389 (((node->pos != KDNODE_UNSET) && (co[node->axis] <= bounds[node->axis].max)) && \
390 kdtree2d_isect_tri_recursive( \
391 tree, tri_index, tri_coords, tri_center, bounds, &tree->nodes[node->pos]))
393 if (tri_center[node->axis] > co[node->axis]) {
410# undef KDTREE2D_ISECT_TRI_RECURSE_NEG
411# undef KDTREE2D_ISECT_TRI_RECURSE_POS
426 float tri_center[2] = {0.0f, 0.0f};
428 for (i = 0; i < 3; i++) {
429 vs[i] =
tree->coords[ind[i]];
448 return pf->tris[
pf->tris_num++];
455 if (
pf->kdtree.node_num) {
460 pi->next->prev = pi->prev;
461 pi->prev->next = pi->next;
464 pf->indices = pi->next;
468 pi->next = pi->prev =
NULL;
483 bool reverse =
false;
486 while (
pf->coords_num > 3) {
488 eSign sign_orig_prev, sign_orig_next;
501#ifdef USE_CONVEX_SKIP
503 pf->coords_num_concave -= 1;
507 pi_prev = pi_ear->
prev;
508 pi_next = pi_ear->
next;
513 sign_orig_prev = pi_prev->
sign;
514 sign_orig_next = pi_next->
sign;
518 if (sign_orig_prev !=
CONVEX) {
520#ifdef USE_CONVEX_SKIP
522 pf->coords_num_concave -= 1;
529 if (sign_orig_next !=
CONVEX) {
531#ifdef USE_CONVEX_SKIP
533 pf->coords_num_concave -= 1;
542# ifdef USE_CLIP_SWEEP
543 pi_ear_init = reverse ? pi_prev->
prev : pi_next->
next;
545 pi_ear_init = pi_next->
next;
550# ifdef USE_CLIP_SWEEP
553 pi_ear_init = reverse ? pi_ear_init->
prev : pi_ear_init->
next;
558# ifdef USE_CLIP_SWEEP
566 if (
pf->coords_num == 3) {
568 pi_ear =
pf->indices;
569 tri[0] = pi_ear->
index;
570 pi_ear = pi_ear->
next;
571 tri[1] = pi_ear->
index;
572 pi_ear = pi_ear->
next;
573 tri[2] = pi_ear->
index;
583 const float(*coords)[2] =
pf->coords;
585 pi->sign =
span_tri_v2_sign(coords[pi->prev->index], coords[pi->index], coords[pi->next->index]);
628 pi_ear = pi_ear_init;
630 pi_ear =
pf->indices;
638 pi_ear = reverse ? pi_ear->
prev : pi_ear->
next;
640 pi_ear = pi_ear->
next;
658 pi_ear = pi_ear_init;
660 pi_ear =
pf->indices;
668 pi_ear = pi_ear->
next;
679 const float(*coords)[2] =
pf->coords;
682 const float *v1, *
v2, *v3;
685#if defined(USE_CONVEX_SKIP) && !defined(USE_KDTREE)
686 uint32_t coords_num_concave_checked = 0;
689#ifdef USE_CONVEX_SKIP
691# ifdef USE_CONVEX_SKIP_TEST
694 uint32_t coords_num_concave_test = 0;
698 coords_num_concave_test += 1;
700 }
while ((pi_iter = pi_iter->
next) != pi_ear_tip);
701 BLI_assert(coords_num_concave_test ==
pf->coords_num_concave);
706 if (
pf->coords_num_concave == 0) {
726 v2 = coords[pi_ear_tip->
index];
733 for (pi_curr = pi_ear_tip->
next->
next; pi_curr != pi_ear_tip->
prev; pi_curr = pi_curr->
next) {
737 const float *
v = coords[pi_curr->
index];
752# ifdef USE_CONVEX_SKIP
753 coords_num_concave_checked += 1;
754 if (coords_num_concave_checked ==
pf->coords_num_concave) {
770 tri[1] = pi_ear_tip->
index;
780 const float (*coords)[2],
792 pf->indices = r_indices;
794 pf->coords_num = coords_num;
795#ifdef USE_CONVEX_SKIP
796 pf->coords_num_concave = 0;
801 if (coords_sign == 0) {
802 coords_sign = (
cross_poly_v2(coords, coords_num) <= 0.0f) ? 1 : -1;
806#ifdef USE_STRICT_ASSERT
808 if (coords_sign == 1) {
818 if (coords_sign == 1) {
819 for (i = 0; i < coords_num; i++) {
820 indices[i].next = &indices[i + 1];
821 indices[i].prev = &indices[i - 1];
822 indices[i].index = i;
828 for (i = 0; i < coords_num; i++) {
829 indices[i].next = &indices[i + 1];
830 indices[i].prev = &indices[i - 1];
831 indices[i].index = (n - i);
834 indices[0].prev = &indices[coords_num - 1];
835 indices[coords_num - 1].next = &indices[0];
837 for (i = 0; i < coords_num; i++) {
840#ifdef USE_CONVEX_SKIP
842 pf->coords_num_concave += 1;
851# ifdef USE_CONVEX_SKIP
852 if (
pf->coords_num_concave)
867 const int coords_sign,
888 if (
pf.coords_num_concave) {
890 pf.kdtree.nodes_map = memset(
893 sizeof(*
pf.kdtree.nodes_map) * coords_num);
896 pf.kdtree.node_num = 0;
912 const int coords_sign,
944 if (
pf.coords_num_concave) {
948 sizeof(*
pf.kdtree.nodes_map) * coords_num);
951 pf.kdtree.node_num = 0;
#define BLI_array_alloca(arr, realsize)
float cross_poly_v2(const float verts[][2], unsigned int nr)
MINLINE void mul_v2_fl(float r[2], float f)
MINLINE void add_v2_v2(float r[2], const float a[2])
MINLINE void sub_v2_v2v2(float r[2], const float a[2], const float b[2])
void * BLI_memarena_alloc(struct MemArena *ma, size_t size) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_ALLOC_SIZE(2)
void BLI_memarena_free(struct MemArena *ma) ATTR_NONNULL(1)
struct MemArena * BLI_memarena_new(size_t bufsize, const char *name) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL ATTR_NONNULL(2) ATTR_MALLOC
Utility defines for timing/benchmarks.
#define TIMEIT_START(var)
ATTR_WARN_UNUSED_RESULT const BMVert * v2
ATTR_WARN_UNUSED_RESULT const BMVert * v
static btDbvtVolume bounds(btDbvtNode **leaves, int count)
draw_view in_light_buf[] float
#define pf(_x, _i)
Prefetch 64.
static void pf_coord_sign_calc(const PolyFill *pf, PolyIndex *pi)
static void kdtree2d_init(struct KDTree2D *tree, const uint32_t coords_num, const PolyIndex *indices)
static PolyIndex * pf_ear_tip_find(PolyFill *pf, PolyIndex *pi_ear_init, bool reverse)
struct KDTreeNode2D KDTreeNode2D
void BLI_polyfill_calc_arena(const float(*coords)[2], const uint32_t coords_num, const int coords_sign, uint32_t(*r_tris)[3], MemArena *arena)
static uint32_t kdtree2d_balance_recursive(KDTreeNode2D *nodes, uint32_t node_num, axis_t axis, const float(*coords)[2], const uint32_t ofs)
static void kdtree2d_init_mapping(struct KDTree2D *tree)
struct KDTreeNode2D_head KDTreeNode2D_head
static void kdtree2d_node_remove(struct KDTree2D *tree, uint32_t index)
void BLI_polyfill_calc(const float(*coords)[2], const uint32_t coords_num, const int coords_sign, uint32_t(*r_tris)[3])
static bool pf_ear_tip_check(PolyFill *pf, PolyIndex *pi_ear_tip, const eSign sign_accept)
BLI_INLINE eSign signum_enum(float a)
struct PolyIndex PolyIndex
static void pf_ear_tip_cut(PolyFill *pf, PolyIndex *pi_ear_tip)
#define KDTREE2D_ISECT_TRI_RECURSE_NEG
static uint32_t * pf_tri_add(PolyFill *pf)
BLI_INLINE float area_tri_signed_v2_alt_2x(const float v1[2], const float v2[2], const float v3[2])
static void polyfill_prepare(PolyFill *pf, const float(*coords)[2], const uint32_t coords_num, int coords_sign, uint32_t(*r_tris)[3], PolyIndex *r_indices)
static bool kdtree2d_isect_tri(struct KDTree2D *tree, const uint32_t ind[3])
static void kdtree2d_new(struct KDTree2D *tree, uint32_t tot, const float(*coords)[2])
static eSign span_tri_v2_sign(const float v1[2], const float v2[2], const float v3[2])
static void kdtree2d_balance(struct KDTree2D *tree)
#define KDTREE2D_ISECT_TRI_RECURSE_POS
static void polyfill_calc(PolyFill *pf)
static void pf_coord_remove(PolyFill *pf, PolyIndex *pi)
static bool kdtree2d_isect_tri_recursive(const struct KDTree2D *tree, const uint32_t tri_index[3], const float *tri_coords[3], const float tri_center[2], const struct KDRange2D bounds[2], const KDTreeNode2D *node)
static void pf_triangulate(PolyFill *pf)
struct PolyIndex * indices
uint32_t coords_num_concave