STLVertex::setCoordinatesArray()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/*
3
 * This file is part of the STL package.
4
 *
5
 * (c) Grosan Flaviu Gheorghe <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace php3d\stl;
12
13
/**
14
 * Class STLVertex. Stores STL vertex data.
15
 * @package php3d\stl
16
 */
17
class STLVertex
18
{
19
    /**
20
     * @var array
21
     */
22
    private $coordinatesArray;
23
24
    /**
25
     * @return array
26
     */
27
    public function getCoordinatesArray(): array
28
    {
29
        return $this->coordinatesArray;
30
    }
31
32
    /**
33
     * @param array $coordinatesArray
34
     * @return STLVertex
35
     */
36
    public function setCoordinatesArray(array $coordinatesArray): STLVertex
37
    {
38
        $this->coordinatesArray = $coordinatesArray;
39
        return $this;
40
    }
41
42
    // Class should never be instantiated directly, as it emulates constructor overloading.
43
    private function __construct()
44
    {
45
    }
46
47
    /**
48
     * Class constructor from an STL vertex string.
49
     *
50
     * Example vertex string:
51
     * outerloop
52
     *  vertex -71.74323 47.70205 4.666243
53
     *  vertex -72.13071 47.70205 4.932664
54
     *  vertex -72.1506 47.2273 4.905148
55
     * endloop
56
     *
57
     * @param string $stlVertexString
58
     * @return STLVertex
59
     */
60
    public static function fromString(string $stlVertexString) : STLVertex
61
    {
62
        $class = new self();
63
64
        preg_match_all("/vertex +(\-*\d+\.*\d*e*\-*\+*\d*) +(\-*\d+\.*\d*e*\-*\+*\d*) +(\-*\d+\.*\d*e*\-*\+*\d*)/",
65
            $stlVertexString, $matches);
66
67
        $coordinates = array(
68
            array((float)$matches[1][0], (float)$matches[2][0], (float)$matches[3][0]),
69
            array((float)$matches[1][1], (float)$matches[2][1], (float)$matches[3][1]),
70
            array((float)$matches[1][2], (float)$matches[2][2], (float)$matches[3][2])
71
        );
72
73
        $class->setCoordinatesArray($coordinates);
74
75
        return $class;
76
    }
77
78
    /**
79
     * Class constructor from an STL vertex array.
80
     *
81
     * Example vertex array:
82
     *
83
     * array(
84
     *  array(
85
     *      -71.74323, 47.70205, 4.666243
86
     *  ),
87
     *  array(
88
     *      -72.13071, 47.70205, 4.932664
89
     *  ),
90
     *  array(
91
     *      -72.1506, 47.2273, 4.905148
92
     *  )
93
     * )
94
     *
95
     * @param array $stlVertexArray
96
     * @return STLVertex
97
     */
98
    public static function fromArray(array $stlVertexArray) : STLVertex
99
    {
100
        $class = new self();
101
        $class->setCoordinatesArray($stlVertexArray);
102
        return $class;
103
    }
104
105
    /**
106
     * Wrapper for getCoordinatesArray.
107
     *
108
     * @return array
109
     */
110
    public function toArray() : array
111
    {
112
        return $this->getCoordinatesArray();
113
    }
114
115
    /**
116
     * Returns a vertex outerloop.
117
     *
118
     * @return string
119
     */
120
    public function toString() : string
121
    {
122
        $coordinates = $this->getCoordinatesArray();
123
        $string = "outerloop\n";
124
        foreach ($coordinates as $coordinate) {
125
            $string .= "    vertex " . implode(" ", $coordinate) . "\n";
126
        }
127
        $string .= "endloop";
128
129
        return $string;
130
    }
131
}