0

I need to do quite a bit 2D geometry work and have found a library that is absolutely perfect for my needs. The specific library is MathNet.Spatial which along with offering the exact functionality that I need, it integrates perfectly with the numeric library MathNet.Numerics which makes this an even better choice.

I have one problem though, I need to be able to subclass one type in the MathNet.Spatial library which is impossible as every type is implemented as a struct. It is a requirement that it can be derived from, I have no input on the matter. The type I need to subclass is MathNet.Spatial.Euclidean.Point2D, which as you can imagine every other type relies on. It is the center of the circle type, the terminal points of the lineSegment type, etc.

I am completely new to C# and I am required to implement this project in C#, so there is much I don't fully understand. The rest of the much larger project targets .NET 7, the library targets considerably older versions so I am also required to make the necessary changes so that it targets .NET 7, has all the method signatures brought up to date for the interfaces and so on. Essentially I have to modify this into a C# library that can run on Linux, Windows, MacOS, iOS, and Android.

Right now my plan is change `Point2D' from a struct to a class. I have read the source to the rest of the library any that should not present any problems that I can see, but as I said I'm new to C#. Is there anything that is going to blow up in my face that being new I am unaware of? I know nullable reference types will be something I have to be keenly aware of.

One problem I am aware of is serialization, for instance 'Point2D' has the following:

    public static Point2D ReadFrom(XmlReader reader)
    {
        return reader.ReadElementAs<Point2D>();
    }

which works for value types but not reference types. I simply do not know enough about XmlSerialization to properly implement `IXmlSerializable' either.

As Point2D is a currently a struct the following methods for IXmlSerializable work, but will not when changed to a class.

 void IXmlSerializable.ReadXml(XmlReader reader)
    {
        if (reader.TryReadAttributeAsDouble("X", out var x) &&
            reader.TryReadAttributeAsDouble("Y", out var y))
        {
            reader.Skip();
            this = new Point2D(x, y);
            return;
        }

        if (reader.TryReadChildElementsAsDoubles("X", "Y", out x, out y))
        {
            reader.Skip();
            this = new Point2D(x, y);
            return;
        }

        throw new XmlException("Could not read a Point2D");
    }

Point2D has only two fields total, an X and Y coordinate, each is a double. Is it as simple as modifying a few lines to:

        void IXmlSerializable.ReadXml(XmlReader reader)
    {
        if (reader.TryReadAttributeAsDouble("X", out var x) &&
            reader.TryReadAttributeAsDouble("Y", out var y))
        {
            reader.Skip();
            //this = new Point2D(x, y);
            X = x;
            Y = y;
            return;
        }

        if (reader.TryReadChildElementsAsDoubles("X", "Y", out x, out y))
        {
            reader.Skip();
            //this = new Point2D(x, y);
            X = x;
            Y = y;
            return;
        }

        throw new XmlException("Could not read a Point2D");
    }

I have a suspicion that eventually the requirements will change and I will have to modify every type from a struct to a class. I'm not really sure why you would want to derive from a 2D geometric vector, but these decisions are not made by me. The rational for wanting to be able to subclass Point2D is graph theory related. At the very minimum someone wants the ability to derive a type from Point2D to be used as a node in a graph, so they need to be able to add an adjacency list or a list of directed edges. Ultimately some moderately complex planar geometry needs to be done but also be able to do some analysis on the output from a graph theoretic view.

Any and all input would be greatly appreciated.

0 Answers0