diff options
Diffstat (limited to 'source/blender/blenkernel/intern/mesh_evaluate.cc')
-rw-r--r-- | source/blender/blenkernel/intern/mesh_evaluate.cc | 498 |
1 files changed, 126 insertions, 372 deletions
diff --git a/source/blender/blenkernel/intern/mesh_evaluate.cc b/source/blender/blenkernel/intern/mesh_evaluate.cc index de0489d668f..938d7e42aa3 100644 --- a/source/blender/blenkernel/intern/mesh_evaluate.cc +++ b/source/blender/blenkernel/intern/mesh_evaluate.cc @@ -22,15 +22,17 @@ #include "BLI_math.h" #include "BLI_span.hh" #include "BLI_utildefines.h" +#include "BLI_virtual_array.hh" #include "BKE_customdata.h" +#include "BKE_attribute.hh" #include "BKE_mesh.h" #include "BKE_multires.h" -using blender::IndexRange; using blender::MutableSpan; using blender::Span; +using blender::VArray; /* -------------------------------------------------------------------- */ /** \name Polygon Calculations @@ -203,18 +205,13 @@ float BKE_mesh_calc_poly_area(const MPoly *mpoly, const MLoop *loopstart, const float BKE_mesh_calc_area(const Mesh *me) { - MVert *mvert = me->mvert; - MLoop *mloop = me->mloop; - MPoly *mpoly = me->mpoly; + const Span<MVert> verts = me->verts(); + const Span<MPoly> polys = me->polys(); + const Span<MLoop> loops = me->loops(); - MPoly *mp; - int i = me->totpoly; - float total_area = 0; - - for (mp = mpoly; i--; mp++) { - MLoop *ml_start = &mloop[mp->loopstart]; - - total_area += BKE_mesh_calc_poly_area(mp, ml_start, mvert); + float total_area = 0.0f; + for (const MPoly &poly : polys) { + total_area += BKE_mesh_calc_poly_area(&poly, &loops[poly.loopstart], verts.data()); } return total_area; } @@ -403,11 +400,10 @@ void BKE_mesh_poly_edgebitmap_insert(uint *edge_bitmap, const MPoly *mp, const M bool BKE_mesh_center_median(const Mesh *me, float r_cent[3]) { - int i = me->totvert; - const MVert *mvert; + const Span<MVert> verts = me->verts(); zero_v3(r_cent); - for (mvert = me->mvert; i--; mvert++) { - add_v3_v3(r_cent, mvert->co); + for (const MVert &vert : verts) { + add_v3_v3(r_cent, vert.co); } /* otherwise we get NAN for 0 verts */ if (me->totvert) { @@ -418,18 +414,17 @@ bool BKE_mesh_center_median(const Mesh *me, float r_cent[3]) bool BKE_mesh_center_median_from_polys(const Mesh *me, float r_cent[3]) { - int i = me->totpoly; int tot = 0; - const MPoly *mpoly = me->mpoly; - const MLoop *mloop = me->mloop; - const MVert *mvert = me->mvert; + const Span<MVert> verts = me->verts(); + const Span<MPoly> polys = me->polys(); + const Span<MLoop> loops = me->loops(); zero_v3(r_cent); - for (; i--; mpoly++) { - int loopend = mpoly->loopstart + mpoly->totloop; - for (int j = mpoly->loopstart; j < loopend; j++) { - add_v3_v3(r_cent, mvert[mloop[j].v].co); + for (const MPoly &poly : polys) { + int loopend = poly.loopstart + poly.totloop; + for (int j = poly.loopstart; j < loopend; j++) { + add_v3_v3(r_cent, verts[loops[j].v].co); } - tot += mpoly->totloop; + tot += poly.totloop; } /* otherwise we get NAN for 0 verts */ if (me->totpoly) { @@ -453,17 +448,19 @@ bool BKE_mesh_center_bounds(const Mesh *me, float r_cent[3]) bool BKE_mesh_center_of_surface(const Mesh *me, float r_cent[3]) { int i = me->totpoly; - MPoly *mpoly; + const MPoly *mpoly; float poly_area; float total_area = 0.0f; float poly_cent[3]; + const MVert *verts = BKE_mesh_verts(me); + const MPoly *polys = BKE_mesh_polys(me); + const MLoop *loops = BKE_mesh_loops(me); zero_v3(r_cent); /* calculate a weighted average of polygon centroids */ - for (mpoly = me->mpoly; i--; mpoly++) { - poly_area = mesh_calc_poly_area_centroid( - mpoly, me->mloop + mpoly->loopstart, me->mvert, poly_cent); + for (mpoly = polys; i--; mpoly++) { + poly_area = mesh_calc_poly_area_centroid(mpoly, loops + mpoly->loopstart, verts, poly_cent); madd_v3_v3fl(r_cent, poly_cent, poly_area); total_area += poly_area; @@ -484,10 +481,13 @@ bool BKE_mesh_center_of_surface(const Mesh *me, float r_cent[3]) bool BKE_mesh_center_of_volume(const Mesh *me, float r_cent[3]) { int i = me->totpoly; - MPoly *mpoly; + const MPoly *mpoly; float poly_volume; float total_volume = 0.0f; float poly_cent[3]; + const MVert *verts = BKE_mesh_verts(me); + const MPoly *polys = BKE_mesh_polys(me); + const MLoop *loops = BKE_mesh_loops(me); /* Use an initial center to avoid numeric instability of geometry far away from the center. */ float init_cent[3]; @@ -496,9 +496,9 @@ bool BKE_mesh_center_of_volume(const Mesh *me, float r_cent[3]) zero_v3(r_cent); /* calculate a weighted average of polyhedron centroids */ - for (mpoly = me->mpoly; i--; mpoly++) { + for (mpoly = polys; i--; mpoly++) { poly_volume = mesh_calc_poly_volume_centroid_with_reference_center( - mpoly, me->mloop + mpoly->loopstart, me->mvert, init_cent, poly_cent); + mpoly, loops + mpoly->loopstart, verts, init_cent, poly_cent); /* poly_cent is already volume-weighted, so no need to multiply by the volume */ add_v3_v3(r_cent, poly_cent); @@ -632,278 +632,6 @@ void BKE_mesh_calc_volume(const MVert *mverts, /** \} */ -/* -------------------------------------------------------------------- */ -/** \name NGon Tessellation (NGon to MFace Conversion) - * \{ */ - -static void bm_corners_to_loops_ex(ID *id, - CustomData *fdata, - CustomData *ldata, - MFace *mface, - int totloop, - int findex, - int loopstart, - int numTex, - int numCol) -{ - MFace *mf = mface + findex; - - for (int i = 0; i < numTex; i++) { - const MTFace *texface = (const MTFace *)CustomData_get_n(fdata, CD_MTFACE, findex, i); - - MLoopUV *mloopuv = (MLoopUV *)CustomData_get_n(ldata, CD_MLOOPUV, loopstart, i); - copy_v2_v2(mloopuv->uv, texface->uv[0]); - mloopuv++; - copy_v2_v2(mloopuv->uv, texface->uv[1]); - mloopuv++; - copy_v2_v2(mloopuv->uv, texface->uv[2]); - mloopuv++; - - if (mf->v4) { - copy_v2_v2(mloopuv->uv, texface->uv[3]); - mloopuv++; - } - } - - for (int i = 0; i < numCol; i++) { - MLoopCol *mloopcol = (MLoopCol *)CustomData_get_n(ldata, CD_PROP_BYTE_COLOR, loopstart, i); - const MCol *mcol = (const MCol *)CustomData_get_n(fdata, CD_MCOL, findex, i); - - MESH_MLOOPCOL_FROM_MCOL(mloopcol, &mcol[0]); - mloopcol++; - MESH_MLOOPCOL_FROM_MCOL(mloopcol, &mcol[1]); - mloopcol++; - MESH_MLOOPCOL_FROM_MCOL(mloopcol, &mcol[2]); - mloopcol++; - if (mf->v4) { - MESH_MLOOPCOL_FROM_MCOL(mloopcol, &mcol[3]); - mloopcol++; - } - } - - if (CustomData_has_layer(fdata, CD_TESSLOOPNORMAL)) { - float(*lnors)[3] = (float(*)[3])CustomData_get(ldata, loopstart, CD_NORMAL); - const short(*tlnors)[3] = (short(*)[3])CustomData_get(fdata, findex, CD_TESSLOOPNORMAL); - const int max = mf->v4 ? 4 : 3; - - for (int i = 0; i < max; i++, lnors++, tlnors++) { - normal_short_to_float_v3(*lnors, *tlnors); - } - } - - if (CustomData_has_layer(fdata, CD_MDISPS)) { - MDisps *ld = (MDisps *)CustomData_get(ldata, loopstart, CD_MDISPS); - const MDisps *fd = (const MDisps *)CustomData_get(fdata, findex, CD_MDISPS); - const float(*disps)[3] = fd->disps; - int tot = mf->v4 ? 4 : 3; - int corners; - - if (CustomData_external_test(fdata, CD_MDISPS)) { - if (id && fdata->external) { - CustomData_external_add(ldata, id, CD_MDISPS, totloop, fdata->external->filepath); - } - } - - corners = multires_mdisp_corners(fd); - - if (corners == 0) { - /* Empty #MDisp layers appear in at least one of the `sintel.blend` files. - * Not sure why this happens, but it seems fine to just ignore them here. - * If `corners == 0` for a non-empty layer though, something went wrong. */ - BLI_assert(fd->totdisp == 0); - } - else { - const int side = (int)sqrtf((float)(fd->totdisp / corners)); - const int side_sq = side * side; - - for (int i = 0; i < tot; i++, disps += side_sq, ld++) { - ld->totdisp = side_sq; - ld->level = (int)(logf((float)side - 1.0f) / (float)M_LN2) + 1; - - if (ld->disps) { - MEM_freeN(ld->disps); - } - - ld->disps = (float(*)[3])MEM_malloc_arrayN( - (size_t)side_sq, sizeof(float[3]), "converted loop mdisps"); - if (fd->disps) { - memcpy(ld->disps, disps, (size_t)side_sq * sizeof(float[3])); - } - else { - memset(ld->disps, 0, (size_t)side_sq * sizeof(float[3])); - } - } - } - } -} - -void BKE_mesh_convert_mfaces_to_mpolys(Mesh *mesh) -{ - BKE_mesh_convert_mfaces_to_mpolys_ex(&mesh->id, - &mesh->fdata, - &mesh->ldata, - &mesh->pdata, - mesh->totedge, - mesh->totface, - mesh->totloop, - mesh->totpoly, - mesh->medge, - mesh->mface, - &mesh->totloop, - &mesh->totpoly, - &mesh->mloop, - &mesh->mpoly); - - BKE_mesh_update_customdata_pointers(mesh, true); -} - -void BKE_mesh_do_versions_convert_mfaces_to_mpolys(Mesh *mesh) -{ - BKE_mesh_convert_mfaces_to_mpolys_ex(&mesh->id, - &mesh->fdata, - &mesh->ldata, - &mesh->pdata, - mesh->totedge, - mesh->totface, - mesh->totloop, - mesh->totpoly, - mesh->medge, - mesh->mface, - &mesh->totloop, - &mesh->totpoly, - &mesh->mloop, - &mesh->mpoly); - - CustomData_bmesh_do_versions_update_active_layers(&mesh->fdata, &mesh->ldata); - - BKE_mesh_update_customdata_pointers(mesh, true); -} - -void BKE_mesh_convert_mfaces_to_mpolys_ex(ID *id, - CustomData *fdata, - CustomData *ldata, - CustomData *pdata, - int totedge_i, - int totface_i, - int totloop_i, - int totpoly_i, - MEdge *medge, - MFace *mface, - int *r_totloop, - int *r_totpoly, - MLoop **r_mloop, - MPoly **r_mpoly) -{ - MFace *mf; - MLoop *ml, *mloop; - MPoly *mp, *mpoly; - MEdge *me; - EdgeHash *eh; - int numTex, numCol; - int i, j, totloop, totpoly, *polyindex; - - /* old flag, clear to allow for reuse */ -#define ME_FGON (1 << 3) - - /* just in case some of these layers are filled in (can happen with python created meshes) */ - CustomData_free(ldata, totloop_i); - CustomData_free(pdata, totpoly_i); - - totpoly = totface_i; - mpoly = (MPoly *)MEM_calloc_arrayN((size_t)totpoly, sizeof(MPoly), "mpoly converted"); - CustomData_add_layer(pdata, CD_MPOLY, CD_ASSIGN, mpoly, totpoly); - - numTex = CustomData_number_of_layers(fdata, CD_MTFACE); - numCol = CustomData_number_of_layers(fdata, CD_MCOL); - - totloop = 0; - mf = mface; - for (i = 0; i < totface_i; i++, mf++) { - totloop += mf->v4 ? 4 : 3; - } - - mloop = (MLoop *)MEM_calloc_arrayN((size_t)totloop, sizeof(MLoop), "mloop converted"); - - CustomData_add_layer(ldata, CD_MLOOP, CD_ASSIGN, mloop, totloop); - - CustomData_to_bmeshpoly(fdata, ldata, totloop); - - if (id) { - /* ensure external data is transferred */ - /* TODO(sergey): Use multiresModifier_ensure_external_read(). */ - CustomData_external_read(fdata, id, CD_MASK_MDISPS, totface_i); - } - - eh = BLI_edgehash_new_ex(__func__, (uint)totedge_i); - - /* build edge hash */ - me = medge; - for (i = 0; i < totedge_i; i++, me++) { - BLI_edgehash_insert(eh, me->v1, me->v2, POINTER_FROM_UINT(i)); - - /* unrelated but avoid having the FGON flag enabled, - * so we can reuse it later for something else */ - me->flag &= ~ME_FGON; - } - - polyindex = (int *)CustomData_get_layer(fdata, CD_ORIGINDEX); - - j = 0; /* current loop index */ - ml = mloop; - mf = mface; - mp = mpoly; - for (i = 0; i < totface_i; i++, mf++, mp++) { - mp->loopstart = j; - - mp->totloop = mf->v4 ? 4 : 3; - - mp->mat_nr = mf->mat_nr; - mp->flag = mf->flag; - -#define ML(v1, v2) \ - { \ - ml->v = mf->v1; \ - ml->e = POINTER_AS_UINT(BLI_edgehash_lookup(eh, mf->v1, mf->v2)); \ - ml++; \ - j++; \ - } \ - (void)0 - - ML(v1, v2); - ML(v2, v3); - if (mf->v4) { - ML(v3, v4); - ML(v4, v1); - } - else { - ML(v3, v1); - } - -#undef ML - - bm_corners_to_loops_ex(id, fdata, ldata, mface, totloop, i, mp->loopstart, numTex, numCol); - - if (polyindex) { - *polyindex = i; - polyindex++; - } - } - - /* NOTE: we don't convert NGons at all, these are not even real ngons, - * they have their own UV's, colors etc - its more an editing feature. */ - - BLI_edgehash_free(eh, nullptr); - - *r_totpoly = totpoly; - *r_totloop = totloop; - *r_mpoly = mpoly; - *r_mloop = mloop; - -#undef ME_FGON -} - -/** \} */ - void BKE_mesh_mdisp_flip(MDisps *md, const bool use_loop_mdisp_flip) { if (UNLIKELY(!md->totdisp || !md->disps)) { @@ -940,7 +668,7 @@ void BKE_mesh_mdisp_flip(MDisps *md, const bool use_loop_mdisp_flip) } } -void BKE_mesh_polygon_flip_ex(MPoly *mpoly, +void BKE_mesh_polygon_flip_ex(const MPoly *mpoly, MLoop *mloop, CustomData *ldata, float (*lnors)[3], @@ -983,16 +711,16 @@ void BKE_mesh_polygon_flip_ex(MPoly *mpoly, } } -void BKE_mesh_polygon_flip(MPoly *mpoly, MLoop *mloop, CustomData *ldata) +void BKE_mesh_polygon_flip(const MPoly *mpoly, MLoop *mloop, CustomData *ldata) { MDisps *mdisp = (MDisps *)CustomData_get_layer(ldata, CD_MDISPS); BKE_mesh_polygon_flip_ex(mpoly, mloop, ldata, nullptr, mdisp, true); } -void BKE_mesh_polygons_flip(MPoly *mpoly, MLoop *mloop, CustomData *ldata, int totpoly) +void BKE_mesh_polys_flip(const MPoly *mpoly, MLoop *mloop, CustomData *ldata, int totpoly) { MDisps *mdisp = (MDisps *)CustomData_get_layer(ldata, CD_MDISPS); - MPoly *mp; + const MPoly *mp; int i; for (mp = mpoly, i = 0; i < totpoly; mp++, i++) { @@ -1004,75 +732,90 @@ void BKE_mesh_polygons_flip(MPoly *mpoly, MLoop *mloop, CustomData *ldata, int t /** \name Mesh Flag Flushing * \{ */ -void BKE_mesh_flush_hidden_from_verts_ex(const MVert *mvert, - const MLoop *mloop, - MEdge *medge, - const int totedge, - MPoly *mpoly, - const int totpoly) +void BKE_mesh_flush_hidden_from_verts(Mesh *me) { - int i, j; + using namespace blender; + using namespace blender::bke; + MutableAttributeAccessor attributes = me->attributes_for_write(); - for (i = 0; i < totedge; i++) { - MEdge *e = &medge[i]; - if (mvert[e->v1].flag & ME_HIDE || mvert[e->v2].flag & ME_HIDE) { - e->flag |= ME_HIDE; - } - else { - e->flag &= ~ME_HIDE; - } + const VArray<bool> hide_vert = attributes.lookup_or_default<bool>( + ".hide_vert", ATTR_DOMAIN_POINT, false); + if (hide_vert.is_single() && !hide_vert.get_internal_single()) { + attributes.remove(".hide_edge"); + attributes.remove(".hide_poly"); + return; } - for (i = 0; i < totpoly; i++) { - MPoly *p = &mpoly[i]; - p->flag &= (char)~ME_HIDE; - for (j = 0; j < p->totloop; j++) { - if (mvert[mloop[p->loopstart + j].v].flag & ME_HIDE) { - p->flag |= ME_HIDE; - } - } + const VArraySpan<bool> hide_vert_span{hide_vert}; + const Span<MEdge> edges = me->edges(); + const Span<MPoly> polys = me->polys(); + const Span<MLoop> loops = me->loops(); + + /* Hide edges when either of their vertices are hidden. */ + SpanAttributeWriter<bool> hide_edge = attributes.lookup_or_add_for_write_only_span<bool>( + ".hide_edge", ATTR_DOMAIN_EDGE); + for (const int i : edges.index_range()) { + const MEdge &edge = edges[i]; + hide_edge.span[i] = hide_vert_span[edge.v1] || hide_vert_span[edge.v2]; } -} -void BKE_mesh_flush_hidden_from_verts(Mesh *me) -{ - BKE_mesh_flush_hidden_from_verts_ex( - me->mvert, me->mloop, me->medge, me->totedge, me->mpoly, me->totpoly); + hide_edge.finish(); + + /* Hide polygons when any of their vertices are hidden. */ + SpanAttributeWriter<bool> hide_poly = attributes.lookup_or_add_for_write_only_span<bool>( + ".hide_poly", ATTR_DOMAIN_FACE); + for (const int i : polys.index_range()) { + const MPoly &poly = polys[i]; + const Span<MLoop> poly_loops = loops.slice(poly.loopstart, poly.totloop); + hide_poly.span[i] = std::any_of(poly_loops.begin(), poly_loops.end(), [&](const MLoop &loop) { + return hide_vert_span[loop.v]; + }); + } + hide_poly.finish(); } -void BKE_mesh_flush_hidden_from_polys_ex(MVert *mvert, - const MLoop *mloop, - MEdge *medge, - const int UNUSED(totedge), - const MPoly *mpoly, - const int totpoly) +void BKE_mesh_flush_hidden_from_polys(Mesh *me) { - int i = totpoly; - for (const MPoly *mp = mpoly; i--; mp++) { - if (mp->flag & ME_HIDE) { - const MLoop *ml; - int j = mp->totloop; - for (ml = &mloop[mp->loopstart]; j--; ml++) { - mvert[ml->v].flag |= ME_HIDE; - medge[ml->e].flag |= ME_HIDE; + using namespace blender; + using namespace blender::bke; + MutableAttributeAccessor attributes = me->attributes_for_write(); + + const VArray<bool> hide_poly = attributes.lookup_or_default<bool>( + ".hide_poly", ATTR_DOMAIN_FACE, false); + if (hide_poly.is_single() && !hide_poly.get_internal_single()) { + attributes.remove(".hide_vert"); + attributes.remove(".hide_edge"); + return; + } + const VArraySpan<bool> hide_poly_span{hide_poly}; + const Span<MPoly> polys = me->polys(); + const Span<MLoop> loops = me->loops(); + SpanAttributeWriter<bool> hide_vert = attributes.lookup_or_add_for_write_only_span<bool>( + ".hide_vert", ATTR_DOMAIN_POINT); + SpanAttributeWriter<bool> hide_edge = attributes.lookup_or_add_for_write_only_span<bool>( + ".hide_edge", ATTR_DOMAIN_EDGE); + + /* Hide all edges or vertices connected to hidden polygons. */ + for (const int i : polys.index_range()) { + if (hide_poly_span[i]) { + const MPoly &poly = polys[i]; + for (const MLoop &loop : loops.slice(poly.loopstart, poly.totloop)) { + hide_vert.span[loop.v] = true; + hide_edge.span[loop.e] = true; } } } - - i = totpoly; - for (const MPoly *mp = mpoly; i--; mp++) { - if ((mp->flag & ME_HIDE) == 0) { - const MLoop *ml; - int j = mp->totloop; - for (ml = &mloop[mp->loopstart]; j--; ml++) { - mvert[ml->v].flag &= (char)~ME_HIDE; - medge[ml->e].flag &= (short)~ME_HIDE; + /* Unhide vertices and edges connected to visible polygons. */ + for (const int i : polys.index_range()) { + if (!hide_poly_span[i]) { + const MPoly &poly = polys[i]; + for (const MLoop &loop : loops.slice(poly.loopstart, poly.totloop)) { + hide_vert.span[loop.v] = false; + hide_edge.span[loop.e] = false; } } } -} -void BKE_mesh_flush_hidden_from_polys(Mesh *me) -{ - BKE_mesh_flush_hidden_from_polys_ex( - me->mvert, me->mloop, me->medge, me->totedge, me->mpoly, me->totpoly); + + hide_vert.finish(); + hide_edge.finish(); } void BKE_mesh_flush_select_from_polys_ex(MVert *mvert, @@ -1114,17 +857,24 @@ void BKE_mesh_flush_select_from_polys_ex(MVert *mvert, } void BKE_mesh_flush_select_from_polys(Mesh *me) { - BKE_mesh_flush_select_from_polys_ex( - me->mvert, me->totvert, me->mloop, me->medge, me->totedge, me->mpoly, me->totpoly); + BKE_mesh_flush_select_from_polys_ex(me->verts_for_write().data(), + me->totvert, + me->loops().data(), + me->edges_for_write().data(), + me->totedge, + me->polys().data(), + me->totpoly); } static void mesh_flush_select_from_verts(const Span<MVert> verts, const Span<MLoop> loops, + const VArray<bool> &hide_edge, + const VArray<bool> &hide_poly, MutableSpan<MEdge> edges, MutableSpan<MPoly> polys) { for (const int i : edges.index_range()) { - if ((edges[i].flag & ME_HIDE) == 0) { + if (!hide_edge[i]) { MEdge &edge = edges[i]; if ((verts[edge.v1].flag & SELECT) && (verts[edge.v2].flag & SELECT)) { edge.flag |= SELECT; @@ -1136,7 +886,7 @@ static void mesh_flush_select_from_verts(const Span<MVert> verts, } for (const int i : polys.index_range()) { - if (polys[i].flag & ME_HIDE) { + if (hide_poly[i]) { continue; } MPoly &poly = polys[i]; @@ -1157,10 +907,14 @@ static void mesh_flush_select_from_verts(const Span<MVert> verts, void BKE_mesh_flush_select_from_verts(Mesh *me) { - mesh_flush_select_from_verts({me->mvert, me->totvert}, - {me->mloop, me->totloop}, - {me->medge, me->totedge}, - {me->mpoly, me->totpoly}); + const blender::bke::AttributeAccessor attributes = me->attributes(); + mesh_flush_select_from_verts( + me->verts(), + me->loops(), + attributes.lookup_or_default<bool>(".hide_edge", ATTR_DOMAIN_EDGE, false), + attributes.lookup_or_default<bool>(".hide_poly", ATTR_DOMAIN_FACE, false), + me->edges_for_write(), + me->polys_for_write()); } /** \} */ |