0

How would you normally compare two GLKMatrix4, or at least check if on is the identity matrix?

A cursory search of GLKMatrix4.h shows no util function, and I was feeling silly checking every field manually like this:

static BOOL GLKMatrix4EqualToMatrix4(GLKMatrix4 a, GLKMatrix4 b)
{
  return
  a.m00 == b.m00 &&
  a.m01 == b.m01 &&
  a.m02 == b.m02 &&
  a.m03 == b.m03 &&
  a.m10 == b.m10 &&
  a.m11 == b.m11 &&
  a.m12 == b.m12 &&
  a.m13 == b.m13 &&
  a.m20 == b.m20 &&
  a.m21 == b.m21 &&
  a.m22 == b.m22 &&
  a.m23 == b.m23 &&
  a.m30 == b.m30 &&
  a.m31 == b.m31 &&
  a.m32 == b.m32 &&
  a.m33 == b.m33;
}
hpique
  • 119,096
  • 131
  • 338
  • 476

2 Answers2

1

You can transform it into string and then compare using this function NSStringFromGLKMatrix4

user1917506
  • 41
  • 1
  • 4
0

You can do:

static BOOL GLKMatrix4EqualToMatrix4(GLKMatrix4 a, GLKMatrix4 b) {
    return memcmp(a.m, b.m, sizeof(a.m)) == 0;
}

Because memcmp is usually highly optimized for specific architectures, it should be the fastest, and cleanest approach.

See Why is memcmp so much faster than a for loop check? for discussion on memcmp.

Will Powers
  • 155
  • 1
  • 6