src/analysis.js
function percentVariationExplainedByFittedPlane(vertices, centroid, normal, range) {
    var numVertices = vertices.length,
        diff = 0;
    for (var i = 0; i < numVertices; i++) {
        var fittedZ = Math.sqrt(
                (vertices[i].x - centroid.x) * (vertices[i].x - centroid.x) +
                (vertices[i].y - centroid.y) * (vertices[i].y - centroid.y)
            ) * Math.tan(normal.z * Math.PI) + centroid.z;
        diff += (vertices[i].z - fittedZ) * (vertices[i].z - fittedZ);
    }
    return 1 - Math.sqrt(diff / numVertices) * 2 / range;
}
function percentVariationExplainedByFittedPlane(vertices, centroid, normal, range) {
    var numVertices = vertices.length,
        diff = 0;
    for (var i = 0; i < numVertices; i++) {
        var fittedZ = Math.sqrt(
                (vertices[i].x - centroid.x) * (vertices[i].x - centroid.x) +
                (vertices[i].y - centroid.y) * (vertices[i].y - centroid.y)
            ) * Math.tan(normal.z * Math.PI) + centroid.z;
        diff += (vertices[i].z - fittedZ) * (vertices[i].z - fittedZ);
    }
    return 1 - Math.sqrt(diff / numVertices) * 2 / range;
}
function percentVariationExplainedByFittedPlane ( vertices , centroid , normal , range ) { var numVertices = vertices . length , diff = 0 ; for ( var i = 0 ; i < numVertices ; i ++ ) { var fittedZ = Math . sqrt ( ( vertices [ i ] . x - centroid . x ) * ( vertices [ i ] . x - centroid . x ) + ( vertices [ i ] . y - centroid . y ) * ( vertices [ i ] . y - centroid . y ) ) * Math . tan ( normal . z * Math . PI ) + centroid . z ; diff += ( vertices [ i ] . z - fittedZ ) * ( vertices [ i ] . z - fittedZ ) ; } return 1 - Math . sqrt ( diff / numVertices ) * 2 / range ; }
A measure of correlation between a terrain and its fitted plane.

This uses a different approach than the common one (R^2, aka Pearson's
correlation coefficient) because the range is constricted and the data is
often non-normal. The approach taken here compares the differences between
the terrain elevations and the fitted plane at each vertex, and divides by
half the range to arrive at a dimensionless value.

@param {THREE.Vector3[]} vertices
The terrain vertices.
@param {THREE.Vector3} centroid
The fitted plane centroid.
@param {THREE.Vector3} normal
The fitted plane normal.
@param {Number} range
The allowed range in elevations.

@return {Number}
Returns a number between 0 and 1 indicating how well the fitted plane
explains the variation in terrain elevation. 1 means entirely explained; 0
means not explained at all.
A measure of correlation between a terrain and its fitted plane .