palette/octree.js
// separate node samples into 8 subblocks
separate node samples into 8 subblocks
separate node samples into 8 subblocks
function divideNode(node) {
    var tresh = calcNodeTreshold(node);
    node.children = [];
    for (var i = 0; i < 8; i++) {
        node.children.push({
            samples : []
        });
    }
    function setRanges(node, rMin, rMax, gMin, gMax, bMin, bMax) {
        node.rMax = rMax;
        node.rMin = rMin;
        node.gMax = gMax;
        node.gMin = gMin;
        node.bMax = bMax;
        node.bMin = bMin;
    }
    setRanges(node.children[0], node.rMin, tresh.r, node.gMin, tresh.g, node.bMin, tresh.b);
    setRanges(node.children[1], tresh.r, node.rMax, node.gMin, tresh.g, node.bMin, tresh.b);
    setRanges(node.children[2], node.rMin, tresh.r, tresh.g, node.gMax, node.bMin, tresh.b);
    setRanges(node.children[3], tresh.r, node.rMax, tresh.g, node.gMax, node.bMin, tresh.b);
    setRanges(node.children[4], node.rMin, tresh.r, node.gMin, tresh.g, tresh.b, node.bMax);
    setRanges(node.children[5], tresh.r, node.rMax, node.gMin, tresh.g, tresh.b, node.bMax);
    setRanges(node.children[6], node.rMin, tresh.r, tresh.g, node.gMax, tresh.b, node.bMax);
    setRanges(node.children[7], tresh.r, node.rMax, tresh.g, node.gMax, tresh.b, node.bMax);
    for (var i = 0; i < node.samples.length; i++) {
        var c = node.samples[i];
        var p = calcColorProgression(c, tresh);
        node.children[p].samples.push(c);
    }
    for (var i = 0; i < 8; i++) {
        node.children[i].average = calcNodeAverage(node.children[i]);
        node.children[i].error = calcNodeError(node.children[i]);
    }
}
function divideNode ( node ) { var tresh = calcNodeTreshold ( node ) ; node . children = [ ] ; for ( var i = 0 ; i < 8 ; i ++ ) { node . children . push ( { samples : [ ] } ) ; } function setRanges ( node , rMin , rMax , gMin , gMax , bMin , bMax ) { node . rMax = rMax ; node . rMin = rMin ; node . gMax = gMax ; node . gMin = gMin ; node . bMax = bMax ; node . bMin = bMin ; } setRanges ( node . children [ 0 ] , node . rMin , tresh . r , node . gMin , tresh . g , node . bMin , tresh . b ) ; setRanges ( node . children [ 1 ] , tresh . r , node . rMax , node . gMin , tresh . g , node . bMin , tresh . b ) ; setRanges ( node . children [ 2 ] , node . rMin , tresh . r , tresh . g , node . gMax , node . bMin , tresh . b ) ; setRanges ( node . children [ 3 ] , tresh . r , node . rMax , tresh . g , node . gMax , node . bMin , tresh . b ) ; setRanges ( node . children [ 4 ] , node . rMin , tresh . r , node . gMin , tresh . g , tresh . b , node . bMax ) ; setRanges ( node . children [ 5 ] , tresh . r , node . rMax , node . gMin , tresh . g , tresh . b , node . bMax ) ; setRanges ( node . children [ 6 ] , node . rMin , tresh . r , tresh . g , node . gMax , tresh . b , node . bMax ) ; setRanges ( node . children [ 7 ] , tresh . r , node . rMax , tresh . g , node . gMax , tresh . b , node . bMax ) ; for ( var i = 0 ; i < node . samples . length ; i ++ ) { var c = node . samples [ i ] ; var p = calcColorProgression ( c , tresh ) ; node . children [ p ] . samples . push ( c ) ; } for ( var i = 0 ; i < 8 ; i ++ ) { node . children [ i ] . average = calcNodeAverage ( node . children [ i ] ) ; node . children [ i ] . error = calcNodeError ( node . children [ i ] ) ; } }
separate node samples into 8 subblocks
separate node samples into 8 subblocks
// calculate center point //console.log("Dividing node with t=["+tresh.r+","+tresh.g+","+tresh.b+"]"); // create the subnodes // reposition samples in subnodes // offsets can be 0 or 1 // calculate average value for each subnode