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 STLFacetNormal. Stores facet normal data. |
15
|
|
|
* @package php3d\stl |
16
|
|
|
*/ |
17
|
|
|
class STLFacetNormal |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private $coordinatesArray; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var STLVertex |
26
|
|
|
*/ |
27
|
|
|
private $vertex; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return STLVertex |
31
|
|
|
*/ |
32
|
|
|
public function getVertex(): STLVertex |
33
|
|
|
{ |
34
|
|
|
return $this->vertex; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param STLVertex $vertex |
39
|
|
|
* @return STLFacetNormal |
40
|
|
|
*/ |
41
|
|
|
public function setVertex(STLVertex $vertex): STLFacetNormal |
42
|
|
|
{ |
43
|
|
|
$this->vertex = $vertex; |
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
|
|
public function getCoordinatesArray(): array |
51
|
|
|
{ |
52
|
|
|
return $this->coordinatesArray; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param array $coordinatesArray |
57
|
|
|
* @return STLFacetNormal |
58
|
|
|
*/ |
59
|
|
|
public function setCoordinatesArray(array $coordinatesArray): STLFacetNormal |
60
|
|
|
{ |
61
|
|
|
$this->coordinatesArray = $coordinatesArray; |
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// Class should never be instantiated directly, as it emulates constructor overloading. |
66
|
|
|
private function __construct() |
67
|
|
|
{ |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Class constructor from an STL facet normal string. |
72
|
|
|
* |
73
|
|
|
* Example facet normal string: |
74
|
|
|
* |
75
|
|
|
* facet normal 0.5651193 -0.07131607 0.8219211 |
76
|
|
|
* outerloop |
77
|
|
|
* vertex -71.74323 47.70205 4.666243 |
78
|
|
|
* vertex -72.13071 47.70205 4.932664 |
79
|
|
|
* vertex -72.1506 47.2273 4.905148 |
80
|
|
|
* endloop |
81
|
|
|
* endfacet |
82
|
|
|
* |
83
|
|
|
* @param string $stlFacetNormalString |
84
|
|
|
* @return STLFacetNormal |
85
|
|
|
*/ |
86
|
|
|
public static function fromString(string $stlFacetNormalString) : STLFacetNormal |
87
|
|
|
{ |
88
|
|
|
$class = new self(); |
89
|
|
|
|
90
|
|
|
preg_match("/facet normal +(\-*\d+\.*\d*e*\-*\+*\d*) +(\-*\d+\.*\d*e*\-*\+*\d*) +(\-*\d+\.*\d*e*\-*\+*\d*)/", |
91
|
|
|
$stlFacetNormalString, $matches); |
92
|
|
|
$class->setCoordinatesArray(array((float)$matches[1], (float)$matches[2], (float)$matches[3])); |
93
|
|
|
|
94
|
|
|
$lines = explode("\n", $stlFacetNormalString); |
95
|
|
|
$vertex = ""; |
96
|
|
|
for ($i = 1; $i < count($lines) - 1; $i++) { |
97
|
|
|
$vertex .= $lines[$i]; |
98
|
|
|
} |
99
|
|
|
$class->setVertex(STLVertex::fromString($vertex)); |
100
|
|
|
|
101
|
|
|
return $class; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Class constructor from an STL facet normal array. |
106
|
|
|
* |
107
|
|
|
* Example facet normal array: |
108
|
|
|
* array( |
109
|
|
|
* "coordinates" => array(0.5651193, -0.07131607, 0.8219211), |
110
|
|
|
* "vertex" => array( |
111
|
|
|
* array( |
112
|
|
|
* -71.74323, 47.70205, 4.666243 |
113
|
|
|
* ), |
114
|
|
|
* array( |
115
|
|
|
* -72.13071, 47.70205, 4.932664 |
116
|
|
|
* ), |
117
|
|
|
* array( |
118
|
|
|
* -72.1506, 47.2273, 4.905148 |
119
|
|
|
* ) |
120
|
|
|
* ) |
121
|
|
|
* ) |
122
|
|
|
* |
123
|
|
|
* @param array $stlFacetNormalArray |
124
|
|
|
* @return STLFacetNormal |
125
|
|
|
*/ |
126
|
|
|
public static function fromArray(array $stlFacetNormalArray) : STLFacetNormal |
127
|
|
|
{ |
128
|
|
|
$class = new self(); |
129
|
|
|
$class->setCoordinatesArray($stlFacetNormalArray["coordinates"]); |
130
|
|
|
$class->setVertex(STLVertex::fromArray($stlFacetNormalArray["vertex"])); |
131
|
|
|
return $class; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Returns facet normal as array. |
136
|
|
|
* |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
|
|
public function toArray() : array |
140
|
|
|
{ |
141
|
|
|
return array( |
142
|
|
|
"coordinates" => $this->getCoordinatesArray(), |
143
|
|
|
"vertex" => $this->getVertex()->toArray() |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Returns facet as string. |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
public function toString() : string |
153
|
|
|
{ |
154
|
|
|
$string = "facet normal " . implode(" ", $this->getCoordinatesArray()) . "\n"; |
155
|
|
|
$string .= $this->getVertex()->toString() . "\n"; |
156
|
|
|
$string .= "endfacet"; |
157
|
|
|
return $string; |
158
|
|
|
} |
159
|
|
|
} |