Class: Collada::Parser::Geometry::Mesh::Polygons

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/collada/parser/scene.rb,
lib/collada/parser/geometry.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inputs, indices, size, elements) ⇒ Polygons

Returns a new instance of Polygons.



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/collada/parser/scene.rb', line 95

def initialize(inputs, indices, count, vertices)
	@inputs = inputs
	@indices = indices
	
	# The total number of polygons:
	@count = count
	
	# The number of vertices per polygon:
	@vertices = vertices
	
	# The number of indices per vertex:
	@stride = @inputs.sort_by(&:offset).last.offset + 1
end

Instance Attribute Details

#countObject (readonly)

Element count:



113
114
115
# File 'lib/collada/parser/scene.rb', line 113

def count
  @count
end

#elementsObject (readonly)

Per-element data:



111
112
113
# File 'lib/collada/parser/geometry.rb', line 111

def elements
  @elements
end

#indicesObject (readonly)

Returns the value of attribute indices.



110
111
112
# File 'lib/collada/parser/scene.rb', line 110

def indices
  @indices
end

#inputsObject (readonly)

Returns the value of attribute inputs.



109
110
111
# File 'lib/collada/parser/scene.rb', line 109

def inputs
  @inputs
end

#sizeObject (readonly)

Number of polygons



108
109
110
# File 'lib/collada/parser/geometry.rb', line 108

def size
	@count
end

#strideObject (readonly)

Number of indices consumed per vertex:



119
120
121
# File 'lib/collada/parser/scene.rb', line 119

def stride
  @stride
end

#verticesObject (readonly)

Number of vertices per element:



116
117
118
# File 'lib/collada/parser/scene.rb', line 116

def vertices
  @vertices
end

Class Method Details

.parse(doc, element, sources = {}) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/collada/parser/scene.rb', line 157

def self.parse(doc, element, sources = {})
	inputs = parse_inputs(doc, element, sources)
	indices = parse_indices(doc, element)
	count = element.attributes['count'].to_i
	
	if element.name == 'triangles'
		self.new(inputs, indices, count, TriangleVertices.new)
	elsif element.name == 'polylist'
		self.new(inputs, indices, count, PolygonVertices.parse(doc, element))
	else
		raise UnsupportedFeature.new(element)
	end
end

.parse_indices(doc, element) ⇒ Object



153
154
155
# File 'lib/collada/parser/scene.rb', line 153

def self.parse_indices(doc, element)
	element.elements['p'].text.strip.split(/\s+/).collect{|index| index.to_i - 1}
end

.parse_inputs(doc, element, sources = {}) ⇒ Object



147
148
149
150
151
# File 'lib/collada/parser/scene.rb', line 147

def self.parse_inputs(doc, element, sources = {})
	OrderedMap.parse(element, '//input') do |input_element|
		Input.parse(doc, input_element, sources)
	end
end

Instance Method Details

#[](index) ⇒ Object

Vertices by index:



126
127
128
129
130
131
132
# File 'lib/collada/parser/scene.rb', line 126

def [] index
	offset = @stride * index
	
	@inputs.collect do |input|
		input.source.accessor[@indices[offset + input.offset]]
	end
end

#eachObject



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/collada/parser/scene.rb', line 134

def each
	consumed = 0
	
	@count.times do |index|
		elements = @vertices.count(index)
		polygon = elements.times.collect{|edge| self[consumed + edge]}
		
		yield polygon
		
		consumed += elements
	end
end

#each_indicesObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/collada/parser/geometry.rb', line 127

def each_indices
	return to_enum(:each_indices) unless block_given?
	
	vertex_offset = 0
	
	@size.times do |index|
		# There are n vertices per face:
		vertex_count = @elements.vertex_count(index)
		
		# Grap all the vertices
		polygon_indices = vertex_count.times.collect do |vertex_index|
			vertex_offset + vertex_index
		end
		
		yield polygon_indices
		
		vertex_offset += vertex_count
	end
end

#vertex(index) ⇒ Object

Vertices by index:



117
118
119
120
121
122
123
124
125
# File 'lib/collada/parser/geometry.rb', line 117

def vertex(index)
	offset = @stride * index
	
	attributes = @inputs.collect do |input|
		input[@indices[offset + input.offset]]
	end
	
	return Attribute.flatten(attributes)
end