Blender V5.3
editmesh.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2005 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "MEM_guardedalloc.h"
10
11#include "DNA_mesh_types.h"
12#include "DNA_object_types.h"
13
14#include "BLI_bitmap.hh"
15#include "BLI_math_geom_c.hh"
16#include "BLI_math_vector_c.hh"
17
18#include "BKE_customdata.hh"
19#include "BKE_editmesh.hh"
20#include "BKE_mesh.hh"
21#include "BKE_mesh_iterators.hh"
22#include "BKE_mesh_runtime.hh"
23#include "BKE_mesh_wrapper.hh"
24#include "BKE_object.hh"
25
27
28namespace blender {
29
31{
32 BMEditMesh *em = MEM_new<BMEditMesh>(__func__);
33 em->bm = bm;
34 return em;
35}
36
38{
39 BMEditMesh *em_copy = MEM_new<BMEditMesh>(__func__);
40 *em_copy = *em;
41
42 em_copy->bm = BM_mesh_copy(em->bm);
43
44 /* The tessellation is NOT calculated on the copy here,
45 * because currently all the callers of this function use
46 * it to make a backup copy of the #BMEditMesh to restore
47 * it in the case of errors in an operation. For performance reasons,
48 * in that case it makes more sense to do the
49 * tessellation only when/if that copy ends up getting used. */
50 em_copy->looptris = {};
51
52 /* Copy various settings. */
53 em_copy->selectmode = em->selectmode;
54 em_copy->mat_nr = em->mat_nr;
55
56 return em_copy;
57}
58
60{
61 BLI_assert(ob->type == OB_MESH);
62 return (id_cast<Mesh *>(ob->data))->runtime->edit_mesh.get();
63}
64
66{
67 const Mesh *mesh = id_cast<const Mesh *>(ob->data);
69}
70
72{
73 if (!mesh->runtime->edit_mesh) {
74 return nullptr;
75 }
76 return mesh->runtime->edit_mesh->bm;
77}
78
80{
81 if (!mesh->runtime->edit_mesh) {
82 return nullptr;
83 }
84 return mesh->runtime->edit_mesh->bm;
85}
86
92
93bool BKE_editmesh_eval_orig_map_available(const Mesh &mesh_eval, const Mesh *mesh_orig)
94{
95 if (!mesh_orig) {
96 return false;
97 }
98 if (&mesh_eval == mesh_orig) {
99 return true;
100 }
101 if (mesh_eval.runtime->edit_mesh) {
102 return mesh_eval.runtime->edit_mesh == mesh_orig->runtime->edit_mesh;
103 }
104 return false;
105}
106
114
116{
118 params.face_normals = false;
119 BKE_editmesh_looptris_calc_ex(em, bm, &params);
120}
121
123{
124 BMeshCalcTessellation_Params looptris_params{};
125 looptris_params.face_normals = true;
126 BKE_editmesh_looptris_calc_ex(em, bm, &looptris_params);
127 BMeshNormalsUpdate_Params normals_params{};
128 normals_params.face_normals = false;
129 BM_mesh_normals_update_ex(bm, &normals_params);
130}
131
133 BMesh *bm,
134 BMPartialUpdate *bmpinfo,
135 const BMeshCalcTessellation_Params *params)
136{
138 BLI_assert(!(bm->totface && em->looptris.is_empty()));
139
141}
142
144{
145 BMeshCalcTessellation_Params looptris_params{};
146 looptris_params.face_normals = false;
147 BKE_editmesh_looptris_calc_with_partial_ex(em, bm, bmpinfo, &looptris_params);
148}
149
151 BMesh *bm,
152 BMPartialUpdate *bmpinfo)
153{
154 BMeshCalcTessellation_Params looptris_params{};
155 looptris_params.face_normals = true;
156 BKE_editmesh_looptris_calc_with_partial_ex(em, bm, bmpinfo, &looptris_params);
157 BMeshNormalsUpdate_Params normals_params{};
158 normals_params.face_normals = false;
159 BM_mesh_normals_update_with_partial_ex(bm, bmpinfo, &normals_params);
160}
161
163{
164 em->looptris = {};
165
166 if (BMesh *bm = em->bm) {
168 }
169}
170
176
177static void cage_mapped_verts_callback(void *user_data,
178 int index,
179 const float co[3],
180 const float /*no*/[3])
181{
182 CageUserData *data = static_cast<CageUserData *>(user_data);
183
184 if ((index >= 0 && index < data->totvert) && !BLI_BITMAP_TEST(data->visit_bitmap, index)) {
185 BLI_BITMAP_ENABLE(data->visit_bitmap, index);
186 copy_v3_v3(data->positions_cage[index], co);
187 }
188}
189
191 const BMesh *bm,
192 Scene *scene,
193 Object *ob)
194{
195 const Mesh *cage = bke::editbmesh_get_eval_cage(depsgraph, scene, ob, &CD_MASK_BAREMESH);
196 Array<float3> positions_cage(bm->totvert);
197
198 /* When initializing cage verts, we only want the first cage coordinate for each vertex,
199 * so that e.g. mirror or array use original vertex coordinates and not mirrored or duplicate. */
200 BLI_bitmap *visit_bitmap = BLI_BITMAP_NEW(bm->totvert, __func__);
201
204 data.positions_cage = positions_cage;
205 data.visit_bitmap = visit_bitmap;
206
208
209 MEM_delete(visit_bitmap);
210
211 return positions_cage;
212}
213
215 Scene *scene,
216 Object *ob,
217 Array<float3> &r_alloc)
218{
219 Object *object_orig = DEG_get_original(ob);
220 const Object *object_eval = DEG_get_evaluated(depsgraph, ob);
221 const Mesh *editmesh_eval_final = BKE_object_get_editmesh_eval_final(object_eval);
222 const Mesh *mesh_cage = BKE_object_get_editmesh_eval_cage(ob);
223 const BMesh *bm = BKE_editmesh_bmesh_get(object_orig);
224
225 Span<float3> vert_positions;
226 if (mesh_cage && mesh_cage->runtime->deformed_only) {
228 /* Deformed, and we have deformed coords already. */
229 vert_positions = BKE_mesh_wrapper_vert_coords(mesh_cage);
230 }
231 else if ((editmesh_eval_final != nullptr) &&
232 (editmesh_eval_final->runtime->wrapper_type == ME_WRAPPER_TYPE_BMESH))
233 {
234 /* If this is an edit-mesh type, leave nullptr as we can use the vertex coords. */
235
236 /* If this is not empty, it's value should be assigned to `vert_positions`
237 * however the `mesh_cage` check above should handle this case. */
238 BLI_assert(BKE_mesh_wrapper_vert_coords(mesh_cage).is_empty());
239 }
240 else {
241 /* Constructive modifiers have been used, we need to allocate coordinates. */
242 r_alloc = BKE_editmesh_vert_coords_alloc(depsgraph, bm, scene, ob);
243 return r_alloc.as_span();
244 }
245 return vert_positions;
246}
247
252
257
258} // namespace blender
CustomData interface, see also DNA_customdata_types.h.
General operations, lookup, etc. for blender objects.
#define BLI_BITMAP_NEW(_num, _alloc_string)
Definition BLI_bitmap.hh:39
#define BLI_BITMAP_TEST(_bitmap, _index)
Definition BLI_bitmap.hh:63
#define BLI_BITMAP_ENABLE(_bitmap, _index)
Definition BLI_bitmap.hh:80
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
Span< T > as_span() const
Definition BLI_array.hh:254
const Mesh * editbmesh_get_eval_cage(Depsgraph *depsgraph, const Scene *scene, Object *obedit, const CustomData_MeshMasks *dataMask)
BMesh Iterators.
void BM_lnorspace_update(BMesh *bm)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BKE_editmesh_looptris_calc_with_partial(BMEditMesh *em, BMesh *bm, BMPartialUpdate *bmpinfo)
Definition editmesh.cc:143
const Mesh * BKE_object_get_editmesh_eval_cage(const Object *object)
Definition object.cc:4492
void BKE_editmesh_looptris_calc_ex(BMEditMesh *em, BMesh *bm, const BMeshCalcTessellation_Params *params)
Definition editmesh.cc:107
const CustomData_MeshMasks CD_MASK_BAREMESH
static void cage_mapped_verts_callback(void *user_data, int index, const float co[3], const float[3])
Definition editmesh.cc:177
void BM_mesh_free(BMesh *bm)
BMesh Free Mesh.
void BKE_editmesh_looptris_calc(BMEditMesh *em, BMesh *bm)
Definition editmesh.cc:115
BMesh * BKE_editmesh_bmesh_get_for_write(Mesh *mesh)
Definition editmesh.cc:79
void BKE_editmesh_looptris_calc_with_partial_ex(BMEditMesh *em, BMesh *bm, BMPartialUpdate *bmpinfo, const BMeshCalcTessellation_Params *params)
Definition editmesh.cc:132
Span< float3 > BKE_editmesh_vert_coords_when_deformed(Depsgraph *depsgraph, Scene *scene, Object *obedit, Array< float3 > &r_alloc)
Definition editmesh.cc:214
MINLINE void copy_v3_v3(float r[3], const float a[3])
BMEditMesh * BKE_editmesh_create(BMesh *bm)
Definition editmesh.cc:30
const Mesh * BKE_object_get_editmesh_eval_final(const Object *object)
Definition object.cc:4477
void BM_mesh_normals_update_ex(BMesh *bm, const BMeshNormalsUpdate_Params *params)
BMesh Compute Normals.
BMesh * BM_mesh_copy(BMesh *bm_old)
BMesh const char void * data
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
void BKE_editmesh_free_data(BMEditMesh *em)
Definition editmesh.cc:162
void BM_mesh_calc_tessellation_with_partial_ex(BMesh *bm, MutableSpan< std::array< BMLoop *, 3 > > looptris, const BMPartialUpdate *bmpinfo, const BMeshCalcTessellation_Params *params)
void BKE_editmesh_looptris_and_normals_calc(BMEditMesh *em, BMesh *bm)
Definition editmesh.cc:122
void BKE_editmesh_looptris_and_normals_calc_with_partial(BMEditMesh *em, BMesh *bm, BMPartialUpdate *bmpinfo)
Definition editmesh.cc:150
Array< float3 > BM_mesh_vert_coords_alloc(const BMesh *bm)
int BKE_mesh_wrapper_vert_len(const Mesh *mesh)
bool BKE_editmesh_eval_orig_map_available(const Mesh &mesh_eval, const Mesh *mesh_orig)
Definition editmesh.cc:93
MINLINE int poly_to_tri_count(int poly_count, int corner_count)
BMEditMesh * BKE_editmesh_from_object(Object *ob)
Return the BMEditMesh for a given object.
Definition editmesh.cc:59
void BKE_editmesh_lnorspace_update(BMesh *bm)
Definition editmesh.cc:253
T * DEG_get_original(T *id)
unsigned int BLI_bitmap
Definition BLI_bitmap.hh:15
Array< float3 > BKE_editmesh_vert_coords_alloc(Depsgraph *depsgraph, const BMesh *bm, Scene *scene, Object *ob)
Definition editmesh.cc:190
void BKE_mesh_foreach_mapped_vert(const Mesh *mesh, void(*func)(void *user_data, int index, const float co[3], const float no[3]), void *user_data, MeshForeachFlag flag)
Span< float3 > BKE_mesh_wrapper_vert_coords(const Mesh *mesh)
const BMesh * BKE_editmesh_bmesh_get(const Object *ob)
Definition editmesh.cc:65
Array< float3 > BKE_editmesh_vert_coords_alloc_orco(const BMesh *bm)
Definition editmesh.cc:248
@ ME_WRAPPER_TYPE_BMESH
void BM_mesh_calc_tessellation_ex(BMesh *bm, MutableSpan< std::array< BMLoop *, 3 > > looptris, const BMeshCalcTessellation_Params *params)
BMEditMesh * BKE_editmesh_copy(BMEditMesh *em)
Definition editmesh.cc:37
void BM_mesh_normals_update_with_partial_ex(BMesh *, const BMPartialUpdate *bmpinfo, const BMeshNormalsUpdate_Params *params)
Array< std::array< BMLoop *, 3 > > looptris
BLI_bitmap * visit_bitmap
Definition editmesh.cc:174
MutableSpan< float3 > positions_cage
Definition editmesh.cc:173
bke::MeshRuntime * runtime
std::shared_ptr< BMEditMesh > edit_mesh
eMeshWrapperType wrapper_type