Skip to content

Fix centroid calculation to return bounding box center for multiple vectors#3545

Closed
daniil-loban wants to merge 2 commits intoGraphiteEditor:masterfrom
daniil-loban:fix_centroid_calculation
Closed

Fix centroid calculation to return bounding box center for multiple vectors#3545
daniil-loban wants to merge 2 commits intoGraphiteEditor:masterfrom
daniil-loban:fix_centroid_calculation

Conversation

@daniil-loban
Copy link
Copy Markdown
Contributor

@daniil-loban daniil-loban commented Dec 30, 2025

I noticed that the centroid for the Bounding Box node is calculated incorrectly when there are multiple shapes passed via Flatten Path. However, the rectangle itself is calculated correctly. It seemed logical to me to use the bounding_box call to get the rectangle, since I believe its center will be the correct centroid.

Result:
after

Further calculations may not be necessary, but I left them in just in case.

Fixes #3544

return (min + max) / 2.;
}

// Fallback to the original method if bounding box calculation fails
Copy link
Copy Markdown
Contributor

@0HyperCube 0HyperCube Dec 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bounding box only fails if there are no segments to take the bounding box. In this case, the code below isn't going to work either.

Essentially this changes the implementation to just always return the centre of the bounding box.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I need to delete the following code in this case?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As kindly pointed out by elbertronnie on #3544, there was a typo in the pathseg_to_parametric_polynomial function that caused incorrect results. With the fix in #3562 it is now resolved:

result with star in centroid node graph

Copy link
Copy Markdown
Contributor Author

@daniil-loban daniil-loban Jan 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may be wrong about the concept, but in my opinion the centroid is not where it should be.
I think that the general centroid should act according to the same principles as the single centroid — find the center.

image

Copy link
Copy Markdown
Member

@Keavon Keavon Jan 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bottom left shape has the largest area so the center of mass will be concentrated towards it, as we observe.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Centroid computes the center of mass. It sounds like you are expecting the center of the bounding box. You can add a Bounding Box node before Centroid to get the center of mass of the rectangular bounding box, which would have your desired effect.

Copy link
Copy Markdown
Contributor Author

@daniil-loban daniil-loban Jan 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Centroid computes the center of mass. It sounds like you are expecting the center of the bounding box. You can add a Bounding Box node before Centroid to get the center of mass of the rectangular bounding box, which would have your desired effect.

Perhaps then it's worth simply creating an additional node that will calculate the geometric center?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is planned, yes. Feel free to also open an issue to track that so we don't forget. Thanks!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@daniil-loban If you have the bounding box -> centroid then it will find the centre of the bounding box.

image image

@Keavon Keavon changed the title Fix centroid calculation to return bounding box center for multiple v… Fix centroid calculation to return bounding box center for multiple vectors Dec 31, 2025
@daniil-loban
Copy link
Copy Markdown
Contributor Author

Changing the centroid type to Length works (If you just want the green rectangle on top of the pink one)

Ezbaze

@daniil-loban daniil-loban deleted the fix_centroid_calculation branch January 2, 2026 08:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect calculations of a centroid position

3 participants