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:
Diffstat (limited to 'source/blender/modifiers/intern/MOD_build.c')
-rw-r--r--source/blender/modifiers/intern/MOD_build.c500
1 files changed, 245 insertions, 255 deletions
diff --git a/source/blender/modifiers/intern/MOD_build.c b/source/blender/modifiers/intern/MOD_build.c
index 01478c855bb..7c8470b26d9 100644
--- a/source/blender/modifiers/intern/MOD_build.c
+++ b/source/blender/modifiers/intern/MOD_build.c
@@ -44,270 +44,260 @@
static void initData(ModifierData *md)
{
- BuildModifierData *bmd = (BuildModifierData *) md;
+ BuildModifierData *bmd = (BuildModifierData *)md;
- bmd->start = 1.0;
- bmd->length = 100.0;
+ bmd->start = 1.0;
+ bmd->length = 100.0;
}
static bool dependsOnTime(ModifierData *UNUSED(md))
{
- return true;
+ return true;
}
-static Mesh *applyModifier(
- ModifierData *md, const ModifierEvalContext *ctx,
- struct Mesh *mesh)
+static Mesh *applyModifier(ModifierData *md, const ModifierEvalContext *ctx, struct Mesh *mesh)
{
- Mesh *result;
- BuildModifierData *bmd = (BuildModifierData *) md;
- int i, j, k;
- int numFaces_dst, numEdges_dst, numLoops_dst = 0;
- int *vertMap, *edgeMap, *faceMap;
- float frac;
- MPoly *mpoly_dst;
- MLoop *ml_dst, *ml_src /*, *mloop_dst */;
- GHashIterator gh_iter;
- /* maps vert indices in old mesh to indices in new mesh */
- GHash *vertHash = BLI_ghash_int_new("build ve apply gh");
- /* maps edge indices in new mesh to indices in old mesh */
- GHash *edgeHash = BLI_ghash_int_new("build ed apply gh");
- /* maps edge indices in old mesh to indices in new mesh */
- GHash *edgeHash2 = BLI_ghash_int_new("build ed apply gh");
-
- const int numVert_src = mesh->totvert;
- const int numEdge_src = mesh->totedge;
- const int numPoly_src = mesh->totpoly;
- MPoly *mpoly_src = mesh->mpoly;
- MLoop *mloop_src = mesh->mloop;
- MEdge *medge_src = mesh->medge;
- MVert *mvert_src = mesh->mvert;
-
- vertMap = MEM_malloc_arrayN(numVert_src, sizeof(*vertMap), "build modifier vertMap");
- edgeMap = MEM_malloc_arrayN(numEdge_src, sizeof(*edgeMap), "build modifier edgeMap");
- faceMap = MEM_malloc_arrayN(numPoly_src, sizeof(*faceMap), "build modifier faceMap");
-
- range_vn_i(vertMap, numVert_src, 0);
- range_vn_i(edgeMap, numEdge_src, 0);
- range_vn_i(faceMap, numPoly_src, 0);
-
- struct Scene *scene = DEG_get_input_scene(ctx->depsgraph);
- frac = (BKE_scene_frame_get(scene) - bmd->start) / bmd->length;
- CLAMP(frac, 0.0f, 1.0f);
- if (bmd->flag & MOD_BUILD_FLAG_REVERSE) {
- frac = 1.0f - frac;
- }
-
- numFaces_dst = numPoly_src * frac;
- numEdges_dst = numEdge_src * frac;
-
- /* if there's at least one face, build based on faces */
- if (numFaces_dst) {
- MPoly *mpoly, *mp;
- MLoop *ml, *mloop;
- uintptr_t hash_num, hash_num_alt;
-
- if (bmd->flag & MOD_BUILD_FLAG_RANDOMIZE) {
- BLI_array_randomize(faceMap, sizeof(*faceMap),
- numPoly_src, bmd->seed);
- }
-
- /* get the set of all vert indices that will be in the final mesh,
- * mapped to the new indices
- */
- mpoly = mpoly_src;
- mloop = mloop_src;
- hash_num = 0;
- for (i = 0; i < numFaces_dst; i++) {
- mp = mpoly + faceMap[i];
- ml = mloop + mp->loopstart;
-
- for (j = 0; j < mp->totloop; j++, ml++) {
- void **val_p;
- if (!BLI_ghash_ensure_p(vertHash, POINTER_FROM_INT(ml->v), &val_p)) {
- *val_p = (void *)hash_num;
- hash_num++;
- }
- }
-
- numLoops_dst += mp->totloop;
- }
- BLI_assert(hash_num == BLI_ghash_len(vertHash));
-
- /* get the set of edges that will be in the new mesh (i.e. all edges
- * that have both verts in the new mesh)
- */
- hash_num = 0;
- hash_num_alt = 0;
- for (i = 0; i < numEdge_src; i++, hash_num_alt++) {
- MEdge *me = medge_src + i;
-
- if (BLI_ghash_haskey(vertHash, POINTER_FROM_INT(me->v1)) &&
- BLI_ghash_haskey(vertHash, POINTER_FROM_INT(me->v2)))
- {
- BLI_ghash_insert(edgeHash, (void *)hash_num, (void *)hash_num_alt);
- BLI_ghash_insert(edgeHash2, (void *)hash_num_alt, (void *)hash_num);
- hash_num++;
- }
- }
- BLI_assert(hash_num == BLI_ghash_len(edgeHash));
- }
- else if (numEdges_dst) {
- MEdge *medge, *me;
- uintptr_t hash_num;
-
- if (bmd->flag & MOD_BUILD_FLAG_RANDOMIZE)
- BLI_array_randomize(edgeMap, sizeof(*edgeMap),
- numEdge_src, bmd->seed);
-
- /* get the set of all vert indices that will be in the final mesh,
- * mapped to the new indices
- */
- medge = medge_src;
- hash_num = 0;
- BLI_assert(hash_num == BLI_ghash_len(vertHash));
- for (i = 0; i < numEdges_dst; i++) {
- void **val_p;
- me = medge + edgeMap[i];
-
- if (!BLI_ghash_ensure_p(vertHash, POINTER_FROM_INT(me->v1), &val_p)) {
- *val_p = (void *)hash_num;
- hash_num++;
- }
- if (!BLI_ghash_ensure_p(vertHash, POINTER_FROM_INT(me->v2), &val_p)) {
- *val_p = (void *)hash_num;
- hash_num++;
- }
- }
- BLI_assert(hash_num == BLI_ghash_len(vertHash));
-
- /* get the set of edges that will be in the new mesh */
- for (i = 0; i < numEdges_dst; i++) {
- j = BLI_ghash_len(edgeHash);
-
- BLI_ghash_insert(edgeHash, POINTER_FROM_INT(j),
- POINTER_FROM_INT(edgeMap[i]));
- BLI_ghash_insert(edgeHash2, POINTER_FROM_INT(edgeMap[i]),
- POINTER_FROM_INT(j));
- }
- }
- else {
- int numVerts = numVert_src * frac;
-
- if (bmd->flag & MOD_BUILD_FLAG_RANDOMIZE) {
- BLI_array_randomize(vertMap, sizeof(*vertMap),
- numVert_src, bmd->seed);
- }
-
- /* get the set of all vert indices that will be in the final mesh,
- * mapped to the new indices
- */
- for (i = 0; i < numVerts; i++) {
- BLI_ghash_insert(vertHash, POINTER_FROM_INT(vertMap[i]), POINTER_FROM_INT(i));
- }
- }
-
- /* now we know the number of verts, edges and faces, we can create the mesh. */
- result = BKE_mesh_new_nomain_from_template(
- mesh, BLI_ghash_len(vertHash), BLI_ghash_len(edgeHash),
- 0, numLoops_dst, numFaces_dst);
-
- /* copy the vertices across */
- GHASH_ITER (gh_iter, vertHash) {
- MVert source;
- MVert *dest;
- int oldIndex = POINTER_AS_INT(BLI_ghashIterator_getKey(&gh_iter));
- int newIndex = POINTER_AS_INT(BLI_ghashIterator_getValue(&gh_iter));
-
- source = mvert_src[oldIndex];
- dest = &result->mvert[newIndex];
-
- CustomData_copy_data(&mesh->vdata, &result->vdata, oldIndex, newIndex, 1);
- *dest = source;
- }
-
- /* copy the edges across, remapping indices */
- for (i = 0; i < BLI_ghash_len(edgeHash); i++) {
- MEdge source;
- MEdge *dest;
- int oldIndex = POINTER_AS_INT(BLI_ghash_lookup(edgeHash, POINTER_FROM_INT(i)));
-
- source = medge_src[oldIndex];
- dest = &result->medge[i];
-
- source.v1 = POINTER_AS_INT(BLI_ghash_lookup(vertHash, POINTER_FROM_INT(source.v1)));
- source.v2 = POINTER_AS_INT(BLI_ghash_lookup(vertHash, POINTER_FROM_INT(source.v2)));
-
- CustomData_copy_data(&mesh->edata, &result->edata, oldIndex, i, 1);
- *dest = source;
- }
-
- mpoly_dst = result->mpoly;
- ml_dst = result->mloop;
-
- /* copy the faces across, remapping indices */
- k = 0;
- for (i = 0; i < numFaces_dst; i++) {
- MPoly *source;
- MPoly *dest;
-
- source = mpoly_src + faceMap[i];
- dest = mpoly_dst + i;
- CustomData_copy_data(&mesh->pdata, &result->pdata, faceMap[i], i, 1);
-
- *dest = *source;
- dest->loopstart = k;
- CustomData_copy_data(&mesh->ldata, &result->ldata, source->loopstart, dest->loopstart, dest->totloop);
-
- ml_src = mloop_src + source->loopstart;
- for (j = 0; j < source->totloop; j++, k++, ml_src++, ml_dst++) {
- ml_dst->v = POINTER_AS_INT(BLI_ghash_lookup(vertHash, POINTER_FROM_INT(ml_src->v)));
- ml_dst->e = POINTER_AS_INT(BLI_ghash_lookup(edgeHash2, POINTER_FROM_INT(ml_src->e)));
- }
- }
-
- BLI_ghash_free(vertHash, NULL, NULL);
- BLI_ghash_free(edgeHash, NULL, NULL);
- BLI_ghash_free(edgeHash2, NULL, NULL);
-
- MEM_freeN(vertMap);
- MEM_freeN(edgeMap);
- MEM_freeN(faceMap);
-
- if (mesh->runtime.cd_dirty_vert & CD_MASK_NORMAL) {
- result->runtime.cd_dirty_vert |= CD_MASK_NORMAL;
- }
-
- /* TODO(sybren): also copy flags & tags? */
- return result;
+ Mesh *result;
+ BuildModifierData *bmd = (BuildModifierData *)md;
+ int i, j, k;
+ int numFaces_dst, numEdges_dst, numLoops_dst = 0;
+ int *vertMap, *edgeMap, *faceMap;
+ float frac;
+ MPoly *mpoly_dst;
+ MLoop *ml_dst, *ml_src /*, *mloop_dst */;
+ GHashIterator gh_iter;
+ /* maps vert indices in old mesh to indices in new mesh */
+ GHash *vertHash = BLI_ghash_int_new("build ve apply gh");
+ /* maps edge indices in new mesh to indices in old mesh */
+ GHash *edgeHash = BLI_ghash_int_new("build ed apply gh");
+ /* maps edge indices in old mesh to indices in new mesh */
+ GHash *edgeHash2 = BLI_ghash_int_new("build ed apply gh");
+
+ const int numVert_src = mesh->totvert;
+ const int numEdge_src = mesh->totedge;
+ const int numPoly_src = mesh->totpoly;
+ MPoly *mpoly_src = mesh->mpoly;
+ MLoop *mloop_src = mesh->mloop;
+ MEdge *medge_src = mesh->medge;
+ MVert *mvert_src = mesh->mvert;
+
+ vertMap = MEM_malloc_arrayN(numVert_src, sizeof(*vertMap), "build modifier vertMap");
+ edgeMap = MEM_malloc_arrayN(numEdge_src, sizeof(*edgeMap), "build modifier edgeMap");
+ faceMap = MEM_malloc_arrayN(numPoly_src, sizeof(*faceMap), "build modifier faceMap");
+
+ range_vn_i(vertMap, numVert_src, 0);
+ range_vn_i(edgeMap, numEdge_src, 0);
+ range_vn_i(faceMap, numPoly_src, 0);
+
+ struct Scene *scene = DEG_get_input_scene(ctx->depsgraph);
+ frac = (BKE_scene_frame_get(scene) - bmd->start) / bmd->length;
+ CLAMP(frac, 0.0f, 1.0f);
+ if (bmd->flag & MOD_BUILD_FLAG_REVERSE) {
+ frac = 1.0f - frac;
+ }
+
+ numFaces_dst = numPoly_src * frac;
+ numEdges_dst = numEdge_src * frac;
+
+ /* if there's at least one face, build based on faces */
+ if (numFaces_dst) {
+ MPoly *mpoly, *mp;
+ MLoop *ml, *mloop;
+ uintptr_t hash_num, hash_num_alt;
+
+ if (bmd->flag & MOD_BUILD_FLAG_RANDOMIZE) {
+ BLI_array_randomize(faceMap, sizeof(*faceMap), numPoly_src, bmd->seed);
+ }
+
+ /* get the set of all vert indices that will be in the final mesh,
+ * mapped to the new indices
+ */
+ mpoly = mpoly_src;
+ mloop = mloop_src;
+ hash_num = 0;
+ for (i = 0; i < numFaces_dst; i++) {
+ mp = mpoly + faceMap[i];
+ ml = mloop + mp->loopstart;
+
+ for (j = 0; j < mp->totloop; j++, ml++) {
+ void **val_p;
+ if (!BLI_ghash_ensure_p(vertHash, POINTER_FROM_INT(ml->v), &val_p)) {
+ *val_p = (void *)hash_num;
+ hash_num++;
+ }
+ }
+
+ numLoops_dst += mp->totloop;
+ }
+ BLI_assert(hash_num == BLI_ghash_len(vertHash));
+
+ /* get the set of edges that will be in the new mesh (i.e. all edges
+ * that have both verts in the new mesh)
+ */
+ hash_num = 0;
+ hash_num_alt = 0;
+ for (i = 0; i < numEdge_src; i++, hash_num_alt++) {
+ MEdge *me = medge_src + i;
+
+ if (BLI_ghash_haskey(vertHash, POINTER_FROM_INT(me->v1)) &&
+ BLI_ghash_haskey(vertHash, POINTER_FROM_INT(me->v2))) {
+ BLI_ghash_insert(edgeHash, (void *)hash_num, (void *)hash_num_alt);
+ BLI_ghash_insert(edgeHash2, (void *)hash_num_alt, (void *)hash_num);
+ hash_num++;
+ }
+ }
+ BLI_assert(hash_num == BLI_ghash_len(edgeHash));
+ }
+ else if (numEdges_dst) {
+ MEdge *medge, *me;
+ uintptr_t hash_num;
+
+ if (bmd->flag & MOD_BUILD_FLAG_RANDOMIZE)
+ BLI_array_randomize(edgeMap, sizeof(*edgeMap), numEdge_src, bmd->seed);
+
+ /* get the set of all vert indices that will be in the final mesh,
+ * mapped to the new indices
+ */
+ medge = medge_src;
+ hash_num = 0;
+ BLI_assert(hash_num == BLI_ghash_len(vertHash));
+ for (i = 0; i < numEdges_dst; i++) {
+ void **val_p;
+ me = medge + edgeMap[i];
+
+ if (!BLI_ghash_ensure_p(vertHash, POINTER_FROM_INT(me->v1), &val_p)) {
+ *val_p = (void *)hash_num;
+ hash_num++;
+ }
+ if (!BLI_ghash_ensure_p(vertHash, POINTER_FROM_INT(me->v2), &val_p)) {
+ *val_p = (void *)hash_num;
+ hash_num++;
+ }
+ }
+ BLI_assert(hash_num == BLI_ghash_len(vertHash));
+
+ /* get the set of edges that will be in the new mesh */
+ for (i = 0; i < numEdges_dst; i++) {
+ j = BLI_ghash_len(edgeHash);
+
+ BLI_ghash_insert(edgeHash, POINTER_FROM_INT(j), POINTER_FROM_INT(edgeMap[i]));
+ BLI_ghash_insert(edgeHash2, POINTER_FROM_INT(edgeMap[i]), POINTER_FROM_INT(j));
+ }
+ }
+ else {
+ int numVerts = numVert_src * frac;
+
+ if (bmd->flag & MOD_BUILD_FLAG_RANDOMIZE) {
+ BLI_array_randomize(vertMap, sizeof(*vertMap), numVert_src, bmd->seed);
+ }
+
+ /* get the set of all vert indices that will be in the final mesh,
+ * mapped to the new indices
+ */
+ for (i = 0; i < numVerts; i++) {
+ BLI_ghash_insert(vertHash, POINTER_FROM_INT(vertMap[i]), POINTER_FROM_INT(i));
+ }
+ }
+
+ /* now we know the number of verts, edges and faces, we can create the mesh. */
+ result = BKE_mesh_new_nomain_from_template(
+ mesh, BLI_ghash_len(vertHash), BLI_ghash_len(edgeHash), 0, numLoops_dst, numFaces_dst);
+
+ /* copy the vertices across */
+ GHASH_ITER (gh_iter, vertHash) {
+ MVert source;
+ MVert *dest;
+ int oldIndex = POINTER_AS_INT(BLI_ghashIterator_getKey(&gh_iter));
+ int newIndex = POINTER_AS_INT(BLI_ghashIterator_getValue(&gh_iter));
+
+ source = mvert_src[oldIndex];
+ dest = &result->mvert[newIndex];
+
+ CustomData_copy_data(&mesh->vdata, &result->vdata, oldIndex, newIndex, 1);
+ *dest = source;
+ }
+
+ /* copy the edges across, remapping indices */
+ for (i = 0; i < BLI_ghash_len(edgeHash); i++) {
+ MEdge source;
+ MEdge *dest;
+ int oldIndex = POINTER_AS_INT(BLI_ghash_lookup(edgeHash, POINTER_FROM_INT(i)));
+
+ source = medge_src[oldIndex];
+ dest = &result->medge[i];
+
+ source.v1 = POINTER_AS_INT(BLI_ghash_lookup(vertHash, POINTER_FROM_INT(source.v1)));
+ source.v2 = POINTER_AS_INT(BLI_ghash_lookup(vertHash, POINTER_FROM_INT(source.v2)));
+
+ CustomData_copy_data(&mesh->edata, &result->edata, oldIndex, i, 1);
+ *dest = source;
+ }
+
+ mpoly_dst = result->mpoly;
+ ml_dst = result->mloop;
+
+ /* copy the faces across, remapping indices */
+ k = 0;
+ for (i = 0; i < numFaces_dst; i++) {
+ MPoly *source;
+ MPoly *dest;
+
+ source = mpoly_src + faceMap[i];
+ dest = mpoly_dst + i;
+ CustomData_copy_data(&mesh->pdata, &result->pdata, faceMap[i], i, 1);
+
+ *dest = *source;
+ dest->loopstart = k;
+ CustomData_copy_data(
+ &mesh->ldata, &result->ldata, source->loopstart, dest->loopstart, dest->totloop);
+
+ ml_src = mloop_src + source->loopstart;
+ for (j = 0; j < source->totloop; j++, k++, ml_src++, ml_dst++) {
+ ml_dst->v = POINTER_AS_INT(BLI_ghash_lookup(vertHash, POINTER_FROM_INT(ml_src->v)));
+ ml_dst->e = POINTER_AS_INT(BLI_ghash_lookup(edgeHash2, POINTER_FROM_INT(ml_src->e)));
+ }
+ }
+
+ BLI_ghash_free(vertHash, NULL, NULL);
+ BLI_ghash_free(edgeHash, NULL, NULL);
+ BLI_ghash_free(edgeHash2, NULL, NULL);
+
+ MEM_freeN(vertMap);
+ MEM_freeN(edgeMap);
+ MEM_freeN(faceMap);
+
+ if (mesh->runtime.cd_dirty_vert & CD_MASK_NORMAL) {
+ result->runtime.cd_dirty_vert |= CD_MASK_NORMAL;
+ }
+
+ /* TODO(sybren): also copy flags & tags? */
+ return result;
}
-
ModifierTypeInfo modifierType_Build = {
- /* name */ "Build",
- /* structName */ "BuildModifierData",
- /* structSize */ sizeof(BuildModifierData),
- /* type */ eModifierTypeType_Nonconstructive,
- /* flags */ eModifierTypeFlag_AcceptsMesh |
- eModifierTypeFlag_AcceptsCVs,
-
- /* copyData */ modifier_copyData_generic,
-
- /* deformVerts */ NULL,
- /* deformMatrices */ NULL,
- /* deformVertsEM */ NULL,
- /* deformMatricesEM */ NULL,
- /* applyModifier */ applyModifier,
-
- /* initData */ initData,
- /* requiredDataMask */ NULL,
- /* freeData */ NULL,
- /* isDisabled */ NULL,
- /* updateDepsgraph */ NULL,
- /* dependsOnTime */ dependsOnTime,
- /* dependsOnNormals */ NULL,
- /* foreachObjectLink */ NULL,
- /* foreachIDLink */ NULL,
- /* foreachTexLink */ NULL,
- /* freeRuntimeData */ NULL,
+ /* name */ "Build",
+ /* structName */ "BuildModifierData",
+ /* structSize */ sizeof(BuildModifierData),
+ /* type */ eModifierTypeType_Nonconstructive,
+ /* flags */ eModifierTypeFlag_AcceptsMesh | eModifierTypeFlag_AcceptsCVs,
+
+ /* copyData */ modifier_copyData_generic,
+
+ /* deformVerts */ NULL,
+ /* deformMatrices */ NULL,
+ /* deformVertsEM */ NULL,
+ /* deformMatricesEM */ NULL,
+ /* applyModifier */ applyModifier,
+
+ /* initData */ initData,
+ /* requiredDataMask */ NULL,
+ /* freeData */ NULL,
+ /* isDisabled */ NULL,
+ /* updateDepsgraph */ NULL,
+ /* dependsOnTime */ dependsOnTime,
+ /* dependsOnNormals */ NULL,
+ /* foreachObjectLink */ NULL,
+ /* foreachIDLink */ NULL,
+ /* foreachTexLink */ NULL,
+ /* freeRuntimeData */ NULL,
};