2022-02-11 09:07:11 +11:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
* Copyright 2005 Blender Foundation. All rights reserved. */
|
2018-05-08 10:07:21 +02:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup bke
|
2018-05-08 10:07:21 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "atomic_ops.h"
|
|
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
|
|
|
|
#include "DNA_mesh_types.h"
|
|
|
|
|
#include "DNA_meshdata_types.h"
|
2018-06-05 16:58:08 +02:00
|
|
|
#include "DNA_object_types.h"
|
2018-05-08 10:07:21 +02:00
|
|
|
|
|
|
|
|
#include "BLI_math_geom.h"
|
2022-04-13 17:51:05 -05:00
|
|
|
#include "BLI_task.hh"
|
2018-05-08 10:07:21 +02:00
|
|
|
|
2018-05-11 15:48:14 -03:00
|
|
|
#include "BKE_bvhutils.h"
|
2020-02-10 12:58:59 +01:00
|
|
|
#include "BKE_lib_id.h"
|
2018-05-08 10:07:21 +02:00
|
|
|
#include "BKE_mesh.h"
|
2018-06-05 15:59:53 +02:00
|
|
|
#include "BKE_mesh_runtime.h"
|
Shrinkwrap: new mode that projects along the target normal.
The Nearest Surface Point shrink method, while fast, is neither
smooth nor continuous: as the source point moves, the projected
point can both stop and jump. This causes distortions in the
deformation of the shrinkwrap modifier, and the motion of an
animated object with a shrinkwrap constraint.
This patch implements a new mode, which, instead of using the simple
nearest point search, iteratively solves an equation for each triangle
to find a point which has its interpolated normal point to or from the
original vertex. Non-manifold boundary edges are treated as infinitely
thin cylinders that cast normals in all perpendicular directions.
Since this is useful for the constraint, and having multiple
objects with constraints targeting the same guide mesh is a quite
reasonable use case, rather than calculating the mesh boundary edge
data over and over again, it is precomputed and cached in the mesh.
Reviewers: mont29
Differential Revision: https://developer.blender.org/D3836
2018-11-06 21:04:53 +03:00
|
|
|
#include "BKE_shrinkwrap.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BKE_subdiv_ccg.h"
|
2018-05-08 10:07:21 +02:00
|
|
|
|
2018-06-05 15:54:12 +02:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Mesh Runtime Struct Utils
|
|
|
|
|
* \{ */
|
2018-05-08 10:07:21 +02:00
|
|
|
|
2018-05-09 15:38:58 +02:00
|
|
|
/**
|
2021-11-10 13:50:00 +01:00
|
|
|
* \brief Initialize the runtime mutexes of the given mesh.
|
|
|
|
|
*
|
|
|
|
|
* Any existing mutexes will be overridden.
|
2018-05-09 15:38:58 +02:00
|
|
|
*/
|
2021-11-10 13:50:00 +01:00
|
|
|
static void mesh_runtime_init_mutexes(Mesh *mesh)
|
2018-05-09 15:38:58 +02:00
|
|
|
{
|
2022-04-13 17:51:05 -05:00
|
|
|
mesh->runtime.eval_mutex = MEM_new<ThreadMutex>("mesh runtime eval_mutex");
|
|
|
|
|
BLI_mutex_init(static_cast<ThreadMutex *>(mesh->runtime.eval_mutex));
|
|
|
|
|
mesh->runtime.normals_mutex = MEM_new<ThreadMutex>("mesh runtime normals_mutex");
|
|
|
|
|
BLI_mutex_init(static_cast<ThreadMutex *>(mesh->runtime.normals_mutex));
|
|
|
|
|
mesh->runtime.render_mutex = MEM_new<ThreadMutex>("mesh runtime render_mutex");
|
|
|
|
|
BLI_mutex_init(static_cast<ThreadMutex *>(mesh->runtime.render_mutex));
|
2018-05-09 15:38:58 +02:00
|
|
|
}
|
2018-05-08 10:07:21 +02:00
|
|
|
|
2021-11-10 13:50:00 +01:00
|
|
|
/**
|
|
|
|
|
* \brief free the mutexes of the given mesh runtime.
|
|
|
|
|
*/
|
|
|
|
|
static void mesh_runtime_free_mutexes(Mesh *mesh)
|
|
|
|
|
{
|
2022-04-13 17:51:05 -05:00
|
|
|
if (mesh->runtime.eval_mutex != nullptr) {
|
|
|
|
|
BLI_mutex_end(static_cast<ThreadMutex *>(mesh->runtime.eval_mutex));
|
2021-11-10 13:50:00 +01:00
|
|
|
MEM_freeN(mesh->runtime.eval_mutex);
|
2022-04-13 17:51:05 -05:00
|
|
|
mesh->runtime.eval_mutex = nullptr;
|
2021-11-10 13:50:00 +01:00
|
|
|
}
|
2022-04-13 17:51:05 -05:00
|
|
|
if (mesh->runtime.normals_mutex != nullptr) {
|
|
|
|
|
BLI_mutex_end(static_cast<ThreadMutex *>(mesh->runtime.normals_mutex));
|
Refactor: Move normals out of MVert, lazy calculation
As described in T91186, this commit moves mesh vertex normals into a
contiguous array of float vectors in a custom data layer, how face
normals are currently stored.
The main interface is documented in `BKE_mesh.h`. Vertex and face
normals are now calculated on-demand and cached, retrieved with an
"ensure" function. Since the logical state of a mesh is now "has
normals when necessary", they can be retrieved from a `const` mesh.
The goal is to use on-demand calculation for all derived data, but
leave room for eager calculation for performance purposes (modifier
evaluation is threaded, but viewport data generation is not).
**Benefits**
This moves us closer to a SoA approach rather than the current AoS
paradigm. Accessing a contiguous `float3` is much more efficient than
retrieving data from a larger struct. The memory requirements for
accessing only normals or vertex locations are smaller, and at the
cost of more memory usage for just normals, they now don't have to
be converted between float and short, which also simplifies code
In the future, the remaining items can be removed from `MVert`,
leaving only `float3`, which has similar benefits (see T93602).
Removing the combination of derived and original data makes it
conceptually simpler to only calculate normals when necessary.
This is especially important now that we have more opportunities
for temporary meshes in geometry nodes.
**Performance**
In addition to the theoretical future performance improvements by
making `MVert == float3`, I've done some basic performance testing
on this patch directly. The data is fairly rough, but it gives an idea
about where things stand generally.
- Mesh line primitive 4m Verts: 1.16x faster (36 -> 31 ms),
showing that accessing just `MVert` is now more efficient.
- Spring Splash Screen: 1.03-1.06 -> 1.06-1.11 FPS, a very slight
change that at least shows there is no regression.
- Sprite Fright Snail Smoosh: 3.30-3.40 -> 3.42-3.50 FPS, a small
but observable speedup.
- Set Position Node with Scaled Normal: 1.36x faster (53 -> 39 ms),
shows that using normals in geometry nodes is faster.
- Normal Calculation 1.6m Vert Cube: 1.19x faster (25 -> 21 ms),
shows that calculating normals is slightly faster now.
- File Size of 1.6m Vert Cube: 1.03x smaller (214.7 -> 208.4 MB),
Normals are not saved in files, which can help with large meshes.
As for memory usage, it may be slightly more in some cases, but
I didn't observe any difference in the production files I tested.
**Tests**
Some modifiers and cycles test results need to be updated with this
commit, for two reasons:
- The subdivision surface modifier is not responsible for calculating
normals anymore. In master, the modifier creates different normals
than the result of the `Mesh` normal calculation, so this is a bug
fix.
- There are small differences in the results of some modifiers that
use normals because they are not converted to and from `short`
anymore.
**Future improvements**
- Remove `ModifierTypeInfo::dependsOnNormals`. Code in each modifier
already retrieves normals if they are needed anyway.
- Copy normals as part of a better CoW system for attributes.
- Make more areas use lazy instead of eager normal calculation.
- Remove `BKE_mesh_normals_tag_dirty` in more places since that is
now the default state of a new mesh.
- Possibly apply a similar change to derived face corner normals.
Differential Revision: https://developer.blender.org/D12770
2022-01-13 14:37:58 -06:00
|
|
|
MEM_freeN(mesh->runtime.normals_mutex);
|
2022-04-13 17:51:05 -05:00
|
|
|
mesh->runtime.normals_mutex = nullptr;
|
Refactor: Move normals out of MVert, lazy calculation
As described in T91186, this commit moves mesh vertex normals into a
contiguous array of float vectors in a custom data layer, how face
normals are currently stored.
The main interface is documented in `BKE_mesh.h`. Vertex and face
normals are now calculated on-demand and cached, retrieved with an
"ensure" function. Since the logical state of a mesh is now "has
normals when necessary", they can be retrieved from a `const` mesh.
The goal is to use on-demand calculation for all derived data, but
leave room for eager calculation for performance purposes (modifier
evaluation is threaded, but viewport data generation is not).
**Benefits**
This moves us closer to a SoA approach rather than the current AoS
paradigm. Accessing a contiguous `float3` is much more efficient than
retrieving data from a larger struct. The memory requirements for
accessing only normals or vertex locations are smaller, and at the
cost of more memory usage for just normals, they now don't have to
be converted between float and short, which also simplifies code
In the future, the remaining items can be removed from `MVert`,
leaving only `float3`, which has similar benefits (see T93602).
Removing the combination of derived and original data makes it
conceptually simpler to only calculate normals when necessary.
This is especially important now that we have more opportunities
for temporary meshes in geometry nodes.
**Performance**
In addition to the theoretical future performance improvements by
making `MVert == float3`, I've done some basic performance testing
on this patch directly. The data is fairly rough, but it gives an idea
about where things stand generally.
- Mesh line primitive 4m Verts: 1.16x faster (36 -> 31 ms),
showing that accessing just `MVert` is now more efficient.
- Spring Splash Screen: 1.03-1.06 -> 1.06-1.11 FPS, a very slight
change that at least shows there is no regression.
- Sprite Fright Snail Smoosh: 3.30-3.40 -> 3.42-3.50 FPS, a small
but observable speedup.
- Set Position Node with Scaled Normal: 1.36x faster (53 -> 39 ms),
shows that using normals in geometry nodes is faster.
- Normal Calculation 1.6m Vert Cube: 1.19x faster (25 -> 21 ms),
shows that calculating normals is slightly faster now.
- File Size of 1.6m Vert Cube: 1.03x smaller (214.7 -> 208.4 MB),
Normals are not saved in files, which can help with large meshes.
As for memory usage, it may be slightly more in some cases, but
I didn't observe any difference in the production files I tested.
**Tests**
Some modifiers and cycles test results need to be updated with this
commit, for two reasons:
- The subdivision surface modifier is not responsible for calculating
normals anymore. In master, the modifier creates different normals
than the result of the `Mesh` normal calculation, so this is a bug
fix.
- There are small differences in the results of some modifiers that
use normals because they are not converted to and from `short`
anymore.
**Future improvements**
- Remove `ModifierTypeInfo::dependsOnNormals`. Code in each modifier
already retrieves normals if they are needed anyway.
- Copy normals as part of a better CoW system for attributes.
- Make more areas use lazy instead of eager normal calculation.
- Remove `BKE_mesh_normals_tag_dirty` in more places since that is
now the default state of a new mesh.
- Possibly apply a similar change to derived face corner normals.
Differential Revision: https://developer.blender.org/D12770
2022-01-13 14:37:58 -06:00
|
|
|
}
|
2022-04-13 17:51:05 -05:00
|
|
|
if (mesh->runtime.render_mutex != nullptr) {
|
|
|
|
|
BLI_mutex_end(static_cast<ThreadMutex *>(mesh->runtime.render_mutex));
|
2021-11-10 13:50:00 +01:00
|
|
|
MEM_freeN(mesh->runtime.render_mutex);
|
2022-04-13 17:51:05 -05:00
|
|
|
mesh->runtime.render_mutex = nullptr;
|
2021-11-10 13:50:00 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BKE_mesh_runtime_init_data(Mesh *mesh)
|
|
|
|
|
{
|
|
|
|
|
mesh_runtime_init_mutexes(mesh);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BKE_mesh_runtime_free_data(Mesh *mesh)
|
|
|
|
|
{
|
|
|
|
|
BKE_mesh_runtime_clear_cache(mesh);
|
|
|
|
|
mesh_runtime_free_mutexes(mesh);
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-13 14:29:27 +01:00
|
|
|
void BKE_mesh_runtime_reset_on_copy(Mesh *mesh, const int UNUSED(flag))
|
2018-12-17 12:28:16 +01:00
|
|
|
{
|
|
|
|
|
Mesh_Runtime *runtime = &mesh->runtime;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-04-13 17:51:05 -05:00
|
|
|
runtime->mesh_eval = nullptr;
|
|
|
|
|
runtime->edit_data = nullptr;
|
|
|
|
|
runtime->batch_cache = nullptr;
|
|
|
|
|
runtime->subdiv_ccg = nullptr;
|
|
|
|
|
runtime->looptris = blender::dna::shallow_zero_initialize();
|
|
|
|
|
runtime->bvh_cache = nullptr;
|
|
|
|
|
runtime->shrinkwrap_data = nullptr;
|
2022-04-18 23:48:43 -05:00
|
|
|
runtime->subsurf_face_dot_tags = nullptr;
|
2019-05-27 11:45:33 +02:00
|
|
|
|
2022-02-22 12:43:53 -05:00
|
|
|
runtime->vert_normals_dirty = true;
|
|
|
|
|
runtime->poly_normals_dirty = true;
|
2022-04-13 17:51:05 -05:00
|
|
|
runtime->vert_normals = nullptr;
|
|
|
|
|
runtime->poly_normals = nullptr;
|
2022-02-22 12:43:53 -05:00
|
|
|
|
2021-11-10 13:50:00 +01:00
|
|
|
mesh_runtime_init_mutexes(mesh);
|
2018-12-17 12:28:16 +01:00
|
|
|
}
|
|
|
|
|
|
2018-05-11 15:48:14 -03:00
|
|
|
void BKE_mesh_runtime_clear_cache(Mesh *mesh)
|
|
|
|
|
{
|
2022-04-13 17:51:05 -05:00
|
|
|
if (mesh->runtime.mesh_eval != nullptr) {
|
|
|
|
|
mesh->runtime.mesh_eval->edit_mesh = nullptr;
|
|
|
|
|
BKE_id_free(nullptr, mesh->runtime.mesh_eval);
|
|
|
|
|
mesh->runtime.mesh_eval = nullptr;
|
2019-05-27 11:45:33 +02:00
|
|
|
}
|
2018-05-11 15:48:14 -03:00
|
|
|
BKE_mesh_runtime_clear_geometry(mesh);
|
|
|
|
|
BKE_mesh_batch_cache_free(mesh);
|
|
|
|
|
BKE_mesh_runtime_clear_edit_data(mesh);
|
2022-02-22 12:43:53 -05:00
|
|
|
BKE_mesh_clear_derived_normals(mesh);
|
2018-05-11 15:48:14 -03:00
|
|
|
}
|
|
|
|
|
|
2018-05-08 10:07:21 +02:00
|
|
|
/**
|
|
|
|
|
* Ensure the array is large enough
|
|
|
|
|
*
|
2019-04-27 12:07:07 +10:00
|
|
|
* \note This function must always be thread-protected by caller.
|
|
|
|
|
* It should only be used by internal code.
|
2018-05-08 10:07:21 +02:00
|
|
|
*/
|
|
|
|
|
static void mesh_ensure_looptri_data(Mesh *mesh)
|
|
|
|
|
{
|
2021-12-07 17:19:15 +11:00
|
|
|
/* This is a ported copy of `DM_ensure_looptri_data(dm)`. */
|
2021-06-18 14:27:41 +10:00
|
|
|
const uint totpoly = mesh->totpoly;
|
2018-05-08 19:36:02 +02:00
|
|
|
const int looptris_len = poly_to_tri_count(totpoly, mesh->totloop);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-04-13 17:51:05 -05:00
|
|
|
BLI_assert(mesh->runtime.looptris.array_wip == nullptr);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-05-08 10:07:21 +02:00
|
|
|
SWAP(MLoopTri *, mesh->runtime.looptris.array, mesh->runtime.looptris.array_wip);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-05-08 19:36:02 +02:00
|
|
|
if ((looptris_len > mesh->runtime.looptris.len_alloc) ||
|
|
|
|
|
(looptris_len < mesh->runtime.looptris.len_alloc * 2) || (totpoly == 0)) {
|
2018-05-08 10:07:21 +02:00
|
|
|
MEM_SAFE_FREE(mesh->runtime.looptris.array_wip);
|
2018-05-08 19:36:02 +02:00
|
|
|
mesh->runtime.looptris.len_alloc = 0;
|
|
|
|
|
mesh->runtime.looptris.len = 0;
|
2018-05-08 10:07:21 +02:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-05-08 10:07:21 +02:00
|
|
|
if (totpoly) {
|
2022-04-13 17:51:05 -05:00
|
|
|
if (mesh->runtime.looptris.array_wip == nullptr) {
|
|
|
|
|
mesh->runtime.looptris.array_wip = static_cast<MLoopTri *>(
|
|
|
|
|
MEM_malloc_arrayN(looptris_len, sizeof(*mesh->runtime.looptris.array_wip), __func__));
|
2018-05-08 19:36:02 +02:00
|
|
|
mesh->runtime.looptris.len_alloc = looptris_len;
|
2018-05-08 10:07:21 +02:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-05-08 19:36:02 +02:00
|
|
|
mesh->runtime.looptris.len = looptris_len;
|
2018-05-08 10:07:21 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-08 19:26:36 +02:00
|
|
|
void BKE_mesh_runtime_looptri_recalc(Mesh *mesh)
|
2018-05-08 10:07:21 +02:00
|
|
|
{
|
|
|
|
|
mesh_ensure_looptri_data(mesh);
|
2022-04-13 17:51:05 -05:00
|
|
|
BLI_assert(mesh->totpoly == 0 || mesh->runtime.looptris.array_wip != nullptr);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-05-08 10:07:21 +02:00
|
|
|
BKE_mesh_recalc_looptri(mesh->mloop,
|
|
|
|
|
mesh->mpoly,
|
|
|
|
|
mesh->mvert,
|
|
|
|
|
mesh->totloop,
|
|
|
|
|
mesh->totpoly,
|
|
|
|
|
mesh->runtime.looptris.array_wip);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-04-13 17:51:05 -05:00
|
|
|
BLI_assert(mesh->runtime.looptris.array == nullptr);
|
2018-05-08 10:07:21 +02:00
|
|
|
atomic_cas_ptr((void **)&mesh->runtime.looptris.array,
|
|
|
|
|
mesh->runtime.looptris.array,
|
|
|
|
|
mesh->runtime.looptris.array_wip);
|
2022-04-13 17:51:05 -05:00
|
|
|
mesh->runtime.looptris.array_wip = nullptr;
|
2018-05-08 10:07:21 +02:00
|
|
|
}
|
|
|
|
|
|
2018-05-08 19:26:36 +02:00
|
|
|
int BKE_mesh_runtime_looptri_len(const Mesh *mesh)
|
2018-05-08 10:07:21 +02:00
|
|
|
{
|
2021-12-07 17:19:15 +11:00
|
|
|
/* This is a ported copy of `dm_getNumLoopTri(dm)`. */
|
2018-05-08 19:26:36 +02:00
|
|
|
const int looptri_len = poly_to_tri_count(mesh->totpoly, mesh->totloop);
|
2018-05-08 19:36:02 +02:00
|
|
|
BLI_assert(ELEM(mesh->runtime.looptris.len, 0, looptri_len));
|
2018-05-08 19:26:36 +02:00
|
|
|
return looptri_len;
|
2018-05-08 10:07:21 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-02 11:37:01 -05:00
|
|
|
const MLoopTri *BKE_mesh_runtime_looptri_ensure(const Mesh *mesh)
|
2018-05-08 10:07:21 +02:00
|
|
|
{
|
2020-07-29 17:36:27 +02:00
|
|
|
ThreadMutex *mesh_eval_mutex = (ThreadMutex *)mesh->runtime.eval_mutex;
|
|
|
|
|
BLI_mutex_lock(mesh_eval_mutex);
|
|
|
|
|
|
2020-12-27 21:46:57 -06:00
|
|
|
MLoopTri *looptri = mesh->runtime.looptris.array;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-04-13 17:51:05 -05:00
|
|
|
if (looptri != nullptr) {
|
2018-05-08 19:36:02 +02:00
|
|
|
BLI_assert(BKE_mesh_runtime_looptri_len(mesh) == mesh->runtime.looptris.len);
|
2018-05-08 10:07:21 +02:00
|
|
|
}
|
|
|
|
|
else {
|
2021-06-22 18:17:48 +02:00
|
|
|
/* Must isolate multithreaded tasks while holding a mutex lock. */
|
2022-04-13 17:51:05 -05:00
|
|
|
blender::threading::isolate_task(
|
|
|
|
|
[&]() { BKE_mesh_runtime_looptri_recalc(const_cast<Mesh *>(mesh)); });
|
2018-05-08 10:07:21 +02:00
|
|
|
looptri = mesh->runtime.looptris.array;
|
|
|
|
|
}
|
2020-07-29 17:36:27 +02:00
|
|
|
|
|
|
|
|
BLI_mutex_unlock(mesh_eval_mutex);
|
|
|
|
|
|
2018-05-08 10:07:21 +02:00
|
|
|
return looptri;
|
|
|
|
|
}
|
2018-05-11 15:48:14 -03:00
|
|
|
|
2018-06-05 15:54:12 +02:00
|
|
|
void BKE_mesh_runtime_verttri_from_looptri(MVertTri *r_verttri,
|
|
|
|
|
const MLoop *mloop,
|
|
|
|
|
const MLoopTri *looptri,
|
|
|
|
|
int looptri_num)
|
2018-05-17 15:26:59 +02:00
|
|
|
{
|
2020-12-27 21:46:57 -06:00
|
|
|
for (int i = 0; i < looptri_num; i++) {
|
2018-05-17 15:26:59 +02:00
|
|
|
r_verttri[i].tri[0] = mloop[looptri[i].tri[0]].v;
|
|
|
|
|
r_verttri[i].tri[1] = mloop[looptri[i].tri[1]].v;
|
|
|
|
|
r_verttri[i].tri[2] = mloop[looptri[i].tri[2]].v;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-11 15:48:14 -03:00
|
|
|
bool BKE_mesh_runtime_ensure_edit_data(struct Mesh *mesh)
|
|
|
|
|
{
|
2022-04-13 17:51:05 -05:00
|
|
|
if (mesh->runtime.edit_data != nullptr) {
|
2018-05-11 15:48:14 -03:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-13 17:51:05 -05:00
|
|
|
mesh->runtime.edit_data = MEM_cnew<EditMeshData>(__func__);
|
2018-05-11 15:48:14 -03:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-24 22:09:40 +10:00
|
|
|
bool BKE_mesh_runtime_reset_edit_data(Mesh *mesh)
|
|
|
|
|
{
|
|
|
|
|
EditMeshData *edit_data = mesh->runtime.edit_data;
|
2022-04-13 17:51:05 -05:00
|
|
|
if (edit_data == nullptr) {
|
2020-06-24 22:09:40 +10:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MEM_SAFE_FREE(edit_data->polyCos);
|
|
|
|
|
MEM_SAFE_FREE(edit_data->polyNos);
|
|
|
|
|
MEM_SAFE_FREE(edit_data->vertexCos);
|
|
|
|
|
MEM_SAFE_FREE(edit_data->vertexNos);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-11 15:48:14 -03:00
|
|
|
bool BKE_mesh_runtime_clear_edit_data(Mesh *mesh)
|
|
|
|
|
{
|
2022-04-13 17:51:05 -05:00
|
|
|
if (mesh->runtime.edit_data == nullptr) {
|
2018-05-11 15:48:14 -03:00
|
|
|
return false;
|
|
|
|
|
}
|
2020-06-24 22:09:40 +10:00
|
|
|
BKE_mesh_runtime_reset_edit_data(mesh);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-06-24 22:09:40 +10:00
|
|
|
MEM_freeN(mesh->runtime.edit_data);
|
2022-04-13 17:51:05 -05:00
|
|
|
mesh->runtime.edit_data = nullptr;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-05-11 15:48:14 -03:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BKE_mesh_runtime_clear_geometry(Mesh *mesh)
|
|
|
|
|
{
|
2020-06-02 15:59:30 +02:00
|
|
|
if (mesh->runtime.bvh_cache) {
|
|
|
|
|
bvhcache_free(mesh->runtime.bvh_cache);
|
2022-04-13 17:51:05 -05:00
|
|
|
mesh->runtime.bvh_cache = nullptr;
|
2020-06-02 15:59:30 +02:00
|
|
|
}
|
2018-05-11 15:48:14 -03:00
|
|
|
MEM_SAFE_FREE(mesh->runtime.looptris.array);
|
2018-09-06 17:06:17 +02:00
|
|
|
/* TODO(sergey): Does this really belong here? */
|
2022-04-13 17:51:05 -05:00
|
|
|
if (mesh->runtime.subdiv_ccg != nullptr) {
|
2018-09-25 10:32:34 +02:00
|
|
|
BKE_subdiv_ccg_destroy(mesh->runtime.subdiv_ccg);
|
2022-04-13 17:51:05 -05:00
|
|
|
mesh->runtime.subdiv_ccg = nullptr;
|
2018-09-06 17:06:17 +02:00
|
|
|
}
|
Shrinkwrap: new mode that projects along the target normal.
The Nearest Surface Point shrink method, while fast, is neither
smooth nor continuous: as the source point moves, the projected
point can both stop and jump. This causes distortions in the
deformation of the shrinkwrap modifier, and the motion of an
animated object with a shrinkwrap constraint.
This patch implements a new mode, which, instead of using the simple
nearest point search, iteratively solves an equation for each triangle
to find a point which has its interpolated normal point to or from the
original vertex. Non-manifold boundary edges are treated as infinitely
thin cylinders that cast normals in all perpendicular directions.
Since this is useful for the constraint, and having multiple
objects with constraints targeting the same guide mesh is a quite
reasonable use case, rather than calculating the mesh boundary edge
data over and over again, it is precomputed and cached in the mesh.
Reviewers: mont29
Differential Revision: https://developer.blender.org/D3836
2018-11-06 21:04:53 +03:00
|
|
|
BKE_shrinkwrap_discard_boundary_data(mesh);
|
2022-04-18 23:48:43 -05:00
|
|
|
|
|
|
|
|
MEM_SAFE_FREE(mesh->runtime.subsurf_face_dot_tags);
|
2018-05-11 15:48:14 -03:00
|
|
|
}
|
|
|
|
|
|
2018-06-05 15:54:12 +02:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Mesh Batch Cache Callbacks
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2018-05-11 15:48:14 -03:00
|
|
|
/* Draw Engine */
|
2022-04-13 17:51:05 -05:00
|
|
|
void (*BKE_mesh_batch_cache_dirty_tag_cb)(Mesh *me, eMeshBatchDirtyMode mode) = nullptr;
|
|
|
|
|
void (*BKE_mesh_batch_cache_free_cb)(Mesh *me) = nullptr;
|
2018-05-11 15:48:14 -03:00
|
|
|
|
2020-10-09 07:27:18 +02:00
|
|
|
void BKE_mesh_batch_cache_dirty_tag(Mesh *me, eMeshBatchDirtyMode mode)
|
2018-05-11 15:48:14 -03:00
|
|
|
{
|
|
|
|
|
if (me->runtime.batch_cache) {
|
2018-08-23 10:14:29 -03:00
|
|
|
BKE_mesh_batch_cache_dirty_tag_cb(me, mode);
|
2018-05-11 15:48:14 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void BKE_mesh_batch_cache_free(Mesh *me)
|
|
|
|
|
{
|
|
|
|
|
if (me->runtime.batch_cache) {
|
|
|
|
|
BKE_mesh_batch_cache_free_cb(me);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-06-05 15:54:12 +02:00
|
|
|
|
|
|
|
|
/** \} */
|
2018-06-22 17:54:18 +02:00
|
|
|
|
2018-06-05 15:54:12 +02:00
|
|
|
/* -------------------------------------------------------------------- */
|
2022-01-19 15:09:48 +11:00
|
|
|
/** \name Mesh Runtime Validation
|
2018-06-22 17:54:18 +02:00
|
|
|
* \{ */
|
2021-12-14 15:49:31 +11:00
|
|
|
|
2018-06-22 17:54:18 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
|
|
|
|
|
|
bool BKE_mesh_runtime_is_valid(Mesh *me_eval)
|
|
|
|
|
{
|
|
|
|
|
const bool do_verbose = true;
|
|
|
|
|
const bool do_fixes = false;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-06-22 17:54:18 +02:00
|
|
|
bool is_valid = true;
|
|
|
|
|
bool changed = true;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-06-22 17:54:18 +02:00
|
|
|
if (do_verbose) {
|
|
|
|
|
printf("MESH: %s\n", me_eval->id.name + 2);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-06-22 17:54:18 +02:00
|
|
|
is_valid &= BKE_mesh_validate_all_customdata(
|
2018-12-03 16:19:08 +01:00
|
|
|
&me_eval->vdata,
|
|
|
|
|
me_eval->totvert,
|
|
|
|
|
&me_eval->edata,
|
|
|
|
|
me_eval->totedge,
|
|
|
|
|
&me_eval->ldata,
|
|
|
|
|
me_eval->totloop,
|
|
|
|
|
&me_eval->pdata,
|
|
|
|
|
me_eval->totpoly,
|
2018-07-13 08:37:20 +02:00
|
|
|
false, /* setting mask here isn't useful, gives false positives */
|
|
|
|
|
do_verbose,
|
|
|
|
|
do_fixes,
|
|
|
|
|
&changed);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-06-22 17:54:18 +02:00
|
|
|
is_valid &= BKE_mesh_validate_arrays(me_eval,
|
2018-07-13 08:37:20 +02:00
|
|
|
me_eval->mvert,
|
|
|
|
|
me_eval->totvert,
|
|
|
|
|
me_eval->medge,
|
|
|
|
|
me_eval->totedge,
|
|
|
|
|
me_eval->mface,
|
|
|
|
|
me_eval->totface,
|
|
|
|
|
me_eval->mloop,
|
|
|
|
|
me_eval->totloop,
|
|
|
|
|
me_eval->mpoly,
|
|
|
|
|
me_eval->totpoly,
|
|
|
|
|
me_eval->dvert,
|
|
|
|
|
do_verbose,
|
|
|
|
|
do_fixes,
|
|
|
|
|
&changed);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-06-22 17:54:18 +02:00
|
|
|
BLI_assert(changed == false);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-06-22 17:54:18 +02:00
|
|
|
return is_valid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif /* NDEBUG */
|
|
|
|
|
|
|
|
|
|
/** \} */
|