iliak's picture

Maths function inheritance from one project to an other


I'm currently making a framework using OpenTK. Everything is OK, but now, I'm facing a problem...

OpenTK uses its own maths (Matrix4, Vector3...) and my framework tries to hide most of the internal and technical parts. I want the final project to link only with my framework (see adding reference to the project). The problem is how can I "let" final project use those maths functions ? I see two solutions :

- Struct inheritance : Not allowed in C#
- Copying all OpenTK maths source to my framework. The problem is when OpenTK update maths functions, I need to update mines... More, I have to cast structs...

Do you see any better one ?


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Hortus Longus's picture


Perhaps you can go the way over interface(s)?

iliak's picture

Can you elaborate a little bit ?

the Fiddler's picture

Interfaces have the disadvantage that they cause boxing when accessing structures through them. For a while I thought there was no good solution to this issue, but someone posted an ingenious hack on recently:

struct Vector2
    [FieldOffset(0)] public float X;
    [FieldOffset(4)] public float Y;
    [FieldOffset(0)] private OpenTK.Vector2 OpenTK_Vector2;
    public static implicit operator Vector2(OpenTK.Vector2 v)
        OpenTK_Vector2 = v;
    public static implicit operator OpenTK.Vector2(Vector2 v)
        return v.OpenTK_Vector2;

You can also define implicit operators to cast your Vector2 from/to any other library you care to support (XNA, from/to OpenTK.Vector2 (and XNA, SlimDX etc).

The downside is that you'll have to reimplement any operations you wish to support (I'd suggest using extension methods for this) but at least this gives you a simple way to interoperate with different math libraries.

iliak's picture

I quickly read the license of OpenTK and this is allowed that I copy some part of the source code to use them in my project. Is this right ?
If so, I can easily steal, *cough*... copy needed structs in my framework... With the hint given by The Fiddler (using implicit operators), the cast will be easy.

is it feasible ?

c2woody's picture

Please read the full license text. You are required to add notion of OpenTK's usage/parts of it, accompanied by the same licensing text. If in doubt ask some lawyers about the MIT/X11 licensing.

iliak's picture

From what I saw, I just have to let the header of each file and it's good, right ?

nythrix's picture

You can do pretty much anything with it except:
1) If you're redistributing part of OpenTK source do not remove the original license from the files.
2) If you're also offering binary only packages for download, an explicit "OpenTK License.txt" would be necessary. That's what I did a couple of months ago. No lawsuits yet :)

iliak's picture

Ok, thank you.

Experimenting Fiddler's suggestion...