WIP: Geometry Nodes: Curve intersections #109393

Draft
Charlie Jolly wants to merge 8 commits from CharlieJolly/blender:gn-curve-intersections into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
Member

PR updating my old patch D15624 to the new development site.
https://archive.blender.org/developer/D15624

Outputs curve intersections as a collection of points.

Three modes:
Self - checks a spline for self intersections only
All - checks all spline edges for intersections
Plane - checks all spline edges for intersections with a plane

Inspired by this post in stackexchange:
https://blender.stackexchange.com/questions/264070/geometrynodes-curve-intersections

image

PR updating my old patch D15624 to the new development site. https://archive.blender.org/developer/D15624 Outputs curve intersections as a collection of points. Three modes: Self - checks a spline for self intersections only All - checks all spline edges for intersections Plane - checks all spline edges for intersections with a plane Inspired by this post in stackexchange: https://blender.stackexchange.com/questions/264070/geometrynodes-curve-intersections ![image](/attachments/bf5bd848-9b56-48bc-8143-1203dc42ff47)
269 KiB
Charlie Jolly added the
Interest
Geometry Nodes
label 2023-06-27 02:03:55 +02:00
Charlie Jolly requested review from Hans Goudey 2023-06-27 02:04:43 +02:00
Iliya Katushenock added this to the Nodes & Physics project 2023-06-27 02:05:33 +02:00
Author
Member

Finally had some time to port over some of my old patches to the new development site.

Finally had some time to port over some of my old patches to the new development site.
Hans Goudey requested changes 2023-06-28 04:05:15 +02:00
Hans Goudey left a comment
Member

I think this is a nice feature, and it's been through a lot already, so it should be worth finishing!

My only larger comment is that I think this node should propagate attributes from the source curves. That probably adds a fair amount of complexity to the code, but I think it's fairly essential for the node to work as expected. Let me know if you'd like advice for that part in chat.

I think this is a nice feature, and it's been through a lot already, so it should be worth finishing! My only larger comment is that I think this node should propagate attributes from the source curves. That probably adds a fair amount of complexity to the code, but I think it's fairly essential for the node to work as expected. Let me know if you'd like advice for that part in chat.
@ -675,3 +675,2 @@
classes = (
NODE_MT_geometry_node_add_all,
classes = (NODE_MT_geometry_node_add_all,
Member

Unrelated formatting changes in this file

Unrelated formatting changes in this file
CharlieJolly marked this conversation as resolved
@ -290,6 +290,8 @@ class CurvesGeometry : public ::CurvesGeometry {
Span<float3> evaluated_tangents() const;
Span<float3> evaluated_normals() const;
Span<float3> evaluated_positions_for_curve(int curve_index) const;
Member

We decided to avoid adding functions like this, and just rely on the combination of local variables OffsetIndices and Span<float3> in calling functions. (See uses of evaluated_points_by_curve)

We decided to avoid adding functions like this, and just rely on the combination of local variables `OffsetIndices` and `Span<float3>` in calling functions. (See uses of `evaluated_points_by_curve`)
CharlieJolly marked this conversation as resolved
@ -0,0 +26,4 @@
static void node_declare(NodeDeclarationBuilder &b)
{
b.add_input<decl::Geometry>(N_("Curve")).supported_type(GeometryComponent::Type::Curve);
Member

The N_ translation markers are unnecessary now actually :)

They were made automatic a few weeks ago

The `N_` translation markers are unnecessary now actually :) They were made automatic a few weeks ago
CharlieJolly marked this conversation as resolved
@ -0,0 +38,4 @@
.subtype(PROP_DISTANCE)
.make_available(
[](bNode &node) { node_storage(node).mode = GEO_NODE_CURVE_INTERSECT_PLANE; })
.description(N_("Plane offset"));
Member

The description shouldn't be the same as the input name :P

For these offset inputs, I'm not sure what they do right now-- the descriptions should probably say in which direction the offset is.

The description shouldn't be the same as the input name :P For these offset inputs, I'm not sure what they do right now-- the descriptions should probably say in which direction the offset is.
Author
Member

Removed description. This is like Offset used in Set Position.

Removed description. This is like Offset used in Set Position.
Member

Hmm, so can the offset be achieved by using a set position node afterwards?

Hmm, so can the offset be achieved by using a set position node afterwards?
CharlieJolly marked this conversation as resolved
@ -0,0 +92,4 @@
bool intersects;
};
struct CurveInfo {
Member

I think this CurveInfo array is unnecessary. With the indices in the BVH tree, the raw data can be accessed directly, and process could be a separate boolean array or bit vector.

I think this `CurveInfo` array is unnecessary. With the indices in the BVH tree, the raw data can be accessed directly, and `process` could be a separate boolean array or bit vector.
Author
Member

When the patch started it was using the old spline api. I think this is a hangover from that way of thinking.

When the patch started it was using the old spline api. I think this is a hangover from that way of thinking.
CharlieJolly marked this conversation as resolved
@ -0,0 +169,4 @@
for (const int64_t curve_i : src_curves.curves_range()) {
const CurveInfo &curveinfo = curve_data[curve_i];
const int totpoints = curveinfo.positions.size() - 1;
const int loopcount = curveinfo.cyclic ? totpoints + 1 : totpoints;
Member

This can use the function curves::segments_num

This can use the function `curves::segments_num`
CharlieJolly marked this conversation as resolved
@ -0,0 +189,4 @@
}
/* Based on isect_line_plane_v3 with check that lines cross between start and end points. */
static bool isect_line_plane_crossing(const float3 point_1,
Member

This sort of function shouldn't be specific to this node IMO. There's a precedent for including utilities like this in the newer BLI math headers. See isect_seg_seg. Maybe this could be isect_seg_plane?

This sort of function shouldn't be specific to this node IMO. There's a precedent for including utilities like this in the newer BLI math headers. See `isect_seg_seg`. Maybe this could be `isect_seg_plane`?
Author
Member

Will add to BLI math if patch is accepted. Easier to keep with patch whilst it is in development.

The function is based on isect_line_plane_v3 with an additional check that lines cross between start and end points. It also stores the lambda.

Will add to BLI math if patch is accepted. Easier to keep with patch whilst it is in development. _The function is based on isect_line_plane_v3 with an additional check that lines cross between start and end points. It also stores the lambda._
@ -0,0 +265,4 @@
continue;
}
curve_plane_intersections_to_points(
Member

Looks like multiple threads are appending to the same vector here, which isn't thread-safe. Maybe it would work to accumulate the intersections per thread with EnumerableThreadSpecific and then combine them at the end?

Looks like multiple threads are appending to the same vector here, which isn't thread-safe. Maybe it would work to accumulate the intersections per thread with `EnumerableThreadSpecific` and then combine them at the end?
CharlieJolly marked this conversation as resolved
@ -0,0 +306,4 @@
BVHTree *bvhtree = create_curve_segment_bvhtree(src_curves, &curve_segments);
const int max_segments = curve_segments.size() + 1;
Array<Vector<OutputData>> intersection_output_data;
Member

Maybe this array wouldn't have to be so big with the EnumerableThreadSpecific suggestion from above.

Maybe this array wouldn't have to be so big with the `EnumerableThreadSpecific` suggestion from above.
CharlieJolly marked this conversation as resolved
@ -0,0 +313,4 @@
Vector<OutputData> output_data;
Vector<int> curve_ids;
Vector<int> bvh_indices;
bvh_indices.reserve(max_segments);
Member

Allocating something curve_segments in size for every segment and then freeing it seems a bit inefficient, maybe that's not right?

Allocating something `curve_segments` in size for every segment and then freeing it seems a bit inefficient, maybe that's not right?
CharlieJolly marked this conversation as resolved
@ -0,0 +367,4 @@
});
for (int64_t i : intersection_output_data.index_range()) {
const Vector<OutputData> data = intersection_output_data[i];
Member

Careful, this line copies the Vector without a &

Careful, this line copies the `Vector` without a `&`
CharlieJolly marked this conversation as resolved
@ -0,0 +387,4 @@
AnonymousAttributeIDPtr curve_index_attr_id = params.get_output_anonymous_attribute_id_if_needed(
"Curve Index");
lazy_threading::send_hint();
Member

Most code shouldn't need to do this manually, parallel_for calls that end up using threading do that already

Most code shouldn't need to do this manually, `parallel_for` calls that end up using threading do that already
CharlieJolly marked this conversation as resolved
@ -0,0 +434,4 @@
Vector<float3> r_position;
Vector<int> r_curve_id;
for (auto begin = std::make_move_iterator(r_data.begin()),
Member

Should be fine to write directly to the attributes, without the temporary r_position and r_curve_id vectors.

Should be fine to write directly to the attributes, without the temporary `r_position` and `r_curve_id` vectors.
CharlieJolly marked this conversation as resolved
Hans Goudey changed title from WIP: D15624 Curve intersections to WIP: Geometry Nodes: Curve intersections 2023-06-28 04:05:38 +02:00

Is it really necessary to have a plane mode?

Is it really necessary to have a plane mode?
Author
Member

My only larger comment is that I think this node should propagate attributes from the source curves. That probably adds a fair amount of complexity to the code, but I think it's fairly essential for the node to work as expected. Let me know if you'd like advice for that part in chat.

@HooglyBoogly for attributes are you thinking the same as for the Curve to Points node e.g. Normal, Tangent etc?

> My only larger comment is that I think this node should propagate attributes from the source curves. That probably adds a fair amount of complexity to the code, but I think it's fairly essential for the node to work as expected. Let me know if you'd like advice for that part in chat. @HooglyBoogly for attributes are you thinking the same as for the Curve to Points node e.g. Normal, Tangent etc?
Member

@HooglyBoogly for attributes are you thinking the same as for the Curve to Points node e.g. Normal, Tangent etc?

No, I don't think so, just the attributes stored on the curves.

>@HooglyBoogly for attributes are you thinking the same as for the Curve to Points node e.g. Normal, Tangent etc? No, I don't think so, just the attributes stored on the curves.
Charlie Jolly force-pushed gn-curve-intersections from 16e55403d7 to 9d8f6edf5b 2023-08-02 18:33:55 +02:00 Compare
Author
Member

Forced push commit because I had squashed and amended commits locally.

Last commit:

  • Add Length and Factor sockets
  • Remove CurveInfo struct
  • Use EnumerableThreadSpecific and gather_thread_storage based on @LukasTonne Closest Neighbour patch #110675
  • Address most comments

Still need to look at passing attributes etc

image

Forced push commit because I had squashed and amended commits locally. Last commit: - Add Length and Factor sockets - Remove CurveInfo struct - Use EnumerableThreadSpecific and gather_thread_storage based on @LukasTonne Closest Neighbour patch #110675 - Address most comments Still need to look at passing attributes etc ![image](/attachments/4c9edbc7-3ef7-474a-96e6-bf85b7ba1b62)
Charlie Jolly added 1 commit 2023-08-02 18:44:31 +02:00
Charlie Jolly added 2 commits 2023-08-08 12:55:55 +02:00
Author
Member

Trim curves between first and last intersections provides a way to Trim Loose Ends.

image

Trim curves between first and last intersections provides a way to Trim Loose Ends. ![image](/attachments/26fd9921-f752-4433-9a5b-6f591f754a1c)
421 KiB

If you point out the fact that this node is very similar to the Closest Neighbors node (#110675), then it turns out that this is not a real practical application node, but just a way to output the intersection factors array field on curve domains as a point cloud. All the other operations look like just working with data arrays.
At least i don't see other case to use this points. But as you have shown (#109393 (comment)), this data may be relevant a lot. But not in the form of outgoing geometry and group id fields, ... .
A similar concept a year ago i considered as an implementation of the output of topology data arrays (devtalk: Access to geometry data. Post 7, point 2). A useful experiment, but it seems to break how we want to represent this kind of data (as array attribute).

If you point out the fact that this node is very similar to the Closest Neighbors node (#110675), then it turns out that this is not a real practical application node, but just a way to output the intersection factors array field on curve domains as a point cloud. All the other operations look like just working with data arrays. At least i don't see other case to use this points. But as you have shown (https://projects.blender.org/blender/blender/pulls/109393#issuecomment-995061), this data may be relevant a lot. But not in the form of outgoing geometry and group id fields, ... . A similar concept a year ago i considered as an implementation of the output of topology data arrays ([devtalk: Access to geometry data. Post 7, point 2](https://devtalk.blender.org/t/access-to-geometry-data/24618/7)). A useful experiment, but it seems to break how we want to represent this kind of data (as array attribute).
Author
Member

@mod_moder the comment about the closest neighbour node was only to reference the use of EnumerableThreadSpecific to make it thread safe and to address some comments from Hans.

This patch is WIP so what data and how the data is exposed is still to be decided. Since intersections can occur between points, that data is exposed by the Factor and Length. It's useful to have this available for testing etc. Offset mode which was proposed on the old system could be replaced by using the Direction data for example. Curve index provides data to determine which curve the intersection belongs to.

@mod_moder the comment about the closest neighbour node was only to reference the use of `EnumerableThreadSpecific` to make it thread safe and to address some comments from Hans. This patch is WIP so what data and how the data is exposed is still to be decided. Since intersections can occur between points, that data is exposed by the Factor and Length. It's useful to have this available for testing etc. Offset mode which was proposed on the old system could be replaced by using the Direction data for example. Curve index provides data to determine which curve the intersection belongs to.
This pull request has changes conflicting with the target branch.
  • source/blender/blenkernel/BKE_node.h
  • source/blender/makesrna/intern/rna_nodetree.cc
  • source/blender/nodes/geometry/node_geometry_register.cc
  • source/blender/nodes/geometry/node_geometry_register.hh

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u gn-curve-intersections:CharlieJolly-gn-curve-intersections
git checkout CharlieJolly-gn-curve-intersections
Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#109393
No description provided.