0

In my program I am dealing with tracking device movement with CMMotionManager and use quaternion representation of device attitude. For positioning the device in global coordinate system I need to do some basic calculations involving quaternion. For that I want to write some Quaternion class with all needed functions implemented. Does somebody know what is the proper way or maybe some general guidelines how it should be done. I've found a sample project from Apple which has Quaternion, Vector2 and Vector3 classes written in cpp, but I think that it's not very easy to use them in Cocoa, since I can't define properties of the object in Obj-C header file using these c++ classes. Or am I wrong? Thank you.

BartoNaz
  • 2,743
  • 2
  • 26
  • 42

2 Answers2

1

You're quite possibly not interested in OpenGL at all but as part of GLKit, Apple supplies GLKQuaternion. It has a C interface but should be easy to extrapolate into a class. Using it is recommended over expressing the mathematics directly since it likely uses the maths vector unit as fully as possible.

Alternatively you can declare C++ and Objective-C uses of GLKQuaternion directly since both superset C.

Tommy
  • 99,986
  • 12
  • 185
  • 204
1

You're asking multiple questions here. How to implement a Quaternion class, how to use them to represent orientation (attitude), how to integrate a C++ implementation with Objective C, how to integrate all that into Cocoa. I'll answer the last with a question: Does Cocoa really need to know what you are using under the hood to represent attitude?

There are lots of existing packages out there that use quaternions to represent orientation in three dimensional space. Eigen is one. There are lots of others. Don't reinvent the wheel. There are some gotchas that you do need to beware of, particularly when using a third party package. See View Matrix from Quaternion . If you scroll down and look at some of the other answers, you'll see that two of the packages mentioned by others are subject to the very issues I talked about in my answer.

Note well: I am not saying don't use quaternions. That would be rather hypocritical; I use them all the time. They work very nicely as a compact means of representing rotation. You just need to beware of issues, mainly because too many people who use them / implement software for them are clueless regarding those issues.

Community
  • 1
  • 1
David Hammen
  • 32,454
  • 9
  • 60
  • 108
  • Thank you for the link. That's really useful. But maybe you have also some link to the guidelines on how the different mathematical structures should be implemented to be easily used in Cocoa? As far as I know, if I'd like to have a representation of a 3d vector, I could implement it as a struct with 3 components, but if I want to define some mathematical operations with them, I'm not sure how it is usually done. – BartoNaz Oct 16 '12 at 18:12