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
|
|
|
*/
|
|
|
|
|
|
2019-02-25 11:56:24 +01:00
|
|
|
#include "BLI_utildefines.h"
|
|
|
|
|
|
2020-12-15 10:47:58 +11:00
|
|
|
#include "BLI_kdopbvh.h"
|
2019-02-25 11:56:24 +01:00
|
|
|
#include "BLI_math.h"
|
|
|
|
|
|
2020-06-05 10:41:03 -04:00
|
|
|
#include "BLT_translation.h"
|
|
|
|
|
|
2020-10-01 09:38:00 -05:00
|
|
|
#include "DNA_defaults.h"
|
2018-05-17 15:26:59 +02:00
|
|
|
#include "DNA_mesh_types.h"
|
2010-08-10 05:41:51 +00:00
|
|
|
#include "DNA_meshdata_types.h"
|
2020-12-15 10:47:58 +11:00
|
|
|
#include "DNA_object_force_types.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "DNA_object_types.h"
|
2020-06-05 10:41:03 -04:00
|
|
|
#include "DNA_screen_types.h"
|
2010-04-11 22:12:30 +00:00
|
|
|
|
2010-08-16 05:46:10 +00:00
|
|
|
#include "MEM_guardedalloc.h"
|
2010-04-11 22:12:30 +00:00
|
|
|
|
2010-04-12 00:36:50 +00:00
|
|
|
#include "BKE_collision.h"
|
2020-06-05 10:41:03 -04:00
|
|
|
#include "BKE_context.h"
|
2010-04-11 22:12:30 +00:00
|
|
|
#include "BKE_global.h"
|
2020-02-10 12:58:59 +01:00
|
|
|
#include "BKE_lib_id.h"
|
2018-05-17 15:26:59 +02:00
|
|
|
#include "BKE_mesh.h"
|
2018-06-05 15:59:53 +02:00
|
|
|
#include "BKE_mesh_runtime.h"
|
2010-04-11 22:12:30 +00:00
|
|
|
#include "BKE_modifier.h"
|
2016-12-28 17:30:58 +01:00
|
|
|
#include "BKE_pointcache.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
|
|
|
|
2017-08-01 09:06:34 +10:00
|
|
|
#include "MOD_modifiertypes.h"
|
2020-06-05 10:41:03 -04:00
|
|
|
#include "MOD_ui_common.h"
|
2018-05-17 15:26:59 +02:00
|
|
|
#include "MOD_util.h"
|
|
|
|
|
|
2020-06-23 17:25:44 +02:00
|
|
|
#include "BLO_read_write.h"
|
|
|
|
|
|
2018-05-17 15:26:59 +02:00
|
|
|
#include "DEG_depsgraph_query.h"
|
2017-08-01 09:06:34 +10:00
|
|
|
|
2018-06-17 17:04:27 +02:00
|
|
|
static void initData(ModifierData *md)
|
2010-04-11 22:12:30 +00:00
|
|
|
{
|
2012-05-06 13:38:33 +00:00
|
|
|
CollisionModifierData *collmd = (CollisionModifierData *)md;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-10-01 09:38:00 -05:00
|
|
|
BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(collmd, modifier));
|
|
|
|
|
|
|
|
|
|
MEMCPY_STRUCT_AFTER(collmd, DNA_struct_default_get(CollisionModifierData), modifier);
|
2010-04-11 22:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void freeData(ModifierData *md)
|
|
|
|
|
{
|
2012-05-06 13:38:33 +00:00
|
|
|
CollisionModifierData *collmd = (CollisionModifierData *)md;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-05-08 14:21:02 +02:00
|
|
|
if (collmd) { /* Seriously? */
|
2015-07-30 15:43:07 +10:00
|
|
|
if (collmd->bvhtree) {
|
2010-04-11 22:12:30 +00:00
|
|
|
BLI_bvhtree_free(collmd->bvhtree);
|
2015-07-30 15:43:07 +10:00
|
|
|
collmd->bvhtree = NULL;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-07-30 15:43:07 +10:00
|
|
|
MEM_SAFE_FREE(collmd->x);
|
|
|
|
|
MEM_SAFE_FREE(collmd->xnew);
|
|
|
|
|
MEM_SAFE_FREE(collmd->current_x);
|
|
|
|
|
MEM_SAFE_FREE(collmd->current_xnew);
|
|
|
|
|
MEM_SAFE_FREE(collmd->current_v);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-05-08 15:04:10 +02:00
|
|
|
MEM_SAFE_FREE(collmd->tri);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-03-18 15:31:32 +00:00
|
|
|
collmd->time_x = collmd->time_xnew = -1000;
|
2015-07-31 14:00:07 +10:00
|
|
|
collmd->mvert_num = 0;
|
|
|
|
|
collmd->tri_num = 0;
|
2016-10-07 13:27:11 +03:00
|
|
|
collmd->is_static = false;
|
2010-04-11 22:12:30 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 15:27:11 +02:00
|
|
|
static bool dependsOnTime(struct Scene *UNUSED(scene), ModifierData *UNUSED(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
|
|
|
}
|
|
|
|
|
|
2018-05-12 08:04:56 +02:00
|
|
|
static void deformVerts(ModifierData *md,
|
2018-05-12 08:21:07 +02:00
|
|
|
const ModifierEvalContext *ctx,
|
2018-05-17 15:26:59 +02:00
|
|
|
Mesh *mesh,
|
2018-05-12 08:04:56 +02:00
|
|
|
float (*vertexCos)[3],
|
2022-03-28 12:29:47 +11:00
|
|
|
int verts_num)
|
2010-04-11 22:12:30 +00:00
|
|
|
{
|
2012-05-06 13:38:33 +00:00
|
|
|
CollisionModifierData *collmd = (CollisionModifierData *)md;
|
2018-05-17 15:26:59 +02:00
|
|
|
Mesh *mesh_src;
|
2018-05-01 17:33:04 +02:00
|
|
|
Object *ob = ctx->object;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-01-09 21:15:53 +03:00
|
|
|
/* If collision is disabled, free the stale data and exit. */
|
|
|
|
|
if (!ob->pd || !ob->pd->deflect) {
|
|
|
|
|
if (!ob->pd) {
|
|
|
|
|
printf("CollisionModifier: collision settings are missing!\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
freeData(md);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-17 15:26:59 +02:00
|
|
|
if (mesh == NULL) {
|
2022-08-12 22:08:48 +10:00
|
|
|
mesh_src = MOD_deform_mesh_eval_get(ob, NULL, NULL, NULL, verts_num, false);
|
2018-05-17 15:26:59 +02:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
/* Not possible to use get_mesh() in this case as we'll modify its vertices
|
|
|
|
|
* and get_mesh() would return 'mesh' directly. */
|
2020-10-07 14:27:33 +02:00
|
|
|
mesh_src = (Mesh *)BKE_id_copy_ex(NULL, (ID *)mesh, NULL, LIB_ID_COPY_LOCALIZE);
|
2018-05-17 15:26:59 +02:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-05-17 15:26:59 +02:00
|
|
|
if (mesh_src) {
|
2011-02-13 03:21:27 +00:00
|
|
|
float current_time = 0;
|
2019-09-19 13:32:36 +10:00
|
|
|
uint mvert_num = 0;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-08-22 06:28:35 +10:00
|
|
|
BKE_mesh_vert_coords_apply(mesh_src, vertexCos);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-05-17 15:28:18 +02:00
|
|
|
current_time = DEG_get_ctime(ctx->depsgraph);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-01-16 19:41:29 +01:00
|
|
|
if (G.debug & G_DEBUG_SIMDATA) {
|
2011-03-18 15:31:32 +00:00
|
|
|
printf("current_time %f, collmd->time_xnew %f\n", current_time, collmd->time_xnew);
|
2019-01-16 19:41:29 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-05-17 15:26:59 +02:00
|
|
|
mvert_num = mesh_src->totvert;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-26 17:18:16 +02:00
|
|
|
if (current_time < collmd->time_xnew) {
|
|
|
|
|
freeData((ModifierData *)collmd);
|
|
|
|
|
}
|
|
|
|
|
else if (current_time == collmd->time_xnew) {
|
|
|
|
|
if (mvert_num != collmd->mvert_num) {
|
2010-04-11 22:12:30 +00:00
|
|
|
freeData((ModifierData *)collmd);
|
2018-09-26 17:18:16 +02:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
2018-09-26 17:18:16 +02:00
|
|
|
/* check if mesh has changed */
|
2019-04-22 09:15:10 +10:00
|
|
|
if (collmd->x && (mvert_num != collmd->mvert_num)) {
|
2018-09-26 17:18:16 +02:00
|
|
|
freeData((ModifierData *)collmd);
|
2019-04-22 09:15:10 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-26 17:18:16 +02:00
|
|
|
if (collmd->time_xnew == -1000) { /* first time */
|
2019-04-17 06:17:24 +02:00
|
|
|
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
collmd->x = MEM_dupallocN(BKE_mesh_vert_positions(mesh_src)); /* frame start position */
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-26 17:18:16 +02:00
|
|
|
for (uint i = 0; i < mvert_num; i++) {
|
|
|
|
|
/* we save global positions */
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
mul_m4_v3(ob->object_to_world, collmd->x[i]);
|
2018-09-26 17:18:16 +02:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-08-31 13:36:28 +10:00
|
|
|
collmd->xnew = MEM_dupallocN(collmd->x); /* Frame end position. */
|
|
|
|
|
collmd->current_x = MEM_dupallocN(collmd->x); /* Inter-frame. */
|
|
|
|
|
collmd->current_xnew = MEM_dupallocN(collmd->x); /* Inter-frame. */
|
|
|
|
|
collmd->current_v = MEM_dupallocN(collmd->x); /* Inter-frame. */
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-26 17:18:16 +02:00
|
|
|
collmd->mvert_num = mvert_num;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-26 17:18:16 +02:00
|
|
|
{
|
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 *mloop = BKE_mesh_loops(mesh_src);
|
2018-09-26 17:18:16 +02:00
|
|
|
const MLoopTri *looptri = BKE_mesh_runtime_looptri_ensure(mesh_src);
|
|
|
|
|
collmd->tri_num = BKE_mesh_runtime_looptri_len(mesh_src);
|
|
|
|
|
MVertTri *tri = MEM_mallocN(sizeof(*tri) * collmd->tri_num, __func__);
|
|
|
|
|
BKE_mesh_runtime_verttri_from_looptri(tri, mloop, looptri, collmd->tri_num);
|
|
|
|
|
collmd->tri = tri;
|
2010-04-11 22:12:30 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-26 17:18:16 +02:00
|
|
|
/* create bounding box hierarchy */
|
|
|
|
|
collmd->bvhtree = bvhtree_build_from_mvert(
|
2018-10-01 08:42:26 +10:00
|
|
|
collmd->x, collmd->tri, collmd->tri_num, ob->pd->pdef_sboft);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-26 17:18:16 +02:00
|
|
|
collmd->time_x = collmd->time_xnew = current_time;
|
|
|
|
|
collmd->is_static = true;
|
|
|
|
|
}
|
|
|
|
|
else if (mvert_num == collmd->mvert_num) {
|
|
|
|
|
/* put positions to old positions */
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
float(*temp)[3] = collmd->x;
|
2018-09-26 17:18:16 +02:00
|
|
|
collmd->x = collmd->xnew;
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
collmd->xnew = temp;
|
2018-09-26 17:18:16 +02:00
|
|
|
collmd->time_x = collmd->time_xnew;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
memcpy(collmd->xnew, BKE_mesh_vert_positions(mesh_src), mvert_num * sizeof(float[3]));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-26 17:18:16 +02:00
|
|
|
bool is_static = true;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-26 17:18:16 +02:00
|
|
|
for (uint i = 0; i < mvert_num; i++) {
|
|
|
|
|
/* we save global positions */
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
mul_m4_v3(ob->object_to_world, collmd->xnew[i]);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-26 17:18:16 +02:00
|
|
|
/* detect motion */
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
is_static = is_static && equals_v3v3(collmd->x[i], collmd->xnew[i]);
|
2018-09-26 17:18:16 +02:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
memcpy(collmd->current_xnew, collmd->x, mvert_num * sizeof(float[3]));
|
|
|
|
|
memcpy(collmd->current_x, collmd->x, mvert_num * sizeof(float[3]));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-26 17:18:16 +02:00
|
|
|
/* check if GUI setting has changed for bvh */
|
|
|
|
|
if (collmd->bvhtree) {
|
|
|
|
|
if (ob->pd->pdef_sboft != BLI_bvhtree_get_epsilon(collmd->bvhtree)) {
|
|
|
|
|
BLI_bvhtree_free(collmd->bvhtree);
|
2015-07-31 14:00:07 +10:00
|
|
|
collmd->bvhtree = bvhtree_build_from_mvert(
|
2018-10-01 08:42:26 +10:00
|
|
|
collmd->current_x, collmd->tri, collmd->tri_num, ob->pd->pdef_sboft);
|
2010-04-11 22:12:30 +00:00
|
|
|
}
|
2018-09-26 17:18:16 +02:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-02-05 16:23:34 +11:00
|
|
|
/* Happens on file load (ONLY when I un-comment changes in readfile.c) */
|
2018-09-26 17:18:16 +02:00
|
|
|
if (!collmd->bvhtree) {
|
|
|
|
|
collmd->bvhtree = bvhtree_build_from_mvert(
|
2018-10-01 08:42:26 +10:00
|
|
|
collmd->current_x, collmd->tri, collmd->tri_num, ob->pd->pdef_sboft);
|
2010-04-11 22:12:30 +00:00
|
|
|
}
|
2018-09-26 17:18:16 +02:00
|
|
|
else if (!collmd->is_static || !is_static) {
|
|
|
|
|
/* recalc static bounding boxes */
|
|
|
|
|
bvhtree_update_from_mvert(collmd->bvhtree,
|
2018-10-01 08:42:26 +10:00
|
|
|
collmd->current_x,
|
|
|
|
|
collmd->current_xnew,
|
|
|
|
|
collmd->tri,
|
|
|
|
|
collmd->tri_num,
|
|
|
|
|
true);
|
2010-04-11 22:12:30 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-26 17:18:16 +02:00
|
|
|
collmd->is_static = is_static;
|
|
|
|
|
collmd->time_xnew = current_time;
|
2010-04-11 22:12:30 +00:00
|
|
|
}
|
2018-09-26 17:18:16 +02:00
|
|
|
else if (mvert_num != collmd->mvert_num) {
|
2010-04-11 22:12:30 +00:00
|
|
|
freeData((ModifierData *)collmd);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-03-29 16:08:22 +11:00
|
|
|
if (!ELEM(mesh_src, NULL, mesh)) {
|
2018-05-17 15:26:59 +02:00
|
|
|
BKE_id_free(NULL, mesh_src);
|
|
|
|
|
}
|
2010-04-11 22:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
2019-04-04 10:39:12 +02:00
|
|
|
static void updateDepsgraph(ModifierData *UNUSED(md), const ModifierUpdateDepsgraphContext *ctx)
|
|
|
|
|
{
|
2022-08-04 12:11:31 +02:00
|
|
|
DEG_add_depends_on_transform_relation(ctx->node, "Collision Modifier");
|
2019-04-04 10:39:12 +02:00
|
|
|
}
|
2010-04-11 22:12:30 +00:00
|
|
|
|
2020-09-02 14:13:26 -05:00
|
|
|
static void panel_draw(const bContext *UNUSED(C), Panel *panel)
|
2020-06-05 10:41:03 -04:00
|
|
|
{
|
|
|
|
|
uiLayout *layout = panel->layout;
|
|
|
|
|
|
2020-09-02 14:13:26 -05:00
|
|
|
PointerRNA *ptr = modifier_panel_get_property_pointers(panel, NULL);
|
2020-06-05 10:41:03 -04:00
|
|
|
|
2021-11-02 17:50:18 +01:00
|
|
|
uiItemL(layout, TIP_("Settings are inside the Physics tab"), 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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void panelRegister(ARegionType *region_type)
|
|
|
|
|
{
|
|
|
|
|
modifier_panel_register(region_type, eModifierType_Collision, panel_draw);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-23 17:25:44 +02:00
|
|
|
static void blendRead(BlendDataReader *UNUSED(reader), ModifierData *md)
|
|
|
|
|
{
|
|
|
|
|
CollisionModifierData *collmd = (CollisionModifierData *)md;
|
|
|
|
|
#if 0
|
2021-08-26 12:27:14 +10:00
|
|
|
/* TODO: #CollisionModifier should use point-cache
|
|
|
|
|
* + have proper reset events before enabling this. */
|
2020-06-23 17:25:44 +02:00
|
|
|
collmd->x = newdataadr(fd, collmd->x);
|
|
|
|
|
collmd->xnew = newdataadr(fd, collmd->xnew);
|
|
|
|
|
collmd->mfaces = newdataadr(fd, collmd->mfaces);
|
|
|
|
|
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
collmd->current_x = MEM_calloc_arrayN(collmd->mvert_num, sizeof(float[3]), "current_x");
|
|
|
|
|
collmd->current_xnew = MEM_calloc_arrayN(collmd->mvert_num, sizeof(float[3]), "current_xnew");
|
|
|
|
|
collmd->current_v = MEM_calloc_arrayN(collmd->mvert_num, sizeof(float[3]), "current_v");
|
2020-06-23 17:25:44 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
collmd->x = NULL;
|
|
|
|
|
collmd->xnew = NULL;
|
|
|
|
|
collmd->current_x = NULL;
|
|
|
|
|
collmd->current_xnew = NULL;
|
|
|
|
|
collmd->current_v = NULL;
|
|
|
|
|
collmd->time_x = collmd->time_xnew = -1000;
|
|
|
|
|
collmd->mvert_num = 0;
|
|
|
|
|
collmd->tri_num = 0;
|
|
|
|
|
collmd->is_static = false;
|
|
|
|
|
collmd->bvhtree = NULL;
|
|
|
|
|
collmd->tri = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-11 22:12:30 +00:00
|
|
|
ModifierTypeInfo modifierType_Collision = {
|
2023-01-16 12:41:11 +11:00
|
|
|
/*name*/ N_("Collision"),
|
|
|
|
|
/*structName*/ "CollisionModifierData",
|
|
|
|
|
/*structSize*/ sizeof(CollisionModifierData),
|
|
|
|
|
/*srna*/ &RNA_CollisionModifier,
|
|
|
|
|
/*type*/ eModifierTypeType_OnlyDeform,
|
|
|
|
|
/*flags*/ eModifierTypeFlag_AcceptsMesh | eModifierTypeFlag_Single,
|
|
|
|
|
/*icon*/ ICON_MOD_PHYSICS,
|
|
|
|
|
|
|
|
|
|
/*copyData*/ NULL,
|
|
|
|
|
|
|
|
|
|
/*deformVerts*/ deformVerts,
|
|
|
|
|
/*deformMatrices*/ NULL,
|
|
|
|
|
/*deformVertsEM*/ NULL,
|
|
|
|
|
/*deformMatricesEM*/ NULL,
|
|
|
|
|
/*modifyMesh*/ NULL,
|
|
|
|
|
/*modifyGeometrySet*/ NULL,
|
|
|
|
|
|
|
|
|
|
/*initData*/ initData,
|
|
|
|
|
/*requiredDataMask*/ NULL,
|
|
|
|
|
/*freeData*/ freeData,
|
|
|
|
|
/*isDisabled*/ NULL,
|
|
|
|
|
/*updateDepsgraph*/ updateDepsgraph,
|
|
|
|
|
/*dependsOnTime*/ dependsOnTime,
|
|
|
|
|
/*dependsOnNormals*/ NULL,
|
|
|
|
|
/*foreachIDLink*/ NULL,
|
|
|
|
|
/*foreachTexLink*/ NULL,
|
|
|
|
|
/*freeRuntimeData*/ NULL,
|
|
|
|
|
/*panelRegister*/ panelRegister,
|
|
|
|
|
/*blendWrite*/ NULL,
|
|
|
|
|
/*blendRead*/ blendRead,
|
2010-04-11 22:12:30 +00:00
|
|
|
};
|