diff options
author | Colin Basnett <cmbasnett@gmail.com> | 2022-09-17 03:50:37 +0300 |
---|---|---|
committer | Colin Basnett <cmbasnett@gmail.com> | 2022-09-17 03:50:37 +0300 |
commit | 564bda241a973396da51d7c3ccd9efd97d51728a (patch) | |
tree | 4d9aa4cccd82e2e606b47774afa5e1015bf14768 /source/blender/bmesh/intern | |
parent | 0fff238150d076576053c25b646f653d6e3b0edb (diff) | |
parent | 48d7ff68f0df209c77bbb081ab46fbc109fd825a (diff) |
Merge branch 'master' into feature-imformatfeature-imformat
Diffstat (limited to 'source/blender/bmesh/intern')
-rw-r--r-- | source/blender/bmesh/intern/bmesh_construct.c | 46 | ||||
-rw-r--r-- | source/blender/bmesh/intern/bmesh_delete.c | 7 | ||||
-rw-r--r-- | source/blender/bmesh/intern/bmesh_interp.c | 4 | ||||
-rw-r--r-- | source/blender/bmesh/intern/bmesh_log.c | 32 | ||||
-rw-r--r-- | source/blender/bmesh/intern/bmesh_log.h | 113 | ||||
-rw-r--r-- | source/blender/bmesh/intern/bmesh_mesh.cc (renamed from source/blender/bmesh/intern/bmesh_mesh.c) | 211 | ||||
-rw-r--r-- | source/blender/bmesh/intern/bmesh_mesh.h | 2 | ||||
-rw-r--r-- | source/blender/bmesh/intern/bmesh_mesh_convert.cc | 337 | ||||
-rw-r--r-- | source/blender/bmesh/intern/bmesh_mesh_convert.h | 2 | ||||
-rw-r--r-- | source/blender/bmesh/intern/bmesh_mesh_tessellate.c | 2 | ||||
-rw-r--r-- | source/blender/bmesh/intern/bmesh_query.h | 1 | ||||
-rw-r--r-- | source/blender/bmesh/intern/bmesh_query_uv.cc (renamed from source/blender/bmesh/intern/bmesh_query_uv.c) | 44 | ||||
-rw-r--r-- | source/blender/bmesh/intern/bmesh_structure.c | 3 |
13 files changed, 453 insertions, 351 deletions
diff --git a/source/blender/bmesh/intern/bmesh_construct.c b/source/blender/bmesh/intern/bmesh_construct.c index a7637d2712c..757d006b04d 100644 --- a/source/blender/bmesh/intern/bmesh_construct.c +++ b/source/blender/bmesh/intern/bmesh_construct.c @@ -512,16 +512,24 @@ void BM_mesh_copy_init_customdata_from_mesh_array(BMesh *bm_dst, for (int i = 0; i < me_src_array_len; i++) { const Mesh *me_src = me_src_array[i]; if (i == 0) { - CustomData_copy(&me_src->vdata, &bm_dst->vdata, CD_MASK_BMESH.vmask, CD_CALLOC, 0); - CustomData_copy(&me_src->edata, &bm_dst->edata, CD_MASK_BMESH.emask, CD_CALLOC, 0); - CustomData_copy(&me_src->ldata, &bm_dst->ldata, CD_MASK_BMESH.lmask, CD_CALLOC, 0); - CustomData_copy(&me_src->pdata, &bm_dst->pdata, CD_MASK_BMESH.pmask, CD_CALLOC, 0); + CustomData_copy_mesh_to_bmesh( + &me_src->vdata, &bm_dst->vdata, CD_MASK_BMESH.vmask, CD_SET_DEFAULT, 0); + CustomData_copy_mesh_to_bmesh( + &me_src->edata, &bm_dst->edata, CD_MASK_BMESH.emask, CD_SET_DEFAULT, 0); + CustomData_copy_mesh_to_bmesh( + &me_src->ldata, &bm_dst->ldata, CD_MASK_BMESH.lmask, CD_SET_DEFAULT, 0); + CustomData_copy_mesh_to_bmesh( + &me_src->pdata, &bm_dst->pdata, CD_MASK_BMESH.pmask, CD_SET_DEFAULT, 0); } else { - CustomData_merge(&me_src->vdata, &bm_dst->vdata, CD_MASK_BMESH.vmask, CD_CALLOC, 0); - CustomData_merge(&me_src->edata, &bm_dst->edata, CD_MASK_BMESH.emask, CD_CALLOC, 0); - CustomData_merge(&me_src->ldata, &bm_dst->ldata, CD_MASK_BMESH.lmask, CD_CALLOC, 0); - CustomData_merge(&me_src->pdata, &bm_dst->pdata, CD_MASK_BMESH.pmask, CD_CALLOC, 0); + CustomData_merge_mesh_to_bmesh( + &me_src->vdata, &bm_dst->vdata, CD_MASK_BMESH.vmask, CD_SET_DEFAULT, 0); + CustomData_merge_mesh_to_bmesh( + &me_src->edata, &bm_dst->edata, CD_MASK_BMESH.emask, CD_SET_DEFAULT, 0); + CustomData_merge_mesh_to_bmesh( + &me_src->ldata, &bm_dst->ldata, CD_MASK_BMESH.lmask, CD_SET_DEFAULT, 0); + CustomData_merge_mesh_to_bmesh( + &me_src->pdata, &bm_dst->pdata, CD_MASK_BMESH.pmask, CD_SET_DEFAULT, 0); } cd_flag |= me_src->cd_flag; @@ -550,10 +558,10 @@ void BM_mesh_copy_init_customdata(BMesh *bm_dst, BMesh *bm_src, const BMAllocTem allocsize = &bm_mesh_allocsize_default; } - CustomData_copy(&bm_src->vdata, &bm_dst->vdata, CD_MASK_BMESH.vmask, CD_CALLOC, 0); - CustomData_copy(&bm_src->edata, &bm_dst->edata, CD_MASK_BMESH.emask, CD_CALLOC, 0); - CustomData_copy(&bm_src->ldata, &bm_dst->ldata, CD_MASK_BMESH.lmask, CD_CALLOC, 0); - CustomData_copy(&bm_src->pdata, &bm_dst->pdata, CD_MASK_BMESH.pmask, CD_CALLOC, 0); + CustomData_copy(&bm_src->vdata, &bm_dst->vdata, CD_MASK_BMESH.vmask, CD_SET_DEFAULT, 0); + CustomData_copy(&bm_src->edata, &bm_dst->edata, CD_MASK_BMESH.emask, CD_SET_DEFAULT, 0); + CustomData_copy(&bm_src->ldata, &bm_dst->ldata, CD_MASK_BMESH.lmask, CD_SET_DEFAULT, 0); + CustomData_copy(&bm_src->pdata, &bm_dst->pdata, CD_MASK_BMESH.pmask, CD_SET_DEFAULT, 0); CustomData_bmesh_init_pool(&bm_dst->vdata, allocsize->totvert, BM_VERT); CustomData_bmesh_init_pool(&bm_dst->edata, allocsize->totedge, BM_EDGE); @@ -588,7 +596,7 @@ void BM_mesh_copy_init_customdata_all_layers(BMesh *bm_dst, for (int l = 0; l < src->totlayer; l++) { CustomData_add_layer_named( - dst, src->layers[l].type, CD_CALLOC, NULL, 0, src->layers[l].name); + dst, src->layers[l].type, CD_SET_DEFAULT, NULL, 0, src->layers[l].name); } CustomData_bmesh_init_pool(dst, size, htypes[i]); } @@ -714,26 +722,25 @@ BMesh *BM_mesh_copy(BMesh *bm_old) char BM_vert_flag_from_mflag(const char mflag) { - return (((mflag & SELECT) ? BM_ELEM_SELECT : 0) | ((mflag & ME_HIDE) ? BM_ELEM_HIDDEN : 0)); + return ((mflag & SELECT) ? BM_ELEM_SELECT : 0); } char BM_edge_flag_from_mflag(const short mflag) { return (((mflag & SELECT) ? BM_ELEM_SELECT : 0) | ((mflag & ME_SEAM) ? BM_ELEM_SEAM : 0) | ((mflag & ME_EDGEDRAW) ? BM_ELEM_DRAW : 0) | - ((mflag & ME_SHARP) == 0 ? BM_ELEM_SMOOTH : 0) | /* invert */ - ((mflag & ME_HIDE) ? BM_ELEM_HIDDEN : 0)); + ((mflag & ME_SHARP) == 0 ? BM_ELEM_SMOOTH : 0)); } char BM_face_flag_from_mflag(const char mflag) { return (((mflag & ME_FACE_SEL) ? BM_ELEM_SELECT : 0) | - ((mflag & ME_SMOOTH) ? BM_ELEM_SMOOTH : 0) | ((mflag & ME_HIDE) ? BM_ELEM_HIDDEN : 0)); + ((mflag & ME_SMOOTH) ? BM_ELEM_SMOOTH : 0)); } char BM_vert_flag_to_mflag(BMVert *v) { const char hflag = v->head.hflag; - return (((hflag & BM_ELEM_SELECT) ? SELECT : 0) | ((hflag & BM_ELEM_HIDDEN) ? ME_HIDE : 0)); + return (((hflag & BM_ELEM_SELECT) ? SELECT : 0)); } short BM_edge_flag_to_mflag(BMEdge *e) @@ -743,7 +750,6 @@ short BM_edge_flag_to_mflag(BMEdge *e) return (((hflag & BM_ELEM_SELECT) ? SELECT : 0) | ((hflag & BM_ELEM_SEAM) ? ME_SEAM : 0) | ((hflag & BM_ELEM_DRAW) ? ME_EDGEDRAW : 0) | ((hflag & BM_ELEM_SMOOTH) == 0 ? ME_SHARP : 0) | - ((hflag & BM_ELEM_HIDDEN) ? ME_HIDE : 0) | (BM_edge_is_wire(e) ? ME_LOOSEEDGE : 0) | /* not typical */ ME_EDGERENDER); } @@ -752,5 +758,5 @@ char BM_face_flag_to_mflag(BMFace *f) const char hflag = f->head.hflag; return (((hflag & BM_ELEM_SELECT) ? ME_FACE_SEL : 0) | - ((hflag & BM_ELEM_SMOOTH) ? ME_SMOOTH : 0) | ((hflag & BM_ELEM_HIDDEN) ? ME_HIDE : 0)); + ((hflag & BM_ELEM_SMOOTH) ? ME_SMOOTH : 0)); } diff --git a/source/blender/bmesh/intern/bmesh_delete.c b/source/blender/bmesh/intern/bmesh_delete.c index e2436e53099..8ec7dc410d0 100644 --- a/source/blender/bmesh/intern/bmesh_delete.c +++ b/source/blender/bmesh/intern/bmesh_delete.c @@ -86,7 +86,6 @@ void BMO_mesh_delete_oflag_tagged(BMesh *bm, const short oflag, const char htype void BMO_mesh_delete_oflag_context(BMesh *bm, const short oflag, const int type) { BMEdge *e; - BMFace *f; BMIter eiter; BMIter fiter; @@ -128,6 +127,7 @@ void BMO_mesh_delete_oflag_context(BMesh *bm, const short oflag, const int type) case DEL_FACES: case DEL_FACES_KEEP_BOUNDARY: { /* go through and mark all edges and all verts of all faces for delete */ + BMFace *f; BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) { if (BMO_face_flag_test(bm, f, oflag)) { BMLoop *l_first = BM_FACE_FIRST_LOOP(f); @@ -257,8 +257,6 @@ void BM_mesh_delete_hflag_tagged(BMesh *bm, const char hflag, const char htype) void BM_mesh_delete_hflag_context(BMesh *bm, const char hflag, const int type) { - BMEdge *e; - BMFace *f; BMIter eiter; BMIter fiter; @@ -271,6 +269,7 @@ void BM_mesh_delete_hflag_context(BMesh *bm, const char hflag, const int type) } case DEL_EDGES: { /* flush down to vert */ + BMEdge *e; BM_ITER_MESH (e, &eiter, bm, BM_EDGES_OF_MESH) { if (BM_elem_flag_test(e, hflag)) { BM_elem_flag_enable(e->v1, hflag); @@ -299,6 +298,8 @@ void BM_mesh_delete_hflag_context(BMesh *bm, const char hflag, const int type) } case DEL_FACES: { /* go through and mark all edges and all verts of all faces for delete */ + BMFace *f; + BMEdge *e; BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) { if (BM_elem_flag_test(f, hflag)) { BMLoop *l_first = BM_FACE_FIRST_LOOP(f); diff --git a/source/blender/bmesh/intern/bmesh_interp.c b/source/blender/bmesh/intern/bmesh_interp.c index 0c3db31dd1f..b7028dee5e1 100644 --- a/source/blender/bmesh/intern/bmesh_interp.c +++ b/source/blender/bmesh/intern/bmesh_interp.c @@ -846,7 +846,7 @@ void BM_data_layer_add(BMesh *bm, CustomData *data, int type) /* the pool is now owned by olddata and must not be shared */ data->pool = NULL; - CustomData_add_layer(data, type, CD_DEFAULT, NULL, 0); + CustomData_add_layer(data, type, CD_SET_DEFAULT, NULL, 0); update_data_blocks(bm, &olddata, data); if (olddata.layers) { @@ -864,7 +864,7 @@ void BM_data_layer_add_named(BMesh *bm, CustomData *data, int type, const char * /* the pool is now owned by olddata and must not be shared */ data->pool = NULL; - CustomData_add_layer_named(data, type, CD_DEFAULT, NULL, 0, name); + CustomData_add_layer_named(data, type, CD_SET_DEFAULT, NULL, 0, name); update_data_blocks(bm, &olddata, data); if (olddata.layers) { diff --git a/source/blender/bmesh/intern/bmesh_log.c b/source/blender/bmesh/intern/bmesh_log.c index a81ae934629..64e6c63e9f0 100644 --- a/source/blender/bmesh/intern/bmesh_log.c +++ b/source/blender/bmesh/intern/bmesh_log.c @@ -34,42 +34,41 @@ struct BMLogEntry { struct BMLogEntry *next, *prev; - /* The following GHashes map from an element ID to one of the log - * types above */ + /* The following #GHash members map from an element ID to one of the log types above. */ - /* Elements that were in the previous entry, but have been - * deleted */ + /** Elements that were in the previous entry, but have been deleted. */ GHash *deleted_verts; GHash *deleted_faces; - /* Elements that were not in the previous entry, but are in the - * result of this entry */ + /** Elements that were not in the previous entry, but are in the result of this entry. */ GHash *added_verts; GHash *added_faces; - /* Vertices whose coordinates, mask value, or hflag have changed */ + /** Vertices whose coordinates, mask value, or hflag have changed. */ GHash *modified_verts; GHash *modified_faces; BLI_mempool *pool_verts; BLI_mempool *pool_faces; - /* This is only needed for dropping BMLogEntries while still in + /** + * This is only needed for dropping BMLogEntries while still in * dynamic-topology mode, as that should release vert/face IDs - * back to the BMLog but no BMLog pointer is available at that - * time. + * back to the BMLog but no BMLog pointer is available at that time. * * This field is not guaranteed to be valid, any use of it should - * check for NULL. */ + * check for NULL. + */ BMLog *log; }; struct BMLog { - /* Tree of free IDs */ + /** Tree of free IDs */ struct RangeTreeUInt *unused_ids; - /* Mapping from unique IDs to vertices and faces + /** + * Mapping from unique IDs to vertices and faces * - * Each vertex and face in the log gets a unique uinteger + * Each vertex and face in the log gets a unique `uint` * assigned. That ID is taken from the set managed by the * unused_ids range tree. * @@ -79,10 +78,11 @@ struct BMLog { GHash *id_to_elem; GHash *elem_to_id; - /* All BMLogEntrys, ordered from earliest to most recent */ + /** All #BMLogEntrys, ordered from earliest to most recent. */ ListBase entries; - /* The current log entry from entries list + /** + * The current log entry from entries list * * If null, then the original mesh from before any of the log * entries is current (i.e. there is nothing left to undo.) diff --git a/source/blender/bmesh/intern/bmesh_log.h b/source/blender/bmesh/intern/bmesh_log.h index 189aa97509f..5daa5dd9a68 100644 --- a/source/blender/bmesh/intern/bmesh_log.h +++ b/source/blender/bmesh/intern/bmesh_log.h @@ -14,12 +14,13 @@ struct RangeTreeUInt; typedef struct BMLog BMLog; typedef struct BMLogEntry BMLogEntry; -/* Allocate and initialize a new BMLog */ -/* Allocate, initialize, and assign a new BMLog */ +/** + * Allocate, initialize, and assign a new BMLog. + */ BMLog *BM_log_create(BMesh *bm); -/* Allocate and initialize a new BMLog using existing BMLogEntries */ -/* Allocate and initialize a new BMLog using existing BMLogEntries +/** + * Allocate and initialize a new #BMLog using existing #BMLogEntries * * The 'entry' should be the last entry in the BMLog. Its prev pointer * will be followed back to find the first entry. @@ -29,20 +30,21 @@ BMLog *BM_log_create(BMesh *bm); */ BMLog *BM_log_from_existing_entries_create(BMesh *bm, BMLogEntry *entry); -/* Free all the data in a BMLog including the log itself */ -/* Free all the data in a BMLog including the log itself */ +/** + * Free all the data in a BMLog including the log itself. + */ void BM_log_free(BMLog *log); -/* Get the number of log entries */ -/* Get the number of log entries */ +/** + * Get the number of log entries. + */ int BM_log_length(const BMLog *log); -/* Apply a consistent ordering to BMesh vertices and faces */ -/* Apply a consistent ordering to BMesh vertices */ +/** Apply a consistent ordering to BMesh vertices and faces. */ void BM_log_mesh_elems_reorder(BMesh *bm, BMLog *log); -/* Start a new log entry and update the log entry list */ -/* Start a new log entry and update the log entry list +/** + * Start a new log entry and update the log entry list. * * If the log entry list is empty, or if the current log entry is the * last entry, the new entry is simply appended to the end. @@ -54,35 +56,36 @@ void BM_log_mesh_elems_reorder(BMesh *bm, BMLog *log); */ BMLogEntry *BM_log_entry_add(BMLog *log); -/* Mark all used ids as unused for this node */ +/** Mark all used ids as unused for this node */ void BM_log_cleanup_entry(BMLogEntry *entry); -/* Remove an entry from the log */ -/* Remove an entry from the log +/** + * Remove an entry from the log. * * Uses entry->log as the log. If the log is NULL, the entry will be - * free'd but not removed from any list, nor shall its IDs be - * released. + * free'd but not removed from any list, nor shall its IDs be released. * * This operation is only valid on the first and last entries in the * log. Deleting from the middle will assert. */ void BM_log_entry_drop(BMLogEntry *entry); -/* Undo one BMLogEntry */ -/* Undo one BMLogEntry +/** + * Undo one #BMLogEntry. * - * Has no effect if there's nothing left to undo */ + * Has no effect if there's nothing left to undo. + */ void BM_log_undo(BMesh *bm, BMLog *log); -/* Redo one BMLogEntry */ -/* Redo one BMLogEntry +/** + * Redo one #BMLogEntry. * - * Has no effect if there's nothing left to redo */ + * Has no effect if there's nothing left to redo. + */ void BM_log_redo(BMesh *bm, BMLog *log); -/* Log a vertex before it is modified */ -/* Log a vertex before it is modified +/** + * Log a vertex before it is modified. * * Before modifying vertex coordinates, masks, or hflags, call this * function to log its current values. This is better than logging @@ -107,8 +110,8 @@ void BM_log_redo(BMesh *bm, BMLog *log); */ void BM_log_vert_before_modified(BMLog *log, struct BMVert *v, int cd_vert_mask_offset); -/* Log a new vertex as added to the BMesh */ -/* Log a new vertex as added to the BMesh +/** + * Log a new vertex as added to the #BMesh. * * The new vertex gets a unique ID assigned. It is then added to a map * of added vertices, with the key being its ID and the value @@ -116,16 +119,16 @@ void BM_log_vert_before_modified(BMLog *log, struct BMVert *v, int cd_vert_mask_ */ void BM_log_vert_added(BMLog *log, struct BMVert *v, int cd_vert_mask_offset); -/* Log a face before it is modified */ -/* Log a face before it is modified +/** + * Log a face before it is modified. * * This is intended to handle only header flags and we always - * assume face has been added before + * assume face has been added before. */ void BM_log_face_modified(BMLog *log, struct BMFace *f); -/* Log a new face as added to the BMesh */ -/* Log a new face as added to the BMesh +/** + * Log a new face as added to the #BMesh. * * The new face gets a unique ID assigned. It is then added to a map * of added faces, with the key being its ID and the value containing @@ -133,8 +136,8 @@ void BM_log_face_modified(BMLog *log, struct BMFace *f); */ void BM_log_face_added(BMLog *log, struct BMFace *f); -/* Log a vertex as removed from the BMesh */ -/* Log a vertex as removed from the BMesh +/** + * Log a vertex as removed from the #BMesh. * * A couple things can happen here: * @@ -142,7 +145,7 @@ void BM_log_face_added(BMLog *log, struct BMFace *f); * deleted and forgotten about entirely. Its unique ID is returned to * the unused pool. * - * If the vertex was already part of the BMesh before the current log + * If the vertex was already part of the #BMesh before the current log * entry, it is added to a map of deleted vertices, with the key being * its ID and the value containing everything needed to reconstruct * that vertex. @@ -152,8 +155,8 @@ void BM_log_face_added(BMLog *log, struct BMFace *f); */ void BM_log_vert_removed(BMLog *log, struct BMVert *v, int cd_vert_mask_offset); -/* Log a face as removed from the BMesh */ -/* Log a face as removed from the BMesh +/** + * Log a face as removed from the #BMesh. * * A couple things can happen here: * @@ -161,43 +164,45 @@ void BM_log_vert_removed(BMLog *log, struct BMVert *v, int cd_vert_mask_offset); * deleted and forgotten about entirely. Its unique ID is returned to * the unused pool. * - * If the face was already part of the BMesh before the current log + * If the face was already part of the #BMesh before the current log * entry, it is added to a map of deleted faces, with the key being * its ID and the value containing everything needed to reconstruct * that face. */ void BM_log_face_removed(BMLog *log, struct BMFace *f); -/* Log all vertices/faces in the BMesh as added */ -/* Log all vertices/faces in the BMesh as added */ +/** + * Log all vertices/faces in the #BMesh as added. + */ void BM_log_all_added(BMesh *bm, BMLog *log); -/* Log all vertices/faces in the BMesh as removed */ -/* Log all vertices/faces in the BMesh as removed */ +/** Log all vertices/faces in the #BMesh as removed. */ void BM_log_before_all_removed(BMesh *bm, BMLog *log); -/* Get the logged coordinates of a vertex */ -/* Get the logged coordinates of a vertex +/** + * Get the logged coordinates of a vertex. * - * Does not modify the log or the vertex */ + * Does not modify the log or the vertex. + */ const float *BM_log_original_vert_co(BMLog *log, BMVert *v); -/* Get the logged normal of a vertex +/** + * Get the logged normal of a vertex * - * Does not modify the log or the vertex */ + * Does not modify the log or the vertex. + */ const float *BM_log_original_vert_no(BMLog *log, BMVert *v); -/* Get the logged mask of a vertex */ -/* Get the logged mask of a vertex +/** Get the logged mask of a vertex * - * Does not modify the log or the vertex */ + * Does not modify the log or the vertex. + */ float BM_log_original_mask(BMLog *log, BMVert *v); -/* Get the logged data of a vertex (avoid multiple lookups) */ +/** Get the logged data of a vertex (avoid multiple lookups). */ void BM_log_original_vert_data(BMLog *log, BMVert *v, const float **r_co, const float **r_no); -/* For internal use only (unit testing) */ -/* For internal use only (unit testing) */ +/** For internal use only (unit testing). */ BMLogEntry *BM_log_current_entry(BMLog *log); -/* For internal use only (unit testing) */ +/** For internal use only (unit testing) */ struct RangeTreeUInt *BM_log_unused_ids(BMLog *log); diff --git a/source/blender/bmesh/intern/bmesh_mesh.c b/source/blender/bmesh/intern/bmesh_mesh.cc index 7dba854b9ef..4f42ce4a470 100644 --- a/source/blender/bmesh/intern/bmesh_mesh.c +++ b/source/blender/bmesh/intern/bmesh_mesh.cc @@ -88,19 +88,19 @@ void BM_mesh_elem_toolflags_ensure(BMesh *bm) BMVert_OFlag *v_olfag; BLI_mempool *toolflagpool = bm->vtoolflagpool; BM_ITER_MESH (v_olfag, &iter, bm, BM_VERTS_OF_MESH) { - v_olfag->oflags = BLI_mempool_calloc(toolflagpool); + v_olfag->oflags = static_cast<BMFlagLayer *>(BLI_mempool_calloc(toolflagpool)); } BMEdge_OFlag *e_olfag; toolflagpool = bm->etoolflagpool; BM_ITER_MESH (e_olfag, &iter, bm, BM_EDGES_OF_MESH) { - e_olfag->oflags = BLI_mempool_calloc(toolflagpool); + e_olfag->oflags = static_cast<BMFlagLayer *>(BLI_mempool_calloc(toolflagpool)); } BMFace_OFlag *f_olfag; toolflagpool = bm->ftoolflagpool; BM_ITER_MESH (f_olfag, &iter, bm, BM_FACES_OF_MESH) { - f_olfag->oflags = BLI_mempool_calloc(toolflagpool); + f_olfag->oflags = static_cast<BMFlagLayer *>(BLI_mempool_calloc(toolflagpool)); } bm->totflags = 1; @@ -110,22 +110,22 @@ void BM_mesh_elem_toolflags_clear(BMesh *bm) { if (bm->vtoolflagpool) { BLI_mempool_destroy(bm->vtoolflagpool); - bm->vtoolflagpool = NULL; + bm->vtoolflagpool = nullptr; } if (bm->etoolflagpool) { BLI_mempool_destroy(bm->etoolflagpool); - bm->etoolflagpool = NULL; + bm->etoolflagpool = nullptr; } if (bm->ftoolflagpool) { BLI_mempool_destroy(bm->ftoolflagpool); - bm->ftoolflagpool = NULL; + bm->ftoolflagpool = nullptr; } } BMesh *BM_mesh_create(const BMAllocTemplate *allocsize, const struct BMeshCreateParams *params) { /* allocate the structure */ - BMesh *bm = MEM_callocN(sizeof(BMesh), __func__); + BMesh *bm = static_cast<BMesh *>(MEM_callocN(sizeof(BMesh), __func__)); /* allocate the memory pools for the mesh elements */ bm_mempool_init(bm, allocsize, params->use_toolflags); @@ -262,8 +262,8 @@ void BM_mesh_free(BMesh *bm) if (bm->py_handle) { /* keep this out of 'BM_mesh_data_free' because we want python * to be able to clear the mesh and maintain access. */ - bpy_bm_generic_invalidate(bm->py_handle); - bm->py_handle = NULL; + bpy_bm_generic_invalidate(static_cast<BPy_BMGeneric *>(bm->py_handle)); + bm->py_handle = nullptr; } MEM_freeN(bm); @@ -336,7 +336,7 @@ void BM_mesh_elem_index_ensure_ex(BMesh *bm, const char htype, int elem_offset[4 BM_ELEM_INDEX_VALIDATE(bm, "Should Never Fail!", __func__); #endif - if (elem_offset == NULL) { + if (elem_offset == nullptr) { /* Simple case. */ const char htype_needed = bm->elem_index_dirty & htype; if (htype_needed == 0) { @@ -445,7 +445,7 @@ finally: void BM_mesh_elem_index_ensure(BMesh *bm, const char htype) { - BM_mesh_elem_index_ensure_ex(bm, htype, NULL); + BM_mesh_elem_index_ensure_ex(bm, htype, nullptr); } void BM_mesh_elem_index_validate( @@ -459,7 +459,7 @@ void BM_mesh_elem_index_validate( BMIter iter; BMElem *ele; int i; - bool is_any_error = 0; + bool is_any_error = false; for (i = 0; i < 3; i++) { const bool is_dirty = (flag_types[i] & bm->elem_index_dirty) != 0; @@ -581,9 +581,11 @@ void BM_mesh_elem_table_ensure(BMesh *bm, const char htype) if (bm->vtable) { MEM_freeN(bm->vtable); } - bm->vtable = MEM_mallocN(sizeof(void **) * bm->totvert, "bm->vtable"); + bm->vtable = static_cast<BMVert **>( + MEM_mallocN(sizeof(void **) * bm->totvert, "bm->vtable")); bm->vtable_tot = bm->totvert; } + BM_iter_as_array(bm, BM_VERTS_OF_MESH, nullptr, (void **)bm->vtable, bm->totvert); } if (htype_needed & BM_EDGE) { if (bm->etable && bm->totedge <= bm->etable_tot && bm->totedge * 2 >= bm->etable_tot) { @@ -593,9 +595,11 @@ void BM_mesh_elem_table_ensure(BMesh *bm, const char htype) if (bm->etable) { MEM_freeN(bm->etable); } - bm->etable = MEM_mallocN(sizeof(void **) * bm->totedge, "bm->etable"); + bm->etable = static_cast<BMEdge **>( + MEM_mallocN(sizeof(void **) * bm->totedge, "bm->etable")); bm->etable_tot = bm->totedge; } + BM_iter_as_array(bm, BM_EDGES_OF_MESH, nullptr, (void **)bm->etable, bm->totedge); } if (htype_needed & BM_FACE) { if (bm->ftable && bm->totface <= bm->ftable_tot && bm->totface * 2 >= bm->ftable_tot) { @@ -605,21 +609,11 @@ void BM_mesh_elem_table_ensure(BMesh *bm, const char htype) if (bm->ftable) { MEM_freeN(bm->ftable); } - bm->ftable = MEM_mallocN(sizeof(void **) * bm->totface, "bm->ftable"); + bm->ftable = static_cast<BMFace **>( + MEM_mallocN(sizeof(void **) * bm->totface, "bm->ftable")); bm->ftable_tot = bm->totface; } - } - - if (htype_needed & BM_VERT) { - BM_iter_as_array(bm, BM_VERTS_OF_MESH, NULL, (void **)bm->vtable, bm->totvert); - } - - if (htype_needed & BM_EDGE) { - BM_iter_as_array(bm, BM_EDGES_OF_MESH, NULL, (void **)bm->etable, bm->totedge); - } - - if (htype_needed & BM_FACE) { - BM_iter_as_array(bm, BM_FACES_OF_MESH, NULL, (void **)bm->ftable, bm->totface); + BM_iter_as_array(bm, BM_FACES_OF_MESH, nullptr, (void **)bm->ftable, bm->totface); } finally: @@ -656,17 +650,17 @@ void BM_mesh_elem_table_free(BMesh *bm, const char htype) BMVert *BM_vert_at_index_find(BMesh *bm, const int index) { - return BLI_mempool_findelem(bm->vpool, index); + return static_cast<BMVert *>(BLI_mempool_findelem(bm->vpool, index)); } BMEdge *BM_edge_at_index_find(BMesh *bm, const int index) { - return BLI_mempool_findelem(bm->epool, index); + return static_cast<BMEdge *>(BLI_mempool_findelem(bm->epool, index)); } BMFace *BM_face_at_index_find(BMesh *bm, const int index) { - return BLI_mempool_findelem(bm->fpool, index); + return static_cast<BMFace *>(BLI_mempool_findelem(bm->fpool, index)); } BMLoop *BM_loop_at_index_find(BMesh *bm, const int index) @@ -687,13 +681,13 @@ BMLoop *BM_loop_at_index_find(BMesh *bm, const int index) } i -= f->len; } - return NULL; + return nullptr; } BMVert *BM_vert_at_index_find_or_table(BMesh *bm, const int index) { if ((bm->elem_table_dirty & BM_VERT) == 0) { - return (index < bm->totvert) ? bm->vtable[index] : NULL; + return (index < bm->totvert) ? bm->vtable[index] : nullptr; } return BM_vert_at_index_find(bm, index); } @@ -701,7 +695,7 @@ BMVert *BM_vert_at_index_find_or_table(BMesh *bm, const int index) BMEdge *BM_edge_at_index_find_or_table(BMesh *bm, const int index) { if ((bm->elem_table_dirty & BM_EDGE) == 0) { - return (index < bm->totedge) ? bm->etable[index] : NULL; + return (index < bm->totedge) ? bm->etable[index] : nullptr; } return BM_edge_at_index_find(bm, index); } @@ -709,7 +703,7 @@ BMEdge *BM_edge_at_index_find_or_table(BMesh *bm, const int index) BMFace *BM_face_at_index_find_or_table(BMesh *bm, const int index) { if ((bm->elem_table_dirty & BM_FACE) == 0) { - return (index < bm->totface) ? bm->ftable[index] : NULL; + return (index < bm->totface) ? bm->ftable[index] : nullptr; } return BM_face_at_index_find(bm, index); } @@ -735,7 +729,7 @@ int BM_mesh_elem_count(BMesh *bm, const char htype) void BM_mesh_remap(BMesh *bm, const uint *vert_idx, const uint *edge_idx, const uint *face_idx) { /* Mapping old to new pointers. */ - GHash *vptr_map = NULL, *eptr_map = NULL, *fptr_map = NULL; + GHash *vptr_map = nullptr, *eptr_map = nullptr, *fptr_map = nullptr; BMIter iter, iterl; BMVert *ve; BMEdge *ed; @@ -763,14 +757,17 @@ void BM_mesh_remap(BMesh *bm, const uint *vert_idx, const uint *edge_idx, const /* Make a copy of all vertices. */ verts_pool = bm->vtable; - verts_copy = MEM_mallocN(sizeof(BMVert) * totvert, "BM_mesh_remap verts copy"); - void **pyptrs = (cd_vert_pyptr != -1) ? MEM_mallocN(sizeof(void *) * totvert, __func__) : NULL; + verts_copy = static_cast<BMVert *>( + MEM_mallocN(sizeof(BMVert) * totvert, "BM_mesh_remap verts copy")); + void **pyptrs = (cd_vert_pyptr != -1) ? + static_cast<void **>(MEM_mallocN(sizeof(void *) * totvert, __func__)) : + nullptr; for (i = totvert, ve = verts_copy + totvert - 1, vep = verts_pool + totvert - 1; i--; ve--, vep--) { *ve = **vep; // printf("*vep: %p, verts_pool[%d]: %p\n", *vep, i, verts_pool[i]); if (cd_vert_pyptr != -1) { - void **pyptr = BM_ELEM_CD_GET_VOID_P(((BMElem *)ve), cd_vert_pyptr); + void **pyptr = static_cast<void **>(BM_ELEM_CD_GET_VOID_P(((BMElem *)ve), cd_vert_pyptr)); pyptrs[i] = *pyptr; } } @@ -788,7 +785,8 @@ void BM_mesh_remap(BMesh *bm, const uint *vert_idx, const uint *edge_idx, const #endif BLI_ghash_insert(vptr_map, *vep, new_vep); if (cd_vert_pyptr != -1) { - void **pyptr = BM_ELEM_CD_GET_VOID_P(((BMElem *)new_vep), cd_vert_pyptr); + void **pyptr = static_cast<void **>( + BM_ELEM_CD_GET_VOID_P(((BMElem *)new_vep), cd_vert_pyptr)); *pyptr = pyptrs[*new_idx]; } } @@ -815,13 +813,16 @@ void BM_mesh_remap(BMesh *bm, const uint *vert_idx, const uint *edge_idx, const /* Make a copy of all vertices. */ edges_pool = bm->etable; - edges_copy = MEM_mallocN(sizeof(BMEdge) * totedge, "BM_mesh_remap edges copy"); - void **pyptrs = (cd_edge_pyptr != -1) ? MEM_mallocN(sizeof(void *) * totedge, __func__) : NULL; + edges_copy = static_cast<BMEdge *>( + MEM_mallocN(sizeof(BMEdge) * totedge, "BM_mesh_remap edges copy")); + void **pyptrs = (cd_edge_pyptr != -1) ? + static_cast<void **>(MEM_mallocN(sizeof(void *) * totedge, __func__)) : + nullptr; for (i = totedge, ed = edges_copy + totedge - 1, edp = edges_pool + totedge - 1; i--; ed--, edp--) { *ed = **edp; if (cd_edge_pyptr != -1) { - void **pyptr = BM_ELEM_CD_GET_VOID_P(((BMElem *)ed), cd_edge_pyptr); + void **pyptr = static_cast<void **>(BM_ELEM_CD_GET_VOID_P(((BMElem *)ed), cd_edge_pyptr)); pyptrs[i] = *pyptr; } } @@ -839,7 +840,8 @@ void BM_mesh_remap(BMesh *bm, const uint *vert_idx, const uint *edge_idx, const "mapping edge from %d to %d (%p/%p to %p)\n", i, *new_idx, *edp, edges_pool[i], new_edp); #endif if (cd_edge_pyptr != -1) { - void **pyptr = BM_ELEM_CD_GET_VOID_P(((BMElem *)new_edp), cd_edge_pyptr); + void **pyptr = static_cast<void **>( + BM_ELEM_CD_GET_VOID_P(((BMElem *)new_edp), cd_edge_pyptr)); *pyptr = pyptrs[*new_idx]; } } @@ -866,13 +868,16 @@ void BM_mesh_remap(BMesh *bm, const uint *vert_idx, const uint *edge_idx, const /* Make a copy of all vertices. */ faces_pool = bm->ftable; - faces_copy = MEM_mallocN(sizeof(BMFace) * totface, "BM_mesh_remap faces copy"); - void **pyptrs = (cd_poly_pyptr != -1) ? MEM_mallocN(sizeof(void *) * totface, __func__) : NULL; + faces_copy = static_cast<BMFace *>( + MEM_mallocN(sizeof(BMFace) * totface, "BM_mesh_remap faces copy")); + void **pyptrs = (cd_poly_pyptr != -1) ? + static_cast<void **>(MEM_mallocN(sizeof(void *) * totface, __func__)) : + nullptr; for (i = totface, fa = faces_copy + totface - 1, fap = faces_pool + totface - 1; i--; fa--, fap--) { *fa = **fap; if (cd_poly_pyptr != -1) { - void **pyptr = BM_ELEM_CD_GET_VOID_P(((BMElem *)fa), cd_poly_pyptr); + void **pyptr = static_cast<void **>(BM_ELEM_CD_GET_VOID_P(((BMElem *)fa), cd_poly_pyptr)); pyptrs[i] = *pyptr; } } @@ -886,7 +891,8 @@ void BM_mesh_remap(BMesh *bm, const uint *vert_idx, const uint *edge_idx, const *new_fap = *fa; BLI_ghash_insert(fptr_map, *fap, new_fap); if (cd_poly_pyptr != -1) { - void **pyptr = BM_ELEM_CD_GET_VOID_P(((BMElem *)new_fap), cd_poly_pyptr); + void **pyptr = static_cast<void **>( + BM_ELEM_CD_GET_VOID_P(((BMElem *)new_fap), cd_poly_pyptr)); *pyptr = pyptrs[*new_idx]; } } @@ -906,7 +912,7 @@ void BM_mesh_remap(BMesh *bm, const uint *vert_idx, const uint *edge_idx, const BM_ITER_MESH (ve, &iter, bm, BM_VERTS_OF_MESH) { // printf("Vert e: %p -> %p\n", ve->e, BLI_ghash_lookup(eptr_map, ve->e)); if (ve->e) { - ve->e = BLI_ghash_lookup(eptr_map, ve->e); + ve->e = static_cast<BMEdge *>(BLI_ghash_lookup(eptr_map, ve->e)); BLI_assert(ve->e); } } @@ -922,8 +928,8 @@ void BM_mesh_remap(BMesh *bm, const uint *vert_idx, const uint *edge_idx, const printf("Edge v1: %p -> %p\n", ed->v1, BLI_ghash_lookup(vptr_map, ed->v1)); printf("Edge v2: %p -> %p\n", ed->v2, BLI_ghash_lookup(vptr_map, ed->v2)); #endif - ed->v1 = BLI_ghash_lookup(vptr_map, ed->v1); - ed->v2 = BLI_ghash_lookup(vptr_map, ed->v2); + ed->v1 = static_cast<BMVert *>(BLI_ghash_lookup(vptr_map, ed->v1)); + ed->v2 = static_cast<BMVert *>(BLI_ghash_lookup(vptr_map, ed->v2)); BLI_assert(ed->v1); BLI_assert(ed->v2); } @@ -942,10 +948,14 @@ void BM_mesh_remap(BMesh *bm, const uint *vert_idx, const uint *edge_idx, const ed->v2_disk_link.next, BLI_ghash_lookup(eptr_map, ed->v2_disk_link.next)); #endif - ed->v1_disk_link.prev = BLI_ghash_lookup(eptr_map, ed->v1_disk_link.prev); - ed->v1_disk_link.next = BLI_ghash_lookup(eptr_map, ed->v1_disk_link.next); - ed->v2_disk_link.prev = BLI_ghash_lookup(eptr_map, ed->v2_disk_link.prev); - ed->v2_disk_link.next = BLI_ghash_lookup(eptr_map, ed->v2_disk_link.next); + ed->v1_disk_link.prev = static_cast<BMEdge *>( + BLI_ghash_lookup(eptr_map, ed->v1_disk_link.prev)); + ed->v1_disk_link.next = static_cast<BMEdge *>( + BLI_ghash_lookup(eptr_map, ed->v1_disk_link.next)); + ed->v2_disk_link.prev = static_cast<BMEdge *>( + BLI_ghash_lookup(eptr_map, ed->v2_disk_link.prev)); + ed->v2_disk_link.next = static_cast<BMEdge *>( + BLI_ghash_lookup(eptr_map, ed->v2_disk_link.next)); BLI_assert(ed->v1_disk_link.prev); BLI_assert(ed->v1_disk_link.next); BLI_assert(ed->v2_disk_link.prev); @@ -959,17 +969,17 @@ void BM_mesh_remap(BMesh *bm, const uint *vert_idx, const uint *edge_idx, const BM_ITER_ELEM (lo, &iterl, fa, BM_LOOPS_OF_FACE) { if (vptr_map) { // printf("Loop v: %p -> %p\n", lo->v, BLI_ghash_lookup(vptr_map, lo->v)); - lo->v = BLI_ghash_lookup(vptr_map, lo->v); + lo->v = static_cast<BMVert *>(BLI_ghash_lookup(vptr_map, lo->v)); BLI_assert(lo->v); } if (eptr_map) { // printf("Loop e: %p -> %p\n", lo->e, BLI_ghash_lookup(eptr_map, lo->e)); - lo->e = BLI_ghash_lookup(eptr_map, lo->e); + lo->e = static_cast<BMEdge *>(BLI_ghash_lookup(eptr_map, lo->e)); BLI_assert(lo->e); } if (fptr_map) { // printf("Loop f: %p -> %p\n", lo->f, BLI_ghash_lookup(fptr_map, lo->f)); - lo->f = BLI_ghash_lookup(fptr_map, lo->f); + lo->f = static_cast<BMFace *>(BLI_ghash_lookup(fptr_map, lo->f)); BLI_assert(lo->f); } } @@ -978,23 +988,23 @@ void BM_mesh_remap(BMesh *bm, const uint *vert_idx, const uint *edge_idx, const /* Selection history */ { BMEditSelection *ese; - for (ese = bm->selected.first; ese; ese = ese->next) { + for (ese = static_cast<BMEditSelection *>(bm->selected.first); ese; ese = ese->next) { switch (ese->htype) { case BM_VERT: if (vptr_map) { - ese->ele = BLI_ghash_lookup(vptr_map, ese->ele); + ese->ele = static_cast<BMElem *>(BLI_ghash_lookup(vptr_map, ese->ele)); BLI_assert(ese->ele); } break; case BM_EDGE: if (eptr_map) { - ese->ele = BLI_ghash_lookup(eptr_map, ese->ele); + ese->ele = static_cast<BMElem *>(BLI_ghash_lookup(eptr_map, ese->ele)); BLI_assert(ese->ele); } break; case BM_FACE: if (fptr_map) { - ese->ele = BLI_ghash_lookup(fptr_map, ese->ele); + ese->ele = static_cast<BMElem *>(BLI_ghash_lookup(fptr_map, ese->ele)); BLI_assert(ese->ele); } break; @@ -1004,19 +1014,19 @@ void BM_mesh_remap(BMesh *bm, const uint *vert_idx, const uint *edge_idx, const if (fptr_map) { if (bm->act_face) { - bm->act_face = BLI_ghash_lookup(fptr_map, bm->act_face); + bm->act_face = static_cast<BMFace *>(BLI_ghash_lookup(fptr_map, bm->act_face)); BLI_assert(bm->act_face); } } if (vptr_map) { - BLI_ghash_free(vptr_map, NULL, NULL); + BLI_ghash_free(vptr_map, nullptr, nullptr); } if (eptr_map) { - BLI_ghash_free(eptr_map, NULL, NULL); + BLI_ghash_free(eptr_map, nullptr, nullptr); } if (fptr_map) { - BLI_ghash_free(fptr_map, NULL, NULL); + BLI_ghash_free(fptr_map, nullptr, nullptr); } } @@ -1030,14 +1040,18 @@ void BM_mesh_rebuild(BMesh *bm, const char remap = (vpool_dst ? BM_VERT : 0) | (epool_dst ? BM_EDGE : 0) | (lpool_dst ? BM_LOOP : 0) | (fpool_dst ? BM_FACE : 0); - BMVert **vtable_dst = (remap & BM_VERT) ? MEM_mallocN(bm->totvert * sizeof(BMVert *), __func__) : - NULL; - BMEdge **etable_dst = (remap & BM_EDGE) ? MEM_mallocN(bm->totedge * sizeof(BMEdge *), __func__) : - NULL; - BMLoop **ltable_dst = (remap & BM_LOOP) ? MEM_mallocN(bm->totloop * sizeof(BMLoop *), __func__) : - NULL; - BMFace **ftable_dst = (remap & BM_FACE) ? MEM_mallocN(bm->totface * sizeof(BMFace *), __func__) : - NULL; + BMVert **vtable_dst = (remap & BM_VERT) ? static_cast<BMVert **>(MEM_mallocN( + sizeof(BMVert *) * bm->totvert, __func__)) : + nullptr; + BMEdge **etable_dst = (remap & BM_EDGE) ? static_cast<BMEdge **>(MEM_mallocN( + sizeof(BMEdge *) * bm->totedge, __func__)) : + nullptr; + BMLoop **ltable_dst = (remap & BM_LOOP) ? static_cast<BMLoop **>(MEM_mallocN( + sizeof(BMLoop *) * bm->totloop, __func__)) : + nullptr; + BMFace **ftable_dst = (remap & BM_FACE) ? static_cast<BMFace **>(MEM_mallocN( + sizeof(BMFace *) * bm->totface, __func__)) : + nullptr; const bool use_toolflags = params->use_toolflags; @@ -1046,12 +1060,13 @@ void BM_mesh_rebuild(BMesh *bm, int index; BMVert *v_src; BM_ITER_MESH_INDEX (v_src, &iter, bm, BM_VERTS_OF_MESH, index) { - BMVert *v_dst = BLI_mempool_alloc(vpool_dst); + BMVert *v_dst = static_cast<BMVert *>(BLI_mempool_alloc(vpool_dst)); memcpy(v_dst, v_src, sizeof(BMVert)); if (use_toolflags) { ((BMVert_OFlag *)v_dst)->oflags = bm->vtoolflagpool ? - BLI_mempool_calloc(bm->vtoolflagpool) : - NULL; + static_cast<BMFlagLayer *>( + BLI_mempool_calloc(bm->vtoolflagpool)) : + nullptr; } vtable_dst[index] = v_dst; @@ -1064,12 +1079,13 @@ void BM_mesh_rebuild(BMesh *bm, int index; BMEdge *e_src; BM_ITER_MESH_INDEX (e_src, &iter, bm, BM_EDGES_OF_MESH, index) { - BMEdge *e_dst = BLI_mempool_alloc(epool_dst); + BMEdge *e_dst = static_cast<BMEdge *>(BLI_mempool_alloc(epool_dst)); memcpy(e_dst, e_src, sizeof(BMEdge)); if (use_toolflags) { ((BMEdge_OFlag *)e_dst)->oflags = bm->etoolflagpool ? - BLI_mempool_calloc(bm->etoolflagpool) : - NULL; + static_cast<BMFlagLayer *>( + BLI_mempool_calloc(bm->etoolflagpool)) : + nullptr; } etable_dst[index] = e_dst; @@ -1084,12 +1100,13 @@ void BM_mesh_rebuild(BMesh *bm, BM_ITER_MESH_INDEX (f_src, &iter, bm, BM_FACES_OF_MESH, index) { if (remap & BM_FACE) { - BMFace *f_dst = BLI_mempool_alloc(fpool_dst); + BMFace *f_dst = static_cast<BMFace *>(BLI_mempool_alloc(fpool_dst)); memcpy(f_dst, f_src, sizeof(BMFace)); if (use_toolflags) { ((BMFace_OFlag *)f_dst)->oflags = bm->ftoolflagpool ? - BLI_mempool_calloc(bm->ftoolflagpool) : - NULL; + static_cast<BMFlagLayer *>( + BLI_mempool_calloc(bm->ftoolflagpool)) : + nullptr; } ftable_dst[index] = f_dst; @@ -1101,7 +1118,7 @@ void BM_mesh_rebuild(BMesh *bm, BMLoop *l_iter_src, *l_first_src; l_iter_src = l_first_src = BM_FACE_FIRST_LOOP((BMFace *)f_src); do { - BMLoop *l_dst = BLI_mempool_alloc(lpool_dst); + BMLoop *l_dst = static_cast<BMLoop *>(BLI_mempool_alloc(lpool_dst)); memcpy(l_dst, l_iter_src, sizeof(BMLoop)); ltable_dst[index_loop] = l_dst; BM_elem_index_set(l_iter_src, index_loop++); /* set_ok */ @@ -1279,30 +1296,25 @@ void BM_mesh_toolflags_set(BMesh *bm, bool use_toolflags) const BMAllocTemplate allocsize = BMALLOC_TEMPLATE_FROM_BM(bm); - BLI_mempool *vpool_dst = NULL; - BLI_mempool *epool_dst = NULL; - BLI_mempool *fpool_dst = NULL; + BLI_mempool *vpool_dst = nullptr; + BLI_mempool *epool_dst = nullptr; + BLI_mempool *fpool_dst = nullptr; - bm_mempool_init_ex(&allocsize, use_toolflags, &vpool_dst, &epool_dst, NULL, &fpool_dst); + bm_mempool_init_ex(&allocsize, use_toolflags, &vpool_dst, &epool_dst, nullptr, &fpool_dst); if (use_toolflags == false) { BLI_mempool_destroy(bm->vtoolflagpool); BLI_mempool_destroy(bm->etoolflagpool); BLI_mempool_destroy(bm->ftoolflagpool); - bm->vtoolflagpool = NULL; - bm->etoolflagpool = NULL; - bm->ftoolflagpool = NULL; + bm->vtoolflagpool = nullptr; + bm->etoolflagpool = nullptr; + bm->ftoolflagpool = nullptr; } + struct BMeshCreateParams params = {}; + params.use_toolflags = use_toolflags; - BM_mesh_rebuild(bm, - &((struct BMeshCreateParams){ - .use_toolflags = use_toolflags, - }), - vpool_dst, - epool_dst, - NULL, - fpool_dst); + BM_mesh_rebuild(bm, ¶ms, vpool_dst, epool_dst, nullptr, fpool_dst); bm->use_toolflags = use_toolflags; } @@ -1323,7 +1335,8 @@ void BM_mesh_vert_coords_get(BMesh *bm, float (*vert_coords)[3]) float (*BM_mesh_vert_coords_alloc(BMesh *bm, int *r_vert_len))[3] { - float(*vert_coords)[3] = MEM_mallocN(bm->totvert * sizeof(*vert_coords), __func__); + float(*vert_coords)[3] = static_cast<float(*)[3]>( + MEM_mallocN(bm->totvert * sizeof(*vert_coords), __func__)); BM_mesh_vert_coords_get(bm, vert_coords); *r_vert_len = bm->totvert; return vert_coords; diff --git a/source/blender/bmesh/intern/bmesh_mesh.h b/source/blender/bmesh/intern/bmesh_mesh.h index a5994b52bc2..d766a26cf6e 100644 --- a/source/blender/bmesh/intern/bmesh_mesh.h +++ b/source/blender/bmesh/intern/bmesh_mesh.h @@ -17,7 +17,7 @@ void BM_mesh_elem_toolflags_ensure(BMesh *bm); void BM_mesh_elem_toolflags_clear(BMesh *bm); struct BMeshCreateParams { - bool use_toolflags : true; + bool use_toolflags : 1; }; /** diff --git a/source/blender/bmesh/intern/bmesh_mesh_convert.cc b/source/blender/bmesh/intern/bmesh_mesh_convert.cc index 884f6b5388a..94440916603 100644 --- a/source/blender/bmesh/intern/bmesh_mesh_convert.cc +++ b/source/blender/bmesh/intern/bmesh_mesh_convert.cc @@ -83,7 +83,10 @@ #include "BLI_listbase.h" #include "BLI_math_vector.h" #include "BLI_span.hh" +#include "BLI_string_ref.hh" +#include "BLI_task.hh" +#include "BKE_attribute.hh" #include "BKE_customdata.h" #include "BKE_mesh.h" #include "BKE_mesh_runtime.h" @@ -103,7 +106,9 @@ static CLG_LogRef LOG = {"bmesh.mesh.convert"}; using blender::Array; using blender::IndexRange; +using blender::MutableSpan; using blender::Span; +using blender::StringRef; void BM_mesh_cd_flag_ensure(BMesh *bm, Mesh *mesh, const char cd_flag) { @@ -121,17 +126,6 @@ void BM_mesh_cd_flag_apply(BMesh *bm, const char cd_flag) BLI_assert(bm->edata.totlayer == 0 || bm->edata.pool != nullptr); BLI_assert(bm->pdata.totlayer == 0 || bm->pdata.pool != nullptr); - if (cd_flag & ME_CDFLAG_VERT_BWEIGHT) { - if (!CustomData_has_layer(&bm->vdata, CD_BWEIGHT)) { - BM_data_layer_add(bm, &bm->vdata, CD_BWEIGHT); - } - } - else { - if (CustomData_has_layer(&bm->vdata, CD_BWEIGHT)) { - BM_data_layer_free(bm, &bm->vdata, CD_BWEIGHT); - } - } - if (cd_flag & ME_CDFLAG_VERT_CREASE) { if (!CustomData_has_layer(&bm->vdata, CD_CREASE)) { BM_data_layer_add(bm, &bm->vdata, CD_CREASE); @@ -143,17 +137,6 @@ void BM_mesh_cd_flag_apply(BMesh *bm, const char cd_flag) } } - if (cd_flag & ME_CDFLAG_EDGE_BWEIGHT) { - if (!CustomData_has_layer(&bm->edata, CD_BWEIGHT)) { - BM_data_layer_add(bm, &bm->edata, CD_BWEIGHT); - } - } - else { - if (CustomData_has_layer(&bm->edata, CD_BWEIGHT)) { - BM_data_layer_free(bm, &bm->edata, CD_BWEIGHT); - } - } - if (cd_flag & ME_CDFLAG_EDGE_CREASE) { if (!CustomData_has_layer(&bm->edata, CD_CREASE)) { BM_data_layer_add(bm, &bm->edata, CD_CREASE); @@ -169,15 +152,9 @@ void BM_mesh_cd_flag_apply(BMesh *bm, const char cd_flag) char BM_mesh_cd_flag_from_bmesh(BMesh *bm) { char cd_flag = 0; - if (CustomData_has_layer(&bm->vdata, CD_BWEIGHT)) { - cd_flag |= ME_CDFLAG_VERT_BWEIGHT; - } if (CustomData_has_layer(&bm->vdata, CD_CREASE)) { cd_flag |= ME_CDFLAG_VERT_CREASE; } - if (CustomData_has_layer(&bm->edata, CD_BWEIGHT)) { - cd_flag |= ME_CDFLAG_EDGE_BWEIGHT; - } if (CustomData_has_layer(&bm->edata, CD_CREASE)) { cd_flag |= ME_CDFLAG_EDGE_CREASE; } @@ -212,10 +189,10 @@ void BM_mesh_bm_from_me(BMesh *bm, const Mesh *me, const struct BMeshFromMeshPar if (!me || !me->totvert) { if (me && is_new) { /* No verts? still copy custom-data layout. */ - CustomData_copy(&me->vdata, &bm->vdata, mask.vmask, CD_DEFAULT, 0); - CustomData_copy(&me->edata, &bm->edata, mask.emask, CD_DEFAULT, 0); - CustomData_copy(&me->ldata, &bm->ldata, mask.lmask, CD_DEFAULT, 0); - CustomData_copy(&me->pdata, &bm->pdata, mask.pmask, CD_DEFAULT, 0); + CustomData_copy_mesh_to_bmesh(&me->vdata, &bm->vdata, mask.vmask, CD_CONSTRUCT, 0); + CustomData_copy_mesh_to_bmesh(&me->edata, &bm->edata, mask.emask, CD_CONSTRUCT, 0); + CustomData_copy_mesh_to_bmesh(&me->ldata, &bm->ldata, mask.lmask, CD_CONSTRUCT, 0); + CustomData_copy_mesh_to_bmesh(&me->pdata, &bm->pdata, mask.pmask, CD_CONSTRUCT, 0); CustomData_bmesh_init_pool(&bm->vdata, me->totvert, BM_VERT); CustomData_bmesh_init_pool(&bm->edata, me->totedge, BM_EDGE); @@ -231,16 +208,16 @@ void BM_mesh_bm_from_me(BMesh *bm, const Mesh *me, const struct BMeshFromMeshPar } if (is_new) { - CustomData_copy(&me->vdata, &bm->vdata, mask.vmask, CD_CALLOC, 0); - CustomData_copy(&me->edata, &bm->edata, mask.emask, CD_CALLOC, 0); - CustomData_copy(&me->ldata, &bm->ldata, mask.lmask, CD_CALLOC, 0); - CustomData_copy(&me->pdata, &bm->pdata, mask.pmask, CD_CALLOC, 0); + CustomData_copy_mesh_to_bmesh(&me->vdata, &bm->vdata, mask.vmask, CD_SET_DEFAULT, 0); + CustomData_copy_mesh_to_bmesh(&me->edata, &bm->edata, mask.emask, CD_SET_DEFAULT, 0); + CustomData_copy_mesh_to_bmesh(&me->ldata, &bm->ldata, mask.lmask, CD_SET_DEFAULT, 0); + CustomData_copy_mesh_to_bmesh(&me->pdata, &bm->pdata, mask.pmask, CD_SET_DEFAULT, 0); } else { - CustomData_bmesh_merge(&me->vdata, &bm->vdata, mask.vmask, CD_CALLOC, bm, BM_VERT); - CustomData_bmesh_merge(&me->edata, &bm->edata, mask.emask, CD_CALLOC, bm, BM_EDGE); - CustomData_bmesh_merge(&me->ldata, &bm->ldata, mask.lmask, CD_CALLOC, bm, BM_LOOP); - CustomData_bmesh_merge(&me->pdata, &bm->pdata, mask.pmask, CD_CALLOC, bm, BM_FACE); + CustomData_bmesh_merge(&me->vdata, &bm->vdata, mask.vmask, CD_SET_DEFAULT, bm, BM_VERT); + CustomData_bmesh_merge(&me->edata, &bm->edata, mask.emask, CD_SET_DEFAULT, bm, BM_EDGE); + CustomData_bmesh_merge(&me->ldata, &bm->ldata, mask.lmask, CD_SET_DEFAULT, bm, BM_LOOP); + CustomData_bmesh_merge(&me->pdata, &bm->pdata, mask.pmask, CD_SET_DEFAULT, bm, BM_FACE); } /* -------------------------------------------------------------------- */ @@ -337,12 +314,6 @@ void BM_mesh_bm_from_me(BMesh *bm, const Mesh *me, const struct BMeshFromMeshPar /* Only copy these values over if the source mesh is flagged to be using them. * Even if `bm` has these layers, they may have been added from another mesh, when `!is_new`. */ - const int cd_vert_bweight_offset = (me->cd_flag & ME_CDFLAG_VERT_BWEIGHT) ? - CustomData_get_offset(&bm->vdata, CD_BWEIGHT) : - -1; - const int cd_edge_bweight_offset = (me->cd_flag & ME_CDFLAG_EDGE_BWEIGHT) ? - CustomData_get_offset(&bm->edata, CD_BWEIGHT) : - -1; const int cd_edge_crease_offset = (me->cd_flag & ME_CDFLAG_EDGE_CREASE) ? CustomData_get_offset(&bm->edata, CD_CREASE) : -1; @@ -352,7 +323,16 @@ void BM_mesh_bm_from_me(BMesh *bm, const Mesh *me, const struct BMeshFromMeshPar CustomData_get_offset(&bm->vdata, CD_SHAPE_KEYINDEX) : -1; - Span<MVert> mvert{me->mvert, me->totvert}; + const bool *hide_vert = (const bool *)CustomData_get_layer_named( + &me->vdata, CD_PROP_BOOL, ".hide_vert"); + const bool *hide_edge = (const bool *)CustomData_get_layer_named( + &me->edata, CD_PROP_BOOL, ".hide_edge"); + const bool *hide_poly = (const bool *)CustomData_get_layer_named( + &me->pdata, CD_PROP_BOOL, ".hide_poly"); + const int *material_indices = (const int *)CustomData_get_layer_named( + &me->pdata, CD_PROP_INT32, "material_index"); + + Span<MVert> mvert = me->verts(); Array<BMVert *> vtable(me->totvert); for (const int i : mvert.index_range()) { BMVert *v = vtable[i] = BM_vert_create( @@ -361,6 +341,9 @@ void BM_mesh_bm_from_me(BMesh *bm, const Mesh *me, const struct BMeshFromMeshPar /* Transfer flag. */ v->head.hflag = BM_vert_flag_from_mflag(mvert[i].flag & ~SELECT); + if (hide_vert && hide_vert[i]) { + BM_elem_flag_enable(v, BM_ELEM_HIDDEN); + } /* This is necessary for selection counts to work properly. */ if (mvert[i].flag & SELECT) { @@ -374,10 +357,6 @@ void BM_mesh_bm_from_me(BMesh *bm, const Mesh *me, const struct BMeshFromMeshPar /* Copy Custom Data */ CustomData_to_bmesh_block(&me->vdata, &bm->vdata, i, &v->head.data, true); - if (cd_vert_bweight_offset != -1) { - BM_ELEM_CD_SET_FLOAT(v, cd_vert_bweight_offset, (float)mvert[i].bweight / 255.0f); - } - /* Set shape key original index. */ if (cd_shape_keyindex_offset != -1) { BM_ELEM_CD_SET_INT(v, cd_shape_keyindex_offset, i); @@ -395,7 +374,7 @@ void BM_mesh_bm_from_me(BMesh *bm, const Mesh *me, const struct BMeshFromMeshPar bm->elem_index_dirty &= ~BM_VERT; /* Added in order, clear dirty flag. */ } - Span<MEdge> medge{me->medge, me->totedge}; + const Span<MEdge> medge = me->edges(); Array<BMEdge *> etable(me->totedge); for (const int i : medge.index_range()) { BMEdge *e = etable[i] = BM_edge_create( @@ -404,6 +383,9 @@ void BM_mesh_bm_from_me(BMesh *bm, const Mesh *me, const struct BMeshFromMeshPar /* Transfer flags. */ e->head.hflag = BM_edge_flag_from_mflag(medge[i].flag & ~SELECT); + if (hide_edge && hide_edge[i]) { + BM_elem_flag_enable(e, BM_ELEM_HIDDEN); + } /* This is necessary for selection counts to work properly. */ if (medge[i].flag & SELECT) { @@ -413,9 +395,6 @@ void BM_mesh_bm_from_me(BMesh *bm, const Mesh *me, const struct BMeshFromMeshPar /* Copy Custom Data */ CustomData_to_bmesh_block(&me->edata, &bm->edata, i, &e->head.data, true); - if (cd_edge_bweight_offset != -1) { - BM_ELEM_CD_SET_FLOAT(e, cd_edge_bweight_offset, (float)medge[i].bweight / 255.0f); - } if (cd_edge_crease_offset != -1) { BM_ELEM_CD_SET_FLOAT(e, cd_edge_crease_offset, (float)medge[i].crease / 255.0f); } @@ -424,8 +403,8 @@ void BM_mesh_bm_from_me(BMesh *bm, const Mesh *me, const struct BMeshFromMeshPar bm->elem_index_dirty &= ~BM_EDGE; /* Added in order, clear dirty flag. */ } - Span<MPoly> mpoly{me->mpoly, me->totpoly}; - Span<MLoop> mloop{me->mloop, me->totloop}; + const Span<MPoly> mpoly = me->polys(); + const Span<MLoop> mloop = me->loops(); /* Only needed for selection. */ @@ -457,13 +436,16 @@ void BM_mesh_bm_from_me(BMesh *bm, const Mesh *me, const struct BMeshFromMeshPar /* Transfer flag. */ f->head.hflag = BM_face_flag_from_mflag(mpoly[i].flag & ~ME_FACE_SEL); + if (hide_poly && hide_poly[i]) { + BM_elem_flag_enable(f, BM_ELEM_HIDDEN); + } /* This is necessary for selection counts to work properly. */ if (mpoly[i].flag & ME_FACE_SEL) { BM_face_select_set(bm, f, true); } - f->mat_nr = mpoly[i].mat_nr; + f->mat_nr = material_indices == nullptr ? 0 : material_indices[i]; if (i == me->act_face) { bm->act_face = f; } @@ -682,7 +664,7 @@ static int bm_to_mesh_shape_layer_index_from_kb(BMesh *bm, KeyBlock *currkey) */ static void bm_to_mesh_shape(BMesh *bm, Key *key, - MVert *mvert, + MutableSpan<MVert> mvert, const bool active_shapekey_to_mvert) { KeyBlock *actkey = static_cast<KeyBlock *>(BLI_findlink(&key->block, bm->shapenr - 1)); @@ -902,17 +884,71 @@ BLI_INLINE void bmesh_quick_edgedraw_flag(MEdge *med, BMEdge *e) } } +template<typename T, typename GetFn> +static void write_fn_to_attribute(blender::bke::MutableAttributeAccessor attributes, + const StringRef attribute_name, + const eAttrDomain domain, + const bool do_write, + const GetFn &get_fn) +{ + using namespace blender; + if (do_write) { + bke::SpanAttributeWriter<T> attribute = attributes.lookup_or_add_for_write_only_span<T>( + attribute_name, domain); + threading::parallel_for(attribute.span.index_range(), 4096, [&](IndexRange range) { + for (const int i : range) { + attribute.span[i] = get_fn(i); + } + }); + attribute.finish(); + } + else { + /* To avoid overhead, remove the hide attribute if possible. */ + attributes.remove(attribute_name); + } +} + +static void convert_bmesh_hide_flags_to_mesh_attributes(BMesh &bm, + const bool need_hide_vert, + const bool need_hide_edge, + const bool need_hide_poly, + Mesh &mesh) +{ + using namespace blender; + /* The "hide" attributes are stored as flags on #BMesh. */ + BLI_assert(CustomData_get_layer_named(&bm.vdata, CD_PROP_BOOL, ".hide_vert") == nullptr); + BLI_assert(CustomData_get_layer_named(&bm.edata, CD_PROP_BOOL, ".hide_edge") == nullptr); + BLI_assert(CustomData_get_layer_named(&bm.pdata, CD_PROP_BOOL, ".hide_poly") == nullptr); + + if (!(need_hide_vert || need_hide_edge || need_hide_poly)) { + return; + } + + bke::MutableAttributeAccessor attributes = mesh.attributes_for_write(); + BM_mesh_elem_table_ensure(&bm, BM_VERT | BM_EDGE | BM_FACE); + + write_fn_to_attribute<bool>( + attributes, ".hide_vert", ATTR_DOMAIN_POINT, need_hide_vert, [&](const int i) { + return BM_elem_flag_test(BM_vert_at_index(&bm, i), BM_ELEM_HIDDEN); + }); + write_fn_to_attribute<bool>( + attributes, ".hide_edge", ATTR_DOMAIN_EDGE, need_hide_edge, [&](const int i) { + return BM_elem_flag_test(BM_edge_at_index(&bm, i), BM_ELEM_HIDDEN); + }); + write_fn_to_attribute<bool>( + attributes, ".hide_poly", ATTR_DOMAIN_FACE, need_hide_poly, [&](const int i) { + return BM_elem_flag_test(BM_face_at_index(&bm, i), BM_ELEM_HIDDEN); + }); +} + void BM_mesh_bm_to_me(Main *bmain, BMesh *bm, Mesh *me, const struct BMeshToMeshParams *params) { - MEdge *med; BMVert *v, *eve; BMEdge *e; BMFace *f; BMIter iter; int i, j; - const int cd_vert_bweight_offset = CustomData_get_offset(&bm->vdata, CD_BWEIGHT); - const int cd_edge_bweight_offset = CustomData_get_offset(&bm->edata, CD_BWEIGHT); const int cd_edge_crease_offset = CustomData_get_offset(&bm->edata, CD_CREASE); const int cd_shape_keyindex_offset = CustomData_get_offset(&bm->vdata, CD_SHAPE_KEYINDEX); @@ -938,25 +974,41 @@ void BM_mesh_bm_to_me(Main *bmain, BMesh *bm, Mesh *me, const struct BMeshToMesh { CustomData_MeshMasks mask = CD_MASK_MESH; CustomData_MeshMasks_update(&mask, ¶ms->cd_mask_extra); - CustomData_copy(&bm->vdata, &me->vdata, mask.vmask, CD_CALLOC, me->totvert); - CustomData_copy(&bm->edata, &me->edata, mask.emask, CD_CALLOC, me->totedge); - CustomData_copy(&bm->ldata, &me->ldata, mask.lmask, CD_CALLOC, me->totloop); - CustomData_copy(&bm->pdata, &me->pdata, mask.pmask, CD_CALLOC, me->totpoly); + CustomData_copy_mesh_to_bmesh(&bm->vdata, &me->vdata, mask.vmask, CD_SET_DEFAULT, me->totvert); + CustomData_copy_mesh_to_bmesh(&bm->edata, &me->edata, mask.emask, CD_SET_DEFAULT, me->totedge); + CustomData_copy_mesh_to_bmesh(&bm->ldata, &me->ldata, mask.lmask, CD_SET_DEFAULT, me->totloop); + CustomData_copy_mesh_to_bmesh(&bm->pdata, &me->pdata, mask.pmask, CD_SET_DEFAULT, me->totpoly); } - MVert *mvert = bm->totvert ? (MVert *)MEM_callocN(sizeof(MVert) * bm->totvert, "bm_to_me.vert") : - nullptr; - MEdge *medge = bm->totedge ? (MEdge *)MEM_callocN(sizeof(MEdge) * bm->totedge, "bm_to_me.edge") : - nullptr; - MLoop *mloop = bm->totloop ? (MLoop *)MEM_callocN(sizeof(MLoop) * bm->totloop, "bm_to_me.loop") : - nullptr; - MPoly *mpoly = bm->totface ? (MPoly *)MEM_callocN(sizeof(MPoly) * bm->totface, "bm_to_me.poly") : - nullptr; + MutableSpan<MVert> mvert; + MutableSpan<MEdge> medge; + MutableSpan<MPoly> mpoly; + MutableSpan<MLoop> mloop; + if (me->totvert > 0) { + mvert = {static_cast<MVert *>( + CustomData_add_layer(&me->vdata, CD_MVERT, CD_SET_DEFAULT, nullptr, me->totvert)), + me->totvert}; + } + if (me->totedge > 0) { + medge = {static_cast<MEdge *>( + CustomData_add_layer(&me->edata, CD_MEDGE, CD_SET_DEFAULT, nullptr, me->totedge)), + me->totedge}; + } + if (me->totpoly > 0) { + mpoly = {static_cast<MPoly *>( + CustomData_add_layer(&me->pdata, CD_MPOLY, CD_SET_DEFAULT, nullptr, me->totpoly)), + me->totpoly}; + } + if (me->totloop > 0) { + mloop = {static_cast<MLoop *>( + CustomData_add_layer(&me->ldata, CD_MLOOP, CD_SET_DEFAULT, nullptr, me->totloop)), + me->totloop}; + } - CustomData_add_layer(&me->vdata, CD_MVERT, CD_ASSIGN, mvert, me->totvert); - CustomData_add_layer(&me->edata, CD_MEDGE, CD_ASSIGN, medge, me->totedge); - CustomData_add_layer(&me->ldata, CD_MLOOP, CD_ASSIGN, mloop, me->totloop); - CustomData_add_layer(&me->pdata, CD_MPOLY, CD_ASSIGN, mpoly, me->totpoly); + bool need_hide_vert = false; + bool need_hide_edge = false; + bool need_hide_poly = false; + bool need_material_index = false; /* Clear normals on the mesh completely, since the original vertex and polygon count might be * different than the BMesh's. */ @@ -964,55 +1016,48 @@ void BM_mesh_bm_to_me(Main *bmain, BMesh *bm, Mesh *me, const struct BMeshToMesh me->cd_flag = BM_mesh_cd_flag_from_bmesh(bm); - /* This is called again, 'dotess' arg is used there. */ - BKE_mesh_update_customdata_pointers(me, false); - i = 0; BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) { - copy_v3_v3(mvert->co, v->co); + copy_v3_v3(mvert[i].co, v->co); - mvert->flag = BM_vert_flag_to_mflag(v); + mvert[i].flag = BM_vert_flag_to_mflag(v); + if (BM_elem_flag_test(v, BM_ELEM_HIDDEN)) { + need_hide_vert = true; + } BM_elem_index_set(v, i); /* set_inline */ /* Copy over custom-data. */ CustomData_from_bmesh_block(&bm->vdata, &me->vdata, v->head.data, i); - if (cd_vert_bweight_offset != -1) { - mvert->bweight = BM_ELEM_CD_GET_FLOAT_AS_UCHAR(v, cd_vert_bweight_offset); - } - i++; - mvert++; BM_CHECK_ELEMENT(v); } bm->elem_index_dirty &= ~BM_VERT; - med = medge; i = 0; BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) { - med->v1 = BM_elem_index_get(e->v1); - med->v2 = BM_elem_index_get(e->v2); + medge[i].v1 = BM_elem_index_get(e->v1); + medge[i].v2 = BM_elem_index_get(e->v2); - med->flag = BM_edge_flag_to_mflag(e); + medge[i].flag = BM_edge_flag_to_mflag(e); + if (BM_elem_flag_test(e, BM_ELEM_HIDDEN)) { + need_hide_edge = true; + } BM_elem_index_set(e, i); /* set_inline */ /* Copy over custom-data. */ CustomData_from_bmesh_block(&bm->edata, &me->edata, e->head.data, i); - bmesh_quick_edgedraw_flag(med, e); + bmesh_quick_edgedraw_flag(&medge[i], e); if (cd_edge_crease_offset != -1) { - med->crease = BM_ELEM_CD_GET_FLOAT_AS_UCHAR(e, cd_edge_crease_offset); - } - if (cd_edge_bweight_offset != -1) { - med->bweight = BM_ELEM_CD_GET_FLOAT_AS_UCHAR(e, cd_edge_bweight_offset); + medge[i].crease = BM_ELEM_CD_GET_FLOAT_AS_UCHAR(e, cd_edge_crease_offset); } i++; - med++; BM_CHECK_ELEMENT(e); } bm->elem_index_dirty &= ~BM_EDGE; @@ -1021,21 +1066,25 @@ void BM_mesh_bm_to_me(Main *bmain, BMesh *bm, Mesh *me, const struct BMeshToMesh j = 0; BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) { BMLoop *l_iter, *l_first; - mpoly->loopstart = j; - mpoly->totloop = f->len; - mpoly->mat_nr = f->mat_nr; - mpoly->flag = BM_face_flag_to_mflag(f); + mpoly[i].loopstart = j; + mpoly[i].totloop = f->len; + if (f->mat_nr != 0) { + need_material_index = true; + } + mpoly[i].flag = BM_face_flag_to_mflag(f); + if (BM_elem_flag_test(f, BM_ELEM_HIDDEN)) { + need_hide_poly = true; + } l_iter = l_first = BM_FACE_FIRST_LOOP(f); do { - mloop->e = BM_elem_index_get(l_iter->e); - mloop->v = BM_elem_index_get(l_iter->v); + mloop[j].e = BM_elem_index_get(l_iter->e); + mloop[j].v = BM_elem_index_get(l_iter->v); /* Copy over custom-data. */ CustomData_from_bmesh_block(&bm->ldata, &me->ldata, l_iter->head.data, j); j++; - mloop++; BM_CHECK_ELEMENT(l_iter); BM_CHECK_ELEMENT(l_iter->e); BM_CHECK_ELEMENT(l_iter->v); @@ -1049,10 +1098,17 @@ void BM_mesh_bm_to_me(Main *bmain, BMesh *bm, Mesh *me, const struct BMeshToMesh CustomData_from_bmesh_block(&bm->pdata, &me->pdata, f->head.data, i); i++; - mpoly++; BM_CHECK_ELEMENT(f); } + if (need_material_index) { + BM_mesh_elem_table_ensure(bm, BM_FACE); + write_fn_to_attribute<int>( + me->attributes_for_write(), "material_index", ATTR_DOMAIN_FACE, true, [&](const int i) { + return static_cast<int>(BM_face_at_index(bm, i)->mat_nr); + }); + } + /* Patch hook indices and vertex parents. */ if (params->calc_object_remap && (ototvert > 0)) { BLI_assert(bmain != nullptr); @@ -1117,7 +1173,8 @@ void BM_mesh_bm_to_me(Main *bmain, BMesh *bm, Mesh *me, const struct BMeshToMesh } } - BKE_mesh_update_customdata_pointers(me, false); + convert_bmesh_hide_flags_to_mesh_attributes( + *bm, need_hide_vert, need_hide_edge, need_hide_poly, *me); { me->totselect = BLI_listbase_count(&(bm->selected)); @@ -1144,7 +1201,7 @@ void BM_mesh_bm_to_me(Main *bmain, BMesh *bm, Mesh *me, const struct BMeshToMesh } if (me->key) { - bm_to_mesh_shape(bm, me->key, me->mvert, params->active_shapekey_to_mvert); + bm_to_mesh_shape(bm, me->key, mvert, params->active_shapekey_to_mvert); } /* Run this even when shape keys aren't used since it may be used for hooks or vertex parents. */ @@ -1177,10 +1234,10 @@ void BM_mesh_bm_to_me_for_eval(BMesh *bm, Mesh *me, const CustomData_MeshMasks * me->totloop = bm->totloop; me->totpoly = bm->totface; - CustomData_add_layer(&me->vdata, CD_MVERT, CD_CALLOC, nullptr, bm->totvert); - CustomData_add_layer(&me->edata, CD_MEDGE, CD_CALLOC, nullptr, bm->totedge); - CustomData_add_layer(&me->ldata, CD_MLOOP, CD_CALLOC, nullptr, bm->totloop); - CustomData_add_layer(&me->pdata, CD_MPOLY, CD_CALLOC, nullptr, bm->totface); + CustomData_add_layer(&me->vdata, CD_MVERT, CD_SET_DEFAULT, nullptr, bm->totvert); + CustomData_add_layer(&me->edata, CD_MEDGE, CD_SET_DEFAULT, nullptr, bm->totedge); + CustomData_add_layer(&me->ldata, CD_MLOOP, CD_SET_DEFAULT, nullptr, bm->totloop); + CustomData_add_layer(&me->pdata, CD_MPOLY, CD_SET_DEFAULT, nullptr, bm->totface); /* Don't process shape-keys, we only feed them through the modifier stack as needed, * e.g. for applying modifiers or the like. */ @@ -1189,27 +1246,29 @@ void BM_mesh_bm_to_me_for_eval(BMesh *bm, Mesh *me, const CustomData_MeshMasks * CustomData_MeshMasks_update(&mask, cd_mask_extra); } mask.vmask &= ~CD_MASK_SHAPEKEY; - CustomData_merge(&bm->vdata, &me->vdata, mask.vmask, CD_CALLOC, me->totvert); - CustomData_merge(&bm->edata, &me->edata, mask.emask, CD_CALLOC, me->totedge); - CustomData_merge(&bm->ldata, &me->ldata, mask.lmask, CD_CALLOC, me->totloop); - CustomData_merge(&bm->pdata, &me->pdata, mask.pmask, CD_CALLOC, me->totpoly); - - BKE_mesh_update_customdata_pointers(me, false); + CustomData_merge(&bm->vdata, &me->vdata, mask.vmask, CD_SET_DEFAULT, me->totvert); + CustomData_merge(&bm->edata, &me->edata, mask.emask, CD_SET_DEFAULT, me->totedge); + CustomData_merge(&bm->ldata, &me->ldata, mask.lmask, CD_SET_DEFAULT, me->totloop); + CustomData_merge(&bm->pdata, &me->pdata, mask.pmask, CD_SET_DEFAULT, me->totpoly); BMIter iter; BMVert *eve; BMEdge *eed; BMFace *efa; - MVert *mvert = me->mvert; - MEdge *medge = me->medge; - MLoop *mloop = me->mloop; - MPoly *mpoly = me->mpoly; + MutableSpan<MVert> mvert = me->verts_for_write(); + MutableSpan<MEdge> medge = me->edges_for_write(); + MutableSpan<MPoly> mpoly = me->polys_for_write(); + MutableSpan<MLoop> loops = me->loops_for_write(); + MLoop *mloop = loops.data(); unsigned int i, j; - const int cd_vert_bweight_offset = CustomData_get_offset(&bm->vdata, CD_BWEIGHT); - const int cd_edge_bweight_offset = CustomData_get_offset(&bm->edata, CD_BWEIGHT); const int cd_edge_crease_offset = CustomData_get_offset(&bm->edata, CD_CREASE); + bool need_hide_vert = false; + bool need_hide_edge = false; + bool need_hide_poly = false; + bool need_material_index = false; + /* Clear normals on the mesh completely, since the original vertex and polygon count might be * different than the BMesh's. */ BKE_mesh_clear_derived_normals(me); @@ -1224,9 +1283,8 @@ void BM_mesh_bm_to_me_for_eval(BMesh *bm, Mesh *me, const CustomData_MeshMasks * BM_elem_index_set(eve, i); /* set_inline */ mv->flag = BM_vert_flag_to_mflag(eve); - - if (cd_vert_bweight_offset != -1) { - mv->bweight = BM_ELEM_CD_GET_FLOAT_AS_UCHAR(eve, cd_vert_bweight_offset); + if (BM_elem_flag_test(eve, BM_ELEM_HIDDEN)) { + need_hide_vert = true; } CustomData_from_bmesh_block(&bm->vdata, &me->vdata, eve->head.data, i); @@ -1242,6 +1300,9 @@ void BM_mesh_bm_to_me_for_eval(BMesh *bm, Mesh *me, const CustomData_MeshMasks * med->v2 = BM_elem_index_get(eed->v2); med->flag = BM_edge_flag_to_mflag(eed); + if (BM_elem_flag_test(eed, BM_ELEM_HIDDEN)) { + need_hide_edge = true; + } /* Handle this differently to editmode switching, * only enable draw for single user edges rather than calculating angle. */ @@ -1254,9 +1315,6 @@ void BM_mesh_bm_to_me_for_eval(BMesh *bm, Mesh *me, const CustomData_MeshMasks * if (cd_edge_crease_offset != -1) { med->crease = BM_ELEM_CD_GET_FLOAT_AS_UCHAR(eed, cd_edge_crease_offset); } - if (cd_edge_bweight_offset != -1) { - med->bweight = BM_ELEM_CD_GET_FLOAT_AS_UCHAR(eed, cd_edge_bweight_offset); - } CustomData_from_bmesh_block(&bm->edata, &me->edata, eed->head.data, i); } @@ -1272,8 +1330,14 @@ void BM_mesh_bm_to_me_for_eval(BMesh *bm, Mesh *me, const CustomData_MeshMasks * mp->totloop = efa->len; mp->flag = BM_face_flag_to_mflag(efa); + if (BM_elem_flag_test(efa, BM_ELEM_HIDDEN)) { + need_hide_poly = true; + } + mp->loopstart = j; - mp->mat_nr = efa->mat_nr; + if (efa->mat_nr != 0) { + need_material_index = true; + } l_iter = l_first = BM_FACE_FIRST_LOOP(efa); do { @@ -1291,5 +1355,16 @@ void BM_mesh_bm_to_me_for_eval(BMesh *bm, Mesh *me, const CustomData_MeshMasks * } bm->elem_index_dirty &= ~(BM_FACE | BM_LOOP); + if (need_material_index) { + BM_mesh_elem_table_ensure(bm, BM_FACE); + write_fn_to_attribute<int>( + me->attributes_for_write(), "material_index", ATTR_DOMAIN_FACE, true, [&](const int i) { + return static_cast<int>(BM_face_at_index(bm, i)->mat_nr); + }); + } + + convert_bmesh_hide_flags_to_mesh_attributes( + *bm, need_hide_vert, need_hide_edge, need_hide_poly, *me); + me->cd_flag = BM_mesh_cd_flag_from_bmesh(bm); } diff --git a/source/blender/bmesh/intern/bmesh_mesh_convert.h b/source/blender/bmesh/intern/bmesh_mesh_convert.h index e2871dc04d3..a04136afc1d 100644 --- a/source/blender/bmesh/intern/bmesh_mesh_convert.h +++ b/source/blender/bmesh/intern/bmesh_mesh_convert.h @@ -61,8 +61,8 @@ struct BMeshToMeshParams { bool active_shapekey_to_mvert; struct CustomData_MeshMasks cd_mask_extra; }; + /** - * * \param bmain: May be NULL in case \a calc_object_remap parameter option is not set. */ void BM_mesh_bm_to_me(struct Main *bmain, diff --git a/source/blender/bmesh/intern/bmesh_mesh_tessellate.c b/source/blender/bmesh/intern/bmesh_mesh_tessellate.c index 8d401ae1e5c..69510263ec9 100644 --- a/source/blender/bmesh/intern/bmesh_mesh_tessellate.c +++ b/source/blender/bmesh/intern/bmesh_mesh_tessellate.c @@ -6,7 +6,7 @@ * This file contains code for polygon tessellation * (creating triangles from polygons). * - * \see mesh_tessellate.c for the #Mesh equivalent of this file. + * \see mesh_tessellate.cc for the #Mesh equivalent of this file. */ #include "DNA_meshdata_types.h" diff --git a/source/blender/bmesh/intern/bmesh_query.h b/source/blender/bmesh/intern/bmesh_query.h index 85eadd3076a..9d690395d72 100644 --- a/source/blender/bmesh/intern/bmesh_query.h +++ b/source/blender/bmesh/intern/bmesh_query.h @@ -138,7 +138,6 @@ BMLoop *BM_face_other_vert_loop(BMFace *f, BMVert *v_prev, BMVert *v) ATTR_WARN_ * +----------+ <-- This loop defines the face and vertex.. * l * </pre> - * */ BMLoop *BM_loop_other_vert_loop_by_edge(BMLoop *l, BMEdge *e) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); diff --git a/source/blender/bmesh/intern/bmesh_query_uv.c b/source/blender/bmesh/intern/bmesh_query_uv.cc index 1225543cd06..5a725407c6b 100644 --- a/source/blender/bmesh/intern/bmesh_query_uv.c +++ b/source/blender/bmesh/intern/bmesh_query_uv.cc @@ -6,9 +6,10 @@ #include "MEM_guardedalloc.h" -#include "BLI_alloca.h" +#include "BLI_array.hh" #include "BLI_linklist.h" #include "BLI_math.h" +#include "BLI_math_vec_types.hh" #include "BLI_utildefines_stack.h" #include "BKE_customdata.h" @@ -80,7 +81,7 @@ void BM_face_uv_calc_center_median(const BMFace *f, const int cd_loop_uv_offset, zero_v2(r_cent); l_iter = l_first = BM_FACE_FIRST_LOOP(f); do { - const MLoopUV *luv = BM_ELEM_CD_GET_VOID_P(l_iter, cd_loop_uv_offset); + const MLoopUV *luv = (const MLoopUV *)BM_ELEM_CD_GET_VOID_P(l_iter, cd_loop_uv_offset); add_v2_v2(r_cent, luv->uv); } while ((l_iter = l_iter->next) != l_first); @@ -89,16 +90,16 @@ void BM_face_uv_calc_center_median(const BMFace *f, const int cd_loop_uv_offset, float BM_face_uv_calc_cross(const BMFace *f, const int cd_loop_uv_offset) { - float(*uvs)[2] = BLI_array_alloca(uvs, f->len); + blender::Array<blender::float2, BM_DEFAULT_NGON_STACK_SIZE> uvs(f->len); const BMLoop *l_iter; const BMLoop *l_first; int i = 0; l_iter = l_first = BM_FACE_FIRST_LOOP(f); do { - const MLoopUV *luv = BM_ELEM_CD_GET_VOID_P(l_iter, cd_loop_uv_offset); - copy_v2_v2(uvs[i++], luv->uv); + const MLoopUV *luv = (const MLoopUV *)BM_ELEM_CD_GET_VOID_P(l_iter, cd_loop_uv_offset); + uvs[i++] = luv->uv; } while ((l_iter = l_iter->next) != l_first); - return cross_poly_v2(uvs, f->len); + return cross_poly_v2(reinterpret_cast<const float(*)[2]>(uvs.data()), f->len); } void BM_face_uv_minmax(const BMFace *f, float min[2], float max[2], const int cd_loop_uv_offset) @@ -107,7 +108,7 @@ void BM_face_uv_minmax(const BMFace *f, float min[2], float max[2], const int cd const BMLoop *l_first; l_iter = l_first = BM_FACE_FIRST_LOOP(f); do { - const MLoopUV *luv = BM_ELEM_CD_GET_VOID_P(l_iter, cd_loop_uv_offset); + const MLoopUV *luv = (const MLoopUV *)BM_ELEM_CD_GET_VOID_P(l_iter, cd_loop_uv_offset); minmax_v2v2_v2(min, max, luv->uv); } while ((l_iter = l_iter->next) != l_first); } @@ -118,7 +119,7 @@ void BM_face_uv_transform(BMFace *f, const float matrix[2][2], const int cd_loop BMLoop *l_first; l_iter = l_first = BM_FACE_FIRST_LOOP(f); do { - MLoopUV *luv = BM_ELEM_CD_GET_VOID_P(l_iter, cd_loop_uv_offset); + MLoopUV *luv = (MLoopUV *)BM_ELEM_CD_GET_VOID_P(l_iter, cd_loop_uv_offset); mul_m2_v2(matrix, luv->uv); } while ((l_iter = l_iter->next) != l_first); } @@ -126,12 +127,12 @@ void BM_face_uv_transform(BMFace *f, const float matrix[2][2], const int cd_loop bool BM_loop_uv_share_edge_check(BMLoop *l_a, BMLoop *l_b, const int cd_loop_uv_offset) { BLI_assert(l_a->e == l_b->e); - MLoopUV *luv_a_curr = BM_ELEM_CD_GET_VOID_P(l_a, cd_loop_uv_offset); - MLoopUV *luv_a_next = BM_ELEM_CD_GET_VOID_P(l_a->next, cd_loop_uv_offset); - MLoopUV *luv_b_curr = BM_ELEM_CD_GET_VOID_P(l_b, cd_loop_uv_offset); - MLoopUV *luv_b_next = BM_ELEM_CD_GET_VOID_P(l_b->next, cd_loop_uv_offset); + MLoopUV *luv_a_curr = (MLoopUV *)BM_ELEM_CD_GET_VOID_P(l_a, cd_loop_uv_offset); + MLoopUV *luv_a_next = (MLoopUV *)BM_ELEM_CD_GET_VOID_P(l_a->next, cd_loop_uv_offset); + MLoopUV *luv_b_curr = (MLoopUV *)BM_ELEM_CD_GET_VOID_P(l_b, cd_loop_uv_offset); + MLoopUV *luv_b_next = (MLoopUV *)BM_ELEM_CD_GET_VOID_P(l_b->next, cd_loop_uv_offset); if (l_a->v != l_b->v) { - SWAP(MLoopUV *, luv_b_curr, luv_b_next); + std::swap(luv_b_curr, luv_b_next); } return (equals_v2v2(luv_a_curr->uv, luv_b_curr->uv) && equals_v2v2(luv_a_next->uv, luv_b_next->uv)); @@ -140,8 +141,8 @@ bool BM_loop_uv_share_edge_check(BMLoop *l_a, BMLoop *l_b, const int cd_loop_uv_ bool BM_loop_uv_share_vert_check(BMLoop *l_a, BMLoop *l_b, const int cd_loop_uv_offset) { BLI_assert(l_a->v == l_b->v); - const MLoopUV *luv_a = BM_ELEM_CD_GET_VOID_P(l_a, cd_loop_uv_offset); - const MLoopUV *luv_b = BM_ELEM_CD_GET_VOID_P(l_b, cd_loop_uv_offset); + const MLoopUV *luv_a = (const MLoopUV *)BM_ELEM_CD_GET_VOID_P(l_a, cd_loop_uv_offset); + const MLoopUV *luv_b = (const MLoopUV *)BM_ELEM_CD_GET_VOID_P(l_b, cd_loop_uv_offset); if (!equals_v2v2(luv_a->uv, luv_b->uv)) { return false; } @@ -160,8 +161,10 @@ bool BM_edge_uv_share_vert_check(BMEdge *e, BMLoop *l_a, BMLoop *l_b, const int const BMLoop *l_other_b = BM_loop_other_vert_loop_by_edge(l_b, e); { - const MLoopUV *luv_other_a = BM_ELEM_CD_GET_VOID_P(l_other_a, cd_loop_uv_offset); - const MLoopUV *luv_other_b = BM_ELEM_CD_GET_VOID_P(l_other_b, cd_loop_uv_offset); + const MLoopUV *luv_other_a = (const MLoopUV *)BM_ELEM_CD_GET_VOID_P(l_other_a, + cd_loop_uv_offset); + const MLoopUV *luv_other_b = (const MLoopUV *)BM_ELEM_CD_GET_VOID_P(l_other_b, + cd_loop_uv_offset); if (!equals_v2v2(luv_other_a->uv, luv_other_b->uv)) { return false; } @@ -172,7 +175,7 @@ bool BM_edge_uv_share_vert_check(BMEdge *e, BMLoop *l_a, BMLoop *l_b, const int bool BM_face_uv_point_inside_test(const BMFace *f, const float co[2], const int cd_loop_uv_offset) { - float(*projverts)[2] = BLI_array_alloca(projverts, f->len); + blender::Array<blender::float2, BM_DEFAULT_NGON_STACK_SIZE> projverts(f->len); BMLoop *l_iter; int i; @@ -180,8 +183,9 @@ bool BM_face_uv_point_inside_test(const BMFace *f, const float co[2], const int BLI_assert(BM_face_is_normal_valid(f)); for (i = 0, l_iter = BM_FACE_FIRST_LOOP(f); i < f->len; i++, l_iter = l_iter->next) { - copy_v2_v2(projverts[i], BM_ELEM_CD_GET_VOID_P(l_iter, cd_loop_uv_offset)); + projverts[i] = ((const MLoopUV *)BM_ELEM_CD_GET_VOID_P(l_iter, cd_loop_uv_offset))->uv; } - return isect_point_poly_v2(co, projverts, f->len, false); + return isect_point_poly_v2( + co, reinterpret_cast<const float(*)[2]>(projverts.data()), f->len, false); } diff --git a/source/blender/bmesh/intern/bmesh_structure.c b/source/blender/bmesh/intern/bmesh_structure.c index 6baaeb43f1a..7d340f02f2e 100644 --- a/source/blender/bmesh/intern/bmesh_structure.c +++ b/source/blender/bmesh/intern/bmesh_structure.c @@ -182,9 +182,8 @@ void bmesh_disk_edge_remove(BMEdge *e, BMVert *v) BMEdge *bmesh_disk_edge_exists(const BMVert *v1, const BMVert *v2) { - BMEdge *e_iter, *e_first; - if (v1->e) { + BMEdge *e_iter, *e_first; e_first = e_iter = v1->e; do { |