Skip to content

Add Matrix3x3 #16226

Closed
Closed

Description

@miloush

I am working on a project involving graphics & Win2D and I would welcome to have an invertible Matrix3x3 type, mainly for transformations between color spaces. Anyone else?

Activity

  1. miloush commented on Jan 27, 2016

    @miloush
    ContributorAuthor

    Or if Matrix4x4 supported multiplication by Vector4, it could be used instead with M44 = 1 and W = 0.

  2. mellinoe commented on Jan 27, 2016

    @mellinoe
    Contributor

    @shawnhar how have you generally been solving this in Win2D?

    if Matrix4x4 supported multiplication by Vector4

    This seems like a fairly reasonable operation to support to me.

  3. miloush commented on Jan 27, 2016

    @miloush
    ContributorAuthor

    @shawnhar I don't think that does the job
    Vector3.Transform does v.X * m.M11 + v.Y * m.M21 + v.Z * m.M31 + m.M41
    Vector3.TransformNormal does v.X * m.M11 + v.Y * m.M21 + v.Z * m.M31
    Vector4.Transform does v.X * m.M11 + v.Y * m.M21 + v.Z * m.M31 + v.W * m.M41

    What you need is matrix by vector:
    v.X * m.M11 + v.Y * m.M12 + v.Z * m.M13 (+ v.W * m.14)

  4. mellinoe commented on Jan 27, 2016

    @mellinoe
    Contributor

    Ah yes sorry, multiplications are exposed as Vector2/3/4.Transform, not on Matrix4x4; had a bit of a brain freeze because I skimmed through the wrong type.

    What you need is matrix by vector:
    v.X * m.M11 + v.Y * m.M12 + v.Z * m.M13 (+ v.W * m.14)

    Are you sure that is what you need? It seems that if you have your color-space transformation matrix, you should be able to just multiply your colors by that matrix (Vector4.Transform(color, matrix)) to move from one space to the other. I think your last equation could also be computed by transposing your matrix (and then using Vector4.Transform), but I don't think that's what you want.

  5. shawnhar commented on Jan 27, 2016

    @shawnhar
    Contributor

    @miloush Vector3.Transform or TransformNormal are exactly what you want here (these are the same thing except if the matrix includes a translation component, which I assume yours does not since you mention it could also be expressed as a 3x3).

    System.Numerics uses row-major matrices with pre-multiplication ordering. Perhaps you are used to some other matrix implementation that adopted a different convention?

  6. miloush commented on Jan 27, 2016

    @miloush
    ContributorAuthor

    Transforming the matrix and doing Vector.Transform of course does it. Indeed, apparently I was looking for row-major post-multiplication.

    So for my original scenario there seems to be a workaround with Matrix4x4. Looks like the library has been designed mainly for graphics purposes. I still believe 3x3 would be useful for more mathematical-oriented tasks, though I can see the confusion for people who would want to use it as 3x2 one.

    If anything I believe the convention (and perhaps intention of the matrices) should be present in the documentation.

  7. shawnhar commented on Jan 28, 2016

    @shawnhar
    Contributor

    There is a tension in the design of a numerics library between including all potentially useful matrix dimensions, versus streamlining the set. 3x3 occasionally comes up - also 4x3 is useful in some situations. But there is a cost (of runtime size and API complexity as well as implementation cost) to expanding all these out. A larger matrix type like 4x4 is a strict superset of 3x3 and 4x3, so no functionality is lost by rounding all such use cases up to just use a 4x4 instead. And with SIMD codegen, there is not usually any performance hit either - in fact the 4x4 version may run faster due to better alignment.

    We decided to include 3x2 matrices for three reasons:

    1. This particular size is widely used in 2D graphics programming
    2. We already had many APIs with a precedent for operating on 3x2 matrices (eg. System.Drawing and WPF)
    3. Unlike intermediate size matrices, some operations can execute significantly faster with 3x2 rather than 4x4, as things like invert collapse down to very trivial calculations.
  8. mellinoe commented on Feb 3, 2016

    @mellinoe
    Contributor

    I think @shawnhar covered the main reasons we don't have 3x3 (or specific sizes than 4x4 and 3x2). The current types are intended to cover mainline graphics scenarios, and to provide an easy migration path for other API's (XNA, etc.), but we don't have support for every fixed size matrix.

    I also think that other matrix sizes (3x3 included) are useful from a generic mathematical perspective. However, we'd probably want to give a more holistic look at such a feature, rather than add more specific (fixed-size) types which again might not cover all uses. I'm going to close this as it seems like your original use case was solved above, but feel free to open other specific issues if you find deficiencies in the scenarios we support.

  9. JimBobSquarePants commented on Mar 22, 2017

    @JimBobSquarePants

    Hi @mellinoe

    Does the closing of this issue mean that we will most likely not see the inclusion of a 3x3 matrix?

    I ask because I think that an important point has been missed here. That of API consumption for the general public.

    While it is possible to do the relevant calculations (Multiplication, Invert) for colorspace transforms using Vector3 and Matrix4x4 it's awkward to do so as you have to ensure that M44 is set to 1 for the latter.

    If I wanted to create a class library that supported accurate transforms using Chromatic Adaptation matrices in conjunction with ICC profiles (which I am as part of ImageSharp) I have to expose the various matrices, Bradford etc to the public API. A Matrix3x3 would be perfect for this, a Matrix4x4 much less so. It's clunky to consume, and I also believe it will hurt performance.

    This, to me, would be considered a mainline use case within graphics processing. It hasn't been done before now with C# simply because the tooling wasn't there in traditional Windows graphics API's

    Cheers

    James

  10. mellinoe commented on Mar 22, 2017

    @mellinoe
    Contributor

    Does the closing of this issue mean that we will most likely not see the inclusion of a 3x3 matrix?

    There's no plans to add any types right now, especially to the set of things in System.Numerics.Vectors.

    This, to me, would be considered a mainline use case within graphics processing. It hasn't been done before now with C# simply because the tooling wasn't there in traditional Windows graphics API's

    There are certainly some existing C# graphics API's which include extra things like 3x3 matrix types (SharpDX, OpenTK). But there are a huge number of numeric types of different shapes and sizes (just take a look at https://github.com/sharpdx/SharpDX/tree/master/Source/SharpDX.Mathematics) that could potentially be added for various different applications.

    This is not to say that a 3x3 matrix isn't useful or a good structure to use. It sounds like you've got the perfect use case for it, but I'm not sure that means we need to add another specialized matrix type to the BCL if it wouldn't be often-used.

  11. JimBobSquarePants commented on Mar 22, 2017

    @JimBobSquarePants

    @mellinoe Yeah, there are definitely a lot of different shapes out there. Most of those are extremely specialized though whereas the 3x3 matrix has more generic applications.

    If one isn't to be added what would your advice be - To roll my own or use 4x4?

  12. shawnhar commented on Mar 22, 2017

    @shawnhar
    Contributor

    I would generally recommend using the 4x4.

  13. JimBobSquarePants commented on Mar 22, 2017

    @JimBobSquarePants

    Thanks @shawnhar I thought that would be the case.

  14. transferred this issue fromdotnet/corefxon Jan 31, 2020
  15. added this to the 1.0.0-rtm milestone on Jan 31, 2020
  16. ghost locked as resolved and limited conversation to collaborators on Jan 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-needs-workarea-System.Numerics

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions