wavefront-obj 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/wavefront_obj.rb +89 -0
  2. metadata +49 -0
@@ -0,0 +1,89 @@
1
+ require 'rubygems'
2
+ require 'digest/md5'
3
+
4
+ class WavefrontObj
5
+
6
+ attr_reader :point_index
7
+ attr_accessor :name, :points, :faces
8
+
9
+ DEFAULT_NAME = "Wavefront Obj"
10
+
11
+ def initialize
12
+ @name = DEFAULT_NAME
13
+ @points = Hash.new
14
+ @point_index = 0
15
+ @faces = Hash.new
16
+ end
17
+
18
+ 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
30
+ end
31
+ end
32
+
33
+ def add_face(pnt_arr)
34
+ face_points = []
35
+ pnt_arr.each do |pnt|
36
+ face_points.push add_point(pnt)
37
+ end
38
+ indentifier = create_face_identifier(face_points)
39
+ if @faces[indentifier].nil?
40
+ @faces[indentifier] = Hash.new
41
+ @faces[indentifier]["face"] = face_points
42
+ end
43
+ 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
+
52
+ def get_raw_data
53
+ buffer = []
54
+ buffer.push(get_name_syntax(@name))
55
+
56
+ @points.each do |key, pnt|
57
+ buffer.push(get_point_syntax(pnt["point"]))
58
+ end
59
+
60
+ @faces.each do |key, face|
61
+ buffer.push(get_face_syntax(face["face"]))
62
+ end
63
+
64
+ return buffer.join("\n")
65
+ end
66
+
67
+ private
68
+
69
+ def get_name_syntax(name)
70
+ "o #{name}"
71
+ end
72
+
73
+ def get_point_syntax(point)
74
+ "v #{point.join(" ")}"
75
+ end
76
+
77
+ def get_face_syntax(face)
78
+ "f #{face.join(" ")}"
79
+ end
80
+
81
+ def create_point_identifier(pnt)
82
+ Digest::MD5.hexdigest(pnt.join("|"))
83
+ end
84
+
85
+ def create_face_identifier(pnts)
86
+ Digest::MD5.hexdigest(pnts.join("|"))
87
+ end
88
+
89
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wavefront-obj
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Stefan Kracht
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-25 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: This library provides a handy interface to create wavefront .obj files.
15
+ You can add vertex and faces to define a 3d object. It handles the syntax of the
16
+ .obj file format and takes care of vertex definition (no vertex is defined twice
17
+ which reduces filesize). You can access the result in raw data or write it into
18
+ a file.
19
+ email: stefankracht@gmail.com
20
+ executables: []
21
+ extensions: []
22
+ extra_rdoc_files: []
23
+ files:
24
+ - lib/wavefront_obj.rb
25
+ homepage: https://github.com/krachtstefan/wavefront-obj
26
+ licenses: []
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ none: false
33
+ requirements:
34
+ - - ! '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 1.8.24
46
+ signing_key:
47
+ specification_version: 3
48
+ summary: Ruby library to create wavefront obj files
49
+ test_files: []