2023-08-15 16:20:26 +02:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2022-02-16 17:53:40 +01:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
/** \file
|
|
|
|
* \ingroup bli
|
|
|
|
*
|
|
|
|
* Generic algorithms for finding the largest and smallest elements in a span.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <optional>
|
|
|
|
|
Geometry: Cache bounds min and max, share between data-blocks
Bounding box calculation can be a large in some situations, especially
instancing. This patch caches the min and max of the bounding box in
runtime data of meshes, point clouds, and curves, implementing part of
T96968.
Bounds are now calculated lazily-- only after they are tagged dirty.
Also, cached bounds are also shared when copying geometry data-blocks
that have equivalent data. When bounds are calculated on an evaluated
data-block, they are also accessible on the original, and the next
evaluated ID will also share them. A geometry will stop sharing bounds
as soon as its positions (or radii) are changed.
Just caching the bounds gave a 2-3x speedup with thousands of mesh
geometry instances in the viewport. Sharing the bounds can eliminate
recalculations entirely in cases like copying meshes in geometry nodes
or the selection paint brush in curves sculpt mode, which causes a
reevaluation but doesn't change the positions.
**Implementation**
The sharing is achieved with a `shared_ptr` that points to a cache mutex
(from D16419) and the cached bounds data. When geometries are copied,
the bounds are shared by default, and only "un-shared" when the bounds
are tagged dirty.
Point clouds have a new runtime struct to store this data. Functions
for tagging the data dirty are improved for added for point clouds
and improved for curves. A missing tag has also been fixed for mesh
sculpt mode.
**Future**
There are further improvements which can be worked on next
- Apply changes to volume objects and other types where it makes sense
- Continue cleanup changes described in T96968
- Apply shared cache design to more expensive data like triangulation
or normals
Differential Revision: https://developer.blender.org/D16204
2022-11-15 20:46:55 +01:00
|
|
|
#include "BLI_bounds_types.hh"
|
2023-11-03 09:42:56 +01:00
|
|
|
#include "BLI_index_mask.hh"
|
2022-02-16 17:53:40 +01:00
|
|
|
#include "BLI_math_vector.hh"
|
|
|
|
#include "BLI_task.hh"
|
|
|
|
|
2024-03-05 14:32:48 +01:00
|
|
|
namespace blender {
|
|
|
|
|
|
|
|
namespace bounds {
|
2022-02-16 17:53:40 +01:00
|
|
|
|
2023-06-23 03:24:48 +02:00
|
|
|
template<typename T> [[nodiscard]] inline Bounds<T> merge(const Bounds<T> &a, const Bounds<T> &b)
|
|
|
|
{
|
|
|
|
return {math::min(a.min, b.min), math::max(a.max, b.max)};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
[[nodiscard]] inline std::optional<Bounds<T>> merge(const std::optional<Bounds<T>> &a,
|
|
|
|
const std::optional<Bounds<T>> &b)
|
|
|
|
{
|
|
|
|
if (a.has_value() && b.has_value()) {
|
|
|
|
return merge(*a, *b);
|
|
|
|
}
|
|
|
|
if (a.has_value()) {
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
if (b.has_value()) {
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2022-02-16 17:53:40 +01:00
|
|
|
/**
|
|
|
|
* Find the smallest and largest values element-wise in the span.
|
|
|
|
*/
|
2023-06-23 03:24:48 +02:00
|
|
|
template<typename T> [[nodiscard]] inline std::optional<Bounds<T>> min_max(const Span<T> values)
|
2022-02-16 17:53:40 +01:00
|
|
|
{
|
|
|
|
if (values.is_empty()) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
Geometry: Cache bounds min and max, share between data-blocks
Bounding box calculation can be a large in some situations, especially
instancing. This patch caches the min and max of the bounding box in
runtime data of meshes, point clouds, and curves, implementing part of
T96968.
Bounds are now calculated lazily-- only after they are tagged dirty.
Also, cached bounds are also shared when copying geometry data-blocks
that have equivalent data. When bounds are calculated on an evaluated
data-block, they are also accessible on the original, and the next
evaluated ID will also share them. A geometry will stop sharing bounds
as soon as its positions (or radii) are changed.
Just caching the bounds gave a 2-3x speedup with thousands of mesh
geometry instances in the viewport. Sharing the bounds can eliminate
recalculations entirely in cases like copying meshes in geometry nodes
or the selection paint brush in curves sculpt mode, which causes a
reevaluation but doesn't change the positions.
**Implementation**
The sharing is achieved with a `shared_ptr` that points to a cache mutex
(from D16419) and the cached bounds data. When geometries are copied,
the bounds are shared by default, and only "un-shared" when the bounds
are tagged dirty.
Point clouds have a new runtime struct to store this data. Functions
for tagging the data dirty are improved for added for point clouds
and improved for curves. A missing tag has also been fixed for mesh
sculpt mode.
**Future**
There are further improvements which can be worked on next
- Apply changes to volume objects and other types where it makes sense
- Continue cleanup changes described in T96968
- Apply shared cache design to more expensive data like triangulation
or normals
Differential Revision: https://developer.blender.org/D16204
2022-11-15 20:46:55 +01:00
|
|
|
const Bounds<T> init{values.first(), values.first()};
|
2022-02-16 17:53:40 +01:00
|
|
|
return threading::parallel_reduce(
|
|
|
|
values.index_range(),
|
|
|
|
1024,
|
2022-06-08 18:40:08 +02:00
|
|
|
init,
|
2023-06-23 03:24:48 +02:00
|
|
|
[&](const IndexRange range, const Bounds<T> &init) {
|
Geometry: Cache bounds min and max, share between data-blocks
Bounding box calculation can be a large in some situations, especially
instancing. This patch caches the min and max of the bounding box in
runtime data of meshes, point clouds, and curves, implementing part of
T96968.
Bounds are now calculated lazily-- only after they are tagged dirty.
Also, cached bounds are also shared when copying geometry data-blocks
that have equivalent data. When bounds are calculated on an evaluated
data-block, they are also accessible on the original, and the next
evaluated ID will also share them. A geometry will stop sharing bounds
as soon as its positions (or radii) are changed.
Just caching the bounds gave a 2-3x speedup with thousands of mesh
geometry instances in the viewport. Sharing the bounds can eliminate
recalculations entirely in cases like copying meshes in geometry nodes
or the selection paint brush in curves sculpt mode, which causes a
reevaluation but doesn't change the positions.
**Implementation**
The sharing is achieved with a `shared_ptr` that points to a cache mutex
(from D16419) and the cached bounds data. When geometries are copied,
the bounds are shared by default, and only "un-shared" when the bounds
are tagged dirty.
Point clouds have a new runtime struct to store this data. Functions
for tagging the data dirty are improved for added for point clouds
and improved for curves. A missing tag has also been fixed for mesh
sculpt mode.
**Future**
There are further improvements which can be worked on next
- Apply changes to volume objects and other types where it makes sense
- Continue cleanup changes described in T96968
- Apply shared cache design to more expensive data like triangulation
or normals
Differential Revision: https://developer.blender.org/D16204
2022-11-15 20:46:55 +01:00
|
|
|
Bounds<T> result = init;
|
2022-02-16 17:53:40 +01:00
|
|
|
for (const int i : range) {
|
|
|
|
math::min_max(values[i], result.min, result.max);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
},
|
2023-06-23 03:24:48 +02:00
|
|
|
[](const Bounds<T> &a, const Bounds<T> &b) { return merge(a, b); });
|
2022-02-16 17:53:40 +01:00
|
|
|
}
|
|
|
|
|
2023-11-03 09:42:56 +01:00
|
|
|
template<typename T>
|
|
|
|
[[nodiscard]] inline std::optional<Bounds<T>> min_max(const IndexMask &mask, const Span<T> values)
|
|
|
|
{
|
|
|
|
if (values.is_empty() || mask.is_empty()) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
if (mask.size() == values.size()) {
|
|
|
|
/* To avoid mask slice/lookup. */
|
|
|
|
return min_max(values);
|
|
|
|
}
|
|
|
|
const Bounds<T> init{values.first(), values.first()};
|
|
|
|
return threading::parallel_reduce(
|
|
|
|
mask.index_range(),
|
|
|
|
1024,
|
|
|
|
init,
|
|
|
|
[&](const IndexRange range, const Bounds<T> &init) {
|
|
|
|
Bounds<T> result = init;
|
|
|
|
mask.slice(range).foreach_index_optimized<int64_t>(
|
|
|
|
[&](const int i) { math::min_max(values[i], result.min, result.max); });
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
[](const Bounds<T> &a, const Bounds<T> &b) { return merge(a, b); });
|
|
|
|
}
|
|
|
|
|
2022-02-16 17:53:40 +01:00
|
|
|
/**
|
|
|
|
* Find the smallest and largest values element-wise in the span, adding the radius to each element
|
|
|
|
* first. The template type T is expected to have an addition operator implemented with RadiusT.
|
|
|
|
*/
|
|
|
|
template<typename T, typename RadiusT>
|
2023-06-23 03:24:48 +02:00
|
|
|
[[nodiscard]] inline std::optional<Bounds<T>> min_max_with_radii(const Span<T> values,
|
|
|
|
const Span<RadiusT> radii)
|
2022-02-16 17:53:40 +01:00
|
|
|
{
|
|
|
|
BLI_assert(values.size() == radii.size());
|
|
|
|
if (values.is_empty()) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
Geometry: Cache bounds min and max, share between data-blocks
Bounding box calculation can be a large in some situations, especially
instancing. This patch caches the min and max of the bounding box in
runtime data of meshes, point clouds, and curves, implementing part of
T96968.
Bounds are now calculated lazily-- only after they are tagged dirty.
Also, cached bounds are also shared when copying geometry data-blocks
that have equivalent data. When bounds are calculated on an evaluated
data-block, they are also accessible on the original, and the next
evaluated ID will also share them. A geometry will stop sharing bounds
as soon as its positions (or radii) are changed.
Just caching the bounds gave a 2-3x speedup with thousands of mesh
geometry instances in the viewport. Sharing the bounds can eliminate
recalculations entirely in cases like copying meshes in geometry nodes
or the selection paint brush in curves sculpt mode, which causes a
reevaluation but doesn't change the positions.
**Implementation**
The sharing is achieved with a `shared_ptr` that points to a cache mutex
(from D16419) and the cached bounds data. When geometries are copied,
the bounds are shared by default, and only "un-shared" when the bounds
are tagged dirty.
Point clouds have a new runtime struct to store this data. Functions
for tagging the data dirty are improved for added for point clouds
and improved for curves. A missing tag has also been fixed for mesh
sculpt mode.
**Future**
There are further improvements which can be worked on next
- Apply changes to volume objects and other types where it makes sense
- Continue cleanup changes described in T96968
- Apply shared cache design to more expensive data like triangulation
or normals
Differential Revision: https://developer.blender.org/D16204
2022-11-15 20:46:55 +01:00
|
|
|
const Bounds<T> init{values.first(), values.first()};
|
2022-02-16 17:53:40 +01:00
|
|
|
return threading::parallel_reduce(
|
|
|
|
values.index_range(),
|
|
|
|
1024,
|
2022-06-08 18:40:08 +02:00
|
|
|
init,
|
2023-06-23 03:24:48 +02:00
|
|
|
[&](const IndexRange range, const Bounds<T> &init) {
|
Geometry: Cache bounds min and max, share between data-blocks
Bounding box calculation can be a large in some situations, especially
instancing. This patch caches the min and max of the bounding box in
runtime data of meshes, point clouds, and curves, implementing part of
T96968.
Bounds are now calculated lazily-- only after they are tagged dirty.
Also, cached bounds are also shared when copying geometry data-blocks
that have equivalent data. When bounds are calculated on an evaluated
data-block, they are also accessible on the original, and the next
evaluated ID will also share them. A geometry will stop sharing bounds
as soon as its positions (or radii) are changed.
Just caching the bounds gave a 2-3x speedup with thousands of mesh
geometry instances in the viewport. Sharing the bounds can eliminate
recalculations entirely in cases like copying meshes in geometry nodes
or the selection paint brush in curves sculpt mode, which causes a
reevaluation but doesn't change the positions.
**Implementation**
The sharing is achieved with a `shared_ptr` that points to a cache mutex
(from D16419) and the cached bounds data. When geometries are copied,
the bounds are shared by default, and only "un-shared" when the bounds
are tagged dirty.
Point clouds have a new runtime struct to store this data. Functions
for tagging the data dirty are improved for added for point clouds
and improved for curves. A missing tag has also been fixed for mesh
sculpt mode.
**Future**
There are further improvements which can be worked on next
- Apply changes to volume objects and other types where it makes sense
- Continue cleanup changes described in T96968
- Apply shared cache design to more expensive data like triangulation
or normals
Differential Revision: https://developer.blender.org/D16204
2022-11-15 20:46:55 +01:00
|
|
|
Bounds<T> result = init;
|
2022-02-16 17:53:40 +01:00
|
|
|
for (const int i : range) {
|
|
|
|
result.min = math::min(values[i] - radii[i], result.min);
|
|
|
|
result.max = math::max(values[i] + radii[i], result.max);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
},
|
2023-06-23 03:24:48 +02:00
|
|
|
[](const Bounds<T> &a, const Bounds<T> &b) { return merge(a, b); });
|
2023-06-16 14:08:18 +02:00
|
|
|
}
|
|
|
|
|
2024-03-05 14:32:48 +01:00
|
|
|
} // namespace bounds
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
template<typename T, int Size>
|
|
|
|
[[nodiscard]] inline bool less_or_equal_than(const VecBase<T, Size> &a, const VecBase<T, Size> &b)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < Size; i++) {
|
2024-03-29 17:12:51 +01:00
|
|
|
if (a[i] <= b[i]) {
|
|
|
|
return true;
|
2024-03-05 14:32:48 +01:00
|
|
|
}
|
|
|
|
}
|
2024-03-29 17:12:51 +01:00
|
|
|
return false;
|
2024-03-05 14:32:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
template<typename T> inline bool Bounds<T>::is_empty() const
|
|
|
|
{
|
|
|
|
if constexpr (std::is_integral<T>::value || std::is_floating_point<T>::value) {
|
|
|
|
return this->max <= this->min;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return detail::less_or_equal_than(this->max, this->min);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T> inline T Bounds<T>::center() const
|
|
|
|
{
|
|
|
|
return math::midpoint(this->min, this->max);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T> inline T Bounds<T>::size() const
|
|
|
|
{
|
|
|
|
return math::abs(max - min);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T> inline void Bounds<T>::translate(const T &offset)
|
|
|
|
{
|
|
|
|
this->min += offset;
|
|
|
|
this->max += offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T> inline void Bounds<T>::scale_from_center(const T &scale)
|
|
|
|
{
|
|
|
|
const T center = this->center();
|
|
|
|
const T new_half_size = this->size() / T(2) * scale;
|
|
|
|
this->min = center - new_half_size;
|
|
|
|
this->max = center + new_half_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T> inline void Bounds<T>::resize(const T &new_size)
|
|
|
|
{
|
|
|
|
this->min = this->center() - (new_size / T(2));
|
|
|
|
this->max = this->min + new_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T> inline void Bounds<T>::recenter(const T &new_center)
|
|
|
|
{
|
|
|
|
const T offset = new_center - this->center();
|
|
|
|
this->translate(offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
template<typename PaddingT>
|
|
|
|
inline void Bounds<T>::pad(const PaddingT &padding)
|
|
|
|
{
|
|
|
|
this->min = this->min - padding;
|
|
|
|
this->max = this->max + padding;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace blender
|