Files
test2/source/blender/blenkernel/intern/bvhutils.cc

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

866 lines
28 KiB
C++
Raw Normal View History

/* SPDX-FileCopyrightText: 2024 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
2011-02-27 20:40:57 +00:00
/** \file
* \ingroup bke
2011-02-27 20:40:57 +00:00
*/
#include "DNA_meshdata_types.h"
#include "DNA_pointcloud_types.h"
#include "BLI_math_geom.h"
#include "BKE_attribute.hh"
#include "BKE_bvhutils.hh"
#include "BKE_editmesh.hh"
#include "BKE_mesh.hh"
#include "BKE_pointcloud.hh"
namespace blender::bke {
2015-07-22 20:35:33 +10:00
/* -------------------------------------------------------------------- */
/** \name Local Callbacks
* \{ */
/* Math stuff for ray casting on mesh faces and for nearest surface */
2015-07-22 20:35:33 +10:00
float bvhtree_ray_tri_intersection(const BVHTreeRay *ray,
const float /*m_dist*/,
2015-07-22 20:35:33 +10:00
const float v0[3],
const float v1[3],
const float v2[3])
{
float dist;
#ifdef USE_KDOPBVH_WATERTIGHT
if (isect_ray_tri_watertight_v3(ray->origin, ray->isect_precalc, v0, v1, v2, &dist, nullptr))
#else
if (isect_ray_tri_epsilon_v3(
ray->origin, ray->direction, v0, v1, v2, &dist, nullptr, FLT_EPSILON))
#endif
{
return dist;
}
return FLT_MAX;
}
float bvhtree_sphereray_tri_intersection(const BVHTreeRay *ray,
2015-07-22 20:35:33 +10:00
float radius,
const float m_dist,
const float v0[3],
const float v1[3],
const float v2[3])
{
2018-06-17 17:05:51 +02:00
float idist;
float p1[3];
float hit_point[3];
2011-11-06 15:17:43 +00:00
madd_v3_v3v3fl(p1, ray->origin, ray->direction, m_dist);
2012-03-07 04:53:43 +00:00
if (isect_sweeping_sphere_tri_v3(ray->origin, p1, radius, v0, v1, v2, &idist, hit_point)) {
return idist * m_dist;
}
return FLT_MAX;
}
/*
* BVH from meshes callbacks
*/
2021-10-12 17:52:35 +11:00
/**
* Callback to BVH-tree nearest point.
* The tree must have been built using #bvhtree_from_mesh_faces.
*
* \param userdata: Must be a #BVHMeshCallbackUserdata built from the same mesh as the tree.
*/
2011-11-06 15:17:43 +00:00
static void mesh_faces_nearest_point(void *userdata,
int index,
const float co[3],
BVHTreeNearest *nearest)
{
2012-05-06 17:22:54 +00:00
const BVHTreeFromMesh *data = (BVHTreeFromMesh *)userdata;
2015-07-22 17:39:33 +10:00
const MFace *face = data->face + index;
const float *t0, *t1, *t2, *t3;
t0 = data->vert_positions[face->v1];
t1 = data->vert_positions[face->v2];
t2 = data->vert_positions[face->v3];
t3 = face->v4 ? &data->vert_positions[face->v4].x : nullptr;
2012-05-06 17:22:54 +00:00
do {
float nearest_tmp[3], dist_sq;
closest_on_tri_to_point_v3(nearest_tmp, co, t0, t1, t2);
dist_sq = len_squared_v3v3(co, nearest_tmp);
if (dist_sq < nearest->dist_sq) {
nearest->index = index;
nearest->dist_sq = dist_sq;
2011-11-06 15:17:43 +00:00
copy_v3_v3(nearest->co, nearest_tmp);
2012-04-29 15:47:02 +00:00
normal_tri_v3(nearest->no, t0, t1, t2);
}
t1 = t2;
t2 = t3;
t3 = nullptr;
} while (t2);
}
/* copy of function above */
static void mesh_corner_tris_nearest_point(void *userdata,
int index,
const float co[3],
BVHTreeNearest *nearest)
{
const BVHTreeFromMesh *data = (BVHTreeFromMesh *)userdata;
const int3 &tri = data->corner_tris[index];
const float *vtri_co[3] = {
data->vert_positions[data->corner_verts[tri[0]]],
data->vert_positions[data->corner_verts[tri[1]]],
data->vert_positions[data->corner_verts[tri[2]]],
};
float nearest_tmp[3], dist_sq;
closest_on_tri_to_point_v3(nearest_tmp, co, UNPACK3(vtri_co));
dist_sq = len_squared_v3v3(co, nearest_tmp);
if (dist_sq < nearest->dist_sq) {
nearest->index = index;
nearest->dist_sq = dist_sq;
copy_v3_v3(nearest->co, nearest_tmp);
normal_tri_v3(nearest->no, UNPACK3(vtri_co));
}
}
2021-10-12 17:52:35 +11:00
/**
* Callback to BVH-tree ray-cast.
* The tree must have been built using bvhtree_from_mesh_faces.
*
* \param userdata: Must be a #BVHMeshCallbackUserdata built from the same mesh as the tree.
*/
static void mesh_faces_spherecast(void *userdata,
int index,
const BVHTreeRay *ray,
BVHTreeRayHit *hit)
{
2012-05-06 17:22:54 +00:00
const BVHTreeFromMesh *data = (BVHTreeFromMesh *)userdata;
2015-07-22 17:39:33 +10:00
const MFace *face = &data->face[index];
const float *t0, *t1, *t2, *t3;
t0 = data->vert_positions[face->v1];
t1 = data->vert_positions[face->v2];
t2 = data->vert_positions[face->v3];
t3 = face->v4 ? &data->vert_positions[face->v4].x : nullptr;
2012-05-06 17:22:54 +00:00
do {
float dist;
if (ray->radius == 0.0f) {
dist = bvhtree_ray_tri_intersection(ray, hit->dist, t0, t1, t2);
}
else {
dist = bvhtree_sphereray_tri_intersection(ray, ray->radius, hit->dist, t0, t1, t2);
}
2012-03-07 04:53:43 +00:00
if (dist >= 0 && dist < hit->dist) {
hit->index = index;
hit->dist = dist;
2011-11-06 15:17:43 +00:00
madd_v3_v3v3fl(hit->co, ray->origin, ray->direction, dist);
2012-04-29 15:47:02 +00:00
normal_tri_v3(hit->no, t0, t1, t2);
}
t1 = t2;
t2 = t3;
t3 = nullptr;
} while (t2);
}
/* copy of function above */
static void mesh_corner_tris_spherecast(void *userdata,
int index,
const BVHTreeRay *ray,
BVHTreeRayHit *hit)
{
const BVHTreeFromMesh *data = (BVHTreeFromMesh *)userdata;
const Span<float3> positions = data->vert_positions;
const int3 &tri = data->corner_tris[index];
const float *vtri_co[3] = {
positions[data->corner_verts[tri[0]]],
positions[data->corner_verts[tri[1]]],
positions[data->corner_verts[tri[2]]],
};
float dist;
if (ray->radius == 0.0f) {
dist = bvhtree_ray_tri_intersection(ray, hit->dist, UNPACK3(vtri_co));
}
else {
dist = bvhtree_sphereray_tri_intersection(ray, ray->radius, hit->dist, UNPACK3(vtri_co));
}
if (dist >= 0 && dist < hit->dist) {
hit->index = index;
hit->dist = dist;
madd_v3_v3v3fl(hit->co, ray->origin, ray->direction, dist);
normal_tri_v3(hit->no, UNPACK3(vtri_co));
}
}
2021-10-12 17:52:35 +11:00
/**
* Callback to BVH-tree nearest point.
* The tree must have been built using #bvhtree_from_mesh_edges.
*
* \param userdata: Must be a #BVHMeshCallbackUserdata built from the same mesh as the tree.
*/
2011-11-06 15:17:43 +00:00
static void mesh_edges_nearest_point(void *userdata,
int index,
const float co[3],
BVHTreeNearest *nearest)
{
2012-05-06 17:22:54 +00:00
const BVHTreeFromMesh *data = (BVHTreeFromMesh *)userdata;
const Span<float3> positions = data->vert_positions;
const int2 edge = data->edges[index];
float nearest_tmp[3], dist_sq;
const float *t0, *t1;
Mesh: Move edges to a generic attribute Implements #95966, as the final step of #95965. This commit changes the storage of mesh edge vertex indices from the `MEdge` type to the generic `int2` attribute type. This follows the general design for geometry and the attribute system, where the data storage type and the usage semantics are separated. The main benefit of the change is reduced memory usage-- the requirements of storing mesh edges is reduced by 1/3. For example, this saves 8MB on a 1 million vertex grid. This also gives performance benefits to any memory-bound mesh processing algorithm that uses edges. Another benefit is that all of the edge's vertex indices are contiguous. In a few cases, it's helpful to process all of them as `Span<int>` rather than `Span<int2>`. Similarly, the type is more likely to match a generic format used by a library, or code that shouldn't know about specific Blender `Mesh` types. Various Notes: - The `.edge_verts` name is used to reflect a mapping between domains, similar to `.corner_verts`, etc. The period means that it the data shouldn't change arbitrarily by the user or procedural operations. - `edge[0]` is now used instead of `edge.v1` - Signed integers are used instead of unsigned to reduce the mixing of signed-ness, which can be error prone. - All of the previously used core mesh data types (`MVert`, `MEdge`, `MLoop`, `MPoly` are now deprecated. Only generic types are used). - The `vec2i` DNA type is used in the few C files where necessary. Pull Request: https://projects.blender.org/blender/blender/pulls/106638
2023-04-17 13:47:41 +02:00
t0 = positions[edge[0]];
t1 = positions[edge[1]];
2011-11-06 15:17:43 +00:00
closest_to_line_segment_v3(nearest_tmp, co, t0, t1);
dist_sq = len_squared_v3v3(nearest_tmp, co);
if (dist_sq < nearest->dist_sq) {
nearest->index = index;
nearest->dist_sq = dist_sq;
2011-11-06 15:17:43 +00:00
copy_v3_v3(nearest->co, nearest_tmp);
sub_v3_v3v3(nearest->no, t0, t1);
normalize_v3(nearest->no);
}
}
2021-07-02 12:11:54 +10:00
/* Helper, does all the point-sphere-cast work actually. */
static void mesh_verts_spherecast_do(int index,
const float v[3],
const BVHTreeRay *ray,
BVHTreeRayHit *hit)
{
const float *r1;
float r2[3], i1[3];
r1 = ray->origin;
add_v3_v3v3(r2, r1, ray->direction);
closest_to_line_segment_v3(i1, v, r1, r2);
/* No hit if closest point is 'behind' the origin of the ray, or too far away from it. */
if (dot_v3v3v3(r1, i1, r2) >= 0.0f) {
const float dist = len_v3v3(r1, i1);
if (dist < hit->dist) {
hit->index = index;
hit->dist = dist;
copy_v3_v3(hit->co, i1);
}
}
}
Make bvhutil safe for multi-threaded usage There were couple of reasons why it wasn't safe for usage from multiple threads. First of all, it was trying to cache BVH in derived mesh, which wasn't safe because multiple threads might have requested BVH tree and simultaneous reading and writing to the cache became a big headache. Solved this with RW lock so now access to BVH cache is safe. Another issue is causes by the fact that it's not guaranteed DM to have vert/edge/face arrays pre-allocated and when one was calling functions like getVertDataArray() array could have been allocated and marked as temporary. This is REALLY bad, because NO ONE is ever allowed to modify data which doesn't belong to him. This lead to situations when multiple threads were using BVH tree and they run into race condition with this temporary allocated arrays. Now bvhtree owns allocated arrays and keeps track of them, so no race condition happens with temporary data stored in the derived mesh. This solved threading issues and likely wouldn't introduce noticeable slowdown. Even when DM was keeping track of this arrays, they were re-allocated on every BVH creation anyway, because those arrays were temporary and were freed with dm->release() call. We might re-consider this a bit and make it so BVH trees are allocated when DM itself is being allocated based on the DAG layout, but that i'd consider an optimization and not something we need to do 1st priority. Fixes crash happening with 05_4g_track.blend from Mango after the threaded object update landed to master.
2014-01-10 17:21:39 +06:00
2021-10-12 17:52:35 +11:00
/**
* Callback to BVH-tree ray-cast.
* The tree must have been built using bvhtree_from_mesh_verts.
*
* \param userdata: Must be a #BVHMeshCallbackUserdata built from the same mesh as the tree.
*/
static void mesh_verts_spherecast(void *userdata,
int index,
const BVHTreeRay *ray,
BVHTreeRayHit *hit)
{
const BVHTreeFromMesh *data = (BVHTreeFromMesh *)userdata;
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
const float *v = data->vert_positions[index];
Make bvhutil safe for multi-threaded usage There were couple of reasons why it wasn't safe for usage from multiple threads. First of all, it was trying to cache BVH in derived mesh, which wasn't safe because multiple threads might have requested BVH tree and simultaneous reading and writing to the cache became a big headache. Solved this with RW lock so now access to BVH cache is safe. Another issue is causes by the fact that it's not guaranteed DM to have vert/edge/face arrays pre-allocated and when one was calling functions like getVertDataArray() array could have been allocated and marked as temporary. This is REALLY bad, because NO ONE is ever allowed to modify data which doesn't belong to him. This lead to situations when multiple threads were using BVH tree and they run into race condition with this temporary allocated arrays. Now bvhtree owns allocated arrays and keeps track of them, so no race condition happens with temporary data stored in the derived mesh. This solved threading issues and likely wouldn't introduce noticeable slowdown. Even when DM was keeping track of this arrays, they were re-allocated on every BVH creation anyway, because those arrays were temporary and were freed with dm->release() call. We might re-consider this a bit and make it so BVH trees are allocated when DM itself is being allocated based on the DAG layout, but that i'd consider an optimization and not something we need to do 1st priority. Fixes crash happening with 05_4g_track.blend from Mango after the threaded object update landed to master.
2014-01-10 17:21:39 +06:00
mesh_verts_spherecast_do(index, v, ray, hit);
}
2021-10-12 17:52:35 +11:00
/**
* Callback to BVH-tree ray-cast.
* The tree must have been built using bvhtree_from_mesh_edges.
*
* \param userdata: Must be a #BVHMeshCallbackUserdata built from the same mesh as the tree.
*/
static void mesh_edges_spherecast(void *userdata,
int index,
const BVHTreeRay *ray,
BVHTreeRayHit *hit)
{
const BVHTreeFromMesh *data = (BVHTreeFromMesh *)userdata;
const Span<float3> positions = data->vert_positions;
const int2 edge = data->edges[index];
const float radius_sq = square_f(ray->radius);
const float *v1, *v2, *r1;
float r2[3], i1[3], i2[3];
Mesh: Move edges to a generic attribute Implements #95966, as the final step of #95965. This commit changes the storage of mesh edge vertex indices from the `MEdge` type to the generic `int2` attribute type. This follows the general design for geometry and the attribute system, where the data storage type and the usage semantics are separated. The main benefit of the change is reduced memory usage-- the requirements of storing mesh edges is reduced by 1/3. For example, this saves 8MB on a 1 million vertex grid. This also gives performance benefits to any memory-bound mesh processing algorithm that uses edges. Another benefit is that all of the edge's vertex indices are contiguous. In a few cases, it's helpful to process all of them as `Span<int>` rather than `Span<int2>`. Similarly, the type is more likely to match a generic format used by a library, or code that shouldn't know about specific Blender `Mesh` types. Various Notes: - The `.edge_verts` name is used to reflect a mapping between domains, similar to `.corner_verts`, etc. The period means that it the data shouldn't change arbitrarily by the user or procedural operations. - `edge[0]` is now used instead of `edge.v1` - Signed integers are used instead of unsigned to reduce the mixing of signed-ness, which can be error prone. - All of the previously used core mesh data types (`MVert`, `MEdge`, `MLoop`, `MPoly` are now deprecated. Only generic types are used). - The `vec2i` DNA type is used in the few C files where necessary. Pull Request: https://projects.blender.org/blender/blender/pulls/106638
2023-04-17 13:47:41 +02:00
v1 = positions[edge[0]];
v2 = positions[edge[1]];
/* In case we get a zero-length edge, handle it as a point! */
if (equals_v3v3(v1, v2)) {
mesh_verts_spherecast_do(index, v1, ray, hit);
return;
}
r1 = ray->origin;
add_v3_v3v3(r2, r1, ray->direction);
if (isect_line_line_v3(v1, v2, r1, r2, i1, i2)) {
/* No hit if intersection point is 'behind' the origin of the ray, or too far away from it. */
if (dot_v3v3v3(r1, i2, r2) >= 0.0f) {
const float dist = len_v3v3(r1, i2);
if (dist < hit->dist) {
const float e_fac = line_point_factor_v3(i1, v1, v2);
if (e_fac < 0.0f) {
copy_v3_v3(i1, v1);
}
else if (e_fac > 1.0f) {
copy_v3_v3(i1, v2);
}
/* Ensure ray is really close enough from edge! */
if (len_squared_v3v3(i1, i2) <= radius_sq) {
hit->index = index;
hit->dist = dist;
copy_v3_v3(hit->co, i2);
}
}
}
}
}
2015-07-22 20:35:33 +10:00
/** \} */
/*
* BVH builders
*/
/* -------------------------------------------------------------------- */
/** \name Common Utils
* \{ */
static BVHTreeFromMesh create_verts_tree_data(const BVHTree *tree, const Span<float3> positions)
{
BVHTreeFromMesh data{};
data.tree = tree;
data.vert_positions = positions;
/* a nullptr nearest callback works fine
* remember the min distance to point is the same as the min distance to BV of point */
data.nearest_callback = nullptr;
data.raycast_callback = mesh_verts_spherecast;
return data;
}
static BVHTreeFromMesh create_verts_tree_data(std::unique_ptr<BVHTree, BVHTreeDeleter> tree,
const Span<float3> positions)
{
BVHTreeFromMesh data = create_verts_tree_data(tree.get(), positions);
data.owned_tree = std::move(tree);
return data;
}
static BVHTreeFromMesh create_edges_tree_data(const BVHTree *tree,
const Span<float3> positions,
const Span<int2> edges)
{
BVHTreeFromMesh data{};
data.tree = tree;
data.vert_positions = positions;
data.edges = edges;
data.nearest_callback = mesh_edges_nearest_point;
data.raycast_callback = mesh_edges_spherecast;
return data;
}
static BVHTreeFromMesh create_edges_tree_data(std::unique_ptr<BVHTree, BVHTreeDeleter> tree,
const Span<float3> positions,
const Span<int2> edges)
{
BVHTreeFromMesh data = create_edges_tree_data(tree.get(), positions, edges);
data.owned_tree = std::move(tree);
return data;
}
static BVHTreeFromMesh create_legacy_faces_tree_data(const BVHTree *tree,
const Span<float3> positions,
const MFace *face)
{
BVHTreeFromMesh data{};
data.tree = tree;
data.vert_positions = positions;
data.face = face;
data.nearest_callback = mesh_faces_nearest_point;
data.raycast_callback = mesh_faces_spherecast;
return data;
}
static BVHTreeFromMesh create_tris_tree_data(const BVHTree *tree,
const Span<float3> positions,
const Span<int> corner_verts,
const Span<int3> corner_tris)
{
BVHTreeFromMesh data{};
data.tree = tree;
data.vert_positions = positions;
data.corner_verts = corner_verts;
data.corner_tris = corner_tris;
data.nearest_callback = mesh_corner_tris_nearest_point;
data.raycast_callback = mesh_corner_tris_spherecast;
return data;
}
static BVHTreeFromMesh create_tris_tree_data(std::unique_ptr<BVHTree, BVHTreeDeleter> tree,
const Span<float3> positions,
const Span<int> corner_verts,
const Span<int3> corner_tris)
{
BVHTreeFromMesh data = create_tris_tree_data(tree.get(), positions, corner_verts, corner_tris);
data.owned_tree = std::move(tree);
return data;
}
static std::unique_ptr<BVHTree, BVHTreeDeleter> bvhtree_new_common(int elems_num)
{
if (elems_num == 0) {
return nullptr;
}
return std::unique_ptr<BVHTree, BVHTreeDeleter>(BLI_bvhtree_new(elems_num, 0.0f, 2, 6));
}
static std::unique_ptr<BVHTree, BVHTreeDeleter> create_tree_from_verts(
const Span<float3> positions, const IndexMask &verts_mask)
{
std::unique_ptr<BVHTree, BVHTreeDeleter> tree = bvhtree_new_common(verts_mask.size());
if (!tree) {
return nullptr;
}
verts_mask.foreach_index(
[&](const int i) { BLI_bvhtree_insert(tree.get(), i, positions[i], 1); });
BLI_bvhtree_balance(tree.get());
return tree;
}
2024-12-17 19:57:36 -05:00
BVHTreeFromMesh bvhtree_from_mesh_verts_ex(const Span<float3> vert_positions,
const IndexMask &verts_mask)
{
return create_verts_tree_data(create_tree_from_verts(vert_positions, verts_mask),
vert_positions);
}
static std::unique_ptr<BVHTree, BVHTreeDeleter> create_tree_from_edges(
const Span<float3> positions, const Span<int2> edges, const IndexMask &edges_mask)
{
std::unique_ptr<BVHTree, BVHTreeDeleter> tree = bvhtree_new_common(edges_mask.size());
if (!tree) {
return nullptr;
}
edges_mask.foreach_index([&](const int edge_i) {
const int2 &edge = edges[edge_i];
float co[2][3];
copy_v3_v3(co[0], positions[edge[0]]);
copy_v3_v3(co[1], positions[edge[1]]);
BLI_bvhtree_insert(tree.get(), edge_i, co[0], 2);
});
BLI_bvhtree_balance(tree.get());
return tree;
}
2024-12-17 19:57:36 -05:00
BVHTreeFromMesh bvhtree_from_mesh_edges_ex(const Span<float3> vert_positions,
const Span<int2> edges,
const IndexMask &edges_mask)
{
return create_edges_tree_data(
create_tree_from_edges(vert_positions, edges, edges_mask), vert_positions, edges);
}
static std::unique_ptr<BVHTree, BVHTreeDeleter> create_tree_from_legacy_faces(
const Span<float3> positions, const Span<MFace> faces)
{
std::unique_ptr<BVHTree, BVHTreeDeleter> tree = bvhtree_new_common(faces.size());
if (!tree) {
return nullptr;
}
for (const int i : faces.index_range()) {
float co[4][3];
copy_v3_v3(co[0], positions[faces[i].v1]);
copy_v3_v3(co[1], positions[faces[i].v2]);
copy_v3_v3(co[2], positions[faces[i].v3]);
if (faces[i].v4) {
copy_v3_v3(co[3], positions[faces[i].v4]);
}
BLI_bvhtree_insert(tree.get(), i, co[0], faces[i].v4 ? 4 : 3);
}
BLI_bvhtree_balance(tree.get());
return tree;
}
static std::unique_ptr<BVHTree, BVHTreeDeleter> create_tree_from_tris(const Span<float3> positions,
const Span<int> corner_verts,
const Span<int3> corner_tris)
{
std::unique_ptr<BVHTree, BVHTreeDeleter> tree = bvhtree_new_common(corner_tris.size());
if (!tree) {
return {};
}
for (const int tri : corner_tris.index_range()) {
float co[3][3];
copy_v3_v3(co[0], positions[corner_verts[corner_tris[tri][0]]]);
copy_v3_v3(co[1], positions[corner_verts[corner_tris[tri][1]]]);
copy_v3_v3(co[2], positions[corner_verts[corner_tris[tri][2]]]);
BLI_bvhtree_insert(tree.get(), tri, co[0], 3);
}
BLI_bvhtree_balance(tree.get());
return tree;
}
static std::unique_ptr<BVHTree, BVHTreeDeleter> create_tree_from_tris(
const Span<float3> positions,
const OffsetIndices<int> faces,
const Span<int> corner_verts,
const Span<int3> corner_tris,
const IndexMask &faces_mask)
{
if (faces_mask.size() == faces.size()) {
/* Avoid accessing face offsets if the selection is full. */
return create_tree_from_tris(positions, corner_verts, corner_tris);
}
int tris_num = 0;
faces_mask.foreach_index_optimized<int>(
[&](const int i) { tris_num += mesh::face_triangles_num(faces[i].size()); });
std::unique_ptr<BVHTree, BVHTreeDeleter> tree = bvhtree_new_common(tris_num);
if (!tree) {
return {};
}
faces_mask.foreach_index([&](const int face) {
for (const int tri : mesh::face_triangles_range(faces, face)) {
float co[3][3];
copy_v3_v3(co[0], positions[corner_verts[corner_tris[tri][0]]]);
copy_v3_v3(co[1], positions[corner_verts[corner_tris[tri][1]]]);
copy_v3_v3(co[2], positions[corner_verts[corner_tris[tri][2]]]);
BLI_bvhtree_insert(tree.get(), tri, co[0], 3);
}
});
BLI_bvhtree_balance(tree.get());
return tree;
}
2024-12-17 19:57:36 -05:00
BVHTreeFromMesh bvhtree_from_mesh_corner_tris_ex(const Span<float3> vert_positions,
const OffsetIndices<int> faces,
2024-12-17 19:57:36 -05:00
const Span<int> corner_verts,
const Span<int3> corner_tris,
const IndexMask &faces_mask)
{
return create_tris_tree_data(
create_tree_from_tris(vert_positions, faces, corner_verts, corner_tris, faces_mask),
vert_positions,
corner_verts,
corner_tris);
}
static BitVector<> loose_verts_no_hidden_mask_get(const Mesh &mesh)
{
int count = mesh.verts_num;
BitVector<> verts_mask(count, true);
const AttributeAccessor attributes = mesh.attributes();
const Span<int2> edges = mesh.edges();
const VArray<bool> hide_edge = *attributes.lookup_or_default(
".hide_edge", AttrDomain::Edge, false);
const VArray<bool> hide_vert = *attributes.lookup_or_default(
".hide_vert", AttrDomain::Point, false);
for (const int i : edges.index_range()) {
if (hide_edge[i]) {
continue;
}
for (const int vert : {edges[i][0], edges[i][1]}) {
if (verts_mask[vert]) {
verts_mask[vert].reset();
count--;
}
}
}
if (count) {
for (const int vert : verts_mask.index_range()) {
if (verts_mask[vert] && hide_vert[vert]) {
verts_mask[vert].reset();
}
}
}
return verts_mask;
}
static BitVector<> loose_edges_no_hidden_mask_get(const Mesh &mesh)
{
int count = mesh.edges_num;
BitVector<> edge_mask(count, true);
const AttributeAccessor attributes = mesh.attributes();
const OffsetIndices faces = mesh.faces();
const Span<int> corner_edges = mesh.corner_edges();
const VArray<bool> hide_poly = *attributes.lookup_or_default(
".hide_poly", AttrDomain::Face, false);
const VArray<bool> hide_edge = *attributes.lookup_or_default(
".hide_edge", AttrDomain::Edge, false);
for (const int i : faces.index_range()) {
if (hide_poly[i]) {
continue;
}
for (const int edge : corner_edges.slice(faces[i])) {
if (edge_mask[edge]) {
edge_mask[edge].reset();
count--;
}
}
}
if (count) {
for (const int edge : edge_mask.index_range()) {
if (edge_mask[edge] && hide_edge[edge]) {
edge_mask[edge].reset();
}
}
}
return edge_mask;
}
} // namespace blender::bke
blender::bke::BVHTreeFromMesh Mesh::bvh_loose_verts() const
{
using namespace blender;
using namespace blender::bke;
const Span<float3> positions = this->vert_positions();
this->runtime->bvh_cache_loose_verts.ensure([&](std::unique_ptr<BVHTree, BVHTreeDeleter> &data) {
const LooseVertCache &loose_verts = this->loose_verts();
IndexMaskMemory memory;
const IndexMask mask = IndexMask::from_bits(loose_verts.is_loose_bits, memory);
data = create_tree_from_verts(positions, mask);
});
return create_verts_tree_data(this->runtime->bvh_cache_loose_verts.data().get(), positions);
}
blender::bke::BVHTreeFromMesh Mesh::bvh_loose_no_hidden_verts() const
{
using namespace blender;
using namespace blender::bke;
const Span<float3> positions = this->vert_positions();
this->runtime->bvh_cache_loose_verts_no_hidden.ensure(
[&](std::unique_ptr<BVHTree, BVHTreeDeleter> &data) {
const BitVector<> mask = loose_verts_no_hidden_mask_get(*this);
IndexMaskMemory memory;
data = create_tree_from_verts(positions, IndexMask::from_bits(mask, memory));
});
return create_verts_tree_data(this->runtime->bvh_cache_loose_verts_no_hidden.data().get(),
positions);
}
blender::bke::BVHTreeFromMesh Mesh::bvh_verts() const
{
using namespace blender;
using namespace blender::bke;
const Span<float3> positions = this->vert_positions();
this->runtime->bvh_cache_verts.ensure([&](std::unique_ptr<BVHTree, BVHTreeDeleter> &data) {
data = create_tree_from_verts(positions, positions.index_range());
});
return create_verts_tree_data(this->runtime->bvh_cache_verts.data().get(), positions);
}
blender::bke::BVHTreeFromMesh Mesh::bvh_loose_edges() const
{
using namespace blender;
using namespace blender::bke;
const Span<float3> positions = this->vert_positions();
const Span<int2> edges = this->edges();
this->runtime->bvh_cache_loose_edges.ensure([&](std::unique_ptr<BVHTree, BVHTreeDeleter> &data) {
const LooseEdgeCache &loose_edges = this->loose_edges();
IndexMaskMemory memory;
const IndexMask mask = IndexMask::from_bits(loose_edges.is_loose_bits, memory);
data = create_tree_from_edges(positions, edges, mask);
});
return create_edges_tree_data(
this->runtime->bvh_cache_loose_edges.data().get(), positions, edges);
}
blender::bke::BVHTreeFromMesh Mesh::bvh_loose_no_hidden_edges() const
{
using namespace blender;
using namespace blender::bke;
const Span<float3> positions = this->vert_positions();
const Span<int2> edges = this->edges();
this->runtime->bvh_cache_loose_edges_no_hidden.ensure(
[&](std::unique_ptr<BVHTree, BVHTreeDeleter> &data) {
const BitVector<> mask = loose_edges_no_hidden_mask_get(*this);
IndexMaskMemory memory;
data = create_tree_from_edges(positions, edges, IndexMask::from_bits(mask, memory));
});
return create_edges_tree_data(
this->runtime->bvh_cache_loose_edges_no_hidden.data().get(), positions, edges);
}
blender::bke::BVHTreeFromMesh Mesh::bvh_edges() const
{
using namespace blender;
using namespace blender::bke;
const Span<float3> positions = this->vert_positions();
const Span<int2> edges = this->edges();
this->runtime->bvh_cache_edges.ensure([&](std::unique_ptr<BVHTree, BVHTreeDeleter> &data) {
data = create_tree_from_edges(positions, edges, edges.index_range());
});
return create_edges_tree_data(this->runtime->bvh_cache_edges.data().get(), positions, edges);
}
blender::bke::BVHTreeFromMesh Mesh::bvh_legacy_faces() const
{
using namespace blender;
using namespace blender::bke;
BLI_assert(!(this->totface_legacy == 0 && this->faces_num != 0));
Span legacy_faces{
static_cast<const MFace *>(CustomData_get_layer(&this->fdata_legacy, CD_MFACE)),
this->totface_legacy};
const Span<float3> positions = this->vert_positions();
this->runtime->bvh_cache_faces.ensure([&](std::unique_ptr<BVHTree, BVHTreeDeleter> &data) {
data = create_tree_from_legacy_faces(positions, legacy_faces);
});
return create_legacy_faces_tree_data(
this->runtime->bvh_cache_faces.data().get(), positions, legacy_faces.data());
}
blender::bke::BVHTreeFromMesh Mesh::bvh_corner_tris_no_hidden() const
{
using namespace blender;
using namespace blender::bke;
const Span<float3> positions = this->vert_positions();
const Span<int> corner_verts = this->corner_verts();
const Span<int3> corner_tris = this->corner_tris();
const AttributeAccessor attributes = this->attributes();
const VArray hide_poly = *attributes.lookup<bool>(".hide_poly", AttrDomain::Face);
if (!hide_poly) {
return this->bvh_corner_tris();
}
this->runtime->bvh_cache_corner_tris_no_hidden.ensure(
[&](std::unique_ptr<BVHTree, BVHTreeDeleter> &data) {
const OffsetIndices<int> faces = this->faces();
IndexMaskMemory memory;
const IndexMask visible_faces = IndexMask::from_bools_inverse(
faces.index_range(), VArraySpan(hide_poly), memory);
data = create_tree_from_tris(positions, faces, corner_verts, corner_tris, visible_faces);
});
return create_tris_tree_data(this->runtime->bvh_cache_corner_tris_no_hidden.data().get(),
positions,
corner_verts,
corner_tris);
}
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
blender::bke::BVHTreeFromMesh Mesh::bvh_corner_tris() const
{
using namespace blender;
using namespace blender::bke;
const Span<float3> positions = this->vert_positions();
const Span<int> corner_verts = this->corner_verts();
const Span<int3> corner_tris = this->corner_tris();
this->runtime->bvh_cache_corner_tris.ensure([&](std::unique_ptr<BVHTree, BVHTreeDeleter> &data) {
data = create_tree_from_tris(positions, corner_verts, corner_tris);
});
return create_tris_tree_data(
this->runtime->bvh_cache_corner_tris.data().get(), positions, corner_verts, corner_tris);
}
namespace blender::bke {
2024-12-17 19:57:36 -05:00
BVHTreeFromMesh bvhtree_from_mesh_tris_init(const Mesh &mesh, const IndexMask &faces_mask)
{
if (faces_mask.size() == mesh.faces_num) {
2024-12-17 19:57:36 -05:00
return mesh.bvh_corner_tris();
}
return bvhtree_from_mesh_corner_tris_ex(
mesh.vert_positions(), mesh.faces(), mesh.corner_verts(), mesh.corner_tris(), faces_mask);
}
2024-12-17 19:57:36 -05:00
BVHTreeFromMesh bvhtree_from_mesh_edges_init(const Mesh &mesh, const IndexMask &edges_mask)
{
if (edges_mask.size() == mesh.edges_num) {
2024-12-17 19:57:36 -05:00
return mesh.bvh_edges();
}
return bvhtree_from_mesh_edges_ex(mesh.vert_positions(), mesh.edges(), edges_mask);
}
2024-12-17 19:57:36 -05:00
BVHTreeFromMesh bvhtree_from_mesh_verts_init(const Mesh &mesh, const IndexMask &verts_mask)
{
if (verts_mask.size() == mesh.verts_num) {
2024-12-17 19:57:36 -05:00
return mesh.bvh_verts();
}
return bvhtree_from_mesh_verts_ex(mesh.vert_positions(), verts_mask);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Point Cloud BVH Building
* \{ */
static BVHTreeFromPointCloud create_points_tree_data(const BVHTree *tree,
const Span<float3> positions)
{
2024-12-17 19:57:36 -05:00
BVHTreeFromPointCloud data{};
data.tree = tree;
data.positions = positions;
2024-12-17 19:57:36 -05:00
data.nearest_callback = nullptr;
return data;
}
static BVHTreeFromPointCloud create_pointcloud_tree_data(const BVHTree *tree,
const Span<float3> positions)
{
BVHTreeFromPointCloud data{};
data.tree = tree;
data.positions = positions;
return data;
}
static BVHTreeFromPointCloud create_pointcloud_tree_data(
std::unique_ptr<BVHTree, BVHTreeDeleter> tree, const Span<float3> positions)
{
BVHTreeFromPointCloud data = create_points_tree_data(tree.get(), positions);
data.owned_tree = std::move(tree);
return data;
}
BVHTreeFromPointCloud bvhtree_from_pointcloud_get(const PointCloud &pointcloud,
const IndexMask &points_mask)
{
if (points_mask.size() == pointcloud.totpoint) {
return pointcloud.bvh_tree();
}
const Span<float3> positions = pointcloud.positions();
return create_pointcloud_tree_data(create_tree_from_verts(positions, points_mask), positions);
}
} // namespace blender::bke
blender::bke::BVHTreeFromPointCloud PointCloud::bvh_tree() const
{
using namespace blender;
using namespace blender::bke;
const Span<float3> positions = this->positions();
this->runtime->bvh_cache.ensure([&](std::unique_ptr<BVHTree, BVHTreeDeleter> &data) {
data = create_tree_from_verts(positions, positions.index_range());
});
return create_pointcloud_tree_data(this->runtime->bvh_cache.data().get(), positions);
}
/** \} */