add amendments metric
[contractdashboard.git] / lib / jpgraph / jpgraph_contour.php
blob:a/lib/jpgraph/jpgraph_contour.php -> blob:b/lib/jpgraph/jpgraph_contour.php
<?php <?php
/*======================================================================= /*=======================================================================
// File: JPGRAPH_CONTOUR.PHP // File: JPGRAPH_CONTOUR.PHP
// Description: Contour plot // Description: Contour plot
// Created: 2009-03-08 // Created: 2009-03-08
// Ver: $Id: jpgraph_contour.php 1870 2009-09-29 04:24:18Z ljp $ // Ver: $Id: jpgraph_contour.php 1870 2009-09-29 04:24:18Z ljp $
// //
// Copyright (c) Aditus Consulting. All rights reserved. // Copyright (c) Aditus Consulting. All rights reserved.
//======================================================================== //========================================================================
*/ */
require_once('jpgraph_meshinterpolate.inc.php'); require_once('jpgraph_meshinterpolate.inc.php');
define('HORIZ_EDGE',0); define('HORIZ_EDGE',0);
define('VERT_EDGE',1); define('VERT_EDGE',1);
   
/** /**
* This class encapsulates the core contour plot algorithm. It will find the path * This class encapsulates the core contour plot algorithm. It will find the path
* of the specified isobars in the data matrix specified. It is assumed that the * of the specified isobars in the data matrix specified. It is assumed that the
* data matrix models an equspaced X-Y mesh of datavalues corresponding to the Z * data matrix models an equspaced X-Y mesh of datavalues corresponding to the Z
* values. * values.
* *
*/ */
class Contour { class Contour {
   
private $dataPoints = array(); private $dataPoints = array();
private $nbrCols=0,$nbrRows=0; private $nbrCols=0,$nbrRows=0;
private $horizEdges = array(), $vertEdges=array(); private $horizEdges = array(), $vertEdges=array();
private $isobarValues = array(); private $isobarValues = array();
private $stack = null; private $stack = null;
private $isobarCoord = array(); private $isobarCoord = array();
private $nbrIsobars = 10, $isobarColors = array(); private $nbrIsobars = 10, $isobarColors = array();
private $invert = true; private $invert = true;
private $highcontrast = false, $highcontrastbw = false; private $highcontrast = false, $highcontrastbw = false;
   
/** /**
* Create a new contour level "algorithm machine". * Create a new contour level "algorithm machine".
* @param $aMatrix The values to find the contour from * @param $aMatrix The values to find the contour from
* @param $aIsobars Mixed. If integer it determines the number of isobars to be used. The levels are determined * @param $aIsobars Mixed. If integer it determines the number of isobars to be used. The levels are determined
* automatically as equdistance between the min and max value of the matrice. * automatically as equdistance between the min and max value of the matrice.
* If $aIsobars is an array then this is interpretated as an array of values to be used as isobars in the * If $aIsobars is an array then this is interpretated as an array of values to be used as isobars in the
* contour plot. * contour plot.
* @return an instance of the contour algorithm * @return an instance of the contour algorithm
*/ */
function __construct($aMatrix,$aIsobars=10, $aColors=null) { function __construct($aMatrix,$aIsobars=10, $aColors=null) {
   
$this->nbrRows = count($aMatrix); $this->nbrRows = count($aMatrix);
$this->nbrCols = count($aMatrix[0]); $this->nbrCols = count($aMatrix[0]);
$this->dataPoints = $aMatrix; $this->dataPoints = $aMatrix;
   
if( is_array($aIsobars) ) { if( is_array($aIsobars) ) {
// use the isobar values supplied // use the isobar values supplied
$this->nbrIsobars = count($aIsobars); $this->nbrIsobars = count($aIsobars);
$this->isobarValues = $aIsobars; $this->isobarValues = $aIsobars;
} }
else { else {
// Determine the isobar values automatically // Determine the isobar values automatically
$this->nbrIsobars = $aIsobars; $this->nbrIsobars = $aIsobars;
list($min,$max) = $this->getMinMaxVal(); list($min,$max) = $this->getMinMaxVal();
$stepSize = ($max-$min) / $aIsobars ; $stepSize = ($max-$min) / $aIsobars ;
$isobar = $min+$stepSize/2; $isobar = $min+$stepSize/2;
for ($i = 0; $i < $aIsobars; $i++) { for ($i = 0; $i < $aIsobars; $i++) {
$this->isobarValues[$i] = $isobar; $this->isobarValues[$i] = $isobar;
$isobar += $stepSize; $isobar += $stepSize;
} }
} }
   
if( $aColors !== null && count($aColors) > 0 ) { if( $aColors !== null && count($aColors) > 0 ) {
   
if( !is_array($aColors) ) { if( !is_array($aColors) ) {
JpGraphError::RaiseL(28001); JpGraphError::RaiseL(28001);
//'Third argument to Contour must be an array of colors.' //'Third argument to Contour must be an array of colors.'
} }
   
if( count($aColors) != count($this->isobarValues) ) { if( count($aColors) != count($this->isobarValues) ) {
JpGraphError::RaiseL(28002); JpGraphError::RaiseL(28002);
//'Number of colors must equal the number of isobar lines specified'; //'Number of colors must equal the number of isobar lines specified';
} }
   
$this->isobarColors = $aColors; $this->isobarColors = $aColors;
} }
} }
   
/** /**
* Flip the plot around the Y-coordinate. This has the same affect as flipping the input * Flip the plot around the Y-coordinate. This has the same affect as flipping the input
* data matrice * data matrice
* *
* @param $aFlg If true the the vertice in input data matrice position (0,0) corresponds to the top left * @param $aFlg If true the the vertice in input data matrice position (0,0) corresponds to the top left
* corner of teh plot otherwise it will correspond to the bottom left corner (a horizontal flip) * corner of teh plot otherwise it will correspond to the bottom left corner (a horizontal flip)
*/ */
function SetInvert($aFlg=true) { function SetInvert($aFlg=true) {
$this->invert = $aFlg; $this->invert = $aFlg;
} }
   
/** /**
* Find the min and max values in the data matrice * Find the min and max values in the data matrice
* *
* @return array(min_value,max_value) * @return array(min_value,max_value)
*/ */
function getMinMaxVal() { function getMinMaxVal() {
$min = $this->dataPoints[0][0]; $min = $this->dataPoints[0][0];
$max = $this->dataPoints[0][0]; $max = $this->dataPoints[0][0];
for ($i = 0; $i < $this->nbrRows; $i++) { for ($i = 0; $i < $this->nbrRows; $i++) {
if( ($mi=min($this->dataPoints[$i])) < $min ) $min = $mi; if( ($mi=min($this->dataPoints[$i])) < $min ) $min = $mi;
if( ($ma=max($this->dataPoints[$i])) > $max ) $max = $ma; if( ($ma=max($this->dataPoints[$i])) > $max ) $max = $ma;
} }
return array($min,$max); return array($min,$max);
} }
   
/** /**
* Reset the two matrices that keeps track on where the isobars crosses the * Reset the two matrices that keeps track on where the isobars crosses the
* horizontal and vertical edges * horizontal and vertical edges
*/ */
function resetEdgeMatrices() { function resetEdgeMatrices() {
for ($k = 0; $k < 2; $k++) { for ($k = 0; $k < 2; $k++) {
for ($i = 0; $i <= $this->nbrRows; $i++) { for ($i = 0; $i <= $this->nbrRows; $i++) {
for ($j = 0; $j <= $this->nbrCols; $j++) { for ($j = 0; $j <= $this->nbrCols; $j++) {
$this->edges[$k][$i][$j] = false; $this->edges[$k][$i][$j] = false;
} }
} }
} }
} }
   
/** /**
* Determine if the specified isobar crosses the horizontal edge specified by its row and column * Determine if the specified isobar crosses the horizontal edge specified by its row and column
* *
* @param $aRow Row index of edge to be checked * @param $aRow Row index of edge to be checked
* @param $aCol Col index of edge to be checked * @param $aCol Col index of edge to be checked
* @param $aIsobar Isobar value * @param $aIsobar Isobar value
* @return true if the isobar is crossing this edge * @return true if the isobar is crossing this edge
*/ */
function isobarHCrossing($aRow,$aCol,$aIsobar) { function isobarHCrossing($aRow,$aCol,$aIsobar) {
   
if( $aCol >= $this->nbrCols-1 ) { if( $aCol >= $this->nbrCols-1 ) {
JpGraphError::RaiseL(28003,$aCol); JpGraphError::RaiseL(28003,$aCol);
//'ContourPlot Internal Error: isobarHCrossing: Coloumn index too large (%d)' //'ContourPlot Internal Error: isobarHCrossing: Coloumn index too large (%d)'
} }
if( $aRow >= $this->nbrRows ) { if( $aRow >= $this->nbrRows ) {
JpGraphError::RaiseL(28004,$aRow); JpGraphError::RaiseL(28004,$aRow);
//'ContourPlot Internal Error: isobarHCrossing: Row index too large (%d)' //'ContourPlot Internal Error: isobarHCrossing: Row index too large (%d)'
} }
   
$v1 = $this->dataPoints[$aRow][$aCol]; $v1 = $this->dataPoints[$aRow][$aCol];
$v2 = $this->dataPoints[$aRow][$aCol+1]; $v2 = $this->dataPoints[$aRow][$aCol+1];
   
return ($aIsobar-$v1)*($aIsobar-$v2) < 0 ; return ($aIsobar-$v1)*($aIsobar-$v2) < 0 ;
   
} }
   
/** /**
* Determine if the specified isobar crosses the vertical edge specified by its row and column * Determine if the specified isobar crosses the vertical edge specified by its row and column
* *
* @param $aRow Row index of edge to be checked * @param $aRow Row index of edge to be checked
* @param $aCol Col index of edge to be checked * @param $aCol Col index of edge to be checked
* @param $aIsobar Isobar value * @param $aIsobar Isobar value
* @return true if the isobar is crossing this edge * @return true if the isobar is crossing this edge
*/ */
function isobarVCrossing($aRow,$aCol,$aIsobar) { function isobarVCrossing($aRow,$aCol,$aIsobar) {
   
if( $aRow >= $this->nbrRows-1) { if( $aRow >= $this->nbrRows-1) {
JpGraphError::RaiseL(28005,$aRow); JpGraphError::RaiseL(28005,$aRow);
//'isobarVCrossing: Row index too large //'isobarVCrossing: Row index too large
} }
if( $aCol >= $this->nbrCols ) { if( $aCol >= $this->nbrCols ) {
JpGraphError::RaiseL(28006,$aCol); JpGraphError::RaiseL(28006,$aCol);
//'isobarVCrossing: Col index too large //'isobarVCrossing: Col index too large
} }
   
$v1 = $this->dataPoints[$aRow][$aCol]; $v1 = $this->dataPoints[$aRow][$aCol];
$v2 = $this->dataPoints[$aRow+1][$aCol]; $v2 = $this->dataPoints[$aRow+1][$aCol];
   
return ($aIsobar-$v1)*($aIsobar-$v2) < 0 ; return ($aIsobar-$v1)*($aIsobar-$v2) < 0 ;
   
} }
   
/** /**
* Determine all edges, horizontal and vertical that the specified isobar crosses. The crossings * Determine all edges, horizontal and vertical that the specified isobar crosses. The crossings
* are recorded in the two edge matrices. * are recorded in the two edge matrices.
* *
* @param $aIsobar The value of the isobar to be checked * @param $aIsobar The value of the isobar to be checked
*/ */
function determineIsobarEdgeCrossings($aIsobar) { function determineIsobarEdgeCrossings($aIsobar) {
   
$ib = $this->isobarValues[$aIsobar]; $ib = $this->isobarValues[$aIsobar];
   
for ($i = 0; $i < $this->nbrRows-1; $i++) { for ($i = 0; $i < $this->nbrRows-1; $i++) {
for ($j = 0; $j < $this->nbrCols-1; $j++) { for ($j = 0; $j < $this->nbrCols-1; $j++) {
$this->edges[HORIZ_EDGE][$i][$j] = $this->isobarHCrossing($i,$j,$ib); $this->edges[HORIZ_EDGE][$i][$j] = $this->isobarHCrossing($i,$j,$ib);
$this->edges[VERT_EDGE][$i][$j] = $this->isobarVCrossing($i,$j,$ib); $this->edges[VERT_EDGE][$i][$j] = $this->isobarVCrossing($i,$j,$ib);
} }
} }
   
// We now have the bottom and rightmost edges unsearched // We now have the bottom and rightmost edges unsearched
for ($i = 0; $i < $this->nbrRows-1; $i++) { for ($i = 0; $i < $this->nbrRows-1; $i++) {
$this->edges[VERT_EDGE][$i][$j] = $this->isobarVCrossing($i,$this->nbrCols-1,$ib); $this->edges[VERT_EDGE][$i][$j] = $this->isobarVCrossing($i,$this->nbrCols-1,$ib);
} }
for ($j = 0; $j < $this->nbrCols-1; $j++) { for ($j = 0; $j < $this->nbrCols-1; $j++) {
$this->edges[HORIZ_EDGE][$i][$j] = $this->isobarHCrossing($this->nbrRows-1,$j,$ib); $this->edges[HORIZ_EDGE][$i][$j] = $this->isobarHCrossing($this->nbrRows-1,$j,$ib);
} }
   
} }
   
/** /**
* Return the normalized coordinates for the crossing of the specified edge with the specified * Return the normalized coordinates for the crossing of the specified edge with the specified
* isobar- The crossing is simpy detrmined with a linear interpolation between the two vertices * isobar- The crossing is simpy detrmined with a linear interpolation between the two vertices
* on each side of the edge and the value of the isobar * on each side of the edge and the value of the isobar
* *
* @param $aRow Row of edge * @param $aRow Row of edge
* @param $aCol Column of edge * @param $aCol Column of edge
* @param $aEdgeDir Determine if this is a horizontal or vertical edge * @param $aEdgeDir Determine if this is a horizontal or vertical edge
* @param $ib The isobar value * @param $ib The isobar value
* @return unknown_type * @return unknown_type
*/ */
function getCrossingCoord($aRow,$aCol,$aEdgeDir,$aIsobarVal) { function getCrossingCoord($aRow,$aCol,$aEdgeDir,$aIsobarVal) {
   
// In order to avoid numerical problem when two vertices are very close // In order to avoid numerical problem when two vertices are very close
// we have to check and avoid dividing by close to zero denumerator. // we have to check and avoid dividing by close to zero denumerator.
if( $aEdgeDir == HORIZ_EDGE ) { if( $aEdgeDir == HORIZ_EDGE ) {
$d = abs($this->dataPoints[$aRow][$aCol] - $this->dataPoints[$aRow][$aCol+1]); $d = abs($this->dataPoints[$aRow][$aCol] - $this->dataPoints[$aRow][$aCol+1]);
if( $d > 0.001 ) { if( $d > 0.001 ) {
$xcoord = $aCol + abs($aIsobarVal - $this->dataPoints[$aRow][$aCol]) / $d; $xcoord = $aCol + abs($aIsobarVal - $this->dataPoints[$aRow][$aCol]) / $d;
} }
else { else {
$xcoord = $aCol; $xcoord = $aCol;
} }
$ycoord = $aRow; $ycoord = $aRow;
} }
else { else {
$d = abs($this->dataPoints[$aRow][$aCol] - $this->dataPoints[$aRow+1][$aCol]); $d = abs($this->dataPoints[$aRow][$aCol] - $this->dataPoints[$aRow+1][$aCol]);
if( $d > 0.001 ) { if( $d > 0.001 ) {
$ycoord = $aRow + abs($aIsobarVal - $this->dataPoints[$aRow][$aCol]) / $d; $ycoord = $aRow + abs($aIsobarVal - $this->dataPoints[$aRow][$aCol]) / $d;
} }
else { else {
$ycoord = $aRow; $ycoord = $aRow;
} }
$xcoord = $aCol; $xcoord = $aCol;
} }
if( $this->invert ) { if( $this->invert ) {
$ycoord = $this->nbrRows-1 - $ycoord; $ycoord = $this->nbrRows-1 - $ycoord;
} }
return array($xcoord,$ycoord); return array($xcoord,$ycoord);
   
} }
   
/** /**
* In order to avoid all kinds of unpleasent extra checks and complex boundary * In order to avoid all kinds of unpleasent extra checks and complex boundary
* controls for the degenerated case where the contour levels exactly crosses * controls for the degenerated case where the contour levels exactly crosses
* one of the vertices we add a very small delta (0.1%) to the data point value. * one of the vertices we add a very small delta (0.1%) to the data point value.
* This has no visible affect but it makes the code sooooo much cleaner. * This has no visible affect but it makes the code sooooo much cleaner.
* *
*/ */
function adjustDataPointValues() { function adjustDataPointValues() {
   
$ni = count($this->isobarValues); $ni = count($this->isobarValues);
for ($k = 0; $k < $ni; $k++) { for ($k = 0; $k < $ni; $k++) {
$ib = $this->isobarValues[$k]; $ib = $this->isobarValues[$k];
for ($row = 0 ; $row < $this->nbrRows-1; ++$row) { for ($row = 0 ; $row < $this->nbrRows-1; ++$row) {
for ($col = 0 ; $col < $this->nbrCols-1; ++$col ) { for ($col = 0 ; $col < $this->nbrCols-1; ++$col ) {
if( abs($this->dataPoints[$row][$col] - $ib) < 0.0001 ) { if( abs($this->dataPoints[$row][$col] - $ib) < 0.0001 ) {
$this->dataPoints[$row][$col] += $this->dataPoints[$row][$col]*0.001; $this->dataPoints[$row][$col] += $this->dataPoints[$row][$col]*0.001;
} }
} }
} }
} }
   
} }
   
/** /**
* @param $aFlg * @param $aFlg
* @param $aBW * @param $aBW
* @return unknown_type * @return unknown_type
*/ */
function UseHighContrastColor($aFlg=true,$aBW=false) { function UseHighContrastColor($aFlg=true,$aBW=false) {
$this->highcontrast = $aFlg; $this->highcontrast = $aFlg;
$this->highcontrastbw = $aBW; $this->highcontrastbw = $aBW;
} }
   
/** /**
* Calculate suitable colors for each defined isobar * Calculate suitable colors for each defined isobar
* *
*/ */
function CalculateColors() { function CalculateColors() {
if ( $this->highcontrast ) { if ( $this->highcontrast ) {
if ( $this->highcontrastbw ) { if ( $this->highcontrastbw ) {
for ($ib = 0; $ib < $this->nbrIsobars; $ib++) { for ($ib = 0; $ib < $this->nbrIsobars; $ib++) {
$this->isobarColors[$ib] = 'black'; $this->isobarColors[$ib] = 'black';
} }
} }
else { else {
// Use only blue/red scale // Use only blue/red scale
$step = round(255/($this->nbrIsobars-1)); $step = round(255/($this->nbrIsobars-1));
for ($ib = 0; $ib < $this->nbrIsobars; $ib++) { for ($ib = 0; $ib < $this->nbrIsobars; $ib++) {
$this->isobarColors[$ib] = array($ib*$step, 50, 255-$ib*$step); $this->isobarColors[$ib] = array($ib*$step, 50, 255-$ib*$step);
} }
} }
} }
else { else {
$n = $this->nbrIsobars; $n = $this->nbrIsobars;
$v = 0; $step = 1 / ($this->nbrIsobars-1); $v = 0; $step = 1 / ($this->nbrIsobars-1);
for ($ib = 0; $ib < $this->nbrIsobars; $ib++) { for ($ib = 0; $ib < $this->nbrIsobars; $ib++) {
$this->isobarColors[$ib] = RGB::GetSpectrum($v); $this->isobarColors[$ib] = RGB::GetSpectrum($v);
$v += $step; $v += $step;
} }
} }
} }
   
/** /**
* This is where the main work is done. For each isobar the crossing of the edges are determined * This is where the main work is done. For each isobar the crossing of the edges are determined
* and then each cell is analyzed to find the 0, 2 or 4 crossings. Then the normalized coordinate * and then each cell is analyzed to find the 0, 2 or 4 crossings. Then the normalized coordinate
* for the crossings are determined and pushed on to the isobar stack. When the method is finished * for the crossings are determined and pushed on to the isobar stack. When the method is finished
* the $isobarCoord will hold one arrayfor each isobar where all the line segments that makes * the $isobarCoord will hold one arrayfor each isobar where all the line segments that makes
* up the contour plot are stored. * up the contour plot are stored.
* *
* @return array( $isobarCoord, $isobarValues, $isobarColors ) * @return array( $isobarCoord, $isobarValues, $isobarColors )
*/ */
function getIsobars() { function getIsobars() {
   
$this->adjustDataPointValues(); $this->adjustDataPointValues();
   
for ($isobar = 0; $isobar < $this->nbrIsobars; $isobar++) { for ($isobar = 0; $isobar < $this->nbrIsobars; $isobar++) {
   
$ib = $this->isobarValues[$isobar]; $ib = $this->isobarValues[$isobar];
$this->resetEdgeMatrices(); $this->resetEdgeMatrices();
$this->determineIsobarEdgeCrossings($isobar); $this->determineIsobarEdgeCrossings($isobar);
$this->isobarCoord[$isobar] = array(); $this->isobarCoord[$isobar] = array();
   
$ncoord = 0; $ncoord = 0;
   
for ($row = 0 ; $row < $this->nbrRows-1; ++$row) { for ($row = 0 ; $row < $this->nbrRows-1; ++$row) {
for ($col = 0 ; $col < $this->nbrCols-1; ++$col ) { for ($col = 0 ; $col < $this->nbrCols-1; ++$col ) {
   
// Find out how many crossings around the edges // Find out how many crossings around the edges
$n = 0; $n = 0;
if ( $this->edges[HORIZ_EDGE][$row][$col] ) $neigh[$n++] = array($row, $col, HORIZ_EDGE); if ( $this->edges[HORIZ_EDGE][$row][$col] ) $neigh[$n++] = array($row, $col, HORIZ_EDGE);
if ( $this->edges[HORIZ_EDGE][$row+1][$col] ) $neigh[$n++] = array($row+1,$col, HORIZ_EDGE); if ( $this->edges[HORIZ_EDGE][$row+1][$col] ) $neigh[$n++] = array($row+1,$col, HORIZ_EDGE);
if ( $this->edges[VERT_EDGE][$row][$col] ) $neigh[$n++] = array($row, $col, VERT_EDGE); if ( $this->edges[VERT_EDGE][$row][$col] ) $neigh[$n++] = array($row, $col, VERT_EDGE);
if ( $this->edges[VERT_EDGE][$row][$col+1] ) $neigh[$n++] = array($row, $col+1,VERT_EDGE); if ( $this->edges[VERT_EDGE][$row][$col+1] ) $neigh[$n++] = array($row, $col+1,VERT_EDGE);
   
if ( $n == 2 ) { if ( $n == 2 ) {
$n1=0; $n2=1; $n1=0; $n2=1;
$this->isobarCoord[$isobar][$ncoord++] = array( $this->isobarCoord[$isobar][$ncoord++] = array(
$this->getCrossingCoord($neigh[$n1][0],$neigh[$n1][1],$neigh[$n1][2],$ib), $this->getCrossingCoord($neigh[$n1][0],$neigh[$n1][1],$neigh[$n1][2],$ib),
$this->getCrossingCoord($neigh[$n2][0],$neigh[$n2][1],$neigh[$n2][2],$ib) ); $this->getCrossingCoord($neigh[$n2][0],$neigh[$n2][1],$neigh[$n2][2],$ib) );
} }
elseif ( $n == 4 ) { elseif ( $n == 4 ) {
// We must determine how to connect the edges either northwest->southeast or // We must determine how to connect the edges either northwest->southeast or
// northeast->southwest. We do that by calculating the imaginary m