Goals of this refactor: * Simplify creating virtual arrays. * Simplify passing virtual arrays around. * Simplify converting between typed and generic virtual arrays. * Reduce memory allocations. As a quick reminder, a virtual arrays is a data structure that behaves like an array (i.e. it can be accessed using an index). However, it may not actually be stored as array internally. The two most important implementations of virtual arrays are those that correspond to an actual plain array and those that have the same value for every index. However, many more implementations exist for various reasons (interfacing with legacy attributes, unified iterator over all points in multiple splines, ...). With this refactor the core types (`VArray`, `GVArray`, `VMutableArray` and `GVMutableArray`) can be used like "normal values". They typically live on the stack. Before, they were usually inside a `std::unique_ptr`. This makes passing them around much easier. Creation of new virtual arrays is also much simpler now due to some constructors. Memory allocations are reduced by making use of small object optimization inside the core types. Previously, `VArray` was a class with virtual methods that had to be overridden to change the behavior of a the virtual array. Now,`VArray` has a fixed size and has no virtual methods. Instead it contains a `VArrayImpl` that is similar to the old `VArray`. `VArrayImpl` should rarely ever be used directly, unless a new virtual array implementation is added. To support the small object optimization for many `VArrayImpl` classes, a new `blender::Any` type is added. It is similar to `std::any` with two additional features. It has an adjustable inline buffer size and alignment. The inline buffer size of `std::any` can't be relied on and is usually too small for our use case here. Furthermore, `blender::Any` can store additional user-defined type information without increasing the stack size. Differential Revision: https://developer.blender.org/D12986
129 lines
3.0 KiB
C++
129 lines
3.0 KiB
C++
/*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation; either version 2
|
|
* of the License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
*/
|
|
|
|
#include "BLI_span.hh"
|
|
#include "BLI_virtual_array.hh"
|
|
|
|
#include "BKE_spline.hh"
|
|
|
|
using blender::float3;
|
|
using blender::MutableSpan;
|
|
using blender::Span;
|
|
using blender::fn::GVArray;
|
|
|
|
void PolySpline::copy_settings(Spline &UNUSED(dst)) const
|
|
{
|
|
/* Poly splines have no settings not covered by the base class. */
|
|
}
|
|
|
|
void PolySpline::copy_data(Spline &dst) const
|
|
{
|
|
PolySpline &poly = static_cast<PolySpline &>(dst);
|
|
poly.positions_ = positions_;
|
|
poly.radii_ = radii_;
|
|
poly.tilts_ = tilts_;
|
|
}
|
|
|
|
int PolySpline::size() const
|
|
{
|
|
const int size = positions_.size();
|
|
BLI_assert(size == radii_.size());
|
|
BLI_assert(size == tilts_.size());
|
|
return size;
|
|
}
|
|
|
|
/**
|
|
* \warning Call #reallocate on the spline's attributes after adding all points.
|
|
*/
|
|
void PolySpline::add_point(const float3 position, const float radius, const float tilt)
|
|
{
|
|
positions_.append(position);
|
|
radii_.append(radius);
|
|
tilts_.append(tilt);
|
|
this->mark_cache_invalid();
|
|
}
|
|
|
|
void PolySpline::resize(const int size)
|
|
{
|
|
positions_.resize(size);
|
|
radii_.resize(size);
|
|
tilts_.resize(size);
|
|
this->mark_cache_invalid();
|
|
attributes.reallocate(size);
|
|
}
|
|
|
|
MutableSpan<float3> PolySpline::positions()
|
|
{
|
|
return positions_;
|
|
}
|
|
Span<float3> PolySpline::positions() const
|
|
{
|
|
return positions_;
|
|
}
|
|
MutableSpan<float> PolySpline::radii()
|
|
{
|
|
return radii_;
|
|
}
|
|
Span<float> PolySpline::radii() const
|
|
{
|
|
return radii_;
|
|
}
|
|
MutableSpan<float> PolySpline::tilts()
|
|
{
|
|
return tilts_;
|
|
}
|
|
Span<float> PolySpline::tilts() const
|
|
{
|
|
return tilts_;
|
|
}
|
|
|
|
void PolySpline::reverse_impl()
|
|
{
|
|
}
|
|
|
|
void PolySpline::mark_cache_invalid()
|
|
{
|
|
tangent_cache_dirty_ = true;
|
|
normal_cache_dirty_ = true;
|
|
length_cache_dirty_ = true;
|
|
}
|
|
|
|
int PolySpline::evaluated_points_size() const
|
|
{
|
|
return this->size();
|
|
}
|
|
|
|
void PolySpline::correct_end_tangents() const
|
|
{
|
|
}
|
|
|
|
Span<float3> PolySpline::evaluated_positions() const
|
|
{
|
|
return this->positions();
|
|
}
|
|
|
|
/**
|
|
* Poly spline interpolation from control points to evaluated points is a special case, since
|
|
* the result data is the same as the input data. This function returns a GVArray that points to
|
|
* the original data. Therefore the lifetime of the returned virtual array must not be longer than
|
|
* the source data.
|
|
*/
|
|
GVArray PolySpline::interpolate_to_evaluated(const GVArray &src) const
|
|
{
|
|
BLI_assert(src.size() == this->size());
|
|
return src;
|
|
}
|