Search code examples
swiftpoint-cloud-librarypoint-clouds

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: Image

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


Solution

  • 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

    enter image description here

    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
        }