stl 0.1 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.markdown +6 -1
- data/lib/stl.rb +35 -0
- data/lib/stl/parser.rb +7 -5
- data/stl.gemspec +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9358e58b0e36e487bfb97b511e4fb5c545e98a01
|
4
|
+
data.tar.gz: 50c4aa3e0bf3fd5cff6b998c166be6b5720fbc74
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 672e5bd8ad471c1d5e17337c9e7e6fc5c9d135c8a982742e76b230d821df3d74fef7eab227682615aa133972ea5fd3fa4553d2810814e2ab93389764b296e4c8
|
7
|
+
data.tar.gz: bf46b1582ff58139bf094e292a8d5e7a42c49e4bb7ce87f72a0cb882f7e6286ffdb14003f4026ab7b9c1e28a134551aa340a9f968495fe31e26195d3393ad444
|
data/README.markdown
CHANGED
data/lib/stl.rb
CHANGED
@@ -7,4 +7,39 @@ module STL
|
|
7
7
|
def self.read(filename)
|
8
8
|
File.open(filename, 'r') {|f| STL::Parser.parse(f) }
|
9
9
|
end
|
10
|
+
|
11
|
+
# Write to an STL file
|
12
|
+
# @param filename [String] The path to write to
|
13
|
+
# @param faces [Array] An array of faces to write: [[Normal, Triangle], ...]
|
14
|
+
# @param format [Symbol] Pass :ascii to write an ASCII formatted file, and :binary to write a binary file
|
15
|
+
def self.write(filename, faces, format=:binary)
|
16
|
+
File.open(filename, 'w') do |file|
|
17
|
+
if format == :ascii
|
18
|
+
file.puts 'solid '
|
19
|
+
faces.each do |normal, triangle|
|
20
|
+
file.puts " facet normal %E %E %E" % [*normal]
|
21
|
+
file.puts "\touter loop"
|
22
|
+
triangle.points.each do |point|
|
23
|
+
file.puts "\t vertex %E %E %E" % [*point]
|
24
|
+
end
|
25
|
+
file.puts "\tendloop"
|
26
|
+
file.puts ' endfacet'
|
27
|
+
end
|
28
|
+
file.puts 'endsolid '
|
29
|
+
elsif format == :binary
|
30
|
+
file.write 'STL Ruby'.ljust(80, "\0") # A meager header
|
31
|
+
file.write [faces.length].pack('V') # The triangle count
|
32
|
+
|
33
|
+
faces.each do |normal, triangle|
|
34
|
+
file.write normal.to_a.pack("FFF")
|
35
|
+
|
36
|
+
triangle.points.each do |point|
|
37
|
+
file.write point.to_a.pack("FFF")
|
38
|
+
end
|
39
|
+
|
40
|
+
file.write "\0\0"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
10
45
|
end
|
data/lib/stl/parser.rb
CHANGED
@@ -10,7 +10,8 @@ module STL
|
|
10
10
|
# the word 'solid'. The first non-whitespace characters of an ASCII
|
11
11
|
# STL file should contain 'solid'.
|
12
12
|
if io.gets(80).include?('solid')
|
13
|
-
|
13
|
+
io.rewind
|
14
|
+
self.new.parse_ascii(io)
|
14
15
|
else
|
15
16
|
self.new.parse_binary(io)
|
16
17
|
end
|
@@ -26,12 +27,13 @@ module STL
|
|
26
27
|
case line
|
27
28
|
when /solid (.*)/
|
28
29
|
name = $1
|
29
|
-
when /facet normal
|
30
|
+
when /facet normal\s+(\S+)\s+(\S+)\s+(\S+)/
|
30
31
|
stack.push Vector[Float($1), Float($2), Float($3)]
|
31
|
-
when /vertex
|
32
|
+
when /vertex\s+(\S+)\s+(\S+)\s+(\S+)/
|
32
33
|
stack.push Vector[Float($1), Float($2), Float($3)]
|
33
34
|
when /endloop/
|
34
|
-
|
35
|
+
normal, *vertices = stack.pop(4)
|
36
|
+
triangles.push [normal, Geometry::Triangle.new(*vertices)]
|
35
37
|
end
|
36
38
|
end
|
37
39
|
triangles
|
@@ -46,7 +48,7 @@ module STL
|
|
46
48
|
faces = []
|
47
49
|
while not io.eof?
|
48
50
|
normal, *vertices = io.read(50).unpack('F3F3F3F3x2').each_slice(3).to_a
|
49
|
-
faces.push [normal, Geometry::Triangle.new(*vertices)]
|
51
|
+
faces.push [Vector[*normal], Geometry::Triangle.new(*vertices)]
|
50
52
|
end
|
51
53
|
raise StandardError, "Unexpected end of file after #{faces.length} triangles" if faces.length != count
|
52
54
|
|
data/stl.gemspec
CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = 'stl'
|
7
|
-
spec.version = '0.
|
7
|
+
spec.version = '0.2'
|
8
8
|
spec.authors = ["Brandon Fosdick"]
|
9
9
|
spec.email = ["bfoz@bfoz.net"]
|
10
10
|
spec.description = %q{Read and write STL files}
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.2'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brandon Fosdick
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-08-
|
11
|
+
date: 2014-08-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|