recp / cglm

📽 Highly Optimized 2D / 3D Graphics Math (glm) for C
MIT License
2.21k stars 228 forks source link

glm::adjugate ? #246

Open ghost opened 2 years ago

ghost commented 2 years ago

Hello there,

I am currently using cglm in my game engine and was wondering if there is an implementation of adjugate in cglm? here how the function looks like from glm C++ library

image

Thank you.

recp commented 2 years ago

Hi @wobes1

Thanks for your feedback,

We can add this function to cglm, I will work on it asap if anyone wont before me

It is similar to inverse just without determinant division

ghost commented 2 years ago

Thank you for your reply @recp

Wow, I really appreciate the reactivity! Sorry I couldn't contribute myself, I lack proper knowledge of cglm and complex matrices operations, but will be happy to learn

Cheers

EDIT:

oh so just without this part? image

ghost commented 2 years ago

Yes I believe this is it:

CGLM_INLINE
void glm_mat3_adjugate(mat3 mat, mat3 dest) {
  float det;
  float a = mat[0][0], b = mat[0][1], c = mat[0][2], d = mat[1][0], e = mat[1][1], f = mat[1][2],
        g = mat[2][0], h = mat[2][1], i = mat[2][2];

  dest[0][0] = e * i - f * h;
  dest[0][1] = -(b * i - h * c);
  dest[0][2] = b * f - e * c;
  dest[1][0] = -(d * i - g * f);
  dest[1][1] = a * i - c * g;
  dest[1][2] = -(a * f - d * c);
  dest[2][0] = d * h - g * e;
  dest[2][1] = -(a * h - g * b);
  dest[2][2] = a * e - b * d;
}

Please let me know if something is off. Thank you!

recp commented 2 years ago

Yes that's it! But with correct indents/alignment ;)

CGLM_INLINE
void
glm_mat3_adjugate(mat3 mat, mat3 dest) {
  float a = mat[0][0], b = mat[0][1], c = mat[0][2],
        d = mat[1][0], e = mat[1][1], f = mat[1][2],
        g = mat[2][0], h = mat[2][1], i = mat[2][2];

  dest[0][0] =   e * i - f * h;
  dest[0][1] = -(b * i - h * c);
  dest[0][2] =   b * f - e * c;
  dest[1][0] = -(d * i - g * f);
  dest[1][1] =   a * i - c * g;
  dest[1][2] = -(a * f - d * c);
  dest[2][0] =   d * h - g * e;
  dest[2][1] = -(a * h - g * b);
  dest[2][2] =   a * e - b * d;
}

maybe glm_mat3_adj() could be used for the name but not sure.