stl 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3e442a1d1d6c73f5568291d19e4a9c76ca9f7960
4
- data.tar.gz: b13b828b174516b81743aafec4f9830b5d39cdfd
3
+ metadata.gz: 9358e58b0e36e487bfb97b511e4fb5c545e98a01
4
+ data.tar.gz: 50c4aa3e0bf3fd5cff6b998c166be6b5720fbc74
5
5
  SHA512:
6
- metadata.gz: 7a0e02cf15977359cf82cbcfe31e6376ad4e6d2d059ffe940bb030b529e804c3f183d96e903d1556b081bc5da803b1a6346ca298b510967ffda944fdc8570bf8
7
- data.tar.gz: d31042303cf87bf9f1b533e090a3837c120585ca134a175d8ffee15cfc292cd174c2e8883b946972a2b51354ce56c2a00dfac716b1d1d48d29b383ea503ba566
6
+ metadata.gz: 672e5bd8ad471c1d5e17337c9e7e6fc5c9d135c8a982742e76b230d821df3d74fef7eab227682615aa133972ea5fd3fa4553d2810814e2ab93389764b296e4c8
7
+ data.tar.gz: bf46b1582ff58139bf094e292a8d5e7a42c49e4bb7ce87f72a0cb882f7e6286ffdb14003f4026ab7b9c1e28a134551aa340a9f968495fe31e26195d3393ad444
@@ -22,4 +22,9 @@ Or install it yourself as:
22
22
  require 'stl'
23
23
 
24
24
  faces = STL.read('my_awesome.stl') # => [[normal, Triangle], ...]
25
- ```
25
+ ```
26
+
27
+ License
28
+ -------
29
+
30
+ Copyright 2014 Brandon Fosdick <bfoz@bfoz.net> and released under the BSD license.
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
@@ -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
- self.new.parse_ascii(io.rewind)
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
- triangles.push [stack.pop, Geometry::Triangle.new(*stack.pop(3))]
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
 
@@ -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.1'
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.1'
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-24 00:00:00.000000000 Z
11
+ date: 2014-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler