在 iOS/Swift 中,MatrixHelper 属于哪个模块?
In iOS/Swift, what module is MatrixHelper a part of?
我正在尝试遵循下面的一些示例代码 https://medium.com/@yatchoi/getting-started-with-arkit-real-life-waypoints-1707e3cb1da2,我得到了 unresolved identifier 'MatrixHelper'
let translationMatrix = MatrixHelper.translate(
x: 0,
y: 0,
z: distance * -1
)
// Rotation matrix theta degrees
let rotationMatrix = MatrixHelper.rotateAboutY(
degrees: bearing * -1
)
我需要导入什么库或包才能获得 MatrixHelper - 一些数学包?我用谷歌搜索了文档,但找不到任何东西。
我认为他只是写了一个自定义class来包装GLKit提供的功能。
他将 class MatrixHelper 命名为
使用 MatrixHelper.rotateAboutY() 他调用类似的东西:
GLKMatrix4 GLKMatrix4Rotate(GLKMatrix4 matrix, float radians, float x, float y, float z);
所以MatrixHelper中使用的包是GLKit,更准确的说是GLKMatrix4
https://developer.apple.com/documentation/glkit/glkmatrix4-pce
可以避免使用MatrixHelper(还有GLKit),使用simd,很简单。这些方法如下所示:
func getTranslationMatrix(tx: Float, ty: Float, tz: Float) -> simd_float4x4 {
var translationMatrix = matrix_identity_float4x4
translationMatrix.columns.3 = simd_float4(tx, ty, tz, 1)
return translationMatrix
}
func getRotationYMatrix(angle: Float) -> simd_float3x3 {
let rows = [
simd_float3(cos(angle), 0, -sin(angle)),
simd_float3(0, 1, 0),
simd_float3(-sin(angle), 0, cos(angle))
]
return float3x3(rows: rows)
}
或更具体的翻译矩阵:
func getTranslationZMatrix(tz: Float) -> simd_float4x4 {
var translationMatrix = matrix_identity_float4x4
translationMatrix.columns.3.z = tz
return translationMatrix
}
那么代码看起来像:
func getTransformGiven(currentLocation: CLLocation) -> matrix_float4x4 {
let bearing = bearingBetween(
startLocation: currentLocation,
endLocation: location
)
let distance: Float = 5
let originTransform = matrix_identity_float4x4
// Create a transform with a translation of 5meter away
// let translationMatrix = MatrixHelper.translate(
// x: 0,
// y: 0,
// z: distance * -1
// )
let translationMatrix = getTranslationZMatrix(tz: distance * -1)
// Rotation matrix theta degrees
// let rotationMatrix = MatrixHelper.rotateAboutY(
// degrees: bearing * -1
// )
let rotationMatrix = getRotationYMatrix(angle: Float(bearing * 180 / Double.pi))
var transformMatrix = simd_mul(rotationMatrix, translationMatrix)
return simd_mul(originTransform, transformMatrix)
}
我正在尝试遵循下面的一些示例代码 https://medium.com/@yatchoi/getting-started-with-arkit-real-life-waypoints-1707e3cb1da2,我得到了 unresolved identifier 'MatrixHelper'
let translationMatrix = MatrixHelper.translate(
x: 0,
y: 0,
z: distance * -1
)
// Rotation matrix theta degrees
let rotationMatrix = MatrixHelper.rotateAboutY(
degrees: bearing * -1
)
我需要导入什么库或包才能获得 MatrixHelper - 一些数学包?我用谷歌搜索了文档,但找不到任何东西。
我认为他只是写了一个自定义class来包装GLKit提供的功能。
他将 class MatrixHelper 命名为
使用 MatrixHelper.rotateAboutY() 他调用类似的东西:
GLKMatrix4 GLKMatrix4Rotate(GLKMatrix4 matrix, float radians, float x, float y, float z);
所以MatrixHelper中使用的包是GLKit,更准确的说是GLKMatrix4
https://developer.apple.com/documentation/glkit/glkmatrix4-pce
可以避免使用MatrixHelper(还有GLKit),使用simd,很简单。这些方法如下所示:
func getTranslationMatrix(tx: Float, ty: Float, tz: Float) -> simd_float4x4 {
var translationMatrix = matrix_identity_float4x4
translationMatrix.columns.3 = simd_float4(tx, ty, tz, 1)
return translationMatrix
}
func getRotationYMatrix(angle: Float) -> simd_float3x3 {
let rows = [
simd_float3(cos(angle), 0, -sin(angle)),
simd_float3(0, 1, 0),
simd_float3(-sin(angle), 0, cos(angle))
]
return float3x3(rows: rows)
}
或更具体的翻译矩阵:
func getTranslationZMatrix(tz: Float) -> simd_float4x4 {
var translationMatrix = matrix_identity_float4x4
translationMatrix.columns.3.z = tz
return translationMatrix
}
那么代码看起来像:
func getTransformGiven(currentLocation: CLLocation) -> matrix_float4x4 {
let bearing = bearingBetween(
startLocation: currentLocation,
endLocation: location
)
let distance: Float = 5
let originTransform = matrix_identity_float4x4
// Create a transform with a translation of 5meter away
// let translationMatrix = MatrixHelper.translate(
// x: 0,
// y: 0,
// z: distance * -1
// )
let translationMatrix = getTranslationZMatrix(tz: distance * -1)
// Rotation matrix theta degrees
// let rotationMatrix = MatrixHelper.rotateAboutY(
// degrees: bearing * -1
// )
let rotationMatrix = getRotationYMatrix(angle: Float(bearing * 180 / Double.pi))
var transformMatrix = simd_mul(rotationMatrix, translationMatrix)
return simd_mul(originTransform, transformMatrix)
}