the Fiddler.'s picture

Scope of OpenTK.Math

Continuing the discussion from the "Math module?" topic which has started growing too long.

Inertia said: This brings up an interesting question: What's the real scope of the mathlib?
I haven't really given this much thought, but the general idea was to build a set of functions useful for 3d graphics. Not physics or anything of that sort, but I can imagine it containing noise functions, geometric primitives (useful for bounding volumes, projections etc) or even a tri-strip generator. I don't have a concrete plan though, and this is not high priority stuff at this point.

What do you think the math toolkit should contain? Should it stay simple and focused (vectors, matrices, quaternions and that's it), or should it grow as needed by projects that use it?


Comments

Comment viewing options

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

The thing is, OpenTK aims to be more than a simple wrapper but short of a 3d engine. That is, it should include commonly used functionality (like fonts!) but be low-level enough that it doesn't get in the way of building a higher level system.
This is how I understood it aswell, a Foundation to build your custom Structures upon. Providing an elegant interface to OpenGL/AL in the Core and commonly used functionality like loading/saving Images/Sounds from File, Fonts, Noise, etc. in the Utilities.

Most higher level demands like Texture Managers, Collision Testing or Model loading might be best off in wiki/tutorial example codes, since the requirements to such components vary wildly.

JTalton's picture

Removed double post.

JTalton's picture

I'll put together my math stuff. I need some suggestions though.

* Structures vs Classes
It is recommended that structures are 12 to 16 bytes or less.
Vector2f == 8 bytes
Vector3f == 12 bytes
Vector4f == 16 bytes
Vector2d == 16 bytes
Vector3d == 24 bytes
Vector4d == 32 bytes
Matrix3f == 36 bytes
Matrix4f == 64 bytes
Matrix3d == 72 bytes
Matrix4d == 128 bytes
Quaternionf == 16 bytes
Quaterniond == 32 bytes

Structures can be put on the stack while classes are on the heap.
Structures are copied while classes are reference counted.

There are benefits to both and I think the 16 byte limit refers to where you start losing performance when passing variables. I would not want to make some structs and some classes, because they would behave differently.

My code is currently classes, while the current OpenTK.Math is vectors. I am leaning toward classes unless anyone has objections.

* Class names
Do the class names above look good? Or should the floating point be Vector3 while the double precision be Vector3d?

* Interfaces
My classes currently implement IEquatable<>, IComparer<>, and IComparable<>.
Any others?

the Fiddler.'s picture

* Names
Let's keep the single precision version clean and the add a 'd' in the double version. Looks better and an 'f' provides no real benefit.

* classes vs structs:
This was discussed that at length and decided to implement eveything in structs, but provide overloads with 'ref' and 'out' parameters to avoid copying memory around. I.e.

Vector2d Add(Vector2d, Vector2d) { } // Slow version
void Add(ref Vector2d, ref Vector2d, out Vector2d) { } // Fast version

Since we don't use inheritance, and we benefit from the improved locality of reference structs provide, there is little reason to use classes. Tests showed that struct references are a little faster than classes, too.

* Interfaces
Can't think of anything else.

I'll add the necessary overloads in GL/Glu once these are complete.

Mantheren's picture

In converting my camera class from DirectX to an OpenTK version I could not find a Plane class (which I use for the frustum) in the Math lib.

It was not entirely clear to me, but is there a plan for a Plane class to be added?
Or should I not wait for this and just go about finding an alternate solution?

Thanks,
Mantheren

the Fiddler's picture

The math lib is mostly built on contributed code (Vectors, Matrices, Beziers). The idea is that whenever someone implements (are has implemented) a new structure (e.g. Planes, Splines etc), he can donate it to help other people who might need the same thing.

It happens that noone needed a Plane class until now. There's a good chance you can find something useful in the Axiom Engine, Math.NET or even the SlimDX source code. If nothing works as you'd like it to and you happen to roll your own class, you are welcome to donate the code (of course you are not expected to do so if you don't wish to).

Kamujin's picture

Here is my plane class. I think I poached it from Axiom. Hopefully, this help.

	public struct Plane
	{
		public Vector3 Normal;
		public float D;
 
		private static readonly Plane nullPlane = new Plane(Vector3.Zero, 0);
 
		public Plane(Plane plane)
		{
			this.Normal = plane.Normal;
			this.D = plane.D;
		}
 
		public Plane( Vector3 normal, float constant )
		{
			this.Normal = normal;
			this.D = -constant;
		}
 
		public Plane( Vector3 normal, Vector3 point )
		{
			this.Normal = normal;
			this.D = -Vector3.Dot(normal, point);
		}
 
		public Plane( Vector3 point0, Vector3 point1, Vector3 point2 )
		{
			Vector3 edge1 = point1 - point0;
			Vector3 edge2 = point2 - point0;
			Normal = Vector3.Cross(edge1, edge2);
			Normal.Normalize();
			D = -Vector3.Dot(Normal, point0);
		}
 
		public float GetDistance( Vector3 point )
		{
			return Vector3.Dot(Normal, point) + D;
		}
	}
objarni's picture

Are classes such as Plane put into TK or TKU?

Mantheren's picture

Thanks for the info,

In that case I think I will have a go at trying to create a relatively complete Plane struct.
@Kamujin: I never needed GetDistance yet, but it seems handy so I'll add that as well if you don't mind, thanks. :)

Of course I will gladly donate it back, so what is the preferred way to post it? I see I can't add attachments to posts right?

-- Edit --
Oh wait I see now, I can only attach to new topics, and not in replies to topics, so never mind...
-- End Edit --