Geometry Node: Index of Nearest #104619
|
@ -324,7 +324,7 @@ DefNode(GeometryNode, GEO_NODE_FLIP_FACES, 0, "FLIP_FACES", FlipFaces, "Flip Fac
|
|||
DefNode(GeometryNode, GEO_NODE_GEOMETRY_TO_INSTANCE, 0, "GEOMETRY_TO_INSTANCE", GeometryToInstance, "Geometry to Instance", "Convert each input geometry into an instance, which can be much faster than the Join Geometry node when the inputs are large")
|
||||
DefNode(GeometryNode, GEO_NODE_IMAGE_INFO, 0, "IMAGE_INFO", ImageInfo, "Image Info", "Retrieve information about an image")
|
||||
DefNode(GeometryNode, GEO_NODE_IMAGE_TEXTURE, def_geo_image_texture, "IMAGE_TEXTURE", ImageTexture, "Image Texture", "Sample values from an image texture")
|
||||
DefNode(GeometryNode, GEO_NODE_INDEX_OF_NEAREST, 0, "INDEX_OF_NEAREST", IndexOfNearest, "Index of Nearest", "Index of nearest element by groups condition")
|
||||
DefNode(GeometryNode, GEO_NODE_INDEX_OF_NEAREST, 0, "INDEX_OF_NEAREST", IndexOfNearest, "Index of Nearest", "Find the nearest element in the a group. Similar to the \"Sample Nearest\" node")
|
||||
mod_moder marked this conversation as resolved
Outdated
|
||||
DefNode(GeometryNode, GEO_NODE_IMAGE, def_geo_image, "IMAGE", InputImage, "Image", "Input image")
|
||||
DefNode(GeometryNode, GEO_NODE_INPUT_CURVE_HANDLES, 0, "INPUT_CURVE_HANDLES", InputCurveHandlePositions,"Curve Handle Positions", "Retrieve the position of each Bézier control point's handles")
|
||||
DefNode(GeometryNode, GEO_NODE_INPUT_CURVE_TILT, 0, "INPUT_CURVE_TILT", InputCurveTilt, "Curve Tilt", "Retrieve the angle at each control point used to twist the curve's normal around its tangent")
|
||||
|
|
|
@ -8,34 +8,19 @@
|
|||
#include "BLI_multi_value_map.hh"
|
||||
#include "BLI_task.hh"
|
||||
|
||||
#include "BKE_geometry_fields.hh"
|
||||
|
||||
#include "node_geometry_util.hh"
|
||||
mod_moder marked this conversation as resolved
Hans Goudey
commented
`BKE_geometry_fields.hh` is already included indirectly by `node_geometry_util.hh`
|
||||
|
||||
#include "UI_interface.h"
|
||||
#include "UI_resources.h"
|
||||
|
||||
namespace blender::nodes::node_geo_index_of_nearest_cc {
|
||||
|
||||
static void node_declare(NodeDeclarationBuilder &b)
|
||||
mod_moder marked this conversation as resolved
Outdated
Hans Goudey
commented
I think these UI includes are unused I think these UI includes are unused
|
||||
{
|
||||
b.add_input<decl::Vector>("Position").implicit_field(implicit_field_inputs::position);
|
||||
b.add_input<decl::Vector>(N_("Position")).implicit_field(implicit_field_inputs::position);
|
||||
|
||||
b.add_input<decl::Int>("Self Group ID").supports_field().hide_value().default_value(0);
|
||||
b.add_input<decl::Int>("Group ID to Search").supports_field().hide_value().default_value(0);
|
||||
b.add_input<decl::Int>(N_("Self Group ID")).supports_field().hide_value().default_value(0);
|
||||
b.add_input<decl::Int>(N_("Nearest Group ID")).supports_field().hide_value().default_value(0);
|
||||
|
||||
b.add_output<decl::Int>("Index").field_source().description(N_("Index of nearest element"));
|
||||
}
|
||||
|
||||
int kdtree_find_neighboard(KDTree_3d *tree, const float3 &position, int index)
|
||||
{
|
||||
return BLI_kdtree_3d_find_nearest_cb(
|
||||
tree, position, 0, [index](const int other_new_i, const float * /*co*/, float /*dist_sq*/) {
|
||||
if (index == other_new_i) {
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
});
|
||||
b.add_output<decl::Int>(N_("Index")).field_source().description(N_("Index of nearest element"));
|
||||
b.add_output<decl::Bool>(N_("Valid")).field_source();
|
||||
}
|
||||
mod_moder marked this conversation as resolved
Outdated
Hans Goudey
commented
These identifiers/names are missing the translation macro These identifiers/names are missing the translation macro `N_`
|
||||
|
||||
mod_moder marked this conversation as resolved
Outdated
Hans Goudey
commented
We settled on We settled on `Self Group ID` and `Nearest Group ID` in the module meeting.
|
||||
class IndexOfNearestFieldInput final : public bke::GeometryFieldInput {
|
||||
|
@ -64,63 +49,56 @@ class IndexOfNearestFieldInput final : public bke::GeometryFieldInput {
|
|||
evaluator.add(search_group_);
|
||||
evaluator.evaluate();
|
||||
|
||||
Hans Goudey
commented
I don't think there's a benefit to specifying the capture in a case like this (instead of I don't think there's a benefit to specifying the capture in a case like this (instead of `[&]`)
|
||||
const VArray<float3> positions = evaluator.get_evaluated<float3>(0);
|
||||
const VArray<int> group = evaluator.get_evaluated<int>(1);
|
||||
const VArray<int> search_group = evaluator.get_evaluated<int>(2);
|
||||
const VArray<float3> &positions = evaluator.get_evaluated<float3>(0);
|
||||
const VArray<int> &group = evaluator.get_evaluated<int>(1);
|
||||
const VArray<int> &search_group = evaluator.get_evaluated<int>(2);
|
||||
|
||||
const bool group_use = !group.is_single();
|
||||
const bool group_to_find_use = !search_group.is_single();
|
||||
const bool use_group = !group.is_single();
|
||||
const bool use_search_group = !search_group.is_single();
|
||||
|
||||
MultiValueMap<int, int64_t> in_group;
|
||||
MultiValueMap<int, int64_t> out_group;
|
||||
Array<int> indices(mask.min_array_size());
|
||||
|
||||
threading::parallel_invoke((indices.size() > 512) && group_to_find_use && group_use,
|
||||
[&]() {
|
||||
if (group_use) {
|
||||
for (const int64_t i : mask.index_range()) {
|
||||
in_group.add(group[mask[i]], i);
|
||||
}
|
||||
}
|
||||
else {
|
||||
const int group_key = group.get_internal_single();
|
||||
in_group.add_multiple(group_key, {});
|
||||
}
|
||||
},
|
||||
[&]() {
|
||||
if (group_to_find_use) {
|
||||
for (const int64_t i : mask.index_range()) {
|
||||
out_group.add(search_group[mask[i]], i);
|
||||
}
|
||||
}
|
||||
});
|
||||
threading::parallel_invoke(
|
||||
(indices.size() > 512) && use_search_group && use_group,
|
||||
[&]() {
|
||||
if (use_group) {
|
||||
Jacques Lucke
commented
Can just use a normal for loop here. The performance benefit of using Can just use a normal for loop here. The performance benefit of using `foreach_index` is negligible, because most time is spend in the kdtree traversal.
|
||||
mask.foreach_index([&](const auto index) { in_group.add(group[index], index); });
|
||||
return;
|
||||
}
|
||||
const int group_key = group.get_internal_single();
|
||||
in_group.add_multiple(group_key, {});
|
||||
mod_moder marked this conversation as resolved
Outdated
Hans Goudey
commented
I'd change these names to I'd change these names to `use_group` and `use_search_group` for consistency with your other variable names and because `use` at front sounds more natural.
|
||||
},
|
||||
[&]() {
|
||||
if (use_search_group) {
|
||||
mask.foreach_index(
|
||||
[&](const auto index) { out_group.add(search_group[index], index); });
|
||||
}
|
||||
});
|
||||
Hans Goudey
commented
This comment is a bit vague, but I'm finding it hard to keep track of the separate This comment is a bit vague, but I'm finding it hard to keep track of the separate `group_use` and `group_to_find_use` conditions. I wonder if the first condition could be handled with by splitting logic into a separate function, like `SampleCurveFunction` does with its `sample_curve` lambda.
|
||||
|
||||
for (int key : in_group.keys()) {
|
||||
for (const int key : in_group.keys()) {
|
||||
mod_moder marked this conversation as resolved
Outdated
Hans Goudey
commented
No strong opinion, but using regular No strong opinion, but using regular `=` assignment seems more consistent here.
|
||||
/* Never empty. */
|
||||
const Span<int64_t> self_points = group_use ? in_group.lookup(key) : mask.indices();
|
||||
const Span<int64_t> search_points = group_to_find_use ? out_group.lookup(key) : self_points;
|
||||
const IndexMask self_points(use_group ? IndexMask(in_group.lookup(key)) : mask);
|
||||
const IndexMask search_points(use_search_group ? IndexMask(out_group.lookup(key)) :
|
||||
self_points);
|
||||
|
||||
if (search_points.is_empty()) {
|
||||
indices.as_mutable_span().fill_indices(self_points, 0);
|
||||
indices.as_mutable_span().fill_indices(self_points, -1);
|
||||
continue;
|
||||
}
|
||||
Hans Goudey
commented
It seems like creating a KD tree for every group ID will mean that the KD tree for the same set of search points will potentially be built multiple times. There should probably be some way to avoid building a tree multiple times for the same key. Overall this area looks much cleaner than the last time I checked though! It seems like creating a KD tree for every group ID will mean that the KD tree for the same set of search points will potentially be built multiple times. There should probably be some way to avoid building a tree multiple times for the same key.
Overall this area looks much cleaner than the last time I checked though!
Iliya Katushenock
commented
After you point it out... it seems I made some kind of logical error in the implementation of switching groups when iterating over them. Or not .. i need to redo this part of the code After you point it out... it seems I made some kind of logical error in the implementation of switching groups when iterating over them. Or not .. i need to redo this part of the code
Hans Goudey
commented
Remove empty line between Remove empty line between `group_indexing` declaration and loop that creates it
|
||||
|
||||
KDTree_3d *tree = BLI_kdtree_3d_new(search_points.size());
|
||||
|
||||
for (const int64_t index : search_points) {
|
||||
BLI_kdtree_3d_insert(tree, index, positions[index]);
|
||||
mod_moder marked this conversation as resolved
Outdated
Hans Goudey
commented
We can't selectively ignore the mask actually, it may be incorrect to write the output to non-selected indices. So this check really has to be We can't selectively ignore the mask actually, it may be incorrect to write the output to non-selected indices. So this check really has to be `mask.size() == domain_size`
Iliya Katushenock
commented
I see. I see.
Just if domain size is 10000, mask is 9999, is no much sense to compute the cheap mask. I think, is better to just allocate all elements if cheap mask is used.
|
||||
}
|
||||
mod_moder marked this conversation as resolved
Outdated
Hans Goudey
commented
Replace this with Replace this with `const bool mask_is_full = mask.size() == domain_size;`
|
||||
|
||||
BLI_kdtree_3d_balance(tree);
|
||||
|
||||
mod_moder marked this conversation as resolved
Outdated
Hans Goudey
commented
`int key` -> `const int key`
|
||||
threading::parallel_for(self_points.index_range(), 128, [&](const IndexRange range) {
|
||||
threading::parallel_for(self_points.index_range(), 512, [&](const IndexRange range) {
|
||||
for (const int64_t index : self_points.slice(range)) {
|
||||
const int index_of_nearest = kdtree_find_neighboard(tree, positions[index], index);
|
||||
if (index_of_nearest == -1) {
|
||||
indices[index] = index;
|
||||
}
|
||||
else {
|
||||
indices[index] = index_of_nearest;
|
||||
}
|
||||
indices[index] = this->kdtree_find_neighboard(tree, positions[index], index);
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -130,6 +108,22 @@ class IndexOfNearestFieldInput final : public bke::GeometryFieldInput {
|
|||
return VArray<int>::ForContainer(std::move(indices));
|
||||
}
|
||||
|
||||
protected:
|
||||
mod_moder marked this conversation as resolved
Outdated
Jacques Lucke
commented
While forest is used as a technical term for a graph containing multiple trees, I don't think the term should be used for a collection of multiple independent kd trees. Just use While forest is used as a technical term for a graph containing multiple trees, I don't think the term should be used for a collection of multiple independent kd trees. Just use `kdtrees`.
Iliya Katushenock
commented
If i do merge If i do merge `parallel_for`s below, i can delete this vector.
|
||||
int kdtree_find_neighboard(KDTree_3d *tree, const float3 &position, int index) const
|
||||
{
|
||||
return BLI_kdtree_3d_find_nearest_cb(
|
||||
mod_moder marked this conversation as resolved
Outdated
Hans Goudey
commented
I think this should be I think this should be `!mask_is_cheap`
Iliya Katushenock
commented
I was isn't invented the best name, I was isn't invented the best name, `mask_is_cheap` == true if computing of indices make sense and cheap mask is used.
|
||||
tree,
|
||||
Hans Goudey
commented
TBH I think it's better to just use TBH I think it's better to just use `int64_t` here to avoid duplicating the functions above. Eventually when `IndexMask` is refactored, these could benefit from using that, and the `int64_t` will make that more clear too.
|
||||
position,
|
||||
mod_moder marked this conversation as resolved
Outdated
Hans Goudey
commented
`Vector` -> `Array` here
|
||||
0,
|
||||
[index](const int other_new_i, const float * /*co*/, float /*dist_sq*/) {
|
||||
if (index == other_new_i) {
|
||||
return 0;
|
||||
}
|
||||
Hans Goudey
commented
`domain_size > 1024 && use_cheap_mask` -> `domain_size > 1024 && !mask_is_full`
|
||||
return 1;
|
||||
Iliya Katushenock
commented
Are Are `grain_size` is a power of 2?
Hans Goudey
commented
They don't need to be powers of two, though I think it can be slightly beneficial. Since these numbers are a bit arbitrary anyway I started using powers of two. They don't need to be powers of two, though I think it can be slightly beneficial. Since these numbers are a bit arbitrary anyway I started using powers of two.
Iliya Katushenock
commented
I remember fixing a bug, only related to the fact that there was no power of 2x. I remember fixing a bug, only related to the fact that there was no power of 2x.
For the blur node, I made the search function for the top bit part in the threading namespace.
|
||||
});
|
||||
}
|
||||
|
||||
Iliya Katushenock
commented
Why It seems that trying to use a mask to multiply all user groups by groups from the mask leads to much more overhead in most cases. Why `tree_masks[i]` and `evaluate_masks[i]` is groups with the same id? this isn't sorted somether or protected, that mask isn't avoid all elements of some grouop and create offset for all other groups even all of that added in same order.
It seems that trying to use a mask to multiply all user groups by groups from the mask leads to much more overhead in most cases.
If you don't do some kind of analogue of a tree, then it's like doing a boolean through mask1 * mask2. When there is something like bvh. so I think it's easier to just calculate everything. I'm not sure there are many cases now where the mask can actually be a small part.
Hans Goudey
commented
Oh, great point! I think it's still worth having a separate evaluation mask. Even just the set position node with a small selection would benefit from it, I think kd tree lookups are fairly expensive. I guess both masks have to be created at the same time. Oh, great point! I think it's still worth having a separate evaluation mask. Even just the set position node with a small selection would benefit from it, I think kd tree lookups are fairly expensive. I guess both masks have to be created at the same time.
|
||||
public:
|
||||
void for_each_field_input_recursive(FunctionRef<void(const FieldInput &)> fn) const
|
||||
{
|
||||
positions_.node().for_each_field_input_recursive(fn);
|
||||
|
@ -160,14 +154,34 @@ class IndexOfNearestFieldInput final : public bke::GeometryFieldInput {
|
|||
|
||||
static void node_geo_exec(GeoNodeExecParams params)
|
||||
{
|
||||
const Field<float3> position = params.extract_input<Field<float3>>("Position");
|
||||
Field<float3> position_field = params.extract_input<Field<float3>>("Position");
|
||||
|
||||
const Field<int> self_group = params.extract_input<Field<int>>("Self Group ID");
|
||||
const Field<int> search_group = params.extract_input<Field<int>>("Group ID to Search");
|
||||
Field<int> self_group_field = params.extract_input<Field<int>>("Self Group ID");
|
||||
Field<int> search_group_field = params.extract_input<Field<int>>("Nearest Group ID");
|
||||
|
||||
params.set_output("Index",
|
||||
Field<int>{std::make_shared<IndexOfNearestFieldInput>(
|
||||
std::move(position), std::move(self_group), std::move(search_group))});
|
||||
Field<int> index_of_nearest_field(std::make_shared<IndexOfNearestFieldInput>(
|
||||
std::move(position_field), std::move(self_group_field), std::move(search_group_field)));
|
||||
|
||||
if (params.output_is_required("Index")) {
|
||||
mod_moder marked this conversation as resolved
Outdated
Hans Goudey
commented
If using I think it would be fine to retrieve them directly in If using `std::move` with these variables, don't declare them const.
I think it would be fine to retrieve them directly in `make_shared` though, rather than having temporary variables.
|
||||
static auto clamp_fn = mf::build::SI1_SO<int, int>(
|
||||
"Clamp",
|
||||
[](const int index) { return math::max(0, index); },
|
||||
mf::build::exec_presets::AllSpanOrSingle());
|
||||
auto clamp_op = std::make_shared<FieldOperation>(
|
||||
FieldOperation(std::move(clamp_fn), {index_of_nearest_field}));
|
||||
params.set_output("Index", Field<int>(clamp_op, 0));
|
||||
}
|
||||
|
||||
if (params.output_is_required("Valid")) {
|
||||
static auto valid_fn = mf::build::SI1_SO<int, bool>(
|
||||
"Valid Index",
|
||||
[](const int index) { return index != -1; },
|
||||
mf::build::exec_presets::AllSpanOrSingle());
|
||||
|
||||
auto valid_op = std::make_shared<FieldOperation>(
|
||||
FieldOperation(std::move(valid_fn), {std::move(index_of_nearest_field)}));
|
||||
params.set_output("Valid", Field<bool>(valid_op, 0));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace blender::nodes::node_geo_index_of_nearest_cc
|
||||
|
|
For the description, I'd suggest:
Find the nearest element in the a group
Adding
. Similar to the \"Sample Nearest\" node
might be helpful too.