Cleanup: Use Vector for metaball tessellation vertex data

Avoid manually reallocating arrays here, and simplify future changes
to the ownership and assignment of mesh vertex normals.
This commit is contained in:
Hans Goudey
2023-03-27 19:48:04 -04:00
parent b828641a93
commit 9bb6f92fe0

View File

@@ -125,9 +125,8 @@ typedef struct process {
uint totindex; /* size of memory allocated for indices */
uint curindex; /* number of currently added indices */
float (*co)[3], (*no)[3]; /* surface vertices - positions and normals */
uint totvertex; /* memory size */
uint curvertex; /* currently added vertices */
blender::Vector<blender::float3> co; /* surface vertices positions */
blender::Vector<blender::float3> no; /* surface vertex normals */
/* memory allocation from common pool */
MemArena *pgn_elements;
@@ -940,18 +939,8 @@ static int getedge(EDGELIST *table[], int i1, int j1, int k1, int i2, int j2, in
*/
static void addtovertices(PROCESS *process, const float v[3], const float no[3])
{
if (UNLIKELY(process->curvertex == process->totvertex)) {
process->totvertex = process->totvertex ? process->totvertex * 2 : MBALL_ARRAY_LEN_INIT;
process->co = static_cast<float(*)[3]>(
MEM_reallocN(process->co, process->totvertex * sizeof(float[3])));
process->no = static_cast<float(*)[3]>(
MEM_reallocN(process->no, process->totvertex * sizeof(float[3])));
}
copy_v3_v3(process->co[process->curvertex], v);
copy_v3_v3(process->no[process->curvertex], no);
process->curvertex++;
process->co.append(v);
process->no.append(no);
}
#ifndef USE_ACCUM_NORMAL
@@ -994,7 +983,7 @@ static int vertid(PROCESS *process, const CORNER *c1, const CORNER *c2)
#endif
addtovertices(process, v, no); /* save vertex */
vid = int(process->curvertex) - 1;
vid = int(process->co.size()) - 1;
setedge(process, c1->i, c1->j, c1->k, c2->i, c2->j, c2->k, vid);
return vid;
@@ -1392,7 +1381,7 @@ static void init_meta(Depsgraph *depsgraph, PROCESS *process, Scene *scene, Obje
Mesh *BKE_mball_polygonize(Depsgraph *depsgraph, Scene *scene, Object *ob)
{
PROCESS process = {0};
PROCESS process{};
const bool is_render = DEG_get_mode(depsgraph) == DAG_EVAL_RENDER;
MetaBall *mb = static_cast<MetaBall *>(ob->data);
@@ -1431,6 +1420,8 @@ Mesh *BKE_mball_polygonize(Depsgraph *depsgraph, Scene *scene, Object *ob)
process.delta = process.size * 0.001f;
process.co.reserve(MBALL_ARRAY_LEN_INIT);
process.no.reserve(MBALL_ARRAY_LEN_INIT);
process.pgn_elements = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, "Metaball memarena");
/* initialize all mainb (MetaElems) */
@@ -1462,10 +1453,10 @@ Mesh *BKE_mball_polygonize(Depsgraph *depsgraph, Scene *scene, Object *ob)
Mesh *mesh = (Mesh *)BKE_id_new_nomain(ID_ME, ((ID *)ob->data)->name + 2);
mesh->totvert = int(process.curvertex);
CustomData_add_layer_named_with_data(
&mesh->vdata, CD_PROP_FLOAT3, process.co, mesh->totvert, "position");
process.co = nullptr;
mesh->totvert = int(process.co.size());
CustomData_add_layer_named(
&mesh->vdata, CD_PROP_FLOAT3, CD_CONSTRUCT, mesh->totvert, "position");
mesh->vert_positions_for_write().copy_from(process.co);
mesh->totpoly = int(process.curindex);
MPoly *polys = static_cast<MPoly *>(
@@ -1495,10 +1486,9 @@ Mesh *BKE_mball_polygonize(Depsgraph *depsgraph, Scene *scene, Object *ob)
for (int i = 0; i < mesh->totvert; i++) {
normalize_v3(process.no[i]);
}
memcpy(
BKE_mesh_vert_normals_for_write(mesh), process.no, sizeof(float[3]) * size_t(mesh->totvert));
MEM_freeN(process.no);
BKE_mesh_vert_normals_clear_dirty(mesh);
memcpy(BKE_mesh_vert_normals_for_write(mesh),
process.no.data(),
sizeof(float[3]) * size_t(mesh->totvert));
mesh->totloop = loop_offset;