Skip to content
Snippets Groups Projects
LoadFile.ts 43.5 KiB
Newer Older
Laura Cappelli's avatar
Laura Cappelli committed
    }
}


/*
la funzione prepara i dati per il grafico se è da disegnare completo
*/
function setDataForCompleteChart(){

    //creo un array di 16384 elementi con la somma dei conteggi di tutti i pixel
    //per ogni canale
    var dataForChart: number[] = new Array(16384);
    for(var i: number = 0; i < 16384; i++){
        dataForChart[i] = 0;
    }
    for(var i: number = 0; i < xDim; i++){
        for(var j: number = 0; j < yDim; j++){
            for(var k: number = 0; k < 16384; k++){
                dataForChart[k] += DataMatrix[i][j][k];
            }
        }
    }

    //riempio le stringhe con i dati per il grafico
    dataCompleteChart = "Channel,Counts\n";
    dataCompleteChartCalibrated = "Energy,Counts\n";
    for(var i: number = 0; i < 16348; i++){
        dataCompleteChart += (i+1) + "," + dataForChart[i] + "\n";
        dataCompleteChartCalibrated += round3(((i+1)*a-b)/1000) + "," +
            dataForChart[i] + "\n";
    }
}

function round3(val: number){
    return (Math.round(val*Math.pow(10,3))/Math.pow(10,3));
}


/*
La funzione findMax riceve in input la matrice e gli estremi della sottomatrice
di cui deve trovare il massimo
*/
function findMax(matrix: number[][], pixel1: coordinates, pixel2:coordinates){
    var max: number = 0;
    for(var i: number = pixel1.xp; i <= pixel2.xp; i++){
        for(var j: number = pixel1.yp; j <= pixel2.yp; j++){
            if(matrix[i][j] > max){
                max = matrix[i][j];
            }
        }
    }  
    return max;    
}


/*
la funzione findPosition definisce la posizione del cursore del mouse
relativa al canvas nel momento in cui avviene l'evento passato in input 
*/
function findPosition(event: any, pixel: coordinates){
    var scrollTOP: number = (document.documentElement.scrollTop) ? 
        document.documentElement.scrollTop : document.body.scrollTop;
    var scrollLEFT: number = (document.documentElement.scrollLeft) ? 
        document.documentElement.scrollLeft : document.body.scrollLeft;
    var allX: number = event.clientX+scrollLEFT;
    var allY: number = event.clientY+scrollTOP;
    var elParent: any = document.getElementById('myCanvas');
    var objX: number = 0, objY: number = 0;
    while (elParent){
        objX += elParent.offsetLeft;
        objY += elParent.offsetTop;
        elParent = elParent.offsetParent;
    }

    pixel.xp = Math.floor((allX-objX - 1) / realPixelDim ) + newOrigin.xp;
    pixel.yp = Math.floor((allY-objY - 1) / realPixelDim ) + newOrigin.yp;
}


/*
la funzione findPosDown memorizza la posizione del pixel cliccato mentre la
funzione findPosUp memorizza la posizione del pixel quando il mouse viene
rilasciato. Inoltre questa seconda funzione ordina le coordinate, poi, se è
stato richiesto uno zoom, ridisegna il canvas e aggiorna l'origine; in ogni caso
aggiorna il grafico.
*/
function findPosDown(event: any){
    findPosition(event, zPixel1);
}
function findPosUp(event: any){
    findPosition(event, zPixel2);
    //alert(zPixel1.xp + " " + zPixel1.yp + " " +zPixel2.xp + " " + zPixel2.yp);
    //alert(newOrigin.xp + " " + newOrigin.yp);
    //controllo che pixel1.xp < pixel2.xp e che pixel1.yp < pixel2.yp
    var tmp: number;
    if (zPixel1.xp > zPixel2.xp) {
        tmp = zPixel1.xp;
        zPixel1.xp = zPixel2.xp;
        zPixel2.xp = tmp;
    }
    if (zPixel1.yp > zPixel2.yp) {
        tmp = zPixel1.yp;
        zPixel1.yp = zPixel2.yp;
        zPixel2.yp = tmp;
    }
    //verifico che non sia stato cliccato un singolo punto perchè, se così
    //fosse, non dovrei aggiornare la mappa nè segnalare che c'è uno zoom
    if (zPixel1.xp != zPixel2.xp || zPixel1.yp != zPixel2.yp){
        newOrigin = {xp: zPixel1.xp, yp:zPixel1.yp};
        drawImg(zPixel1, zPixel2, globalxMinRange, globalxMaxRange);
    }
    drawChart(zPixel1, zPixel2, globalxMinRange, globalxMaxRange);

}

function sumVect(vect: number[], from: number, to: number){
    var sum: number = 0;
    for(var i: number = from; i < to; i++){
        sum += vect[i];
    }
    return sum;
}