stl_parser 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/stl_parser.rb +93 -49
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1e0d1e6f44f5d886054ca5c2758440f645301d1b
|
4
|
+
data.tar.gz: a0903116fd552c3fff0eaf2927816164abc245d1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 644c3728386b5e18e15093ed8ea9d306fafdf025c16028fb2d584074a339e4e8181feb2e57af0f536dd69c22f5680fda420209b71fa3c9f69118700634f5d9b4
|
7
|
+
data.tar.gz: e07eabdeffcf2d0d12aa8375c268288a30cdf029298f0a1e231ca3d9c6676a3fa81497b4d47557332cb9afb08edffa47452b40680afe3fb650f556d71b17180c
|
data/lib/stl_parser.rb
CHANGED
@@ -4,7 +4,6 @@ class STLParser
|
|
4
4
|
@normals = []
|
5
5
|
@points = []
|
6
6
|
@triangles = []
|
7
|
-
@bytecount = []
|
8
7
|
@fb = [] # debug list
|
9
8
|
@volume = 0
|
10
9
|
@max_x = 0
|
@@ -14,6 +13,7 @@ class STLParser
|
|
14
13
|
@min_y = 0
|
15
14
|
@min_z = 0
|
16
15
|
@num_triangles = 0
|
16
|
+
@file_type = :binary
|
17
17
|
end
|
18
18
|
|
19
19
|
# Calculate volume fo the 3D mesh using Tetrahedron volume
|
@@ -34,44 +34,75 @@ class STLParser
|
|
34
34
|
end
|
35
35
|
|
36
36
|
def read_triangle()
|
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
|
-
|
37
|
+
if(@file_type === :binary)
|
38
|
+
n = custom_unpack("eee", 12)
|
39
|
+
p1 = custom_unpack("eee", 12)
|
40
|
+
p2 = custom_unpack("eee", 12)
|
41
|
+
p3 = custom_unpack("eee", 12)
|
42
|
+
|
43
|
+
# To get past the space filler
|
44
|
+
@f.read(2)
|
45
|
+
else
|
46
|
+
temp = @f.gets
|
47
|
+
unless temp.include? 'endsolid'
|
48
|
+
# Get the normal
|
49
|
+
temp.sub!(/facet normal/, '').strip!
|
50
|
+
n = temp.split(' ').map{ |num| num.to_f }
|
51
|
+
|
52
|
+
# get past 'outer loop'
|
53
|
+
@f.gets
|
54
|
+
|
55
|
+
# get vertex one
|
56
|
+
temp = @f.gets
|
57
|
+
p1 = temp.sub(/vertex/, '').split(' ').map{ |num| num.to_f }
|
58
|
+
|
59
|
+
# get vertex two
|
60
|
+
temp = @f.gets
|
61
|
+
p2 = temp.sub(/vertex/, '').split(' ').map{ |num| num.to_f }
|
62
|
+
|
63
|
+
# get vertex three
|
64
|
+
temp = @f.gets
|
65
|
+
p3 = temp.sub(/vertex/, '').split(' ').map{ |num| num.to_f }
|
66
|
+
|
67
|
+
# Get past endloop
|
68
|
+
@f.gets
|
69
|
+
# Get past endfacet
|
70
|
+
temp = @f.gets
|
71
|
+
end
|
72
|
+
end
|
73
|
+
unless @f.eof
|
74
|
+
@min_x = p1[0] if(@min_x > p1[0])
|
75
|
+
@min_x = p2[0] if(@min_x > p2[0])
|
76
|
+
@min_x = p3[0] if(@min_x > p3[0])
|
77
|
+
|
78
|
+
@min_y = p1[1] if(@min_y > p1[1])
|
79
|
+
@min_y = p2[1] if(@min_y > p2[1])
|
80
|
+
@min_y = p3[1] if(@min_y > p3[1])
|
81
|
+
|
82
|
+
@min_z = p2[2] if(@min_z > p2[2])
|
83
|
+
@min_z = p2[2] if(@min_z > p2[2])
|
84
|
+
@min_z = p3[2] if(@min_z > p3[2])
|
85
|
+
|
86
|
+
@max_x = p1[0] if(@max_x < p1[0])
|
87
|
+
@max_x = p2[0] if(@max_x < p2[0])
|
88
|
+
@max_x = p3[0] if(@max_x < p3[0])
|
89
|
+
|
90
|
+
@max_y = p1[1] if(@max_y < p1[1])
|
91
|
+
@max_y = p2[1] if(@max_y < p2[1])
|
92
|
+
@max_y = p3[1] if(@max_y < p3[1])
|
93
|
+
|
94
|
+
@max_z = p2[2] if(@max_z < p2[2])
|
95
|
+
@max_z = p2[2] if(@max_z < p2[2])
|
96
|
+
@max_z = p3[2] if(@max_z < p3[2])
|
97
|
+
|
98
|
+
@normals.push(n)
|
99
|
+
l = @points.length
|
100
|
+
@points.push(p1)
|
101
|
+
@points.push(p2)
|
102
|
+
@points.push(p3)
|
103
|
+
@triangles.push(l)
|
104
|
+
return signedVolumeOfTriangle(p1,p2,p3)
|
105
|
+
end
|
75
106
|
end
|
76
107
|
|
77
108
|
def read_length()
|
@@ -79,7 +110,7 @@ class STLParser
|
|
79
110
|
return length[0]
|
80
111
|
end
|
81
112
|
|
82
|
-
def
|
113
|
+
def read_binary_header()
|
83
114
|
@f.seek(@f.tell()+80)
|
84
115
|
end
|
85
116
|
|
@@ -108,17 +139,30 @@ class STLParser
|
|
108
139
|
totalVolume = 0
|
109
140
|
|
110
141
|
@f = open(infilename, "rb")
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
142
|
+
|
143
|
+
# Set the file type to ascii if needed
|
144
|
+
@file_type = :ascii if(@f.gets.include? "solid")
|
145
|
+
|
146
|
+
# Go back to beginning of the file
|
147
|
+
@f.seek(0)
|
148
|
+
|
149
|
+
# Get past the header info that we don't care about
|
150
|
+
if(@file_type === :binary)
|
151
|
+
read_binary_header()
|
152
|
+
@num_triangles = read_length()
|
153
|
+
else
|
154
|
+
@f.gets
|
155
|
+
end
|
156
|
+
|
157
|
+
# Keep repeating until the end of the file is reached
|
158
|
+
while @f.eof === false do
|
159
|
+
added_volume = read_triangle()
|
160
|
+
totalVolume += added_volume if added_volume
|
120
161
|
end
|
121
162
|
|
122
163
|
@volume = totalVolume
|
164
|
+
|
165
|
+
# Close the file
|
166
|
+
@f.close
|
123
167
|
end
|
124
168
|
end
|