Cleanup: Use generic edge calculation for legacy curve to mesh

Change the "displist to mesh" conversion to use the edge calculation
function used everywhere else, to allow removing the old code. This
changes edge vertex and corner edge indices, requiring a test update,
but the visual result should be the same.
This commit is contained in:
Hans Goudey
2023-04-19 09:29:08 -04:00
parent b647c2b88d
commit 5ab48a53e4

View File

@@ -62,98 +62,8 @@ using blender::MutableSpan;
using blender::Span;
using blender::StringRefNull;
/* Define for cases when you want extra validation of mesh
* after certain modifications.
*/
// #undef VALIDATE_MESH
#ifdef VALIDATE_MESH
# define ASSERT_IS_VALID_MESH(mesh) \
(BLI_assert((mesh == nullptr) || (BKE_mesh_is_valid(mesh) == true)))
#else
# define ASSERT_IS_VALID_MESH(mesh)
#endif
static CLG_LogRef LOG = {"bke.mesh_convert"};
static void poly_edgehash_insert(EdgeHash *ehash, const Span<int> poly_verts)
{
int i = poly_verts.size();
int next = 0; /* first loop */
int poly_corner = (i - 1); /* last loop */
while (i-- != 0) {
BLI_edgehash_reinsert(ehash, poly_verts[poly_corner], poly_verts[next], nullptr);
poly_corner = next;
next++;
}
}
/**
* Specialized function to use when we _know_ existing edges don't overlap with poly edges.
*/
static void make_edges_mdata_extend(Mesh &mesh)
{
int totedge = mesh.totedge;
const blender::OffsetIndices polys = mesh.polys();
const Span<int> corner_verts = mesh.corner_verts();
MutableSpan<int> corner_edges = mesh.corner_edges_for_write();
const int eh_reserve = max_ii(totedge, BLI_EDGEHASH_SIZE_GUESS_FROM_POLYS(mesh.totpoly));
EdgeHash *eh = BLI_edgehash_new_ex(__func__, eh_reserve);
for (const int i : polys.index_range()) {
poly_edgehash_insert(eh, corner_verts.slice(polys[i]));
}
const int totedge_new = BLI_edgehash_len(eh);
#ifdef DEBUG
/* ensure that there's no overlap! */
if (totedge_new) {
for (const blender::int2 &edge : mesh.edges()) {
BLI_assert(BLI_edgehash_haskey(eh, edge[0], edge[1]) == false);
}
}
#endif
if (totedge_new) {
/* The only layer should be edges, so no other layers need to be initialized. */
BLI_assert(mesh.edata.totlayer == 1);
CustomData_realloc(&mesh.edata, totedge, totedge + totedge_new);
mesh.totedge += totedge_new;
MutableSpan<blender::int2> edges = mesh.edges_for_write();
blender::int2 *edge = &edges[totedge];
EdgeHashIterator *ehi;
uint e_index = totedge;
for (ehi = BLI_edgehashIterator_new(eh); BLI_edgehashIterator_isDone(ehi) == false;
BLI_edgehashIterator_step(ehi), ++edge, e_index++) {
BLI_edgehashIterator_getKey(ehi, &(*edge)[0], &(*edge)[1]);
BLI_edgehashIterator_setValue(ehi, POINTER_FROM_UINT(e_index));
}
BLI_edgehashIterator_free(ehi);
for (const int i : polys.index_range()) {
const IndexRange poly = polys[i];
int corner = poly.start();
int corner_prev = poly.start() + (poly.size() - 1);
int j;
for (j = 0; j < poly.size(); j++, corner++) {
/* lookup hashed edge index */
corner_edges[corner_prev] = POINTER_AS_UINT(
BLI_edgehash_lookup(eh, corner_verts[corner_prev], corner_verts[corner]));
corner_prev = corner;
}
}
}
BLI_edgehash_free(eh, nullptr);
}
static Mesh *mesh_nurbs_displist_to_mesh(const Curve *cu, const ListBase *dispbase)
{
using namespace blender::bke;
@@ -393,7 +303,7 @@ static Mesh *mesh_nurbs_displist_to_mesh(const Curve *cu, const ListBase *dispba
}
if (totpoly) {
make_edges_mdata_extend(*mesh);
BKE_mesh_calc_edges(mesh, true, false);
}
material_indices.finish();