Depreciate MySQL and GD image graphs
[contractdashboard.git] / lib / jpgraph_contour.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
<?php
/*=======================================================================
// File:        JPGRAPH_CONTOUR.PHP
// Description: Contour plot
// Created:     2009-03-08
// Ver:         $Id: jpgraph_contour.php 1870 2009-09-29 04:24:18Z ljp $
//
// Copyright (c) Aditus Consulting. All rights reserved.
//========================================================================
*/
require_once('jpgraph_meshinterpolate.inc.php');
define('HORIZ_EDGE',0);
define('VERT_EDGE',1);
 
/**
 * 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
 * data matrix models an equspaced X-Y mesh of datavalues corresponding to the Z
 * values.
 *
 */
class Contour {
 
    private $dataPoints = array();
    private $nbrCols=0,$nbrRows=0;
    private $horizEdges = array(), $vertEdges=array();
    private $isobarValues = array();
    private $stack = null;
    private $isobarCoord = array();
    private $nbrIsobars = 10, $isobarColors = array();
    private $invert = true;
    private $highcontrast = false, $highcontrastbw = false;
 
    /**
     * Create a new contour level "algorithm machine".
     * @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
     * 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
     * contour plot.
     * @return an instance of the contour algorithm
     */
    function __construct($aMatrix,$aIsobars=10, $aColors=null) {
 
        $this->nbrRows = count($aMatrix);
        $this->nbrCols = count($aMatrix[0]);
        $this->dataPoints = $aMatrix;
 
        if( is_array($aIsobars) ) {
            // use the isobar values supplied
            $this->nbrIsobars = count($aIsobars);
            $this->isobarValues = $aIsobars;
        }
        else {
            // Determine the isobar values automatically
            $this->nbrIsobars = $aIsobars;
            list($min,$max) = $this->getMinMaxVal();
            $stepSize = ($max-$min) / $aIsobars ;
            $isobar = $min+$stepSize/2;
            for ($i = 0; $i < $aIsobars; $i++) {
                $this->isobarValues[$i] = $isobar;
                $isobar += $stepSize;
            }
        }
 
        if( $aColors !== null && count($aColors) > 0 ) {
 
            if( !is_array($aColors) ) {
                JpGraphError::RaiseL(28001);
                //'Third argument to Contour must be an array of colors.'
            }
 
            if( count($aColors) != count($this->isobarValues) ) {
                JpGraphError::RaiseL(28002);
                //'Number of colors must equal the number of isobar lines specified';
            }
 
            $this->isobarColors = $aColors;
        }
    }
 
    /**
     * Flip the plot around the Y-coordinate. This has the same affect as flipping the input
     * data matrice
     *
     * @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)
     */
    function SetInvert($aFlg=true) {
        $this->invert = $aFlg;
    }
 
    /**
     * Find the min and max values in the data matrice
     *
     * @return array(min_value,max_value)
     */
    function getMinMaxVal() {
        $min = $this->dataPoints[0][0];
        $max = $this->dataPoints[0][0];
        for ($i = 0; $i < $this->nbrRows; $i++) {
            if( ($mi=min($this->dataPoints[$i])) < $min )  $min = $mi;
            if( ($ma=max($this->dataPoints[$i])) > $max )  $max = $ma;
        }
        return array($min,$max);
    }
 
    /**
     * Reset the two matrices that keeps track on where the isobars crosses the
     * horizontal and vertical edges
     */
    function resetEdgeMatrices() {
        for ($k = 0; $k < 2; $k++) {
            for ($i = 0; $i <= $this->nbrRows; $i++) {
                for ($j = 0; $j <= $this->nbrCols; $j++) {
                    $this->edges[$k][$i][$j] = false;
                }
            }
        }
    }
 
    /**
     * 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 $aCol Col index of edge to be checked
     * @param $aIsobar Isobar value
     * @return true if the isobar is crossing this edge
     */
    function isobarHCrossing($aRow,$aCol,$aIsobar) {
 
        if( $aCol >= $this->nbrCols-1 ) {
            JpGraphError::RaiseL(28003,$aCol);
            //'ContourPlot Internal Error: isobarHCrossing: Coloumn index too large (%d)'
        }
        if( $aRow >= $this->nbrRows ) {
            JpGraphError::RaiseL(28004,$aRow);
            //'ContourPlot Internal Error: isobarHCrossing: Row index too large (%d)'
        }
 
        $v1 = $this->dataPoints[$aRow][$aCol];
        $v2 = $this->dataPoints[$aRow][$aCol+1];
 
        return ($aIsobar-$v1)*($aIsobar-$v2) < 0 ;
 
    }
 
    /**
     * 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 $aCol Col index of edge to be checked
     * @param $aIsobar Isobar value
     * @return true if the isobar is crossing this edge
     */
    function isobarVCrossing($aRow,$aCol,$aIsobar) {
 
        if( $aRow >= $this->nbrRows-1) {
            JpGraphError::RaiseL(28005,$aRow);
            //'isobarVCrossing: Row index too large
        }
        if( $aCol >= $this->nbrCols ) {
            JpGraphError::RaiseL(28006,$aCol);
            //'isobarVCrossing: Col index too large
        }
 
        $v1 = $this->dataPoints[$aRow][$aCol];
        $v2 = $this->dataPoints[$aRow+1][$aCol];
 
        return ($aIsobar-$v1)*($aIsobar-$v2) < 0 ;
 
    }
 
    /**
     * Determine all edges, horizontal and vertical that the specified isobar crosses. The crossings
     * are recorded in the two edge matrices.
     *
     * @param $aIsobar The value of the isobar to be checked
     */
    function determineIsobarEdgeCrossings($aIsobar) {
 
        $ib = $this->isobarValues[$aIsobar];
 
        for ($i = 0; $i < $this->nbrRows-1; $i++) {
            for ($j = 0; $j < $this->nbrCols-1; $j++) {
                $this->edges[HORIZ_EDGE][$i][$j] = $this->isobarHCrossing($i,$j,$ib);
                $this->edges[VERT_EDGE][$i][$j] = $this->isobarVCrossing($i,$j,$ib);
            }
        }
 
        // We now have the bottom and rightmost edges unsearched
        for ($i = 0; $i < $this->nbrRows-1; $i++) {
            $this->edges[VERT_EDGE][$i][$j] = $this->isobarVCrossing($i,$this->nbrCols-1,$ib);
        }
        for ($j = 0; $j < $this->nbrCols-1; $j++) {
            $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
     * 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
     *
     * @param $aRow Row of edge
     * @param $aCol Column of edge
     * @param $aEdgeDir Determine if this is a horizontal or vertical edge
     * @param $ib The isobar value
     * @return unknown_type
     */
    function getCrossingCoord($aRow,$aCol,$aEdgeDir,$aIsobarVal) {
 
        // In order to avoid numerical problem when two vertices are very close
        // we have to check and avoid dividing by close to zero denumerator.
        if( $aEdgeDir == HORIZ_EDGE ) {
            $d = abs($this->dataPoints[$aRow][$aCol] - $this->dataPoints[$aRow][$aCol+1]);
            if( $d > 0.001 ) {
                $xcoord = $aCol + abs($aIsobarVal - $this->dataPoints[$aRow][$aCol]) / $d;
            }
            else {
                $xcoord = $aCol;
            }
            $ycoord = $aRow;
        }
        else {
            $d = abs($this->dataPoints[$aRow][$aCol] - $this->dataPoints[$aRow+1][$aCol]);
            if( $d > 0.001 ) {
                $ycoord = $aRow + abs($aIsobarVal - $this->dataPoints[$aRow][$aCol]) / $d;
            }
            else {
                $ycoord = $aRow;
            }
            $xcoord = $aCol;
        }
        if( $this->invert ) {
            $ycoord = $this->nbrRows-1 - $ycoord;
        }
        return array($xcoord,$ycoord);
 
    }
 
    /**
     * In order to avoid all kinds of unpleasent extra checks and complex boundary
     * 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.
     * This has no visible affect but it makes the code sooooo much cleaner.
     *
     */
    function adjustDataPointValues() {
 
        $ni = count($this->isobarValues);
        for ($k = 0; $k < $ni; $k++) {
            $ib = $this->isobarValues[$k];
            for ($row = 0 ; $row < $this->nbrRows-1; ++$row) {
                for ($col = 0 ; $col < $this->nbrCols-1; ++$col ) {
                    if( abs($this->dataPoints[$row][$col] - $ib) < 0.0001 ) {
                        $this->dataPoints[$row][$col] += $this->dataPoints[$row][$col]*0.001;
                    }
                }
            }
        }
 
    }
 
    /**
     * @param $aFlg
     * @param $aBW
     * @return unknown_type
     */
    function UseHighContrastColor($aFlg=true,$aBW=false) {
        $this->highcontrast = $aFlg;
        $this->highcontrastbw = $aBW;
    }
 
    /**
     * Calculate suitable colors for each defined isobar
     *
     */
    function CalculateColors() {
        if ( $this->highcontrast ) {
            if ( $this->highcontrastbw ) {
                for ($ib = 0; $ib < $this->nbrIsobars; $ib++) {
                    $this->isobarColors[$ib] = 'black';
                }
            }
            else {
                // Use only blue/red scale
                $step = round(255/($this->nbrIsobars-1));
                for ($ib = 0; $ib < $this->nbrIsobars; $ib++) {
                    $this->isobarColors[$ib] = array($ib*$step, 50, 255-$ib*$step);
                }
            }
        }
        else {
            $n = $this->nbrIsobars;
            $v = 0; $step = 1 / ($this->nbrIsobars-1);
            for ($ib = 0; $ib < $this->nbrIsobars; $ib++) {
                $this->isobarColors[$ib] = RGB::GetSpectrum($v);
                $v += $step