2021-01-15 12:00:30 +01:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
Geometry Nodes: Initial basic curve data support
This patch adds initial curve support to geometry nodes. Currently
there is only one node available, the "Curve to Mesh" node, T87428.
However, the aim of the changes here is larger than just supporting
curve data in nodes-- it also uses the opportunity to add better spline
data structures, intended to replace the existing curve evaluation code.
The curve code in Blender is quite old, and it's generally regarded as
some of the messiest, hardest-to-understand code as well. The classes
in `BKE_spline.hh` aim to be faster, more extensible, and much more
easily understandable. Further explanation can be found in comments in
that file.
Initial builtin spline attributes are supported-- reading and writing
from the `cyclic` and `resolution` attributes works with any of the
attribute nodes. Also, only Z-up normal calculation is implemented
at the moment, and tilts do not apply yet.
**Limitations**
- For now, you must bring curves into the node tree with an "Object
Info" node. Changes to the curve modifier stack will come later.
- Converting to a mesh is necessary to visualize the curve data.
Further progress can be tracked in: T87245
Higher level design document: https://wiki.blender.org/wiki/Modules/Physics_Nodes/Projects/EverythingNodes/CurveNodes
Differential Revision: https://developer.blender.org/D11091
2021-05-03 19:29:17 +02:00
|
|
|
#pragma once
|
|
|
|
|
2021-02-09 11:44:58 +01:00
|
|
|
#include "BLI_array.hh"
|
2021-01-15 12:00:30 +01:00
|
|
|
#include "BLI_color.hh"
|
|
|
|
#include "BLI_float2.hh"
|
|
|
|
#include "BLI_float3.hh"
|
2021-02-09 11:44:58 +01:00
|
|
|
|
2021-01-15 12:00:30 +01:00
|
|
|
#include "DNA_customdata_types.h"
|
|
|
|
|
2021-04-21 16:57:43 +02:00
|
|
|
#include "FN_cpp_type.hh"
|
|
|
|
|
2021-01-15 12:00:30 +01:00
|
|
|
namespace blender::attribute_math {
|
|
|
|
|
2021-04-21 16:57:43 +02:00
|
|
|
using fn::CPPType;
|
|
|
|
|
2021-01-15 12:00:30 +01:00
|
|
|
/**
|
|
|
|
* Utility function that simplifies calling a templated function based on a custom data type.
|
|
|
|
*/
|
|
|
|
template<typename Func>
|
2021-04-21 17:02:19 +02:00
|
|
|
inline void convert_to_static_type(const CustomDataType data_type, const Func &func)
|
2021-01-15 12:00:30 +01:00
|
|
|
{
|
|
|
|
switch (data_type) {
|
|
|
|
case CD_PROP_FLOAT:
|
|
|
|
func(float());
|
|
|
|
break;
|
|
|
|
case CD_PROP_FLOAT2:
|
|
|
|
func(float2());
|
|
|
|
break;
|
|
|
|
case CD_PROP_FLOAT3:
|
|
|
|
func(float3());
|
|
|
|
break;
|
|
|
|
case CD_PROP_INT32:
|
|
|
|
func(int());
|
|
|
|
break;
|
|
|
|
case CD_PROP_BOOL:
|
|
|
|
func(bool());
|
|
|
|
break;
|
|
|
|
case CD_PROP_COLOR:
|
2021-05-25 17:00:14 +02:00
|
|
|
func(ColorGeometry4f());
|
2021-01-15 12:00:30 +01:00
|
|
|
break;
|
|
|
|
default:
|
2021-03-23 16:49:47 +01:00
|
|
|
BLI_assert_unreachable();
|
2021-01-15 12:00:30 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-21 17:02:19 +02:00
|
|
|
template<typename Func>
|
|
|
|
inline void convert_to_static_type(const fn::CPPType &cpp_type, const Func &func)
|
2021-04-21 16:57:43 +02:00
|
|
|
{
|
|
|
|
if (cpp_type.is<float>()) {
|
|
|
|
func(float());
|
|
|
|
}
|
|
|
|
else if (cpp_type.is<float2>()) {
|
|
|
|
func(float2());
|
|
|
|
}
|
|
|
|
else if (cpp_type.is<float3>()) {
|
|
|
|
func(float3());
|
|
|
|
}
|
|
|
|
else if (cpp_type.is<int>()) {
|
|
|
|
func(int());
|
|
|
|
}
|
|
|
|
else if (cpp_type.is<bool>()) {
|
|
|
|
func(bool());
|
|
|
|
}
|
2021-05-25 17:00:14 +02:00
|
|
|
else if (cpp_type.is<ColorGeometry4f>()) {
|
|
|
|
func(ColorGeometry4f());
|
2021-04-21 16:57:43 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
BLI_assert_unreachable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-09 11:44:58 +01:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/** \name Mix three values of the same type.
|
|
|
|
*
|
|
|
|
* This is typically used to interpolate values within a triangle.
|
|
|
|
* \{ */
|
|
|
|
|
2021-01-15 12:00:30 +01:00
|
|
|
template<typename T> T mix3(const float3 &weights, const T &v0, const T &v1, const T &v2);
|
|
|
|
|
|
|
|
template<> inline bool mix3(const float3 &weights, const bool &v0, const bool &v1, const bool &v2)
|
|
|
|
{
|
|
|
|
return (weights.x * v0 + weights.y * v1 + weights.z * v2) >= 0.5f;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> inline int mix3(const float3 &weights, const int &v0, const int &v1, const int &v2)
|
|
|
|
{
|
|
|
|
return static_cast<int>(weights.x * v0 + weights.y * v1 + weights.z * v2);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
inline float mix3(const float3 &weights, const float &v0, const float &v1, const float &v2)
|
|
|
|
{
|
|
|
|
return weights.x * v0 + weights.y * v1 + weights.z * v2;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
inline float2 mix3(const float3 &weights, const float2 &v0, const float2 &v1, const float2 &v2)
|
|
|
|
{
|
|
|
|
return weights.x * v0 + weights.y * v1 + weights.z * v2;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
inline float3 mix3(const float3 &weights, const float3 &v0, const float3 &v1, const float3 &v2)
|
|
|
|
{
|
|
|
|
return weights.x * v0 + weights.y * v1 + weights.z * v2;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2021-05-25 17:00:14 +02:00
|
|
|
inline ColorGeometry4f mix3(const float3 &weights,
|
|
|
|
const ColorGeometry4f &v0,
|
|
|
|
const ColorGeometry4f &v1,
|
|
|
|
const ColorGeometry4f &v2)
|
2021-01-15 12:00:30 +01:00
|
|
|
{
|
2021-05-25 17:00:14 +02:00
|
|
|
ColorGeometry4f result;
|
2021-01-15 12:00:30 +01:00
|
|
|
interp_v4_v4v4v4(result, v0, v1, v2, weights);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-02-09 11:44:58 +01:00
|
|
|
/** \} */
|
|
|
|
|
2021-04-30 04:52:34 +02:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/** \name Mix two values of the same type.
|
|
|
|
*
|
|
|
|
* This is just basic linear interpolation.
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
template<typename T> T mix2(const float factor, const T &a, const T &b);
|
|
|
|
|
|
|
|
template<> inline bool mix2(const float factor, const bool &a, const bool &b)
|
|
|
|
{
|
|
|
|
return ((1.0f - factor) * a + factor * b) >= 0.5f;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> inline int mix2(const float factor, const int &a, const int &b)
|
|
|
|
{
|
|
|
|
return static_cast<int>((1.0f - factor) * a + factor * b);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> inline float mix2(const float factor, const float &a, const float &b)
|
|
|
|
{
|
|
|
|
return (1.0f - factor) * a + factor * b;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> inline float2 mix2(const float factor, const float2 &a, const float2 &b)
|
|
|
|
{
|
|
|
|
return float2::interpolate(a, b, factor);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> inline float3 mix2(const float factor, const float3 &a, const float3 &b)
|
|
|
|
{
|
|
|
|
return float3::interpolate(a, b, factor);
|
|
|
|
}
|
|
|
|
|
2021-05-25 17:00:14 +02:00
|
|
|
template<>
|
|
|
|
inline ColorGeometry4f mix2(const float factor, const ColorGeometry4f &a, const ColorGeometry4f &b)
|
2021-04-30 04:52:34 +02:00
|
|
|
{
|
2021-05-25 17:00:14 +02:00
|
|
|
ColorGeometry4f result;
|
2021-04-30 04:52:34 +02:00
|
|
|
interp_v4_v4v4(result, a, b, factor);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** \} */
|
|
|
|
|
2021-02-09 11:44:58 +01:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/** \name Mix a dynamic amount of values with weights for many elements.
|
|
|
|
*
|
|
|
|
* This section provides an abstraction for "mixers". The abstraction encapsulates details about
|
|
|
|
* how different types should be mixed. Usually #DefaultMixer<T> should be used to get a mixer for
|
|
|
|
* a specific type.
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
template<typename T> class SimpleMixer {
|
|
|
|
private:
|
|
|
|
MutableSpan<T> buffer_;
|
|
|
|
T default_value_;
|
|
|
|
Array<float> total_weights_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* \param buffer: Span where the interpolated values should be stored.
|
|
|
|
* \param default_value: Output value for an element that has not been affected by a #mix_in.
|
|
|
|
*/
|
|
|
|
SimpleMixer(MutableSpan<T> buffer, T default_value = {})
|
|
|
|
: buffer_(buffer), default_value_(default_value), total_weights_(buffer.size(), 0.0f)
|
|
|
|
{
|
|
|
|
BLI_STATIC_ASSERT(std::is_trivial_v<T>, "");
|
|
|
|
memset(buffer_.data(), 0, sizeof(T) * buffer_.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mix a #value into the element with the given #index.
|
|
|
|
*/
|
|
|
|
void mix_in(const int64_t index, const T &value, const float weight = 1.0f)
|
|
|
|
{
|
|
|
|
BLI_assert(weight >= 0.0f);
|
|
|
|
buffer_[index] += value * weight;
|
|
|
|
total_weights_[index] += weight;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Has to be called before the buffer provided in the constructor is used.
|
|
|
|
*/
|
|
|
|
void finalize()
|
|
|
|
{
|
|
|
|
for (const int64_t i : buffer_.index_range()) {
|
|
|
|
const float weight = total_weights_[i];
|
|
|
|
if (weight > 0.0f) {
|
|
|
|
buffer_[i] *= 1.0f / weight;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
buffer_[i] = default_value_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-05-12 13:55:25 +02:00
|
|
|
/**
|
|
|
|
* This mixer accumulates values in a type that is different from the one that is mixed.
|
|
|
|
* Some types cannot encode the floating point weights in their values (e.g. int and bool).
|
|
|
|
*/
|
2021-02-09 11:44:58 +01:00
|
|
|
template<typename T, typename AccumulationT, T (*ConvertToT)(const AccumulationT &value)>
|
|
|
|
class SimpleMixerWithAccumulationType {
|
|
|
|
private:
|
|
|
|
struct Item {
|
|
|
|
/* Store both values together, because they are accessed together. */
|
|
|
|
AccumulationT value = {0};
|
|
|
|
float weight = 0.0f;
|
|
|
|
};
|
|
|
|
|
|
|
|
MutableSpan<T> buffer_;
|
|
|
|
T default_value_;
|
|
|
|
Array<Item> accumulation_buffer_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
SimpleMixerWithAccumulationType(MutableSpan<T> buffer, T default_value = {})
|
|
|
|
: buffer_(buffer), default_value_(default_value), accumulation_buffer_(buffer.size())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void mix_in(const int64_t index, const T &value, const float weight = 1.0f)
|
|
|
|
{
|
|
|
|
const AccumulationT converted_value = static_cast<AccumulationT>(value);
|
|
|
|
Item &item = accumulation_buffer_[index];
|
|
|
|
item.value += converted_value * weight;
|
|
|
|
item.weight += weight;
|
|
|
|
}
|
|
|
|
|
|
|
|
void finalize()
|
|
|
|
{
|
|
|
|
for (const int64_t i : buffer_.index_range()) {
|
|
|
|
const Item &item = accumulation_buffer_[i];
|
|
|
|
if (item.weight > 0.0f) {
|
|
|
|
const float weight_inv = 1.0f / item.weight;
|
|
|
|
const T converted_value = ConvertToT(item.value * weight_inv);
|
|
|
|
buffer_[i] = converted_value;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
buffer_[i] = default_value_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-05-25 17:00:14 +02:00
|
|
|
class ColorGeometryMixer {
|
2021-02-09 11:44:58 +01:00
|
|
|
private:
|
2021-05-25 17:00:14 +02:00
|
|
|
MutableSpan<ColorGeometry4f> buffer_;
|
|
|
|
ColorGeometry4f default_color_;
|
2021-02-09 11:44:58 +01:00
|
|
|
Array<float> total_weights_;
|
|
|
|
|
|
|
|
public:
|
2021-05-25 17:00:14 +02:00
|
|
|
ColorGeometryMixer(MutableSpan<ColorGeometry4f> buffer,
|
|
|
|
ColorGeometry4f default_color = ColorGeometry4f(0.0f, 0.0f, 0.0f, 1.0f));
|
|
|
|
void mix_in(const int64_t index, const ColorGeometry4f &color, const float weight = 1.0f);
|
2021-02-09 11:44:58 +01:00
|
|
|
void finalize();
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T> struct DefaultMixerStruct {
|
|
|
|
/* Use void by default. This can be check for in `if constexpr` statements. */
|
|
|
|
using type = void;
|
|
|
|
};
|
|
|
|
template<> struct DefaultMixerStruct<float> {
|
|
|
|
using type = SimpleMixer<float>;
|
|
|
|
};
|
|
|
|
template<> struct DefaultMixerStruct<float2> {
|
|
|
|
using type = SimpleMixer<float2>;
|
|
|
|
};
|
|
|
|
template<> struct DefaultMixerStruct<float3> {
|
|
|
|
using type = SimpleMixer<float3>;
|
|
|
|
};
|
2021-05-25 17:00:14 +02:00
|
|
|
template<> struct DefaultMixerStruct<ColorGeometry4f> {
|
|
|
|
/* Use a special mixer for colors. ColorGeometry4f can't be added/multiplied, because this is not
|
2021-02-09 11:44:58 +01:00
|
|
|
* something one should usually do with colors. */
|
2021-05-25 17:00:14 +02:00
|
|
|
using type = ColorGeometryMixer;
|
2021-02-09 11:44:58 +01:00
|
|
|
};
|
|
|
|
template<> struct DefaultMixerStruct<int> {
|
|
|
|
static int double_to_int(const double &value)
|
|
|
|
{
|
|
|
|
return static_cast<int>(value);
|
|
|
|
}
|
|
|
|
/* Store interpolated ints in a double temporarily, so that weights are handled correctly. It
|
|
|
|
* uses double instead of float so that it is accurate for all 32 bit integers. */
|
|
|
|
using type = SimpleMixerWithAccumulationType<int, double, double_to_int>;
|
|
|
|
};
|
|
|
|
template<> struct DefaultMixerStruct<bool> {
|
|
|
|
static bool float_to_bool(const float &value)
|
|
|
|
{
|
|
|
|
return value >= 0.5f;
|
|
|
|
}
|
2021-02-09 21:57:52 +01:00
|
|
|
/* Store interpolated booleans in a float temporary.
|
|
|
|
* Otherwise information provided by weights is easily rounded away. */
|
2021-02-09 11:44:58 +01:00
|
|
|
using type = SimpleMixerWithAccumulationType<bool, float, float_to_bool>;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Utility to get a good default mixer for a given type. This is `void` when there is no default
|
|
|
|
* mixer for the given type. */
|
|
|
|
template<typename T> using DefaultMixer = typename DefaultMixerStruct<T>::type;
|
|
|
|
|
|
|
|
/** \} */
|
|
|
|
|
2021-01-15 12:00:30 +01:00
|
|
|
} // namespace blender::attribute_math
|