|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* JPGraph v4.0.3 |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Amenadiel\JpGraph\Plot; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* File: JPGRAPH_MESHINTERPOLATE.INC.PHP |
|
11
|
|
|
* // Description: Utility class to do mesh linear interpolation of a matrix |
|
12
|
|
|
* // Created: 2009-03-09 |
|
13
|
|
|
* // Ver: $Id: jpgraph_meshinterpolate.inc.php 1709 2009-07-30 08:00:08Z ljp $ |
|
14
|
|
|
* // |
|
15
|
|
|
* // Copyright (c) Asial Corporation. All rights reserved. |
|
16
|
|
|
* |
|
17
|
|
|
* @param mixed $aFactor |
|
18
|
|
|
* @param mixed $aData |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Utility function to do linear mesh interpolation. |
|
23
|
|
|
* |
|
24
|
|
|
* @param $aDat Matrix to interpolate |
|
25
|
|
|
* @param $aFactor Interpolation factor |
|
26
|
|
|
*/ |
|
27
|
|
|
function doMeshInterpolate(&$aData, $aFactor) |
|
28
|
|
|
{ |
|
29
|
|
|
$m = new MeshInterpolate(); |
|
30
|
|
|
$aData = $m->Linear($aData, $aFactor); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Utility class to interpolate a given data matrix. |
|
35
|
|
|
*/ |
|
36
|
|
|
class MeshInterpolate |
|
37
|
|
|
{ |
|
38
|
|
|
private $data = []; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Calculate the mid points of the given rectangle which has its top left |
|
42
|
|
|
* corner at $row,$col. The $aFactordecides how many spliots should be done. |
|
43
|
|
|
* i.e. how many more divisions should be done recursively. |
|
44
|
|
|
* |
|
45
|
|
|
* @param $row Top left corner of square to work with |
|
46
|
|
|
* @param $col Top left corner of square to work with |
|
47
|
|
|
* $param $aFactor In how many subsquare should we split this square. A value of 1 indicates that no action |
|
48
|
|
|
* @param mixed $aRow |
|
49
|
|
|
* @param mixed $aCol |
|
50
|
|
|
* @param mixed $aFactor |
|
51
|
|
|
*/ |
|
52
|
1 |
|
public function IntSquare($aRow, $aCol, $aFactor) |
|
53
|
|
|
{ |
|
54
|
1 |
|
if ($aFactor <= 1) { |
|
55
|
1 |
|
return; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
1 |
|
$step = pow(2, $aFactor - 1); |
|
59
|
|
|
|
|
60
|
1 |
|
$v0 = $this->data[$aRow][$aCol]; |
|
61
|
1 |
|
$v1 = $this->data[$aRow][$aCol + $step]; |
|
62
|
1 |
|
$v2 = $this->data[$aRow + $step][$aCol]; |
|
63
|
1 |
|
$v3 = $this->data[$aRow + $step][$aCol + $step]; |
|
64
|
|
|
|
|
65
|
1 |
|
$this->data[$aRow][$aCol + $step / 2] = ($v0 + $v1) / 2; |
|
66
|
1 |
|
$this->data[$aRow + $step / 2][$aCol] = ($v0 + $v2) / 2; |
|
67
|
1 |
|
$this->data[$aRow + $step][$aCol + $step / 2] = ($v2 + $v3) / 2; |
|
68
|
1 |
|
$this->data[$aRow + $step / 2][$aCol + $step] = ($v1 + $v3) / 2; |
|
69
|
1 |
|
$this->data[$aRow + $step / 2][$aCol + $step / 2] = ($v0 + $v1 + $v2 + $v3) / 4; |
|
70
|
|
|
|
|
71
|
1 |
|
$this->IntSquare($aRow, $aCol, $aFactor - 1); |
|
72
|
1 |
|
$this->IntSquare($aRow, $aCol + $step / 2, $aFactor - 1); |
|
73
|
1 |
|
$this->IntSquare($aRow + $step / 2, $aCol, $aFactor - 1); |
|
74
|
1 |
|
$this->IntSquare($aRow + $step / 2, $aCol + $step / 2, $aFactor - 1); |
|
75
|
1 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Interpolate values in a matrice so that the total number of data points |
|
79
|
|
|
* in vert and horizontal axis are $aIntNbr more. For example $aIntNbr=2 will |
|
80
|
|
|
* make the data matrice have tiwce as many vertical and horizontal dta points. |
|
81
|
|
|
* |
|
82
|
|
|
* Note: This will blow up the matrcide in memory size in the order of $aInNbr^2 |
|
83
|
|
|
* |
|
84
|
|
|
* @param $ &$aData The original data matricde |
|
|
|
|
|
|
85
|
|
|
* @param $aInNbr Interpolation factor |
|
86
|
|
|
* @param mixed $aIntFactor |
|
87
|
|
|
* @param mixed $aData |
|
88
|
|
|
* |
|
89
|
|
|
* @return the interpolated matrice |
|
90
|
|
|
*/ |
|
91
|
1 |
|
public function Linear(&$aData, $aIntFactor) |
|
92
|
|
|
{ |
|
93
|
1 |
|
$step = pow(2, $aIntFactor - 1); |
|
94
|
|
|
|
|
95
|
1 |
|
$orig_cols = safe_count($aData[0]); |
|
96
|
1 |
|
$orig_rows = safe_count($aData); |
|
97
|
|
|
// Number of new columns/rows |
|
98
|
|
|
// N = (a-1) * 2^(f-1) + 1 |
|
99
|
1 |
|
$p = pow(2, $aIntFactor - 1); |
|
100
|
1 |
|
$new_cols = $p * ($orig_cols - 1) + 1; |
|
101
|
1 |
|
$new_rows = $p * ($orig_rows - 1) + 1; |
|
102
|
|
|
|
|
103
|
1 |
|
$this->data = array_fill(0, $new_rows, array_fill(0, $new_cols, 0)); |
|
104
|
|
|
// Initialize the new matrix with the values that we know |
|
105
|
1 |
|
for ($i = 0; $i < $new_rows; ++$i) { |
|
106
|
1 |
|
for ($j = 0; $j < $new_cols; ++$j) { |
|
107
|
1 |
|
$v = 0; |
|
108
|
1 |
|
if (($i % $step == 0) && ($j % $step == 0)) { |
|
109
|
1 |
|
$v = $aData[$i / $step][$j / $step]; |
|
110
|
|
|
} |
|
111
|
1 |
|
$this->data[$i][$j] = $v; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
1 |
|
for ($i = 0; $i < $new_rows - 1; $i += $step) { |
|
116
|
1 |
|
for ($j = 0; $j < $new_cols - 1; $j += $step) { |
|
117
|
1 |
|
$this->IntSquare($i, $j, $aIntFactor); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
1 |
|
return $this->data; |
|
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|