src/analysis.js
function getFittedPlaneNormal(points, centroid) {
    var n = points.length,
        xx = 0,
        xy = 0,
        xz = 0,
        yy = 0,
        yz = 0,
        zz = 0;
    if (n < 3) throw new Error('At least three points are required to fit a plane');

    var r = new THREE.Vector3();
    for (var i = 0, l = points.length; i < l; i++) {
        r.copy(points[i]).sub(centroid);
        xx += r.x * r.x;
        xy += r.x * r.y;
        xz += r.x * r.z;
        yy += r.y * r.y;
        yz += r.y * r.z;
        zz += r.z * r.z;
    }

    var xDeterminant = yy*zz - yz*yz,
        yDeterminant = xx*zz - xz*xz,
        zDeterminant = xx*yy - xy*xy,
        maxDeterminant = Math.max(xDeterminant, yDeterminant, zDeterminant);
    if (maxDeterminant <= 0) throw new Error("The points don't span a plane");

    if (maxDeterminant === xDeterminant) {
        r.set(
            1,
            (xz*yz - xy*zz) / xDeterminant,
            (xy*yz - xz*yy) / xDeterminant
        );
    }
    else if (maxDeterminant === yDeterminant) {
        r.set(
            (yz*xz - xy*zz) / yDeterminant,
            1,
            (xy*xz - yz*xx) / yDeterminant
        );
    }
    else if (maxDeterminant === zDeterminant) {
        r.set(
            (yz*xy - xz*yy) / zDeterminant,
            (xz*xy - yz*xx) / zDeterminant,
            1
        );
    }
    return r.normalize();
}
function getFittedPlaneNormal(points, centroid) {
    var n = points.length,
        xx = 0,
        xy = 0,
        xz = 0,
        yy = 0,
        yz = 0,
        zz = 0;
    if (n < 3) throw new Error('At least three points are required to fit a plane');

    var r = new THREE.Vector3();
    for (var i = 0, l = points.length; i < l; i++) {
        r.copy(points[i]).sub(centroid);
        xx += r.x * r.x;
        xy += r.x * r.y;
        xz += r.x * r.z;
        yy += r.y * r.y;
        yz += r.y * r.z;
        zz += r.z * r.z;
    }

    var xDeterminant = yy*zz - yz*yz,
        yDeterminant = xx*zz - xz*xz,
        zDeterminant = xx*yy - xy*xy,
        maxDeterminant = Math.max(xDeterminant, yDeterminant, zDeterminant);
    if (maxDeterminant <= 0) throw new Error("The points don't span a plane");

    if (maxDeterminant === xDeterminant) {
        r.set(
            1,
            (xz*yz - xy*zz) / xDeterminant,
            (xy*yz - xz*yy) / xDeterminant
        );
    }
    else if (maxDeterminant === yDeterminant) {
        r.set(
            (yz*xz - xy*zz) / yDeterminant,
            1,
            (xy*xz - yz*xx) / yDeterminant
        );
    }
    else if (maxDeterminant === zDeterminant) {
        r.set(
            (yz*xy - xz*yy) / zDeterminant,
            (xz*xy - yz*xx) / zDeterminant,
            1
        );
    }
    return r.normalize();
}
function getFittedPlaneNormal ( points , centroid ) { var n = points . length , xx = 0 , xy = 0 , xz = 0 , yy = 0 , yz = 0 , zz = 0 ; if ( n < 3 ) throw new Error ( 'At least three points are required to fit a plane' ) ; var r = new THREE . Vector3 ( ) ; for ( var i = 0 , l = points . length ; i < l ; i ++ ) { r . copy ( points [ i ] ) . sub ( centroid ) ; xx += r . x * r . x ; xy += r . x * r . y ; xz += r . x * r . z ; yy += r . y * r . y ; yz += r . y * r . z ; zz += r . z * r . z ; } var xDeterminant = yy * zz - yz * yz , yDeterminant = xx * zz - xz * xz , zDeterminant = xx * yy - xy * xy , maxDeterminant = Math . max ( xDeterminant , yDeterminant , zDeterminant ) ; if ( maxDeterminant <= 0 ) throw new Error ( "The points don't span a plane" ) ; if ( maxDeterminant === xDeterminant ) { r . set ( 1 , ( xz * yz - xy * zz ) / xDeterminant , ( xy * yz - xz * yy ) / xDeterminant ) ; } else if ( maxDeterminant === yDeterminant ) { r . set ( ( yz * xz - xy * zz ) / yDeterminant , 1 , ( xy * xz - yz * xx ) / yDeterminant ) ; } else if ( maxDeterminant === zDeterminant ) { r . set ( ( yz * xy - xz * yy ) / zDeterminant , ( xz * xy - yz * xx ) / zDeterminant , 1 ) ; } return r . normalize ( ) ; }
Gets the normal vector of the fitted plane of a 3D array of points.

@param {THREE.Vector3[]} points
A 3D array of vertices to which to fit a plane.
@param {THREE.Vector3} centroid
The centroid of the vertex cloud.

@return {THREE.Vector3}
The normal vector of the fitted plane.
Gets the normal vector of the fitted plane of a 3D array of points .