3

I'm looking at this

https://developer.apple.com/library/mac/documentation/SceneKit/Reference/SceneKit_Functions/index.html

Aren't basic operations supported?

var t:SCNVector3 = SCNVector3(x: 0,y: -1,z: 2)
var a:SCNVector3 = SCNVector3(x: 1,y: -2,z: -2)
var b:SCNVector3 = t + a

or do I need to create my own operations C-style?

Where are the math vector/matrix functions like transpose? inverse? add? blas? lapack functions?

Accelerate framework doesn't look compatible??

https://developer.apple.com/library/ios/documentation/Accelerate/Reference/BLAS_Ref/index.html#//apple_ref/doc/uid/TP30000414-SW9

I didn't find any official apple simd extensions for swift. I did find this on git hub https://github.com/noxytrux/SwiftGeom

David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
  • This question and the two 2015 answers are obsolete. There's now (as of iOS 11) built-in SIMD support in SceneKit. See the answers from @russ and from me below. – Hal Mueller Nov 29 '19 at 21:37

4 Answers4

5

You don't want to use tiny vectors like this with Accelerate framework, with the possible exception of vMathLib. You will lose your performance in overhead to check to see if the arrays and aligned and whether the problem is large enough to multithread.

Much of the SIMD level stuff you are looking for is in simd/simd.h and associated headers.

Also, Swift doesn't support SIMD vectors at the moment.

Ian Ollmann
  • 1,592
  • 9
  • 16
3

In Swift 2 (currently part of the Xcode 7 beta), Swift includes the vector and matrix types from the SIMD library. The interface is, for better or for worse, pretty much the same as the C version, but with better support for operators (i.e. you can use * instead of calling matrix_multiply, etc).


Types and functions from the SIMD library aren't in Swift 1.x, but in Xcode 6.3, the GLKit Math library is (at least somewhat) accessible from Swift 1.2. So, you can use, for example, SCNVector3ToGLKVector3 to convert to a GLK value, then use GLK math functions to do things like cross or project.

Note there isn't a builtin function to convert SCNMatrix4 to/from GLKMatrix4, but it's not too hard to write your own.

Community
  • 1
  • 1
rickster
  • 124,678
  • 26
  • 272
  • 326
3

There are now initialisers to convert e.g. SCNVector3 to simd_float3 and vice versa, then you can use simd operations:

let v1 = simd_float3(SCNVector3(x: 1.0, y: 2.0, z: 3.0))
let v2 = simd_float3(SCNVector3(x: 2.0, y: 3.0, z: 4.0))
let cross = simd_cross(v1, v2)
Russ
  • 31
  • 3
0

As of iOS 11 and macOS 10.13, there are SIMD accessors and functions for reading, setting, and transforming the various node properties and scene transforms. @Russ's answer gives an example, but you could also extract node positions directly, with something like:

let t = nodeT.simdPosition
let a = nodeA.simdPosition
let b = simdCross(t, a)

or even

let b = simdCross(nodeT.simdPosition, nodeA.simdPosition)
Hal Mueller
  • 7,019
  • 2
  • 24
  • 42