stl 0 → 0.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0ecf41f3cb85f2b3f74b5ac05560c972fd4d8e82
4
- data.tar.gz: dd2f9b13e1d6c802649f30d951a521e5df20852a
3
+ metadata.gz: 3e442a1d1d6c73f5568291d19e4a9c76ca9f7960
4
+ data.tar.gz: b13b828b174516b81743aafec4f9830b5d39cdfd
5
5
  SHA512:
6
- metadata.gz: 128823cbeabf56dcbb1ed77c10d732cbd6d28528b5b306f914470a4b8e73b2629684ce39d9dcf33d07d72e01f6d5ced0d10b11ae4b07c209f8c1ce56e100aa86
7
- data.tar.gz: 6714a65fabb1da01e56992ec77f95f45a9ec1335740eb5793cb2a3d5e33e61ffc2a7b272d609beb159940eccab402aff1bb709ada11eb304a483cd54dd4330f5
6
+ metadata.gz: 7a0e02cf15977359cf82cbcfe31e6376ad4e6d2d059ffe940bb030b529e804c3f183d96e903d1556b081bc5da803b1a6346ca298b510967ffda944fdc8570bf8
7
+ data.tar.gz: d31042303cf87bf9f1b533e090a3837c120585ca134a175d8ffee15cfc292cd174c2e8883b946972a2b51354ce56c2a00dfac716b1d1d48d29b383ea503ba566
@@ -1,6 +1,6 @@
1
1
  # STL
2
2
 
3
- TODO: Write a gem description
3
+ Read, write and manipulate both ASCII and binary [STL files](http://en.wikipedia.org/wiki/STL_(file_format))
4
4
 
5
5
  ## Installation
6
6
 
@@ -18,12 +18,8 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ ```ruby
22
+ require 'stl'
22
23
 
23
- ## Contributing
24
-
25
- 1. Fork it
26
- 2. Create your feature branch (`git checkout -b my-new-feature`)
27
- 3. Commit your changes (`git commit -am 'Add some feature'`)
28
- 4. Push to the branch (`git push origin my-new-feature`)
29
- 5. Create new Pull Request
24
+ faces = STL.read('my_awesome.stl') # => [[normal, Triangle], ...]
25
+ ```
data/lib/stl.rb CHANGED
@@ -1,3 +1,10 @@
1
+ require_relative 'stl/parser'
2
+
1
3
  module STL
2
- # Your code goes here...
4
+ # Read an STL file
5
+ # @param filename [String] The path to the file to read
6
+ # @return [STL] the resulting {STL} object
7
+ def self.read(filename)
8
+ File.open(filename, 'r') {|f| STL::Parser.parse(f) }
9
+ end
3
10
  end
@@ -0,0 +1,56 @@
1
+ require 'geometry'
2
+
3
+ module STL
4
+ # http://en.wikipedia.org/wiki/STL_(file_format)
5
+ class Parser
6
+ # @param io [IO] the stream to parse
7
+ # @return [Array] An array of [Normal, Triangle] pairs
8
+ def self.parse(io)
9
+ # A binary STL file has an 80 byte header that should never contain
10
+ # the word 'solid'. The first non-whitespace characters of an ASCII
11
+ # STL file should contain 'solid'.
12
+ if io.gets(80).include?('solid')
13
+ self.new.parse_ascii(io.rewind)
14
+ else
15
+ self.new.parse_binary(io)
16
+ end
17
+ end
18
+
19
+ # Parse an ASCII STL file
20
+ # @param io [IO] the stream to parse
21
+ # @return [Array] An array of [Normal, Triangle] pairs
22
+ def parse_ascii(io)
23
+ stack = []
24
+ triangles = []
25
+ io.each do |line|
26
+ case line
27
+ when /solid (.*)/
28
+ name = $1
29
+ when /facet normal (.+) (.+) (.+)/
30
+ stack.push Vector[Float($1), Float($2), Float($3)]
31
+ when /vertex (.+) (.+) (.+)/
32
+ stack.push Vector[Float($1), Float($2), Float($3)]
33
+ when /endloop/
34
+ triangles.push [stack.pop, Geometry::Triangle.new(*stack.pop(3))]
35
+ end
36
+ end
37
+ triangles
38
+ end
39
+
40
+ # Parse a binary STL file, assuming that the header has already been read
41
+ # @param io [IO] the stream to parse
42
+ # @return [Array] An array of [Normal, Triangle] pairs
43
+ def parse_binary(io)
44
+ count = io.read(4).unpack('V').first
45
+
46
+ faces = []
47
+ while not io.eof?
48
+ normal, *vertices = io.read(50).unpack('F3F3F3F3x2').each_slice(3).to_a
49
+ faces.push [normal, Geometry::Triangle.new(*vertices)]
50
+ end
51
+ raise StandardError, "Unexpected end of file after #{faces.length} triangles" if faces.length != count
52
+
53
+ faces
54
+ end
55
+ end
56
+ end
@@ -3,8 +3,8 @@ lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
- spec.name = "stl"
7
- spec.version = '0'
6
+ spec.name = 'stl'
7
+ spec.version = '0.1'
8
8
  spec.authors = ["Brandon Fosdick"]
9
9
  spec.email = ["bfoz@bfoz.net"]
10
10
  spec.description = %q{Read and write STL files}
@@ -19,4 +19,6 @@ Gem::Specification.new do |spec|
19
19
 
20
20
  spec.add_development_dependency "bundler", "~> 1.5"
21
21
  spec.add_development_dependency "rake"
22
+
23
+ spec.add_dependency 'geometry'
22
24
  end
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.1'
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-01-25 00:00:00.000000000 Z
11
+ date: 2014-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: geometry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  description: Read and write STL files
42
56
  email:
43
57
  - bfoz@bfoz.net
@@ -50,6 +64,7 @@ files:
50
64
  - README.markdown
51
65
  - Rakefile
52
66
  - lib/stl.rb
67
+ - lib/stl/parser.rb
53
68
  - stl.gemspec
54
69
  homepage: https://github.com/bfoz/stl-ruby
55
70
  licenses:
@@ -71,7 +86,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
71
86
  version: '0'
72
87
  requirements: []
73
88
  rubyforge_project:
74
- rubygems_version: 2.1.11
89
+ rubygems_version: 2.2.2
75
90
  signing_key:
76
91
  specification_version: 4
77
92
  summary: Read, write and manipulate stereolithography files