wavefront-obj 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.md +77 -0
  2. data/lib/wavefront_obj.rb +60 -22
  3. metadata +5 -2
@@ -0,0 +1,77 @@
1
+ wavefront-obj
2
+ =============
3
+
4
+ #### Description
5
+
6
+ Ruby library to create wavefront .obj files.
7
+
8
+ This library provides a handy interface to create [wavefront .obj files](http://en.wikipedia.org/wiki/Wavefront_.obj_file). You can add vertex and faces to define a 3d object. It handles the syntax of the .obj file format and takes care of vertex definition (no vertex is defined twice which reduces filesize). You can access the result in raw data or write it into a file.
9
+
10
+ Obj-Files can be used to feed a 3d printer.
11
+
12
+ #### Usage
13
+
14
+ install
15
+
16
+ gem install wavefront-obj
17
+
18
+ require library, create an object and give it a name
19
+
20
+ require 'wavefront_obj'
21
+ cube = WavefrontObj.new
22
+ cube.name = "my awesome cube"
23
+
24
+ add faces
25
+
26
+ cube.add_face [[1, -1, -1],[1, -1, 1],[-1, -1, 1],[-1, -1, -1]]
27
+ cube.add_face [[1, 1, -1],[-1, 1, -1],[-1, 1, 1],[1, 1, 1]]
28
+ cube.add_face [[1, -1, -1],[1, 1, -1],[1, 1, 1],[1, -1, 1]]
29
+ cube.add_face [[1, -1, 1],[1, 1, 1],[-1, 1, 1],[-1, -1, 1]]
30
+ cube.add_face [[-1, -1, 1],[-1, 1, 1],[-1, 1, -1],[-1, -1, -1]]
31
+ cube.add_face [[1, 1, -1],[1, -1, -1],[-1, -1, -1],[-1, 1, -1]]
32
+
33
+ access the raw data
34
+
35
+ puts cube.get_raw_data
36
+
37
+ which will look like this
38
+
39
+ o my awesome cube
40
+ v 1 -1 -1
41
+ v 1 -1 1
42
+ v -1 -1 1
43
+ v -1 -1 -1
44
+ v 1 1 -1
45
+ v -1 1 -1
46
+ v -1 1 1
47
+ v 1 1 1
48
+ f 1 2 3 4
49
+ f 5 6 7 8
50
+ f 1 5 8 2
51
+ f 2 8 7 3
52
+ f 3 7 6 4
53
+ f 5 1 4 6
54
+
55
+ or save it as a file
56
+
57
+ cube.save "my_awesome_cube.obj"
58
+
59
+ You can open .obj files with most 3d programs like blender or some newer Photoshop versions as well.
60
+
61
+
62
+ ## Version History
63
+ **v1.0.0** 02/25/2013
64
+
65
+ - initial release (yanked)
66
+
67
+ **v1.0.1** 02/25/2013
68
+
69
+ - renamed _export\_obj\_path_ method to _save_
70
+
71
+ **v1.0.2** 03/02/2013
72
+
73
+ - make _add\_face_ method return _true_ instead of _void_
74
+ - added dependency injection to mock ruby file class
75
+ - added test for save method
76
+ - added yard documentation
77
+ - updated _README.md_ - Fixed some typos, adjust structure, added version history
@@ -1,36 +1,45 @@
1
1
  require 'rubygems'
2
2
  require 'digest/md5'
3
3
 
4
+ # @author Stefan Kracht
4
5
  class WavefrontObj
5
6
 
6
7
  attr_reader :point_index
7
8
  attr_accessor :name, :points, :faces
8
9
 
10
+ # dependency injectors
11
+ attr_writer :file
12
+
9
13
  DEFAULT_NAME = "Wavefront Obj"
10
14
 
15
+ # inits attributes
11
16
  def initialize
12
17
  @name = DEFAULT_NAME
13
18
  @points = Hash.new
14
19
  @point_index = 0
15
20
  @faces = Hash.new
16
21
  end
17
-
22
+
23
+ # adds a point to the 3d object
24
+ # @param pnt [Array] array of 3 coordinates
25
+ # @return [String, false] the point index
18
26
  def add_point(pnt)
19
- if pnt.class == Array && pnt.length == 3
20
- indentifier = create_point_identifier(pnt)
21
- if @points[indentifier].nil?
22
- @points[indentifier] = Hash.new
23
- @point_index = @point_index+1
24
- @points[indentifier]["index"] = @point_index
25
- end
26
- @points[indentifier]["point"] = pnt
27
- return @points[indentifier]["index"]
28
- else
29
- false
27
+ # TODO add error handling
28
+ indentifier = create_point_identifier(pnt)
29
+ if @points[indentifier].nil?
30
+ @points[indentifier] = Hash.new
31
+ @point_index = @point_index+1
32
+ @points[indentifier]["index"] = @point_index
30
33
  end
34
+ @points[indentifier]["point"] = pnt
35
+ return @points[indentifier]["index"]
31
36
  end
32
37
 
38
+ # adds a face to the 3d object
39
+ # @param pnt_arr [Array] array of multiple point arrays
40
+ # @return [true]
33
41
  def add_face(pnt_arr)
42
+ # TODO add error handling
34
43
  face_points = []
35
44
  pnt_arr.each do |pnt|
36
45
  face_points.push add_point(pnt)
@@ -40,15 +49,11 @@ class WavefrontObj
40
49
  @faces[indentifier] = Hash.new
41
50
  @faces[indentifier]["face"] = face_points
42
51
  end
52
+ return true
43
53
  end
44
-
45
- def save(path)
46
- raw_data = get_raw_data
47
- path = "#{path}.obj" unless path.split(".").last == "obj"
48
- File.open(path, 'w') {|f| f.write(get_raw_data) }
49
- path
50
- end
51
-
54
+
55
+ # returns the raw data of the obj file
56
+ # @return [String] raw data
52
57
  def get_raw_data
53
58
  buffer = []
54
59
  buffer.push(get_name_syntax(@name))
@@ -63,26 +68,59 @@ class WavefrontObj
63
68
 
64
69
  return buffer.join("\n")
65
70
  end
71
+
72
+ # stores the obj data as a file
73
+ # @param path [String] path to store the file, ".obj" will be added if missing
74
+ # @return [String] the path of the created file
75
+ def save(path)
76
+ # TODO add error handling
77
+ raw_data = get_raw_data
78
+ path = "#{path}.obj" unless path.split(".").last == "obj"
79
+ file.open(path, 'w') {|f| f.write(get_raw_data) }
80
+ path
81
+ end
66
82
 
67
83
  private
68
84
 
85
+ # injectable dependencies for file
86
+ # @return [void]
87
+ def file
88
+ @file || File
89
+ end
90
+
91
+ # returns the syntax of object definition
92
+ # @param name [String] object name
93
+ # @return [String] object name in .obj format
69
94
  def get_name_syntax(name)
70
95
  "o #{name}"
71
96
  end
72
97
 
98
+ # returns the syntax of a point definition
99
+ # @param point [String] object name
100
+ # @return [String, false] point in .obj format
73
101
  def get_point_syntax(point)
102
+ # TODO convert to array
74
103
  "v #{point.join(" ")}"
75
104
  end
76
105
 
106
+ # returns the syntax of a face definition
107
+ # @return [String, false] face in .obj format
77
108
  def get_face_syntax(face)
109
+ # TODO convert to array
78
110
  "f #{face.join(" ")}"
79
111
  end
80
-
112
+
113
+ # generates the unique id of a point
114
+ # @return [String] point indetifier
81
115
  def create_point_identifier(pnt)
116
+ # TODO convert to array
82
117
  Digest::MD5.hexdigest(pnt.join("|"))
83
118
  end
84
-
119
+
120
+ # generates the unique id of a face
121
+ # @return [String] face indetifier
85
122
  def create_face_identifier(pnts)
123
+ # TODO convert to array
86
124
  Digest::MD5.hexdigest(pnts.join("|"))
87
125
  end
88
126
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wavefront-obj
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -19,9 +19,11 @@ description: This library provides a handy interface to create wavefront .obj fi
19
19
  email: stefankracht@gmail.com
20
20
  executables: []
21
21
  extensions: []
22
- extra_rdoc_files: []
22
+ extra_rdoc_files:
23
+ - README.md
23
24
  files:
24
25
  - lib/wavefront_obj.rb
26
+ - README.md
25
27
  homepage: https://github.com/krachtstefan/wavefront-obj
26
28
  licenses: []
27
29
  post_install_message:
@@ -47,3 +49,4 @@ signing_key:
47
49
  specification_version: 3
48
50
  summary: Ruby library to create wavefront obj files
49
51
  test_files: []
52
+ has_rdoc: