Class: WavefrontObj

Inherits:
Object
  • Object
show all
Defined in:
lib/wavefront_obj.rb

Constant Summary collapse

DEFAULT_NAME =
"Wavefront Obj"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWavefrontObj

Returns a new instance of WavefrontObj.



11
12
13
14
15
16
# File 'lib/wavefront_obj.rb', line 11

def initialize
	@name = DEFAULT_NAME
	@points = Hash.new
	@point_index = 0
	@faces = Hash.new
end

Instance Attribute Details

#facesObject

Returns the value of attribute faces.



7
8
9
# File 'lib/wavefront_obj.rb', line 7

def faces
  @faces
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/wavefront_obj.rb', line 7

def name
  @name
end

#point_indexObject (readonly)

Returns the value of attribute point_index.



6
7
8
# File 'lib/wavefront_obj.rb', line 6

def point_index
  @point_index
end

#pointsObject

Returns the value of attribute points.



7
8
9
# File 'lib/wavefront_obj.rb', line 7

def points
  @points
end

Instance Method Details

#add_face(pnt_arr) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/wavefront_obj.rb', line 33

def add_face(pnt_arr)
	face_points = []
	pnt_arr.each do |pnt|
		face_points.push add_point(pnt)
	end
	indentifier = create_face_identifier(face_points)
	if @faces[indentifier].nil?
		@faces[indentifier] = Hash.new
		@faces[indentifier]["face"] = face_points
	end
end

#add_point(pnt) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/wavefront_obj.rb', line 18

def add_point(pnt)
	if pnt.class == Array && pnt.length == 3
		indentifier = create_point_identifier(pnt)
		if @points[indentifier].nil?
			@points[indentifier] = Hash.new 
			@point_index = @point_index+1
			@points[indentifier]["index"] = @point_index
		end
		@points[indentifier]["point"] = pnt
		return @points[indentifier]["index"]
   else
     false
	end
end

#get_raw_dataObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/wavefront_obj.rb', line 52

def get_raw_data
buffer = []
buffer.push(get_name_syntax(@name))

@points.each do |key, pnt|
	buffer.push(get_point_syntax(pnt["point"]))
end

@faces.each do |key, face|
	buffer.push(get_face_syntax(face["face"]))
end

return buffer.join("\n")
end

#save(path) ⇒ Object



45
46
47
48
49
50
# File 'lib/wavefront_obj.rb', line 45

def save(path)
raw_data = get_raw_data
path = "#{path}.obj" unless path.split(".").last == "obj"
File.open(path, 'w') {|f| f.write(get_raw_data) }
path
end