Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--source/blender/modifiers/CMakeLists.txt2
-rw-r--r--source/blender/modifiers/intern/MOD_mask.c388
-rw-r--r--source/blender/modifiers/intern/MOD_mask.cc417
3 files changed, 418 insertions, 389 deletions
diff --git a/source/blender/modifiers/CMakeLists.txt b/source/blender/modifiers/CMakeLists.txt
index cec7ddb4b68..3e340461673 100644
--- a/source/blender/modifiers/CMakeLists.txt
+++ b/source/blender/modifiers/CMakeLists.txt
@@ -59,7 +59,7 @@ set(SRC
intern/MOD_laplaciandeform.c
intern/MOD_laplaciansmooth.c
intern/MOD_lattice.c
- intern/MOD_mask.c
+ intern/MOD_mask.cc
intern/MOD_meshcache.c
intern/MOD_meshcache_mdd.c
intern/MOD_meshcache_pc2.c
diff --git a/source/blender/modifiers/intern/MOD_mask.c b/source/blender/modifiers/intern/MOD_mask.c
deleted file mode 100644
index 00b0068bd11..00000000000
--- a/source/blender/modifiers/intern/MOD_mask.c
+++ /dev/null
@@ -1,388 +0,0 @@
-/*
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- *
- * The Original Code is Copyright (C) 2005 by the Blender Foundation.
- * All rights reserved.
- */
-
-/** \file
- * \ingroup modifiers
- */
-
-#include "MEM_guardedalloc.h"
-
-#include "BLI_utildefines.h"
-
-#include "BLI_listbase.h"
-#include "BLI_ghash.h"
-
-#include "DNA_armature_types.h"
-#include "DNA_mesh_types.h"
-#include "DNA_meshdata_types.h"
-#include "DNA_modifier_types.h"
-#include "DNA_object_types.h"
-
-#include "BKE_action.h" /* BKE_pose_channel_find_name */
-#include "BKE_customdata.h"
-#include "BKE_lib_query.h"
-#include "BKE_mesh.h"
-#include "BKE_modifier.h"
-#include "BKE_deform.h"
-
-#include "DEG_depsgraph_build.h"
-#include "DEG_depsgraph_query.h"
-
-#include "MOD_modifiertypes.h"
-
-#include "BLI_strict_flags.h"
-
-static void requiredDataMask(Object *UNUSED(ob),
- ModifierData *UNUSED(md),
- CustomData_MeshMasks *r_cddata_masks)
-{
- r_cddata_masks->vmask |= CD_MASK_MDEFORMVERT;
-}
-
-static void foreachObjectLink(ModifierData *md, Object *ob, ObjectWalkFunc walk, void *userData)
-{
- MaskModifierData *mmd = (MaskModifierData *)md;
- walk(userData, ob, &mmd->ob_arm, IDWALK_CB_NOP);
-}
-
-static void updateDepsgraph(ModifierData *md, const ModifierUpdateDepsgraphContext *ctx)
-{
- MaskModifierData *mmd = (MaskModifierData *)md;
- if (mmd->ob_arm) {
- bArmature *arm = (bArmature *)mmd->ob_arm->data;
- /* Tag relationship in depsgraph, but also on the armature. */
- /* TODO(sergey): Is it a proper relation here? */
- DEG_add_object_relation(ctx->node, mmd->ob_arm, DEG_OB_COMP_TRANSFORM, "Mask Modifier");
- arm->flag |= ARM_HAS_VIZ_DEPS;
- DEG_add_modifier_to_transform_relation(ctx->node, "Mask Modifier");
- }
-}
-
-static Mesh *applyModifier(ModifierData *md, const ModifierEvalContext *ctx, Mesh *mesh)
-{
- MaskModifierData *mmd = (MaskModifierData *)md;
- Object *ob = ctx->object;
- const bool found_test = (mmd->flag & MOD_MASK_INV) == 0;
- Mesh *result = NULL;
- GHash *vertHash = NULL, *edgeHash, *polyHash;
- GHashIterator gh_iter;
- MDeformVert *dvert, *dv;
- int numPolys = 0, numLoops = 0, numEdges = 0, numVerts = 0;
- int maxVerts, maxEdges, maxPolys;
- int i;
-
- const MVert *mvert_src;
- const MEdge *medge_src;
- const MPoly *mpoly_src;
- const MLoop *mloop_src;
-
- MPoly *mpoly_dst;
- MLoop *mloop_dst;
- MEdge *medge_dst;
- MVert *mvert_dst;
-
- int *loop_mapping;
-
- dvert = CustomData_get_layer(&mesh->vdata, CD_MDEFORMVERT);
- if (dvert == NULL) {
- return found_test ? BKE_mesh_new_nomain_from_template(mesh, 0, 0, 0, 0, 0) : mesh;
- }
-
- /* Overview of Method:
- * 1. Get the vertices that are in the vertexgroup of interest.
- * 2. Filter out unwanted geometry (i.e. not in vertexgroup),
- * by populating mappings with new vs old indices.
- * 3. Make a new mesh containing only the mapping data.
- */
-
- /* get original number of verts, edges, and faces */
- maxVerts = mesh->totvert;
- maxEdges = mesh->totedge;
- maxPolys = mesh->totpoly;
-
- /* check if we can just return the original mesh
- * - must have verts and therefore verts assigned to vgroups to do anything useful
- */
- if (!(ELEM(mmd->mode, MOD_MASK_MODE_ARM, MOD_MASK_MODE_VGROUP)) || (maxVerts == 0) ||
- BLI_listbase_is_empty(&ob->defbase)) {
- return mesh;
- }
-
- /* if mode is to use selected armature bones, aggregate the bone groups */
- if (mmd->mode == MOD_MASK_MODE_ARM) { /* --- using selected bones --- */
- Object *oba = mmd->ob_arm;
- bPoseChannel *pchan;
- bDeformGroup *def;
- bool *bone_select_array;
- int bone_select_tot = 0;
- const uint defbase_tot = (uint)BLI_listbase_count(&ob->defbase);
-
- /* check that there is armature object with bones to use, otherwise return original mesh */
- if (ELEM(NULL, oba, oba->pose, ob->defbase.first)) {
- return mesh;
- }
-
- /* Determine whether each vertex-group is associated with a selected bone or not:
- * - Each cell is a boolean saying whether bone corresponding to the i'th group selected.
- * - Groups that don't match a bone are treated as not existing
- * (along with the corresponding un-grouped verts).
- */
- bone_select_array = MEM_malloc_arrayN((size_t)defbase_tot, sizeof(char), "mask array");
-
- for (i = 0, def = ob->defbase.first; def; def = def->next, i++) {
- pchan = BKE_pose_channel_find_name(oba->pose, def->name);
- if (pchan && pchan->bone && (pchan->bone->flag & BONE_SELECTED)) {
- bone_select_array[i] = true;
- bone_select_tot++;
- }
- else {
- bone_select_array[i] = false;
- }
- }
-
- /* verthash gives mapping from original vertex indices to the new indices
- * (including selected matches only):
- * key = oldindex, value = newindex
- */
- vertHash = BLI_ghash_int_new_ex("mask vert gh", (uint)maxVerts);
-
- /* add vertices which exist in vertexgroups into vertHash for filtering
- * - dv = for each vertex, what vertexgroups does it belong to
- * - dw = weight that vertex was assigned to a vertexgroup it belongs to
- */
- for (i = 0, dv = dvert; i < maxVerts; i++, dv++) {
- MDeformWeight *dw = dv->dw;
- bool found = false;
- int j;
-
- /* check the groups that vertex is assigned to, and see if it was any use */
- for (j = 0; j < dv->totweight; j++, dw++) {
- if (dw->def_nr < defbase_tot) {
- if (bone_select_array[dw->def_nr]) {
- if (dw->weight > mmd->threshold) {
- found = true;
- break;
- }
- }
- }
- }
-
- if (found_test != found) {
- continue;
- }
-
- /* add to ghash for verts (numVerts acts as counter for mapping) */
- BLI_ghash_insert(vertHash, POINTER_FROM_INT(i), POINTER_FROM_INT(numVerts));
- numVerts++;
- }
-
- /* free temp hashes */
- MEM_freeN(bone_select_array);
- }
- else { /* --- Using Nominated VertexGroup only --- */
- int defgrp_index = defgroup_name_index(ob, mmd->vgroup);
-
- /* if no vgroup (i.e. dverts) found, return the initial mesh */
- if (defgrp_index == -1) {
- return mesh;
- }
-
- /* hashes for quickly providing a mapping from old to new - use key=oldindex, value=newindex */
- vertHash = BLI_ghash_int_new_ex("mask vert2 bh", (uint)maxVerts);
-
- /* add vertices which exist in vertexgroup into ghash for filtering */
- for (i = 0, dv = dvert; i < maxVerts; i++, dv++) {
- const bool found = defvert_find_weight(dv, defgrp_index) > mmd->threshold;
- if (found_test != found) {
- continue;
- }
-
- /* add to ghash for verts (numVerts acts as counter for mapping) */
- BLI_ghash_insert(vertHash, POINTER_FROM_INT(i), POINTER_FROM_INT(numVerts));
- numVerts++;
- }
- }
-
- /* hashes for quickly providing a mapping from old to new - use key=oldindex, value=newindex */
- edgeHash = BLI_ghash_int_new_ex("mask ed2 gh", (uint)maxEdges);
- polyHash = BLI_ghash_int_new_ex("mask fa2 gh", (uint)maxPolys);
-
- mvert_src = mesh->mvert;
- medge_src = mesh->medge;
- mpoly_src = mesh->mpoly;
- mloop_src = mesh->mloop;
-
- /* overalloc, assume all polys are seen */
- loop_mapping = MEM_malloc_arrayN((size_t)maxPolys, sizeof(int), "mask loopmap");
-
- /* loop over edges and faces, and do the same thing to
- * ensure that they only reference existing verts
- */
- for (i = 0; i < maxEdges; i++) {
- const MEdge *me = &medge_src[i];
-
- /* only add if both verts will be in new mesh */
- if (BLI_ghash_haskey(vertHash, POINTER_FROM_INT(me->v1)) &&
- BLI_ghash_haskey(vertHash, POINTER_FROM_INT(me->v2))) {
- BLI_ghash_insert(edgeHash, POINTER_FROM_INT(i), POINTER_FROM_INT(numEdges));
- numEdges++;
- }
- }
- for (i = 0; i < maxPolys; i++) {
- const MPoly *mp_src = &mpoly_src[i];
- const MLoop *ml_src = &mloop_src[mp_src->loopstart];
- bool ok = true;
- int j;
-
- for (j = 0; j < mp_src->totloop; j++, ml_src++) {
- if (!BLI_ghash_haskey(vertHash, POINTER_FROM_INT(ml_src->v))) {
- ok = false;
- break;
- }
- }
-
- /* all verts must be available */
- if (ok) {
- BLI_ghash_insert(polyHash, POINTER_FROM_INT(i), POINTER_FROM_INT(numPolys));
- loop_mapping[numPolys] = numLoops;
- numPolys++;
- numLoops += mp_src->totloop;
- }
- }
-
- /* now we know the number of verts, edges and faces,
- * we can create the new (reduced) mesh
- */
- result = BKE_mesh_new_nomain_from_template(mesh, numVerts, numEdges, 0, numLoops, numPolys);
-
- mpoly_dst = result->mpoly;
- mloop_dst = result->mloop;
- medge_dst = result->medge;
- mvert_dst = result->mvert;
-
- /* using ghash-iterators, map data into new mesh */
- /* vertices */
- GHASH_ITER (gh_iter, vertHash) {
- const MVert *v_src;
- MVert *v_dst;
- const int i_src = POINTER_AS_INT(BLI_ghashIterator_getKey(&gh_iter));
- const int i_dst = POINTER_AS_INT(BLI_ghashIterator_getValue(&gh_iter));
-
- v_src = &mvert_src[i_src];
- v_dst = &mvert_dst[i_dst];
-
- *v_dst = *v_src;
- CustomData_copy_data(&mesh->vdata, &result->vdata, i_src, i_dst, 1);
- }
-
- /* edges */
- GHASH_ITER (gh_iter, edgeHash) {
- const MEdge *e_src;
- MEdge *e_dst;
- const int i_src = POINTER_AS_INT(BLI_ghashIterator_getKey(&gh_iter));
- const int i_dst = POINTER_AS_INT(BLI_ghashIterator_getValue(&gh_iter));
-
- e_src = &medge_src[i_src];
- e_dst = &medge_dst[i_dst];
-
- CustomData_copy_data(&mesh->edata, &result->edata, i_src, i_dst, 1);
- *e_dst = *e_src;
- e_dst->v1 = POINTER_AS_UINT(BLI_ghash_lookup(vertHash, POINTER_FROM_UINT(e_src->v1)));
- e_dst->v2 = POINTER_AS_UINT(BLI_ghash_lookup(vertHash, POINTER_FROM_UINT(e_src->v2)));
- }
-
- /* faces */
- GHASH_ITER (gh_iter, polyHash) {
- const int i_src = POINTER_AS_INT(BLI_ghashIterator_getKey(&gh_iter));
- const int i_dst = POINTER_AS_INT(BLI_ghashIterator_getValue(&gh_iter));
- const MPoly *mp_src = &mpoly_src[i_src];
- MPoly *mp_dst = &mpoly_dst[i_dst];
- const int i_ml_src = mp_src->loopstart;
- const int i_ml_dst = loop_mapping[i_dst];
- const MLoop *ml_src = &mloop_src[i_ml_src];
- MLoop *ml_dst = &mloop_dst[i_ml_dst];
-
- CustomData_copy_data(&mesh->pdata, &result->pdata, i_src, i_dst, 1);
- CustomData_copy_data(&mesh->ldata, &result->ldata, i_ml_src, i_ml_dst, mp_src->totloop);
-
- *mp_dst = *mp_src;
- mp_dst->loopstart = i_ml_dst;
- for (i = 0; i < mp_src->totloop; i++) {
- ml_dst[i].v = POINTER_AS_UINT(BLI_ghash_lookup(vertHash, POINTER_FROM_UINT(ml_src[i].v)));
- ml_dst[i].e = POINTER_AS_UINT(BLI_ghash_lookup(edgeHash, POINTER_FROM_UINT(ml_src[i].e)));
- }
- }
-
- MEM_freeN(loop_mapping);
-
- /* why is this needed? - campbell */
- /* recalculate normals */
- result->runtime.cd_dirty_vert |= CD_MASK_NORMAL;
-
- /* free hashes */
- BLI_ghash_free(vertHash, NULL, NULL);
- BLI_ghash_free(edgeHash, NULL, NULL);
- BLI_ghash_free(polyHash, NULL, NULL);
-
- /* return the new mesh */
- return result;
-}
-
-static bool isDisabled(const struct Scene *UNUSED(scene),
- ModifierData *md,
- bool UNUSED(useRenderParams))
-{
- MaskModifierData *mmd = (MaskModifierData *)md;
-
- /* The object type check is only needed here in case we have a placeholder
- * object assigned (because the library containing the armature is missing).
- *
- * In other cases it should be impossible to have a type mismatch.
- */
- return mmd->ob_arm && mmd->ob_arm->type != OB_ARMATURE;
-}
-
-ModifierTypeInfo modifierType_Mask = {
- /* name */ "Mask",
- /* structName */ "MaskModifierData",
- /* structSize */ sizeof(MaskModifierData),
- /* type */ eModifierTypeType_Nonconstructive,
- /* flags */ eModifierTypeFlag_AcceptsMesh | eModifierTypeFlag_SupportsMapping |
- eModifierTypeFlag_SupportsEditmode,
-
- /* copyData */ modifier_copyData_generic,
-
- /* deformVerts */ NULL,
- /* deformMatrices */ NULL,
- /* deformVertsEM */ NULL,
- /* deformMatricesEM */ NULL,
- /* applyModifier */ applyModifier,
-
- /* initData */ NULL,
- /* requiredDataMask */ requiredDataMask,
- /* freeData */ NULL,
- /* isDisabled */ isDisabled,
- /* updateDepsgraph */ updateDepsgraph,
- /* dependsOnTime */ NULL,
- /* dependsOnNormals */ NULL,
- /* foreachObjectLink */ foreachObjectLink,
- /* foreachIDLink */ NULL,
- /* foreachTexLink */ NULL,
- /* freeRuntimeData */ NULL,
-};
diff --git a/source/blender/modifiers/intern/MOD_mask.cc b/source/blender/modifiers/intern/MOD_mask.cc
new file mode 100644
index 00000000000..fb179d2e485
--- /dev/null
+++ b/source/blender/modifiers/intern/MOD_mask.cc
@@ -0,0 +1,417 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * The Original Code is Copyright (C) 2005 by the Blender Foundation.
+ * All rights reserved.
+ */
+
+/** \file
+ * \ingroup modifiers
+ */
+
+#include "MEM_guardedalloc.h"
+
+#include "BLI_utildefines.h"
+
+#include "BLI_listbase.h"
+#include "BLI_ghash.h"
+
+#include "DNA_armature_types.h"
+#include "DNA_mesh_types.h"
+#include "DNA_meshdata_types.h"
+#include "DNA_modifier_types.h"
+#include "DNA_object_types.h"
+
+#include "BKE_action.h" /* BKE_pose_channel_find_name */
+#include "BKE_customdata.h"
+#include "BKE_lib_query.h"
+#include "BKE_mesh.h"
+#include "BKE_modifier.h"
+#include "BKE_deform.h"
+
+#include "DEG_depsgraph_build.h"
+#include "DEG_depsgraph_query.h"
+
+#include "MOD_modifiertypes.h"
+
+#include "BLI_array_cxx.h"
+#include "BLI_vector.h"
+#include "BLI_listbase_wrapper.h"
+
+using BLI::Array;
+using BLI::ArrayRef;
+using BLI::IndexRange;
+using BLI::IntrusiveListBaseWrapper;
+using BLI::MutableArrayRef;
+using BLI::Vector;
+
+static void requiredDataMask(Object *UNUSED(ob),
+ ModifierData *UNUSED(md),
+ CustomData_MeshMasks *r_cddata_masks)
+{
+ r_cddata_masks->vmask |= CD_MASK_MDEFORMVERT;
+}
+
+static void foreachObjectLink(ModifierData *md, Object *ob, ObjectWalkFunc walk, void *userData)
+{
+ MaskModifierData *mmd = (MaskModifierData *)md;
+ walk(userData, ob, &mmd->ob_arm, IDWALK_CB_NOP);
+}
+
+static void updateDepsgraph(ModifierData *md, const ModifierUpdateDepsgraphContext *ctx)
+{
+ MaskModifierData *mmd = (MaskModifierData *)md;
+ if (mmd->ob_arm) {
+ bArmature *arm = (bArmature *)mmd->ob_arm->data;
+ /* Tag relationship in depsgraph, but also on the armature. */
+ /* TODO(sergey): Is it a proper relation here? */
+ DEG_add_object_relation(ctx->node, mmd->ob_arm, DEG_OB_COMP_TRANSFORM, "Mask Modifier");
+ arm->flag |= ARM_HAS_VIZ_DEPS;
+ DEG_add_modifier_to_transform_relation(ctx->node, "Mask Modifier");
+ }
+}
+
+/* A vertex will be in the mask if a selected bone influences it more than a certain threshold. */
+static void compute_vertex_mask__armature_mode(MDeformVert *dvert,
+ Object *ob,
+ Object *armature_ob,
+ float threshold,
+ MutableArrayRef<bool> r_vertex_mask)
+{
+ /* Element i is true if there is a selected bone that uses vertex group i. */
+ Vector<bool> selected_bone_uses_group;
+
+ for (bDeformGroup *def : IntrusiveListBaseWrapper<bDeformGroup>(ob->defbase)) {
+ bPoseChannel *pchan = BKE_pose_channel_find_name(armature_ob->pose, def->name);
+ bool bone_for_group_exists = pchan && pchan->bone && (pchan->bone->flag & BONE_SELECTED);
+ selected_bone_uses_group.append(bone_for_group_exists);
+ }
+
+ ArrayRef<bool> use_vertex_group = selected_bone_uses_group;
+
+ for (int i : r_vertex_mask.index_range()) {
+ ArrayRef<MDeformWeight> weights(dvert[i].dw, dvert[i].totweight);
+ r_vertex_mask[i] = false;
+
+ /* check the groups that vertex is assigned to, and see if it was any use */
+ for (const MDeformWeight &dw : weights) {
+ if (use_vertex_group.get(dw.def_nr, false)) {
+ if (dw.weight > threshold) {
+ r_vertex_mask[i] = true;
+ break;
+ }
+ }
+ }
+ }
+}
+
+/* A vertex will be in the mask if the vertex group influences it more than a certain threshold. */
+static void compute_vertex_mask__vertex_group_mode(MDeformVert *dvert,
+ int defgrp_index,
+ float threshold,
+ MutableArrayRef<bool> r_vertex_mask)
+{
+ for (int i : r_vertex_mask.index_range()) {
+ const bool found = defvert_find_weight(&dvert[i], defgrp_index) > threshold;
+ r_vertex_mask[i] = found;
+ }
+}
+
+static void invert_boolean_array(MutableArrayRef<bool> array)
+{
+ for (bool &value : array) {
+ value = !value;
+ }
+}
+
+static void compute_masked_vertices(ArrayRef<bool> vertex_mask,
+ MutableArrayRef<int> r_vertex_map,
+ uint *r_num_masked_vertices)
+{
+ BLI_assert(vertex_mask.size() == r_vertex_map.size());
+
+ uint num_masked_vertices = 0;
+ for (uint i_src : r_vertex_map.index_range()) {
+ if (vertex_mask[i_src]) {
+ r_vertex_map[i_src] = num_masked_vertices;
+ num_masked_vertices++;
+ }
+ else {
+ r_vertex_map[i_src] = -1;
+ }
+ }
+
+ *r_num_masked_vertices = num_masked_vertices;
+}
+
+static void computed_masked_edges(const Mesh *mesh,
+ ArrayRef<bool> vertex_mask,
+ MutableArrayRef<int> r_edge_map,
+ uint *r_num_masked_edges)
+{
+ BLI_assert(mesh->totedge == r_edge_map.size());
+
+ uint num_masked_edges = 0;
+ for (int i : IndexRange(mesh->totedge)) {
+ const MEdge &edge = mesh->medge[i];
+
+ /* only add if both verts will be in new mesh */
+ if (vertex_mask[edge.v1] && vertex_mask[edge.v2]) {
+ r_edge_map[i] = num_masked_edges;
+ num_masked_edges++;
+ }
+ else {
+ r_edge_map[i] = -1;
+ }
+ }
+
+ *r_num_masked_edges = num_masked_edges;
+}
+
+static void computed_masked_polygons(const Mesh *mesh,
+ ArrayRef<bool> vertex_mask,
+ Vector<int> &r_masked_poly_indices,
+ Vector<int> &r_loop_starts,
+ uint *r_num_masked_polys,
+ uint *r_num_masked_loops)
+{
+ BLI_assert(mesh->totvert == vertex_mask.size());
+
+ r_masked_poly_indices.reserve(mesh->totpoly);
+ r_loop_starts.reserve(mesh->totloop);
+
+ uint num_masked_loops = 0;
+ for (int i : IndexRange(mesh->totpoly)) {
+ const MPoly &poly_src = mesh->mpoly[i];
+
+ bool all_verts_in_mask = true;
+ ArrayRef<MLoop> loops_src(&mesh->mloop[poly_src.loopstart], poly_src.totloop);
+ for (const MLoop &loop : loops_src) {
+ if (!vertex_mask[loop.v]) {
+ all_verts_in_mask = false;
+ break;
+ }
+ }
+
+ if (all_verts_in_mask) {
+ r_masked_poly_indices.append_unchecked(i);
+ r_loop_starts.append_unchecked(num_masked_loops);
+ num_masked_loops += poly_src.totloop;
+ }
+ }
+
+ *r_num_masked_polys = r_masked_poly_indices.size();
+ *r_num_masked_loops = num_masked_loops;
+}
+
+static void copy_masked_vertices_to_new_mesh(const Mesh &src_mesh,
+ Mesh &dst_mesh,
+ ArrayRef<int> vertex_map)
+{
+ BLI_assert(src_mesh.totvert == vertex_map.size());
+ for (const int i_src : vertex_map.index_range()) {
+ const int i_dst = vertex_map[i_src];
+ if (i_dst == -1) {
+ continue;
+ }
+
+ const MVert &v_src = src_mesh.mvert[i_src];
+ MVert &v_dst = dst_mesh.mvert[i_dst];
+
+ v_dst = v_src;
+ CustomData_copy_data(&src_mesh.vdata, &dst_mesh.vdata, i_src, i_dst, 1);
+ }
+}
+
+static void copy_masked_edges_to_new_mesh(const Mesh &src_mesh,
+ Mesh &dst_mesh,
+ ArrayRef<int> vertex_map,
+ ArrayRef<int> edge_map)
+{
+ BLI_assert(src_mesh.totvert == vertex_map.size());
+ BLI_assert(src_mesh.totedge == edge_map.size());
+ for (const int i_src : IndexRange(src_mesh.totedge)) {
+ const int i_dst = edge_map[i_src];
+ if (i_dst == -1) {
+ continue;
+ }
+
+ const MEdge &e_src = src_mesh.medge[i_src];
+ MEdge &e_dst = dst_mesh.medge[i_dst];
+
+ CustomData_copy_data(&src_mesh.edata, &dst_mesh.edata, i_src, i_dst, 1);
+ e_dst = e_src;
+ e_dst.v1 = vertex_map[e_src.v1];
+ e_dst.v2 = vertex_map[e_src.v2];
+ }
+}
+
+static void copy_masked_polys_to_new_mesh(const Mesh &src_mesh,
+ Mesh &dst_mesh,
+ ArrayRef<int> vertex_map,
+ ArrayRef<int> edge_map,
+ ArrayRef<int> masked_poly_indices,
+ ArrayRef<int> new_loop_starts)
+{
+ for (const int i_dst : masked_poly_indices.index_range()) {
+ const int i_src = masked_poly_indices[i_dst];
+
+ const MPoly &mp_src = src_mesh.mpoly[i_src];
+ MPoly &mp_dst = dst_mesh.mpoly[i_dst];
+ const int i_ml_src = mp_src.loopstart;
+ const int i_ml_dst = new_loop_starts[i_dst];
+
+ CustomData_copy_data(&src_mesh.pdata, &dst_mesh.pdata, i_src, i_dst, 1);
+ CustomData_copy_data(&src_mesh.ldata, &dst_mesh.ldata, i_ml_src, i_ml_dst, mp_src.totloop);
+
+ const MLoop *ml_src = src_mesh.mloop + i_ml_src;
+ MLoop *ml_dst = dst_mesh.mloop + i_ml_dst;
+
+ mp_dst = mp_src;
+ mp_dst.loopstart = i_ml_dst;
+ for (int i : IndexRange(mp_src.totloop)) {
+ ml_dst[i].v = vertex_map[ml_src[i].v];
+ ml_dst[i].e = edge_map[ml_src[i].e];
+ }
+ }
+}
+
+/* Components of the algorithm:
+ * 1. Figure out which vertices should be present in the output mesh.
+ * 2. Find edges and polygons only using those vertices.
+ * 3. Create a new mesh that only uses the found vertices, edges and polygons.
+ */
+static Mesh *applyModifier(ModifierData *md, const ModifierEvalContext *ctx, Mesh *mesh)
+{
+ MaskModifierData *mmd = (MaskModifierData *)md;
+ Object *ob = ctx->object;
+ const bool invert_mask = mmd->flag & MOD_MASK_INV;
+
+ /* Return empty or input mesh when there are no vertex groups. */
+ MDeformVert *dvert = (MDeformVert *)CustomData_get_layer(&mesh->vdata, CD_MDEFORMVERT);
+ if (dvert == NULL) {
+ return invert_mask ? mesh : BKE_mesh_new_nomain_from_template(mesh, 0, 0, 0, 0, 0);
+ }
+
+ /* Quick test to see if we can return early. */
+ if (!(ELEM(mmd->mode, MOD_MASK_MODE_ARM, MOD_MASK_MODE_VGROUP)) || (mesh->totvert == 0) ||
+ BLI_listbase_is_empty(&ob->defbase)) {
+ return mesh;
+ }
+
+ Array<bool> vertex_mask;
+ if (mmd->mode == MOD_MASK_MODE_ARM) {
+ Object *armature_ob = mmd->ob_arm;
+
+ /* Return input mesh if there is no armature with bones. */
+ if (ELEM(NULL, armature_ob, armature_ob->pose, ob->defbase.first)) {
+ return mesh;
+ }
+
+ vertex_mask = Array<bool>(mesh->totvert);
+ compute_vertex_mask__armature_mode(dvert, ob, armature_ob, mmd->threshold, vertex_mask);
+ }
+ else {
+ int defgrp_index = defgroup_name_index(ob, mmd->vgroup);
+
+ /* Return input mesh if the vertex group does not exist. */
+ if (defgrp_index == -1) {
+ return mesh;
+ }
+
+ vertex_mask = Array<bool>(mesh->totvert);
+ compute_vertex_mask__vertex_group_mode(dvert, defgrp_index, mmd->threshold, vertex_mask);
+ }
+
+ if (invert_mask) {
+ invert_boolean_array(vertex_mask);
+ }
+
+ Array<int> vertex_map(mesh->totvert);
+ uint num_masked_vertices;
+ compute_masked_vertices(vertex_mask, vertex_map, &num_masked_vertices);
+
+ Array<int> edge_map(mesh->totedge);
+ uint num_masked_edges;
+ computed_masked_edges(mesh, vertex_mask, edge_map, &num_masked_edges);
+
+ Vector<int> masked_poly_indices;
+ Vector<int> new_loop_starts;
+ uint num_masked_polys;
+ uint num_masked_loops;
+ computed_masked_polygons(mesh,
+ vertex_mask,
+ masked_poly_indices,
+ new_loop_starts,
+ &num_masked_polys,
+ &num_masked_loops);
+
+ Mesh *result = BKE_mesh_new_nomain_from_template(
+ mesh, num_masked_vertices, num_masked_edges, 0, num_masked_loops, num_masked_polys);
+
+ copy_masked_vertices_to_new_mesh(*mesh, *result, vertex_map);
+ copy_masked_edges_to_new_mesh(*mesh, *result, vertex_map, edge_map);
+ copy_masked_polys_to_new_mesh(
+ *mesh, *result, vertex_map, edge_map, masked_poly_indices, new_loop_starts);
+
+ /* Tag to recalculate normals later. */
+ result->runtime.cd_dirty_vert |= CD_MASK_NORMAL;
+
+ return result;
+}
+
+static bool isDisabled(const struct Scene *UNUSED(scene),
+ ModifierData *md,
+ bool UNUSED(useRenderParams))
+{
+ MaskModifierData *mmd = (MaskModifierData *)md;
+
+ /* The object type check is only needed here in case we have a placeholder
+ * object assigned (because the library containing the armature is missing).
+ *
+ * In other cases it should be impossible to have a type mismatch.
+ */
+ return mmd->ob_arm && mmd->ob_arm->type != OB_ARMATURE;
+}
+
+ModifierTypeInfo modifierType_Mask = {
+ /* name */ "Mask",
+ /* structName */ "MaskModifierData",
+ /* structSize */ sizeof(MaskModifierData),
+ /* type */ eModifierTypeType_Nonconstructive,
+ /* flags */
+ (ModifierTypeFlag)(eModifierTypeFlag_AcceptsMesh | eModifierTypeFlag_SupportsMapping |
+ eModifierTypeFlag_SupportsEditmode),
+
+ /* copyData */ modifier_copyData_generic,
+
+ /* deformVerts */ NULL,
+ /* deformMatrices */ NULL,
+ /* deformVertsEM */ NULL,
+ /* deformMatricesEM */ NULL,
+ /* applyModifier */ applyModifier,
+
+ /* initData */ NULL,
+ /* requiredDataMask */ requiredDataMask,
+ /* freeData */ NULL,
+ /* isDisabled */ isDisabled,
+ /* updateDepsgraph */ updateDepsgraph,
+ /* dependsOnTime */ NULL,
+ /* dependsOnNormals */ NULL,
+ /* foreachObjectLink */ foreachObjectLink,
+ /* foreachIDLink */ NULL,
+ /* foreachTexLink */ NULL,
+ /* freeRuntimeData */ NULL,
+};