Skip to content

Distance structs TODO item#213

Closed
nsmela wants to merge 3 commits intogradientspace:dotnet8from
nsmela:distance-structs
Closed

Distance structs TODO item#213
nsmela wants to merge 3 commits intogradientspace:dotnet8from
nsmela:distance-structs

Conversation

@nsmela
Copy link
Copy Markdown

@nsmela nsmela commented Jan 6, 2026

Had some spare time, tried to handle a simple TODO item.

Classes within /distance converted to struct, aside from static Distance. Considering adding an Obsolete attribute to it?

MeshQueries hit a snag: it returns null on lines 17, 65, and 123. Structs cannot be null and I think wrapping with nullable defeats purpose of switching to struct. For now, returning a new struct.

Taking a guess on what you'd prefer to in the future for MeshQueries:

  • returns ResultOrFail
  • out parameter: public static void TriangleDistance(DMesh3 mesh, int ti, Vector3d point, out DistPoint3Triangle3 value)
  • Create an empty version of each struct
  • output (bool success, DistPoint3Triangle3 result)
  • int ti for triangle index becomes a struct that must be valid to be made. The mesh it is coming from would create it. This would move the failure closer to the start of the operation:
public struct TriangleId(int id);

public ResultOrFail<TriangleId> DMesh3.GetTriangleId(int index);

@nsmela
Copy link
Copy Markdown
Author

nsmela commented Jan 6, 2026

Ran some tests, found a bug. It may have existed before:

Test_DistPoint2Circle2
Message: 

Expected: 5.0d +/- 9.9999999999999995E-07d

But was: 7.0710678118654755d

Off by: -2.0710678118654755d

Gemini's explanation:

In geometry3Sharp, adding a scalar (double) to a Vector2d adds that scalar to both components ($x+s, y+s$).
For a radius of 5, this resulted in (0,0) + 5 = (5,5).
The distance from center (0,0) to (5,5) is $\sqrt{5^2 + 5^2} \approx 7.071$, causing the test failure.

CircleClosest = circle.Center;
CircleClosest.x += circle.Radius;

@rms80
Copy link
Copy Markdown
Contributor

rms80 commented Jan 9, 2026

yes that's a bug, make you can make a separate PR for it as I can't accept this entire thing right now.

This is a major breaking change in the library API, because something that used to be a by-reference type is changed to a by-value type. IMO doing it w/ structs is better, but a lot of code is written assuming they are classes, and changing to by-value potentially could introduce subtle bugs in library user code (eg if someone is passing the object to a function that initializes it, that wouldn't work w/ a struct unless it's the argument is changed to be passed as ref, etc).

The TODO was basically a note to myself to spend some time thinking about this. IE do I want to say that the new 'dotnet8' version is not backwards-compatible w/ the old version, and fix mistakes like this, or be safe and do things like make the struct versions have new names and keep/deprecate the class versions, etc. Also there is a bunch of internal usage in the library of these types that needs to be investigated / cleaned-up.

So, appreciate the effort but I don't have time to think about all this right now, sorry. I will keep the PR open in case I get motivated on some weekend.

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Jan 9, 2026

CLA assistant check
All committers have signed the CLA.

@nsmela
Copy link
Copy Markdown
Author

nsmela commented Jan 9, 2026

Understood!

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.

3 participants