Mapping::getPosition()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 2
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace AmaTeam\Image\Projection\Type\CubeMap;
4
5
use AmaTeam\Image\Projection\Type\AbstractValidatingMapping;
6
use AmaTeam\Image\Projection\Type\CubeMap\Mapping\Face;
7
use AmaTeam\Image\Projection\Type\CubeMap\Mapping\Vector;
8
use BadMethodCallException;
9
10
class Mapping extends AbstractValidatingMapping
11
{
12
    /**
13
     * @var Face[]
14
     */
15
    private $faces;
16
    /**
17
     * @var string[]
18
     */
19
    private $faceNameMap;
20
    /**
21
     * @var int[]
22
     */
23
    private $inverseFaceNameMap;
24
25
    public function __construct()
26
    {
27
        $this->faces = Face::generateCubeFaces();
28
        $this->faceNameMap = Face::getNames();
29
        $this->inverseFaceNameMap = array_flip($this->faceNameMap);
30
    }
31
32
    /**
33
     * Converts UV mapping to latitude/longitude.
34
     *
35
     * @param int|string $faceIndex
36
     * @param float $u
37
     * @param float $v
38
     *
39
     * @return float[]
40
     */
41
    public function getCoordinates($faceIndex, $u, $v)
42
    {
43
        if (is_string($faceIndex)) {
44
            $faceIndex = $this->inverseFaceNameMap[$faceIndex];
45
        }
46
        $face = $this->faces[$faceIndex];
47
        $vector = $face->vectorize($u, $v);
48
        // mangling vector since classic XYZ don't map to cube map XYZ
49
        $vector = [$vector[2], $vector[0], $vector[1], $vector[3]];
50
        return Vector::toPolar($vector);
51
    }
52
53
    /**
54
     * @param float $longitude
55
     * @param float $latitude
56
     *
57
     * @return array Array of [Face, int (u), int (v)]
58
     */
59
    public function getPosition($latitude, $longitude)
60
    {
61
        $vector = Vector::fromPolar($latitude, $longitude);
62
        // mangling vector since classic XYZ don't map to cube map XYZ
63
        $vector = [$vector[1], $vector[2], $vector[0], 1];
64
        $dominant = Vector::getDominant($vector);
65
        $value = $vector[$dominant];
66
        $vector = Vector::multiply($vector, abs(1 / $value));
67
        $faceIndex = ($dominant * 2) + ($value < 0 ? 1 : 0);
68
        $face = $this->faces[$faceIndex];
69
        $position = $face->map($vector);
70
        return [
71
            $this->faceNameMap[$faceIndex],
72
            $position[0],
73
            $position[1]
74
        ];
75
    }
76
77
    /**
78
     * @param int|string $index
79
     * @return Face
80
     */
81
    public function getFace($index)
82
    {
83
        return $this->faces[$index];
84
    }
85
86
    /**
87
     * @inheritDoc
88
     */
89
    public function getFaces()
90
    {
91
        return $this->faceNameMap;
92
    }
93
}
94