2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software Foundation,
14 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * This file contains code for dealing
21 * with polygons (normal/area calculation,
25 #include "DNA_listBase.h"
26 #include "DNA_modifier_types.h"
27 #include "DNA_meshdata_types.h"
29 #include "MEM_guardedalloc.h"
31 #include "BLI_alloca.h"
33 #include "BLI_memarena.h"
34 #include "BLI_polyfill_2d.h"
35 #include "BLI_polyfill_2d_beautify.h"
36 #include "BLI_linklist.h"
40 #include "bmesh_tools.h"
42 #include "BKE_customdata.h"
44 #include "intern/bmesh_private.h"
47 * \brief COMPUTE POLY NORMAL (BMFace)
49 * Same as #normal_poly_v3 but operates directly on a bmesh face.
51 static float bm_face_calc_poly_normal(const BMFace *f, float n[3])
53 BMLoop *l_first = BM_FACE_FIRST_LOOP(f);
54 BMLoop *l_iter = l_first;
55 const float *v_prev = l_first->prev->v->co;
56 const float *v_curr = l_first->v->co;
62 add_newell_cross_v3_v3v3(n, v_prev, v_curr);
64 l_iter = l_iter->next;
66 v_curr = l_iter->v->co;
68 } while (l_iter != l_first);
70 return normalize_v3(n);
74 * \brief COMPUTE POLY NORMAL (BMFace)
76 * Same as #bm_face_calc_poly_normal
77 * but takes an array of vertex locations.
79 static float bm_face_calc_poly_normal_vertex_cos(const BMFace *f,
81 float const (*vertexCos)[3])
83 BMLoop *l_first = BM_FACE_FIRST_LOOP(f);
84 BMLoop *l_iter = l_first;
85 const float *v_prev = vertexCos[BM_elem_index_get(l_first->prev->v)];
86 const float *v_curr = vertexCos[BM_elem_index_get(l_first->v)];
92 add_newell_cross_v3_v3v3(r_no, v_prev, v_curr);
94 l_iter = l_iter->next;
96 v_curr = vertexCos[BM_elem_index_get(l_iter->v)];
97 } while (l_iter != l_first);
99 return normalize_v3(r_no);
103 * \brief COMPUTE POLY CENTER (BMFace)
105 static void bm_face_calc_poly_center_median_vertex_cos(const BMFace *f,
107 float const (*vertexCos)[3])
109 const BMLoop *l_first, *l_iter;
113 /* Newell's Method */
114 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
116 add_v3_v3(r_cent, vertexCos[BM_elem_index_get(l_iter->v)]);
117 } while ((l_iter = l_iter->next) != l_first);
118 mul_v3_fl(r_cent, 1.0f / f->len);
122 * For tools that insist on using triangles, ideally we would cache this data.
124 * \param use_fixed_quad: When true,
125 * always split quad along (0 -> 2) regardless of concave corners,
126 * (as done in #BM_mesh_calc_tessellation).
127 * \param r_loops: Store face loop pointers, (f->len)
128 * \param r_index: Store triangle triples, indices into \a r_loops, `((f->len - 2) * 3)`
130 void BM_face_calc_tessellation(const BMFace *f,
131 const bool use_fixed_quad,
135 BMLoop *l_first = BM_FACE_FIRST_LOOP(f);
139 *r_loops++ = (l_iter = l_first);
140 *r_loops++ = (l_iter = l_iter->next);
141 *r_loops++ = (l_iter->next);
147 else if (f->len == 4 && use_fixed_quad) {
148 *r_loops++ = (l_iter = l_first);
149 *r_loops++ = (l_iter = l_iter->next);
150 *r_loops++ = (l_iter = l_iter->next);
151 *r_loops++ = (l_iter->next);
162 float axis_mat[3][3];
163 float(*projverts)[2] = BLI_array_alloca(projverts, f->len);
166 axis_dominant_v3_to_m3(axis_mat, f->no);
171 mul_v2_m3v3(projverts[j], axis_mat, l_iter->v->co);
174 } while ((l_iter = l_iter->next) != l_first);
176 /* complete the loop */
177 BLI_polyfill_calc(projverts, f->len, -1, r_index);
182 * Return a point inside the face.
184 void BM_face_calc_point_in_face(const BMFace *f, float r_co[3])
186 const BMLoop *l_tri[3];
189 const BMLoop *l = BM_FACE_FIRST_LOOP(f);
190 ARRAY_SET_ITEMS(l_tri, l, l->next, l->prev);
193 /* tessellation here seems overkill when in many cases this will be the center,
194 * but without this we can't be sure the point is inside a concave face. */
195 const int tottri = f->len - 2;
196 BMLoop **loops = BLI_array_alloca(loops, f->len);
197 uint(*index)[3] = BLI_array_alloca(index, tottri);
199 int j_best = 0; /* use as fallback when unset */
200 float area_best = -1.0f;
202 BM_face_calc_tessellation(f, false, loops, index);
204 for (j = 0; j < tottri; j++) {
205 const float *p1 = loops[index[j][0]]->v->co;
206 const float *p2 = loops[index[j][1]]->v->co;
207 const float *p3 = loops[index[j][2]]->v->co;
208 const float area = area_squared_tri_v3(p1, p2, p3);
209 if (area > area_best) {
216 l_tri, loops[index[j_best][0]], loops[index[j_best][1]], loops[index[j_best][2]]);
219 mid_v3_v3v3v3(r_co, l_tri[0]->v->co, l_tri[1]->v->co, l_tri[2]->v->co);
223 * get the area of the face
225 float BM_face_calc_area(const BMFace *f)
227 /* inline 'area_poly_v3' logic, avoid creating a temp array */
228 const BMLoop *l_iter, *l_first;
232 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
234 add_newell_cross_v3_v3v3(n, l_iter->v->co, l_iter->next->v->co);
235 } while ((l_iter = l_iter->next) != l_first);
236 return len_v3(n) * 0.5f;
240 * Get the area of the face in world space.
242 float BM_face_calc_area_with_mat3(const BMFace *f, const float mat3[3][3])
244 /* inline 'area_poly_v3' logic, avoid creating a temp array */
245 const BMLoop *l_iter, *l_first;
250 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
251 mul_v3_m3v3(co, mat3, l_iter->v->co);
254 mul_v3_m3v3(co_next, mat3, l_iter->next->v->co);
255 add_newell_cross_v3_v3v3(n, co, co_next);
256 copy_v3_v3(co, co_next);
257 } while ((l_iter = l_iter->next) != l_first);
258 return len_v3(n) * 0.5f;
262 * get the area of UV face
264 float BM_face_calc_area_uv(const BMFace *f, int cd_loop_uv_offset)
266 /* inline 'area_poly_v2' logic, avoid creating a temp array */
267 const BMLoop *l_iter, *l_first;
269 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
270 /* The Trapezium Area Rule */
273 const MLoopUV *luv = BM_ELEM_CD_GET_VOID_P(l_iter, cd_loop_uv_offset);
274 const MLoopUV *luv_next = BM_ELEM_CD_GET_VOID_P(l_iter->next, cd_loop_uv_offset);
275 cross += (luv_next->uv[0] - luv->uv[0]) * (luv_next->uv[1] + luv->uv[1]);
276 } while ((l_iter = l_iter->next) != l_first);
277 return fabsf(cross * 0.5f);
281 * compute the perimeter of an ngon
283 float BM_face_calc_perimeter(const BMFace *f)
285 const BMLoop *l_iter, *l_first;
286 float perimeter = 0.0f;
288 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
290 perimeter += len_v3v3(l_iter->v->co, l_iter->next->v->co);
291 } while ((l_iter = l_iter->next) != l_first);
297 * Calculate the perimeter of a ngon in world space.
299 float BM_face_calc_perimeter_with_mat3(const BMFace *f, const float mat3[3][3])
301 const BMLoop *l_iter, *l_first;
303 float perimeter = 0.0f;
305 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
306 mul_v3_m3v3(co, mat3, l_iter->v->co);
309 mul_v3_m3v3(co_next, mat3, l_iter->next->v->co);
310 perimeter += len_v3v3(co, co_next);
311 copy_v3_v3(co, co_next);
312 } while ((l_iter = l_iter->next) != l_first);
318 * Utility function to calculate the edge which is most different from the other two.
320 * \return The first edge index, where the second vertex is ``(index + 1) % 3``.
322 static int bm_vert_tri_find_unique_edge(BMVert *verts[3])
324 /* find the most 'unique' loop, (greatest difference to others) */
326 /* optimized version that avoids sqrt */
328 for (int i_prev = 1, i_curr = 2, i_next = 0; i_next < 3; i_prev = i_curr, i_curr = i_next++) {
329 const float *co = verts[i_curr]->co;
330 const float *co_other[2] = {verts[i_prev]->co, verts[i_next]->co};
332 mid_v3_v3v3(proj_dir, co_other[0], co_other[1]);
333 sub_v3_v3(proj_dir, co);
335 float proj_pair[2][3];
336 project_v3_v3v3(proj_pair[0], co_other[0], proj_dir);
337 project_v3_v3v3(proj_pair[1], co_other[1], proj_dir);
338 difs[i_next] = len_squared_v3v3(proj_pair[0], proj_pair[1]);
341 const float lens[3] = {
342 len_v3v3(verts[0]->co, verts[1]->co),
343 len_v3v3(verts[1]->co, verts[2]->co),
344 len_v3v3(verts[2]->co, verts[0]->co),
346 const float difs[3] = {
347 fabsf(lens[1] - lens[2]),
348 fabsf(lens[2] - lens[0]),
349 fabsf(lens[0] - lens[1]),
353 int order[3] = {0, 1, 2};
354 axis_sort_v3(difs, order);
360 * Calculate a tangent from any 3 vertices.
362 * The tangent aligns to the most *unique* edge
363 * (the edge most unlike the other two).
365 * \param r_tangent: Calculated unit length tangent (return value).
367 void BM_vert_tri_calc_tangent_edge(BMVert *verts[3], float r_tangent[3])
369 const int index = bm_vert_tri_find_unique_edge(verts);
371 sub_v3_v3v3(r_tangent, verts[index]->co, verts[(index + 1) % 3]->co);
373 normalize_v3(r_tangent);
377 * Calculate a tangent from any 3 vertices,
379 * The tangent follows the center-line formed by the most unique edges center
380 * and the opposite vertex.
382 * \param r_tangent: Calculated unit length tangent (return value).
384 void BM_vert_tri_calc_tangent_edge_pair(BMVert *verts[3], float r_tangent[3])
386 const int index = bm_vert_tri_find_unique_edge(verts);
388 const float *v_a = verts[index]->co;
389 const float *v_b = verts[(index + 1) % 3]->co;
390 const float *v_other = verts[(index + 2) % 3]->co;
392 mid_v3_v3v3(r_tangent, v_a, v_b);
393 sub_v3_v3v3(r_tangent, v_other, r_tangent);
395 normalize_v3(r_tangent);
399 * Compute the tangent of the face, using the longest edge.
401 void BM_face_calc_tangent_edge(const BMFace *f, float r_tangent[3])
403 const BMLoop *l_long = BM_face_find_longest_loop((BMFace *)f);
405 sub_v3_v3v3(r_tangent, l_long->v->co, l_long->next->v->co);
407 normalize_v3(r_tangent);
411 * Compute the tangent of the face, using the two longest disconnected edges.
413 * \param r_tangent: Calculated unit length tangent (return value).
415 void BM_face_calc_tangent_edge_pair(const BMFace *f, float r_tangent[3])
420 BM_face_as_array_vert_tri((BMFace *)f, verts);
422 BM_vert_tri_calc_tangent_edge_pair(verts, r_tangent);
424 else if (f->len == 4) {
425 /* Use longest edge pair */
427 float vec[3], vec_a[3], vec_b[3];
429 BM_face_as_array_vert_quad((BMFace *)f, verts);
431 sub_v3_v3v3(vec_a, verts[3]->co, verts[2]->co);
432 sub_v3_v3v3(vec_b, verts[0]->co, verts[1]->co);
433 add_v3_v3v3(r_tangent, vec_a, vec_b);
435 sub_v3_v3v3(vec_a, verts[0]->co, verts[3]->co);
436 sub_v3_v3v3(vec_b, verts[1]->co, verts[2]->co);
437 add_v3_v3v3(vec, vec_a, vec_b);
438 /* use the longest edge length */
439 if (len_squared_v3(r_tangent) < len_squared_v3(vec)) {
440 copy_v3_v3(r_tangent, vec);
444 /* For ngons use two longest disconnected edges */
445 BMLoop *l_long = BM_face_find_longest_loop((BMFace *)f);
446 BMLoop *l_long_other = NULL;
448 float len_max_sq = 0.0f;
449 float vec_a[3], vec_b[3];
451 BMLoop *l_iter = l_long->prev->prev;
452 BMLoop *l_last = l_long->next;
455 const float len_sq = len_squared_v3v3(l_iter->v->co, l_iter->next->v->co);
456 if (len_sq >= len_max_sq) {
457 l_long_other = l_iter;
460 } while ((l_iter = l_iter->prev) != l_last);
462 sub_v3_v3v3(vec_a, l_long->next->v->co, l_long->v->co);
463 sub_v3_v3v3(vec_b, l_long_other->v->co, l_long_other->next->v->co);
464 add_v3_v3v3(r_tangent, vec_a, vec_b);
466 /* Edges may not be opposite side of the ngon,
467 * this could cause problems for ngons with multiple-aligned edges of the same length.
468 * Fallback to longest edge. */
469 if (UNLIKELY(normalize_v3(r_tangent) == 0.0f)) {
470 normalize_v3_v3(r_tangent, vec_a);
476 * Compute the tangent of the face, using the edge farthest away from any vertex in the face.
478 * \param r_tangent: Calculated unit length tangent (return value).
480 void BM_face_calc_tangent_edge_diagonal(const BMFace *f, float r_tangent[3])
482 BMLoop *l_iter, *l_first;
484 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
486 /* In case of degenerate faces. */
489 /* warning: O(n^2) loop here, take care! */
490 float dist_max_sq = 0.0f;
492 BMLoop *l_iter_other = l_iter->next;
493 BMLoop *l_iter_last = l_iter->prev;
495 BLI_assert(!ELEM(l_iter->v->co, l_iter_other->v->co, l_iter_other->next->v->co));
496 float co_other[3], vec[3];
497 closest_to_line_segment_v3(
498 co_other, l_iter->v->co, l_iter_other->v->co, l_iter_other->next->v->co);
499 sub_v3_v3v3(vec, l_iter->v->co, co_other);
501 const float dist_sq = len_squared_v3(vec);
502 if (dist_sq > dist_max_sq) {
503 dist_max_sq = dist_sq;
504 copy_v3_v3(r_tangent, vec);
506 } while ((l_iter_other = l_iter_other->next) != l_iter_last);
507 } while ((l_iter = l_iter->next) != l_first);
509 normalize_v3(r_tangent);
513 * Compute the tangent of the face, using longest distance between vertices on the face.
515 * \note The logic is almost identical to #BM_face_calc_tangent_edge_diagonal
517 void BM_face_calc_tangent_vert_diagonal(const BMFace *f, float r_tangent[3])
519 BMLoop *l_iter, *l_first;
521 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
523 /* In case of degenerate faces. */
526 /* warning: O(n^2) loop here, take care! */
527 float dist_max_sq = 0.0f;
529 BMLoop *l_iter_other = l_iter->next;
532 sub_v3_v3v3(vec, l_iter->v->co, l_iter_other->v->co);
534 const float dist_sq = len_squared_v3(vec);
535 if (dist_sq > dist_max_sq) {
536 dist_max_sq = dist_sq;
537 copy_v3_v3(r_tangent, vec);
539 } while ((l_iter_other = l_iter_other->next) != l_iter);
540 } while ((l_iter = l_iter->next) != l_first);
542 normalize_v3(r_tangent);
546 * Compute a meaningful direction along the face (use for gizmo axis).
548 * \note Callers shouldn't depend on the *exact* method used here.
550 void BM_face_calc_tangent_auto(const BMFace *f, float r_tangent[3])
553 /* most 'unique' edge of a triangle */
555 BM_face_as_array_vert_tri((BMFace *)f, verts);
556 BM_vert_tri_calc_tangent_edge(verts, r_tangent);
558 else if (f->len == 4) {
559 /* longest edge pair of a quad */
560 BM_face_calc_tangent_edge_pair((BMFace *)f, r_tangent);
563 /* longest edge of an ngon */
564 BM_face_calc_tangent_edge((BMFace *)f, r_tangent);
569 * expands bounds (min/max must be initialized).
571 void BM_face_calc_bounds_expand(const BMFace *f, float min[3], float max[3])
573 const BMLoop *l_iter, *l_first;
574 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
576 minmax_v3v3_v3(min, max, l_iter->v->co);
577 } while ((l_iter = l_iter->next) != l_first);
581 * computes center of face in 3d. uses center of bounding box.
583 void BM_face_calc_center_bounds(const BMFace *f, float r_cent[3])
585 const BMLoop *l_iter, *l_first;
586 float min[3], max[3];
588 INIT_MINMAX(min, max);
590 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
592 minmax_v3v3_v3(min, max, l_iter->v->co);
593 } while ((l_iter = l_iter->next) != l_first);
595 mid_v3_v3v3(r_cent, min, max);
599 * computes the center of a face, using the mean average
601 void BM_face_calc_center_median(const BMFace *f, float r_cent[3])
603 const BMLoop *l_iter, *l_first;
607 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
609 add_v3_v3(r_cent, l_iter->v->co);
610 } while ((l_iter = l_iter->next) != l_first);
611 mul_v3_fl(r_cent, 1.0f / (float)f->len);
615 * computes the center of a face, using the mean average
616 * weighted by edge length
618 void BM_face_calc_center_median_weighted(const BMFace *f, float r_cent[3])
620 const BMLoop *l_iter;
621 const BMLoop *l_first;
627 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
628 w_prev = BM_edge_calc_length(l_iter->prev->e);
630 const float w_curr = BM_edge_calc_length(l_iter->e);
631 const float w = (w_curr + w_prev);
632 madd_v3_v3fl(r_cent, l_iter->v->co, w);
635 } while ((l_iter = l_iter->next) != l_first);
638 mul_v3_fl(r_cent, 1.0f / (float)totw);
643 * \brief POLY ROTATE PLANE
645 * Rotates a polygon so that it's
646 * normal is pointing towards the mesh Z axis
648 void poly_rotate_plane(const float normal[3], float (*verts)[3], const uint nverts)
656 axis_dominant_v3_to_m3(mat, normal);
657 for (i = 0; i < nverts; i++) {
658 mul_v2_m3v3(co, mat, verts[i]);
659 copy_v3_v3(verts[i], co);
664 * updates face and vertex normals incident on an edge
666 void BM_edge_normals_update(BMEdge *e)
671 BM_ITER_ELEM (f, &iter, e, BM_FACES_OF_EDGE) {
672 BM_face_normal_update(f);
675 BM_vert_normal_update(e->v1);
676 BM_vert_normal_update(e->v2);
679 static void bm_loop_normal_accum(const BMLoop *l, float no[3])
681 float vec1[3], vec2[3], fac;
683 /* Same calculation used in BM_mesh_normals_update */
684 sub_v3_v3v3(vec1, l->v->co, l->prev->v->co);
685 sub_v3_v3v3(vec2, l->next->v->co, l->v->co);
689 fac = saacos(-dot_v3v3(vec1, vec2));
691 madd_v3_v3fl(no, l->f->no, fac);
694 bool BM_vert_calc_normal_ex(const BMVert *v, const char hflag, float r_no[3])
701 const BMEdge *e = v->e;
704 const BMLoop *l = e->l;
707 if (BM_elem_flag_test(l->f, hflag)) {
708 bm_loop_normal_accum(l, r_no);
712 } while ((l = l->radial_next) != e->l);
714 } while ((e = bmesh_disk_edge_next(e, v)) != v->e);
726 bool BM_vert_calc_normal(const BMVert *v, float r_no[3])
733 const BMEdge *e = v->e;
736 const BMLoop *l = e->l;
739 bm_loop_normal_accum(l, r_no);
742 } while ((l = l->radial_next) != e->l);
744 } while ((e = bmesh_disk_edge_next(e, v)) != v->e);
756 void BM_vert_normal_update_all(BMVert *v)
763 const BMEdge *e = v->e;
766 const BMLoop *l = e->l;
769 BM_face_normal_update(l->f);
770 bm_loop_normal_accum(l, v->no);
773 } while ((l = l->radial_next) != e->l);
775 } while ((e = bmesh_disk_edge_next(e, v)) != v->e);
784 * update a vert normal (but not the faces incident on it)
786 void BM_vert_normal_update(BMVert *v)
788 BM_vert_calc_normal(v, v->no);
792 * \brief BMESH UPDATE FACE NORMAL
794 * Updates the stored normal for the
795 * given face. Requires that a buffer
796 * of sufficient length to store projected
797 * coordinates for all of the face's vertices
798 * is passed in as well.
801 float BM_face_calc_normal(const BMFace *f, float r_no[3])
805 /* common cases first */
808 const float *co1 = (l = BM_FACE_FIRST_LOOP(f))->v->co;
809 const float *co2 = (l = l->next)->v->co;
810 const float *co3 = (l = l->next)->v->co;
811 const float *co4 = (l->next)->v->co;
813 return normal_quad_v3(r_no, co1, co2, co3, co4);
816 const float *co1 = (l = BM_FACE_FIRST_LOOP(f))->v->co;
817 const float *co2 = (l = l->next)->v->co;
818 const float *co3 = (l->next)->v->co;
820 return normal_tri_v3(r_no, co1, co2, co3);
823 return bm_face_calc_poly_normal(f, r_no);
827 void BM_face_normal_update(BMFace *f)
829 BM_face_calc_normal(f, f->no);
832 /* exact same as 'BM_face_calc_normal' but accepts vertex coords */
833 float BM_face_calc_normal_vcos(const BMesh *bm,
836 float const (*vertexCos)[3])
840 /* must have valid index data */
841 BLI_assert((bm->elem_index_dirty & BM_VERT) == 0);
844 /* common cases first */
847 const float *co1 = vertexCos[BM_elem_index_get((l = BM_FACE_FIRST_LOOP(f))->v)];
848 const float *co2 = vertexCos[BM_elem_index_get((l = l->next)->v)];
849 const float *co3 = vertexCos[BM_elem_index_get((l = l->next)->v)];
850 const float *co4 = vertexCos[BM_elem_index_get((l->next)->v)];
852 return normal_quad_v3(r_no, co1, co2, co3, co4);
855 const float *co1 = vertexCos[BM_elem_index_get((l = BM_FACE_FIRST_LOOP(f))->v)];
856 const float *co2 = vertexCos[BM_elem_index_get((l = l->next)->v)];
857 const float *co3 = vertexCos[BM_elem_index_get((l->next)->v)];
859 return normal_tri_v3(r_no, co1, co2, co3);
862 return bm_face_calc_poly_normal_vertex_cos(f, r_no, vertexCos);
868 * Calculate a normal from a vertex cloud.
870 * \note We could make a higher quality version that takes all vertices into account.
871 * Currently it finds 4 outer most points returning it's normal.
873 void BM_verts_calc_normal_from_cloud_ex(
874 BMVert **varr, int varr_len, float r_normal[3], float r_center[3], int *r_index_tangent)
876 const float varr_len_inv = 1.0f / (float)varr_len;
878 /* Get the center point and collect vector array since we loop over these a lot. */
879 float center[3] = {0.0f, 0.0f, 0.0f};
880 for (int i = 0; i < varr_len; i++) {
881 madd_v3_v3fl(center, varr[i]->co, varr_len_inv);
884 /* Find the 'co_a' point from center. */
886 const float *co_a = NULL;
888 float dist_sq_max = -1.0f;
889 for (int i = 0; i < varr_len; i++) {
890 const float dist_sq_test = len_squared_v3v3(varr[i]->co, center);
891 if (!(dist_sq_test <= dist_sq_max)) {
894 dist_sq_max = dist_sq_test;
900 sub_v3_v3v3(dir_a, co_a, center);
903 const float *co_b = NULL;
904 float dir_b[3] = {0.0f, 0.0f, 0.0f};
906 float dist_sq_max = -1.0f;
907 for (int i = 0; i < varr_len; i++) {
908 if (varr[i]->co == co_a) {
912 sub_v3_v3v3(dir_test, varr[i]->co, center);
913 project_plane_normalized_v3_v3v3(dir_test, dir_test, dir_a);
914 const float dist_sq_test = len_squared_v3(dir_test);
915 if (!(dist_sq_test <= dist_sq_max)) {
917 dist_sq_max = dist_sq_test;
918 copy_v3_v3(dir_b, dir_test);
924 normal_tri_v3(r_normal, center, co_a, co_b);
930 const float *co_a_opposite = NULL;
931 const float *co_b_opposite = NULL;
934 float dot_a_min = FLT_MAX;
935 float dot_b_min = FLT_MAX;
936 for (int i = 0; i < varr_len; i++) {
937 const float *co_test = varr[i]->co;
940 if (co_test != co_a) {
941 dot_test = dot_v3v3(dir_a, co_test);
942 if (dot_test < dot_a_min) {
943 dot_a_min = dot_test;
944 co_a_opposite = co_test;
948 if (co_test != co_b) {
949 dot_test = dot_v3v3(dir_b, co_test);
950 if (dot_test < dot_b_min) {
951 dot_b_min = dot_test;
952 co_b_opposite = co_test;
958 normal_quad_v3(r_normal, co_a, co_b, co_a_opposite, co_b_opposite);
961 if (r_center != NULL) {
962 copy_v3_v3(r_center, center);
964 if (r_index_tangent != NULL) {
965 *r_index_tangent = co_a_index;
969 void BM_verts_calc_normal_from_cloud(BMVert **varr, int varr_len, float r_normal[3])
971 BM_verts_calc_normal_from_cloud_ex(varr, varr_len, r_normal, NULL, NULL);
975 * Calculates the face subset normal.
977 float BM_face_calc_normal_subset(const BMLoop *l_first, const BMLoop *l_last, float r_no[3])
979 const float *v_prev, *v_curr;
981 /* Newell's Method */
982 const BMLoop *l_iter = l_first;
983 const BMLoop *l_term = l_last->next;
987 v_prev = l_last->v->co;
989 v_curr = l_iter->v->co;
990 add_newell_cross_v3_v3v3(r_no, v_prev, v_curr);
992 } while ((l_iter = l_iter->next) != l_term);
994 return normalize_v3(r_no);
997 /* exact same as 'BM_face_calc_normal' but accepts vertex coords */
998 void BM_face_calc_center_median_vcos(const BMesh *bm,
1001 float const (*vertexCos)[3])
1003 /* must have valid index data */
1004 BLI_assert((bm->elem_index_dirty & BM_VERT) == 0);
1007 bm_face_calc_poly_center_median_vertex_cos(f, r_cent, vertexCos);
1011 * \brief Face Flip Normal
1013 * Reverses the winding of a face.
1014 * \note This updates the calculated normal.
1016 void BM_face_normal_flip_ex(BMesh *bm,
1018 const int cd_loop_mdisp_offset,
1019 const bool use_loop_mdisp_flip)
1021 bmesh_kernel_loop_reverse(bm, f, cd_loop_mdisp_offset, use_loop_mdisp_flip);
1025 void BM_face_normal_flip(BMesh *bm, BMFace *f)
1027 const int cd_loop_mdisp_offset = CustomData_get_offset(&bm->ldata, CD_MDISPS);
1028 BM_face_normal_flip_ex(bm, f, cd_loop_mdisp_offset, true);
1034 * Projects co onto face f, and returns true if it is inside
1037 * \note this uses a best-axis projection test,
1038 * instead of projecting co directly into f's orientation space,
1039 * so there might be accuracy issues.
1041 bool BM_face_point_inside_test(const BMFace *f, const float co[3])
1043 float axis_mat[3][3];
1044 float(*projverts)[2] = BLI_array_alloca(projverts, f->len);
1050 BLI_assert(BM_face_is_normal_valid(f));
1052 axis_dominant_v3_to_m3(axis_mat, f->no);
1054 mul_v2_m3v3(co_2d, axis_mat, co);
1056 for (i = 0, l_iter = BM_FACE_FIRST_LOOP(f); i < f->len; i++, l_iter = l_iter->next) {
1057 mul_v2_m3v3(projverts[i], axis_mat, l_iter->v->co);
1060 return isect_point_poly_v2(co_2d, projverts, f->len, false);
1064 * \brief BMESH TRIANGULATE FACE
1066 * Breaks all quads and ngons down to triangles.
1067 * It uses polyfill for the ngons splitting, and
1068 * the beautify operator when use_beauty is true.
1070 * \param r_faces_new: if non-null, must be an array of BMFace pointers,
1071 * with a length equal to (f->len - 3). It will be filled with the new
1072 * triangles (not including the original triangle).
1074 * \param r_faces_double: When newly created faces are duplicates of existing faces,
1075 * they're added to this list. Caller must handle de-duplication.
1076 * This is done because its possible _all_ faces exist already,
1077 * and in that case we would have to remove all faces including the one passed,
1078 * which causes complications adding/removing faces while looking over them.
1080 * \note The number of faces is _almost_ always (f->len - 3),
1081 * However there may be faces that already occupying the
1082 * triangles we would make, so the caller must check \a r_faces_new_tot.
1084 * \note use_tag tags new flags and edges.
1086 void BM_face_triangulate(BMesh *bm,
1088 BMFace **r_faces_new,
1089 int *r_faces_new_tot,
1090 BMEdge **r_edges_new,
1091 int *r_edges_new_tot,
1092 LinkNode **r_faces_double,
1093 const int quad_method,
1094 const int ngon_method,
1096 /* use for ngons only! */
1099 /* use for MOD_TRIANGULATE_NGON_BEAUTY only! */
1100 struct Heap *pf_heap)
1102 const int cd_loop_mdisp_offset = CustomData_get_offset(&bm->ldata, CD_MDISPS);
1103 const bool use_beauty = (ngon_method == MOD_TRIANGULATE_NGON_BEAUTY);
1104 BMLoop *l_first, *l_new;
1109 BLI_assert(BM_face_is_normal_valid(f));
1111 /* ensure both are valid or NULL */
1112 BLI_assert((r_faces_new == NULL) == (r_faces_new_tot == NULL));
1114 BLI_assert(f->len > 3);
1117 BMLoop **loops = BLI_array_alloca(loops, f->len);
1118 uint(*tris)[3] = BLI_array_alloca(tris, f->len);
1119 const int totfilltri = f->len - 2;
1120 const int last_tri = f->len - 3;
1126 /* even though we're not using BLI_polyfill, fill in 'tris' and 'loops'
1127 * so we can share code to handle face creation afterwards. */
1128 BMLoop *l_v1, *l_v2;
1130 l_first = BM_FACE_FIRST_LOOP(f);
1132 switch (quad_method) {
1133 case MOD_TRIANGULATE_QUAD_FIXED: {
1135 l_v2 = l_first->next->next;
1138 case MOD_TRIANGULATE_QUAD_ALTERNATE: {
1139 l_v1 = l_first->next;
1140 l_v2 = l_first->prev;
1143 case MOD_TRIANGULATE_QUAD_SHORTEDGE:
1144 case MOD_TRIANGULATE_QUAD_BEAUTY:
1146 BMLoop *l_v3, *l_v4;
1149 l_v1 = l_first->next;
1150 l_v2 = l_first->next->next;
1151 l_v3 = l_first->prev;
1154 if (quad_method == MOD_TRIANGULATE_QUAD_SHORTEDGE) {
1156 d1 = len_squared_v3v3(l_v4->v->co, l_v2->v->co);
1157 d2 = len_squared_v3v3(l_v1->v->co, l_v3->v->co);
1158 split_24 = ((d2 - d1) > 0.0f);
1161 /* first check if the quad is concave on either diagonal */
1162 const int flip_flag = is_quad_flip_v3(
1163 l_v1->v->co, l_v2->v->co, l_v3->v->co, l_v4->v->co);
1164 if (UNLIKELY(flip_flag & (1 << 0))) {
1167 else if (UNLIKELY(flip_flag & (1 << 1))) {
1171 split_24 = (BM_verts_calc_rotate_beauty(l_v1->v, l_v2->v, l_v3->v, l_v4->v, 0, 0) >
1176 /* named confusingly, l_v1 is in fact the second vertex */
1190 loops[1] = l_v1->next;
1192 loops[3] = l_v2->next;
1194 ARRAY_SET_ITEMS(tris[0], 0, 1, 2);
1195 ARRAY_SET_ITEMS(tris[1], 0, 2, 3);
1199 float axis_mat[3][3];
1200 float(*projverts)[2] = BLI_array_alloca(projverts, f->len);
1202 axis_dominant_v3_to_m3_negate(axis_mat, f->no);
1204 for (i = 0, l_iter = BM_FACE_FIRST_LOOP(f); i < f->len; i++, l_iter = l_iter->next) {
1206 mul_v2_m3v3(projverts[i], axis_mat, l_iter->v->co);
1209 BLI_polyfill_calc_arena(projverts, f->len, 1, tris, pf_arena);
1212 BLI_polyfill_beautify(projverts, f->len, tris, pf_arena, pf_heap);
1215 BLI_memarena_clear(pf_arena);
1218 if (cd_loop_mdisp_offset != -1) {
1219 BM_face_calc_center_median(f, f_center);
1222 /* loop over calculated triangles and create new geometry */
1223 for (i = 0; i < totfilltri; i++) {
1224 BMLoop *l_tri[3] = {loops[tris[i][0]], loops[tris[i][1]], loops[tris[i][2]]};
1226 BMVert *v_tri[3] = {l_tri[0]->v, l_tri[1]->v, l_tri[2]->v};
1228 f_new = BM_face_create_verts(bm, v_tri, 3, f, BM_CREATE_NOP, true);
1229 l_new = BM_FACE_FIRST_LOOP(f_new);
1231 BLI_assert(v_tri[0] == l_new->v);
1233 /* check for duplicate */
1234 if (l_new->radial_next != l_new) {
1235 BMLoop *l_iter = l_new->radial_next;
1237 if (UNLIKELY((l_iter->f->len == 3) && (l_new->prev->v == l_iter->prev->v))) {
1238 /* Check the last tri because we swap last f_new with f at the end... */
1239 BLI_linklist_prepend(r_faces_double, (i != last_tri) ? f_new : f);
1242 } while ((l_iter = l_iter->radial_next) != l_new);
1246 BM_elem_attrs_copy(bm, bm, l_tri[0], l_new);
1247 BM_elem_attrs_copy(bm, bm, l_tri[1], l_new->next);
1248 BM_elem_attrs_copy(bm, bm, l_tri[2], l_new->prev);
1250 /* add all but the last face which is swapped and removed (below) */
1251 if (i != last_tri) {
1253 BM_elem_flag_enable(f_new, BM_ELEM_TAG);
1256 r_faces_new[nf_i++] = f_new;
1260 if (use_tag || r_edges_new) {
1261 /* new faces loops */
1264 l_iter = l_first = l_new;
1266 BMEdge *e = l_iter->e;
1267 /* Confusing! if its not a boundary now, we know it will be later since this will be an
1268 * edge of one of the new faces which we're in the middle of creating. */
1269 bool is_new_edge = (l_iter == l_iter->radial_next);
1273 BM_elem_flag_enable(e, BM_ELEM_TAG);
1276 r_edges_new[ne_i++] = e;
1279 /* note, never disable tag's */
1280 } while ((l_iter = l_iter->next) != l_first);
1283 if (cd_loop_mdisp_offset != -1) {
1284 float f_new_center[3];
1285 BM_face_calc_center_median(f_new, f_new_center);
1286 BM_face_interp_multires_ex(bm, f_new, f, f_new_center, f_center, cd_loop_mdisp_offset);
1291 /* we can't delete the real face, because some of the callers expect it to remain valid.
1292 * so swap data and delete the last created tri */
1293 bmesh_face_swap_data(f, f_new);
1294 BM_face_kill(bm, f_new);
1297 bm->elem_index_dirty |= BM_FACE;
1299 if (r_faces_new_tot) {
1300 *r_faces_new_tot = nf_i;
1303 if (r_edges_new_tot) {
1304 *r_edges_new_tot = ne_i;
1309 * each pair of loops defines a new edge, a split. this function goes
1310 * through and sets pairs that are geometrically invalid to null. a
1311 * split is invalid, if it forms a concave angle or it intersects other
1312 * edges in the face, or it intersects another split. in the case of
1313 * intersecting splits, only the first of the set of intersecting
1316 void BM_face_splits_check_legal(BMesh *bm, BMFace *f, BMLoop *(*loops)[2], int len)
1318 float out[2] = {-FLT_MAX, -FLT_MAX};
1319 float center[2] = {0.0f, 0.0f};
1320 float axis_mat[3][3];
1321 float(*projverts)[2] = BLI_array_alloca(projverts, f->len);
1322 const float *(*edgeverts)[2] = BLI_array_alloca(edgeverts, len);
1326 BLI_assert(BM_face_is_normal_valid(f));
1328 axis_dominant_v3_to_m3(axis_mat, f->no);
1330 for (i = 0, l = BM_FACE_FIRST_LOOP(f); i < f->len; i++, l = l->next) {
1331 mul_v2_m3v3(projverts[i], axis_mat, l->v->co);
1332 add_v2_v2(center, projverts[i]);
1335 /* first test for completely convex face */
1336 if (is_poly_convex_v2(projverts, f->len)) {
1340 mul_v2_fl(center, 1.0f / f->len);
1342 for (i = 0, l = BM_FACE_FIRST_LOOP(f); i < f->len; i++, l = l->next) {
1343 BM_elem_index_set(l, i); /* set_dirty */
1345 /* center the projection for maximum accuracy */
1346 sub_v2_v2(projverts[i], center);
1348 out[0] = max_ff(out[0], projverts[i][0]);
1349 out[1] = max_ff(out[1], projverts[i][1]);
1351 bm->elem_index_dirty |= BM_LOOP;
1353 /* ensure we are well outside the face bounds (value is arbitrary) */
1354 add_v2_fl(out, 1.0f);
1356 for (i = 0; i < len; i++) {
1357 edgeverts[i][0] = projverts[BM_elem_index_get(loops[i][0])];
1358 edgeverts[i][1] = projverts[BM_elem_index_get(loops[i][1])];
1361 /* do convexity test */
1362 for (i = 0; i < len; i++) {
1364 mid_v2_v2v2(mid, edgeverts[i][0], edgeverts[i][1]);
1368 for (j = 0, j_prev = f->len - 1; j < f->len; j_prev = j++) {
1369 const float *f_edge[2] = {projverts[j_prev], projverts[j]};
1370 if (isect_seg_seg_v2(UNPACK2(f_edge), mid, out) == ISECT_LINE_LINE_CROSS) {
1375 if (isect % 2 == 0) {
1380 #define EDGE_SHARE_VERT(e1, e2) \
1381 ((ELEM((e1)[0], (e2)[0], (e2)[1])) || (ELEM((e1)[1], (e2)[0], (e2)[1])))
1383 /* do line crossing tests */
1384 for (i = 0, i_prev = f->len - 1; i < f->len; i_prev = i++) {
1385 const float *f_edge[2] = {projverts[i_prev], projverts[i]};
1386 for (j = 0; j < len; j++) {
1387 if ((loops[j][0] != NULL) && !EDGE_SHARE_VERT(f_edge, edgeverts[j])) {
1388 if (isect_seg_seg_v2(UNPACK2(f_edge), UNPACK2(edgeverts[j])) == ISECT_LINE_LINE_CROSS) {
1395 /* self intersect tests */
1396 for (i = 0; i < len; i++) {
1398 for (j = i + 1; j < len; j++) {
1399 if ((loops[j][0] != NULL) && !EDGE_SHARE_VERT(edgeverts[i], edgeverts[j])) {
1400 if (isect_seg_seg_v2(UNPACK2(edgeverts[i]), UNPACK2(edgeverts[j])) ==
1401 ISECT_LINE_LINE_CROSS) {
1410 #undef EDGE_SHARE_VERT
1414 * This simply checks that the verts don't connect faces which would have more optimal splits.
1415 * but _not_ check for correctness.
1417 void BM_face_splits_check_optimal(BMFace *f, BMLoop *(*loops)[2], int len)
1421 for (i = 0; i < len; i++) {
1422 BMLoop *l_a_dummy, *l_b_dummy;
1423 if (f != BM_vert_pair_share_face_by_angle(
1424 loops[i][0]->v, loops[i][1]->v, &l_a_dummy, &l_b_dummy, false)) {
1431 * Small utility functions for fast access
1433 * faster alternative to:
1434 * BM_iter_as_array(bm, BM_VERTS_OF_FACE, f, (void **)v, 3);
1436 void BM_face_as_array_vert_tri(BMFace *f, BMVert *r_verts[3])
1438 BMLoop *l = BM_FACE_FIRST_LOOP(f);
1440 BLI_assert(f->len == 3);
1450 * faster alternative to:
1451 * BM_iter_as_array(bm, BM_VERTS_OF_FACE, f, (void **)v, 4);
1453 void BM_face_as_array_vert_quad(BMFace *f, BMVert *r_verts[4])
1455 BMLoop *l = BM_FACE_FIRST_LOOP(f);
1457 BLI_assert(f->len == 4);
1469 * Small utility functions for fast access
1471 * faster alternative to:
1472 * BM_iter_as_array(bm, BM_LOOPS_OF_FACE, f, (void **)l, 3);
1474 void BM_face_as_array_loop_tri(BMFace *f, BMLoop *r_loops[3])
1476 BMLoop *l = BM_FACE_FIRST_LOOP(f);
1478 BLI_assert(f->len == 3);
1488 * faster alternative to:
1489 * BM_iter_as_array(bm, BM_LOOPS_OF_FACE, f, (void **)l, 4);
1491 void BM_face_as_array_loop_quad(BMFace *f, BMLoop *r_loops[4])
1493 BMLoop *l = BM_FACE_FIRST_LOOP(f);
1495 BLI_assert(f->len == 4);
1507 * \brief BM_mesh_calc_tessellation get the looptris and its number from a certain bmesh
1510 * \note \a looptris Must be pre-allocated to at least the size of given by: poly_to_tri_count
1512 void BM_mesh_calc_tessellation(BMesh *bm, BMLoop *(*looptris)[3], int *r_looptris_tot)
1514 /* use this to avoid locking pthread for _every_ polygon
1515 * and calling the fill function */
1516 #define USE_TESSFACE_SPEEDUP
1518 /* this assumes all faces can be scan-filled, which isn't always true,
1519 * worst case we over alloc a little which is acceptable */
1521 const int looptris_tot = poly_to_tri_count(bm->totface, bm->totloop);
1528 MemArena *arena = NULL;
1530 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
1531 /* don't consider two-edged faces */
1532 if (UNLIKELY(efa->len < 3)) {
1536 #ifdef USE_TESSFACE_SPEEDUP
1538 /* no need to ensure the loop order, we know its ok */
1540 else if (efa->len == 3) {
1543 BM_ITER_ELEM_INDEX(l, &liter, efa, BM_LOOPS_OF_FACE, j) {
1548 /* more cryptic but faster */
1550 BMLoop **l_ptr = looptris[i++];
1551 l_ptr[0] = l = BM_FACE_FIRST_LOOP(efa);
1552 l_ptr[1] = l = l->next;
1556 else if (efa->len == 4) {
1560 BLI_array_grow_items(looptris, 2);
1561 BM_ITER_ELEM_INDEX(l, &liter, efa, BM_LOOPS_OF_FACE, j) {
1565 looptris[i][0] = ltmp[0];
1566 looptris[i][1] = ltmp[1];
1567 looptris[i][2] = ltmp[2];
1570 looptris[i][0] = ltmp[0];
1571 looptris[i][1] = ltmp[2];
1572 looptris[i][2] = ltmp[3];
1575 /* more cryptic but faster */
1577 BMLoop **l_ptr_a = looptris[i++];
1578 BMLoop **l_ptr_b = looptris[i++];
1579 (l_ptr_a[0] = l_ptr_b[0] = l = BM_FACE_FIRST_LOOP(efa));
1580 (l_ptr_a[1] = l = l->next);
1581 (l_ptr_a[2] = l_ptr_b[1] = l = l->next);
1582 (l_ptr_b[2] = l->next);
1585 if (UNLIKELY(is_quad_flip_v3_first_third_fast(
1586 l_ptr_a[0]->v->co, l_ptr_a[1]->v->co, l_ptr_a[2]->v->co, l_ptr_b[2]->v->co))) {
1587 /* flip out of degenerate 0-2 state. */
1588 l_ptr_a[2] = l_ptr_b[2];
1589 l_ptr_b[0] = l_ptr_a[1];
1593 #endif /* USE_TESSFACE_SPEEDUP */
1602 float axis_mat[3][3];
1603 float(*projverts)[2];
1606 const int totfilltri = efa->len - 2;
1608 if (UNLIKELY(arena == NULL)) {
1609 arena = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, __func__);
1612 tris = BLI_memarena_alloc(arena, sizeof(*tris) * totfilltri);
1613 l_arr = BLI_memarena_alloc(arena, sizeof(*l_arr) * efa->len);
1614 projverts = BLI_memarena_alloc(arena, sizeof(*projverts) * efa->len);
1616 axis_dominant_v3_to_m3_negate(axis_mat, efa->no);
1619 l_iter = l_first = BM_FACE_FIRST_LOOP(efa);
1622 mul_v2_m3v3(projverts[j], axis_mat, l_iter->v->co);
1624 } while ((l_iter = l_iter->next) != l_first);
1626 BLI_polyfill_calc_arena(projverts, efa->len, 1, tris, arena);
1628 for (j = 0; j < totfilltri; j++) {
1629 BMLoop **l_ptr = looptris[i++];
1630 uint *tri = tris[j];
1632 l_ptr[0] = l_arr[tri[0]];
1633 l_ptr[1] = l_arr[tri[1]];
1634 l_ptr[2] = l_arr[tri[2]];
1637 BLI_memarena_clear(arena);
1642 BLI_memarena_free(arena);
1646 *r_looptris_tot = i;
1648 BLI_assert(i <= looptris_tot);
1650 #undef USE_TESSFACE_SPEEDUP
1654 * A version of #BM_mesh_calc_tessellation that avoids degenerate triangles.
1656 void BM_mesh_calc_tessellation_beauty(BMesh *bm, BMLoop *(*looptris)[3], int *r_looptris_tot)
1658 /* this assumes all faces can be scan-filled, which isn't always true,
1659 * worst case we over alloc a little which is acceptable */
1661 const int looptris_tot = poly_to_tri_count(bm->totface, bm->totloop);
1668 MemArena *pf_arena = NULL;
1671 Heap *pf_heap = NULL;
1673 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
1674 /* don't consider two-edged faces */
1675 if (UNLIKELY(efa->len < 3)) {
1678 else if (efa->len == 3) {
1680 BMLoop **l_ptr = looptris[i++];
1681 l_ptr[0] = l = BM_FACE_FIRST_LOOP(efa);
1682 l_ptr[1] = l = l->next;
1685 else if (efa->len == 4) {
1686 BMLoop *l_v1 = BM_FACE_FIRST_LOOP(efa);
1687 BMLoop *l_v2 = l_v1->next;
1688 BMLoop *l_v3 = l_v2->next;
1689 BMLoop *l_v4 = l_v1->prev;
1691 /* #BM_verts_calc_rotate_beauty performs excessive checks we don't need!
1692 * It's meant for rotating edges, it also calculates a new normal.
1694 * Use #BLI_polyfill_beautify_quad_rotate_calc since we have the normal.
1697 const bool split_13 = (BM_verts_calc_rotate_beauty(
1698 l_v1->v, l_v2->v, l_v3->v, l_v4->v, 0, 0) < 0.0f);
1700 float axis_mat[3][3], v_quad[4][2];
1701 axis_dominant_v3_to_m3(axis_mat, efa->no);
1702 mul_v2_m3v3(v_quad[0], axis_mat, l_v1->v->co);
1703 mul_v2_m3v3(v_quad[1], axis_mat, l_v2->v->co);
1704 mul_v2_m3v3(v_quad[2], axis_mat, l_v3->v->co);
1705 mul_v2_m3v3(v_quad[3], axis_mat, l_v4->v->co);
1707 const bool split_13 = BLI_polyfill_beautify_quad_rotate_calc(
1708 v_quad[0], v_quad[1], v_quad[2], v_quad[3]) < 0.0f;
1711 BMLoop **l_ptr_a = looptris[i++];
1712 BMLoop **l_ptr_b = looptris[i++];
1739 float axis_mat[3][3];
1740 float(*projverts)[2];
1741 unsigned int(*tris)[3];
1743 const int totfilltri = efa->len - 2;
1745 if (UNLIKELY(pf_arena == NULL)) {
1746 pf_arena = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, __func__);
1747 pf_heap = BLI_heap_new_ex(BLI_POLYFILL_ALLOC_NGON_RESERVE);
1750 tris = BLI_memarena_alloc(pf_arena, sizeof(*tris) * totfilltri);
1751 l_arr = BLI_memarena_alloc(pf_arena, sizeof(*l_arr) * efa->len);
1752 projverts = BLI_memarena_alloc(pf_arena, sizeof(*projverts) * efa->len);
1754 axis_dominant_v3_to_m3_negate(axis_mat, efa->no);
1757 l_iter = l_first = BM_FACE_FIRST_LOOP(efa);
1760 mul_v2_m3v3(projverts[j], axis_mat, l_iter->v->co);
1762 } while ((l_iter = l_iter->next) != l_first);
1764 BLI_polyfill_calc_arena(projverts, efa->len, 1, tris, pf_arena);
1766 BLI_polyfill_beautify(projverts, efa->len, tris, pf_arena, pf_heap);
1768 for (j = 0; j < totfilltri; j++) {
1769 BMLoop **l_ptr = looptris[i++];
1770 unsigned int *tri = tris[j];
1772 l_ptr[0] = l_arr[tri[0]];
1773 l_ptr[1] = l_arr[tri[1]];
1774 l_ptr[2] = l_arr[tri[2]];
1777 BLI_memarena_clear(pf_arena);
1782 BLI_memarena_free(pf_arena);
1784 BLI_heap_free(pf_heap, NULL);
1787 *r_looptris_tot = i;
1789 BLI_assert(i <= looptris_tot);