Blender V5.3
bmo_flatten.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include "BLI_array.hh"
12#include "BLI_math_vector.hh"
13#include "BLI_math_vector_c.hh"
14#include "BLI_vector.hh"
15#include "BLI_vector_set.hh"
16
17#include "bmesh.hh"
18#include "intern/bmesh_operators_private.hh" /* own include */
19
20namespace blender {
21
23{
24 float3 center(0.0f);
25 for (BMVert *v : verts) {
26 center += float3(v->co);
27 }
28 center /= float(verts.size());
29 return center;
30}
31
33{
34 float3 normal(0.0f);
35 for (BMFace *f : faces) {
36 normal += float3(f->no) * BM_face_calc_area(f);
37 }
38 return (normalize_v3(normal) != 0.0f) ? normal : float3(0.0f, 0.0f, 1.0f);
39}
40
42{
44 for (BMFace *f : faces) {
45 BMIter viter;
46 BMVert *v;
47 BM_ITER_ELEM (v, &viter, f, BM_VERTS_OF_FACE) {
48 verts.add(v);
49 }
50 }
51 return verts.extract_vector();
52}
53
55{
56 const float factor = BMO_slot_float_get(op->slots_in, "factor");
57 const FlattenMethod method = static_cast<FlattenMethod>(
58 BMO_slot_int_get(op->slots_in, "method"));
59 const bool lock_x = BMO_slot_bool_get(op->slots_in, "lock_x");
60 const bool lock_y = BMO_slot_bool_get(op->slots_in, "lock_y");
61 const bool lock_z = BMO_slot_bool_get(op->slots_in, "lock_z");
62
63 float3 view_direction(0.0f, 0.0f, 1.0f);
64 if (method == FLATTEN_VIEW) {
65 BMO_slot_vec_get(op->slots_in, "view_normal", view_direction);
66 }
67
68 auto flatten_verts_fn = [&](MutableSpan<BMVert *> verts, Span<BMFace *> faces) {
69 float3 center;
70 float3 normal(0.0f);
71
72 FlattenMethod actual_method = method;
73 /* The normal method is computed using the average face normal so for vertices
74 * not attached to any faces, the best fit normal is used in place. */
75 if (actual_method == FLATTEN_NORMAL && faces.is_empty()) {
76 actual_method = FLATTEN_BEST_FIT;
77 }
78
79 switch (actual_method) {
82 verts.data(), int(verts.size()), normal, center, nullptr);
83 break;
84 case FLATTEN_NORMAL:
85 normal = compute_average_face_normal(faces);
86 center = compute_centroid(verts);
87 break;
88 case FLATTEN_VIEW:
89 normal = view_direction;
90 center = compute_centroid(verts);
91 break;
92 }
93 BLI_assert(!math::is_zero(normal));
94 for (BMVert *v : verts) {
95 float3 co(v->co);
96 float3 projected = co - math::dot(co - center, normal) * normal;
97
98 if (lock_x) {
99 projected.x = co.x;
100 }
101 if (lock_y) {
102 projected.y = co.y;
103 }
104 if (lock_z) {
105 projected.z = co.z;
106 }
107
108 float3 co_final = math::interpolate(co, projected, factor);
109 copy_v3_v3(v->co, co_final);
110 }
111 };
112
115
116 Array<int> face_groups_array(bm->totface);
117 int (*face_group_index)[2];
118 const int face_group_num = BM_mesh_calc_face_groups(bm,
119 face_groups_array.data(),
120 &face_group_index,
121 nullptr,
122 nullptr,
123 nullptr,
125 BM_EDGE);
126
128
129 for (const int g : IndexRange(face_group_num)) {
130 const int start = face_group_index[g][0];
131 const int length = face_group_index[g][1];
132
133 Array<BMFace *> faces(length);
134 for (const int i : IndexRange(start, length)) {
135 BMFace *f = BM_face_at_index(bm, face_groups_array[i]);
136 faces[i - start] = f;
137 /* For every edge in the face, the BM_ELEM_TAG is disabled so the only edges that will still
138 * have them enabled are those without faces which would be detected in the second pass. */
139 BMIter eiter;
140 BMEdge *e;
141 BM_ITER_ELEM (e, &eiter, f, BM_EDGES_OF_FACE) {
143 }
144 }
145
147 flatten_verts_fn(verts, faces);
148 }
149 MEM_delete(face_group_index);
150
151 Array<int> edge_groups_array(bm->totedge);
152 int (*edge_group_index)[2];
153 const int edge_group_num = BM_mesh_calc_edge_groups(
154 bm, edge_groups_array.data(), &edge_group_index, nullptr, nullptr, BM_ELEM_TAG);
155
157
158 for (const int g : IndexRange(edge_group_num)) {
159 const int start = edge_group_index[g][0];
160 const int length = edge_group_index[g][1];
161
162 VectorSet<BMVert *> verts_set;
163 for (const int i : IndexRange(start, length)) {
164 BMEdge *e = BM_edge_at_index(bm, edge_groups_array[i]);
165 verts_set.add(e->v1);
166 verts_set.add(e->v2);
167 }
168 Vector<BMVert *> verts = verts_set.extract_vector();
169 flatten_verts_fn(verts, {});
170 }
171 MEM_delete(edge_group_index);
172}
173
174} // namespace blender
#define BM_elem_flag_disable(ele, hflag)
#define BM_ITER_ELEM(ele, iter, data, itype)
const T * data() const
Definition BLI_array.hh:323
constexpr IndexRange()=default
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr T * data() const
Definition BLI_span.hh:545
constexpr int64_t size() const
Definition BLI_span.hh:256
bool add(const Key &key)
float length(VecOp< float, D >) RET
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
bool is_zero(const T &a)
T interpolate(const T &a, const T &b, const FactorT &t)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
int BMO_slot_int_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
MINLINE void copy_v3_v3(float r[3], const float a[3])
void BMO_slot_buffer_hflag_enable(BMesh *bm, BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name, char htype, char hflag, bool do_flush)
BMO_FLAG_BUFFER.
BLI_INLINE BMEdge * BM_edge_at_index(BMesh *bm, const int index)
void BM_mesh_elem_table_ensure(BMesh *bm, const char htype)
float BMO_slot_float_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
void BM_mesh_elem_hflag_disable_all(BMesh *bm, const char htype, const char hflag, const bool respecthide)
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)
ATTR_WARN_UNUSED_RESULT const BMVert * v
static float3 compute_average_face_normal(Span< BMFace * > faces)
int BM_mesh_calc_edge_groups(BMesh *bm, int *r_groups_array, int(**r_group_index)[2], BMVertFilterFunc filter_fn, void *user_data, const char hflag_test)
static float3 compute_centroid(Span< BMVert * > verts)
float BM_face_calc_area(const BMFace *f)
void BMO_slot_vec_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name, float r_vec[3])
void bmo_flatten_exec(BMesh *bm, BMOperator *op)
MINLINE float normalize_v3(float n[3])
bool BMO_slot_bool_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
static Vector< BMVert * > collect_verts_from_faces(Span< BMFace * > faces)
int BM_mesh_calc_face_groups(BMesh *bm, int *r_groups_array, int(**r_group_index)[2], BMLoopFilterFunc filter_fn, BMLoopPairFilterFunc filter_pair_fn, void *user_data, const char hflag_test, const char htype_step)
BLI_INLINE BMFace * BM_face_at_index(BMesh *bm, const int index)
VecBase< float, 3 > float3
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
struct BMOpSlot slots_in[BMO_OP_MAX_SLOTS]