diff options
author | Hans Goudey <h.goudey@me.com> | 2022-07-08 06:33:57 +0300 |
---|---|---|
committer | Hans Goudey <h.goudey@me.com> | 2022-07-08 06:33:57 +0300 |
commit | c4b32f1b291a3c3447879175225a3664aeb0b7ef (patch) | |
tree | b575e57a45e30e59ae469f815857f8b9e6f1acd3 /source/blender/blenkernel/intern/mesh_evaluate.cc | |
parent | b98a937db64c365de889adcc084248716607521d (diff) |
Cleanup: Move mesh legacy conversion to a separate file
It's helpful to make the separation of legacy data formats explicit,
because it declutters actively changed code and makes it clear which
areas do not follow Blender's current design. In this case I separated
the `MFace`/"tessface" conversion code into a separate blenkernel
.cc file and header. This also makes refactoring to remove these
functions simpler because they're easier to find.
In the future, conversions to the `MLoopUV` type and `MVert`
can be implemented here for the same reasons (see T95965).
Differential Revision: https://developer.blender.org/D15396
Diffstat (limited to 'source/blender/blenkernel/intern/mesh_evaluate.cc')
-rw-r--r-- | source/blender/blenkernel/intern/mesh_evaluate.cc | 272 |
1 files changed, 0 insertions, 272 deletions
diff --git a/source/blender/blenkernel/intern/mesh_evaluate.cc b/source/blender/blenkernel/intern/mesh_evaluate.cc index de0489d668f..7d26262a504 100644 --- a/source/blender/blenkernel/intern/mesh_evaluate.cc +++ b/source/blender/blenkernel/intern/mesh_evaluate.cc @@ -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)) { |