groverburger/g3d

Would these matrix functions be good to add?

expikr opened this issue · 1 comments

expikr commented
local function MatVec3(A,x,y,z)
   return 
      A[1]*x + A[2]*y + A[3]*z ,
      A[4]*x + A[5]*y + A[6]*z ,
      A[7]*x + A[8]*y + A[9]*z
end

local function MatMat3(out,L,R)
   out[1],out[4],out[7] = MatVec3(L,R[1],R[4],R[7])
   out[2],out[5],out[8] = MatVec3(L,R[2],R[5],R[8])
   out[3],out[6],out[9] = MatVec3(L,R[3],R[6],R[9])
end

local function MatMat3_unrolled(out,L,R)
   out[1] = L[1]*R[1] + L[2]*R[4] + L[3]*R[7]
   out[2] = L[1]*R[2] + L[2]*R[5] + L[3]*R[8]
   out[3] = L[1]*R[3] + L[2]*R[6] + L[3]*R[9]
   
   out[4] = L[4]*R[1] + L[5]*R[4] + L[6]*R[7]
   out[5] = L[4]*R[2] + L[5]*R[5] + L[6]*R[8]
   out[6] = L[4]*R[3] + L[5]*R[6] + L[6]*R[9]
   
   out[7] = L[7]*R[1] + L[8]*R[4] + L[9]*R[7]
   out[8] = L[7]*R[2] + L[8]*R[5] + L[9]*R[8]
   out[9] = L[7]*R[3] + L[8]*R[6] + L[9]*R[9]
end

local function MatMat3_unpacked(L,R)
   return 
      L[1]*R[1]+L[2]*R[4]+L[3]*R[7] , L[1]*R[2]+L[2]*R[5]+L[3]*R[8] , L[1]*R[3]+L[2]*R[6]+L[3]*R[9] ,
      L[4]*R[1]+L[5]*R[4]+L[6]*R[7] , L[4]*R[2]+L[5]*R[5]+L[6]*R[8] , L[4]*R[3]+L[5]*R[6]+L[6]*R[9] ,
      L[7]*R[1]+L[8]*R[4]+L[9]*R[7] , L[7]*R[2]+L[8]*R[5]+L[9]*R[8] , L[7]*R[3]+L[8]*R[6]+L[9]*R[9]
end

local function MatVec4(A,x,y,z,w)
   return 
      A[ 1]*x + A[ 2]*y + A[ 3]*z + A[ 4]*w,
      A[ 5]*x + A[ 6]*y + A[ 7]*z + A[ 8]*w,
      A[ 9]*x + A[10]*y + A[11]*z + A[12]*w,
      A[13]*x + A[14]*y + A[15]*z + A[16]*w
end

local function MatMat4(out,L,R)
   out[1],out[5],out[ 9],out[13] = MatVec4(L,R[1],R[5],R[ 9],R[13])
   out[2],out[6],out[10],out[14] = MatVec4(L,R[2],R[6],R[10],R[14])
   out[3],out[7],out[11],out[15] = MatVec4(L,R[3],R[7],R[11],R[15])
   out[4],out[8],out[12],out[16] = MatVec4(L,R[4],R[8],R[12],R[16])
end

local function MatMat4_unrolled(out,L,R)
   out[ 1] = L[ 1]*R[ 1] + L[ 2]*R[ 5] + L[ 3]*R[ 9] + L[ 4]*R[13]
   out[ 2] = L[ 1]*R[ 2] + L[ 2]*R[ 6] + L[ 3]*R[10] + L[ 4]*R[14]
   out[ 3] = L[ 1]*R[ 3] + L[ 2]*R[ 7] + L[ 3]*R[11] + L[ 4]*R[15]
   out[ 4] = L[ 1]*R[ 4] + L[ 2]*R[ 8] + L[ 3]*R[12] + L[ 4]*R[16]
   
   out[ 5] = L[ 5]*R[ 1] + L[ 6]*R[ 5] + L[ 7]*R[ 9] + L[ 8]*R[13]
   out[ 6] = L[ 5]*R[ 2] + L[ 6]*R[ 6] + L[ 7]*R[10] + L[ 8]*R[14]
   out[ 7] = L[ 5]*R[ 3] + L[ 6]*R[ 7] + L[ 7]*R[11] + L[ 8]*R[15]
   out[ 8] = L[ 5]*R[ 4] + L[ 6]*R[ 8] + L[ 7]*R[12] + L[ 8]*R[16]
   
   out[ 9] = L[ 9]*R[ 1] + L[10]*R[ 5] + L[11]*R[ 9] + L[12]*R[13]
   out[10] = L[ 9]*R[ 2] + L[10]*R[ 6] + L[11]*R[10] + L[12]*R[14]
   out[11] = L[ 9]*R[ 3] + L[10]*R[ 7] + L[11]*R[11] + L[12]*R[15]
   out[12] = L[ 9]*R[ 4] + L[10]*R[ 8] + L[11]*R[12] + L[12]*R[16]
   
   out[13] = L[13]*R[ 1] + L[14]*R[ 5] + L[15]*R[ 9] + L[16]*R[13]
   out[14] = L[13]*R[ 2] + L[14]*R[ 6] + L[15]*R[10] + L[16]*R[14]
   out[15] = L[13]*R[ 3] + L[14]*R[ 7] + L[15]*R[11] + L[16]*R[15]
   out[16] = L[13]*R[ 4] + L[14]*R[ 8] + L[15]*R[12] + L[16]*R[16]
end

local function MatMat4_unpacked(L,R)
   return 
      L[ 1]*R[ 1] + L[ 2]*R[ 5] + L[ 3]*R[ 9] + L[ 4]*R[13],
      L[ 1]*R[ 2] + L[ 2]*R[ 6] + L[ 3]*R[10] + L[ 4]*R[14],
      L[ 1]*R[ 3] + L[ 2]*R[ 7] + L[ 3]*R[11] + L[ 4]*R[15],
      L[ 1]*R[ 4] + L[ 2]*R[ 8] + L[ 3]*R[12] + L[ 4]*R[16],
      L[ 5]*R[ 1] + L[ 6]*R[ 5] + L[ 7]*R[ 9] + L[ 8]*R[13],
      L[ 5]*R[ 2] + L[ 6]*R[ 6] + L[ 7]*R[10] + L[ 8]*R[14],
      L[ 5]*R[ 3] + L[ 6]*R[ 7] + L[ 7]*R[11] + L[ 8]*R[15],
      L[ 5]*R[ 4] + L[ 6]*R[ 8] + L[ 7]*R[12] + L[ 8]*R[16],
      L[ 9]*R[ 1] + L[10]*R[ 5] + L[11]*R[ 9] + L[12]*R[13],
      L[ 9]*R[ 2] + L[10]*R[ 6] + L[11]*R[10] + L[12]*R[14],
      L[ 9]*R[ 3] + L[10]*R[ 7] + L[11]*R[11] + L[12]*R[15],
      L[ 9]*R[ 4] + L[10]*R[ 8] + L[11]*R[12] + L[12]*R[16],
      L[13]*R[ 1] + L[14]*R[ 5] + L[15]*R[ 9] + L[16]*R[13],
      L[13]*R[ 2] + L[14]*R[ 6] + L[15]*R[10] + L[16]*R[14],
      L[13]*R[ 3] + L[14]*R[ 7] + L[15]*R[11] + L[16]*R[15],
      L[13]*R[ 4] + L[14]*R[ 8] + L[15]*R[12] + L[16]*R[16]
end

Those would probably be good to add to your own personal library, but I don't think they would be helpful for g3d's goals. Thanks for asking!