Blender V4.5
bmesh_polygon.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
11
12#include "DNA_modifier_types.h"
13
14#include "BLI_alloca.h"
15#include "BLI_linklist.h"
16#include "BLI_math_base.hh"
17#include "BLI_math_geom.h"
18#include "BLI_math_matrix.h"
19#include "BLI_math_vector.h"
20#include "BLI_memarena.h"
21#include "BLI_polyfill_2d.h"
23
24#include "bmesh.hh"
25#include "bmesh_tools.hh"
26
27#include "BKE_customdata.hh"
28
30
31using blender::float3;
32using blender::Span;
33
39static float bm_face_calc_poly_normal(const BMFace *f, float n[3])
40{
41 BMLoop *l_first = BM_FACE_FIRST_LOOP(f);
42 BMLoop *l_iter = l_first;
43 const float *v_prev = l_first->prev->v->co;
44 const float *v_curr = l_first->v->co;
45
46 zero_v3(n);
47
48 /* Newell's Method */
49 do {
50 add_newell_cross_v3_v3v3(n, v_prev, v_curr);
51
52 l_iter = l_iter->next;
53 v_prev = v_curr;
54 v_curr = l_iter->v->co;
55
56 } while (l_iter != l_first);
57
58 return normalize_v3(n);
59}
60
68 float r_no[3],
69 const Span<float3> vertexCos)
70{
71 BMLoop *l_first = BM_FACE_FIRST_LOOP(f);
72 BMLoop *l_iter = l_first;
73 const float *v_prev = vertexCos[BM_elem_index_get(l_first->prev->v)];
74 const float *v_curr = vertexCos[BM_elem_index_get(l_first->v)];
75
76 zero_v3(r_no);
77
78 /* Newell's Method */
79 do {
80 add_newell_cross_v3_v3v3(r_no, v_prev, v_curr);
81
82 l_iter = l_iter->next;
83 v_prev = v_curr;
84 v_curr = vertexCos[BM_elem_index_get(l_iter->v)];
85 } while (l_iter != l_first);
86
87 return normalize_v3(r_no);
88}
89
94 const BMFace *f, float r_cent[3], const blender::Span<blender::float3> vert_positions)
95{
96 const BMLoop *l_first, *l_iter;
97
98 zero_v3(r_cent);
99
100 /* Newell's Method */
101 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
102 do {
103 add_v3_v3(r_cent, vert_positions[BM_elem_index_get(l_iter->v)]);
104 } while ((l_iter = l_iter->next) != l_first);
105 mul_v3_fl(r_cent, 1.0f / f->len);
106}
107
109 const bool use_fixed_quad,
110 BMLoop **r_loops,
111 uint (*r_index)[3])
112{
113 BMLoop *l_first = BM_FACE_FIRST_LOOP(f);
114 BMLoop *l_iter;
115
116 if (f->len == 3) {
117 *r_loops++ = (l_iter = l_first);
118 *r_loops++ = (l_iter = l_iter->next);
119 *r_loops++ = (l_iter->next);
120
121 r_index[0][0] = 0;
122 r_index[0][1] = 1;
123 r_index[0][2] = 2;
124 }
125 else if (f->len == 4 && use_fixed_quad) {
126 *r_loops++ = (l_iter = l_first);
127 *r_loops++ = (l_iter = l_iter->next);
128 *r_loops++ = (l_iter = l_iter->next);
129 *r_loops++ = (l_iter->next);
130
131 r_index[0][0] = 0;
132 r_index[0][1] = 1;
133 r_index[0][2] = 2;
134
135 r_index[1][0] = 0;
136 r_index[1][1] = 2;
137 r_index[1][2] = 3;
138 }
139 else {
140 float axis_mat[3][3];
141 float(*projverts)[2] = BLI_array_alloca(projverts, f->len);
142 int j;
143
144 axis_dominant_v3_to_m3_negate(axis_mat, f->no);
145
146 j = 0;
147 l_iter = l_first;
148 do {
149 mul_v2_m3v3(projverts[j], axis_mat, l_iter->v->co);
150 r_loops[j] = l_iter;
151 j++;
152 } while ((l_iter = l_iter->next) != l_first);
153
154 /* complete the loop */
155 BLI_polyfill_calc(projverts, f->len, 1, r_index);
156 }
157}
158
159void BM_face_calc_point_in_face(const BMFace *f, float r_co[3])
160{
161 const BMLoop *ltri[3];
162
163 if (f->len == 3) {
164 const BMLoop *l = BM_FACE_FIRST_LOOP(f);
165 ARRAY_SET_ITEMS(ltri, l, l->next, l->prev);
166 }
167 else {
168 /* tessellation here seems overkill when in many cases this will be the center,
169 * but without this we can't be sure the point is inside a concave face. */
170 const int tottri = f->len - 2;
171 BMLoop **loops = BLI_array_alloca(loops, f->len);
172 uint(*index)[3] = BLI_array_alloca(index, tottri);
173 int j;
174 int j_best = 0; /* use as fallback when unset */
175 float area_best = -1.0f;
176
177 BM_face_calc_tessellation(f, false, loops, index);
178
179 for (j = 0; j < tottri; j++) {
180 const float *p1 = loops[index[j][0]]->v->co;
181 const float *p2 = loops[index[j][1]]->v->co;
182 const float *p3 = loops[index[j][2]]->v->co;
183 const float area = area_squared_tri_v3(p1, p2, p3);
184 if (area > area_best) {
185 j_best = j;
186 area_best = area;
187 }
188 }
189
191 ltri, loops[index[j_best][0]], loops[index[j_best][1]], loops[index[j_best][2]]);
192 }
193
194 mid_v3_v3v3v3(r_co, ltri[0]->v->co, ltri[1]->v->co, ltri[2]->v->co);
195}
196
198{
199 /* inline 'area_poly_v3' logic, avoid creating a temp array */
200 const BMLoop *l_iter, *l_first;
201 float n[3];
202
203 zero_v3(n);
204 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
205 do {
206 add_newell_cross_v3_v3v3(n, l_iter->v->co, l_iter->next->v->co);
207 } while ((l_iter = l_iter->next) != l_first);
208 return len_v3(n) * 0.5f;
209}
210
211float BM_face_calc_area_with_mat3(const BMFace *f, const float mat3[3][3])
212{
213 /* inline 'area_poly_v3' logic, avoid creating a temp array */
214 const BMLoop *l_iter, *l_first;
215 float co[3];
216 float n[3];
217
218 zero_v3(n);
219 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
220 mul_v3_m3v3(co, mat3, l_iter->v->co);
221 do {
222 float co_next[3];
223 mul_v3_m3v3(co_next, mat3, l_iter->next->v->co);
224 add_newell_cross_v3_v3v3(n, co, co_next);
225 copy_v3_v3(co, co_next);
226 } while ((l_iter = l_iter->next) != l_first);
227 return len_v3(n) * 0.5f;
228}
229
230float BM_face_calc_area_uv_signed(const BMFace *f, int cd_loop_uv_offset)
231{
232 /* inline 'area_poly_v2' logic, avoid creating a temp array */
233 const BMLoop *l_iter, *l_first;
234
235 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
236 /* Green's theorem applied to area of a polygon.
237 * TODO: `cross` should be of type `double` to reduce rounding error. */
238 float cross = 0.0f;
239 do {
240 const float *luv = BM_ELEM_CD_GET_FLOAT_P(l_iter, cd_loop_uv_offset);
241 const float *luv_next = BM_ELEM_CD_GET_FLOAT_P(l_iter->next, cd_loop_uv_offset);
242 cross += (luv_next[0] - luv[0]) * (luv_next[1] + luv[1]);
243 } while ((l_iter = l_iter->next) != l_first);
244 return cross * 0.5f;
245}
246
247float BM_face_calc_area_uv(const BMFace *f, int cd_loop_uv_offset)
248{
249 return fabsf(BM_face_calc_area_uv_signed(f, cd_loop_uv_offset));
250}
251
253{
254 const BMLoop *l_iter, *l_first;
255 float perimeter = 0.0f;
256
257 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
258 do {
259 perimeter += len_v3v3(l_iter->v->co, l_iter->next->v->co);
260 } while ((l_iter = l_iter->next) != l_first);
261
262 return perimeter;
263}
264
265float BM_face_calc_perimeter_with_mat3(const BMFace *f, const float mat3[3][3])
266{
267 const BMLoop *l_iter, *l_first;
268 float co[3];
269 float perimeter = 0.0f;
270
271 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
272 mul_v3_m3v3(co, mat3, l_iter->v->co);
273 do {
274 float co_next[3];
275 mul_v3_m3v3(co_next, mat3, l_iter->next->v->co);
276 perimeter += len_v3v3(co, co_next);
277 copy_v3_v3(co, co_next);
278 } while ((l_iter = l_iter->next) != l_first);
279
280 return perimeter;
281}
282
289{
290/* find the most 'unique' loop, (greatest difference to others) */
291#if 1
292 /* Optimized version that avoids `sqrt`. */
293 float difs[3];
294 for (int i_prev = 1, i_curr = 2, i_next = 0; i_next < 3; i_prev = i_curr, i_curr = i_next++) {
295 const float *co = verts[i_curr]->co;
296 const float *co_other[2] = {verts[i_prev]->co, verts[i_next]->co};
297 float proj_dir[3];
298 mid_v3_v3v3(proj_dir, co_other[0], co_other[1]);
299 sub_v3_v3(proj_dir, co);
300
301 float proj_pair[2][3];
302 project_v3_v3v3(proj_pair[0], co_other[0], proj_dir);
303 project_v3_v3v3(proj_pair[1], co_other[1], proj_dir);
304 difs[i_next] = len_squared_v3v3(proj_pair[0], proj_pair[1]);
305 }
306#else
307 const float lens[3] = {
308 len_v3v3(verts[0]->co, verts[1]->co),
309 len_v3v3(verts[1]->co, verts[2]->co),
310 len_v3v3(verts[2]->co, verts[0]->co),
311 };
312 const float difs[3] = {
313 fabsf(lens[1] - lens[2]),
314 fabsf(lens[2] - lens[0]),
315 fabsf(lens[0] - lens[1]),
316 };
317#endif
318
319 int order[3] = {0, 1, 2};
320 axis_sort_v3(difs, order);
321
322 return order[0];
323}
324
326{
327 const int index = bm_vert_tri_find_unique_edge(verts);
328 const int index_next = (index + 1) % 3;
329
330 sub_v3_v3v3(r_tangent, verts[index]->co, verts[index_next]->co);
331 normalize_v3(r_tangent);
332}
333
335 float r_tangent_a[3],
336 float r_tangent_b[3])
337{
338 const int index = bm_vert_tri_find_unique_edge(verts);
339 const int index_next = (index + 1) % 3;
340 const int index_prev = (index_next + 1) % 3;
341
342 sub_v3_v3v3(r_tangent_a, verts[index]->co, verts[index_next]->co);
343 normalize_v3(r_tangent_a);
344
345 /* Pick the adjacent loop that is least co-linear. */
346 float vec_prev[3], vec_next[3];
347 float tmp_prev[3], tmp_next[3];
348
349 sub_v3_v3v3(vec_prev, verts[index_prev]->co, verts[index]->co);
350 sub_v3_v3v3(vec_next, verts[index_next]->co, verts[index_prev]->co);
351
352 cross_v3_v3v3(tmp_prev, r_tangent_a, vec_prev);
353 cross_v3_v3v3(tmp_next, r_tangent_a, vec_next);
354
355 normalize_v3_v3(r_tangent_b,
356 len_squared_v3(tmp_next) > len_squared_v3(tmp_prev) ? vec_next : vec_prev);
357}
358
360{
361 const int index = bm_vert_tri_find_unique_edge(verts);
362
363 const float *v_a = verts[index]->co;
364 const float *v_b = verts[(index + 1) % 3]->co;
365 const float *v_other = verts[(index + 2) % 3]->co;
366
367 mid_v3_v3v3(r_tangent, v_a, v_b);
368 sub_v3_v3v3(r_tangent, v_other, r_tangent);
369
370 normalize_v3(r_tangent);
371}
372
373void BM_face_calc_tangent_from_edge(const BMFace *f, float r_tangent[3])
374{
375 const BMLoop *l_long = BM_face_find_longest_loop((BMFace *)f);
376
377 sub_v3_v3v3(r_tangent, l_long->v->co, l_long->next->v->co);
378
379 normalize_v3(r_tangent);
380}
381
382static void bm_face_calc_tangent_from_quad_edge_pair(const BMFace *f, float r_tangent[3])
383{
384 BMVert *verts[4];
385 float vec[3], vec_a[3], vec_b[3];
386
388
389 sub_v3_v3v3(vec_a, verts[3]->co, verts[2]->co);
390 sub_v3_v3v3(vec_b, verts[0]->co, verts[1]->co);
391 add_v3_v3v3(r_tangent, vec_a, vec_b);
392
393 sub_v3_v3v3(vec_a, verts[0]->co, verts[3]->co);
394 sub_v3_v3v3(vec_b, verts[1]->co, verts[2]->co);
395 add_v3_v3v3(vec, vec_a, vec_b);
396 /* use the longest edge length */
397 if (len_squared_v3(r_tangent) < len_squared_v3(vec)) {
398 copy_v3_v3(r_tangent, vec);
399 }
400 normalize_v3(r_tangent);
401}
402
404 float r_tangent_a[3],
405 float r_tangent_b[3])
406{
407 BLI_assert(f->len == 4);
408 BMVert *verts[4];
409 float vec_a[3], vec_b[3];
410
412
413 sub_v3_v3v3(vec_a, verts[3]->co, verts[2]->co);
414 sub_v3_v3v3(vec_b, verts[0]->co, verts[1]->co);
415 add_v3_v3v3(r_tangent_a, vec_a, vec_b);
416
417 sub_v3_v3v3(vec_a, verts[0]->co, verts[3]->co);
418 sub_v3_v3v3(vec_b, verts[1]->co, verts[2]->co);
419 add_v3_v3v3(r_tangent_b, vec_a, vec_b);
420
421 /* `r_tangent_a` always gets the longest edge. */
422 if (normalize_v3(r_tangent_a) < normalize_v3(r_tangent_b)) {
423 swap_v3_v3(r_tangent_a, r_tangent_b);
424 }
425}
426
428 float r_tangent_a[3],
429 float r_tangent_b[3])
430{
431 const BMLoop *l_long = BM_face_find_longest_loop((BMFace *)f);
432
433 sub_v3_v3v3(r_tangent_a, l_long->v->co, l_long->next->v->co);
434 normalize_v3(r_tangent_a);
435
436 /* Pick the adjacent loop that is least co-linear. */
437 float vec_prev[3], vec_next[3];
438 float tmp_prev[3], tmp_next[3];
439
440 sub_v3_v3v3(vec_prev, l_long->prev->v->co, l_long->v->co);
441 sub_v3_v3v3(vec_next, l_long->next->v->co, l_long->next->next->v->co);
442
443 cross_v3_v3v3(tmp_prev, r_tangent_a, vec_prev);
444 cross_v3_v3v3(tmp_next, r_tangent_a, vec_next);
445
446 normalize_v3_v3(r_tangent_b,
447 len_squared_v3(tmp_next) > len_squared_v3(tmp_prev) ? vec_next : vec_prev);
448}
449
450void BM_face_calc_tangent_from_edge_pair(const BMFace *f, float r_tangent[3])
451{
452 if (f->len == 3) {
453 BMVert *verts[3];
454
456
458 }
459 else if (f->len == 4) {
460 /* Use longest edge pair */
461 float r_tangent_dummy[3];
462 bm_face_calc_tangent_pair_from_quad_edge_pair(f, r_tangent, r_tangent_dummy);
463 }
464 else {
465 /* For ngons use two longest disconnected edges */
467 BMLoop *l_long_other = nullptr;
468
469 float len_max_sq = 0.0f;
470 float vec_a[3], vec_b[3];
471
472 BMLoop *l_iter = l_long->prev->prev;
473 BMLoop *l_last = l_long->next;
474
475 do {
476 const float len_sq = len_squared_v3v3(l_iter->v->co, l_iter->next->v->co);
477 if (len_sq >= len_max_sq) {
478 l_long_other = l_iter;
479 len_max_sq = len_sq;
480 }
481 } while ((l_iter = l_iter->prev) != l_last);
482
483 sub_v3_v3v3(vec_a, l_long->next->v->co, l_long->v->co);
484 sub_v3_v3v3(vec_b, l_long_other->v->co, l_long_other->next->v->co);
485 add_v3_v3v3(r_tangent, vec_a, vec_b);
486
487 /* Edges may not be opposite side of the ngon,
488 * this could cause problems for ngons with multiple-aligned edges of the same length.
489 * Fall back to longest edge. */
490 if (UNLIKELY(normalize_v3(r_tangent) == 0.0f)) {
491 normalize_v3_v3(r_tangent, vec_a);
492 }
493 }
494}
495
496void BM_face_calc_tangent_from_edge_diagonal(const BMFace *f, float r_tangent[3])
497{
498 BMLoop *l_iter, *l_first;
499
500 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
501
502 /* In case of degenerate faces. */
503 zero_v3(r_tangent);
504
505 /* WARNING: O(n^2) loop here, take care! */
506 float dist_max_sq = 0.0f;
507 do {
508 BMLoop *l_iter_other = l_iter->next;
509 BMLoop *l_iter_last = l_iter->prev;
510 do {
511 BLI_assert(!ELEM(l_iter->v, l_iter_other->v, l_iter_other->next->v));
512 float co_other[3], vec[3];
514 co_other, l_iter->v->co, l_iter_other->v->co, l_iter_other->next->v->co);
515 sub_v3_v3v3(vec, l_iter->v->co, co_other);
516
517 const float dist_sq = len_squared_v3(vec);
518 if (dist_sq > dist_max_sq) {
519 dist_max_sq = dist_sq;
520 copy_v3_v3(r_tangent, vec);
521 }
522 } while ((l_iter_other = l_iter_other->next) != l_iter_last);
523 } while ((l_iter = l_iter->next) != l_first);
524
525 normalize_v3(r_tangent);
526}
527
528void BM_face_calc_tangent_from_vert_diagonal(const BMFace *f, float r_tangent[3])
529{
530 BMLoop *l_iter, *l_first;
531
532 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
533
534 /* In case of degenerate faces. */
535 zero_v3(r_tangent);
536
537 /* WARNING: O(n^2) loop here, take care! */
538 float dist_max_sq = 0.0f;
539 do {
540 BMLoop *l_iter_other = l_iter->next;
541 do {
542 float vec[3];
543 sub_v3_v3v3(vec, l_iter->v->co, l_iter_other->v->co);
544
545 const float dist_sq = len_squared_v3(vec);
546 if (dist_sq > dist_max_sq) {
547 dist_max_sq = dist_sq;
548 copy_v3_v3(r_tangent, vec);
549 }
550 } while ((l_iter_other = l_iter_other->next) != l_iter);
551 } while ((l_iter = l_iter->next) != l_first);
552
553 normalize_v3(r_tangent);
554}
555
556void BM_face_calc_tangent_auto(const BMFace *f, float r_tangent[3])
557{
558 if (f->len == 3) {
559 /* most 'unique' edge of a triangle */
560 BMVert *verts[3];
563 }
564 else if (f->len == 4) {
565 /* longest edge pair of a quad */
567 }
568 else {
569 /* longest edge of an ngon */
570 BM_face_calc_tangent_from_edge(f, r_tangent);
571 }
572}
573
574void BM_face_calc_tangent_pair_auto(const BMFace *f, float r_tangent_a[3], float r_tangent_b[3])
575{
576 if (f->len == 3) {
577 /* most 'unique' edge of a triangle */
578 BMVert *verts[3];
580 BM_vert_tri_calc_tangent_pair_from_edge(verts, r_tangent_a, r_tangent_b);
581 }
582 else if (f->len == 4) {
583 /* longest edge pair of a quad */
584 bm_face_calc_tangent_pair_from_quad_edge_pair(f, r_tangent_a, r_tangent_b);
585 }
586 else {
587 /* longest edge of an ngon */
588 BM_face_calc_tangent_pair_from_edge(f, r_tangent_a, r_tangent_b);
589 }
590}
591
592void BM_face_calc_bounds_expand(const BMFace *f, float min[3], float max[3])
593{
594 const BMLoop *l_iter, *l_first;
595 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
596 do {
597 minmax_v3v3_v3(min, max, l_iter->v->co);
598 } while ((l_iter = l_iter->next) != l_first);
599}
600
601void BM_face_calc_center_bounds(const BMFace *f, float r_cent[3])
602{
603 const BMLoop *l_iter, *l_first;
604 float min[3], max[3];
605
607
608 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
609 do {
610 minmax_v3v3_v3(min, max, l_iter->v->co);
611 } while ((l_iter = l_iter->next) != l_first);
612
613 mid_v3_v3v3(r_cent, min, max);
614}
615
617 const BMFace *f,
618 float r_cent[3],
619 const blender::Span<blender::float3> vert_positions)
620{
621 /* must have valid index data */
622 BLI_assert((bm->elem_index_dirty & BM_VERT) == 0);
623 (void)bm;
624
625 const BMLoop *l_iter, *l_first;
626 float min[3], max[3];
627
629
630 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
631 do {
632 minmax_v3v3_v3(min, max, vert_positions[BM_elem_index_get(l_iter->v)]);
633 } while ((l_iter = l_iter->next) != l_first);
634
635 mid_v3_v3v3(r_cent, min, max);
636}
637
638void BM_face_calc_center_median(const BMFace *f, float r_cent[3])
639{
640 const BMLoop *l_iter, *l_first;
641
642 zero_v3(r_cent);
643
644 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
645 do {
646 add_v3_v3(r_cent, l_iter->v->co);
647 } while ((l_iter = l_iter->next) != l_first);
648 mul_v3_fl(r_cent, 1.0f / float(f->len));
649}
650
651void BM_face_calc_center_median_weighted(const BMFace *f, float r_cent[3])
652{
653 const BMLoop *l_iter;
654 const BMLoop *l_first;
655 float totw = 0.0f;
656 float w_prev;
657
658 zero_v3(r_cent);
659
660 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
661 w_prev = BM_edge_calc_length(l_iter->prev->e);
662 do {
663 const float w_curr = BM_edge_calc_length(l_iter->e);
664 const float w = (w_curr + w_prev);
665 madd_v3_v3fl(r_cent, l_iter->v->co, w);
666 totw += w;
667 w_prev = w_curr;
668 } while ((l_iter = l_iter->next) != l_first);
669
670 if (totw != 0.0f) {
671 mul_v3_fl(r_cent, 1.0f / totw);
672 }
673}
674
675void poly_rotate_plane(const float normal[3], float (*verts)[3], const uint nverts)
676{
677 float mat[3][3];
678 float co[3];
679 uint i;
680
681 co[2] = 0.0f;
682
683 axis_dominant_v3_to_m3(mat, normal);
684 for (i = 0; i < nverts; i++) {
685 mul_v2_m3v3(co, mat, verts[i]);
686 copy_v3_v3(verts[i], co);
687 }
688}
689
691{
692 BMIter iter;
693 BMFace *f;
694
695 BM_ITER_ELEM (f, &iter, e, BM_FACES_OF_EDGE) {
697 }
698
701}
702
703static void bm_loop_normal_accum(const BMLoop *l, float no[3])
704{
705 float vec1[3], vec2[3], fac;
706
707 /* Same calculation used in BM_mesh_normals_update */
708 sub_v3_v3v3(vec1, l->v->co, l->prev->v->co);
709 sub_v3_v3v3(vec2, l->next->v->co, l->v->co);
710 normalize_v3(vec1);
711 normalize_v3(vec2);
712
713 fac = blender::math::safe_acos_approx(-dot_v3v3(vec1, vec2));
714
715 madd_v3_v3fl(no, l->f->no, fac);
716}
717
718bool BM_vert_calc_normal_ex(const BMVert *v, const char hflag, float r_no[3])
719{
720 int len = 0;
721
722 zero_v3(r_no);
723
724 if (v->e) {
725 const BMEdge *e = v->e;
726 do {
727 if (e->l) {
728 const BMLoop *l = e->l;
729 do {
730 if (l->v == v) {
731 if (BM_elem_flag_test(l->f, hflag)) {
732 bm_loop_normal_accum(l, r_no);
733 len++;
734 }
735 }
736 } while ((l = l->radial_next) != e->l);
737 }
738 } while ((e = bmesh_disk_edge_next(e, v)) != v->e);
739 }
740
741 if (len) {
742 normalize_v3(r_no);
743 return true;
744 }
745 return false;
746}
747
748bool BM_vert_calc_normal(const BMVert *v, float r_no[3])
749{
750 int len = 0;
751
752 zero_v3(r_no);
753
754 if (v->e) {
755 const BMEdge *e = v->e;
756 do {
757 if (e->l) {
758 const BMLoop *l = e->l;
759 do {
760 if (l->v == v) {
761 bm_loop_normal_accum(l, r_no);
762 len++;
763 }
764 } while ((l = l->radial_next) != e->l);
765 }
766 } while ((e = bmesh_disk_edge_next(e, v)) != v->e);
767 }
768
769 if (len) {
770 normalize_v3(r_no);
771 return true;
772 }
773 return false;
774}
775
777{
778 int len = 0;
779
780 zero_v3(v->no);
781
782 if (v->e) {
783 const BMEdge *e = v->e;
784 do {
785 if (e->l) {
786 const BMLoop *l = e->l;
787 do {
788 if (l->v == v) {
791 len++;
792 }
793 } while ((l = l->radial_next) != e->l);
794 }
795 } while ((e = bmesh_disk_edge_next(e, v)) != v->e);
796 }
797
798 if (len) {
799 normalize_v3(v->no);
800 }
801}
802
807
808float BM_face_calc_normal(const BMFace *f, float r_no[3])
809{
810 BMLoop *l;
811
812 /* common cases first */
813 switch (f->len) {
814 case 4: {
815 const float *co1 = (l = BM_FACE_FIRST_LOOP(f))->v->co;
816 const float *co2 = (l = l->next)->v->co;
817 const float *co3 = (l = l->next)->v->co;
818 const float *co4 = (l->next)->v->co;
819
820 return normal_quad_v3(r_no, co1, co2, co3, co4);
821 }
822 case 3: {
823 const float *co1 = (l = BM_FACE_FIRST_LOOP(f))->v->co;
824 const float *co2 = (l = l->next)->v->co;
825 const float *co3 = (l->next)->v->co;
826
827 return normal_tri_v3(r_no, co1, co2, co3);
828 }
829 default: {
830 return bm_face_calc_poly_normal(f, r_no);
831 }
832 }
833}
835{
836 BM_face_calc_normal(f, f->no);
837}
838
840 const BMFace *f,
841 float r_no[3],
842 const Span<float3> vertexCos)
843{
844 BMLoop *l;
845
846 /* must have valid index data */
847 BLI_assert((bm->elem_index_dirty & BM_VERT) == 0);
848 (void)bm;
849
850 /* common cases first */
851 switch (f->len) {
852 case 4: {
853 const float *co1 = vertexCos[BM_elem_index_get((l = BM_FACE_FIRST_LOOP(f))->v)];
854 const float *co2 = vertexCos[BM_elem_index_get((l = l->next)->v)];
855 const float *co3 = vertexCos[BM_elem_index_get((l = l->next)->v)];
856 const float *co4 = vertexCos[BM_elem_index_get((l->next)->v)];
857
858 return normal_quad_v3(r_no, co1, co2, co3, co4);
859 }
860 case 3: {
861 const float *co1 = vertexCos[BM_elem_index_get((l = BM_FACE_FIRST_LOOP(f))->v)];
862 const float *co2 = vertexCos[BM_elem_index_get((l = l->next)->v)];
863 const float *co3 = vertexCos[BM_elem_index_get((l->next)->v)];
864
865 return normal_tri_v3(r_no, co1, co2, co3);
866 }
867 default: {
868 return bm_face_calc_poly_normal_vertex_cos(f, r_no, vertexCos);
869 }
870 }
871}
872
874 BMVert **varr, int varr_len, float r_normal[3], float r_center[3], int *r_index_tangent)
875{
876 const float varr_len_inv = 1.0f / float(varr_len);
877
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);
882 }
883
884 /* Find the 'co_a' point from center. */
885 int co_a_index = 0;
886 const float *co_a = nullptr;
887 {
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)) {
892 co_a = varr[i]->co;
893 co_a_index = i;
894 dist_sq_max = dist_sq_test;
895 }
896 }
897 }
898
899 float dir_a[3];
900 sub_v3_v3v3(dir_a, co_a, center);
901 normalize_v3(dir_a);
902
903 const float *co_b = nullptr;
904 float dir_b[3] = {0.0f, 0.0f, 0.0f};
905 {
906 float dist_sq_max = -1.0f;
907 for (int i = 0; i < varr_len; i++) {
908 if (varr[i]->co == co_a) {
909 continue;
910 }
911 float dir_test[3];
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)) {
916 co_b = varr[i]->co;
917 dist_sq_max = dist_sq_test;
918 copy_v3_v3(dir_b, dir_test);
919 }
920 }
921 }
922
923 if (varr_len <= 3) {
924 normal_tri_v3(r_normal, center, co_a, co_b);
925 goto finally;
926 }
927
928 {
929 normalize_v3(dir_b);
930
931 const float *co_a_opposite = nullptr;
932 const float *co_b_opposite = nullptr;
933
934 {
935 float dot_a_min = FLT_MAX;
936 float dot_b_min = FLT_MAX;
937 for (int i = 0; i < varr_len; i++) {
938 const float *co_test = varr[i]->co;
939 float dot_test;
940
941 if (co_test != co_a) {
942 dot_test = dot_v3v3(dir_a, co_test);
943 if (dot_test < dot_a_min) {
944 dot_a_min = dot_test;
945 co_a_opposite = co_test;
946 }
947 }
948
949 if (co_test != co_b) {
950 dot_test = dot_v3v3(dir_b, co_test);
951 if (dot_test < dot_b_min) {
952 dot_b_min = dot_test;
953 co_b_opposite = co_test;
954 }
955 }
956 }
957 }
958
959 normal_quad_v3(r_normal, co_a, co_b, co_a_opposite, co_b_opposite);
960 }
961
962finally:
963 if (r_center != nullptr) {
964 copy_v3_v3(r_center, center);
965 }
966 if (r_index_tangent != nullptr) {
967 *r_index_tangent = co_a_index;
968 }
969}
970
971void BM_verts_calc_normal_from_cloud(BMVert **varr, int varr_len, float r_normal[3])
972{
973 BM_verts_calc_normal_from_cloud_ex(varr, varr_len, r_normal, nullptr, nullptr);
974}
975
976float BM_face_calc_normal_subset(const BMLoop *l_first, const BMLoop *l_last, float r_no[3])
977{
978 const float *v_prev, *v_curr;
979
980 /* Newell's Method */
981 const BMLoop *l_iter = l_first;
982 const BMLoop *l_term = l_last->next;
983
984 zero_v3(r_no);
985
986 v_prev = l_last->v->co;
987 do {
988 v_curr = l_iter->v->co;
989 add_newell_cross_v3_v3v3(r_no, v_prev, v_curr);
990 v_prev = v_curr;
991 } while ((l_iter = l_iter->next) != l_term);
992
993 return normalize_v3(r_no);
994}
995
997 const BMFace *f,
998 float r_cent[3],
999 const blender::Span<blender::float3> vert_positions)
1000{
1001 /* must have valid index data */
1002 BLI_assert((bm->elem_index_dirty & BM_VERT) == 0);
1003 (void)bm;
1004
1005 bm_face_calc_poly_center_median_vertex_cos(f, r_cent, vert_positions);
1006}
1007
1009 BMFace *f,
1010 const int cd_loop_mdisp_offset,
1011 const bool use_loop_mdisp_flip)
1012{
1013 bmesh_kernel_loop_reverse(bm, f, cd_loop_mdisp_offset, use_loop_mdisp_flip);
1014 negate_v3(f->no);
1015}
1016
1018{
1019 const int cd_loop_mdisp_offset = CustomData_get_offset(&bm->ldata, CD_MDISPS);
1020 BM_face_normal_flip_ex(bm, f, cd_loop_mdisp_offset, true);
1021}
1022
1023bool BM_face_point_inside_test(const BMFace *f, const float co[3])
1024{
1025 float axis_mat[3][3];
1026 float(*projverts)[2] = BLI_array_alloca(projverts, f->len);
1027
1028 float co_2d[2];
1029 BMLoop *l_iter;
1030 int i;
1031
1033
1034 axis_dominant_v3_to_m3(axis_mat, f->no);
1035
1036 mul_v2_m3v3(co_2d, axis_mat, co);
1037
1038 for (i = 0, l_iter = BM_FACE_FIRST_LOOP(f); i < f->len; i++, l_iter = l_iter->next) {
1039 mul_v2_m3v3(projverts[i], axis_mat, l_iter->v->co);
1040 }
1041
1042 return isect_point_poly_v2(co_2d, projverts, f->len);
1043}
1044
1046 BMFace *f,
1047 BMFace **r_faces_new,
1048 int *r_faces_new_tot,
1049 BMEdge **r_edges_new,
1050 int *r_edges_new_tot,
1051 LinkNode **r_faces_double,
1052 const int quad_method,
1053 const int ngon_method,
1054 const bool use_tag,
1055 /* use for ngons only! */
1056 MemArena *pf_arena,
1057
1058 /* use for MOD_TRIANGULATE_NGON_BEAUTY only! */
1059 Heap *pf_heap)
1060{
1061 const int cd_loop_mdisp_offset = CustomData_get_offset(&bm->ldata, CD_MDISPS);
1062 const bool use_beauty = (ngon_method == MOD_TRIANGULATE_NGON_BEAUTY);
1063 BMLoop *l_first, *l_new;
1064 BMFace *f_new;
1065 int nf_i = 0;
1066 int ne_i = 0;
1067
1069
1070 /* ensure both are valid or nullptr */
1071 BLI_assert((r_faces_new == nullptr) == (r_faces_new_tot == nullptr));
1072
1073 BLI_assert(f->len > 3);
1074
1075 {
1076 BMLoop **loops = BLI_array_alloca(loops, f->len);
1077 uint(*tris)[3] = BLI_array_alloca(tris, f->len);
1078 const int totfilltri = f->len - 2;
1079 const int last_tri = f->len - 3;
1080 int i;
1081 /* for mdisps */
1082 float f_center[3];
1083
1084 if (f->len == 4) {
1085 /* even though we're not using BLI_polyfill, fill in 'tris' and 'loops'
1086 * so we can share code to handle face creation afterwards. */
1087 BMLoop *l_v1, *l_v2;
1088
1089 l_first = BM_FACE_FIRST_LOOP(f);
1090
1091 switch (quad_method) {
1093 l_v1 = l_first;
1094 l_v2 = l_first->next->next;
1095 break;
1096 }
1098 l_v1 = l_first->next;
1099 l_v2 = l_first->prev;
1100 break;
1101 }
1105 default: {
1106 BMLoop *l_v3, *l_v4;
1107 bool split_24;
1108
1109 l_v1 = l_first->next;
1110 l_v2 = l_first->next->next;
1111 l_v3 = l_first->prev;
1112 l_v4 = l_first;
1113
1114 if (quad_method == MOD_TRIANGULATE_QUAD_SHORTEDGE) {
1115 float d1, d2;
1116 d1 = len_squared_v3v3(l_v4->v->co, l_v2->v->co);
1117 d2 = len_squared_v3v3(l_v1->v->co, l_v3->v->co);
1118 split_24 = ((d2 - d1) > 0.0f);
1119 }
1120 else if (quad_method == MOD_TRIANGULATE_QUAD_LONGEDGE) {
1121 float d1, d2;
1122 d1 = len_squared_v3v3(l_v4->v->co, l_v2->v->co);
1123 d2 = len_squared_v3v3(l_v1->v->co, l_v3->v->co);
1124 split_24 = ((d2 - d1) < 0.0f);
1125 }
1126 else {
1127 /* first check if the quad is concave on either diagonal */
1128 const int flip_flag = is_quad_flip_v3(
1129 l_v1->v->co, l_v2->v->co, l_v3->v->co, l_v4->v->co);
1130 if (UNLIKELY(flip_flag & (1 << 0))) {
1131 split_24 = true;
1132 }
1133 else if (UNLIKELY(flip_flag & (1 << 1))) {
1134 split_24 = false;
1135 }
1136 else {
1137 split_24 = (BM_verts_calc_rotate_beauty(l_v1->v, l_v2->v, l_v3->v, l_v4->v, 0, 0) >
1138 0.0f);
1139 }
1140 }
1141
1142 /* named confusingly, l_v1 is in fact the second vertex */
1143 if (split_24) {
1144 l_v1 = l_v4;
1145 // l_v2 = l_v2;
1146 }
1147 else {
1148 // l_v1 = l_v1;
1149 l_v2 = l_v3;
1150 }
1151 break;
1152 }
1153 }
1154
1155 loops[0] = l_v1;
1156 loops[1] = l_v1->next;
1157 loops[2] = l_v2;
1158 loops[3] = l_v2->next;
1159
1160 ARRAY_SET_ITEMS(tris[0], 0, 1, 2);
1161 ARRAY_SET_ITEMS(tris[1], 0, 2, 3);
1162 }
1163 else {
1164 BMLoop *l_iter;
1165 float axis_mat[3][3];
1166 float(*projverts)[2] = BLI_array_alloca(projverts, f->len);
1167
1168 axis_dominant_v3_to_m3_negate(axis_mat, f->no);
1169
1170 for (i = 0, l_iter = BM_FACE_FIRST_LOOP(f); i < f->len; i++, l_iter = l_iter->next) {
1171 loops[i] = l_iter;
1172 mul_v2_m3v3(projverts[i], axis_mat, l_iter->v->co);
1173 }
1174
1175 BLI_polyfill_calc_arena(projverts, f->len, 1, tris, pf_arena);
1176
1177 if (use_beauty) {
1178 BLI_polyfill_beautify(projverts, f->len, tris, pf_arena, pf_heap);
1179 }
1180
1181 BLI_memarena_clear(pf_arena);
1182 }
1183
1184 if (cd_loop_mdisp_offset != -1) {
1185 BM_face_calc_center_median(f, f_center);
1186 }
1187
1188 /* loop over calculated triangles and create new geometry */
1189 for (i = 0; i < totfilltri; i++) {
1190 BMLoop *ltri[3] = {loops[tris[i][0]], loops[tris[i][1]], loops[tris[i][2]]};
1191
1192 BMVert *v_tri[3] = {ltri[0]->v, ltri[1]->v, ltri[2]->v};
1193
1194 f_new = BM_face_create_verts(bm, v_tri, 3, f, BM_CREATE_NOP, true);
1195 l_new = BM_FACE_FIRST_LOOP(f_new);
1196
1197 BLI_assert(v_tri[0] == l_new->v);
1198
1199 /* check for duplicate */
1200 if (l_new->radial_next != l_new) {
1201 BMLoop *l_iter = l_new->radial_next;
1202 do {
1203 if (UNLIKELY((l_iter->f->len == 3) && (l_new->prev->v == l_iter->prev->v))) {
1204 /* Check the last tri because we swap last f_new with f at the end... */
1205 BLI_linklist_prepend(r_faces_double, (i != last_tri) ? f_new : f);
1206 break;
1207 }
1208 } while ((l_iter = l_iter->radial_next) != l_new);
1209 }
1210
1211 /* copy CD data */
1212 BM_elem_attrs_copy(bm, ltri[0], l_new);
1213 BM_elem_attrs_copy(bm, ltri[1], l_new->next);
1214 BM_elem_attrs_copy(bm, ltri[2], l_new->prev);
1215
1216 /* add all but the last face which is swapped and removed (below) */
1217 if (i != last_tri) {
1218 if (use_tag) {
1220 }
1221 if (r_faces_new) {
1222 r_faces_new[nf_i++] = f_new;
1223 }
1224 }
1225
1226 if (use_tag || r_edges_new) {
1227 /* new faces loops */
1228 BMLoop *l_iter;
1229
1230 l_iter = l_first = l_new;
1231 do {
1232 BMEdge *e = l_iter->e;
1233 /* Confusing! if its not a boundary now, we know it will be later since this will be an
1234 * edge of one of the new faces which we're in the middle of creating. */
1235 bool is_new_edge = (l_iter == l_iter->radial_next);
1236
1237 if (is_new_edge) {
1238 if (use_tag) {
1240 }
1241 if (r_edges_new) {
1242 r_edges_new[ne_i++] = e;
1243 }
1244 }
1245 /* NOTE: never disable tag's. */
1246 } while ((l_iter = l_iter->next) != l_first);
1247 }
1248
1249 if (cd_loop_mdisp_offset != -1) {
1250 float f_new_center[3];
1251 BM_face_calc_center_median(f_new, f_new_center);
1252 BM_face_interp_multires_ex(bm, f_new, f, f_new_center, f_center, cd_loop_mdisp_offset);
1253 }
1254 }
1255
1256 {
1257 /* we can't delete the real face, because some of the callers expect it to remain valid.
1258 * so swap data and delete the last created tri */
1259 bmesh_face_swap_data(f, f_new);
1260 BM_face_kill(bm, f_new);
1261 }
1262 }
1263 bm->elem_index_dirty |= BM_FACE;
1264
1265 if (r_faces_new_tot) {
1266 *r_faces_new_tot = nf_i;
1267 }
1268
1269 if (r_edges_new_tot) {
1270 *r_edges_new_tot = ne_i;
1271 }
1272}
1273
1275{
1277 float center[2] = {0.0f, 0.0f};
1278 float axis_mat[3][3];
1279 float(*projverts)[2] = BLI_array_alloca(projverts, f->len);
1280 const float *(*edgeverts)[2] = BLI_array_alloca(edgeverts, len);
1281 BMLoop *l;
1282 int i, i_prev, j;
1283
1285
1286 axis_dominant_v3_to_m3(axis_mat, f->no);
1287
1288 for (i = 0, l = BM_FACE_FIRST_LOOP(f); i < f->len; i++, l = l->next) {
1289 mul_v2_m3v3(projverts[i], axis_mat, l->v->co);
1290 add_v2_v2(center, projverts[i]);
1291 }
1292
1293 /* first test for completely convex face */
1294 if (is_poly_convex_v2(projverts, f->len)) {
1295 return;
1296 }
1297
1298 mul_v2_fl(center, 1.0f / f->len);
1299
1300 for (i = 0, l = BM_FACE_FIRST_LOOP(f); i < f->len; i++, l = l->next) {
1301 BM_elem_index_set(l, i); /* set_dirty */
1302
1303 /* center the projection for maximum accuracy */
1304 sub_v2_v2(projverts[i], center);
1305
1306 out[0] = max_ff(out[0], projverts[i][0]);
1307 out[1] = max_ff(out[1], projverts[i][1]);
1308 }
1309 bm->elem_index_dirty |= BM_LOOP;
1310
1311 /* ensure we are well outside the face bounds (value is arbitrary) */
1312 out += 1.0f;
1313
1314 for (i = 0; i < len; i++) {
1315 edgeverts[i][0] = projverts[BM_elem_index_get(loops[i][0])];
1316 edgeverts[i][1] = projverts[BM_elem_index_get(loops[i][1])];
1317 }
1318
1319 /* do convexity test */
1320 for (i = 0; i < len; i++) {
1321 float mid[2];
1322 mid_v2_v2v2(mid, edgeverts[i][0], edgeverts[i][1]);
1323
1324 int isect = 0;
1325 int j_prev;
1326 for (j = 0, j_prev = f->len - 1; j < f->len; j_prev = j++) {
1327 const float *f_edge[2] = {projverts[j_prev], projverts[j]};
1328 if (isect_seg_seg_v2(UNPACK2(f_edge), mid, out) == ISECT_LINE_LINE_CROSS) {
1329 isect++;
1330 }
1331 }
1332
1333 if (isect % 2 == 0) {
1334 loops[i][0] = nullptr;
1335 }
1336 }
1337
1338#define EDGE_SHARE_VERT(e1, e2) \
1339 (ELEM((e1)[0], (e2)[0], (e2)[1]) || ELEM((e1)[1], (e2)[0], (e2)[1]))
1340
1341 /* do line crossing tests */
1342 for (i = 0, i_prev = f->len - 1; i < f->len; i_prev = i++) {
1343 const float *f_edge[2] = {projverts[i_prev], projverts[i]};
1344 for (j = 0; j < len; j++) {
1345 if ((loops[j][0] != nullptr) && !EDGE_SHARE_VERT(f_edge, edgeverts[j])) {
1346 if (isect_seg_seg_v2(UNPACK2(f_edge), UNPACK2(edgeverts[j])) == ISECT_LINE_LINE_CROSS) {
1347 loops[j][0] = nullptr;
1348 }
1349 }
1350 }
1351 }
1352
1353 /* self intersect tests */
1354 for (i = 0; i < len; i++) {
1355 if (loops[i][0]) {
1356 for (j = i + 1; j < len; j++) {
1357 if ((loops[j][0] != nullptr) && !EDGE_SHARE_VERT(edgeverts[i], edgeverts[j])) {
1358 if (isect_seg_seg_v2(UNPACK2(edgeverts[i]), UNPACK2(edgeverts[j])) ==
1360 {
1361 loops[i][0] = nullptr;
1362 break;
1363 }
1364 }
1365 }
1366 }
1367 }
1368
1369#undef EDGE_SHARE_VERT
1370}
1371
1373{
1374 int i;
1375
1376 for (i = 0; i < len; i++) {
1377 BMLoop *l_a_dummy, *l_b_dummy;
1379 loops[i][0]->v, loops[i][1]->v, &l_a_dummy, &l_b_dummy, false))
1380 {
1381 loops[i][0] = nullptr;
1382 }
1383 }
1384}
1385
1387{
1389
1390 BLI_assert(f->len == 3);
1391
1392 r_verts[0] = l->v;
1393 l = l->next;
1394 r_verts[1] = l->v;
1395 l = l->next;
1396 r_verts[2] = l->v;
1397}
1398
1400{
1402
1403 BLI_assert(f->len == 4);
1404
1405 r_verts[0] = l->v;
1406 l = l->next;
1407 r_verts[1] = l->v;
1408 l = l->next;
1409 r_verts[2] = l->v;
1410 l = l->next;
1411 r_verts[3] = l->v;
1412}
1413
1415{
1417
1418 BLI_assert(f->len == 3);
1419
1420 r_loops[0] = l;
1421 l = l->next;
1422 r_loops[1] = l;
1423 l = l->next;
1424 r_loops[2] = l;
1425}
1426
1428{
1430
1431 BLI_assert(f->len == 4);
1432
1433 r_loops[0] = l;
1434 l = l->next;
1435 r_loops[1] = l;
1436 l = l->next;
1437 r_loops[2] = l;
1438 l = l->next;
1439 r_loops[3] = l;
1440}
CustomData interface, see also DNA_customdata_types.h.
int CustomData_get_offset(const CustomData *data, eCustomDataType type)
#define BLI_array_alloca(arr, realsize)
Definition BLI_alloca.h:18
#define BLI_assert(a)
Definition BLI_assert.h:46
MINLINE float max_ff(float a, float b)
int isect_seg_seg_v2(const float v1[2], const float v2[2], const float v3[2], const float v4[2])
float normal_quad_v3(float n[3], const float v1[3], const float v2[3], const float v3[3], const float v4[3])
Definition math_geom.cc:58
int is_quad_flip_v3(const float v1[3], const float v2[3], const float v3[3], const float v4[3])
bool isect_point_poly_v2(const float pt[2], const float verts[][2], unsigned int nr)
float closest_to_line_segment_v3(float r_close[3], const float p[3], const float l1[3], const float l2[3])
Definition math_geom.cc:387
void axis_dominant_v3_to_m3_negate(float r_mat[3][3], const float normal[3])
void axis_dominant_v3_to_m3(float r_mat[3][3], const float normal[3])
Normal to x,y matrix.
float area_squared_tri_v3(const float v1[3], const float v2[3], const float v3[3])
Definition math_geom.cc:107
bool is_poly_convex_v2(const float verts[][2], unsigned int nr)
float normal_tri_v3(float n[3], const float v1[3], const float v2[3], const float v3[3])
Definition math_geom.cc:41
#define ISECT_LINE_LINE_CROSS
void mul_v2_m3v3(float r[2], const float M[3][3], const float a[3])
void mul_v3_m3v3(float r[3], const float M[3][3], const float a[3])
void axis_sort_v3(const float axis_values[3], int r_axis_order[3])
MINLINE float len_squared_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
MINLINE float len_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
void minmax_v3v3_v3(float min[3], float max[3], const float vec[3])
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
MINLINE void sub_v2_v2(float r[2], const float a[2])
MINLINE void sub_v3_v3(float r[3], const float a[3])
MINLINE float len_squared_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void add_newell_cross_v3_v3v3(float n[3], const float v_prev[3], const float v_curr[3])
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void mul_v2_fl(float r[2], float f)
MINLINE void mul_v3_fl(float r[3], float f)
MINLINE void copy_v3_v3(float r[3], const float a[3])
void project_v3_v3v3(float out[3], const float p[3], const float v_proj[3])
MINLINE void add_v2_v2(float r[2], const float a[2])
void project_plane_normalized_v3_v3v3(float out[3], const float p[3], const float v_plane[3])
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void cross_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void negate_v3(float r[3])
void mid_v2_v2v2(float r[2], const float a[2], const float b[2])
MINLINE float normalize_v3_v3(float r[3], const float a[3])
void mid_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void swap_v3_v3(float a[3], float b[3])
MINLINE void zero_v3(float r[3])
MINLINE void add_v3_v3(float r[3], const float a[3])
MINLINE float normalize_v3(float n[3])
void mid_v3_v3v3v3(float v[3], const float v1[3], const float v2[3], const float v3[3])
MINLINE float len_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT
void BLI_memarena_clear(MemArena *ma) ATTR_NONNULL(1)
void BLI_polyfill_calc(const float(*coords)[2], unsigned int coords_num, int coords_sign, unsigned int(*r_tris)[3])
void BLI_polyfill_calc_arena(const float(*coords)[2], unsigned int coords_num, int coords_sign, unsigned int(*r_tris)[3], struct MemArena *arena)
void BLI_polyfill_beautify(const float(*coords)[2], unsigned int coords_num, unsigned int(*tris)[3], struct MemArena *arena, struct Heap *eheap)
unsigned int uint
#define UNPACK2(a)
#define INIT_MINMAX(min, max)
#define ARRAY_SET_ITEMS(...)
#define UNLIKELY(x)
#define ELEM(...)
@ MOD_TRIANGULATE_QUAD_SHORTEDGE
@ MOD_TRIANGULATE_QUAD_FIXED
@ MOD_TRIANGULATE_QUAD_LONGEDGE
@ MOD_TRIANGULATE_QUAD_BEAUTY
@ MOD_TRIANGULATE_QUAD_ALTERNATE
@ MOD_TRIANGULATE_NGON_BEAUTY
float BM_verts_calc_rotate_beauty(const BMVert *v1, const BMVert *v2, const BMVert *v3, const BMVert *v4, const short flag, const short method)
#define BM_FACE_FIRST_LOOP(p)
#define BM_ELEM_CD_GET_FLOAT_P(ele, offset)
@ BM_LOOP
@ BM_ELEM_TAG
void BM_elem_attrs_copy(BMesh *bm, const BMCustomDataCopyMap &map, const BMVert *src, BMVert *dst)
void bmesh_face_swap_data(BMFace *f_a, BMFace *f_b)
void BM_face_kill(BMesh *bm, BMFace *f)
BMFace * BM_face_create_verts(BMesh *bm, BMVert **vert_arr, const int len, const BMFace *f_example, const eBMCreateFlag create_flag, const bool create_edges)
void bmesh_kernel_loop_reverse(BMesh *bm, BMFace *f, const int cd_loop_mdisp_offset, const bool use_loop_mdisp_flip)
Loop Reverse.
@ BM_CREATE_NOP
Definition bmesh_core.hh:28
#define BM_elem_index_get(ele)
#define BM_elem_index_set(ele, index)
#define BM_elem_flag_test(ele, hflag)
#define BM_elem_flag_enable(ele, hflag)
void BM_face_interp_multires_ex(BMesh *bm, BMFace *f_dst, const BMFace *f_src, const float f_dst_center[3], const float f_src_center[3], const int cd_loop_mdisp_offset)
#define BM_ITER_ELEM(ele, iter, data, itype)
@ BM_FACES_OF_EDGE
BMesh * bm
#define BM_FACE
#define BM_VERT
void BM_face_calc_tangent_from_vert_diagonal(const BMFace *f, float r_tangent[3])
void BM_face_as_array_loop_tri(BMFace *f, BMLoop *r_loops[3])
static void bm_face_calc_tangent_from_quad_edge_pair(const BMFace *f, float r_tangent[3])
void BM_face_calc_tangent_pair_from_edge(const BMFace *f, float r_tangent_a[3], float r_tangent_b[3])
float BM_face_calc_area_uv(const BMFace *f, int cd_loop_uv_offset)
void poly_rotate_plane(const float normal[3], float(*verts)[3], const uint nverts)
POLY ROTATE PLANE.
void BM_face_splits_check_optimal(BMFace *f, BMLoop *(*loops)[2], int len)
void BM_face_calc_center_bounds(const BMFace *f, float r_cent[3])
static void bm_face_calc_poly_center_median_vertex_cos(const BMFace *f, float r_cent[3], const blender::Span< blender::float3 > vert_positions)
COMPUTE POLY CENTER (BMFace).
void BM_vert_normal_update(BMVert *v)
void BM_face_splits_check_legal(BMesh *bm, BMFace *f, BMLoop *(*loops)[2], int len)
static void bm_face_calc_tangent_pair_from_quad_edge_pair(const BMFace *f, float r_tangent_a[3], float r_tangent_b[3])
void BM_face_calc_tangent_from_edge_pair(const BMFace *f, float r_tangent[3])
void BM_face_as_array_vert_quad(BMFace *f, BMVert *r_verts[4])
static int bm_vert_tri_find_unique_edge(BMVert *verts[3])
float BM_face_calc_area_with_mat3(const BMFace *f, const float mat3[3][3])
void BM_face_calc_point_in_face(const BMFace *f, float r_co[3])
void BM_verts_calc_normal_from_cloud(BMVert **varr, int varr_len, float r_normal[3])
bool BM_face_point_inside_test(const BMFace *f, const float co[3])
float BM_face_calc_area_uv_signed(const BMFace *f, int cd_loop_uv_offset)
bool BM_vert_calc_normal(const BMVert *v, float r_no[3])
void BM_vert_tri_calc_tangent_from_edge(BMVert *verts[3], float r_tangent[3])
void BM_vert_tri_calc_tangent_pair_from_edge(BMVert *verts[3], float r_tangent_a[3], float r_tangent_b[3])
void BM_face_calc_center_bounds_vcos(const BMesh *bm, const BMFace *f, float r_cent[3], const blender::Span< blender::float3 > vert_positions)
void BM_face_normal_flip_ex(BMesh *bm, BMFace *f, const int cd_loop_mdisp_offset, const bool use_loop_mdisp_flip)
Face Flip Normal.
void BM_face_normal_update(BMFace *f)
void BM_face_as_array_vert_tri(BMFace *f, BMVert *r_verts[3])
bool BM_vert_calc_normal_ex(const BMVert *v, const char hflag, float r_no[3])
void BM_face_as_array_loop_quad(BMFace *f, BMLoop *r_loops[4])
float BM_face_calc_normal_vcos(const BMesh *bm, const BMFace *f, float r_no[3], const Span< float3 > vertexCos)
void BM_face_normal_flip(BMesh *bm, BMFace *f)
void BM_face_calc_center_median_vcos(const BMesh *bm, const BMFace *f, float r_cent[3], const blender::Span< blender::float3 > vert_positions)
void BM_verts_calc_normal_from_cloud_ex(BMVert **varr, int varr_len, float r_normal[3], float r_center[3], int *r_index_tangent)
void BM_face_calc_bounds_expand(const BMFace *f, float min[3], float max[3])
float BM_face_calc_area(const BMFace *f)
#define EDGE_SHARE_VERT(e1, e2)
float BM_face_calc_normal_subset(const BMLoop *l_first, const BMLoop *l_last, float r_no[3])
float BM_face_calc_normal(const BMFace *f, float r_no[3])
BMESH UPDATE FACE NORMAL.
float BM_face_calc_perimeter_with_mat3(const BMFace *f, const float mat3[3][3])
void BM_face_triangulate(BMesh *bm, BMFace *f, BMFace **r_faces_new, int *r_faces_new_tot, BMEdge **r_edges_new, int *r_edges_new_tot, LinkNode **r_faces_double, const int quad_method, const int ngon_method, const bool use_tag, MemArena *pf_arena, Heap *pf_heap)
static float bm_face_calc_poly_normal_vertex_cos(const BMFace *f, float r_no[3], const Span< float3 > vertexCos)
COMPUTE POLY NORMAL (BMFace).
void BM_face_calc_tangent_from_edge(const BMFace *f, float r_tangent[3])
static void bm_loop_normal_accum(const BMLoop *l, float no[3])
void BM_vert_tri_calc_tangent_edge_pair(BMVert *verts[3], float r_tangent[3])
void BM_face_calc_tessellation(const BMFace *f, const bool use_fixed_quad, BMLoop **r_loops, uint(*r_index)[3])
void BM_face_calc_center_median(const BMFace *f, float r_cent[3])
void BM_face_calc_tangent_auto(const BMFace *f, float r_tangent[3])
static float bm_face_calc_poly_normal(const BMFace *f, float n[3])
COMPUTE POLY NORMAL (BMFace).
void BM_face_calc_tangent_from_edge_diagonal(const BMFace *f, float r_tangent[3])
void BM_face_calc_center_median_weighted(const BMFace *f, float r_cent[3])
void BM_vert_normal_update_all(BMVert *v)
void BM_face_calc_tangent_pair_auto(const BMFace *f, float r_tangent_a[3], float r_tangent_b[3])
float BM_face_calc_perimeter(const BMFace *f)
void BM_edge_normals_update(BMEdge *e)
bool BM_face_is_normal_valid(const BMFace *f)
BMFace * BM_vert_pair_share_face_by_angle(BMVert *v_a, BMVert *v_b, BMLoop **r_l_a, BMLoop **r_l_b, const bool allow_adjacent)
BMLoop * BM_face_find_longest_loop(BMFace *f)
float BM_edge_calc_length(const BMEdge *e)
ATTR_WARN_UNUSED_RESULT const BMLoop * l
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
ATTR_WARN_UNUSED_RESULT const BMVert * v
BLI_INLINE BMEdge * bmesh_disk_edge_next(const BMEdge *e, const BMVert *v) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
#define fabsf(x)
static float verts[][3]
#define out
VecBase< float, 3 > cross(VecOp< float, 3 >, VecOp< float, 3 >) RET
float safe_acos_approx(float x)
VecBase< float, 2 > float2
VecBase< float, 3 > float3
#define min(a, b)
Definition sort.cc:36
#define FLT_MAX
Definition stdcycles.h:14
float no[3]
struct BMVert * v
struct BMEdge * e
struct BMLoop * radial_next
struct BMLoop * prev
struct BMFace * f
struct BMLoop * next
float co[3]
i
Definition text_draw.cc:230
max
Definition text_draw.cc:251
uint len