2022-02-11 09:07:11 +11:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
* Copyright 2005 Blender Foundation. All rights reserved. */
|
2010-04-11 22:12:30 +00:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup modifiers
|
2011-02-25 13:57:17 +00:00
|
|
|
*/
|
|
|
|
|
|
2010-08-16 05:46:10 +00:00
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
2011-01-07 18:36:47 +00:00
|
|
|
#include "BLI_utildefines.h"
|
2019-02-25 11:56:24 +01:00
|
|
|
|
2010-04-12 00:36:50 +00:00
|
|
|
#include "BLI_ghash.h"
|
2019-02-25 11:56:24 +01:00
|
|
|
#include "BLI_math_vector.h"
|
|
|
|
|
#include "BLI_rand.h"
|
2010-04-11 22:12:30 +00:00
|
|
|
|
2022-07-15 14:12:34 +02:00
|
|
|
#include "BLT_translation.h"
|
|
|
|
|
|
2020-10-01 09:38:00 -05:00
|
|
|
#include "DNA_defaults.h"
|
2018-04-19 11:03:58 +02:00
|
|
|
#include "DNA_mesh_types.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "DNA_meshdata_types.h"
|
2018-04-19 11:03:58 +02:00
|
|
|
#include "DNA_object_types.h"
|
2020-06-05 10:41:03 -04:00
|
|
|
#include "DNA_screen_types.h"
|
2018-04-19 11:03:58 +02:00
|
|
|
|
|
|
|
|
#include "DEG_depsgraph_query.h"
|
2010-04-11 22:12:30 +00:00
|
|
|
|
2020-06-05 10:41:03 -04:00
|
|
|
#include "BKE_context.h"
|
2023-03-12 22:29:15 +01:00
|
|
|
#include "BKE_mesh.hh"
|
2010-04-11 22:12:30 +00:00
|
|
|
#include "BKE_modifier.h"
|
2016-12-28 17:30:58 +01:00
|
|
|
#include "BKE_particle.h"
|
2010-06-27 05:39:55 +00:00
|
|
|
#include "BKE_scene.h"
|
2020-06-05 10:41:03 -04:00
|
|
|
#include "BKE_screen.h"
|
|
|
|
|
|
|
|
|
|
#include "UI_interface.h"
|
|
|
|
|
#include "UI_resources.h"
|
|
|
|
|
|
|
|
|
|
#include "RNA_access.h"
|
2022-03-14 16:54:46 +01:00
|
|
|
#include "RNA_prototypes.h"
|
2010-04-11 22:12:30 +00:00
|
|
|
|
2018-12-30 15:14:00 +11:00
|
|
|
#include "MOD_modifiertypes.h"
|
2020-06-05 10:41:03 -04:00
|
|
|
#include "MOD_ui_common.h"
|
2018-12-30 15:14:00 +11:00
|
|
|
|
2010-04-11 22:12:30 +00:00
|
|
|
static void initData(ModifierData *md)
|
|
|
|
|
{
|
2012-05-06 13:38:33 +00:00
|
|
|
BuildModifierData *bmd = (BuildModifierData *)md;
|
2010-04-11 22:12:30 +00:00
|
|
|
|
2020-10-01 09:38:00 -05:00
|
|
|
BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(bmd, modifier));
|
|
|
|
|
|
|
|
|
|
MEMCPY_STRUCT_AFTER(bmd, DNA_struct_default_get(BuildModifierData), modifier);
|
2010-04-11 22:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-19 15:54:47 -06:00
|
|
|
static bool dependsOnTime(Scene * /*scene*/, ModifierData * /*md*/)
|
2010-04-11 22:12:30 +00:00
|
|
|
{
|
2013-06-02 03:59:19 +00:00
|
|
|
return true;
|
2010-04-11 22:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-19 15:54:47 -06:00
|
|
|
static Mesh *modifyMesh(ModifierData *md, const ModifierEvalContext *ctx, Mesh *mesh)
|
2010-04-11 22:12:30 +00:00
|
|
|
{
|
2018-04-19 11:03:58 +02:00
|
|
|
Mesh *result;
|
2012-05-06 13:38:33 +00:00
|
|
|
BuildModifierData *bmd = (BuildModifierData *)md;
|
2011-04-16 23:58:49 +00:00
|
|
|
int i, j, k;
|
2022-03-28 12:29:47 +11:00
|
|
|
int faces_dst_num, edges_dst_num, loops_dst_num = 0;
|
2010-04-11 22:12:30 +00:00
|
|
|
float frac;
|
2012-01-20 00:26:25 +00:00
|
|
|
MPoly *mpoly_dst;
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
MLoop *ml_dst;
|
|
|
|
|
const MLoop *ml_src;
|
2015-02-06 15:31:08 +11:00
|
|
|
GHashIterator gh_iter;
|
2010-04-11 22:12:30 +00:00
|
|
|
/* maps vert indices in old mesh to indices in new mesh */
|
2012-05-16 00:51:36 +00:00
|
|
|
GHash *vertHash = BLI_ghash_int_new("build ve apply gh");
|
2010-04-11 22:12:30 +00:00
|
|
|
/* maps edge indices in new mesh to indices in old mesh */
|
2012-05-16 00:51:36 +00:00
|
|
|
GHash *edgeHash = BLI_ghash_int_new("build ed apply gh");
|
2018-04-19 11:03:58 +02:00
|
|
|
/* maps edge indices in old mesh to indices in new mesh */
|
2012-05-16 00:51:36 +00:00
|
|
|
GHash *edgeHash2 = BLI_ghash_int_new("build ed apply gh");
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 12:29:47 +11:00
|
|
|
const int vert_src_num = mesh->totvert;
|
2023-02-23 10:39:51 -05:00
|
|
|
const blender::Span<MEdge> edges_src = mesh->edges();
|
|
|
|
|
const blender::Span<MPoly> polys_src = mesh->polys();
|
|
|
|
|
const blender::Span<MLoop> loops_src = mesh->loops();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-01-19 15:54:47 -06:00
|
|
|
int *vertMap = static_cast<int *>(MEM_malloc_arrayN(vert_src_num, sizeof(int), __func__));
|
2023-02-23 10:39:51 -05:00
|
|
|
int *edgeMap = static_cast<int *>(MEM_malloc_arrayN(edges_src.size(), sizeof(int), __func__));
|
|
|
|
|
int *faceMap = static_cast<int *>(MEM_malloc_arrayN(polys_src.size(), sizeof(int), __func__));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 12:29:47 +11:00
|
|
|
range_vn_i(vertMap, vert_src_num, 0);
|
2023-02-23 10:39:51 -05:00
|
|
|
range_vn_i(edgeMap, edges_src.size(), 0);
|
|
|
|
|
range_vn_i(faceMap, polys_src.size(), 0);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-01-19 15:54:47 -06:00
|
|
|
Scene *scene = DEG_get_input_scene(ctx->depsgraph);
|
2021-07-12 16:15:03 +02:00
|
|
|
frac = (BKE_scene_ctime_get(scene) - bmd->start) / bmd->length;
|
2011-03-27 13:49:53 +00:00
|
|
|
CLAMP(frac, 0.0f, 1.0f);
|
2014-01-17 00:13:36 +13:00
|
|
|
if (bmd->flag & MOD_BUILD_FLAG_REVERSE) {
|
2014-01-17 17:35:03 +11:00
|
|
|
frac = 1.0f - frac;
|
2014-01-17 00:13:36 +13:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-02-23 10:39:51 -05:00
|
|
|
faces_dst_num = polys_src.size() * frac;
|
|
|
|
|
edges_dst_num = edges_src.size() * frac;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-04-11 22:12:30 +00:00
|
|
|
/* if there's at least one face, build based on faces */
|
2022-03-28 12:29:47 +11:00
|
|
|
if (faces_dst_num) {
|
2023-03-01 15:57:50 -05:00
|
|
|
const MPoly *polys, *poly;
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
const MLoop *ml, *mloop;
|
2015-04-06 20:03:49 +10:00
|
|
|
uintptr_t hash_num, hash_num_alt;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-01-17 00:13:36 +13:00
|
|
|
if (bmd->flag & MOD_BUILD_FLAG_RANDOMIZE) {
|
2023-02-23 10:39:51 -05:00
|
|
|
BLI_array_randomize(faceMap, sizeof(*faceMap), polys_src.size(), bmd->seed);
|
2012-01-20 02:03:37 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-04-11 22:12:30 +00:00
|
|
|
/* get the set of all vert indices that will be in the final mesh,
|
2012-01-20 02:03:37 +00:00
|
|
|
* mapped to the new indices
|
|
|
|
|
*/
|
2023-03-01 15:57:50 -05:00
|
|
|
polys = polys_src.data();
|
2023-02-23 10:39:51 -05:00
|
|
|
mloop = loops_src.data();
|
2015-04-06 20:03:49 +10:00
|
|
|
hash_num = 0;
|
2022-03-28 12:29:47 +11:00
|
|
|
for (i = 0; i < faces_dst_num; i++) {
|
2023-03-01 15:57:50 -05:00
|
|
|
poly = polys + faceMap[i];
|
|
|
|
|
ml = mloop + poly->loopstart;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-03-01 15:57:50 -05:00
|
|
|
for (j = 0; j < poly->totloop; j++, ml++) {
|
2015-04-06 20:03:49 +10:00
|
|
|
void **val_p;
|
2018-09-19 12:05:58 +10:00
|
|
|
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++;
|
2011-04-16 23:58:49 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
2023-03-01 15:57:50 -05:00
|
|
|
loops_dst_num += poly->totloop;
|
2010-04-11 22:12:30 +00:00
|
|
|
}
|
2018-02-15 23:36:11 +11:00
|
|
|
BLI_assert(hash_num == BLI_ghash_len(vertHash));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-04-11 22:12:30 +00:00
|
|
|
/* get the set of edges that will be in the new mesh (i.e. all edges
|
2012-01-20 02:03:37 +00:00
|
|
|
* that have both verts in the new mesh)
|
|
|
|
|
*/
|
2015-04-06 20:03:49 +10:00
|
|
|
hash_num = 0;
|
|
|
|
|
hash_num_alt = 0;
|
2023-02-23 10:39:51 -05:00
|
|
|
for (i = 0; i < edges_src.size(); i++, hash_num_alt++) {
|
2023-03-01 15:57:50 -05:00
|
|
|
const MEdge *edge = edges_src.data() + i;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-03-01 15:57:50 -05:00
|
|
|
if (BLI_ghash_haskey(vertHash, POINTER_FROM_INT(edge->v1)) &&
|
|
|
|
|
BLI_ghash_haskey(vertHash, POINTER_FROM_INT(edge->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++;
|
2011-04-16 23:58:49 +00:00
|
|
|
}
|
2010-04-11 22:12:30 +00:00
|
|
|
}
|
2018-04-19 11:03:58 +02:00
|
|
|
BLI_assert(hash_num == BLI_ghash_len(edgeHash));
|
2012-01-20 02:03:37 +00:00
|
|
|
}
|
2022-03-28 12:29:47 +11:00
|
|
|
else if (edges_dst_num) {
|
2023-03-01 15:57:50 -05:00
|
|
|
const MEdge *edge;
|
2015-04-06 20:03:49 +10:00
|
|
|
uintptr_t hash_num;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-04-22 09:15:10 +10:00
|
|
|
if (bmd->flag & MOD_BUILD_FLAG_RANDOMIZE) {
|
2023-02-23 10:39:51 -05:00
|
|
|
BLI_array_randomize(edgeMap, sizeof(*edgeMap), edges_src.size(), bmd->seed);
|
2019-04-22 09:15:10 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-04-11 22:12:30 +00:00
|
|
|
/* get the set of all vert indices that will be in the final mesh,
|
2012-01-20 02:03:37 +00:00
|
|
|
* mapped to the new indices
|
|
|
|
|
*/
|
2023-03-01 15:57:50 -05:00
|
|
|
const MEdge *edges = edges_src.data();
|
2015-04-06 20:03:49 +10:00
|
|
|
hash_num = 0;
|
2018-02-15 23:36:11 +11:00
|
|
|
BLI_assert(hash_num == BLI_ghash_len(vertHash));
|
2022-03-28 12:29:47 +11:00
|
|
|
for (i = 0; i < edges_dst_num; i++) {
|
2015-04-06 20:03:49 +10:00
|
|
|
void **val_p;
|
2023-03-01 15:57:50 -05:00
|
|
|
edge = edges + edgeMap[i];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-03-01 15:57:50 -05:00
|
|
|
if (!BLI_ghash_ensure_p(vertHash, POINTER_FROM_INT(edge->v1), &val_p)) {
|
2015-04-06 20:03:49 +10:00
|
|
|
*val_p = (void *)hash_num;
|
|
|
|
|
hash_num++;
|
2012-01-20 02:03:37 +00:00
|
|
|
}
|
2023-03-01 15:57:50 -05:00
|
|
|
if (!BLI_ghash_ensure_p(vertHash, POINTER_FROM_INT(edge->v2), &val_p)) {
|
2015-04-06 20:03:49 +10:00
|
|
|
*val_p = (void *)hash_num;
|
|
|
|
|
hash_num++;
|
2012-01-20 02:03:37 +00:00
|
|
|
}
|
2010-04-11 22:12:30 +00:00
|
|
|
}
|
2018-02-15 23:36:11 +11:00
|
|
|
BLI_assert(hash_num == BLI_ghash_len(vertHash));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-03-09 18:28:30 +00:00
|
|
|
/* get the set of edges that will be in the new mesh */
|
2022-03-28 12:29:47 +11:00
|
|
|
for (i = 0; i < edges_dst_num; i++) {
|
2018-02-15 23:36:11 +11:00
|
|
|
j = BLI_ghash_len(edgeHash);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-19 12:05:58 +10:00
|
|
|
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));
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2010-04-11 22:12:30 +00:00
|
|
|
}
|
2012-01-20 02:03:37 +00:00
|
|
|
else {
|
2022-03-28 12:29:47 +11:00
|
|
|
int verts_num = vert_src_num * frac;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-01-17 00:13:36 +13:00
|
|
|
if (bmd->flag & MOD_BUILD_FLAG_RANDOMIZE) {
|
2022-03-28 12:29:47 +11:00
|
|
|
BLI_array_randomize(vertMap, sizeof(*vertMap), vert_src_num, bmd->seed);
|
2012-01-20 02:03:37 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-04-11 22:12:30 +00:00
|
|
|
/* get the set of all vert indices that will be in the final mesh,
|
2012-01-20 02:03:37 +00:00
|
|
|
* mapped to the new indices
|
|
|
|
|
*/
|
2022-03-28 12:29:47 +11:00
|
|
|
for (i = 0; i < verts_num; i++) {
|
2018-09-19 12:05:58 +10:00
|
|
|
BLI_ghash_insert(vertHash, POINTER_FROM_INT(vertMap[i]), POINTER_FROM_INT(i));
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2012-01-20 02:03:37 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-04-19 11:03:58 +02:00
|
|
|
/* now we know the number of verts, edges and faces, we can create the mesh. */
|
2018-05-08 17:06:30 +02:00
|
|
|
result = BKE_mesh_new_nomain_from_template(
|
2023-02-27 11:09:26 -05:00
|
|
|
mesh, BLI_ghash_len(vertHash), BLI_ghash_len(edgeHash), loops_dst_num, faces_dst_num);
|
2023-02-23 10:39:51 -05:00
|
|
|
blender::MutableSpan<MEdge> result_edges = result->edges_for_write();
|
|
|
|
|
blender::MutableSpan<MPoly> result_polys = result->polys_for_write();
|
|
|
|
|
blender::MutableSpan<MLoop> result_loops = result->loops_for_write();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-04-11 22:12:30 +00:00
|
|
|
/* copy the vertices across */
|
2015-02-06 15:31:08 +11:00
|
|
|
GHASH_ITER (gh_iter, vertHash) {
|
2018-09-19 12:05:58 +10:00
|
|
|
int oldIndex = POINTER_AS_INT(BLI_ghashIterator_getKey(&gh_iter));
|
|
|
|
|
int newIndex = POINTER_AS_INT(BLI_ghashIterator_getValue(&gh_iter));
|
2018-04-19 11:03:58 +02:00
|
|
|
CustomData_copy_data(&mesh->vdata, &result->vdata, oldIndex, newIndex, 1);
|
2011-04-21 15:53:30 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-02-20 15:48:01 +00:00
|
|
|
/* copy the edges across, remapping indices */
|
2018-02-15 23:36:11 +11:00
|
|
|
for (i = 0; i < BLI_ghash_len(edgeHash); i++) {
|
2011-02-20 15:48:01 +00:00
|
|
|
MEdge source;
|
|
|
|
|
MEdge *dest;
|
2018-09-19 12:05:58 +10:00
|
|
|
int oldIndex = POINTER_AS_INT(BLI_ghash_lookup(edgeHash, POINTER_FROM_INT(i)));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-02-23 10:39:51 -05:00
|
|
|
source = edges_src[oldIndex];
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
dest = &result_edges[i];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-19 12:05:58 +10:00
|
|
|
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)));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-04-19 11:03:58 +02:00
|
|
|
CustomData_copy_data(&mesh->edata, &result->edata, oldIndex, i, 1);
|
2011-02-20 15:48:01 +00:00
|
|
|
*dest = source;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-02-23 10:39:51 -05:00
|
|
|
mpoly_dst = result_polys.data();
|
|
|
|
|
ml_dst = result_loops.data();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-02-20 15:48:01 +00:00
|
|
|
/* copy the faces across, remapping indices */
|
2011-04-16 23:58:49 +00:00
|
|
|
k = 0;
|
2022-03-28 12:29:47 +11:00
|
|
|
for (i = 0; i < faces_dst_num; i++) {
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
const MPoly *source;
|
2011-04-16 23:58:49 +00:00
|
|
|
MPoly *dest;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-02-23 10:39:51 -05:00
|
|
|
source = &polys_src[faceMap[i]];
|
2012-01-20 00:26:25 +00:00
|
|
|
dest = mpoly_dst + i;
|
2018-04-19 11:03:58 +02:00
|
|
|
CustomData_copy_data(&mesh->pdata, &result->pdata, faceMap[i], i, 1);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-04-16 23:58:49 +00:00
|
|
|
*dest = *source;
|
|
|
|
|
dest->loopstart = k;
|
2018-04-19 11:03:58 +02:00
|
|
|
CustomData_copy_data(
|
|
|
|
|
&mesh->ldata, &result->ldata, source->loopstart, dest->loopstart, dest->totloop);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-02-23 10:39:51 -05:00
|
|
|
ml_src = &loops_src[source->loopstart];
|
2012-05-06 13:38:33 +00:00
|
|
|
for (j = 0; j < source->totloop; j++, k++, ml_src++, ml_dst++) {
|
2018-09-19 12:05:58 +10:00
|
|
|
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)));
|
2011-04-16 23:58:49 +00:00
|
|
|
}
|
2011-02-20 15:48:01 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-01-19 15:54:47 -06:00
|
|
|
BLI_ghash_free(vertHash, nullptr, nullptr);
|
|
|
|
|
BLI_ghash_free(edgeHash, nullptr, nullptr);
|
|
|
|
|
BLI_ghash_free(edgeHash2, nullptr, nullptr);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-02-20 15:48:01 +00:00
|
|
|
MEM_freeN(vertMap);
|
|
|
|
|
MEM_freeN(edgeMap);
|
|
|
|
|
MEM_freeN(faceMap);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-04-19 11:03:58 +02:00
|
|
|
/* TODO(sybren): also copy flags & tags? */
|
2011-02-20 15:48:01 +00:00
|
|
|
return result;
|
2010-04-11 22:12:30 +00:00
|
|
|
}
|
2010-04-13 22:43:48 +00:00
|
|
|
|
2023-01-19 15:54:47 -06:00
|
|
|
static void panel_draw(const bContext * /*C*/, Panel *panel)
|
2020-06-05 10:41:03 -04:00
|
|
|
{
|
|
|
|
|
uiLayout *layout = panel->layout;
|
|
|
|
|
|
2023-01-19 15:54:47 -06:00
|
|
|
PointerRNA *ptr = modifier_panel_get_property_pointers(panel, nullptr);
|
2020-06-05 10:41:03 -04:00
|
|
|
|
|
|
|
|
uiLayoutSetPropSep(layout, true);
|
|
|
|
|
|
2023-01-19 15:54:47 -06:00
|
|
|
uiItemR(layout, ptr, "frame_start", 0, nullptr, ICON_NONE);
|
|
|
|
|
uiItemR(layout, ptr, "frame_duration", 0, nullptr, ICON_NONE);
|
|
|
|
|
uiItemR(layout, ptr, "use_reverse", 0, nullptr, ICON_NONE);
|
2020-06-05 10:41:03 -04:00
|
|
|
|
2020-09-02 14:13:26 -05:00
|
|
|
modifier_panel_end(layout, ptr);
|
2020-06-05 10:41:03 -04:00
|
|
|
}
|
|
|
|
|
|
2023-01-19 15:54:47 -06:00
|
|
|
static void random_panel_header_draw(const bContext * /*C*/, Panel *panel)
|
2020-06-05 10:41:03 -04:00
|
|
|
{
|
|
|
|
|
uiLayout *layout = panel->layout;
|
|
|
|
|
|
2023-01-19 15:54:47 -06:00
|
|
|
PointerRNA *ptr = modifier_panel_get_property_pointers(panel, nullptr);
|
2020-06-05 10:41:03 -04:00
|
|
|
|
2023-01-19 15:54:47 -06:00
|
|
|
uiItemR(layout, ptr, "use_random_order", 0, nullptr, ICON_NONE);
|
2020-06-05 10:41:03 -04:00
|
|
|
}
|
|
|
|
|
|
2023-01-19 15:54:47 -06:00
|
|
|
static void random_panel_draw(const bContext * /*C*/, Panel *panel)
|
2020-06-05 10:41:03 -04:00
|
|
|
{
|
|
|
|
|
uiLayout *layout = panel->layout;
|
|
|
|
|
|
2023-01-19 15:54:47 -06:00
|
|
|
PointerRNA *ptr = modifier_panel_get_property_pointers(panel, nullptr);
|
2020-06-05 10:41:03 -04:00
|
|
|
|
|
|
|
|
uiLayoutSetPropSep(layout, true);
|
|
|
|
|
|
2020-09-02 14:13:26 -05:00
|
|
|
uiLayoutSetActive(layout, RNA_boolean_get(ptr, "use_random_order"));
|
2023-01-19 15:54:47 -06:00
|
|
|
uiItemR(layout, ptr, "seed", 0, nullptr, ICON_NONE);
|
2020-06-05 10:41:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-11 22:12:30 +00:00
|
|
|
ModifierTypeInfo modifierType_Build = {
|
2023-01-16 12:41:11 +11:00
|
|
|
/*name*/ N_("Build"),
|
|
|
|
|
/*structName*/ "BuildModifierData",
|
|
|
|
|
/*structSize*/ sizeof(BuildModifierData),
|
|
|
|
|
/*srna*/ &RNA_BuildModifier,
|
|
|
|
|
/*type*/ eModifierTypeType_Nonconstructive,
|
|
|
|
|
/*flags*/ eModifierTypeFlag_AcceptsMesh | eModifierTypeFlag_AcceptsCVs,
|
|
|
|
|
/*icon*/ ICON_MOD_BUILD,
|
|
|
|
|
|
|
|
|
|
/*copyData*/ BKE_modifier_copydata_generic,
|
|
|
|
|
|
2023-01-19 15:54:47 -06:00
|
|
|
/*deformVerts*/ nullptr,
|
|
|
|
|
/*deformMatrices*/ nullptr,
|
|
|
|
|
/*deformVertsEM*/ nullptr,
|
|
|
|
|
/*deformMatricesEM*/ nullptr,
|
2023-01-16 12:41:11 +11:00
|
|
|
/*modifyMesh*/ modifyMesh,
|
2023-01-19 15:54:47 -06:00
|
|
|
/*modifyGeometrySet*/ nullptr,
|
2023-01-16 12:41:11 +11:00
|
|
|
|
|
|
|
|
/*initData*/ initData,
|
2023-01-19 15:54:47 -06:00
|
|
|
/*requiredDataMask*/ nullptr,
|
|
|
|
|
/*freeData*/ nullptr,
|
|
|
|
|
/*isDisabled*/ nullptr,
|
|
|
|
|
/*updateDepsgraph*/ nullptr,
|
2023-01-16 12:41:11 +11:00
|
|
|
/*dependsOnTime*/ dependsOnTime,
|
2023-01-19 15:54:47 -06:00
|
|
|
/*dependsOnNormals*/ nullptr,
|
|
|
|
|
/*foreachIDLink*/ nullptr,
|
|
|
|
|
/*foreachTexLink*/ nullptr,
|
|
|
|
|
/*freeRuntimeData*/ nullptr,
|
2023-01-16 12:41:11 +11:00
|
|
|
/*panelRegister*/ panelRegister,
|
2023-01-19 15:54:47 -06:00
|
|
|
/*blendWrite*/ nullptr,
|
|
|
|
|
/*blendRead*/ nullptr,
|
2010-04-11 22:12:30 +00:00
|
|
|
};
|