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