Swift SIMD rounding errors in making 90 degree rotation matrix with sin, cos - swift

I'm following this guide about working with matrices with the accelerate framework.
There they using something similar to this to rotate a vector:
func makeRotationMatrix(angle: Float) -> simd_float3x3 {
let rows = [
simd_float3(cos(angle), -sin(angle), 0),
simd_float3(sin(angle), cos(angle), 0),
simd_float3(0, 0, 1)
]
return float3x3(rows: rows)
}
let vector = simd_float3(x: 1, y: 1, z: 1)
let angle = Measurement(value: 180, unit: UnitAngle.degrees)
let radians = Float(angle.converted(to: .radians).value)
let rotationMatrix = makeRotationMatrix(angle: radians)
let rotatedVector = rotationMatrix * vector
print("vector:", vector) // SIMD3<Float>(1.0, 1.0, 1.0)
print("angle:", angle) // 180.0 °
print("radians:", radians) // 3.1415927
print("rotatedVector:", rotatedVector) // SIMD3<Float>(-0.99999994, -1.0000001, 1.0)
I expected the x of the rotated vector to be -1 instead of -0.99999994. I guess this is caused by the radians being a float? We could correct for this by rounding by hand:
let correctedVector = simd_float3(
x: rotatedVector.x.rounded(),
y: rotatedVector.y.rounded(),
z: rotatedVector.z.rounded()
)
print("correctedVector:", correctedVector) // SIMD3<Float>(-1.0, -1.0, 1.0)
But I'm wondering if there is a way to rotate this vector without rounding errors?

Related

How to move PointCloud to coordinates center?

I have a PointCloud which have points with position(x, y, z) and color(r, g, b)
But points lays in big distance from coordinates canter:
Question is: what algorithm can be used to place all points to coordinates center? My guess is to create translation matrix and multiply all pointCloud points to it, but I can't determine what this matrix should contain
Just found an answer. Need to find center of mass of PointCloud with something like this:
var summX: Float = 0
var summY: Float = 0
var summZ: Float = 0
for point in points {
summX += point.x
summY += point.y
summZ += point.z
}
let middleX = summX / Float(points.count)
let middleY = summY / Float(points.count)
let middleZ = summZ / Float(points.count)
let centerOfMass = Float3(x: middleX, y: middleY, z: middleZ)
Then create translation matrix
And finally multiply all points to this matrix
let translationMatrix = float4x4(simd_float4(x: 1, y: 0, z: 0, w: -centerOfMass.x),
simd_float4(x: 0, y: 1, z: 0, w: -centerOfMass.y),
simd_float4(x: 0, y: 0, z: 1, w: -centerOfMass.z),
simd_float4(x: 0, y: 0, z: 0, w: 1))
let translatedPoints = points.map { point in
return point * translationMatrix
}

Procedural mesh not rendering lighting [SceneKit - Xcode]

I am quite new to swift and Xcode however, I have been programming in other languages for several years. I am trying to procedurally create a 3D mesh in SceneKit (iOS). My code works as expected however, when running the application the generated object renders a flat black colour, ignoring all lighting. I have also added a cube to the scene to show that the scene lighting is working.
I would imagine that there is either a problem with the shader or that I need to define the normals of the geometry to fix this. I have tried playing around with a few properties of the SCNMaterial, but they don't seem to change anything.
If it is just a case of defining the normals, please could you advise how I would do this in Swift / SceneKit. Or perhaps I have missed something else, any help would be much appreciated.
Screenshot below:
My code below:
public static func CreateMesh (size: CGFloat, resolution: CGFloat) -> SCNNode? {
let axisCount = Int(floor(size / resolution))
let bottomLeft = CGVector(
dx: CGFloat(-(axisCount / 2)) * resolution,
dy: CGFloat(-(axisCount / 2)) * resolution
)
var verts = Array(
repeating: Array(
repeating: (i: Int(0), pos: SCNVector3.init(x: 0, y: 0, z: 0)),
count: axisCount),
count: axisCount
)
var vertsStream = [SCNVector3]()
var i : Int = 0
for x in 0...axisCount-1 {
for y in 0...axisCount-1 {
verts[x][y] = (
i,
SCNVector3(
x: Float(bottomLeft.dx + CGFloat(x) * resolution),
y: Float.random(in: 0..<0.1),
z: Float(bottomLeft.dy + CGFloat(y) * resolution)
)
)
vertsStream.append(verts[x][y].pos)
i += 1
}
}
var tris = [(a: Int, b: Int, c: Int)]()
var trisStream = [UInt16]()
for x in 0...axisCount - 2 {
for y in 0...axisCount - 2 {
// Quad
tris.append((
a: verts[x][y].i,
b: verts[x][y+1].i,
c: verts[x+1][y+1].i
))
tris.append((
a: verts[x+1][y+1].i,
b: verts[x+1][y].i,
c: verts[x][y].i
))
}
}
for t in tris {
trisStream.append(UInt16(t.a))
trisStream.append(UInt16(t.b))
trisStream.append(UInt16(t.c))
}
// Create scene element
let geometrySource = SCNGeometrySource(vertices: vertsStream)
let geometryElement = SCNGeometryElement(indices: trisStream, primitiveType: .triangles)
let geometryFinal = SCNGeometry(sources: [geometrySource], elements: [geometryElement])
let node = SCNNode(geometry: geometryFinal)
////////////////////////
// FIX MATERIAL
////////////////////////
let mat = SCNMaterial()
mat.diffuse.intensity = 1
mat.lightingModel = .blinn
mat.blendMode = .replace
node.geometry?.materials = [mat]
return node
}
After a lot of searching I managed to find a post with a line of code that looks something like this:
let gsNormals = SCNGeometrySource(normals: normalStream)
So from there I managed to work out how to set the surface normals. It seems like there really isn't a lot of online content / learning material when it comes to the more advanced topics like this in Xcode / Swift, which is quite unfortunate.
I have set it up to create a parabolic shape plane, just for testing. But this code will be used to generate a mesh from a height map, which should now be easy to implement. I think it's pretty useful code, so I have included it below incase anyone else ever has the same issue that I did.
public static func CreateMesh (size: CGFloat, resolution: CGFloat) -> SCNNode? {
let axisCount = Int(floor(size / resolution))
let bottomLeft = CGVector(
dx: CGFloat(-(axisCount / 2)) * resolution,
dy: CGFloat(-(axisCount / 2)) * resolution
)
/// Verticies ///
var verts = Array(
repeating: Array(
repeating: (i: Int(0), pos: SCNVector3.init(x: 0, y: 0, z: 0)),
count: axisCount),
count: axisCount
)
var vertsStream = [SCNVector3]()
var i = 0
for x in 0...axisCount - 1 {
for y in 0...axisCount - 1 {
var dx = axisCount / 2 - x
dx = dx * dx
var dy = axisCount / 2 - y
dy = dy * dy
let yVal = Float(Double(dx + dy) * 0.0125)
verts[x][y] = (
i: i,
pos: SCNVector3(
x: Float(bottomLeft.dx + CGFloat(x) * resolution),
//y: Float.random(in: 0..<0.1),
y: yVal,
z: Float(bottomLeft.dy + CGFloat(y) * resolution)
)
)
vertsStream.append(verts[x][y].pos)
i += 1
}
}
///
/// Triangles ///
var tris = [(a: Int, b: Int, c: Int)]()
var trisStream = [UInt32]()
for x in 0...axisCount - 2 {
for y in 0...axisCount - 2 {
// Quad
tris.append((
a: verts[x][y].i,
b: verts[x][y+1].i,
c: verts[x+1][y].i
))
tris.append((
a: verts[x+1][y].i,
b: verts[x][y+1].i,
c: verts[x+1][y+1].i
))
}
}
for t in tris {
trisStream.append(UInt32(t.a))
trisStream.append(UInt32(t.b))
trisStream.append(UInt32(t.c))
}
///
/// Normals ///
var normalStream = [SCNVector3]()
for x in 0...axisCount - 1 {
for y in 0...axisCount - 1 {
// calculate normal vector perp to average plane
let leftX = x == 0 ? 0 : x - 1
let rightX = x == axisCount - 1 ? axisCount - 1 : x + 1
let leftY = y == 0 ? 0 : y - 1
let rightY = y == axisCount - 1 ? axisCount - 1 : y + 1
let avgXVector = float3(verts[rightX][y].pos) - float3(verts[leftX][y].pos)
let avgYVector = float3(verts[x][rightY].pos) - float3(verts[x][leftY].pos)
// If you are unfamiliar with how to calculate normals
// search for vector cross product, this is used to find
// a vector that is orthogonal to two other vectors, in our
// case perpendicular to the surface
let normal = cross(
normalize(avgYVector),
normalize(avgXVector)
)
normalStream.append(SCNVector3(normal))
}
}
///
// Create scene element
let gsGeometry = SCNGeometrySource(vertices: vertsStream)
let gsNormals = SCNGeometrySource(normals: normalStream)
let geometryElement = SCNGeometryElement(indices: trisStream, primitiveType: .triangles)
let geometryFinal = SCNGeometry(sources: [gsGeometry, gsNormals], elements: [geometryElement])
let node = SCNNode(geometry: geometryFinal)
let mat = SCNMaterial()
mat.isDoubleSided = true
mat.lightingModel = .blinn
node.geometry?.materials = [mat]
return node
}

Get scale, translation and rotation from CATransform3D

Given a CATransform3D transform, I want to extract the scale, translation and rotation as separate transforms. From some digging, I was able to accomplish this for CGAffineTransform in Swift, like so:
extension CGAffineTransform {
var scaleDelta:CGAffineTransform {
let xScale = sqrt(a * a + c * c)
let yScale = sqrt(b * b + d * d)
return CGAffineTransform(scaleX: xScale, y: yScale)
}
var rotationDelta:CGAffineTransform {
let rotation = CGFloat(atan2f(Float(b), Float(a)))
return CGAffineTransform(rotationAngle: rotation)
}
var translationDelta:CGAffineTransform {
return CGAffineTransform(translationX: tx, y: ty)
}
}
How would one do something similar for CATransform3D using math? (I am looking for a solution that doesn't use keypaths.)
(implementation or math-only answers at your discretion)
If you're starting from a proper affine matrix that can be decomposed correctly (if not unambiguously) into a sequence of scale, rotate, translate, this method will perform the decomposition into a tuple of vectors representing the translation, rotation (Euler angles), and scale components:
extension CATransform3D {
func decomposeTRS() -> (float3, float3, float3) {
let m0 = float3(Float(self.m11), Float(self.m12), Float(self.m13))
let m1 = float3(Float(self.m21), Float(self.m22), Float(self.m23))
let m2 = float3(Float(self.m31), Float(self.m32), Float(self.m33))
let m3 = float3(Float(self.m41), Float(self.m42), Float(self.m43))
let t = m3
let sx = length(m0)
let sy = length(m1)
let sz = length(m2)
let s = float3(sx, sy, sz)
let rx = m0 / sx
let ry = m1 / sy
let rz = m2 / sz
let pitch = atan2(ry.z, rz.z)
let yaw = atan2(-rx.z, hypot(ry.z, rz.z))
let roll = atan2(rx.y, rx.x)
let r = float3(pitch, yaw, roll)
return (t, r, s)
}
}
To show that this routine correctly extracts the various components, construct a transform and ensure that it decomposes as expected:
let rotationX = CATransform3DMakeRotation(.pi / 2, 1, 0, 0)
let rotationY = CATransform3DMakeRotation(.pi / 3, 0, 1, 0)
let rotationZ = CATransform3DMakeRotation(.pi / 4, 0, 0, 1)
let translation = CATransform3DMakeTranslation(1, 2, 3)
let scale = CATransform3DMakeScale(0.1, 0.2, 0.3)
let transform = CATransform3DConcat(CATransform3DConcat(CATransform3DConcat(CATransform3DConcat(scale, rotationX), rotationY), rotationZ), translation)
let (T, R, S) = transform.decomposeTRS()
print("\(T), \(R), \(S))")
This produces:
float3(1.0, 2.0, 3.0), float3(1.5708, 1.0472, 0.785398), float3(0.1, 0.2, 0.3))
Note that this decomposition assumes an Euler multiplication order of XYZ, which is only one of several possible orderings.
Caveat: There are certainly values for which this method is not numerically stable. I haven't tested it extensively enough to know where these pitfalls lie, so caveat emptor.
For symmetry with the CGAffineTransform extension in my question, here is the CATransform3D extension that provides the "deltas" for scale, translation and rotation, based on Warren's decomposeTRS, which I have marked as the accepted answer.
extension CATransform3D {
var scaleDelta:CATransform3D {
let s = decomposeTRS().2
return CATransform3DMakeScale(CGFloat(s.x), CGFloat(s.y), CGFloat(s.z))
}
var rotationDelta:CATransform3D {
let r = decomposeTRS().1
let rx = CATransform3DMakeRotation(CGFloat(r.x), 1, 0, 0)
let ry = CATransform3DMakeRotation(CGFloat(r.y), 0, 1, 0)
let rz = CATransform3DMakeRotation(CGFloat(r.z), 0, 0, 1)
return CATransform3DConcat(CATransform3DConcat(rx, ry), rz)
}
var translationDelta:CATransform3D {
let t = decomposeTRS().0
return CATransform3DMakeTranslation(CGFloat(t.x), CGFloat(t.y), CGFloat(t.z))
}
}

Sine Wave UIBezierPath between two points

How does one create a path of a sine wave between two points?
I am able to create a path of a sine wave from an origin, but am not sure how the direction can be transformed so that the sine wave ends at a target CGPoint.
I would like to animate a SKNode along the path using SKAction.followPath
The simplest way to think about this is to transform the coordinate system, rotating by the angle between the two points, scaling by the distance between them and translating by the first point (assuming the sine starts at 0,0).
The OP has specified that he doesn't just want to draw the curve (in which case all one needs to do is apply the transform to the graphics context), but rather to use the curve in a SpriteKit SKAction.followPath call, so the transform has to be applied to the coordinates in the path, not to the context.
Here's a solution using CGPath rather than UIBezierPath, but they are equivalent, and you can get the UI version simply by let uip = UIBezierPath(cgPath: path). (I prefer CoreGraphics as they are cross-platform).
Playground code...
class MyView: UIView {
override func draw(_ rect: CGRect) {
guard let context = UIGraphicsGetCurrentContext() else { return }
context.setFillColor(UIColor.red.cgColor)
context.fill(self.bounds)
// Calculate the transform
let p1 = CGPoint(x: 100, y: 100)
let p2 = CGPoint(x: 400, y: 400)
let dx = p2.x - p1.x
let dy = p2.y - p1.y
let d = sqrt(dx * dx + dy * dy)
let a = atan2(dy, dx)
let cosa = cos(a) // Calculate only once...
let sina = sin(a) // Ditto
// Initialise our path
let path = CGMutablePath()
path.move(to: p1)
// Plot a parametric function with 100 points
let nPoints = 100
for t in 0 ... nPoints {
// Calculate the un-transformed x,y
let tx = CGFloat(t) / CGFloat(nPoints) // 0 ... 1
let ty = 0.1 * sin(tx * 2 * CGFloat.pi ) // 0 ... 2π, arbitrary amplitude
// Apply the transform
let x = p1.x + d * (tx * cosa - ty * sina)
let y = p1.y + d * (tx * sina + ty * cosa)
// Add the transformed point to the path
path.addLine(to: CGPoint(x: x, y: y))
}
// Draw the path
context.setStrokeColor(UIColor.blue.cgColor)
context.addPath(path)
context.strokePath()
}
}
let v = MyView(frame: CGRect(origin: CGPoint(x: 0, y:0), size: CGSize(width: 500, height: 500)))
Not crystal clear what you want but here's one possibility assuming you want a tilted sin curve:
Assume that the start point is (0, 0) and the end point is (x, y).
Let L be the distance between the origin and your point: L = sqrt(x^2 + y^2)
Write a loop that starts at 0 and ends at L, with increment dL and running sum l (which ends up running between 0 and L). This loop will allow us to create the points on your Bezier.
Then the x coordinate of your sin graph will be:
x_P = l * cos(theta), ranging from 0 to L * cos(theta) = x
To get the y coordinate, we add a sin function with the correct period to the sloping line between the origin and your point:
y_P = l * sin(theta) + sin(2 * PI * l / L)
note that, at l = L, (x_P, y_P) = (x, y) which is as it should be.
Was this what you wanted? It is not a rotation and so will not behave when the angle theta is large.

How can i check two angles if they are the same using radians

What i am trying to do is check if two Nodes(objects) are pointing in the same direction. But the problem is that SpriteKit rotates nodes in radians so it keeps adding to the zRotation of the node.
what i do for the rotation is:
if(touched_location.x >= self.frame.size.width/2)
{
node1.runAction(SKaction.RotateByAngle(CGFloat(-M_PI_2), 1.0)
}
else if(touched_location.x <= self.frame.size.width/2)
{
node1.runAction(SKAction.RotateByAngle(CGFloat(M_PI_2), 1.0)
}
so this rotates the node for 90° but RotateByAngle uses radians which is fine it sill works.
But radians keep adding to zRotation so i end up with something like 12,45 radians (not accurate number just for refrence) and this does not match the radians of my second Node(node2) that i am comparing the zRotation to.
For checking the rotation im using:
var first = CGFloat(round(100*node1.zRotation)/100)
var second = CGFloat(round(100*node2.zRoation)/100)
if(first == second)
{
do stuff....
}
But this obviously doesn't work cause if you keep rotation node in one direction the radian is to high (or to low if other direction) but visually the direction is the same.
How do i fix this?
the whole circle (360 deg) has angle 2 * PI, so 0 and 2 * PI represents the same 'normalized' angle.
import Foundation
func normalizeDifferenceAngleInRadians(a1: Double, _ a2: Double)->Double {
let twoPi = 2 * M_PI
return (a2 - a1) % twoPi
}
let a1 = M_PI_2
let a2 = 5 * M_PI_2
let diff = normalizeDifferenceAngleInRadians(a1, a2) // 0
based on Martin's note
import Foundation
func angleDifferenceInRadians(a1: Double, _ a2: Double)->Double {
let twoPi = 2 * M_PI
return -((a2 - a1 + M_PI) % twoPi - M_PI)
}
let a1 = M_PI_2
let a2 = 5 * M_PI_2 + 0.001
let a3 = 5 * M_PI_2 - 0.001
let diff1 = angleDifferenceInRadians(a1, a2) // -0.001
let diff2 = angleDifferenceInRadians(a1, a3) // 0.001
#0x141E thank you! I hope, the code below will be the final and correct implementation of my 'new year mathematical exercise'. Sometimes the simple trouble and quick correction leads to bigger trouble ...
func angleDifferenceInRadians(a1: Double, _ a2: Double)->Double {
let twoPi = 2 * M_PI
let d = (a2 - a1) % twoPi
let s = d < 0 ? -1.0 : 1.0
return d * s < M_PI ? d : (d - s * twoPi)
}