1

Just for fun, I'm implementing from scratch my own Ray Tracer on C# .NET core, and this includes Vectors, Matrices, and other Liner Algebra artifacts.

Question: For Matrices, what should be the right implementation approach, considering these are going to be used for a Ray Tracer (I only need 2x2, 3x3 and 4x4 matrices) and keeping in mind performance?

  1. A Matrix class, you can define its size on the constructor. This means no code duplication, but everything will be stored in the heap, meaning garbage collector will be called frequently.
  2. Several structs for the different custom sizes I need, like Matrix2x2, Matrix3x3, Matrix4x4? Structs in C# doesn't have heritance support, so a lot of code will need to be duplicated across all the structures. On the positive side, these matrices will be stored in the stack.
  3. A mixed approach, following (2) and keep all the method signatures, but instead of having the logic there, using a helper static class that has all the logic to operate these structs. From a user perspective, you don't need to interact with this class, but I don't know if this will be an overkill.

NOTE: I don't want to use a library for this, that's the whole idea of "From Scratch" :)

I was exploring other libraries created on C#, and also game engines like Unity as reference. These libraries usually create several structs with a lot of duplicated code. I'm assuming this is acceptable because these structs should not be modified too often. I'm more inclined to do (2) but I would like to check what you think about approach (3)

For the structs, I can't use arrays, so I need to explicitly create N x N variables to hold the values of the matrix. The good thing that makes approach (3) feasible is the support for C# Indexers (An indexer allows an object to be indexed such as an array. When you define an indexer for a class, this class behaves similar to a virtual array), so from the point of view of the helper class, these will look like arrays, making possible to do logic that will work with any size (2x2, 3x3 and 4x4)

I will appreciate the input here.

Regards.

Bufofa
  • 23
  • 2
  • Please define "better". Better (OO) design? Better performance? Better memory usage? – Julian Jul 05 '19 at 16:32
  • XNA also uses a [struct](https://docs.microsoft.com/en-us/previous-versions/windows/silverlight/dotnet-windows-silverlight/bb197911(v%3Dxnagamestudio.35)). – itsme86 Jul 05 '19 at 16:36
  • Why not try those alternatives you mentioned and then measure the performance ? – auburg Jul 05 '19 at 16:37
  • 1
    Possible duplicate of [Struct or class for Matrix 4x4 object](https://stackoverflow.com/questions/24231389/struct-or-class-for-matrix-4x4-object) – GSerg Jul 05 '19 at 16:40
  • For me better means is the term of performance (including memory usage) I'm looking to use the struct approach, as mentioned in the Pros and Cons of https://stackoverflow.com/questions/24231389/struct-or-class-for-matrix-4x4-object, but I would like to understand if there are any red flags about the approach (3) – Bufofa Jul 05 '19 at 16:48

0 Answers0