forked from alexeygrek/stl-calc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstlcalc.php
206 lines (186 loc) · 7.67 KB
/
stlcalc.php
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
/*
* (c) ALEXEYGREK Studio
* https://alexeygrek.com
*/
class STLCalc {
// Properties
private $volume;
private $weight;
private $density = 1.04;
private $triangles_count;
private $triangles_data;
private $b_binary;
private $points;
private $fstl_handle;
private $fstl_path;
private $triangles;
private $flag = false;
// Initialises the STLCalc class by passing the path to the binary .stl file.
function __construct ( $filepath ) {
$b = $this->IsAscii ( $filepath );
if( !$b ) {
echo "BINARY STL Suspected.\n";
$this->b_binary = TRUE;
$this->fstl_handle = fopen ( $filepath, 'rb' );
$this->fstl_path = $filepath;
} else {
echo "ASCII STL Suspected.\n";
}
$this->triangles = array();
}
// Returns the calculated Volume (cc) of the 3D object represented in the binary STL. If $unit is 'cm' then returns volume in cubic cm, but If $unit is 'inch' then returns volume in cubic inches.
public function GetVolume ( $unit ) {
if ( !$this->flag ) {
$v = $this->CalculateVolume();
$this->volume = $v;
$this->flag = true;
}
$volume = 0;
if( $unit == 'cm' ) {
$volume = ( $this->volume / 1000 );
} else {
$volume = $this->Inch3 ( $this->volume / 1000 );
}
return $volume;
}
// Returns the calculated Weight (gm) of the 3D object represented in the binary STL.
public function getWeight() {
$volume = $this->GetVolume ( 'cm' );
$weight = $this->CalculateWeight ( $volume );
return $weight;
}
// Returns the set Density (gm/cc) of the material.
public function GetDensity() {
return $this->density;
}
// Sets the Density (gm/cc) of the material.
public function SetDensity ( $den ) {
$this->density = $den;
}
// Returns the number of trianges specified in the binary STL definition of the 3D object.
public function GetTrianglesCount() {
$tcount = $this->triangles_count;
return $tcount;
}
// Invokes the binary file reader to read the header, serially reads all the normal vector and triangular co-ordinates, calls the math function to calculate signed tetrahedral volumes for each trangle, sums up these volumes to give the final volume of the 3D object represented in the .stl binary file.
private function CalculateVolume(){
$totalVolume = 0;
if ( $this->b_binary ) {
$totbytes = filesize ( $this->fstl_path );
$totalVolume = 0;
try {
$this->ReadHeader();
$this->triangles_count = $this->ReadTrianglesCount();
$totalVolume = 0;
try {
while ( ftell ( $this->fstl_handle ) < $totbytes ){
$totalVolume += $this->ReadTriangle();
}
}
catch ( Exception $e ) {
return $e;
}
}
catch ( Exception $e ) {
return $e;
}
fclose ( $this->fstl_handle );
} else {
$k = 0;
while ( sizeof ( $this->triangles_data[4] ) > 0 ) {
$totalVolume += $this->ReadTriangleAscii();
$k += 1;
}
$this->triangles_count = $k;
}
return abs ( $totalVolume );
}
// Wrapper around PHP's unpack() function which decodes binary numerical data to float, int, etc types. $sig specifies the type of data (i.e. integer, float, etc), $l specifies number of bytes to read.
function PhUnpack ( $sig, $l ) {
$s = fread ( $this->fstl_handle, $l );
$stuff = unpack ( $sig, $s );
return $stuff;
}
// Appends to an array either a single var or the contents of another array.
function PhAppend ( $myarr, $mystuff ) {
if ( gettype ( $mystuff ) == 'array' ) {
$myarr = array_merge ( $myarr, $mystuff );
} else {
$ctr = sizeof ( $myarr );
$myarr[$ctr] = $mystuff;
}
return $myarr;
}
// Reads the binary header field in the STL file and offsets the file reader pointer to enable reading the triangle-normal data.
function ReadHeader() {
fseek ( $this->fstl_handle, ftell ( $this->fstl_handle ) + 80 );
}
// Reads the binary field in the STL file which specifies the total number of triangles and returns that integer.
function ReadTrianglesCount() {
$length = $this->PhUnpack ( 'I', 4 );
return $length[1];
}
// Reads a triangle data from the binary STL and returns its signed volume. A binary STL is a representation of a 3D object as a collection of triangles and their normal vectors. Its specifiction can be found here: http://en.wikipedia.org/wiki/STL_(file_format)%23Binary_STL This function reads the bytes of the binary STL file, decodes the data to give float XYZ co-ordinates of the trinaglular vertices and the normal vector for a triangle.
function ReadTriangle() {
$n = $this->PhUnpack ( 'f3', 12 );
$p1 = $this->PhUnpack ( 'f3', 12 );
$p2 = $this->PhUnpack ( 'f3', 12 );
$p3 = $this->PhUnpack ( 'f3', 12 );
$b = $this->PhUnpack ( 'v', 2 );
$l = sizeof ( $this->points );
$this->PhAppend ( $this->triangles, array ( $l, $l+1, $l+2 ) );
return $this->SignedVolumeOfTriangle ( $p1, $p2, $p3 );
}
// Reads a triangle data from the ascii STL and returns its signed volume.
function ReadTriangleAscii() {
$p1[1] = floatval ( array_pop ( $this->triangles_data[4] ) );
$p1[2] = floatval ( array_pop ( $this->triangles_data[5] ) );
$p1[3] = floatval ( array_pop ( $this->triangles_data[6] ) );
$p2[1] = floatval ( array_pop ( $this->triangles_data[7] ) );
$p2[2] = floatval ( array_pop ( $this->triangles_data[8] ) );
$p2[3] = floatval ( array_pop ( $this->triangles_data[9] ) );
$p3[1] = floatval ( array_pop ( $this->triangles_data[10] ) );
$p3[2] = floatval ( array_pop ( $this->triangles_data[11] ) );
$p3[3] = floatval ( array_pop ( $this->triangles_data[12] ) );
$l = sizeof ( $this->points );
$this->PhAppend ( $this->triangles, array ( $l, $l+1, $l+2 ) );
return $this->SignedVolumeOfTriangle ( $p1, $p2, $p3 );
}
// Checks if the given file is an ASCII file. Populates the triangles_data array if TRUE.
function IsAscii ( $filename ) {
$b = FALSE;
$namePattern = '/facet\\s+normal\\s+([-+]?\\b(?:[0-9]*\\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\\b)\\s+([-+]?\\b(?:[0-9]*\\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\\b)\\s+([-+]?\\b(?:[0-9]*\\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\\b)\\s+'
. 'outer\\s+loop\\s+'
. 'vertex\\s+([-+]?\\b(?:[0-9]*\\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\\b)\\s+([-+]?\\b(?:[0-9]*\\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\\b)\\s+([-+]?\\b(?:[0-9]*\\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\\b)\\s+'
. 'vertex\\s+([-+]?\\b(?:[0-9]*\\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\\b)\\s+([-+]?\\b(?:[0-9]*\\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\\b)\\s+([-+]?\\b(?:[0-9]*\\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\\b)\\s+'
. 'vertex\\s+([-+]?\\b(?:[0-9]*\\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\\b)\\s+([-+]?\\b(?:[0-9]*\\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\\b)\\s+([-+]?\\b(?:[0-9]*\\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\\b)\\s+'
. 'endloop\\s+' . 'endfacet/';
$fdata = file_get_contents ( $filename );
preg_match_all ( $namePattern, $fdata, $matches );
if ( sizeof ( $matches[0] ) > 0 ) {
$b = TRUE;
$this->triangles_data = $matches;
}
return $b;
}
// Returns the signed volume of a triangle as determined by its 3D, XYZ co-ordinates. The var $pn contains an array ( x, y, z).
function SignedVolumeOfTriangle ( $p1, $p2, $p3 ) {
$v321 = $p3[1] * $p2[2] * $p1[3];
$v231 = $p2[1] * $p3[2] * $p1[3];
$v312 = $p3[1] * $p1[2] * $p2[3];
$v132 = $p1[1] * $p3[2] * $p2[3];
$v213 = $p2[1] * $p1[2] * $p3[3];
$v123 = $p1[1] * $p2[2] * $p3[3];
return ( 1.0 / 6.0 ) * ( -$v321 + $v231 + $v312 - $v132 - $v213 + $v123 );
}
// Converts the volume specified in cubic cm to cubic inches.
function Inch3 ( $v ) {
return $v * 0.0610237441;
}
// Calculates the weight of the supplied volume specified in cubic cm and returns it in gms.
function CalculateWeight ( $volumeInCm ) {
return $volumeInCm * $this->density;
}
}
?>