NearestNeighbourReader   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 73
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A getColorAt() 0 15 1
1
<?php
2
3
namespace AmaTeam\Image\Projection\Type;
4
5
use AmaTeam\Image\Projection\API\SpecificationInterface;
6
use AmaTeam\Image\Projection\API\Tile\TileInterface;
7
use AmaTeam\Image\Projection\API\Type\MappingInterface;
8
use AmaTeam\Image\Projection\API\Type\ReaderInterface;
9
10
class NearestNeighbourReader implements ReaderInterface
11
{
12
    /**
13
     * @var MappingInterface
14
     */
15
    private $mapping;
16
    /**
17
     * @var TileInterface[][][]
18
     */
19
    private $tiles;
20
    /**
21
     * @var float
22
     */
23
    private $rowSize;
24
    /**
25
     * @var float
26
     */
27
    private $columnSize;
28
    /**
29
     * @var int
30
     */
31
    private $tileHeight;
32
    /**
33
     * @var int
34
     */
35
    private $tileWidth;
36
    /**
37
     * @var int
38
     */
39
    private $maxColumn;
40
    /**
41
     * @var int
42
     */
43
    private $maxRow;
44
45
    /**
46
     * @param SpecificationInterface $specification
47
     * @param MappingInterface $mapping
48
     * @param TileInterface[][][] $tiles
49
     */
50
    public function __construct(
51
        SpecificationInterface $specification,
52
        MappingInterface $mapping,
53
        array $tiles
54
    ) {
55
        $this->mapping = $mapping;
56
        $this->tiles = $tiles;
57
        $this->maxColumn = $specification->getLayout()->getWidth() - 1;
58
        $this->maxRow = $specification->getLayout()->getHeight() - 1;
59
        $this->columnSize = 1 / $specification->getLayout()->getWidth();
60
        $this->rowSize = 1 / $specification->getLayout()->getHeight();
61
        $this->tileWidth = $specification->getTileSize()->getWidth();
62
        $this->tileHeight = $specification->getTileSize()->getHeight();
63
    }
64
65
    /**
66
     * @inheritDoc
67
     */
68
    public function getColorAt($latitude, $longitude)
69
    {
70
        $position = $this->mapping->getPosition($latitude, $longitude);
71
        $u = $position[1] / $this->rowSize;
72
        $v = $position[2] / $this->columnSize;
73
        $column = min((int) $u, $this->maxColumn);
74
        $row = min((int) $v, $this->maxRow);
75
        $tileU = $u - $column;
76
        $tileV = $v - $row;
77
        $x = (int) min(0.5 + ($tileU * $this->tileWidth), $this->tileWidth - 1);
78
        $y = (int) min(0.5 + ($tileV * $this->tileHeight), $this->tileHeight - 1);
79
        return $this
80
            ->tiles[$position[0]][$column][$row]
81
            ->getImage()
82
            ->getColorAt($x, $y);
83
    }
84
}
85