2022-02-11 09:07:11 +11:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2021-04-21 17:02:19 +02:00
|
|
|
|
|
|
|
|
#include "BKE_attribute_math.hh"
|
2022-06-17 15:31:07 +02:00
|
|
|
#include "BKE_bvhutils.h"
|
2023-03-12 22:29:15 +01:00
|
|
|
#include "BKE_mesh.hh"
|
2021-04-21 17:02:19 +02:00
|
|
|
#include "BKE_mesh_runtime.h"
|
|
|
|
|
#include "BKE_mesh_sample.hh"
|
|
|
|
|
|
|
|
|
|
#include "DNA_mesh_types.h"
|
|
|
|
|
#include "DNA_meshdata_types.h"
|
|
|
|
|
|
2022-06-17 15:31:07 +02:00
|
|
|
#include "BLI_rand.hh"
|
|
|
|
|
|
2021-04-21 17:02:19 +02:00
|
|
|
namespace blender::bke::mesh_surface_sample {
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
BLI_NOINLINE static void sample_point_attribute(const Mesh &mesh,
|
|
|
|
|
const Span<int> looptri_indices,
|
|
|
|
|
const Span<float3> bary_coords,
|
2022-08-06 14:31:57 -05:00
|
|
|
const VArray<T> &src,
|
2021-10-15 14:08:52 -05:00
|
|
|
const IndexMask mask,
|
2022-08-06 14:31:57 -05:00
|
|
|
const MutableSpan<T> dst)
|
2021-04-21 17:02:19 +02:00
|
|
|
{
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
const Span<MLoop> loops = mesh.loops();
|
2022-09-24 11:41:08 +02:00
|
|
|
const Span<MLoopTri> looptris = mesh.looptris();
|
2021-04-21 17:02:19 +02:00
|
|
|
|
2021-10-15 14:08:52 -05:00
|
|
|
for (const int i : mask) {
|
2021-04-21 17:02:19 +02:00
|
|
|
const int looptri_index = looptri_indices[i];
|
|
|
|
|
const MLoopTri &looptri = looptris[looptri_index];
|
|
|
|
|
const float3 &bary_coord = bary_coords[i];
|
|
|
|
|
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
const int v0_index = loops[looptri.tri[0]].v;
|
|
|
|
|
const int v1_index = loops[looptri.tri[1]].v;
|
|
|
|
|
const int v2_index = loops[looptri.tri[2]].v;
|
2021-04-21 17:02:19 +02:00
|
|
|
|
2022-08-06 14:31:57 -05:00
|
|
|
const T v0 = src[v0_index];
|
|
|
|
|
const T v1 = src[v1_index];
|
|
|
|
|
const T v2 = src[v2_index];
|
2021-04-21 17:02:19 +02:00
|
|
|
|
|
|
|
|
const T interpolated_value = attribute_math::mix3(bary_coord, v0, v1, v2);
|
2022-08-06 14:31:57 -05:00
|
|
|
dst[i] = interpolated_value;
|
2021-04-21 17:02:19 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void sample_point_attribute(const Mesh &mesh,
|
|
|
|
|
const Span<int> looptri_indices,
|
|
|
|
|
const Span<float3> bary_coords,
|
2022-08-06 14:31:57 -05:00
|
|
|
const GVArray &src,
|
2021-10-15 14:08:52 -05:00
|
|
|
const IndexMask mask,
|
2022-08-06 14:31:57 -05:00
|
|
|
const GMutableSpan dst)
|
2021-04-21 17:02:19 +02:00
|
|
|
{
|
2022-08-06 14:31:57 -05:00
|
|
|
BLI_assert(src.size() == mesh.totvert);
|
|
|
|
|
BLI_assert(src.type() == dst.type());
|
2021-04-21 17:02:19 +02:00
|
|
|
|
2022-08-06 14:31:57 -05:00
|
|
|
const CPPType &type = src.type();
|
2021-04-21 17:02:19 +02:00
|
|
|
attribute_math::convert_to_static_type(type, [&](auto dummy) {
|
|
|
|
|
using T = decltype(dummy);
|
|
|
|
|
sample_point_attribute<T>(
|
2022-08-06 14:31:57 -05:00
|
|
|
mesh, looptri_indices, bary_coords, src.typed<T>(), mask, dst.typed<T>());
|
2021-04-21 17:02:19 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
BLI_NOINLINE static void sample_corner_attribute(const Mesh &mesh,
|
|
|
|
|
const Span<int> looptri_indices,
|
|
|
|
|
const Span<float3> bary_coords,
|
2022-08-06 14:31:57 -05:00
|
|
|
const VArray<T> &src,
|
2021-10-15 14:08:52 -05:00
|
|
|
const IndexMask mask,
|
2022-08-06 14:31:57 -05:00
|
|
|
const MutableSpan<T> dst)
|
2021-04-21 17:02:19 +02:00
|
|
|
{
|
2022-09-24 11:41:08 +02:00
|
|
|
const Span<MLoopTri> looptris = mesh.looptris();
|
2021-04-21 17:02:19 +02:00
|
|
|
|
2021-10-15 14:08:52 -05:00
|
|
|
for (const int i : mask) {
|
2021-04-21 17:02:19 +02:00
|
|
|
const int looptri_index = looptri_indices[i];
|
|
|
|
|
const MLoopTri &looptri = looptris[looptri_index];
|
|
|
|
|
const float3 &bary_coord = bary_coords[i];
|
|
|
|
|
|
|
|
|
|
const int loop_index_0 = looptri.tri[0];
|
|
|
|
|
const int loop_index_1 = looptri.tri[1];
|
|
|
|
|
const int loop_index_2 = looptri.tri[2];
|
|
|
|
|
|
2022-08-06 14:31:57 -05:00
|
|
|
const T v0 = src[loop_index_0];
|
|
|
|
|
const T v1 = src[loop_index_1];
|
|
|
|
|
const T v2 = src[loop_index_2];
|
2021-04-21 17:02:19 +02:00
|
|
|
|
|
|
|
|
const T interpolated_value = attribute_math::mix3(bary_coord, v0, v1, v2);
|
2022-08-06 14:31:57 -05:00
|
|
|
dst[i] = interpolated_value;
|
2021-04-21 17:02:19 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void sample_corner_attribute(const Mesh &mesh,
|
|
|
|
|
const Span<int> looptri_indices,
|
|
|
|
|
const Span<float3> bary_coords,
|
2022-08-06 14:31:57 -05:00
|
|
|
const GVArray &src,
|
2021-10-15 14:08:52 -05:00
|
|
|
const IndexMask mask,
|
2022-08-06 14:31:57 -05:00
|
|
|
const GMutableSpan dst)
|
2021-04-21 17:02:19 +02:00
|
|
|
{
|
2022-08-06 14:31:57 -05:00
|
|
|
BLI_assert(src.size() == mesh.totloop);
|
|
|
|
|
BLI_assert(src.type() == dst.type());
|
2021-04-21 17:02:19 +02:00
|
|
|
|
2022-08-06 14:31:57 -05:00
|
|
|
const CPPType &type = src.type();
|
2021-04-21 17:02:19 +02:00
|
|
|
attribute_math::convert_to_static_type(type, [&](auto dummy) {
|
|
|
|
|
using T = decltype(dummy);
|
|
|
|
|
sample_corner_attribute<T>(
|
2022-08-06 14:31:57 -05:00
|
|
|
mesh, looptri_indices, bary_coords, src.typed<T>(), mask, dst.typed<T>());
|
2021-04-21 17:02:19 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
void sample_face_attribute(const Mesh &mesh,
|
|
|
|
|
const Span<int> looptri_indices,
|
2022-08-06 14:31:57 -05:00
|
|
|
const VArray<T> &src,
|
2021-10-15 14:08:52 -05:00
|
|
|
const IndexMask mask,
|
2022-08-06 14:31:57 -05:00
|
|
|
const MutableSpan<T> dst)
|
2021-04-21 17:02:19 +02:00
|
|
|
{
|
2022-09-24 11:41:08 +02:00
|
|
|
const Span<MLoopTri> looptris = mesh.looptris();
|
2021-04-21 17:02:19 +02:00
|
|
|
|
2021-10-15 14:08:52 -05:00
|
|
|
for (const int i : mask) {
|
2021-04-21 17:02:19 +02:00
|
|
|
const int looptri_index = looptri_indices[i];
|
|
|
|
|
const MLoopTri &looptri = looptris[looptri_index];
|
|
|
|
|
const int poly_index = looptri.poly;
|
2022-08-06 14:31:57 -05:00
|
|
|
dst[i] = src[poly_index];
|
2021-04-21 17:02:19 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void sample_face_attribute(const Mesh &mesh,
|
|
|
|
|
const Span<int> looptri_indices,
|
2022-08-06 14:31:57 -05:00
|
|
|
const GVArray &src,
|
2021-10-15 14:08:52 -05:00
|
|
|
const IndexMask mask,
|
2022-08-06 14:31:57 -05:00
|
|
|
const GMutableSpan dst)
|
2021-04-21 17:02:19 +02:00
|
|
|
{
|
2022-08-06 14:31:57 -05:00
|
|
|
BLI_assert(src.size() == mesh.totpoly);
|
|
|
|
|
BLI_assert(src.type() == dst.type());
|
2021-04-21 17:02:19 +02:00
|
|
|
|
2022-08-06 14:31:57 -05:00
|
|
|
const CPPType &type = src.type();
|
2021-04-21 17:02:19 +02:00
|
|
|
attribute_math::convert_to_static_type(type, [&](auto dummy) {
|
|
|
|
|
using T = decltype(dummy);
|
2022-08-06 14:31:57 -05:00
|
|
|
sample_face_attribute<T>(mesh, looptri_indices, src.typed<T>(), mask, dst.typed<T>());
|
2021-04-21 17:02:19 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-17 09:31:53 +01:00
|
|
|
MeshAttributeInterpolator::MeshAttributeInterpolator(const Mesh *mesh,
|
2021-10-19 09:01:39 -05:00
|
|
|
const IndexMask mask,
|
2021-06-18 14:27:41 +10:00
|
|
|
const Span<float3> positions,
|
|
|
|
|
const Span<int> looptri_indices)
|
2021-10-19 09:01:39 -05:00
|
|
|
: mesh_(mesh), mask_(mask), positions_(positions), looptri_indices_(looptri_indices)
|
2021-06-17 09:31:53 +01:00
|
|
|
{
|
|
|
|
|
BLI_assert(positions.size() == looptri_indices.size());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Span<float3> MeshAttributeInterpolator::ensure_barycentric_coords()
|
|
|
|
|
{
|
|
|
|
|
if (!bary_coords_.is_empty()) {
|
2021-10-19 09:01:39 -05:00
|
|
|
BLI_assert(bary_coords_.size() >= mask_.min_array_size());
|
2021-06-17 09:31:53 +01:00
|
|
|
return bary_coords_;
|
|
|
|
|
}
|
2021-10-19 09:01:39 -05:00
|
|
|
bary_coords_.reinitialize(mask_.min_array_size());
|
2021-06-17 09:31:53 +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
|
|
|
const Span<float3> positions = mesh_->vert_positions();
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
const Span<MLoop> loops = mesh_->loops();
|
2022-09-24 11:41:08 +02:00
|
|
|
const Span<MLoopTri> looptris = mesh_->looptris();
|
2021-06-17 09:31:53 +01:00
|
|
|
|
2021-10-19 09:01:39 -05:00
|
|
|
for (const int i : mask_) {
|
2021-06-17 09:31:53 +01:00
|
|
|
const int looptri_index = looptri_indices_[i];
|
|
|
|
|
const MLoopTri &looptri = looptris[looptri_index];
|
|
|
|
|
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
const int v0_index = loops[looptri.tri[0]].v;
|
|
|
|
|
const int v1_index = loops[looptri.tri[1]].v;
|
|
|
|
|
const int v2_index = loops[looptri.tri[2]].v;
|
2021-06-17 09:31:53 +01:00
|
|
|
|
|
|
|
|
interp_weights_tri_v3(bary_coords_[i],
|
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[v0_index],
|
|
|
|
|
positions[v1_index],
|
|
|
|
|
positions[v2_index],
|
2021-06-17 09:31:53 +01:00
|
|
|
positions_[i]);
|
|
|
|
|
}
|
|
|
|
|
return bary_coords_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Span<float3> MeshAttributeInterpolator::ensure_nearest_weights()
|
|
|
|
|
{
|
|
|
|
|
if (!nearest_weights_.is_empty()) {
|
2021-10-19 09:01:39 -05:00
|
|
|
BLI_assert(nearest_weights_.size() >= mask_.min_array_size());
|
2021-06-17 09:31:53 +01:00
|
|
|
return nearest_weights_;
|
|
|
|
|
}
|
2021-10-19 09:01:39 -05:00
|
|
|
nearest_weights_.reinitialize(mask_.min_array_size());
|
2021-06-17 09:31:53 +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
|
|
|
const Span<float3> positions = mesh_->vert_positions();
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
const Span<MLoop> loops = mesh_->loops();
|
2022-09-24 11:41:08 +02:00
|
|
|
const Span<MLoopTri> looptris = mesh_->looptris();
|
2021-06-17 09:31:53 +01:00
|
|
|
|
2021-10-19 09:01:39 -05:00
|
|
|
for (const int i : mask_) {
|
2021-06-17 09:31:53 +01:00
|
|
|
const int looptri_index = looptri_indices_[i];
|
|
|
|
|
const MLoopTri &looptri = looptris[looptri_index];
|
|
|
|
|
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
const int v0_index = loops[looptri.tri[0]].v;
|
|
|
|
|
const int v1_index = loops[looptri.tri[1]].v;
|
|
|
|
|
const int v2_index = loops[looptri.tri[2]].v;
|
2021-06-17 09:31:53 +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
|
|
|
const float d0 = len_squared_v3v3(positions_[i], positions[v0_index]);
|
|
|
|
|
const float d1 = len_squared_v3v3(positions_[i], positions[v1_index]);
|
|
|
|
|
const float d2 = len_squared_v3v3(positions_[i], positions[v2_index]);
|
2021-06-17 09:31:53 +01:00
|
|
|
|
|
|
|
|
nearest_weights_[i] = MIN3_PAIR(d0, d1, d2, float3(1, 0, 0), float3(0, 1, 0), float3(0, 0, 1));
|
|
|
|
|
}
|
|
|
|
|
return nearest_weights_;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-15 14:08:52 -05:00
|
|
|
void MeshAttributeInterpolator::sample_data(const GVArray &src,
|
2022-06-01 14:38:06 +10:00
|
|
|
const eAttrDomain domain,
|
2021-10-15 14:08:52 -05:00
|
|
|
const eAttributeMapMode mode,
|
|
|
|
|
const GMutableSpan dst)
|
2021-06-17 09:31:53 +01:00
|
|
|
{
|
2021-10-15 14:08:52 -05:00
|
|
|
if (src.is_empty() || dst.is_empty()) {
|
2021-06-17 09:31:53 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Compute barycentric coordinates only when they are needed. */
|
|
|
|
|
Span<float3> weights;
|
2021-10-15 14:08:52 -05:00
|
|
|
if (ELEM(domain, ATTR_DOMAIN_POINT, ATTR_DOMAIN_CORNER)) {
|
2021-06-17 09:31:53 +01:00
|
|
|
switch (mode) {
|
|
|
|
|
case eAttributeMapMode::INTERPOLATED:
|
2022-08-06 14:31:57 -05:00
|
|
|
weights = this->ensure_barycentric_coords();
|
2021-06-17 09:31:53 +01:00
|
|
|
break;
|
|
|
|
|
case eAttributeMapMode::NEAREST:
|
2022-08-06 14:31:57 -05:00
|
|
|
weights = this->ensure_nearest_weights();
|
2021-06-17 09:31:53 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Interpolate the source attributes on the surface. */
|
2021-10-15 14:08:52 -05:00
|
|
|
switch (domain) {
|
2022-08-06 14:31:57 -05:00
|
|
|
case ATTR_DOMAIN_POINT:
|
2021-10-19 09:01:39 -05:00
|
|
|
sample_point_attribute(*mesh_, looptri_indices_, weights, src, mask_, dst);
|
2021-06-17 09:31:53 +01:00
|
|
|
break;
|
2022-08-06 14:31:57 -05:00
|
|
|
case ATTR_DOMAIN_FACE:
|
2021-10-19 09:01:39 -05:00
|
|
|
sample_face_attribute(*mesh_, looptri_indices_, src, mask_, dst);
|
2021-06-17 09:31:53 +01:00
|
|
|
break;
|
2022-08-06 14:31:57 -05:00
|
|
|
case ATTR_DOMAIN_CORNER:
|
2021-10-19 09:01:39 -05:00
|
|
|
sample_corner_attribute(*mesh_, looptri_indices_, weights, src, mask_, dst);
|
2021-06-17 09:31:53 +01:00
|
|
|
break;
|
2022-08-06 14:31:57 -05:00
|
|
|
case ATTR_DOMAIN_EDGE:
|
2021-06-17 09:31:53 +01:00
|
|
|
/* Not yet supported. */
|
|
|
|
|
break;
|
2022-08-06 14:31:57 -05:00
|
|
|
default:
|
2021-06-17 09:31:53 +01:00
|
|
|
BLI_assert_unreachable();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-17 15:31:07 +02:00
|
|
|
int sample_surface_points_spherical(RandomNumberGenerator &rng,
|
|
|
|
|
const Mesh &mesh,
|
|
|
|
|
const Span<int> looptri_indices_to_sample,
|
|
|
|
|
const float3 &sample_pos,
|
|
|
|
|
const float sample_radius,
|
|
|
|
|
const float approximate_density,
|
|
|
|
|
Vector<float3> &r_bary_coords,
|
|
|
|
|
Vector<int> &r_looptri_indices,
|
|
|
|
|
Vector<float3> &r_positions)
|
|
|
|
|
{
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
const Span<float3> positions = mesh.vert_positions();
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
const Span<MLoop> loops = mesh.loops();
|
2022-09-24 11:41:08 +02:00
|
|
|
const Span<MLoopTri> looptris = mesh.looptris();
|
2022-06-17 15:31:07 +02:00
|
|
|
|
|
|
|
|
const float sample_radius_sq = pow2f(sample_radius);
|
|
|
|
|
const float sample_plane_area = M_PI * sample_radius_sq;
|
|
|
|
|
/* Used for switching between two triangle sampling strategies. */
|
|
|
|
|
const float area_threshold = sample_plane_area;
|
|
|
|
|
|
|
|
|
|
const int old_num = r_bary_coords.size();
|
|
|
|
|
|
|
|
|
|
for (const int looptri_index : looptri_indices_to_sample) {
|
|
|
|
|
const MLoopTri &looptri = looptris[looptri_index];
|
|
|
|
|
|
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 float3 &v0 = positions[loops[looptri.tri[0]].v];
|
|
|
|
|
const float3 &v1 = positions[loops[looptri.tri[1]].v];
|
|
|
|
|
const float3 &v2 = positions[loops[looptri.tri[2]].v];
|
2022-06-17 15:31:07 +02:00
|
|
|
|
|
|
|
|
const float looptri_area = area_tri_v3(v0, v1, v2);
|
|
|
|
|
|
|
|
|
|
if (looptri_area < area_threshold) {
|
|
|
|
|
/* The triangle is small compared to the sample radius. Sample by generating random
|
|
|
|
|
* barycentric coordinates. */
|
|
|
|
|
const int amount = rng.round_probabilistic(approximate_density * looptri_area);
|
|
|
|
|
for ([[maybe_unused]] const int i : IndexRange(amount)) {
|
|
|
|
|
const float3 bary_coord = rng.get_barycentric_coordinates();
|
|
|
|
|
const float3 point_pos = attribute_math::mix3(bary_coord, v0, v1, v2);
|
|
|
|
|
const float dist_to_sample_sq = math::distance_squared(point_pos, sample_pos);
|
|
|
|
|
if (dist_to_sample_sq > sample_radius_sq) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r_bary_coords.append(bary_coord);
|
|
|
|
|
r_looptri_indices.append(looptri_index);
|
|
|
|
|
r_positions.append(point_pos);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
/* The triangle is large compared to the sample radius. Sample by generating random points
|
|
|
|
|
* on the triangle plane within the sample radius. */
|
|
|
|
|
float3 normal;
|
|
|
|
|
normal_tri_v3(normal, v0, v1, v2);
|
|
|
|
|
|
|
|
|
|
float3 sample_pos_proj = sample_pos;
|
|
|
|
|
project_v3_plane(sample_pos_proj, normal, v0);
|
|
|
|
|
|
|
|
|
|
const float proj_distance_sq = math::distance_squared(sample_pos_proj, sample_pos);
|
|
|
|
|
const float sample_radius_factor_sq = 1.0f -
|
|
|
|
|
std::min(1.0f, proj_distance_sq / sample_radius_sq);
|
|
|
|
|
const float radius_proj_sq = sample_radius_sq * sample_radius_factor_sq;
|
|
|
|
|
const float radius_proj = std::sqrt(radius_proj_sq);
|
2022-06-28 13:12:44 +02:00
|
|
|
const float circle_area = M_PI * radius_proj_sq;
|
2022-06-17 15:31:07 +02:00
|
|
|
|
|
|
|
|
const int amount = rng.round_probabilistic(approximate_density * circle_area);
|
|
|
|
|
|
|
|
|
|
const float3 axis_1 = math::normalize(v1 - v0) * radius_proj;
|
|
|
|
|
const float3 axis_2 = math::normalize(math::cross(axis_1, math::cross(axis_1, v2 - v0))) *
|
|
|
|
|
radius_proj;
|
|
|
|
|
|
|
|
|
|
for ([[maybe_unused]] const int i : IndexRange(amount)) {
|
|
|
|
|
const float r = std::sqrt(rng.get_float());
|
|
|
|
|
const float angle = rng.get_float() * 2.0f * M_PI;
|
|
|
|
|
const float x = r * std::cos(angle);
|
|
|
|
|
const float y = r * std::sin(angle);
|
|
|
|
|
const float3 point_pos = sample_pos_proj + axis_1 * x + axis_2 * y;
|
|
|
|
|
if (!isect_point_tri_prism_v3(point_pos, v0, v1, v2)) {
|
|
|
|
|
/* Sampled point is not in the triangle. */
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float3 bary_coord;
|
|
|
|
|
interp_weights_tri_v3(bary_coord, v0, v1, v2, point_pos);
|
|
|
|
|
|
|
|
|
|
r_bary_coords.append(bary_coord);
|
|
|
|
|
r_looptri_indices.append(looptri_index);
|
|
|
|
|
r_positions.append(point_pos);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return r_bary_coords.size() - old_num;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sample_surface_points_projected(
|
|
|
|
|
RandomNumberGenerator &rng,
|
|
|
|
|
const Mesh &mesh,
|
|
|
|
|
BVHTreeFromMesh &mesh_bvhtree,
|
|
|
|
|
const float2 &sample_pos_re,
|
|
|
|
|
const float sample_radius_re,
|
|
|
|
|
const FunctionRef<void(const float2 &pos_re, float3 &r_start, float3 &r_end)>
|
|
|
|
|
region_position_to_ray,
|
|
|
|
|
const bool front_face_only,
|
|
|
|
|
const int tries_num,
|
|
|
|
|
const int max_points,
|
|
|
|
|
Vector<float3> &r_bary_coords,
|
|
|
|
|
Vector<int> &r_looptri_indices,
|
|
|
|
|
Vector<float3> &r_positions)
|
|
|
|
|
{
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
const Span<float3> positions = mesh.vert_positions();
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
const Span<MLoop> loops = mesh.loops();
|
2022-09-24 11:41:08 +02:00
|
|
|
const Span<MLoopTri> looptris = mesh.looptris();
|
2022-06-17 15:31:07 +02:00
|
|
|
|
|
|
|
|
int point_count = 0;
|
|
|
|
|
for ([[maybe_unused]] const int _ : IndexRange(tries_num)) {
|
|
|
|
|
if (point_count == max_points) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const float r = sample_radius_re * std::sqrt(rng.get_float());
|
|
|
|
|
const float angle = rng.get_float() * 2.0f * M_PI;
|
|
|
|
|
float3 ray_start, ray_end;
|
|
|
|
|
const float2 pos_re = sample_pos_re + r * float2(std::cos(angle), std::sin(angle));
|
|
|
|
|
region_position_to_ray(pos_re, ray_start, ray_end);
|
|
|
|
|
const float3 ray_direction = math::normalize(ray_end - ray_start);
|
|
|
|
|
|
|
|
|
|
BVHTreeRayHit ray_hit;
|
|
|
|
|
ray_hit.dist = FLT_MAX;
|
|
|
|
|
ray_hit.index = -1;
|
|
|
|
|
BLI_bvhtree_ray_cast(mesh_bvhtree.tree,
|
|
|
|
|
ray_start,
|
|
|
|
|
ray_direction,
|
|
|
|
|
0.0f,
|
|
|
|
|
&ray_hit,
|
|
|
|
|
mesh_bvhtree.raycast_callback,
|
|
|
|
|
&mesh_bvhtree);
|
|
|
|
|
|
|
|
|
|
if (ray_hit.index == -1) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (front_face_only) {
|
|
|
|
|
const float3 normal = ray_hit.no;
|
|
|
|
|
if (math::dot(ray_direction, normal) >= 0.0f) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const int looptri_index = ray_hit.index;
|
|
|
|
|
const float3 pos = ray_hit.co;
|
|
|
|
|
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
const float3 bary_coords = compute_bary_coord_in_triangle(
|
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, loops, looptris[looptri_index], pos);
|
2022-06-17 15:31:07 +02:00
|
|
|
|
|
|
|
|
r_positions.append(pos);
|
|
|
|
|
r_bary_coords.append(bary_coords);
|
|
|
|
|
r_looptri_indices.append(looptri_index);
|
|
|
|
|
point_count++;
|
|
|
|
|
}
|
|
|
|
|
return point_count;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
float3 compute_bary_coord_in_triangle(const Span<float3> vert_positions,
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
const Span<MLoop> loops,
|
2022-06-17 15:31:07 +02:00
|
|
|
const MLoopTri &looptri,
|
|
|
|
|
const float3 &position)
|
|
|
|
|
{
|
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 float3 &v0 = vert_positions[loops[looptri.tri[0]].v];
|
|
|
|
|
const float3 &v1 = vert_positions[loops[looptri.tri[1]].v];
|
|
|
|
|
const float3 &v2 = vert_positions[loops[looptri.tri[2]].v];
|
2022-06-17 15:31:07 +02:00
|
|
|
float3 bary_coords;
|
|
|
|
|
interp_weights_tri_v3(bary_coords, v0, v1, v2, position);
|
|
|
|
|
return bary_coords;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-21 17:02:19 +02:00
|
|
|
} // namespace blender::bke::mesh_surface_sample
|