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

inits attributes


16
17
18
19
20
21
# File 'lib/wavefront_obj.rb', line 16

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


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

def faces
  @faces
end

#file=(value) ⇒ Object

dependency injectors


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

def file=(value)
  @file = value
end

#nameObject

Returns the value of attribute name


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

def name
  @name
end

#point_indexObject (readonly)

Returns the value of attribute point_index


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

def point_index
  @point_index
end

#pointsObject

Returns the value of attribute points


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

def points
  @points
end

Instance Method Details

#add_face(pnt_arr) ⇒ true

adds a face to the 3d object


41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/wavefront_obj.rb', line 41

def add_face(pnt_arr)
   # TODO add error handling
  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
   return true
end

#add_point(pnt) ⇒ String, false

adds a point to the 3d object


26
27
28
29
30
31
32
33
34
35
36
# File 'lib/wavefront_obj.rb', line 26

def add_point(pnt)
   # TODO add error handling
  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"]
end

#get_raw_dataString

returns the raw data of the obj file


57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/wavefront_obj.rb', line 57

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) ⇒ String

stores the obj data as a file


75
76
77
78
79
80
81
# File 'lib/wavefront_obj.rb', line 75

def save(path)
  # TODO add error handling
  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