Bounding box calculation can be a large in some situations, especially instancing. This patch caches the min and max of the bounding box in runtime data of meshes, point clouds, and curves, implementing part of T96968. Bounds are now calculated lazily-- only after they are tagged dirty. Also, cached bounds are also shared when copying geometry data-blocks that have equivalent data. When bounds are calculated on an evaluated data-block, they are also accessible on the original, and the next evaluated ID will also share them. A geometry will stop sharing bounds as soon as its positions (or radii) are changed. Just caching the bounds gave a 2-3x speedup with thousands of mesh geometry instances in the viewport. Sharing the bounds can eliminate recalculations entirely in cases like copying meshes in geometry nodes or the selection paint brush in curves sculpt mode, which causes a reevaluation but doesn't change the positions. **Implementation** The sharing is achieved with a `shared_ptr` that points to a cache mutex (from D16419) and the cached bounds data. When geometries are copied, the bounds are shared by default, and only "un-shared" when the bounds are tagged dirty. Point clouds have a new runtime struct to store this data. Functions for tagging the data dirty are improved for added for point clouds and improved for curves. A missing tag has also been fixed for mesh sculpt mode. **Future** There are further improvements which can be worked on next - Apply changes to volume objects and other types where it makes sense - Continue cleanup changes described in T96968 - Apply shared cache design to more expensive data like triangulation or normals Differential Revision: https://developer.blender.org/D16204
146 lines
3.5 KiB
C++
146 lines
3.5 KiB
C++
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
|
|
/** \file
|
|
* \ingroup bke
|
|
*
|
|
* Manage edit mesh cache: #EditMeshData
|
|
*/
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
#include "BLI_bounds.hh"
|
|
#include "BLI_math_vector.h"
|
|
#include "BLI_span.hh"
|
|
|
|
#include "DNA_mesh_types.h"
|
|
|
|
#include "BKE_editmesh.h"
|
|
#include "BKE_editmesh_cache.h" /* own include */
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
/** \name Ensure Data (derived from coords)
|
|
* \{ */
|
|
|
|
void BKE_editmesh_cache_ensure_poly_normals(BMEditMesh *em, EditMeshData *emd)
|
|
{
|
|
if (!(emd->vertexCos && (emd->polyNos == nullptr))) {
|
|
return;
|
|
}
|
|
|
|
BMesh *bm = em->bm;
|
|
BMFace *efa;
|
|
BMIter fiter;
|
|
int i;
|
|
|
|
BM_mesh_elem_index_ensure(bm, BM_VERT);
|
|
|
|
float(*polyNos)[3] = static_cast<float(*)[3]>(
|
|
MEM_mallocN(sizeof(*polyNos) * bm->totface, __func__));
|
|
|
|
const float(*vertexCos)[3] = emd->vertexCos;
|
|
|
|
BM_ITER_MESH_INDEX (efa, &fiter, bm, BM_FACES_OF_MESH, i) {
|
|
BM_elem_index_set(efa, i); /* set_inline */
|
|
BM_face_calc_normal_vcos(bm, efa, polyNos[i], vertexCos);
|
|
}
|
|
bm->elem_index_dirty &= ~BM_FACE;
|
|
|
|
emd->polyNos = (const float(*)[3])polyNos;
|
|
}
|
|
|
|
void BKE_editmesh_cache_ensure_vert_normals(BMEditMesh *em, EditMeshData *emd)
|
|
{
|
|
if (!(emd->vertexCos && (emd->vertexNos == nullptr))) {
|
|
return;
|
|
}
|
|
|
|
BMesh *bm = em->bm;
|
|
const float(*vertexCos)[3], (*polyNos)[3];
|
|
float(*vertexNos)[3];
|
|
|
|
/* Calculate vertex normals from poly normals. */
|
|
BKE_editmesh_cache_ensure_poly_normals(em, emd);
|
|
|
|
BM_mesh_elem_index_ensure(bm, BM_FACE);
|
|
|
|
polyNos = emd->polyNos;
|
|
vertexCos = emd->vertexCos;
|
|
vertexNos = static_cast<float(*)[3]>(MEM_callocN(sizeof(*vertexNos) * bm->totvert, __func__));
|
|
|
|
BM_verts_calc_normal_vcos(bm, polyNos, vertexCos, vertexNos);
|
|
|
|
emd->vertexNos = (const float(*)[3])vertexNos;
|
|
}
|
|
|
|
void BKE_editmesh_cache_ensure_poly_centers(BMEditMesh *em, EditMeshData *emd)
|
|
{
|
|
if (emd->polyCos != nullptr) {
|
|
return;
|
|
}
|
|
BMesh *bm = em->bm;
|
|
|
|
BMFace *efa;
|
|
BMIter fiter;
|
|
int i;
|
|
|
|
float(*polyCos)[3] = static_cast<float(*)[3]>(
|
|
MEM_mallocN(sizeof(*polyCos) * bm->totface, __func__));
|
|
|
|
if (emd->vertexCos) {
|
|
const float(*vertexCos)[3];
|
|
vertexCos = emd->vertexCos;
|
|
|
|
BM_mesh_elem_index_ensure(bm, BM_VERT);
|
|
|
|
BM_ITER_MESH_INDEX (efa, &fiter, bm, BM_FACES_OF_MESH, i) {
|
|
BM_face_calc_center_median_vcos(bm, efa, polyCos[i], vertexCos);
|
|
}
|
|
}
|
|
else {
|
|
BM_ITER_MESH_INDEX (efa, &fiter, bm, BM_FACES_OF_MESH, i) {
|
|
BM_face_calc_center_median(efa, polyCos[i]);
|
|
}
|
|
}
|
|
|
|
emd->polyCos = (const float(*)[3])polyCos;
|
|
}
|
|
|
|
/** \} */
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
/** \name Calculate Min/Max
|
|
* \{ */
|
|
|
|
bool BKE_editmesh_cache_calc_minmax(struct BMEditMesh *em,
|
|
struct EditMeshData *emd,
|
|
float min[3],
|
|
float max[3])
|
|
{
|
|
using namespace blender;
|
|
BMesh *bm = em->bm;
|
|
|
|
if (bm->totvert) {
|
|
if (emd->vertexCos) {
|
|
Span<float3> vert_coords(reinterpret_cast<const float3 *>(emd->vertexCos), bm->totvert);
|
|
std::optional<Bounds<float3>> bounds = bounds::min_max(vert_coords);
|
|
BLI_assert(bounds.has_value());
|
|
copy_v3_v3(min, math::min(bounds->min, float3(min)));
|
|
copy_v3_v3(max, math::max(bounds->max, float3(max)));
|
|
}
|
|
else {
|
|
BMVert *eve;
|
|
BMIter iter;
|
|
BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
|
|
minmax_v3v3_v3(min, max, eve->co);
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
zero_v3(min);
|
|
zero_v3(max);
|
|
return false;
|
|
}
|
|
|
|
/** \} */
|