Files
test/source/blender/modifiers/intern/MOD_build.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

345 lines
10 KiB
C
Raw Normal View History

/* SPDX-License-Identifier: GPL-2.0-or-later
* Copyright 2005 Blender Foundation. All rights reserved. */
/** \file
* \ingroup modifiers
*/
#include "MEM_guardedalloc.h"
#include "BLI_utildefines.h"
#include "BLI_ghash.h"
#include "BLI_math_vector.h"
#include "BLI_rand.h"
#include "DNA_defaults.h"
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_object_types.h"
#include "DNA_screen_types.h"
#include "DEG_depsgraph_query.h"
#include "BKE_context.h"
#include "BKE_mesh.h"
#include "BKE_modifier.h"
#include "BKE_particle.h"
#include "BKE_scene.h"
#include "BKE_screen.h"
#include "UI_interface.h"
#include "UI_resources.h"
#include "RNA_access.h"
#include "RNA_prototypes.h"
2018-12-30 15:14:00 +11:00
#include "MOD_modifiertypes.h"
#include "MOD_ui_common.h"
2018-12-30 15:14:00 +11:00
static void initData(ModifierData *md)
{
2012-05-06 13:38:33 +00:00
BuildModifierData *bmd = (BuildModifierData *)md;
BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(bmd, modifier));
MEMCPY_STRUCT_AFTER(bmd, DNA_struct_default_get(BuildModifierData), modifier);
}
static bool dependsOnTime(struct Scene *UNUSED(scene), ModifierData *UNUSED(md))
{
return true;
}
static Mesh *modifyMesh(ModifierData *md, const ModifierEvalContext *ctx, struct Mesh *mesh)
{
Mesh *result;
2012-05-06 13:38:33 +00:00
BuildModifierData *bmd = (BuildModifierData *)md;
int i, j, k;
int faces_dst_num, edges_dst_num, loops_dst_num = 0;
int *vertMap, *edgeMap, *faceMap;
float frac;
MPoly *mpoly_dst;
MLoop *ml_dst, *ml_src /*, *mloop_dst */;
2015-02-06 15:31:08 +11:00
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 vert_src_num = mesh->totvert;
const int edge_src_num = mesh->totedge;
const int poly_src_num = 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(vert_src_num, sizeof(*vertMap), "build modifier vertMap");
edgeMap = MEM_malloc_arrayN(edge_src_num, sizeof(*edgeMap), "build modifier edgeMap");
faceMap = MEM_malloc_arrayN(poly_src_num, sizeof(*faceMap), "build modifier faceMap");
range_vn_i(vertMap, vert_src_num, 0);
range_vn_i(edgeMap, edge_src_num, 0);
range_vn_i(faceMap, poly_src_num, 0);
struct Scene *scene = DEG_get_input_scene(ctx->depsgraph);
frac = (BKE_scene_ctime_get(scene) - bmd->start) / bmd->length;
CLAMP(frac, 0.0f, 1.0f);
if (bmd->flag & MOD_BUILD_FLAG_REVERSE) {
2014-01-17 17:35:03 +11:00
frac = 1.0f - frac;
}
faces_dst_num = poly_src_num * frac;
edges_dst_num = edge_src_num * frac;
/* if there's at least one face, build based on faces */
if (faces_dst_num) {
MPoly *mpoly, *mp;
MLoop *ml, *mloop;
2015-04-06 20:03:49 +10:00
uintptr_t hash_num, hash_num_alt;
if (bmd->flag & MOD_BUILD_FLAG_RANDOMIZE) {
BLI_array_randomize(faceMap, sizeof(*faceMap), poly_src_num, 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;
2015-04-06 20:03:49 +10:00
hash_num = 0;
for (i = 0; i < faces_dst_num; i++) {
mp = mpoly + faceMap[i];
ml = mloop + mp->loopstart;
2012-05-06 13:38:33 +00:00
for (j = 0; j < mp->totloop; j++, ml++) {
2015-04-06 20:03:49 +10:00
void **val_p;
if (!BLI_ghash_ensure_p(vertHash, POINTER_FROM_INT(ml->v), &val_p)) {
2015-04-06 20:03:49 +10:00
*val_p = (void *)hash_num;
hash_num++;
}
}
loops_dst_num += 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)
*/
2015-04-06 20:03:49 +10:00
hash_num = 0;
hash_num_alt = 0;
for (i = 0; i < edge_src_num; 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))) {
2015-04-06 20:03:49 +10:00
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 (edges_dst_num) {
MEdge *medge, *me;
2015-04-06 20:03:49 +10:00
uintptr_t hash_num;
if (bmd->flag & MOD_BUILD_FLAG_RANDOMIZE) {
BLI_array_randomize(edgeMap, sizeof(*edgeMap), edge_src_num, bmd->seed);
}
/* get the set of all vert indices that will be in the final mesh,
* mapped to the new indices
*/
medge = medge_src;
2015-04-06 20:03:49 +10:00
hash_num = 0;
BLI_assert(hash_num == BLI_ghash_len(vertHash));
for (i = 0; i < edges_dst_num; i++) {
2015-04-06 20:03:49 +10:00
void **val_p;
me = medge + edgeMap[i];
if (!BLI_ghash_ensure_p(vertHash, POINTER_FROM_INT(me->v1), &val_p)) {
2015-04-06 20:03:49 +10:00
*val_p = (void *)hash_num;
hash_num++;
}
if (!BLI_ghash_ensure_p(vertHash, POINTER_FROM_INT(me->v2), &val_p)) {
2015-04-06 20:03:49 +10:00
*val_p = (void *)hash_num;
hash_num++;
}
}
BLI_assert(hash_num == BLI_ghash_len(vertHash));
2012-03-09 18:28:30 +00:00
/* get the set of edges that will be in the new mesh */
for (i = 0; i < edges_dst_num; 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 verts_num = vert_src_num * frac;
if (bmd->flag & MOD_BUILD_FLAG_RANDOMIZE) {
BLI_array_randomize(vertMap, sizeof(*vertMap), vert_src_num, 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 < verts_num; 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, loops_dst_num, faces_dst_num);
/* copy the vertices across */
2015-02-06 15:31:08 +11:00
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));
2012-05-06 13:38:33 +00:00
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 < faces_dst_num; 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;
2012-05-06 13:38:33 +00:00
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);
/* TODO(sybren): also copy flags & tags? */
return result;
}
static void panel_draw(const bContext *UNUSED(C), Panel *panel)
{
uiLayout *layout = panel->layout;
PointerRNA *ptr = modifier_panel_get_property_pointers(panel, NULL);
uiLayoutSetPropSep(layout, true);
uiItemR(layout, ptr, "frame_start", 0, NULL, ICON_NONE);
uiItemR(layout, ptr, "frame_duration", 0, NULL, ICON_NONE);
uiItemR(layout, ptr, "use_reverse", 0, NULL, ICON_NONE);
modifier_panel_end(layout, ptr);
}
static void random_panel_header_draw(const bContext *UNUSED(C), Panel *panel)
{
uiLayout *layout = panel->layout;
PointerRNA *ptr = modifier_panel_get_property_pointers(panel, NULL);
uiItemR(layout, ptr, "use_random_order", 0, NULL, ICON_NONE);
}
static void random_panel_draw(const bContext *UNUSED(C), Panel *panel)
{
uiLayout *layout = panel->layout;
PointerRNA *ptr = modifier_panel_get_property_pointers(panel, NULL);
uiLayoutSetPropSep(layout, true);
uiLayoutSetActive(layout, RNA_boolean_get(ptr, "use_random_order"));
uiItemR(layout, ptr, "seed", 0, NULL, ICON_NONE);
}
static void panelRegister(ARegionType *region_type)
{
PanelType *panel_type = modifier_panel_register(region_type, eModifierType_Build, panel_draw);
modifier_subpanel_register(
region_type, "randomize", "", random_panel_header_draw, random_panel_draw, panel_type);
}
ModifierTypeInfo modifierType_Build = {
2012-10-24 05:45:54 +00:00
/* name */ "Build",
/* structName */ "BuildModifierData",
/* structSize */ sizeof(BuildModifierData),
/* srna */ &RNA_BuildModifier,
2012-10-24 05:45:54 +00:00
/* type */ eModifierTypeType_Nonconstructive,
/* flags */ eModifierTypeFlag_AcceptsMesh | eModifierTypeFlag_AcceptsCVs,
/* icon */ ICON_MOD_BUILD,
/* copyData */ BKE_modifier_copydata_generic,
2012-10-24 05:45:54 +00:00
/* deformVerts */ NULL,
/* deformMatrices */ NULL,
/* deformVertsEM */ NULL,
/* deformMatricesEM */ NULL,
/* modifyMesh */ modifyMesh,
/* modifyGeometrySet */ NULL,
2012-10-24 05:45:54 +00:00
/* initData */ initData,
/* requiredDataMask */ NULL,
/* freeData */ NULL,
/* isDisabled */ NULL,
Depsgraph: New dependency graph integration commit This commit integrates the work done so far on the new dependency graph system, where goal was to replace legacy depsgraph with the new one, supporting loads of neat features like: - More granular dependency relation nature, which solves issues with fake cycles in the dependencies. - Move towards all-animatable, by better integration of drivers into the system. - Lay down some basis for upcoming copy-on-write, overrides and so on. The new system is living side-by-side with the previous one and disabled by default, so nothing will become suddenly broken. The way to enable new depsgraph is to pass `--new-depsgraph` command line argument. It's a bit early to consider the system production-ready, there are some TODOs and issues were discovered during the merge period, they'll be addressed ASAP. But it's important to merge, because it's the only way to attract artists to really start testing this system. There are number of assorted documents related on the design of the new system: * http://wiki.blender.org/index.php/User:Aligorith/GSoC2013_Depsgraph#Design_Documents * http://wiki.blender.org/index.php/User:Nazg-gul/DependencyGraph There are also some user-related information online: * http://code.blender.org/2015/02/blender-dependency-graph-branch-for-users/ * http://code.blender.org/2015/03/more-dependency-graph-tricks/ Kudos to everyone who was involved into the project: - Joshua "Aligorith" Leung -- design specification, initial code - Lukas "lukas_t" Toenne -- integrating code into blender, with further fixes - Sergey "Sergey" "Sharybin" -- some mocking around, trying to wrap up the project and so - Bassam "slikdigit" Kurdali -- stressing the new system, reporting all the issues and recording/writing documentation. - Everyone else who i forgot to mention here :)
2015-05-12 15:05:57 +05:00
/* updateDepsgraph */ NULL,
2012-10-24 05:45:54 +00:00
/* dependsOnTime */ dependsOnTime,
/* dependsOnNormals */ NULL,
/* foreachIDLink */ NULL,
/* foreachTexLink */ NULL,
/* freeRuntimeData */ NULL,
/* panelRegister */ panelRegister,
/* blendWrite */ NULL,
/* blendRead */ NULL,
};