2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2011-02-27 20:40:57 +00:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup bke
|
2011-02-27 20:40:57 +00:00
|
|
|
*/
|
|
|
|
|
|
2021-07-01 16:46:55 -05:00
|
|
|
#include <cmath>
|
|
|
|
|
#include <cstdio>
|
|
|
|
|
#include <cstring>
|
2008-08-11 13:29:38 +00:00
|
|
|
|
2018-05-08 10:07:21 +02:00
|
|
|
#include "DNA_mesh_types.h"
|
2008-08-11 13:29:38 +00:00
|
|
|
#include "DNA_meshdata_types.h"
|
2021-02-05 08:28:31 -06:00
|
|
|
#include "DNA_pointcloud_types.h"
|
2008-08-11 13:29:38 +00:00
|
|
|
|
2022-11-13 23:23:59 -06:00
|
|
|
#include "BLI_bit_vector.hh"
|
2011-10-22 01:53:35 +00:00
|
|
|
#include "BLI_linklist.h"
|
Cleanup: reduce amount of math-related includes
Using ClangBuildAnalyzer on the whole Blender build, it was pointing
out that BLI_math.h is the heaviest "header hub" (i.e. non tiny file
that is included a lot).
However, there's very little (actually zero) source files in Blender
that need "all the math" (base, colors, vectors, matrices,
quaternions, intersection, interpolation, statistics, solvers and
time). A common use case is source files needing just vectors, or
just vectors & matrices, or just colors etc. Actually, 181 files
were including the whole math thing without needing it at all.
This change removes BLI_math.h completely, and instead in all the
places that need it, includes BLI_math_vector.h or BLI_math_color.h
and so on.
Change from that:
- BLI_math_color.h was included 1399 times -> now 408 (took 114.0sec
to parse -> now 36.3sec)
- BLI_simd.h 1403 -> 418 (109.7sec -> 34.9sec).
Full rebuild of Blender (Apple M1, Xcode, RelWithDebInfo) is not
affected much (342sec -> 334sec). Most of benefit would be when
someone's changing BLI_simd.h or BLI_math_color.h or similar files,
that now there's 3x fewer files result in a recompile.
Pull Request #110944
2023-08-09 11:39:20 +03:00
|
|
|
#include "BLI_math_geom.h"
|
|
|
|
|
#include "BLI_math_vector.h"
|
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
|
|
|
#include "BLI_span.hh"
|
2021-06-14 23:50:24 +10:00
|
|
|
#include "BLI_task.h"
|
2014-01-10 17:21:39 +06:00
|
|
|
#include "BLI_threads.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BLI_utildefines.h"
|
2011-01-07 18:36:47 +00:00
|
|
|
|
2022-07-19 18:06:56 -05:00
|
|
|
#include "BKE_attribute.hh"
|
2023-11-16 11:41:55 +01:00
|
|
|
#include "BKE_bvhutils.hh"
|
|
|
|
|
#include "BKE_editmesh.hh"
|
2023-03-12 22:29:15 +01:00
|
|
|
#include "BKE_mesh.hh"
|
2023-08-02 22:14:18 +02:00
|
|
|
#include "BKE_mesh_runtime.hh"
|
2024-01-11 10:54:47 +01:00
|
|
|
#include "BKE_pointcloud.hh"
|
2008-08-11 13:29:38 +00:00
|
|
|
|
2009-05-23 03:24:15 +00:00
|
|
|
#include "MEM_guardedalloc.h"
|
2008-08-11 13:29:38 +00:00
|
|
|
|
2023-02-20 11:51:16 +01:00
|
|
|
using blender::BitSpan;
|
2022-11-13 23:23:59 -06:00
|
|
|
using blender::BitVector;
|
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
|
|
|
using blender::float3;
|
2022-11-14 11:59:31 -06:00
|
|
|
using blender::IndexRange;
|
2023-12-19 14:57:49 +01:00
|
|
|
using blender::int3;
|
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
|
|
|
using blender::Span;
|
Mesh: Move hide flags to generic attributes
This commit moves the hide status of mesh vertices, edges, and faces
from the `ME_FLAG` to optional generic boolean attributes. Storing this
data as generic attributes can significantly simplify and improve code,
as described in T95965.
The attributes are called `.hide_vert`, `.hide_edge`, and `.hide_poly`,
using the attribute name semantics discussed in T97452. The `.` prefix
means they are "UI attributes", so they still contain original data
edited by users, but they aren't meant to be accessed procedurally by
the user in arbitrary situations. They are also be hidden in the
spreadsheet and the attribute list by default,
Until 4.0, the attributes are still written to and read from the mesh
in the old way, so neither forward nor backward compatibility are
affected. This means memory requirements will be increased by one byte
per element when the hide status is used. When the flags are removed
completely, requirements will decrease when hiding is unused.
Further notes:
* Some code can be further simplified to skip some processing when the
hide attributes don't exist.
* The data is still stored in flags for `BMesh`, necessitating some
complexity in the conversion to and from `Mesh`.
* Access to the "hide" property of mesh elements in RNA is slower.
The separate boolean arrays should be used where possible.
Ref T95965
Differential Revision: https://developer.blender.org/D14685
2022-08-11 12:54:24 -04:00
|
|
|
using blender::VArray;
|
|
|
|
|
|
2020-06-02 15:59:30 +02:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name BVHCache
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2021-07-01 16:46:55 -05:00
|
|
|
struct BVHCacheItem {
|
2020-06-02 15:59:30 +02:00
|
|
|
bool is_filled;
|
|
|
|
|
BVHTree *tree;
|
2021-07-01 16:46:55 -05:00
|
|
|
};
|
2020-06-02 15:59:30 +02:00
|
|
|
|
2021-07-01 16:46:55 -05:00
|
|
|
struct BVHCache {
|
2020-06-02 15:59:30 +02:00
|
|
|
BVHCacheItem items[BVHTREE_MAX_ITEM];
|
|
|
|
|
ThreadMutex mutex;
|
2021-07-01 16:46:55 -05:00
|
|
|
};
|
2020-06-02 15:59:30 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Queries a bvhcache for the cache bvhtree of the request type
|
|
|
|
|
*
|
|
|
|
|
* When the `r_locked` is filled and the tree could not be found the caches mutex will be
|
|
|
|
|
* locked. This mutex can be unlocked by calling `bvhcache_unlock`.
|
|
|
|
|
*
|
2022-10-12 22:31:02 -05:00
|
|
|
* When `r_locked` is used the `mesh_eval_mutex` must contain the `MeshRuntime.eval_mutex`.
|
2020-06-02 15:59:30 +02:00
|
|
|
*/
|
|
|
|
|
static bool bvhcache_find(BVHCache **bvh_cache_p,
|
|
|
|
|
BVHCacheType type,
|
|
|
|
|
BVHTree **r_tree,
|
|
|
|
|
bool *r_locked,
|
2022-10-12 22:31:02 -05:00
|
|
|
std::mutex *mesh_eval_mutex)
|
2020-06-02 15:59:30 +02:00
|
|
|
{
|
|
|
|
|
bool do_lock = r_locked;
|
|
|
|
|
if (r_locked) {
|
|
|
|
|
*r_locked = false;
|
|
|
|
|
}
|
2021-07-01 16:46:55 -05:00
|
|
|
if (*bvh_cache_p == nullptr) {
|
2020-06-02 15:59:30 +02:00
|
|
|
if (!do_lock) {
|
|
|
|
|
/* Cache does not exist and no lock is requested. */
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
/* Lazy initialization of the bvh_cache using the `mesh_eval_mutex`. */
|
2022-10-12 22:31:02 -05:00
|
|
|
std::lock_guard lock{*mesh_eval_mutex};
|
2021-07-01 16:46:55 -05:00
|
|
|
if (*bvh_cache_p == nullptr) {
|
2020-06-02 15:59:30 +02:00
|
|
|
*bvh_cache_p = bvhcache_init();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
BVHCache *bvh_cache = *bvh_cache_p;
|
2014-01-10 17:21:39 +06:00
|
|
|
|
2020-06-02 15:59:30 +02:00
|
|
|
if (bvh_cache->items[type].is_filled) {
|
|
|
|
|
*r_tree = bvh_cache->items[type].tree;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (do_lock) {
|
|
|
|
|
BLI_mutex_lock(&bvh_cache->mutex);
|
2021-07-01 16:46:55 -05:00
|
|
|
bool in_cache = bvhcache_find(bvh_cache_p, type, r_tree, nullptr, nullptr);
|
2020-06-02 15:59:30 +02:00
|
|
|
if (in_cache) {
|
|
|
|
|
BLI_mutex_unlock(&bvh_cache->mutex);
|
|
|
|
|
return in_cache;
|
|
|
|
|
}
|
|
|
|
|
*r_locked = true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void bvhcache_unlock(BVHCache *bvh_cache, bool lock_started)
|
|
|
|
|
{
|
|
|
|
|
if (lock_started) {
|
|
|
|
|
BLI_mutex_unlock(&bvh_cache->mutex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool bvhcache_has_tree(const BVHCache *bvh_cache, const BVHTree *tree)
|
|
|
|
|
{
|
2021-07-01 16:46:55 -05:00
|
|
|
if (bvh_cache == nullptr) {
|
2020-06-02 15:59:30 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-01 16:46:55 -05:00
|
|
|
for (int i = 0; i < BVHTREE_MAX_ITEM; i++) {
|
2020-06-02 15:59:30 +02:00
|
|
|
if (bvh_cache->items[i].tree == tree) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-08 00:31:20 -05:00
|
|
|
BVHCache *bvhcache_init()
|
2020-06-02 15:59:30 +02:00
|
|
|
{
|
2021-12-24 22:17:49 -05:00
|
|
|
BVHCache *cache = MEM_cnew<BVHCache>(__func__);
|
2020-06-02 15:59:30 +02:00
|
|
|
BLI_mutex_init(&cache->mutex);
|
|
|
|
|
return cache;
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Inserts a BVHTree of the given type under the cache
|
|
|
|
|
* After that the caller no longer needs to worry when to free the BVHTree
|
|
|
|
|
* as that will be done when the cache is freed.
|
|
|
|
|
*
|
|
|
|
|
* A call to this assumes that there was no previous cached tree of the given type
|
2021-07-01 16:46:55 -05:00
|
|
|
* \warning The #BVHTree can be nullptr.
|
2020-06-02 15:59:30 +02:00
|
|
|
*/
|
|
|
|
|
static void bvhcache_insert(BVHCache *bvh_cache, BVHTree *tree, BVHCacheType type)
|
|
|
|
|
{
|
|
|
|
|
BVHCacheItem *item = &bvh_cache->items[type];
|
|
|
|
|
BLI_assert(!item->is_filled);
|
|
|
|
|
item->tree = tree;
|
|
|
|
|
item->is_filled = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void bvhcache_free(BVHCache *bvh_cache)
|
|
|
|
|
{
|
2021-07-01 16:46:55 -05:00
|
|
|
for (int index = 0; index < BVHTREE_MAX_ITEM; index++) {
|
2020-06-02 15:59:30 +02:00
|
|
|
BVHCacheItem *item = &bvh_cache->items[index];
|
|
|
|
|
BLI_bvhtree_free(item->tree);
|
2021-07-01 16:46:55 -05:00
|
|
|
item->tree = nullptr;
|
2020-06-02 15:59:30 +02:00
|
|
|
}
|
|
|
|
|
BLI_mutex_end(&bvh_cache->mutex);
|
|
|
|
|
MEM_freeN(bvh_cache);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-12 17:52:35 +11:00
|
|
|
/**
|
|
|
|
|
* BVH-tree balancing inside a mutex lock must be run in isolation. Balancing
|
2021-06-14 23:50:24 +10:00
|
|
|
* is multithreaded, and we do not want the current thread to start another task
|
2021-10-12 17:52:35 +11:00
|
|
|
* that may involve acquiring the same mutex lock that it is waiting for.
|
|
|
|
|
*/
|
2021-06-14 23:50:24 +10:00
|
|
|
static void bvhtree_balance_isolated(void *userdata)
|
|
|
|
|
{
|
|
|
|
|
BLI_bvhtree_balance((BVHTree *)userdata);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void bvhtree_balance(BVHTree *tree, const bool isolate)
|
|
|
|
|
{
|
|
|
|
|
if (tree) {
|
|
|
|
|
if (isolate) {
|
|
|
|
|
BLI_task_isolate(bvhtree_balance_isolated, tree);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
BLI_bvhtree_balance(tree);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-02 15:59:30 +02:00
|
|
|
/** \} */
|
2021-12-14 15:49:31 +11:00
|
|
|
|
2015-07-22 20:35:33 +10:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Local Callbacks
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2008-08-11 13:29:38 +00:00
|
|
|
/* 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,
|
2022-10-03 17:37:25 -05:00
|
|
|
const float /*m_dist*/,
|
2015-07-22 20:35:33 +10:00
|
|
|
const float v0[3],
|
|
|
|
|
const float v1[3],
|
|
|
|
|
const float v2[3])
|
2008-08-11 13:29:38 +00:00
|
|
|
{
|
|
|
|
|
float dist;
|
|
|
|
|
|
2015-08-21 17:46:23 +10:00
|
|
|
#ifdef USE_KDOPBVH_WATERTIGHT
|
2021-07-01 16:46:55 -05:00
|
|
|
if (isect_ray_tri_watertight_v3(ray->origin, ray->isect_precalc, v0, v1, v2, &dist, nullptr))
|
2015-08-21 17:46:23 +10:00
|
|
|
#else
|
2021-07-01 16:46:55 -05:00
|
|
|
if (isect_ray_tri_epsilon_v3(
|
|
|
|
|
ray->origin, ray->direction, v0, v1, v2, &dist, nullptr, FLT_EPSILON))
|
2015-08-21 17:46:23 +10:00
|
|
|
#endif
|
|
|
|
|
{
|
2008-08-11 13:29:38 +00:00
|
|
|
return dist;
|
2015-08-21 17:46:23 +10:00
|
|
|
}
|
2008-08-11 13:29:38 +00:00
|
|
|
|
|
|
|
|
return FLT_MAX;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-29 21:14:16 +10:00
|
|
|
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])
|
2008-08-11 13:29:38 +00:00
|
|
|
{
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2008-08-11 13:29:38 +00:00
|
|
|
float idist;
|
|
|
|
|
float p1[3];
|
2014-12-27 16:36:31 +11:00
|
|
|
float hit_point[3];
|
2008-08-11 13:29:38 +00:00
|
|
|
|
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)) {
|
2008-08-11 13:29:38 +00:00
|
|
|
return idist * m_dist;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return FLT_MAX;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2013-10-05 14:19:39 +00:00
|
|
|
* BVH from meshes callbacks
|
2008-08-11 13:29:38 +00:00
|
|
|
*/
|
|
|
|
|
|
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)
|
2008-08-11 13:29:38 +00:00
|
|
|
{
|
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;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-04-27 00:20:13 +10:00
|
|
|
const float *t0, *t1, *t2, *t3;
|
2023-11-28 16:40:43 -05:00
|
|
|
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;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-05-06 17:22:54 +00:00
|
|
|
do {
|
2014-02-03 02:46:45 +11:00
|
|
|
float nearest_tmp[3], dist_sq;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-08-19 17:41:55 +10:00
|
|
|
closest_on_tri_to_point_v3(nearest_tmp, co, t0, t1, t2);
|
|
|
|
|
dist_sq = len_squared_v3v3(co, nearest_tmp);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-02-03 02:46:45 +11:00
|
|
|
if (dist_sq < nearest->dist_sq) {
|
2008-08-13 19:22:35 +00:00
|
|
|
nearest->index = index;
|
2014-02-03 02:46:45 +11:00
|
|
|
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);
|
2008-08-11 13:29:38 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2008-08-11 13:29:38 +00:00
|
|
|
t1 = t2;
|
|
|
|
|
t2 = t3;
|
2021-07-01 16:46:55 -05:00
|
|
|
t3 = nullptr;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
} while (t2);
|
2008-08-11 13:29:38 +00:00
|
|
|
}
|
2015-07-23 13:09:14 +10:00
|
|
|
/* copy of function above */
|
2023-12-19 14:57:49 +01:00
|
|
|
static void mesh_corner_tris_nearest_point(void *userdata,
|
|
|
|
|
int index,
|
|
|
|
|
const float co[3],
|
|
|
|
|
BVHTreeNearest *nearest)
|
2015-07-23 13:09:14 +10:00
|
|
|
{
|
|
|
|
|
const BVHTreeFromMesh *data = (BVHTreeFromMesh *)userdata;
|
2023-12-19 14:57:49 +01:00
|
|
|
const int3 &tri = data->corner_tris[index];
|
2015-07-23 13:09:14 +10:00
|
|
|
const float *vtri_co[3] = {
|
2023-12-19 14:57:49 +01:00
|
|
|
data->vert_positions[data->corner_verts[tri[0]]],
|
|
|
|
|
data->vert_positions[data->corner_verts[tri[1]]],
|
|
|
|
|
data->vert_positions[data->corner_verts[tri[2]]],
|
2015-07-23 13:09:14 +10:00
|
|
|
};
|
|
|
|
|
float nearest_tmp[3], dist_sq;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-07-23 13:09:14 +10:00
|
|
|
closest_on_tri_to_point_v3(nearest_tmp, co, UNPACK3(vtri_co));
|
|
|
|
|
dist_sq = len_squared_v3v3(co, nearest_tmp);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-07-23 13:09:14 +10:00
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-07-31 11:50:54 +10:00
|
|
|
/* Copy of function above (warning, should de-duplicate with `editmesh_bvh.cc`). */
|
2023-12-19 14:57:49 +01:00
|
|
|
static void editmesh_corner_tris_nearest_point(void *userdata,
|
|
|
|
|
int index,
|
|
|
|
|
const float co[3],
|
|
|
|
|
BVHTreeNearest *nearest)
|
2013-04-23 23:57:27 +00:00
|
|
|
{
|
2023-06-30 12:36:48 -03:00
|
|
|
BMEditMesh *em = static_cast<BMEditMesh *>(userdata);
|
2023-12-20 09:56:15 +11:00
|
|
|
const BMLoop **ltri = const_cast<const BMLoop **>(em->looptris[index]);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-04-27 00:20:13 +10:00
|
|
|
const float *t0, *t1, *t2;
|
2013-04-23 23:57:27 +00:00
|
|
|
t0 = ltri[0]->v->co;
|
|
|
|
|
t1 = ltri[1]->v->co;
|
|
|
|
|
t2 = ltri[2]->v->co;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-04-23 23:57:27 +00:00
|
|
|
{
|
2014-02-03 02:46:45 +11:00
|
|
|
float nearest_tmp[3], dist_sq;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-08-19 17:41:55 +10:00
|
|
|
closest_on_tri_to_point_v3(nearest_tmp, co, t0, t1, t2);
|
|
|
|
|
dist_sq = len_squared_v3v3(co, nearest_tmp);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-02-03 02:46:45 +11:00
|
|
|
if (dist_sq < nearest->dist_sq) {
|
2013-04-23 23:57:27 +00:00
|
|
|
nearest->index = index;
|
2014-02-03 02:46:45 +11:00
|
|
|
nearest->dist_sq = dist_sq;
|
2013-04-23 23:57:27 +00:00
|
|
|
copy_v3_v3(nearest->co, nearest_tmp);
|
|
|
|
|
normal_tri_v3(nearest->no, t0, t1, t2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-08-11 13:29:38 +00: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_faces.
|
|
|
|
|
*
|
|
|
|
|
* \param userdata: Must be a #BVHMeshCallbackUserdata built from the same mesh as the tree.
|
|
|
|
|
*/
|
2008-08-11 13:29:38 +00:00
|
|
|
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];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-04-27 00:20:13 +10:00
|
|
|
const float *t0, *t1, *t2, *t3;
|
2023-11-28 16:40:43 -05:00
|
|
|
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;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-05-06 17:22:54 +00:00
|
|
|
do {
|
2008-08-11 13:29:38 +00:00
|
|
|
float dist;
|
2019-04-22 09:39:35 +10:00
|
|
|
if (ray->radius == 0.0f) {
|
2011-10-28 14:46:09 +00:00
|
|
|
dist = bvhtree_ray_tri_intersection(ray, hit->dist, t0, t1, t2);
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
|
|
|
|
else {
|
2018-05-04 11:57:01 -03:00
|
|
|
dist = bvhtree_sphereray_tri_intersection(ray, ray->radius, hit->dist, t0, t1, t2);
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-03-07 04:53:43 +00:00
|
|
|
if (dist >= 0 && dist < hit->dist) {
|
2008-08-11 13:29:38 +00:00
|
|
|
hit->index = index;
|
|
|
|
|
hit->dist = dist;
|
2011-11-06 15:17:43 +00:00
|
|
|
madd_v3_v3v3fl(hit->co, ray->origin, ray->direction, dist);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-04-29 15:47:02 +00:00
|
|
|
normal_tri_v3(hit->no, t0, t1, t2);
|
2008-08-11 13:29:38 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2008-08-11 13:29:38 +00:00
|
|
|
t1 = t2;
|
|
|
|
|
t2 = t3;
|
2021-07-01 16:46:55 -05:00
|
|
|
t3 = nullptr;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
} while (t2);
|
2008-08-11 13:29:38 +00:00
|
|
|
}
|
2015-07-23 13:09:14 +10:00
|
|
|
/* copy of function above */
|
2023-12-19 14:57:49 +01:00
|
|
|
static void mesh_corner_tris_spherecast(void *userdata,
|
|
|
|
|
int index,
|
|
|
|
|
const BVHTreeRay *ray,
|
|
|
|
|
BVHTreeRayHit *hit)
|
2015-07-23 13:09:14 +10:00
|
|
|
{
|
|
|
|
|
const BVHTreeFromMesh *data = (BVHTreeFromMesh *)userdata;
|
2023-11-28 16:40:43 -05:00
|
|
|
const Span<float3> positions = data->vert_positions;
|
2023-12-19 14:57:49 +01:00
|
|
|
const int3 &tri = data->corner_tris[index];
|
2015-07-23 13:09:14 +10:00
|
|
|
const float *vtri_co[3] = {
|
2023-12-19 14:57:49 +01:00
|
|
|
positions[data->corner_verts[tri[0]]],
|
|
|
|
|
positions[data->corner_verts[tri[1]]],
|
|
|
|
|
positions[data->corner_verts[tri[2]]],
|
2015-07-23 13:09:14 +10:00
|
|
|
};
|
|
|
|
|
float dist;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-04-22 09:39:35 +10:00
|
|
|
if (ray->radius == 0.0f) {
|
2015-07-23 13:09:14 +10:00
|
|
|
dist = bvhtree_ray_tri_intersection(ray, hit->dist, UNPACK3(vtri_co));
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
|
|
|
|
else {
|
2018-05-04 11:57:01 -03:00
|
|
|
dist = bvhtree_sphereray_tri_intersection(ray, ray->radius, hit->dist, UNPACK3(vtri_co));
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-07-23 13:09:14 +10:00
|
|
|
if (dist >= 0 && dist < hit->dist) {
|
|
|
|
|
hit->index = index;
|
|
|
|
|
hit->dist = dist;
|
|
|
|
|
madd_v3_v3v3fl(hit->co, ray->origin, ray->direction, dist);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-07-23 13:09:14 +10:00
|
|
|
normal_tri_v3(hit->no, UNPACK3(vtri_co));
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-07-31 11:50:54 +10:00
|
|
|
/* Copy of function above (warning, should de-duplicate with `editmesh_bvh.cc`). */
|
2023-12-19 14:57:49 +01:00
|
|
|
static void editmesh_corner_tris_spherecast(void *userdata,
|
|
|
|
|
int index,
|
|
|
|
|
const BVHTreeRay *ray,
|
|
|
|
|
BVHTreeRayHit *hit)
|
2013-04-23 23:57:27 +00:00
|
|
|
{
|
2023-06-30 12:36:48 -03:00
|
|
|
BMEditMesh *em = static_cast<BMEditMesh *>(userdata);
|
2023-12-20 09:56:15 +11:00
|
|
|
const BMLoop **ltri = const_cast<const BMLoop **>(em->looptris[index]);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-04-27 00:20:13 +10:00
|
|
|
const float *t0, *t1, *t2;
|
2013-04-23 23:57:27 +00:00
|
|
|
t0 = ltri[0]->v->co;
|
|
|
|
|
t1 = ltri[1]->v->co;
|
|
|
|
|
t2 = ltri[2]->v->co;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-04-23 23:57:27 +00:00
|
|
|
{
|
|
|
|
|
float dist;
|
2019-04-22 09:39:35 +10:00
|
|
|
if (ray->radius == 0.0f) {
|
2013-04-23 23:57:27 +00:00
|
|
|
dist = bvhtree_ray_tri_intersection(ray, hit->dist, t0, t1, t2);
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
|
|
|
|
else {
|
2018-05-04 11:57:01 -03:00
|
|
|
dist = bvhtree_sphereray_tri_intersection(ray, ray->radius, hit->dist, t0, t1, t2);
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-04-23 23:57:27 +00:00
|
|
|
if (dist >= 0 && dist < hit->dist) {
|
|
|
|
|
hit->index = index;
|
|
|
|
|
hit->dist = dist;
|
|
|
|
|
madd_v3_v3v3fl(hit->co, ray->origin, ray->direction, dist);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-04-23 23:57:27 +00:00
|
|
|
normal_tri_v3(hit->no, t0, t1, t2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-08-11 13:29:38 +00:00
|
|
|
|
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)
|
2009-10-22 23:22:05 +00:00
|
|
|
{
|
2012-05-06 17:22:54 +00:00
|
|
|
const BVHTreeFromMesh *data = (BVHTreeFromMesh *)userdata;
|
2023-11-28 16:40:43 -05:00
|
|
|
const Span<float3> positions = data->vert_positions;
|
|
|
|
|
const blender::int2 edge = data->edges[index];
|
2014-02-03 02:46:45 +11:00
|
|
|
float nearest_tmp[3], dist_sq;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-04-27 00:20:13 +10:00
|
|
|
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]];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-11-06 15:17:43 +00:00
|
|
|
closest_to_line_segment_v3(nearest_tmp, co, t0, t1);
|
2014-02-03 02:46:45 +11:00
|
|
|
dist_sq = len_squared_v3v3(nearest_tmp, co);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-02-03 02:46:45 +11:00
|
|
|
if (dist_sq < nearest->dist_sq) {
|
2009-10-22 23:22:05 +00:00
|
|
|
nearest->index = index;
|
2014-02-03 02:46:45 +11:00
|
|
|
nearest->dist_sq = dist_sq;
|
2011-11-06 15:17:43 +00:00
|
|
|
copy_v3_v3(nearest->co, nearest_tmp);
|
2009-11-10 20:43:45 +00:00
|
|
|
sub_v3_v3v3(nearest->no, t0, t1);
|
|
|
|
|
normalize_v3(nearest->no);
|
2009-10-22 23:22:05 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-02 12:11:54 +10:00
|
|
|
/* Helper, does all the point-sphere-cast work actually. */
|
2015-01-09 12:25:18 +01:00
|
|
|
static void mesh_verts_spherecast_do(int index,
|
2016-05-06 04:49:21 +10:00
|
|
|
const float v[3],
|
|
|
|
|
const BVHTreeRay *ray,
|
|
|
|
|
BVHTreeRayHit *hit)
|
2008-08-11 13:29:38 +00:00
|
|
|
{
|
2015-01-09 12:25:18 +01:00
|
|
|
float dist;
|
|
|
|
|
const float *r1;
|
|
|
|
|
float r2[3], i1[3];
|
|
|
|
|
r1 = ray->origin;
|
|
|
|
|
add_v3_v3v3(r2, r1, ray->direction);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-01-09 12:25:18 +01:00
|
|
|
closest_to_line_segment_v3(i1, v, r1, r2);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-01-09 12:25:18 +01:00
|
|
|
/* 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) && ((dist = len_v3v3(r1, i1)) < hit->dist)) {
|
|
|
|
|
hit->index = index;
|
|
|
|
|
hit->dist = dist;
|
|
|
|
|
copy_v3_v3(hit->co, i1);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-01-10 17:21:39 +06:00
|
|
|
|
2016-05-06 04:49:21 +10:00
|
|
|
static void editmesh_verts_spherecast(void *userdata,
|
|
|
|
|
int index,
|
|
|
|
|
const BVHTreeRay *ray,
|
|
|
|
|
BVHTreeRayHit *hit)
|
|
|
|
|
{
|
2023-06-30 12:36:48 -03:00
|
|
|
BMEditMesh *em = static_cast<BMEditMesh *>(userdata);
|
|
|
|
|
BMVert *eve = BM_vert_at_index(em->bm, index);
|
2016-05-06 04:49:21 +10:00
|
|
|
|
|
|
|
|
mesh_verts_spherecast_do(index, eve->co, 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_verts.
|
|
|
|
|
*
|
|
|
|
|
* \param userdata: Must be a #BVHMeshCallbackUserdata built from the same mesh as the tree.
|
|
|
|
|
*/
|
2015-01-09 12:25:18 +01:00
|
|
|
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];
|
2014-01-10 17:21:39 +06:00
|
|
|
|
2016-05-06 04:49:21 +10:00
|
|
|
mesh_verts_spherecast_do(index, v, ray, hit);
|
2015-01-09 12:25:18 +01:00
|
|
|
}
|
2008-08-11 13:29:38 +00: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_edges.
|
|
|
|
|
*
|
|
|
|
|
* \param userdata: Must be a #BVHMeshCallbackUserdata built from the same mesh as the tree.
|
|
|
|
|
*/
|
2015-01-09 12:25:18 +01:00
|
|
|
static void mesh_edges_spherecast(void *userdata,
|
|
|
|
|
int index,
|
|
|
|
|
const BVHTreeRay *ray,
|
|
|
|
|
BVHTreeRayHit *hit)
|
|
|
|
|
{
|
|
|
|
|
const BVHTreeFromMesh *data = (BVHTreeFromMesh *)userdata;
|
2023-11-28 16:40:43 -05:00
|
|
|
const Span<float3> positions = data->vert_positions;
|
|
|
|
|
const blender::int2 edge = data->edges[index];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-03-06 17:18:10 +01:00
|
|
|
const float radius_sq = square_f(ray->radius);
|
2015-01-09 12:25:18 +01:00
|
|
|
float dist;
|
|
|
|
|
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]];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-01-09 12:25:18 +01:00
|
|
|
/* In case we get a zero-length edge, handle it as a point! */
|
|
|
|
|
if (equals_v3v3(v1, v2)) {
|
2016-05-06 04:49:21 +10:00
|
|
|
mesh_verts_spherecast_do(index, v1, ray, hit);
|
2015-01-09 12:25:18 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-01-09 12:25:18 +01:00
|
|
|
r1 = ray->origin;
|
|
|
|
|
add_v3_v3v3(r2, r1, ray->direction);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-01-09 12:25:18 +01:00
|
|
|
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) && ((dist = len_v3v3(r1, i2)) < 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-05-23 03:24:15 +00:00
|
|
|
|
2015-07-22 20:35:33 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
2015-01-09 12:25:18 +01:00
|
|
|
/*
|
|
|
|
|
* BVH builders
|
|
|
|
|
*/
|
|
|
|
|
|
2022-04-04 17:41:36 -03:00
|
|
|
/* -------------------------------------------------------------------- */
|
2022-04-06 19:08:10 +10:00
|
|
|
/** \name Common Utils
|
2022-04-04 17:41:36 -03:00
|
|
|
* \{ */
|
|
|
|
|
|
|
|
|
|
static void bvhtree_from_mesh_setup_data(BVHTree *tree,
|
|
|
|
|
const BVHCacheType bvh_cache_type,
|
2023-11-28 16:40:43 -05:00
|
|
|
const Span<float3> positions,
|
|
|
|
|
const Span<blender::int2> edges,
|
|
|
|
|
const Span<int> corner_verts,
|
2023-12-19 14:57:49 +01:00
|
|
|
const Span<int3> corner_tris,
|
2023-11-28 16:40:43 -05:00
|
|
|
const MFace *face,
|
2022-04-04 17:41:36 -03:00
|
|
|
BVHTreeFromMesh *r_data)
|
|
|
|
|
{
|
2023-11-28 16:40:43 -05:00
|
|
|
*r_data = {};
|
2022-04-04 17:41:36 -03:00
|
|
|
|
|
|
|
|
r_data->tree = tree;
|
|
|
|
|
|
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
|
|
|
r_data->vert_positions = positions;
|
2023-11-28 16:40:43 -05:00
|
|
|
r_data->edges = edges;
|
2022-04-04 17:41:36 -03:00
|
|
|
r_data->face = face;
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
r_data->corner_verts = corner_verts;
|
2023-12-19 14:57:49 +01:00
|
|
|
r_data->corner_tris = corner_tris;
|
2022-04-04 17:41:36 -03:00
|
|
|
|
|
|
|
|
switch (bvh_cache_type) {
|
|
|
|
|
case BVHTREE_FROM_VERTS:
|
|
|
|
|
case BVHTREE_FROM_LOOSEVERTS:
|
|
|
|
|
/* a nullptr nearest callback works fine
|
|
|
|
|
* remember the min distance to point is the same as the min distance to BV of point */
|
|
|
|
|
r_data->nearest_callback = nullptr;
|
|
|
|
|
r_data->raycast_callback = mesh_verts_spherecast;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case BVHTREE_FROM_EDGES:
|
|
|
|
|
case BVHTREE_FROM_LOOSEEDGES:
|
|
|
|
|
r_data->nearest_callback = mesh_edges_nearest_point;
|
|
|
|
|
r_data->raycast_callback = mesh_edges_spherecast;
|
|
|
|
|
break;
|
|
|
|
|
case BVHTREE_FROM_FACES:
|
|
|
|
|
r_data->nearest_callback = mesh_faces_nearest_point;
|
|
|
|
|
r_data->raycast_callback = mesh_faces_spherecast;
|
|
|
|
|
break;
|
2023-12-19 14:57:49 +01:00
|
|
|
case BVHTREE_FROM_CORNER_TRIS:
|
|
|
|
|
case BVHTREE_FROM_CORNER_TRIS_NO_HIDDEN:
|
|
|
|
|
r_data->nearest_callback = mesh_corner_tris_nearest_point;
|
|
|
|
|
r_data->raycast_callback = mesh_corner_tris_spherecast;
|
2022-04-04 17:41:36 -03:00
|
|
|
break;
|
2023-06-23 15:36:57 -03:00
|
|
|
case BVHTREE_FROM_EM_LOOSEVERTS:
|
2022-04-04 17:41:36 -03:00
|
|
|
case BVHTREE_FROM_EM_EDGES:
|
2023-12-14 12:08:21 +11:00
|
|
|
case BVHTREE_FROM_EM_LOOPTRIS:
|
2022-04-04 17:41:36 -03:00
|
|
|
case BVHTREE_MAX_ITEM:
|
|
|
|
|
BLI_assert(false);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void bvhtree_from_editmesh_setup_data(BVHTree *tree,
|
|
|
|
|
const BVHCacheType bvh_cache_type,
|
|
|
|
|
BVHTreeFromEditMesh *r_data)
|
|
|
|
|
{
|
|
|
|
|
memset(r_data, 0, sizeof(*r_data));
|
|
|
|
|
|
|
|
|
|
r_data->tree = tree;
|
|
|
|
|
|
|
|
|
|
switch (bvh_cache_type) {
|
2023-06-23 15:36:57 -03:00
|
|
|
case BVHTREE_FROM_EM_LOOSEVERTS:
|
2022-04-04 17:41:36 -03:00
|
|
|
r_data->nearest_callback = nullptr;
|
|
|
|
|
r_data->raycast_callback = editmesh_verts_spherecast;
|
|
|
|
|
break;
|
|
|
|
|
case BVHTREE_FROM_EM_EDGES:
|
|
|
|
|
r_data->nearest_callback = nullptr; /* TODO */
|
|
|
|
|
r_data->raycast_callback = nullptr; /* TODO */
|
|
|
|
|
break;
|
2023-12-14 12:08:21 +11:00
|
|
|
case BVHTREE_FROM_EM_LOOPTRIS:
|
2023-12-19 14:57:49 +01:00
|
|
|
r_data->nearest_callback = editmesh_corner_tris_nearest_point;
|
|
|
|
|
r_data->raycast_callback = editmesh_corner_tris_spherecast;
|
2022-04-04 17:41:36 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case BVHTREE_FROM_VERTS:
|
|
|
|
|
case BVHTREE_FROM_LOOSEVERTS:
|
|
|
|
|
case BVHTREE_FROM_EDGES:
|
|
|
|
|
case BVHTREE_FROM_LOOSEEDGES:
|
|
|
|
|
case BVHTREE_FROM_FACES:
|
2023-12-19 14:57:49 +01:00
|
|
|
case BVHTREE_FROM_CORNER_TRIS:
|
|
|
|
|
case BVHTREE_FROM_CORNER_TRIS_NO_HIDDEN:
|
2022-04-04 17:41:36 -03:00
|
|
|
case BVHTREE_MAX_ITEM:
|
|
|
|
|
BLI_assert(false);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 12:20:40 +10:00
|
|
|
static BVHTree *bvhtree_new_common(
|
2023-06-15 16:52:40 -03:00
|
|
|
float epsilon, int tree_type, int axis, int elems_num, int &elems_num_active)
|
|
|
|
|
{
|
|
|
|
|
if (elems_num_active != -1) {
|
|
|
|
|
BLI_assert(IN_RANGE_INCL(elems_num_active, 0, elems_num));
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
elems_num_active = elems_num;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (elems_num_active == 0) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return BLI_bvhtree_new(elems_num_active, epsilon, tree_type, axis);
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-04 17:41:36 -03:00
|
|
|
/** \} */
|
|
|
|
|
|
2015-07-22 20:35:33 +10:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Vertex Builder
|
|
|
|
|
* \{ */
|
2017-02-04 19:06:41 -03:00
|
|
|
|
2016-05-06 04:49:21 +10:00
|
|
|
static BVHTree *bvhtree_from_editmesh_verts_create_tree(float epsilon,
|
2015-07-22 20:35:33 +10:00
|
|
|
int tree_type,
|
|
|
|
|
int axis,
|
2016-05-06 04:49:21 +10:00
|
|
|
BMEditMesh *em,
|
2023-02-20 11:51:16 +01:00
|
|
|
const BitSpan verts_mask,
|
2016-05-06 04:49:21 +10:00
|
|
|
int verts_num_active)
|
2015-01-09 12:25:18 +01:00
|
|
|
{
|
2019-08-23 19:45:09 -03:00
|
|
|
const int verts_num = em->bm->totvert;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-06-16 12:20:40 +10:00
|
|
|
BVHTree *tree = bvhtree_new_common(epsilon, tree_type, axis, verts_num, verts_num_active);
|
2022-11-13 23:32:52 -06:00
|
|
|
if (!tree) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-06-15 16:52:40 -03:00
|
|
|
BM_mesh_elem_table_ensure(em->bm, BM_VERT);
|
|
|
|
|
|
2022-11-13 23:32:52 -06:00
|
|
|
for (int i = 0; i < verts_num; i++) {
|
|
|
|
|
if (!verts_mask.is_empty() && !verts_mask[i]) {
|
|
|
|
|
continue;
|
2016-05-06 04:49:21 +10:00
|
|
|
}
|
2022-11-13 23:32:52 -06:00
|
|
|
BMVert *eve = BM_vert_at_index(em->bm, i);
|
|
|
|
|
BLI_bvhtree_insert(tree, i, eve->co, 1);
|
2016-01-25 18:18:42 +11:00
|
|
|
}
|
2022-11-13 23:32:52 -06:00
|
|
|
BLI_assert(BLI_bvhtree_get_len(tree) == verts_num_active);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-05-06 04:49:21 +10:00
|
|
|
return tree;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static BVHTree *bvhtree_from_mesh_verts_create_tree(float epsilon,
|
|
|
|
|
int tree_type,
|
|
|
|
|
int axis,
|
2023-11-28 16:40:43 -05:00
|
|
|
const Span<float3> positions,
|
2023-02-20 11:51:16 +01:00
|
|
|
const BitSpan verts_mask,
|
2016-05-06 04:49:21 +10:00
|
|
|
int verts_num_active)
|
|
|
|
|
{
|
2023-11-28 16:40:43 -05:00
|
|
|
BVHTree *tree = bvhtree_new_common(epsilon, tree_type, axis, positions.size(), verts_num_active);
|
2022-11-13 23:32:52 -06:00
|
|
|
if (!tree) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-11-28 16:40:43 -05:00
|
|
|
for (const int i : positions.index_range()) {
|
2022-11-13 23:32:52 -06:00
|
|
|
if (!verts_mask.is_empty() && !verts_mask[i]) {
|
|
|
|
|
continue;
|
2015-01-09 12:25:18 +01: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
|
|
|
BLI_bvhtree_insert(tree, i, positions[i], 1);
|
2008-08-11 13:29:38 +00:00
|
|
|
}
|
2022-11-13 23:32:52 -06:00
|
|
|
BLI_assert(BLI_bvhtree_get_len(tree) == verts_num_active);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-01-09 12:25:18 +01:00
|
|
|
return tree;
|
|
|
|
|
}
|
2008-08-11 13:29:38 +00:00
|
|
|
|
2016-05-06 04:49:21 +10:00
|
|
|
BVHTree *bvhtree_from_editmesh_verts_ex(BVHTreeFromEditMesh *data,
|
|
|
|
|
BMEditMesh *em,
|
2023-02-20 11:51:16 +01:00
|
|
|
const BitSpan verts_mask,
|
2016-05-06 04:49:21 +10:00
|
|
|
int verts_num_active,
|
|
|
|
|
float epsilon,
|
|
|
|
|
int tree_type,
|
2022-04-05 20:33:06 -03:00
|
|
|
int axis)
|
2016-05-06 04:49:21 +10:00
|
|
|
{
|
2022-11-13 23:32:52 -06:00
|
|
|
BVHTree *tree = bvhtree_from_editmesh_verts_create_tree(
|
2022-04-05 18:44:52 -03:00
|
|
|
epsilon, tree_type, axis, em, verts_mask, verts_num_active);
|
2017-02-06 14:11:06 -03:00
|
|
|
|
2022-04-05 20:33:06 -03:00
|
|
|
bvhtree_balance(tree, false);
|
2019-08-23 19:45:09 -03:00
|
|
|
|
2022-04-04 17:41:36 -03:00
|
|
|
if (data) {
|
2023-06-30 12:36:48 -03:00
|
|
|
bvhtree_from_editmesh_setup_data(tree, BVHTREE_FROM_EM_LOOSEVERTS, data);
|
2018-07-23 11:04:58 -03:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-08-23 19:45:09 -03:00
|
|
|
return tree;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BVHTree *bvhtree_from_editmesh_verts(
|
|
|
|
|
BVHTreeFromEditMesh *data, BMEditMesh *em, float epsilon, int tree_type, int axis)
|
|
|
|
|
{
|
2022-11-13 23:23:59 -06:00
|
|
|
return bvhtree_from_editmesh_verts_ex(data, em, {}, -1, epsilon, tree_type, axis);
|
2016-05-06 04:49:21 +10:00
|
|
|
}
|
|
|
|
|
|
2015-07-22 20:35:33 +10:00
|
|
|
BVHTree *bvhtree_from_mesh_verts_ex(BVHTreeFromMesh *data,
|
2023-11-28 16:40:43 -05:00
|
|
|
const Span<float3> vert_positions,
|
2023-02-20 11:51:16 +01:00
|
|
|
const BitSpan verts_mask,
|
2016-05-06 04:49:21 +10:00
|
|
|
int verts_num_active,
|
2015-07-22 20:35:33 +10:00
|
|
|
float epsilon,
|
|
|
|
|
int tree_type,
|
2022-04-05 20:33:06 -03:00
|
|
|
int axis)
|
2015-01-09 12:25:18 +01:00
|
|
|
{
|
2022-11-13 23:32:52 -06:00
|
|
|
BVHTree *tree = bvhtree_from_mesh_verts_create_tree(
|
2023-11-28 16:40:43 -05:00
|
|
|
epsilon, tree_type, axis, vert_positions, verts_mask, verts_num_active);
|
2019-08-22 14:07:40 -03:00
|
|
|
|
2022-04-05 20:33:06 -03:00
|
|
|
bvhtree_balance(tree, false);
|
2020-07-29 10:31:37 +02:00
|
|
|
|
2022-04-04 17:41:36 -03:00
|
|
|
if (data) {
|
|
|
|
|
/* Setup BVHTreeFromMesh */
|
2023-11-28 16:40:43 -05:00
|
|
|
bvhtree_from_mesh_setup_data(tree, BVHTREE_FROM_VERTS, vert_positions, {}, {}, {}, {}, data);
|
2022-04-04 17:41:36 -03:00
|
|
|
}
|
2014-01-10 17:21:39 +06:00
|
|
|
|
2017-02-16 22:55:01 -03:00
|
|
|
return tree;
|
2008-08-11 13:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
2015-07-22 20:35:33 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Edge Builder
|
|
|
|
|
* \{ */
|
2015-01-09 12:25:18 +01:00
|
|
|
|
2016-06-30 15:43:47 +10:00
|
|
|
static BVHTree *bvhtree_from_editmesh_edges_create_tree(float epsilon,
|
|
|
|
|
int tree_type,
|
|
|
|
|
int axis,
|
|
|
|
|
BMEditMesh *em,
|
2023-02-20 11:51:16 +01:00
|
|
|
const BitSpan edges_mask,
|
2016-06-30 15:43:47 +10:00
|
|
|
int edges_num_active)
|
|
|
|
|
{
|
2019-08-23 19:45:09 -03:00
|
|
|
const int edges_num = em->bm->totedge;
|
|
|
|
|
|
2023-06-16 12:20:40 +10:00
|
|
|
BVHTree *tree = bvhtree_new_common(epsilon, tree_type, axis, edges_num, edges_num_active);
|
2022-11-13 23:32:52 -06:00
|
|
|
if (!tree) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-06-15 16:52:40 -03:00
|
|
|
BM_mesh_elem_table_ensure(em->bm, BM_EDGE);
|
|
|
|
|
|
2022-11-13 23:32:52 -06:00
|
|
|
int i;
|
|
|
|
|
BMIter iter;
|
|
|
|
|
BMEdge *eed;
|
|
|
|
|
BM_ITER_MESH_INDEX (eed, &iter, em->bm, BM_EDGES_OF_MESH, i) {
|
|
|
|
|
if (!edges_mask.is_empty() && !edges_mask[i]) {
|
|
|
|
|
continue;
|
2016-06-30 15:43:47 +10:00
|
|
|
}
|
2022-11-13 23:32:52 -06:00
|
|
|
float co[2][3];
|
|
|
|
|
copy_v3_v3(co[0], eed->v1->co);
|
|
|
|
|
copy_v3_v3(co[1], eed->v2->co);
|
|
|
|
|
|
|
|
|
|
BLI_bvhtree_insert(tree, i, co[0], 2);
|
2016-06-30 15:43:47 +10:00
|
|
|
}
|
2022-11-13 23:32:52 -06:00
|
|
|
BLI_assert(BLI_bvhtree_get_len(tree) == edges_num_active);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-06-30 15:43:47 +10:00
|
|
|
return tree;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-28 16:40:43 -05:00
|
|
|
static BVHTree *bvhtree_from_mesh_edges_create_tree(const Span<float3> positions,
|
|
|
|
|
const blender::Span<blender::int2> edges,
|
2023-02-20 11:51:16 +01:00
|
|
|
const BitSpan edges_mask,
|
2017-02-06 14:11:06 -03:00
|
|
|
int edges_num_active,
|
|
|
|
|
float epsilon,
|
|
|
|
|
int tree_type,
|
|
|
|
|
int axis)
|
|
|
|
|
{
|
2023-06-16 12:20:40 +10:00
|
|
|
BVHTree *tree = bvhtree_new_common(epsilon, tree_type, axis, edges.size(), edges_num_active);
|
2022-11-13 23:32:52 -06:00
|
|
|
if (!tree) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-21 08:43:07 -04:00
|
|
|
for (const int i : edges.index_range()) {
|
2022-11-13 23:32:52 -06:00
|
|
|
if (!edges_mask.is_empty() && !edges_mask[i]) {
|
|
|
|
|
continue;
|
2017-02-06 14:11:06 -03:00
|
|
|
}
|
2022-11-13 23:32:52 -06:00
|
|
|
float co[2][3];
|
2023-04-21 08:43:07 -04:00
|
|
|
copy_v3_v3(co[0], positions[edges[i][0]]);
|
|
|
|
|
copy_v3_v3(co[1], positions[edges[i][1]]);
|
2022-11-13 23:32:52 -06:00
|
|
|
|
|
|
|
|
BLI_bvhtree_insert(tree, i, co[0], 2);
|
2017-02-06 14:11:06 -03:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2017-02-06 14:11:06 -03:00
|
|
|
return tree;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-30 15:43:47 +10:00
|
|
|
BVHTree *bvhtree_from_editmesh_edges_ex(BVHTreeFromEditMesh *data,
|
|
|
|
|
BMEditMesh *em,
|
2023-02-20 11:51:16 +01:00
|
|
|
const BitSpan edges_mask,
|
2016-06-30 15:43:47 +10:00
|
|
|
int edges_num_active,
|
|
|
|
|
float epsilon,
|
|
|
|
|
int tree_type,
|
2022-04-05 20:33:06 -03:00
|
|
|
int axis)
|
2016-06-30 15:43:47 +10:00
|
|
|
{
|
2022-11-13 23:32:52 -06:00
|
|
|
BVHTree *tree = bvhtree_from_editmesh_edges_create_tree(
|
2022-04-05 18:44:52 -03:00
|
|
|
epsilon, tree_type, axis, em, edges_mask, edges_num_active);
|
2017-02-06 14:11:06 -03:00
|
|
|
|
2022-04-05 20:33:06 -03:00
|
|
|
bvhtree_balance(tree, false);
|
2019-08-23 19:45:09 -03:00
|
|
|
|
2022-04-04 17:41:36 -03:00
|
|
|
if (data) {
|
2023-06-30 12:36:48 -03:00
|
|
|
bvhtree_from_editmesh_setup_data(tree, BVHTREE_FROM_EM_EDGES, data);
|
2018-07-23 11:04:58 -03:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-08-23 19:45:09 -03:00
|
|
|
return tree;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BVHTree *bvhtree_from_editmesh_edges(
|
|
|
|
|
BVHTreeFromEditMesh *data, BMEditMesh *em, float epsilon, int tree_type, int axis)
|
|
|
|
|
{
|
2022-11-13 23:23:59 -06:00
|
|
|
return bvhtree_from_editmesh_edges_ex(data, em, {}, -1, epsilon, tree_type, axis);
|
2016-06-30 15:43:47 +10:00
|
|
|
}
|
|
|
|
|
|
2017-02-06 14:11:06 -03:00
|
|
|
BVHTree *bvhtree_from_mesh_edges_ex(BVHTreeFromMesh *data,
|
2023-11-28 16:40:43 -05:00
|
|
|
const Span<float3> vert_positions,
|
|
|
|
|
const Span<blender::int2> edges,
|
2023-02-20 11:51:16 +01:00
|
|
|
const BitSpan edges_mask,
|
2017-02-06 14:11:06 -03:00
|
|
|
int edges_num_active,
|
|
|
|
|
float epsilon,
|
|
|
|
|
int tree_type,
|
2022-04-05 20:33:06 -03:00
|
|
|
int axis)
|
2017-02-06 14:11:06 -03:00
|
|
|
{
|
2022-11-13 23:32:52 -06:00
|
|
|
BVHTree *tree = bvhtree_from_mesh_edges_create_tree(
|
2023-11-28 16:40:43 -05:00
|
|
|
vert_positions, edges, edges_mask, edges_num_active, epsilon, tree_type, axis);
|
2019-08-22 14:07:40 -03:00
|
|
|
|
2022-04-05 20:33:06 -03:00
|
|
|
bvhtree_balance(tree, false);
|
2020-07-29 10:31:37 +02:00
|
|
|
|
2022-04-04 17:41:36 -03:00
|
|
|
if (data) {
|
|
|
|
|
/* Setup BVHTreeFromMesh */
|
|
|
|
|
bvhtree_from_mesh_setup_data(
|
2023-11-28 16:40:43 -05:00
|
|
|
tree, BVHTREE_FROM_EDGES, vert_positions, edges, {}, {}, {}, data);
|
2022-04-04 17:41:36 -03:00
|
|
|
}
|
2009-10-22 23:22:05 +00:00
|
|
|
|
2017-02-16 22:55:01 -03:00
|
|
|
return tree;
|
2015-01-09 12:25:18 +01:00
|
|
|
}
|
|
|
|
|
|
2015-07-22 20:35:33 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Tessellated Face Builder
|
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
|
|
static BVHTree *bvhtree_from_mesh_faces_create_tree(float epsilon,
|
|
|
|
|
int tree_type,
|
|
|
|
|
int axis,
|
2023-11-28 16:40:43 -05:00
|
|
|
const Span<float3> positions,
|
2017-02-16 22:55:01 -03:00
|
|
|
const MFace *face,
|
|
|
|
|
const int faces_num,
|
2023-02-20 11:51:16 +01:00
|
|
|
const BitSpan faces_mask,
|
2016-05-06 04:49:21 +10:00
|
|
|
int faces_num_active)
|
2015-01-09 12:25:18 +01:00
|
|
|
{
|
2023-06-16 12:20:40 +10:00
|
|
|
BVHTree *tree = bvhtree_new_common(epsilon, tree_type, axis, faces_num, faces_num_active);
|
2022-11-13 23:32:52 -06:00
|
|
|
if (!tree) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-28 16:40:43 -05:00
|
|
|
if (!positions.is_empty() && face) {
|
2022-11-13 23:32:52 -06:00
|
|
|
for (int i = 0; i < faces_num; i++) {
|
|
|
|
|
float co[4][3];
|
|
|
|
|
if (!faces_mask.is_empty() && !faces_mask[i]) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
copy_v3_v3(co[0], positions[face[i].v1]);
|
|
|
|
|
copy_v3_v3(co[1], positions[face[i].v2]);
|
|
|
|
|
copy_v3_v3(co[2], positions[face[i].v3]);
|
2022-11-13 23:32:52 -06:00
|
|
|
if (face[i].v4) {
|
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
|
|
|
copy_v3_v3(co[3], positions[face[i].v4]);
|
2015-01-09 12:25:18 +01:00
|
|
|
}
|
2022-11-13 23:32:52 -06:00
|
|
|
|
|
|
|
|
BLI_bvhtree_insert(tree, i, co[0], face[i].v4 ? 4 : 3);
|
2015-01-09 12:25:18 +01:00
|
|
|
}
|
|
|
|
|
}
|
2022-11-13 23:32:52 -06:00
|
|
|
BLI_assert(BLI_bvhtree_get_len(tree) == faces_num_active);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-01-09 12:25:18 +01:00
|
|
|
return tree;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-22 20:35:33 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
2015-07-22 20:55:46 +10:00
|
|
|
/* -------------------------------------------------------------------- */
|
2023-12-19 14:57:49 +01:00
|
|
|
/** \name corner_tri Face Builder
|
2015-07-22 20:55:46 +10:00
|
|
|
* \{ */
|
|
|
|
|
|
2023-12-19 14:57:49 +01:00
|
|
|
static BVHTree *bvhtree_from_editmesh_corner_tris_create_tree(float epsilon,
|
|
|
|
|
int tree_type,
|
|
|
|
|
int axis,
|
|
|
|
|
BMEditMesh *em,
|
|
|
|
|
const BitSpan corner_tris_mask,
|
|
|
|
|
int corner_tris_num_active)
|
2015-07-22 20:55:46 +10:00
|
|
|
{
|
2023-12-19 14:57:49 +01:00
|
|
|
const int corner_tris_num = em->tottri;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-12-19 14:57:49 +01:00
|
|
|
BVHTree *tree = bvhtree_new_common(
|
|
|
|
|
epsilon, tree_type, axis, corner_tris_num, corner_tris_num_active);
|
2022-11-13 23:32:52 -06:00
|
|
|
if (!tree) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-20 09:56:15 +11:00
|
|
|
const BMLoop *(*corner_tris)[3] = const_cast<const BMLoop *(*)[3]>(em->looptris);
|
2022-11-13 23:32:52 -06:00
|
|
|
|
|
|
|
|
/* Insert BMesh-tessellation triangles into the BVH-tree, unless they are hidden
|
|
|
|
|
* and/or selected. Even if the faces themselves are not selected for the snapped
|
|
|
|
|
* transform, having a vertex selected means the face (and thus it's tessellated
|
|
|
|
|
* triangles) will be moving and will not be a good snap targets. */
|
2023-12-19 14:57:49 +01:00
|
|
|
for (int i = 0; i < corner_tris_num; i++) {
|
|
|
|
|
const BMLoop **ltri = corner_tris[i];
|
|
|
|
|
bool insert = !corner_tris_mask.is_empty() ? corner_tris_mask[i] : true;
|
2022-11-13 23:32:52 -06:00
|
|
|
|
|
|
|
|
if (insert) {
|
|
|
|
|
/* No reason found to block hit-testing the triangle for snap, so insert it now. */
|
|
|
|
|
float co[3][3];
|
|
|
|
|
copy_v3_v3(co[0], ltri[0]->v->co);
|
|
|
|
|
copy_v3_v3(co[1], ltri[1]->v->co);
|
|
|
|
|
copy_v3_v3(co[2], ltri[2]->v->co);
|
|
|
|
|
|
|
|
|
|
BLI_bvhtree_insert(tree, i, co[0], 3);
|
2016-05-06 04:49:21 +10:00
|
|
|
}
|
|
|
|
|
}
|
2023-12-19 14:57:49 +01:00
|
|
|
BLI_assert(BLI_bvhtree_get_len(tree) == corner_tris_num_active);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-05-06 04:49:21 +10:00
|
|
|
return tree;
|
|
|
|
|
}
|
2015-07-22 20:55:46 +10:00
|
|
|
|
2023-12-19 14:57:49 +01:00
|
|
|
static BVHTree *bvhtree_from_mesh_corner_tris_create_tree(float epsilon,
|
|
|
|
|
int tree_type,
|
|
|
|
|
int axis,
|
|
|
|
|
const Span<float3> positions,
|
|
|
|
|
const Span<int> corner_verts,
|
|
|
|
|
const Span<int3> corner_tris,
|
|
|
|
|
const BitSpan corner_tris_mask,
|
|
|
|
|
int corner_tris_num_active)
|
2016-05-06 04:49:21 +10:00
|
|
|
{
|
2023-11-28 16:40:43 -05:00
|
|
|
if (positions.is_empty()) {
|
2022-11-13 23:32:52 -06:00
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 12:20:40 +10:00
|
|
|
BVHTree *tree = bvhtree_new_common(
|
2023-12-19 14:57:49 +01:00
|
|
|
epsilon, tree_type, axis, corner_tris.size(), corner_tris_num_active);
|
2023-06-15 16:52:40 -03:00
|
|
|
|
2022-11-13 23:32:52 -06:00
|
|
|
if (!tree) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-12-19 14:57:49 +01:00
|
|
|
for (const int i : corner_tris.index_range()) {
|
2023-06-15 16:52:40 -03:00
|
|
|
float co[3][3];
|
2023-12-19 14:57:49 +01:00
|
|
|
if (!corner_tris_mask.is_empty() && !corner_tris_mask[i]) {
|
2023-06-15 16:52:40 -03:00
|
|
|
continue;
|
|
|
|
|
}
|
2022-11-13 23:32:52 -06:00
|
|
|
|
2023-12-19 14:57:49 +01:00
|
|
|
copy_v3_v3(co[0], positions[corner_verts[corner_tris[i][0]]]);
|
|
|
|
|
copy_v3_v3(co[1], positions[corner_verts[corner_tris[i][1]]]);
|
|
|
|
|
copy_v3_v3(co[2], positions[corner_verts[corner_tris[i][2]]]);
|
2022-11-13 23:32:52 -06:00
|
|
|
|
2023-06-15 16:52:40 -03:00
|
|
|
BLI_bvhtree_insert(tree, i, co[0], 3);
|
2015-07-22 20:55:46 +10:00
|
|
|
}
|
2023-06-15 16:52:40 -03:00
|
|
|
|
2023-12-19 14:57:49 +01:00
|
|
|
BLI_assert(BLI_bvhtree_get_len(tree) == corner_tris_num_active);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-07-22 20:55:46 +10:00
|
|
|
return tree;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-14 12:08:21 +11:00
|
|
|
BVHTree *bvhtree_from_editmesh_looptris_ex(BVHTreeFromEditMesh *data,
|
|
|
|
|
BMEditMesh *em,
|
2023-12-19 14:57:49 +01:00
|
|
|
const BitSpan corner_tris_mask,
|
|
|
|
|
int corner_tris_num_active,
|
2023-12-14 12:08:21 +11:00
|
|
|
float epsilon,
|
|
|
|
|
int tree_type,
|
|
|
|
|
int axis)
|
2016-05-06 04:49:21 +10:00
|
|
|
{
|
|
|
|
|
/* BMESH specific check that we have tessfaces,
|
2016-06-29 19:31:57 +10:00
|
|
|
* we _could_ tessellate here but rather not - campbell */
|
2023-12-19 14:57:49 +01:00
|
|
|
BVHTree *tree = bvhtree_from_editmesh_corner_tris_create_tree(
|
|
|
|
|
epsilon, tree_type, axis, em, corner_tris_mask, corner_tris_num_active);
|
2022-04-05 18:44:52 -03:00
|
|
|
|
2022-04-05 20:33:06 -03:00
|
|
|
bvhtree_balance(tree, false);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-04-04 17:41:36 -03:00
|
|
|
if (data) {
|
2023-12-14 12:08:21 +11:00
|
|
|
bvhtree_from_editmesh_setup_data(tree, BVHTREE_FROM_EM_LOOPTRIS, data);
|
2016-05-06 04:49:21 +10:00
|
|
|
}
|
|
|
|
|
return tree;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-19 14:57:49 +01:00
|
|
|
BVHTree *bvhtree_from_editmesh_corner_tris(
|
2019-08-23 19:45:09 -03:00
|
|
|
BVHTreeFromEditMesh *data, BMEditMesh *em, float epsilon, int tree_type, int axis)
|
2016-05-06 04:49:21 +10:00
|
|
|
{
|
2023-12-14 12:08:21 +11:00
|
|
|
return bvhtree_from_editmesh_looptris_ex(data, em, {}, -1, epsilon, tree_type, axis);
|
2016-05-06 04:49:21 +10:00
|
|
|
}
|
|
|
|
|
|
2023-12-19 14:57:49 +01:00
|
|
|
BVHTree *bvhtree_from_mesh_corner_tris_ex(BVHTreeFromMesh *data,
|
|
|
|
|
const Span<float3> vert_positions,
|
|
|
|
|
const Span<int> corner_verts,
|
|
|
|
|
const Span<int3> corner_tris,
|
|
|
|
|
const BitSpan corner_tris_mask,
|
|
|
|
|
int corner_tris_num_active,
|
|
|
|
|
float epsilon,
|
|
|
|
|
int tree_type,
|
|
|
|
|
int axis)
|
2015-07-22 20:55:46 +10:00
|
|
|
{
|
2023-12-19 14:57:49 +01:00
|
|
|
BVHTree *tree = bvhtree_from_mesh_corner_tris_create_tree(epsilon,
|
|
|
|
|
tree_type,
|
|
|
|
|
axis,
|
|
|
|
|
vert_positions,
|
|
|
|
|
corner_verts,
|
|
|
|
|
corner_tris,
|
|
|
|
|
corner_tris_mask,
|
|
|
|
|
corner_tris_num_active);
|
2022-04-05 18:44:52 -03:00
|
|
|
|
2022-04-05 20:33:06 -03:00
|
|
|
bvhtree_balance(tree, false);
|
2020-07-29 10:31:37 +02:00
|
|
|
|
2022-04-04 17:41:36 -03:00
|
|
|
if (data) {
|
|
|
|
|
/* Setup BVHTreeFromMesh */
|
2023-12-19 14:57:49 +01:00
|
|
|
bvhtree_from_mesh_setup_data(tree,
|
|
|
|
|
BVHTREE_FROM_CORNER_TRIS,
|
|
|
|
|
vert_positions,
|
|
|
|
|
{},
|
|
|
|
|
corner_verts,
|
|
|
|
|
corner_tris,
|
|
|
|
|
nullptr,
|
|
|
|
|
data);
|
2022-04-04 17:41:36 -03:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2017-02-16 22:55:01 -03:00
|
|
|
return tree;
|
2015-07-22 20:55:46 +10:00
|
|
|
}
|
|
|
|
|
|
2023-12-19 14:57:49 +01:00
|
|
|
static BitVector<> corner_tris_no_hidden_map_get(const blender::OffsetIndices<int> faces,
|
|
|
|
|
const VArray<bool> &hide_poly,
|
|
|
|
|
const int corner_tris_len,
|
|
|
|
|
int *r_corner_tris_active_len)
|
2019-05-29 01:02:04 -03:00
|
|
|
{
|
Mesh: Move hide flags to generic attributes
This commit moves the hide status of mesh vertices, edges, and faces
from the `ME_FLAG` to optional generic boolean attributes. Storing this
data as generic attributes can significantly simplify and improve code,
as described in T95965.
The attributes are called `.hide_vert`, `.hide_edge`, and `.hide_poly`,
using the attribute name semantics discussed in T97452. The `.` prefix
means they are "UI attributes", so they still contain original data
edited by users, but they aren't meant to be accessed procedurally by
the user in arbitrary situations. They are also be hidden in the
spreadsheet and the attribute list by default,
Until 4.0, the attributes are still written to and read from the mesh
in the old way, so neither forward nor backward compatibility are
affected. This means memory requirements will be increased by one byte
per element when the hide status is used. When the flags are removed
completely, requirements will decrease when hiding is unused.
Further notes:
* Some code can be further simplified to skip some processing when the
hide attributes don't exist.
* The data is still stored in flags for `BMesh`, necessitating some
complexity in the conversion to and from `Mesh`.
* Access to the "hide" property of mesh elements in RNA is slower.
The separate boolean arrays should be used where possible.
Ref T95965
Differential Revision: https://developer.blender.org/D14685
2022-08-11 12:54:24 -04:00
|
|
|
if (hide_poly.is_single() && !hide_poly.get_internal_single()) {
|
2022-11-13 23:23:59 -06:00
|
|
|
return {};
|
Mesh: Move hide flags to generic attributes
This commit moves the hide status of mesh vertices, edges, and faces
from the `ME_FLAG` to optional generic boolean attributes. Storing this
data as generic attributes can significantly simplify and improve code,
as described in T95965.
The attributes are called `.hide_vert`, `.hide_edge`, and `.hide_poly`,
using the attribute name semantics discussed in T97452. The `.` prefix
means they are "UI attributes", so they still contain original data
edited by users, but they aren't meant to be accessed procedurally by
the user in arbitrary situations. They are also be hidden in the
spreadsheet and the attribute list by default,
Until 4.0, the attributes are still written to and read from the mesh
in the old way, so neither forward nor backward compatibility are
affected. This means memory requirements will be increased by one byte
per element when the hide status is used. When the flags are removed
completely, requirements will decrease when hiding is unused.
Further notes:
* Some code can be further simplified to skip some processing when the
hide attributes don't exist.
* The data is still stored in flags for `BMesh`, necessitating some
complexity in the conversion to and from `Mesh`.
* Access to the "hide" property of mesh elements in RNA is slower.
The separate boolean arrays should be used where possible.
Ref T95965
Differential Revision: https://developer.blender.org/D14685
2022-08-11 12:54:24 -04:00
|
|
|
}
|
2023-12-19 14:57:49 +01:00
|
|
|
BitVector<> corner_tris_mask(corner_tris_len);
|
2019-05-29 01:02:04 -03:00
|
|
|
|
2023-12-19 14:57:49 +01:00
|
|
|
int corner_tris_no_hidden_len = 0;
|
|
|
|
|
int tri_index = 0;
|
2023-07-24 22:06:55 +02:00
|
|
|
for (const int64_t i : faces.index_range()) {
|
2023-09-07 08:36:43 -04:00
|
|
|
const int triangles_num = blender::bke::mesh::face_triangles_num(faces[i].size());
|
2022-11-14 11:59:31 -06:00
|
|
|
if (hide_poly[i]) {
|
2023-12-19 14:57:49 +01:00
|
|
|
tri_index += triangles_num;
|
2019-05-29 01:02:04 -03:00
|
|
|
}
|
|
|
|
|
else {
|
2022-11-14 11:59:31 -06:00
|
|
|
for (const int i : IndexRange(triangles_num)) {
|
2022-11-15 16:41:50 +11:00
|
|
|
UNUSED_VARS(i);
|
2023-12-19 14:57:49 +01:00
|
|
|
corner_tris_mask[tri_index].set();
|
|
|
|
|
tri_index++;
|
|
|
|
|
corner_tris_no_hidden_len++;
|
2019-05-29 01:02:04 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-19 14:57:49 +01:00
|
|
|
*r_corner_tris_active_len = corner_tris_no_hidden_len;
|
2019-05-29 01:02:04 -03:00
|
|
|
|
2023-12-19 14:57:49 +01:00
|
|
|
return corner_tris_mask;
|
2019-05-29 01:02:04 -03:00
|
|
|
}
|
|
|
|
|
|
2023-06-03 08:36:28 +10:00
|
|
|
BVHTree *BKE_bvhtree_from_mesh_get(BVHTreeFromMesh *data,
|
|
|
|
|
const Mesh *mesh,
|
2020-05-15 13:54:22 +02:00
|
|
|
const BVHCacheType bvh_cache_type,
|
2018-05-08 20:00:51 -03:00
|
|
|
const int tree_type)
|
|
|
|
|
{
|
2023-12-20 13:13:16 -05:00
|
|
|
using namespace blender;
|
|
|
|
|
using namespace blender::bke;
|
2022-10-12 20:55:26 -05:00
|
|
|
BVHCache **bvh_cache_p = (BVHCache **)&mesh->runtime->bvh_cache;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-12-19 14:57:49 +01:00
|
|
|
Span<int3> corner_tris;
|
|
|
|
|
if (ELEM(bvh_cache_type, BVHTREE_FROM_CORNER_TRIS, BVHTREE_FROM_CORNER_TRIS_NO_HIDDEN)) {
|
|
|
|
|
corner_tris = mesh->corner_tris();
|
2022-04-06 17:08:53 -03:00
|
|
|
}
|
2023-11-28 16:40:43 -05:00
|
|
|
|
|
|
|
|
const Span<float3> positions = mesh->vert_positions();
|
2023-12-28 11:23:40 -05:00
|
|
|
const Span<int2> edges = mesh->edges();
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
const Span<int> corner_verts = mesh->corner_verts();
|
2022-04-06 17:08:53 -03:00
|
|
|
|
2022-04-04 17:41:36 -03:00
|
|
|
/* Setup BVHTreeFromMesh */
|
2022-11-14 11:59:31 -06:00
|
|
|
bvhtree_from_mesh_setup_data(nullptr,
|
|
|
|
|
bvh_cache_type,
|
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
|
|
|
positions,
|
2023-11-28 16:40:43 -05:00
|
|
|
edges,
|
|
|
|
|
corner_verts,
|
2023-12-19 14:57:49 +01:00
|
|
|
corner_tris,
|
2023-11-28 16:40:43 -05:00
|
|
|
(const MFace *)CustomData_get_layer(&mesh->fdata_legacy, CD_MFACE),
|
2022-11-14 11:59:31 -06:00
|
|
|
data);
|
|
|
|
|
|
2022-04-05 18:44:52 -03:00
|
|
|
bool lock_started = false;
|
|
|
|
|
data->cached = bvhcache_find(
|
2022-10-12 22:31:02 -05:00
|
|
|
bvh_cache_p, bvh_cache_type, &data->tree, &lock_started, &mesh->runtime->eval_mutex);
|
2022-04-04 17:41:36 -03:00
|
|
|
|
|
|
|
|
if (data->cached) {
|
2022-04-05 18:44:52 -03:00
|
|
|
BLI_assert(lock_started == false);
|
|
|
|
|
|
2022-04-04 17:41:36 -03:00
|
|
|
/* NOTE: #data->tree can be nullptr. */
|
|
|
|
|
return data->tree;
|
2018-05-12 19:43:36 -03:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-04-05 18:44:52 -03:00
|
|
|
/* Create BVHTree. */
|
2022-04-06 17:08:53 -03:00
|
|
|
|
2019-08-22 14:07:40 -03:00
|
|
|
switch (bvh_cache_type) {
|
2023-04-21 08:43:07 -04:00
|
|
|
case BVHTREE_FROM_LOOSEVERTS: {
|
2023-12-28 11:23:40 -05:00
|
|
|
const LooseVertCache &loose_verts = mesh->loose_verts();
|
2023-11-28 16:40:43 -05:00
|
|
|
data->tree = bvhtree_from_mesh_verts_create_tree(
|
|
|
|
|
0.0f, tree_type, 6, positions, loose_verts.is_loose_bits, loose_verts.count);
|
2022-04-06 17:08:53 -03:00
|
|
|
break;
|
2023-04-21 08:43:07 -04:00
|
|
|
}
|
|
|
|
|
case BVHTREE_FROM_VERTS: {
|
2023-11-28 16:40:43 -05:00
|
|
|
data->tree = bvhtree_from_mesh_verts_create_tree(0.0f, tree_type, 6, positions, {}, -1);
|
2023-04-21 08:43:07 -04:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case BVHTREE_FROM_LOOSEEDGES: {
|
2023-12-28 11:23:40 -05:00
|
|
|
const LooseEdgeCache &loose_edges = mesh->loose_edges();
|
2022-04-06 17:08:53 -03:00
|
|
|
data->tree = bvhtree_from_mesh_edges_create_tree(
|
2023-04-21 08:43:07 -04:00
|
|
|
positions, edges, loose_edges.is_loose_bits, loose_edges.count, 0.0f, tree_type, 6);
|
2022-04-06 17:08:53 -03:00
|
|
|
break;
|
2023-04-21 08:43:07 -04:00
|
|
|
}
|
|
|
|
|
case BVHTREE_FROM_EDGES: {
|
|
|
|
|
data->tree = bvhtree_from_mesh_edges_create_tree(
|
|
|
|
|
positions, edges, {}, -1, 0.0f, tree_type, 6);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case BVHTREE_FROM_FACES: {
|
2023-07-24 22:06:55 +02:00
|
|
|
BLI_assert(!(mesh->totface_legacy == 0 && mesh->faces_num != 0));
|
2022-04-05 20:33:06 -03:00
|
|
|
data->tree = bvhtree_from_mesh_faces_create_tree(
|
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
|
|
|
0.0f,
|
|
|
|
|
tree_type,
|
|
|
|
|
6,
|
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
|
|
|
positions,
|
2023-07-24 22:06:55 +02:00
|
|
|
(const MFace *)CustomData_get_layer(&mesh->fdata_legacy, CD_MFACE),
|
|
|
|
|
mesh->totface_legacy,
|
2022-11-13 23:23:59 -06: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
|
|
|
-1);
|
2022-04-06 17:08:53 -03:00
|
|
|
break;
|
2023-04-21 08:43:07 -04:00
|
|
|
}
|
2023-12-19 14:57:49 +01:00
|
|
|
case BVHTREE_FROM_CORNER_TRIS_NO_HIDDEN: {
|
2023-12-28 11:23:40 -05:00
|
|
|
AttributeAccessor attributes = mesh->attributes();
|
2023-04-21 08:43:07 -04:00
|
|
|
int mask_bits_act_len = -1;
|
2023-12-19 14:57:49 +01:00
|
|
|
const BitVector<> mask = corner_tris_no_hidden_map_get(
|
2023-07-24 22:06:55 +02:00
|
|
|
mesh->faces(),
|
2023-12-20 13:13:16 -05:00
|
|
|
*attributes.lookup_or_default(".hide_poly", AttrDomain::Face, false),
|
2023-12-19 14:57:49 +01:00
|
|
|
corner_tris.size(),
|
Mesh: Move hide flags to generic attributes
This commit moves the hide status of mesh vertices, edges, and faces
from the `ME_FLAG` to optional generic boolean attributes. Storing this
data as generic attributes can significantly simplify and improve code,
as described in T95965.
The attributes are called `.hide_vert`, `.hide_edge`, and `.hide_poly`,
using the attribute name semantics discussed in T97452. The `.` prefix
means they are "UI attributes", so they still contain original data
edited by users, but they aren't meant to be accessed procedurally by
the user in arbitrary situations. They are also be hidden in the
spreadsheet and the attribute list by default,
Until 4.0, the attributes are still written to and read from the mesh
in the old way, so neither forward nor backward compatibility are
affected. This means memory requirements will be increased by one byte
per element when the hide status is used. When the flags are removed
completely, requirements will decrease when hiding is unused.
Further notes:
* Some code can be further simplified to skip some processing when the
hide attributes don't exist.
* The data is still stored in flags for `BMesh`, necessitating some
complexity in the conversion to and from `Mesh`.
* Access to the "hide" property of mesh elements in RNA is slower.
The separate boolean arrays should be used where possible.
Ref T95965
Differential Revision: https://developer.blender.org/D14685
2022-08-11 12:54:24 -04:00
|
|
|
&mask_bits_act_len);
|
2023-12-19 14:57:49 +01:00
|
|
|
data->tree = bvhtree_from_mesh_corner_tris_create_tree(
|
|
|
|
|
0.0f, tree_type, 6, positions, corner_verts, corner_tris, mask, mask_bits_act_len);
|
2022-04-06 17:08:53 -03:00
|
|
|
break;
|
2023-04-21 08:43:07 -04:00
|
|
|
}
|
2023-12-19 14:57:49 +01:00
|
|
|
case BVHTREE_FROM_CORNER_TRIS: {
|
|
|
|
|
data->tree = bvhtree_from_mesh_corner_tris_create_tree(
|
|
|
|
|
0.0f, tree_type, 6, positions, corner_verts, corner_tris, {}, -1);
|
2023-04-21 08:43:07 -04:00
|
|
|
break;
|
|
|
|
|
}
|
2023-06-23 15:36:57 -03:00
|
|
|
case BVHTREE_FROM_EM_LOOSEVERTS:
|
2018-07-23 11:04:58 -03:00
|
|
|
case BVHTREE_FROM_EM_EDGES:
|
2023-12-14 12:08:21 +11:00
|
|
|
case BVHTREE_FROM_EM_LOOPTRIS:
|
2020-06-02 15:59:30 +02:00
|
|
|
case BVHTREE_MAX_ITEM:
|
2023-04-21 08:43:07 -04:00
|
|
|
BLI_assert_unreachable();
|
2018-07-23 11:04:58 -03:00
|
|
|
break;
|
2018-05-08 20:00:51 -03:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-04-05 20:33:06 -03:00
|
|
|
bvhtree_balance(data->tree, lock_started);
|
|
|
|
|
|
2022-04-05 18:44:52 -03:00
|
|
|
/* Save on cache for later use */
|
|
|
|
|
// printf("BVHTree built and saved on cache\n");
|
|
|
|
|
BLI_assert(data->cached == false);
|
2022-04-04 17:41:36 -03:00
|
|
|
data->cached = true;
|
2022-04-05 18:44:52 -03:00
|
|
|
bvhcache_insert(*bvh_cache_p, data->tree, bvh_cache_type);
|
|
|
|
|
bvhcache_unlock(*bvh_cache_p, lock_started);
|
2022-04-04 17:41:36 -03:00
|
|
|
|
2023-12-04 15:13:06 +01:00
|
|
|
#ifndef NDEBUG
|
2022-04-04 17:41:36 -03:00
|
|
|
if (data->tree != nullptr) {
|
2019-08-22 14:07:40 -03:00
|
|
|
if (BLI_bvhtree_get_tree_type(data->tree) != tree_type) {
|
2018-05-16 15:26:33 -03:00
|
|
|
printf("tree_type %d obtained instead of %d\n",
|
2019-08-22 14:07:40 -03:00
|
|
|
BLI_bvhtree_get_tree_type(data->tree),
|
2018-05-16 15:26:33 -03:00
|
|
|
tree_type);
|
2018-05-08 20:00:51 -03:00
|
|
|
}
|
|
|
|
|
}
|
2022-04-04 17:41:36 -03:00
|
|
|
#endif
|
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-04 17:41:36 -03:00
|
|
|
return data->tree;
|
2018-05-08 20:00:51 -03:00
|
|
|
}
|
|
|
|
|
|
2023-06-23 15:36:57 -03:00
|
|
|
static BitVector<> bmverts_loose_map_get(BMesh *bm, int *r_bmvert_active_len)
|
|
|
|
|
{
|
|
|
|
|
BitVector<> bmvert_mask(bm->totvert);
|
|
|
|
|
|
|
|
|
|
int i, bmvert_loose_len = 0;
|
|
|
|
|
BMIter iter;
|
|
|
|
|
BMVert *v;
|
|
|
|
|
BM_ITER_MESH_INDEX (v, &iter, bm, BM_VERTS_OF_MESH, i) {
|
|
|
|
|
if (v->e == nullptr) {
|
|
|
|
|
bmvert_mask[i].set();
|
|
|
|
|
bmvert_loose_len++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*r_bmvert_active_len = bmvert_loose_len;
|
|
|
|
|
|
|
|
|
|
return bmvert_mask;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-23 19:45:09 -03:00
|
|
|
BVHTree *BKE_bvhtree_from_editmesh_get(BVHTreeFromEditMesh *data,
|
2023-06-03 08:36:28 +10:00
|
|
|
BMEditMesh *em,
|
2019-08-23 19:45:09 -03:00
|
|
|
const int tree_type,
|
2020-05-15 13:54:22 +02:00
|
|
|
const BVHCacheType bvh_cache_type,
|
2020-06-02 15:59:30 +02:00
|
|
|
BVHCache **bvh_cache_p,
|
2022-10-12 22:31:02 -05:00
|
|
|
std::mutex *mesh_eval_mutex)
|
2019-08-23 19:45:09 -03:00
|
|
|
{
|
2022-04-05 18:44:52 -03:00
|
|
|
bool lock_started = false;
|
|
|
|
|
|
2023-06-30 12:36:48 -03:00
|
|
|
bvhtree_from_editmesh_setup_data(nullptr, bvh_cache_type, data);
|
2019-08-23 19:45:09 -03:00
|
|
|
|
2020-06-02 15:59:30 +02:00
|
|
|
if (bvh_cache_p) {
|
2022-04-05 18:44:52 -03:00
|
|
|
data->cached = bvhcache_find(
|
|
|
|
|
bvh_cache_p, bvh_cache_type, &data->tree, &lock_started, mesh_eval_mutex);
|
2019-08-23 19:45:09 -03:00
|
|
|
|
2022-04-04 17:41:36 -03:00
|
|
|
if (data->cached) {
|
2022-04-05 18:44:52 -03:00
|
|
|
BLI_assert(lock_started == false);
|
2022-04-04 17:41:36 -03:00
|
|
|
return data->tree;
|
2019-08-23 19:45:09 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (bvh_cache_type) {
|
2023-06-23 15:36:57 -03:00
|
|
|
case BVHTREE_FROM_EM_LOOSEVERTS: {
|
|
|
|
|
int mask_bits_act_len = -1;
|
|
|
|
|
const BitVector<> mask = bmverts_loose_map_get(em->bm, &mask_bits_act_len);
|
|
|
|
|
data->tree = bvhtree_from_editmesh_verts_create_tree(
|
|
|
|
|
0.0f, tree_type, 6, em, mask, mask_bits_act_len);
|
2019-08-23 19:45:09 -03:00
|
|
|
break;
|
2023-06-23 15:36:57 -03:00
|
|
|
}
|
2019-08-23 19:45:09 -03:00
|
|
|
case BVHTREE_FROM_EM_EDGES:
|
2022-11-13 23:23:59 -06:00
|
|
|
data->tree = bvhtree_from_editmesh_edges_create_tree(0.0f, tree_type, 6, em, {}, -1);
|
2019-08-23 19:45:09 -03:00
|
|
|
break;
|
2023-12-14 12:08:21 +11:00
|
|
|
case BVHTREE_FROM_EM_LOOPTRIS:
|
2023-12-19 14:57:49 +01:00
|
|
|
data->tree = bvhtree_from_editmesh_corner_tris_create_tree(0.0f, tree_type, 6, em, {}, -1);
|
2019-08-23 19:45:09 -03:00
|
|
|
break;
|
|
|
|
|
case BVHTREE_FROM_VERTS:
|
|
|
|
|
case BVHTREE_FROM_EDGES:
|
|
|
|
|
case BVHTREE_FROM_FACES:
|
2023-12-19 14:57:49 +01:00
|
|
|
case BVHTREE_FROM_CORNER_TRIS:
|
|
|
|
|
case BVHTREE_FROM_CORNER_TRIS_NO_HIDDEN:
|
2019-08-23 19:45:09 -03:00
|
|
|
case BVHTREE_FROM_LOOSEVERTS:
|
|
|
|
|
case BVHTREE_FROM_LOOSEEDGES:
|
2020-06-02 15:59:30 +02:00
|
|
|
case BVHTREE_MAX_ITEM:
|
2019-08-23 19:45:09 -03:00
|
|
|
BLI_assert(false);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-05 20:33:06 -03:00
|
|
|
bvhtree_balance(data->tree, lock_started);
|
|
|
|
|
|
2022-04-05 18:44:52 -03:00
|
|
|
if (bvh_cache_p) {
|
|
|
|
|
/* Save on cache for later use */
|
|
|
|
|
// printf("BVHTree built and saved on cache\n");
|
|
|
|
|
BLI_assert(data->cached == false);
|
|
|
|
|
data->cached = true;
|
|
|
|
|
bvhcache_insert(*bvh_cache_p, data->tree, bvh_cache_type);
|
|
|
|
|
bvhcache_unlock(*bvh_cache_p, lock_started);
|
|
|
|
|
}
|
2022-04-04 17:41:36 -03:00
|
|
|
|
2023-12-04 15:13:06 +01:00
|
|
|
#ifndef NDEBUG
|
2022-04-04 17:41:36 -03:00
|
|
|
if (data->tree != nullptr) {
|
2019-08-23 19:45:09 -03:00
|
|
|
if (BLI_bvhtree_get_tree_type(data->tree) != tree_type) {
|
|
|
|
|
printf("tree_type %d obtained instead of %d\n",
|
|
|
|
|
BLI_bvhtree_get_tree_type(data->tree),
|
|
|
|
|
tree_type);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-04 17:41:36 -03:00
|
|
|
#endif
|
2019-08-23 19:45:09 -03:00
|
|
|
|
2022-04-04 17:41:36 -03:00
|
|
|
return data->tree;
|
2019-08-23 19:45:09 -03:00
|
|
|
}
|
|
|
|
|
|
2015-07-22 20:55:46 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
2021-12-14 15:49:31 +11:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Free Functions
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2023-06-03 08:36:28 +10:00
|
|
|
void free_bvhtree_from_editmesh(BVHTreeFromEditMesh *data)
|
2016-05-06 04:49:21 +10:00
|
|
|
{
|
|
|
|
|
if (data->tree) {
|
2016-06-29 19:31:57 +10:00
|
|
|
if (!data->cached) {
|
|
|
|
|
BLI_bvhtree_free(data->tree);
|
|
|
|
|
}
|
2016-05-06 04:49:21 +10:00
|
|
|
memset(data, 0, sizeof(*data));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-03 08:36:28 +10:00
|
|
|
void free_bvhtree_from_mesh(BVHTreeFromMesh *data)
|
2008-08-11 13:29:38 +00:00
|
|
|
{
|
2017-02-16 22:55:01 -03:00
|
|
|
if (data->tree && !data->cached) {
|
|
|
|
|
BLI_bvhtree_free(data->tree);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-11-28 16:40:43 -05:00
|
|
|
*data = {};
|
2008-08-11 13:29:38 +00:00
|
|
|
}
|
2021-02-05 08:28:31 -06:00
|
|
|
|
|
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Point Cloud BVH Building
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2023-07-10 18:12:41 +02:00
|
|
|
[[nodiscard]] BVHTree *BKE_bvhtree_from_pointcloud_get(BVHTreeFromPointCloud *data,
|
|
|
|
|
const PointCloud *pointcloud,
|
|
|
|
|
const int tree_type)
|
2021-02-05 08:28:31 -06:00
|
|
|
{
|
2023-06-15 16:52:40 -03:00
|
|
|
int tot_point = pointcloud->totpoint;
|
2023-06-16 12:20:40 +10:00
|
|
|
BVHTree *tree = bvhtree_new_common(0.0f, tree_type, 6, tot_point, tot_point);
|
2021-02-05 08:28:31 -06:00
|
|
|
if (!tree) {
|
2021-07-01 16:46:55 -05:00
|
|
|
return nullptr;
|
2021-02-05 08:28:31 -06:00
|
|
|
}
|
|
|
|
|
|
2023-04-13 11:55:32 -04:00
|
|
|
const Span<float3> positions = pointcloud->positions();
|
2022-07-19 18:06:56 -05:00
|
|
|
for (const int i : positions.index_range()) {
|
|
|
|
|
BLI_bvhtree_insert(tree, i, positions[i], 1);
|
2021-02-05 08:28:31 -06:00
|
|
|
}
|
2023-06-15 16:52:40 -03:00
|
|
|
|
|
|
|
|
BLI_assert(BLI_bvhtree_get_len(tree) == tot_point);
|
2021-06-14 23:50:24 +10:00
|
|
|
bvhtree_balance(tree, false);
|
2021-02-05 08:28:31 -06:00
|
|
|
|
2022-07-19 18:06:56 -05:00
|
|
|
data->coords = (const float(*)[3])positions.data();
|
2021-02-05 08:28:31 -06:00
|
|
|
data->tree = tree;
|
2021-07-01 16:46:55 -05:00
|
|
|
data->nearest_callback = nullptr;
|
2021-02-05 08:28:31 -06:00
|
|
|
|
|
|
|
|
return tree;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void free_bvhtree_from_pointcloud(BVHTreeFromPointCloud *data)
|
|
|
|
|
{
|
|
|
|
|
if (data->tree) {
|
|
|
|
|
BLI_bvhtree_free(data->tree);
|
|
|
|
|
}
|
|
|
|
|
memset(data, 0, sizeof(*data));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \} */
|