2022-02-10 23:07:11 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2021-01-15 12:00:30 +01:00
|
|
|
|
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"
|
2022-03-18 10:57:45 +01:00
|
|
|
#include "BLI_cpp_type.hh"
|
2022-04-21 16:11:26 +02:00
|
|
|
#include "BLI_math_color.hh"
|
2022-02-15 17:27:03 +01:00
|
|
|
#include "BLI_math_vector.h"
|
|
|
|
#include "BLI_math_vector.hh"
|
2021-02-09 11:44:58 +01:00
|
|
|
|
2022-03-19 10:57:40 +01:00
|
|
|
#include "BKE_customdata.h"
|
2021-01-15 12:00:30 +01:00
|
|
|
|
|
|
|
namespace blender::attribute_math {
|
|
|
|
|
|
|
|
/**
|
2022-03-19 10:57:40 +01:00
|
|
|
* Utility function that simplifies calling a templated function based on a run-time data type.
|
2021-01-15 12:00:30 +01:00
|
|
|
*/
|
|
|
|
template<typename Func>
|
2022-03-19 10:57:40 +01:00
|
|
|
inline void convert_to_static_type(const CPPType &cpp_type, const Func &func)
|
2021-01-15 12:00:30 +01:00
|
|
|
{
|
2022-04-21 16:11:26 +02:00
|
|
|
cpp_type.to_static_type_tag<float,
|
|
|
|
float2,
|
|
|
|
float3,
|
|
|
|
int,
|
|
|
|
bool,
|
|
|
|
int8_t,
|
|
|
|
ColorGeometry4f,
|
|
|
|
ColorGeometry4b>([&](auto type_tag) {
|
|
|
|
using T = typename decltype(type_tag)::type;
|
|
|
|
if constexpr (std::is_same_v<T, void>) {
|
|
|
|
/* It's expected that the given cpp type is one of the supported ones. */
|
|
|
|
BLI_assert_unreachable();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
func(T());
|
|
|
|
}
|
|
|
|
});
|
2021-01-15 12:00:30 +01:00
|
|
|
}
|
|
|
|
|
2021-04-21 17:02:19 +02:00
|
|
|
template<typename Func>
|
2022-03-19 10:57:40 +01:00
|
|
|
inline void convert_to_static_type(const CustomDataType data_type, const Func &func)
|
2021-04-21 16:57:43 +02:00
|
|
|
{
|
2022-03-19 10:57:40 +01:00
|
|
|
const CPPType &cpp_type = *bke::custom_data_type_to_cpp_type(data_type);
|
|
|
|
convert_to_static_type(cpp_type, func);
|
2021-04-21 16:57:43 +02:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2022-02-04 17:29:11 +01:00
|
|
|
template<>
|
|
|
|
inline int8_t mix3(const float3 &weights, const int8_t &v0, const int8_t &v1, const int8_t &v2)
|
|
|
|
{
|
|
|
|
return static_cast<int8_t>(weights.x * v0 + weights.y * v1 + weights.z * v2);
|
|
|
|
}
|
|
|
|
|
2021-01-15 12:00:30 +01:00
|
|
|
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:16:35 +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:16:35 +02:00
|
|
|
ColorGeometry4f result;
|
2021-01-15 12:00:30 +01:00
|
|
|
interp_v4_v4v4v4(result, v0, v1, v2, weights);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-04-21 16:11:26 +02:00
|
|
|
template<>
|
|
|
|
inline ColorGeometry4b mix3(const float3 &weights,
|
|
|
|
const ColorGeometry4b &v0,
|
|
|
|
const ColorGeometry4b &v1,
|
|
|
|
const ColorGeometry4b &v2)
|
|
|
|
{
|
|
|
|
const float4 v0_f{&v0.r};
|
|
|
|
const float4 v1_f{&v1.r};
|
|
|
|
const float4 v2_f{&v2.r};
|
|
|
|
const float4 mixed = v0_f * weights[0] + v1_f * weights[1] + v2_f * weights[2];
|
|
|
|
return ColorGeometry4b{static_cast<uint8_t>(mixed[0]),
|
|
|
|
static_cast<uint8_t>(mixed[1]),
|
|
|
|
static_cast<uint8_t>(mixed[2]),
|
|
|
|
static_cast<uint8_t>(mixed[3])};
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
* \{ */
|
|
|
|
|
2022-01-07 01:38:08 +01:00
|
|
|
template<typename T> T mix2(float factor, const T &a, const T &b);
|
2021-04-30 04:52:34 +02:00
|
|
|
|
|
|
|
template<> inline bool mix2(const float factor, const bool &a, const bool &b)
|
|
|
|
{
|
|
|
|
return ((1.0f - factor) * a + factor * b) >= 0.5f;
|
|
|
|
}
|
|
|
|
|
2022-02-04 17:29:11 +01:00
|
|
|
template<> inline int8_t mix2(const float factor, const int8_t &a, const int8_t &b)
|
|
|
|
{
|
|
|
|
return static_cast<int8_t>((1.0f - factor) * a + factor * b);
|
|
|
|
}
|
|
|
|
|
2021-04-30 04:52:34 +02:00
|
|
|
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)
|
|
|
|
{
|
BLI: Refactor vector types & functions to use templates
This patch implements the vector types (i.e:`float2`) by making heavy
usage of templating. All vector functions are now outside of the vector
classes (inside the `blender::math` namespace) and are not vector size
dependent for the most part.
In the ongoing effort to make shaders less GL centric, we are aiming
to share more code between GLSL and C++ to avoid code duplication.
####Motivations:
- We are aiming to share UBO and SSBO structures between GLSL and C++.
This means we will use many of the existing vector types and others
we currently don't have (uintX, intX). All these variations were
asking for many more code duplication.
- Deduplicate existing code which is duplicated for each vector size.
- We also want to share small functions. Which means that vector
functions should be static and not in the class namespace.
- Reduce friction to use these types in new projects due to their
incompleteness.
- The current state of the `BLI_(float|double|mpq)(2|3|4).hh` is a
bit of a let down. Most clases are incomplete, out of sync with each
others with different codestyles, and some functions that should be
static are not (i.e: `float3::reflect()`).
####Upsides:
- Still support `.x, .y, .z, .w` for readability.
- Compact, readable and easilly extendable.
- All of the vector functions are available for all the vectors types
and can be restricted to certain types. Also template specialization
let us define exception for special class (like mpq).
- With optimization ON, the compiler unroll the loops and performance
is the same.
####Downsides:
- Might impact debugability. Though I would arge that the bugs are
rarelly caused by the vector class itself (since the operations are
quite trivial) but by the type conversions.
- Might impact compile time. I did not saw a significant impact since
the usage is not really widespread.
- Functions needs to be rewritten to support arbitrary vector length.
For instance, one can't call `len_squared_v3v3` in
`math::length_squared()` and call it a day.
- Type cast does not work with the template version of the `math::`
vector functions. Meaning you need to manually cast `float *` and
`(float *)[3]` to `float3` for the function calls.
i.e: `math::distance_squared(float3(nearest.co), positions[i]);`
- Some parts might loose in readability:
`float3::dot(v1.normalized(), v2.normalized())`
becoming
`math::dot(math::normalize(v1), math::normalize(v2))`
But I propose, when appropriate, to use
`using namespace blender::math;` on function local or file scope to
increase readability.
`dot(normalize(v1), normalize(v2))`
####Consideration:
- Include back `.length()` method. It is quite handy and is more C++
oriented.
- I considered the GLM library as a candidate for replacement. It felt
like too much for what we need and would be difficult to extend / modify
to our needs.
- I used Macros to reduce code in operators declaration and potential
copy paste bugs. This could reduce debugability and could be reverted.
- This touches `delaunay_2d.cc` and the intersection code. I would like
to know @howardt opinion on the matter.
- The `noexcept` on the copy constructor of `mpq(2|3)` is being removed.
But according to @JacquesLucke it is not a real problem for now.
I would like to give a huge thanks to @JacquesLucke who helped during this
and pushed me to reduce the duplication further.
Reviewed By: brecht, sergey, JacquesLucke
Differential Revision: https://developer.blender.org/D13791
2022-01-12 12:57:07 +01:00
|
|
|
return math::interpolate(a, b, factor);
|
2021-04-30 04:52:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<> inline float3 mix2(const float factor, const float3 &a, const float3 &b)
|
|
|
|
{
|
BLI: Refactor vector types & functions to use templates
This patch implements the vector types (i.e:`float2`) by making heavy
usage of templating. All vector functions are now outside of the vector
classes (inside the `blender::math` namespace) and are not vector size
dependent for the most part.
In the ongoing effort to make shaders less GL centric, we are aiming
to share more code between GLSL and C++ to avoid code duplication.
####Motivations:
- We are aiming to share UBO and SSBO structures between GLSL and C++.
This means we will use many of the existing vector types and others
we currently don't have (uintX, intX). All these variations were
asking for many more code duplication.
- Deduplicate existing code which is duplicated for each vector size.
- We also want to share small functions. Which means that vector
functions should be static and not in the class namespace.
- Reduce friction to use these types in new projects due to their
incompleteness.
- The current state of the `BLI_(float|double|mpq)(2|3|4).hh` is a
bit of a let down. Most clases are incomplete, out of sync with each
others with different codestyles, and some functions that should be
static are not (i.e: `float3::reflect()`).
####Upsides:
- Still support `.x, .y, .z, .w` for readability.
- Compact, readable and easilly extendable.
- All of the vector functions are available for all the vectors types
and can be restricted to certain types. Also template specialization
let us define exception for special class (like mpq).
- With optimization ON, the compiler unroll the loops and performance
is the same.
####Downsides:
- Might impact debugability. Though I would arge that the bugs are
rarelly caused by the vector class itself (since the operations are
quite trivial) but by the type conversions.
- Might impact compile time. I did not saw a significant impact since
the usage is not really widespread.
- Functions needs to be rewritten to support arbitrary vector length.
For instance, one can't call `len_squared_v3v3` in
`math::length_squared()` and call it a day.
- Type cast does not work with the template version of the `math::`
vector functions. Meaning you need to manually cast `float *` and
`(float *)[3]` to `float3` for the function calls.
i.e: `math::distance_squared(float3(nearest.co), positions[i]);`
- Some parts might loose in readability:
`float3::dot(v1.normalized(), v2.normalized())`
becoming
`math::dot(math::normalize(v1), math::normalize(v2))`
But I propose, when appropriate, to use
`using namespace blender::math;` on function local or file scope to
increase readability.
`dot(normalize(v1), normalize(v2))`
####Consideration:
- Include back `.length()` method. It is quite handy and is more C++
oriented.
- I considered the GLM library as a candidate for replacement. It felt
like too much for what we need and would be difficult to extend / modify
to our needs.
- I used Macros to reduce code in operators declaration and potential
copy paste bugs. This could reduce debugability and could be reverted.
- This touches `delaunay_2d.cc` and the intersection code. I would like
to know @howardt opinion on the matter.
- The `noexcept` on the copy constructor of `mpq(2|3)` is being removed.
But according to @JacquesLucke it is not a real problem for now.
I would like to give a huge thanks to @JacquesLucke who helped during this
and pushed me to reduce the duplication further.
Reviewed By: brecht, sergey, JacquesLucke
Differential Revision: https://developer.blender.org/D13791
2022-01-12 12:57:07 +01:00
|
|
|
return math::interpolate(a, b, factor);
|
2021-04-30 04:52:34 +02:00
|
|
|
}
|
|
|
|
|
2021-05-25 17:16:35 +02:00
|
|
|
template<>
|
|
|
|
inline ColorGeometry4f mix2(const float factor, const ColorGeometry4f &a, const ColorGeometry4f &b)
|
2021-04-30 04:52:34 +02:00
|
|
|
{
|
2022-04-21 16:11:26 +02:00
|
|
|
return math::interpolate(a, b, factor);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
inline ColorGeometry4b mix2(const float factor, const ColorGeometry4b &a, const ColorGeometry4b &b)
|
|
|
|
{
|
|
|
|
return math::interpolate(a, b, factor);
|
2021-04-30 04:52:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** \} */
|
|
|
|
|
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_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
Geometry Nodes: Extrude Mesh Node
This patch introduces an extrude node with three modes. The vertex mode
is quite simple, and just attaches new edges to the selected vertices.
The edge mode attaches new faces to the selected edges. The faces mode
extrudes patches of selected faces, or each selected face individually,
depending on the "Individual" boolean input.
The default value of the "Offset" input is the mesh's normals, which
can be scaled with the "Offset Scale" input.
**Attribute Propagation**
Attributes are transferred to the new elements with specific rules.
Attributes will never change domains for interpolations. Generally
boolean attributes are propagated with "or", meaning any connected
"true" value that is mixed in for other types will cause the new value
to be "true" as well. The `"id"` attribute does not have any special
handling currently.
Vertex Mode
- Vertex: Copied values of selected vertices.
- Edge: Averaged values of selected edges. For booleans, edges are
selected if any connected edges are selected.
Edge Mode
- Vertex: Copied values of extruded vertices.
- Connecting edges (vertical): Average values of connected extruded
edges. For booleans, the edges are selected if any connected
extruded edges are selected.
- Duplicate edges: Copied values of selected edges.
- Face: Averaged values of all faces connected to the selected edge.
For booleans, faces are selected if any connected original faces
are selected.
- Corner: Averaged values of corresponding corners in all faces
connected to selected edges. For booleans, corners are selected
if one of those corners are selected.
Face Mode
- Vertex: Copied values of extruded vertices.
- Connecting edges (vertical): Average values of connected selected
edges, not including the edges "on top" of extruded regions.
For booleans, edges are selected when any connected extruded edges
were selected.
- Duplicate edges: Copied values of extruded edges.
- Face: Copied values of the corresponding selected faces.
- Corner: Copied values of corresponding corners in selected faces.
Individual Face Mode
- Vertex: Copied values of extruded vertices.
- Connecting edges (vertical): Average values of the two neighboring
edges on each extruded face. For booleans, edges are selected
when at least one neighbor on the extruded face was selected.
- Duplicate edges: Copied values of extruded edges.
- Face: Copied values of the corresponding selected faces.
- Corner: Copied values of corresponding corners in selected faces.
**Differences from edit mode**
In face mode (non-individual), the behavior can be different than the
extrude tools in edit mode-- this node doesn't handle keeping the back-
faces around in the cases that the edit mode tools do. The planned
"Solidify" node will handle that use case instead. Keeping this node
simpler and faster is preferable at this point, especially because that
sort of "smart" behavior is not that predictable and makes less sense
in a procedural context.
In the future, an "Even Offset" option could be added to this node
hopefully fairly simply. For now it is left out in order to keep
the patch simpler.
**Implementation**
For the implementation, the `Mesh` data structure is used directly
rather than converting to `BMesh` and back like D12224. This optimizes
for large extrusion operations rather than many sequential extrusions.
While this is potentially more verbose, it has some important benefits:
First, there is no conversion to and from `BMesh`. The code only has
to fill arrays and it can do that all at once, making each component of
the algorithm much easier to optimize. It also makes the attribute
interpolation more explicit, and likely faster. Only limited topology
maps must be created in most cases.
While there are some necessary loops and allocations with the size of
the entire mesh, I tried to keep everything I could on the order of the
size of the selection rather than the size of the mesh. In that respect,
the individual faces mode is the best, since there is no topology
information necessary, and the amount of work just depends on the size
of the selection.
Modifying an existing mesh instead of generating a new one was a bit
of a toss-up, but has a few potential benefits:
- Avoids manually copying over attribute data for original elements.
- Avoids some overhead of creating a new mesh.
- Can potentially take advantage of future ammortized mesh growth.
This could be changed easily if it turns out to be the wrong choice.
Differential Revision: https://developer.blender.org/D13709
2022-01-24 05:42:49 +01:00
|
|
|
/**
|
|
|
|
* Mixes together booleans with "or" while fitting the same interface as the other
|
|
|
|
* mixers in order to be simpler to use. This mixing method has a few benefits:
|
|
|
|
* - An "average" for selections is relatively meaningless.
|
|
|
|
* - Predictable selection propagation is very super important.
|
|
|
|
* - It's generally easier to remove an element from a selection that is slightly too large than
|
|
|
|
* the opposite.
|
|
|
|
*/
|
|
|
|
class BooleanPropagationMixer {
|
|
|
|
private:
|
|
|
|
MutableSpan<bool> buffer_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* \param buffer: Span where the interpolated values should be stored.
|
|
|
|
*/
|
|
|
|
BooleanPropagationMixer(MutableSpan<bool> buffer) : buffer_(buffer)
|
|
|
|
{
|
|
|
|
buffer_.fill(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mix a #value into the element with the given #index.
|
|
|
|
*/
|
|
|
|
void mix_in(const int64_t index, const bool value, [[maybe_unused]] const float weight = 1.0f)
|
|
|
|
{
|
|
|
|
buffer_[index] |= value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Does not do anything, since the mixing is trivial.
|
|
|
|
*/
|
|
|
|
void finalize()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-04-21 16:11:26 +02:00
|
|
|
class ColorGeometry4fMixer {
|
2021-02-09 11:44:58 +01:00
|
|
|
private:
|
2021-05-25 17:16:35 +02:00
|
|
|
MutableSpan<ColorGeometry4f> buffer_;
|
|
|
|
ColorGeometry4f default_color_;
|
2021-02-09 11:44:58 +01:00
|
|
|
Array<float> total_weights_;
|
|
|
|
|
|
|
|
public:
|
2022-04-21 16:11:26 +02:00
|
|
|
ColorGeometry4fMixer(MutableSpan<ColorGeometry4f> buffer,
|
|
|
|
ColorGeometry4f default_color = ColorGeometry4f(0.0f, 0.0f, 0.0f, 1.0f));
|
2022-01-07 01:38:08 +01:00
|
|
|
void mix_in(int64_t index, const ColorGeometry4f &color, float weight = 1.0f);
|
2021-02-09 11:44:58 +01:00
|
|
|
void finalize();
|
|
|
|
};
|
|
|
|
|
2022-04-21 16:11:26 +02:00
|
|
|
class ColorGeometry4bMixer {
|
|
|
|
private:
|
|
|
|
MutableSpan<ColorGeometry4b> buffer_;
|
|
|
|
ColorGeometry4b default_color_;
|
|
|
|
Array<float> total_weights_;
|
|
|
|
Array<float4> accumulation_buffer_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ColorGeometry4bMixer(MutableSpan<ColorGeometry4b> buffer,
|
|
|
|
ColorGeometry4b default_color = ColorGeometry4b(0, 0, 0, 255));
|
|
|
|
void mix_in(int64_t index, const ColorGeometry4b &color, float weight = 1.0f);
|
|
|
|
void finalize();
|
|
|
|
};
|
|
|
|
|
2021-02-09 11:44:58 +01:00
|
|
|
template<typename T> struct DefaultMixerStruct {
|
Geometry Nodes: Extrude Mesh Node
This patch introduces an extrude node with three modes. The vertex mode
is quite simple, and just attaches new edges to the selected vertices.
The edge mode attaches new faces to the selected edges. The faces mode
extrudes patches of selected faces, or each selected face individually,
depending on the "Individual" boolean input.
The default value of the "Offset" input is the mesh's normals, which
can be scaled with the "Offset Scale" input.
**Attribute Propagation**
Attributes are transferred to the new elements with specific rules.
Attributes will never change domains for interpolations. Generally
boolean attributes are propagated with "or", meaning any connected
"true" value that is mixed in for other types will cause the new value
to be "true" as well. The `"id"` attribute does not have any special
handling currently.
Vertex Mode
- Vertex: Copied values of selected vertices.
- Edge: Averaged values of selected edges. For booleans, edges are
selected if any connected edges are selected.
Edge Mode
- Vertex: Copied values of extruded vertices.
- Connecting edges (vertical): Average values of connected extruded
edges. For booleans, the edges are selected if any connected
extruded edges are selected.
- Duplicate edges: Copied values of selected edges.
- Face: Averaged values of all faces connected to the selected edge.
For booleans, faces are selected if any connected original faces
are selected.
- Corner: Averaged values of corresponding corners in all faces
connected to selected edges. For booleans, corners are selected
if one of those corners are selected.
Face Mode
- Vertex: Copied values of extruded vertices.
- Connecting edges (vertical): Average values of connected selected
edges, not including the edges "on top" of extruded regions.
For booleans, edges are selected when any connected extruded edges
were selected.
- Duplicate edges: Copied values of extruded edges.
- Face: Copied values of the corresponding selected faces.
- Corner: Copied values of corresponding corners in selected faces.
Individual Face Mode
- Vertex: Copied values of extruded vertices.
- Connecting edges (vertical): Average values of the two neighboring
edges on each extruded face. For booleans, edges are selected
when at least one neighbor on the extruded face was selected.
- Duplicate edges: Copied values of extruded edges.
- Face: Copied values of the corresponding selected faces.
- Corner: Copied values of corresponding corners in selected faces.
**Differences from edit mode**
In face mode (non-individual), the behavior can be different than the
extrude tools in edit mode-- this node doesn't handle keeping the back-
faces around in the cases that the edit mode tools do. The planned
"Solidify" node will handle that use case instead. Keeping this node
simpler and faster is preferable at this point, especially because that
sort of "smart" behavior is not that predictable and makes less sense
in a procedural context.
In the future, an "Even Offset" option could be added to this node
hopefully fairly simply. For now it is left out in order to keep
the patch simpler.
**Implementation**
For the implementation, the `Mesh` data structure is used directly
rather than converting to `BMesh` and back like D12224. This optimizes
for large extrusion operations rather than many sequential extrusions.
While this is potentially more verbose, it has some important benefits:
First, there is no conversion to and from `BMesh`. The code only has
to fill arrays and it can do that all at once, making each component of
the algorithm much easier to optimize. It also makes the attribute
interpolation more explicit, and likely faster. Only limited topology
maps must be created in most cases.
While there are some necessary loops and allocations with the size of
the entire mesh, I tried to keep everything I could on the order of the
size of the selection rather than the size of the mesh. In that respect,
the individual faces mode is the best, since there is no topology
information necessary, and the amount of work just depends on the size
of the selection.
Modifying an existing mesh instead of generating a new one was a bit
of a toss-up, but has a few potential benefits:
- Avoids manually copying over attribute data for original elements.
- Avoids some overhead of creating a new mesh.
- Can potentially take advantage of future ammortized mesh growth.
This could be changed easily if it turns out to be the wrong choice.
Differential Revision: https://developer.blender.org/D13709
2022-01-24 05:42:49 +01:00
|
|
|
/* Use void by default. This can be checked for in `if constexpr` statements. */
|
2021-02-09 11:44:58 +01:00
|
|
|
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:16:35 +02:00
|
|
|
template<> struct DefaultMixerStruct<ColorGeometry4f> {
|
|
|
|
/* Use a special mixer for colors. ColorGeometry4f can't be added/multiplied, because this is not
|
2021-06-24 07:56:58 +02:00
|
|
|
* something one should usually do with colors. */
|
2022-04-21 16:11:26 +02:00
|
|
|
using type = ColorGeometry4fMixer;
|
|
|
|
};
|
|
|
|
template<> struct DefaultMixerStruct<ColorGeometry4b> {
|
|
|
|
using type = ColorGeometry4bMixer;
|
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>;
|
|
|
|
};
|
|
|
|
|
2022-02-04 17:29:11 +01:00
|
|
|
template<> struct DefaultMixerStruct<int8_t> {
|
|
|
|
static int8_t float_to_int8_t(const float &value)
|
|
|
|
{
|
|
|
|
return static_cast<int8_t>(value);
|
|
|
|
}
|
|
|
|
/* Store interpolated 8 bit integers in a float temporarily to increase accuracy. */
|
|
|
|
using type = SimpleMixerWithAccumulationType<int8_t, float, float_to_int8_t>;
|
|
|
|
};
|
|
|
|
|
Geometry Nodes: Extrude Mesh Node
This patch introduces an extrude node with three modes. The vertex mode
is quite simple, and just attaches new edges to the selected vertices.
The edge mode attaches new faces to the selected edges. The faces mode
extrudes patches of selected faces, or each selected face individually,
depending on the "Individual" boolean input.
The default value of the "Offset" input is the mesh's normals, which
can be scaled with the "Offset Scale" input.
**Attribute Propagation**
Attributes are transferred to the new elements with specific rules.
Attributes will never change domains for interpolations. Generally
boolean attributes are propagated with "or", meaning any connected
"true" value that is mixed in for other types will cause the new value
to be "true" as well. The `"id"` attribute does not have any special
handling currently.
Vertex Mode
- Vertex: Copied values of selected vertices.
- Edge: Averaged values of selected edges. For booleans, edges are
selected if any connected edges are selected.
Edge Mode
- Vertex: Copied values of extruded vertices.
- Connecting edges (vertical): Average values of connected extruded
edges. For booleans, the edges are selected if any connected
extruded edges are selected.
- Duplicate edges: Copied values of selected edges.
- Face: Averaged values of all faces connected to the selected edge.
For booleans, faces are selected if any connected original faces
are selected.
- Corner: Averaged values of corresponding corners in all faces
connected to selected edges. For booleans, corners are selected
if one of those corners are selected.
Face Mode
- Vertex: Copied values of extruded vertices.
- Connecting edges (vertical): Average values of connected selected
edges, not including the edges "on top" of extruded regions.
For booleans, edges are selected when any connected extruded edges
were selected.
- Duplicate edges: Copied values of extruded edges.
- Face: Copied values of the corresponding selected faces.
- Corner: Copied values of corresponding corners in selected faces.
Individual Face Mode
- Vertex: Copied values of extruded vertices.
- Connecting edges (vertical): Average values of the two neighboring
edges on each extruded face. For booleans, edges are selected
when at least one neighbor on the extruded face was selected.
- Duplicate edges: Copied values of extruded edges.
- Face: Copied values of the corresponding selected faces.
- Corner: Copied values of corresponding corners in selected faces.
**Differences from edit mode**
In face mode (non-individual), the behavior can be different than the
extrude tools in edit mode-- this node doesn't handle keeping the back-
faces around in the cases that the edit mode tools do. The planned
"Solidify" node will handle that use case instead. Keeping this node
simpler and faster is preferable at this point, especially because that
sort of "smart" behavior is not that predictable and makes less sense
in a procedural context.
In the future, an "Even Offset" option could be added to this node
hopefully fairly simply. For now it is left out in order to keep
the patch simpler.
**Implementation**
For the implementation, the `Mesh` data structure is used directly
rather than converting to `BMesh` and back like D12224. This optimizes
for large extrusion operations rather than many sequential extrusions.
While this is potentially more verbose, it has some important benefits:
First, there is no conversion to and from `BMesh`. The code only has
to fill arrays and it can do that all at once, making each component of
the algorithm much easier to optimize. It also makes the attribute
interpolation more explicit, and likely faster. Only limited topology
maps must be created in most cases.
While there are some necessary loops and allocations with the size of
the entire mesh, I tried to keep everything I could on the order of the
size of the selection rather than the size of the mesh. In that respect,
the individual faces mode is the best, since there is no topology
information necessary, and the amount of work just depends on the size
of the selection.
Modifying an existing mesh instead of generating a new one was a bit
of a toss-up, but has a few potential benefits:
- Avoids manually copying over attribute data for original elements.
- Avoids some overhead of creating a new mesh.
- Can potentially take advantage of future ammortized mesh growth.
This could be changed easily if it turns out to be the wrong choice.
Differential Revision: https://developer.blender.org/D13709
2022-01-24 05:42:49 +01:00
|
|
|
template<typename T> struct DefaultPropatationMixerStruct {
|
|
|
|
/* Use void by default. This can be checked for in `if constexpr` statements. */
|
|
|
|
using type = typename DefaultMixerStruct<T>::type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> struct DefaultPropatationMixerStruct<bool> {
|
|
|
|
using type = BooleanPropagationMixer;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This mixer is meant for propagating attributes when creating new geometry. A key difference
|
|
|
|
* with the default mixer is that booleans are mixed with "or" instead of "at least half"
|
|
|
|
* (the default mixing for booleans).
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
using DefaultPropatationMixer = typename DefaultPropatationMixerStruct<T>::type;
|
|
|
|
|
2021-02-09 11:44:58 +01:00
|
|
|
/* 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
|