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 STL. Provides functionality for parsing STL files. |
15
|
|
|
* @package php3d\stl |
16
|
|
|
*/ |
17
|
|
|
class STL |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $solidName; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private $facets = array(); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
|
|
public function getSolidName(): string |
33
|
|
|
{ |
34
|
|
|
return $this->solidName; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $solidName |
39
|
|
|
* @return STL |
40
|
|
|
*/ |
41
|
|
|
public function setSolidName(string $solidName): STL |
42
|
|
|
{ |
43
|
|
|
$this->solidName = $solidName; |
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// Class should never be instantiated directly, as it emulates constructor overloading. |
48
|
|
|
private function __construct() |
49
|
|
|
{ |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Class constructor from an STL string. |
54
|
|
|
* |
55
|
|
|
* Example STL file string: see STLTest $stlFileString property. |
56
|
|
|
* |
57
|
|
|
* @param string $stlFileContentString |
58
|
|
|
* @return STL |
59
|
|
|
*/ |
60
|
|
|
public static function fromString(string $stlFileContentString) : STL |
61
|
|
|
{ |
62
|
|
|
$class = new self(); |
63
|
|
|
|
64
|
|
|
// Extract name. |
65
|
|
|
$lines = explode("\n", $stlFileContentString); |
66
|
|
|
preg_match("/solid (.+)/", $lines[0], $matches); |
67
|
|
|
$class->setSolidName($matches[1]); |
68
|
|
|
|
69
|
|
|
// Extract facets. |
70
|
|
|
foreach ($lines as $line) { |
71
|
|
|
if (preg_match("/facet normal (.+)/", $line)) { |
72
|
|
|
// Get this line and the following, making a block of facets. |
73
|
|
|
$string = next($lines) . "\n"; |
74
|
|
|
$string .= next($lines) . "\n"; |
75
|
|
|
$string .= next($lines) . "\n"; |
76
|
|
|
$string .= next($lines) . "\n"; |
77
|
|
|
$string .= next($lines) . "\n"; |
78
|
|
|
$string .= next($lines) . "\n"; |
79
|
|
|
$string .= next($lines) . "\n"; |
80
|
|
|
$class->addFacetNormal(STLFacetNormal::fromString($string)); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $class; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Class constructor from an STL array. |
89
|
|
|
* |
90
|
|
|
* Example STL file array: see STLTest $stlFileArray property. |
91
|
|
|
* |
92
|
|
|
* @param array $stlFileArray |
93
|
|
|
* @return STL |
94
|
|
|
*/ |
95
|
|
|
public static function fromArray(array $stlFileArray) : STL |
96
|
|
|
{ |
97
|
|
|
$class = new self(); |
98
|
|
|
|
99
|
|
|
$class->setSolidName($stlFileArray["name"]); |
100
|
|
|
foreach ($stlFileArray["facets"] as $facet) { |
101
|
|
|
$class->addFacetNormal(STLFacetNormal::fromArray($facet)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $class; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Adds a new facet normal object. |
109
|
|
|
* |
110
|
|
|
* @param STLFacetNormal $facetNormal |
111
|
|
|
* @return STL |
112
|
|
|
*/ |
113
|
|
|
public function addFacetNormal(STLFacetNormal $facetNormal) : STL |
114
|
|
|
{ |
115
|
|
|
$this->facets[] = $facetNormal; |
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Returns the number of facets in this STL object. |
121
|
|
|
* |
122
|
|
|
* @return int |
123
|
|
|
*/ |
124
|
|
|
public function getFacetNormalCount() : int |
125
|
|
|
{ |
126
|
|
|
return count($this->facets); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Returns facet normal object at position. |
131
|
|
|
* |
132
|
|
|
* @param int $position |
133
|
|
|
* @return STLFacetNormal |
134
|
|
|
*/ |
135
|
|
|
public function getFacetNormal(int $position) : STLFacetNormal |
136
|
|
|
{ |
137
|
|
|
return $this->facets[$position]; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Sets a facet normal at a given position. |
142
|
|
|
* |
143
|
|
|
* @param int $position |
144
|
|
|
* @param STLFacetNormal $facetNormal |
145
|
|
|
* @return STL |
146
|
|
|
*/ |
147
|
|
|
public function setFacetNormal(int $position, STLFacetNormal $facetNormal) : STL |
148
|
|
|
{ |
149
|
|
|
$this->facets[$position] = $facetNormal; |
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Deletes a facet normal at a given position. |
155
|
|
|
* |
156
|
|
|
* @param int $position |
157
|
|
|
* @return STL |
158
|
|
|
*/ |
159
|
|
|
public function deleteFacetNormal(int $position) : STL |
160
|
|
|
{ |
161
|
|
|
$facets = $this->facets; |
162
|
|
|
unset($facets[$position]); |
163
|
|
|
$this->facets = array_values($facets); |
164
|
|
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Converts STL back to array. |
169
|
|
|
* |
170
|
|
|
* @return array |
171
|
|
|
*/ |
172
|
|
|
public function toArray() : array |
173
|
|
|
{ |
174
|
|
|
$facets = array(); |
175
|
|
|
$count = $this->getFacetNormalCount(); |
176
|
|
|
|
177
|
|
|
for ($i = 0; $i < $count; $i++) { |
178
|
|
|
$facets[] = $this->getFacetNormal($i)->toArray(); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return array( |
182
|
|
|
"name" => $this->getSolidName(), |
183
|
|
|
"facets" => $facets |
184
|
|
|
); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Converts STL back to string. |
189
|
|
|
* |
190
|
|
|
* @return string |
191
|
|
|
*/ |
192
|
|
|
public function toString() : string |
193
|
|
|
{ |
194
|
|
|
$string = "solid " . $this->getSolidName() . "\n"; |
195
|
|
|
|
196
|
|
|
$count = $this->getFacetNormalCount(); |
197
|
|
|
|
198
|
|
|
for ($i = 0; $i < $count; $i++) { |
199
|
|
|
$string .= $this->getFacetNormal($i)->toString() . "\n"; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$string .= "endsolid"; |
203
|
|
|
|
204
|
|
|
return $string; |
205
|
|
|
} |
206
|
|
|
} |