You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
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.
@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?
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.
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:
This particular size is widely used in 2D graphics programming
We already had many APIs with a precedent for operating on 3x2 matrices (eg. System.Drawing and WPF)
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.
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.
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
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
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.
@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?
Activity
miloush commentedon Jan 27, 2016
Or if Matrix4x4 supported multiplication by Vector4, it could be used instead with M44 = 1 and W = 0.
mellinoe commentedon Jan 27, 2016
@shawnhar how have you generally been solving this in Win2D?
This seems like a fairly reasonable operation to support to me.
shawnhar commentedon Jan 27, 2016
You can transform Vector3 and Vector4 values by a Matrix4x4:
miloush commentedon Jan 27, 2016
@shawnhar I don't think that does the job
Vector3.Transformdoesv.X * m.M11 + v.Y * m.M21 + v.Z * m.M31 + m.M41Vector3.TransformNormaldoesv.X * m.M11 + v.Y * m.M21 + v.Z * m.M31Vector4.Transformdoesv.X * m.M11 + v.Y * m.M21 + v.Z * m.M31 + v.W * m.M41What you need is matrix by vector:
v.X * m.M11 + v.Y * m.M12 + v.Z * m.M13 (+ v.W * m.14)mellinoe commentedon Jan 27, 2016
Ah yes sorry, multiplications are exposed as
Vector2/3/4.Transform, not onMatrix4x4; had a bit of a brain freeze because I skimmed through the wrong type.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 usingVector4.Transform), but I don't think that's what you want.shawnhar commentedon Jan 27, 2016
@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?
miloush commentedon Jan 27, 2016
Transforming the matrix and doing
Vector.Transformof 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.
shawnhar commentedon Jan 28, 2016
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:
mellinoe commentedon Feb 3, 2016
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.
JimBobSquarePants commentedon Mar 22, 2017
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
Vector3andMatrix4x4it's awkward to do so as you have to ensure thatM44is set to1for 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
Matrix3x3would be perfect for this, aMatrix4x4much 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
mellinoe commentedon Mar 22, 2017
There's no plans to add any types right now, especially to the set of things in System.Numerics.Vectors.
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.
JimBobSquarePants commentedon Mar 22, 2017
@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?
shawnhar commentedon Mar 22, 2017
I would generally recommend using the 4x4.
JimBobSquarePants commentedon Mar 22, 2017
Thanks @shawnhar I thought that would be the case.