简体   繁体   中英

Why is there no Matrix3x3 in the System.Numerics namespace? c#

I am trying to make a little GUI library in c#, but after doing some research on matrix transformations I found out that I need a Matrix3x3 to store rotation, scale, and translation of a Vector2. But in the C# System.Numerics there is only a Matrix3x2 or Matrix4x4? Could I use one of those instead? If so how would I go about it? And why isnt there a Matrix3x3 in the standard library?

I am very new to Matrix and Vector programming, So sorry if this is a stupid question.

Thanks in advance.

You can use Matrix4x4 . Start with an identity matrix and fill the top left 3×3 elements with your matrix.

For example to solve a 3×3 system of equations:

// Find the barycentric coordinates
//
// Solve for (wA,wB,wC):
// | px |   | ax bx cx | | wA |
// | py | = | ay by cy | | wB |
// | pz |   | az bz cz | | wC |

var m = new Matrix4x4(
    A.X, B.X, C.X, 0,
    A.Y, B.Y, C.Y, 0,
    A.Z, B.Z, C.Z, 0,
    0, 0, 0, 1);

if (Matrix4x4.Invert(m, out Matrix4x4 u))
{
    var w = new Vector3(
        u.M11*P.X + u.M12*P.Y + u.M13*P.Z,
        u.M21*P.X + u.M22*P.Y + u.M23*P.Z,
        u.M31*P.X + u.M32*P.Y + u.M33*P.Z);

    // ...
}

As for the reasons, the intent of System.Numerics has to be related to computer graphics since it utilizes Homogeneous Coordinates in which 3D vectors contain 4 elements. Three regular coordinates and a scalar weight factor. The math with homogeneous coordinates for computer graphics is vastly simplified. The only reason there is a Vector3 is because a Vector4 should be treated as a vector of 3 elements plus a scalar, and thus Vector3 should be used in composition and decomposition of the homogeneous coordinates. It means that not all 4 elements can be treated equally, and sometimes you need to do things with the vector (first three elements) separately from the scalar value (fourth element).

Also System.Numerics uses single precision float elements, which are almost never used in scientific computing, but universally applied to computer graphics for their speed. Hopefully one day when the CLR supports AVX512 there will be double precision numeric intrinsics that scientists can actually use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM