Cleanup: Use indices in curve to mesh, decrease variable scope

This commit is contained in:
Hans Goudey 2022-12-30 23:52:44 -05:00
parent 3340cc8102
commit f891ddd98d
1 changed files with 57 additions and 61 deletions

View File

@ -143,9 +143,7 @@ static void make_edges_mdata_extend(Mesh &mesh)
static Mesh *mesh_nurbs_displist_to_mesh(const Curve *cu, const ListBase *dispbase)
{
using namespace blender::bke;
const float *data;
int a, b, ofs, vertcount, startvert, totvert = 0, totedge = 0, totloop = 0, totpoly = 0;
int p1, p2, p3, p4, *index;
int a, b, ofs;
const bool conv_polys = (
/* 2D polys are filled with #DispList.type == #DL_INDEX3. */
(CU_DO_2DFILL(cu) == false) ||
@ -153,6 +151,10 @@ static Mesh *mesh_nurbs_displist_to_mesh(const Curve *cu, const ListBase *dispba
BKE_curve_type_get(cu) == OB_SURF);
/* count */
int totvert = 0;
int totedge = 0;
int totpoly = 0;
int totloop = 0;
LISTBASE_FOREACH (const DispList *, dl, dispbase) {
if (dl->type == DL_SEGM) {
totvert += dl->parts * dl->nr;
@ -193,117 +195,110 @@ static Mesh *mesh_nurbs_displist_to_mesh(const Curve *cu, const ListBase *dispba
MutableSpan<MPoly> polys = mesh->polys_for_write();
MutableSpan<MLoop> loops = mesh->loops_for_write();
MVert *mvert = verts.data();
MEdge *medge = edges.data();
MPoly *mpoly = polys.data();
MLoop *mloop = loops.data();
MutableAttributeAccessor attributes = mesh->attributes_for_write();
SpanAttributeWriter<int> material_indices = attributes.lookup_or_add_for_write_only_span<int>(
"material_index", ATTR_DOMAIN_FACE);
MLoopUV *mloopuv = static_cast<MLoopUV *>(CustomData_add_layer_named(
&mesh->ldata, CD_MLOOPUV, CD_SET_DEFAULT, nullptr, mesh->totloop, DATA_("UVMap")));
/* verts and faces */
vertcount = 0;
int dst_vert = 0;
int dst_edge = 0;
int dst_poly = 0;
int dst_loop = 0;
LISTBASE_FOREACH (const DispList *, dl, dispbase) {
const bool is_smooth = (dl->rt & CU_SMOOTH) != 0;
if (dl->type == DL_SEGM) {
startvert = vertcount;
const int startvert = dst_vert;
a = dl->parts * dl->nr;
data = dl->verts;
const float *data = dl->verts;
while (a--) {
copy_v3_v3(mvert->co, data);
copy_v3_v3(verts[dst_vert].co, data);
data += 3;
vertcount++;
mvert++;
dst_vert++;
}
for (a = 0; a < dl->parts; a++) {
ofs = a * dl->nr;
for (b = 1; b < dl->nr; b++) {
medge->v1 = startvert + ofs + b - 1;
medge->v2 = startvert + ofs + b;
medge->flag = ME_EDGEDRAW;
edges[dst_edge].v1 = startvert + ofs + b - 1;
edges[dst_edge].v2 = startvert + ofs + b;
edges[dst_edge].flag = ME_EDGEDRAW;
medge++;
dst_edge++;
}
}
}
else if (dl->type == DL_POLY) {
if (conv_polys) {
startvert = vertcount;
const int startvert = dst_vert;
a = dl->parts * dl->nr;
data = dl->verts;
const float *data = dl->verts;
while (a--) {
copy_v3_v3(mvert->co, data);
copy_v3_v3(verts[dst_vert].co, data);
data += 3;
vertcount++;
mvert++;
dst_vert++;
}
for (a = 0; a < dl->parts; a++) {
ofs = a * dl->nr;
for (b = 0; b < dl->nr; b++) {
medge->v1 = startvert + ofs + b;
edges[dst_edge].v1 = startvert + ofs + b;
if (b == dl->nr - 1) {
medge->v2 = startvert + ofs;
edges[dst_edge].v2 = startvert + ofs;
}
else {
medge->v2 = startvert + ofs + b + 1;
edges[dst_edge].v2 = startvert + ofs + b + 1;
}
medge->flag = ME_EDGEDRAW;
medge++;
edges[dst_edge].flag = ME_EDGEDRAW;
dst_edge++;
}
}
}
}
else if (dl->type == DL_INDEX3) {
startvert = vertcount;
const int startvert = dst_vert;
a = dl->nr;
data = dl->verts;
const float *data = dl->verts;
while (a--) {
copy_v3_v3(mvert->co, data);
copy_v3_v3(verts[dst_vert].co, data);
data += 3;
vertcount++;
mvert++;
dst_vert++;
}
a = dl->parts;
index = dl->index;
const int *index = dl->index;
while (a--) {
mloop[0].v = startvert + index[0];
mloop[1].v = startvert + index[2];
mloop[2].v = startvert + index[1];
mpoly->loopstart = int(mloop - loops.data());
mpoly->totloop = 3;
material_indices.span[mpoly - polys.data()] = dl->col;
loops[dst_loop + 0].v = startvert + index[0];
loops[dst_loop + 1].v = startvert + index[2];
loops[dst_loop + 2].v = startvert + index[1];
polys[dst_poly].loopstart = dst_loop;
polys[dst_poly].totloop = 3;
material_indices.span[dst_poly] = dl->col;
if (mloopuv) {
for (int i = 0; i < 3; i++, mloopuv++) {
mloopuv->uv[0] = (mloop[i].v - startvert) / float(dl->nr - 1);
mloopuv->uv[0] = (loops[dst_loop + i].v - startvert) / float(dl->nr - 1);
mloopuv->uv[1] = 0.0f;
}
}
if (is_smooth) {
mpoly->flag |= ME_SMOOTH;
polys[dst_poly].flag |= ME_SMOOTH;
}
mpoly++;
mloop += 3;
dst_poly++;
dst_loop += 3;
index += 3;
}
}
else if (dl->type == DL_SURF) {
startvert = vertcount;
const int startvert = dst_vert;
a = dl->parts * dl->nr;
data = dl->verts;
const float *data = dl->verts;
while (a--) {
copy_v3_v3(mvert->co, data);
copy_v3_v3(verts[dst_vert].co, data);
data += 3;
vertcount++;
mvert++;
dst_vert++;
}
for (a = 0; a < dl->parts; a++) {
@ -312,6 +307,7 @@ static Mesh *mesh_nurbs_displist_to_mesh(const Curve *cu, const ListBase *dispba
break;
}
int p1, p2, p3, p4;
if (dl->flag & DL_CYCL_U) { /* p2 -> p1 -> */
p1 = startvert + dl->nr * a; /* p4 -> p3 -> */
p2 = p1 + dl->nr - 1; /* -----> next row */
@ -332,13 +328,13 @@ static Mesh *mesh_nurbs_displist_to_mesh(const Curve *cu, const ListBase *dispba
}
for (; b < dl->nr; b++) {
mloop[0].v = p1;
mloop[1].v = p3;
mloop[2].v = p4;
mloop[3].v = p2;
mpoly->loopstart = int(mloop - loops.data());
mpoly->totloop = 4;
material_indices.span[mpoly - polys.data()] = dl->col;
loops[dst_loop + 0].v = p1;
loops[dst_loop + 1].v = p3;
loops[dst_loop + 2].v = p4;
loops[dst_loop + 3].v = p2;
polys[dst_poly].loopstart = dst_loop;
polys[dst_poly].totloop = 4;
material_indices.span[dst_poly] = dl->col;
if (mloopuv) {
int orco_sizeu = dl->nr - 1;
@ -357,7 +353,7 @@ static Mesh *mesh_nurbs_displist_to_mesh(const Curve *cu, const ListBase *dispba
for (int i = 0; i < 4; i++, mloopuv++) {
/* find uv based on vertex index into grid array */
int v = mloop[i].v - startvert;
int v = loops[dst_loop + i].v - startvert;
mloopuv->uv[0] = (v / dl->nr) / float(orco_sizev);
mloopuv->uv[1] = (v % dl->nr) / float(orco_sizeu);
@ -373,10 +369,10 @@ static Mesh *mesh_nurbs_displist_to_mesh(const Curve *cu, const ListBase *dispba
}
if (is_smooth) {
mpoly->flag |= ME_SMOOTH;
polys[dst_poly].flag |= ME_SMOOTH;
}
mpoly++;
mloop += 4;
dst_poly++;
dst_loop += 4;
p4 = p3;
p3++;