Blender V4.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
9#include "MEM_guardedalloc.h"
10
11#include "DNA_listBase.h"
12#include "DNA_mesh_types.h"
13#include "DNA_object_types.h"
14
15#include "BLI_bitmap.h"
16#include "BLI_math_geom.h"
17#include "BLI_math_vector.h"
18
19#include "BKE_customdata.hh"
20#include "BKE_editmesh.hh"
21#include "BKE_mesh.hh"
22#include "BKE_mesh_iterators.hh"
23#include "BKE_mesh_runtime.hh"
24#include "BKE_mesh_wrapper.hh"
25#include "BKE_object.hh"
26#include "BKE_object_types.hh"
27
29
30using blender::Array;
31using blender::float3;
32using blender::Span;
33
35{
36 BMEditMesh *em = MEM_new<BMEditMesh>(__func__);
37 em->bm = bm;
38 return em;
39}
40
42{
43 BMEditMesh *em_copy = MEM_new<BMEditMesh>(__func__);
44 *em_copy = *em;
45
46 em_copy->bm = BM_mesh_copy(em->bm);
47
48 /* The tessellation is NOT calculated on the copy here,
49 * because currently all the callers of this function use
50 * it to make a backup copy of the #BMEditMesh to restore
51 * it in the case of errors in an operation. For performance reasons,
52 * in that case it makes more sense to do the
53 * tessellation only when/if that copy ends up getting used. */
54 em_copy->looptris = {};
55
56 /* Copy various settings. */
57 em_copy->selectmode = em->selectmode;
58 em_copy->mat_nr = em->mat_nr;
59
60 return em_copy;
61}
62
64{
65 BLI_assert(ob->type == OB_MESH);
66 return ((Mesh *)ob->data)->runtime->edit_mesh.get();
67}
68
75
82
84{
85 BMeshCalcTessellation_Params looptris_params{};
86 looptris_params.face_normals = true;
87 BKE_editmesh_looptris_calc_ex(em, &looptris_params);
88 BMeshNormalsUpdate_Params normals_params{};
89 normals_params.face_normals = false;
90 BM_mesh_normals_update_ex(em->bm, &normals_params);
91}
92
102
104{
105 BMeshCalcTessellation_Params looptris_params{};
106 looptris_params.face_normals = false;
107 BKE_editmesh_looptris_calc_with_partial_ex(em, bmpinfo, &looptris_params);
108}
109
111{
112 BMeshCalcTessellation_Params looptris_params{};
113 looptris_params.face_normals = true;
114 BKE_editmesh_looptris_calc_with_partial_ex(em, bmpinfo, &looptris_params);
115 BMeshNormalsUpdate_Params normals_params{};
116 normals_params.face_normals = false;
117 BM_mesh_normals_update_with_partial_ex(em->bm, bmpinfo, &normals_params);
118}
119
121{
122 em->looptris = {};
123
124 if (em->bm) {
125 BM_mesh_free(em->bm);
126 }
127}
128
134
135static void cage_mapped_verts_callback(void *user_data,
136 int index,
137 const float co[3],
138 const float /*no*/[3])
139{
140 CageUserData *data = static_cast<CageUserData *>(user_data);
141
142 if ((index >= 0 && index < data->totvert) && !BLI_BITMAP_TEST(data->visit_bitmap, index)) {
143 BLI_BITMAP_ENABLE(data->visit_bitmap, index);
144 copy_v3_v3(data->positions_cage[index], co);
145 }
146}
147
149 BMEditMesh *em,
150 Scene *scene,
151 Object *ob)
152{
154 Array<float3> positions_cage(em->bm->totvert);
155
156 /* When initializing cage verts, we only want the first cage coordinate for each vertex,
157 * so that e.g. mirror or array use original vertex coordinates and not mirrored or duplicate. */
158 BLI_bitmap *visit_bitmap = BLI_BITMAP_NEW(em->bm->totvert, __func__);
159
161 data.totvert = em->bm->totvert;
162 data.positions_cage = positions_cage;
163 data.visit_bitmap = visit_bitmap;
164
166
167 MEM_freeN(visit_bitmap);
168
169 return positions_cage;
170}
171
173 Depsgraph *depsgraph, BMEditMesh *em, Scene *scene, Object *ob, Array<float3> &r_alloc)
174{
175
176 const Object *object_eval = DEG_get_evaluated_object(depsgraph, ob);
177 const Mesh *editmesh_eval_final = BKE_object_get_editmesh_eval_final(object_eval);
178 const Mesh *mesh_cage = BKE_object_get_editmesh_eval_cage(ob);
179
180 Span<float3> vert_positions;
181 if (mesh_cage && mesh_cage->runtime->deformed_only) {
183 /* Deformed, and we have deformed coords already. */
184 vert_positions = BKE_mesh_wrapper_vert_coords(mesh_cage);
185 }
186 else if ((editmesh_eval_final != nullptr) &&
187 (editmesh_eval_final->runtime->wrapper_type == ME_WRAPPER_TYPE_BMESH))
188 {
189 /* If this is an edit-mesh type, leave nullptr as we can use the vertex coords. */
190
191 /* If this is not empty, it's value should be assigned to `vert_positions`
192 * however the `mesh_cage` check above should handle this case. */
193 BLI_assert(BKE_mesh_wrapper_vert_coords(mesh_cage).is_empty());
194 }
195 else {
196 /* Constructive modifiers have been used, we need to allocate coordinates. */
197 r_alloc = BKE_editmesh_vert_coords_alloc(depsgraph, em, scene, ob);
198 return r_alloc.as_span();
199 }
200 return vert_positions;
201}
202
207
CustomData interface, see also DNA_customdata_types.h.
const CustomData_MeshMasks CD_MASK_BAREMESH
@ MESH_FOREACH_NOP
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)
@ ME_WRAPPER_TYPE_BMESH
blender::Span< blender::float3 > BKE_mesh_wrapper_vert_coords(const Mesh *mesh)
int BKE_mesh_wrapper_vert_len(const Mesh *mesh)
General operations, lookup, etc. for blender objects.
const Mesh * BKE_object_get_editmesh_eval_cage(const Object *object)
const Mesh * BKE_object_get_editmesh_eval_final(const Object *object)
#define BLI_assert(a)
Definition BLI_assert.h:50
#define BLI_BITMAP_NEW(_num, _alloc_string)
Definition BLI_bitmap.h:41
#define BLI_BITMAP_TEST(_bitmap, _index)
Definition BLI_bitmap.h:65
#define BLI_BITMAP_ENABLE(_bitmap, _index)
Definition BLI_bitmap.h:82
unsigned int BLI_bitmap
Definition BLI_bitmap.h:17
MINLINE int poly_to_tri_count(int poly_count, int corner_count)
MINLINE void copy_v3_v3(float r[3], const float a[3])
Object * DEG_get_evaluated_object(const Depsgraph *depsgraph, Object *object)
These structs are the foundation for all linked lists in the library system.
Object is a sort of wrapper for general info.
@ OB_MESH
Read Guarded memory(de)allocation.
BMesh * BM_mesh_copy(BMesh *bm_old)
ATTR_WARN_UNUSED_RESULT BMesh * bm
void BM_mesh_free(BMesh *bm)
BMesh Free Mesh.
Array< float3 > BM_mesh_vert_coords_alloc(BMesh *bm)
void BM_lnorspace_update(BMesh *bm)
void BM_mesh_normals_update_ex(BMesh *bm, const BMeshNormalsUpdate_Params *params)
BMesh Compute Normals.
void BM_mesh_normals_update_with_partial_ex(BMesh *, const BMPartialUpdate *bmpinfo, const BMeshNormalsUpdate_Params *params)
void BM_mesh_calc_tessellation_with_partial_ex(BMesh *bm, MutableSpan< std::array< BMLoop *, 3 > > looptris, const BMPartialUpdate *bmpinfo, const BMeshCalcTessellation_Params *params)
void BM_mesh_calc_tessellation_ex(BMesh *bm, MutableSpan< std::array< BMLoop *, 3 > > looptris, const BMeshCalcTessellation_Params *params)
int64_t size() const
Definition BLI_array.hh:245
Span< T > as_span() const
Definition BLI_array.hh:232
void reinitialize(const int64_t new_size)
Definition BLI_array.hh:388
bool is_empty() const
Definition BLI_array.hh:253
const Depsgraph * depsgraph
void BKE_editmesh_free_data(BMEditMesh *em)
Definition editmesh.cc:120
BMEditMesh * BKE_editmesh_from_object(Object *ob)
Return the BMEditMesh for a given object.
Definition editmesh.cc:63
BMEditMesh * BKE_editmesh_create(BMesh *bm)
Definition editmesh.cc:34
void BKE_editmesh_looptris_and_normals_calc_with_partial(BMEditMesh *em, BMPartialUpdate *bmpinfo)
Definition editmesh.cc:110
void BKE_editmesh_looptris_calc_with_partial_ex(BMEditMesh *em, BMPartialUpdate *bmpinfo, const BMeshCalcTessellation_Params *params)
Definition editmesh.cc:93
Span< float3 > BKE_editmesh_vert_coords_when_deformed(Depsgraph *depsgraph, BMEditMesh *em, Scene *scene, Object *ob, Array< float3 > &r_alloc)
Definition editmesh.cc:172
void BKE_editmesh_looptris_calc(BMEditMesh *em)
Definition editmesh.cc:76
void BKE_editmesh_looptris_calc_ex(BMEditMesh *em, const BMeshCalcTessellation_Params *params)
Definition editmesh.cc:69
void BKE_editmesh_looptris_and_normals_calc(BMEditMesh *em)
Definition editmesh.cc:83
void BKE_editmesh_lnorspace_update(BMEditMesh *em)
Definition editmesh.cc:208
BMEditMesh * BKE_editmesh_copy(BMEditMesh *em)
Definition editmesh.cc:41
static void cage_mapped_verts_callback(void *user_data, int index, const float co[3], const float[3])
Definition editmesh.cc:135
Array< float3 > BKE_editmesh_vert_coords_alloc(Depsgraph *depsgraph, BMEditMesh *em, Scene *scene, Object *ob)
Definition editmesh.cc:148
void BKE_editmesh_looptris_calc_with_partial(BMEditMesh *em, BMPartialUpdate *bmpinfo)
Definition editmesh.cc:103
Array< float3 > BKE_editmesh_vert_coords_alloc_orco(BMEditMesh *em)
Definition editmesh.cc:203
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
Mesh * editbmesh_get_eval_cage(Depsgraph *depsgraph, const Scene *scene, Object *obedit, BMEditMesh *em, const CustomData_MeshMasks *dataMask)
short selectmode
blender::Array< std::array< BMLoop *, 3 > > looptris
int totvert
int totloop
int totface
blender::MutableSpan< float3 > positions_cage
Definition editmesh.cc:131
BLI_bitmap * visit_bitmap
Definition editmesh.cc:132
MeshRuntimeHandle * runtime