| 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 | 23 |  |     public function interpolateBilinear( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         float $x, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         float $y | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     ): array { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 23 |  |         $corners = $this->getCornersForBilinear($x, $y); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 23 |  |         $dx = ($x - $corners['lowerLeft']->getX()) / ($corners['lowerRight']->getX() - $corners['lowerLeft']->getX()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 23 |  |         $dy = ($y - $corners['lowerLeft']->getY()) / ($corners['upperLeft']->getY() - $corners['lowerLeft']->getY()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 23 |  |         $interpolations = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 23 |  |         for ($i = 0, $count = count($corners['lowerLeft']->getValues()); $i < $count; ++$i) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             //Interpolate value at lower row | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 23 |  |             $y0 = $this->interpolateLinear($dx, $corners['lowerLeft']->getValues()[$i], $corners['lowerRight']->getValues()[$i]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             //Interpolate value at upper row | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 23 |  |             $y1 = $this->interpolateLinear($dx, $corners['upperLeft']->getValues()[$i], $corners['upperRight']->getValues()[$i]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             //Interpolate between rows | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 23 |  |             $xy = $this->interpolateLinear($dy, $y0, $y1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 23 |  |             $interpolations[] = $xy; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 23 |  |         return $interpolations; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * Linear interpolation at point p, where p is between 0 and 1. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 23 |  |     private function interpolateLinear(float $p, float $valueAt0, float $valueAt1): float | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 23 |  |         assert($p >= 0 && $p <= 1); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 23 |  |         return $valueAt0 * (1 - $p) + $valueAt1 * $p; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @return GridValues[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 23 |  |     private function getCornersForBilinear(float $x, float $y): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 23 |  |         $xIndex = (int) (($x - $this->startX) / $this->columnGridInterval); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 23 |  |         $yIndex = (int) (($y - $this->startY) / $this->rowGridInterval); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 23 |  |         $xIndexPlus1 = min($xIndex + 1, $this->numberOfColumns); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 23 |  |         $yIndexPlus1 = min($yIndex + 1, $this->numberOfRows); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 23 |  |             'lowerLeft' => $this->getRecord($xIndex, $yIndex), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 23 |  |             'lowerRight' => $this->getRecord($xIndexPlus1, $yIndex), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 23 |  |             'upperLeft' => $this->getRecord($xIndex, $yIndexPlus1), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 23 |  |             'upperRight' => $this->getRecord($xIndexPlus1, $yIndexPlus1), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 76 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |  |