|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* PHPCoord. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Doug Wright |
|
6
|
|
|
*/ |
|
7
|
|
|
declare(strict_types=1); |
|
8
|
|
|
|
|
9
|
|
|
namespace PHPCoord\CoordinateOperation; |
|
10
|
|
|
|
|
11
|
|
|
use function assert; |
|
12
|
|
|
use function count; |
|
13
|
|
|
use function min; |
|
14
|
|
|
|
|
15
|
|
|
trait BilinearInterpolation |
|
16
|
|
|
{ |
|
17
|
|
|
protected float $startX; |
|
18
|
|
|
protected float $endX; |
|
19
|
|
|
protected float $startY; |
|
20
|
|
|
protected float $endY; |
|
21
|
|
|
protected int $numberOfColumns; |
|
22
|
|
|
protected int $numberOfRows; |
|
23
|
|
|
protected float $columnGridInterval; |
|
24
|
|
|
protected float $rowGridInterval; |
|
25
|
|
|
|
|
26
|
44 |
|
public function interpolate( |
|
27
|
|
|
float $x, |
|
28
|
|
|
float $y |
|
29
|
|
|
): array { |
|
30
|
44 |
|
$corners = $this->getCorners($x, $y); |
|
31
|
|
|
|
|
32
|
44 |
|
$dx = $corners['lowerRight']->getX() === $corners['lowerLeft']->getX() ? 0 : (($x - $corners['lowerLeft']->getX()) / $this->columnGridInterval); |
|
33
|
44 |
|
$dy = $corners['upperLeft']->getY() === $corners['lowerLeft']->getY() ? 0 : (($y - $corners['lowerLeft']->getY()) / $this->rowGridInterval); |
|
34
|
|
|
|
|
35
|
44 |
|
$interpolations = []; |
|
36
|
44 |
|
for ($i = 0, $count = count($corners['lowerLeft']->getValues()); $i < $count; ++$i) { |
|
37
|
|
|
// Interpolate value at lower row |
|
38
|
44 |
|
$y0 = $this->interpolateLinear($dx, $corners['lowerLeft']->getValues()[$i], $corners['lowerRight']->getValues()[$i]); |
|
39
|
|
|
// Interpolate value at upper row |
|
40
|
44 |
|
$y1 = $this->interpolateLinear($dx, $corners['upperLeft']->getValues()[$i], $corners['upperRight']->getValues()[$i]); |
|
41
|
|
|
// Interpolate between rows |
|
42
|
44 |
|
$xy = $this->interpolateLinear($dy, $y0, $y1); |
|
43
|
44 |
|
$interpolations[] = $xy; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
44 |
|
return $interpolations; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Linear interpolation at point p, where p is between 0 and 1. |
|
51
|
|
|
*/ |
|
52
|
44 |
|
private function interpolateLinear(float $p, float $valueAt0, float $valueAt1): float |
|
53
|
|
|
{ |
|
54
|
44 |
|
assert($p >= 0 && $p <= 1); |
|
55
|
|
|
|
|
56
|
44 |
|
return $valueAt0 * (1 - $p) + $valueAt1 * $p; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return GridValues[] |
|
61
|
|
|
*/ |
|
62
|
44 |
|
private function getCorners(float $x, float $y): array |
|
63
|
|
|
{ |
|
64
|
44 |
|
$xIndex = (int) (($x - $this->startX) / $this->columnGridInterval); |
|
65
|
44 |
|
$yIndex = (int) (($y - $this->startY) / $this->rowGridInterval); |
|
66
|
44 |
|
$xIndexPlus1 = min($xIndex + 1, $this->numberOfColumns - 1); |
|
67
|
44 |
|
$yIndexPlus1 = min($yIndex + 1, $this->numberOfRows - 1); |
|
68
|
|
|
|
|
69
|
|
|
return [ |
|
70
|
44 |
|
'lowerLeft' => $this->getRecord($xIndex, $yIndex), |
|
|
|
|
|
|
71
|
44 |
|
'lowerRight' => $this->getRecord($xIndexPlus1, $yIndex), |
|
72
|
44 |
|
'upperLeft' => $this->getRecord($xIndex, $yIndexPlus1), |
|
73
|
44 |
|
'upperRight' => $this->getRecord($xIndexPlus1, $yIndexPlus1), |
|
74
|
|
|
]; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|