Blender V5.3
bmo_planar_faces.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
10
11#include "MEM_guardedalloc.h"
12
13#include "BLI_ghash.hh"
14#include "BLI_math_geom_c.hh"
15#include "BLI_math_vector_c.hh"
16
17#include "bmesh.hh"
18
19#include "intern/bmesh_operators_private.hh" /* own include */
20
21namespace blender {
22
23#define ELE_VERT_ADJUST (1 << 0)
24#define ELE_FACE_ADJUST (1 << 1)
25
26struct VertAccum {
27 float co[3] = {};
28 int co_tot = 0;
29};
30
32{
33 const float fac = BMO_slot_float_get(op->slots_in, "factor");
34 const int iterations = BMO_slot_int_get(op->slots_in, "iterations");
35 const int faces_num = BMO_slot_buffer_len(op->slots_in, "faces");
36
37 const float eps = 0.00001f;
38 const float eps_sq = square_f(eps);
39
40 BMOIter oiter;
41 BMFace *f;
42 float (*faces_center)[3];
43 int i, iter_step, shared_vert_num;
44
45 faces_center = MEM_new_array_uninitialized<float[3]>(faces_num, __func__);
46
47 shared_vert_num = 0;
48 BMO_ITER_INDEX (f, &oiter, op->slots_in, "faces", BM_FACE, i) {
49 BMLoop *l_iter, *l_first;
50
51 if (f->len == 3) {
52 continue;
53 }
54
55 BM_face_calc_center_median_weighted(f, faces_center[i]);
56
57 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
58 do {
59 if (!BMO_vert_flag_test(bm, l_iter->v, ELE_VERT_ADJUST)) {
61 shared_vert_num += 1;
62 }
63 } while ((l_iter = l_iter->next) != l_first);
64
66 }
67
68 Map<BMVert *, VertAccum> vaccum_map;
69 vaccum_map.reserve(shared_vert_num);
70
71 for (iter_step = 0; iter_step < iterations; iter_step++) {
72 bool changed = false;
73
74 BMO_ITER_INDEX (f, &oiter, op->slots_in, "faces", BM_FACE, i) {
75 BMLoop *l_iter, *l_first;
76 float plane[4];
77
79 continue;
80 }
82
83 BLI_assert(f->len != 3);
84
85/* keep original face data (else we 'move' the face) */
86#if 0
89#endif
90
91 plane_from_point_normal_v3(plane, faces_center[i], f->no);
92
93 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
94 do {
95 VertAccum &va = vaccum_map.lookup_or_add_default(l_iter->v);
96 float co[3];
97 closest_to_plane_normalized_v3(co, plane, l_iter->v->co);
98 va.co_tot += 1;
99
100 interp_v3_v3v3(va.co, va.co, co, 1.0f / float(va.co_tot));
101 } while ((l_iter = l_iter->next) != l_first);
102 }
103
104 for (const auto &item : vaccum_map.items()) {
105 BMVert *v = item.key;
106 const VertAccum &va = item.value;
107 BMIter iter;
108
109 if (len_squared_v3v3(v->co, va.co) > eps_sq) {
111 interp_v3_v3v3(v->co, v->co, va.co, fac);
112 changed = true;
113 }
114
115 /* tag for re-calculation */
116 BM_ITER_ELEM (f, &iter, v, BM_FACES_OF_VERT) {
117 if (f->len != 3) {
119 }
120 }
121 }
122
123 /* if nothing changed, break out early */
124 if (changed == false) {
125 break;
126 }
127
128 vaccum_map.clear();
129 }
130
131 MEM_delete(faces_center);
132}
133
134} // namespace blender
Read Blender memory allocation and freeing.
#define BM_FACE_FIRST_LOOP(p)
#define BM_ITER_ELEM(ele, iter, data, itype)
#define BMO_ITER_INDEX(ele, iter, slot_args, slot_name, restrict_flag, i_)
#define BMO_vert_flag_enable(bm, e, oflag)
#define BMO_face_flag_enable(bm, e, oflag)
#define BMO_vert_flag_test(bm, e, oflag)
#define BMO_face_flag_disable(bm, e, oflag)
#define BMO_face_flag_test(bm, e, oflag)
#define ELE_FACE_ADJUST
#define ELE_VERT_ADJUST
void clear()
Definition BLI_map.hh:1038
Value & lookup_or_add_default(const Key &key)
Definition BLI_map.hh:639
void reserve(int64_t n)
Definition BLI_map.hh:1028
ItemIterator items() const &
Definition BLI_map.hh:902
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
MINLINE float len_squared_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
int BMO_slot_int_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
int BMO_slot_buffer_len(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void bmo_planar_faces_exec(BMesh *bm, BMOperator *op)
float BMO_slot_float_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
ATTR_WARN_UNUSED_RESULT const BMVert * v
void closest_to_plane_normalized_v3(float r_close[3], const float plane[4], const float pt[3])
Definition math_geom.cc:458
void plane_from_point_normal_v3(float r_plane[4], const float plane_co[3], const float plane_no[3])
Definition math_geom.cc:225
void BM_face_normal_update(BMFace *f)
void interp_v3_v3v3(float r[3], const float a[3], const float b[3], float t)
MINLINE float square_f(float a)
void BM_face_calc_center_median_weighted(const BMFace *f, float r_cent[3])
const btScalar eps
Definition poly34.cpp:11
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
struct BMLoop * next
struct BMVert * v
struct BMOpSlot slots_in[BMO_OP_MAX_SLOTS]