Is there a Cocoa type for representing a 3D point

In Cocoa there is a NSPoint type to represent points in 2d-space. I can't find a way to represent a 3D type. Is there one and in that case what is it called? Or should I roll my own?


Right, make your own. I use something like this:

typedef struct Point3D_ {
  CGFloat x, y, z;
} Point3D;

Also do this to make creation of Point3D easier:

Point3D Point3DMake( CGFloat xx, CGFloat yy, CGFloat zz ){
  Point3D p;  
  p.x = xx; p.y = yy; p.z = zz;
  return p;

While I have yet to remember or find something in the core of AppKit or Cocoa that's directly analagous to CGPoint, Apple do provide some types within "extra" frameworks. Here's a collection of what I've got so far.


Apple's SceneKit provides a type SCNVector3, which is documented to be a simple struct:

typedef struct SCNVector3 { CGFloat x, y, z; } SCNVector3;

There are a few helper functions like SCNVector3Make and SCNVector3EqualToVector3 and one to convert between GLKit vectors.


Apple's GLKit provides a type GLKVector3, with even more features and helper functions.

union _GLKVector3 {
    struct { float x, y, z; };
    struct { float r, g, b; };
    struct { float s, t, p; };
    float v[3];
typedef union _GLKVector3 GLKVector3;

As you can see, it is a union of a number of types, including a simple x/y/z struct and an indexable array of floats — so once you create one you can conveniently access its value in many different ways as makes sense in your context(s).

Besides a simple GLKVector3Make, there's a number of geometric calculation helpers, e.g. GLKVector3Length and GLKVector3DotProduct and and GLKVector3Lerp.

This one seems like a nice one to use if you don't mind pulling in GLKit.


Apple's SpriteKit defines a vector_float3, which is a bit messier:

typedef __attribute__((__ext_vector_type__(3))) float vector_float3;

This uses a compiler extension (Clang and perhaps GCC too?) to define this as a SIMD-friendly type. But it doesn't seem to be particularly developer-friendly other than that.


I'm guessing this isn't a good choice in most cases, but Apple's CoreLocation provides a specialized multidimensional type for locations. CLLocation has properties for latitude/longitude/altitude (in addition to a timestamp, accuracy values, speed, course, etc.) So you could use this for 3D data, but only recommended if your data's relative to the earth's surface!

Need Your Help

Prevent browser scroll on HTML5 History popstate

javascript html5 javascript-events browser-history

Is it possible to prevent the default behaviour of scrolling the document when a popstate event occurs?