Class: Collada::Parser::Accessor

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(array, parameters, count, options = {}) ⇒ Accessor

Returns a new instance of Accessor.



221
222
223
224
225
226
227
228
229
230
231
# File 'lib/collada/parser/support.rb', line 221

def initialize(array, parameters, count, options = {})
	@array = array
	@parameters = parameters
	@count = count
	
	# Chunk size and stride are usually the same in many cases.
	@chunk_size = @parameters.inject(0) {|sum, parameter| sum + parameter.size}
	
	@offset = (options[:offset] || 0).to_i
	@stride = (options[:stride] || @chunk_size).to_i
end

Instance Attribute Details

#arrayObject (readonly)

Returns the value of attribute array.



233
234
235
# File 'lib/collada/parser/support.rb', line 233

def array
  @array
end

#countObject (readonly)

Returns the value of attribute count.



237
238
239
# File 'lib/collada/parser/support.rb', line 237

def count
  @count
end

#namesObject (readonly)

Returns the value of attribute names.



235
236
237
# File 'lib/collada/parser/support.rb', line 235

def names
  @names
end

#offsetObject (readonly)

Returns the value of attribute offset.



238
239
240
# File 'lib/collada/parser/support.rb', line 238

def offset
  @offset
end

#parametersObject (readonly)

Returns the value of attribute parameters.



234
235
236
# File 'lib/collada/parser/support.rb', line 234

def parameters
  @parameters
end

#strideObject (readonly)

Returns the value of attribute stride.



239
240
241
# File 'lib/collada/parser/support.rb', line 239

def stride
  @stride
end

Class Method Details

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

Raises:



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/collada/parser/support.rb', line 268

def self.parse(doc, element, arrays = {})
	if (array_id = element.attributes['source'])
		array_id.sub!(/^#/, '')
		
		array = arrays[array_id]
	else
		array = Mesh.parse_arrays(doc, element).first
	end
	
	raise UnsupportedFeature.new("Source array binding must be valid (id=#{array_id})") unless array
	
	parameters = parse_parameters(doc, element)
	count = element.attributes['count'].to_i
	
	options = {
		:offset => element.attributes['offset'],
		:stride => element.attributes['stride'],
	}
	
	self.new(array, parameters, count, options)
end

.parse_parameters(doc, element) ⇒ Object



262
263
264
265
266
# File 'lib/collada/parser/support.rb', line 262

def self.parse_parameters(doc, element)
	OrderedMap.parse(element, 'param', 'name') do |param_element|
		Parameter.parse(doc, param_element)
	end
end

Instance Method Details

#[](index) ⇒ Object



248
249
250
# File 'lib/collada/parser/support.rb', line 248

def [] index
	read(index).delete_if{|(name, value)| name == nil}
end

#eachObject



256
257
258
259
260
# File 'lib/collada/parser/support.rb', line 256

def each
	size.times.each do |i|
		yield self[i]
	end
end

#read(index) ⇒ Object



241
242
243
244
245
246
# File 'lib/collada/parser/support.rb', line 241

def read index
	base = @offset + (index * @stride)
	values = @array[base, @chunk_size]
	
	@parameters.collect{|parameter| [parameter.name, parameter.read(values.shift(parameter.size))]}
end

#sizeObject



252
253
254
# File 'lib/collada/parser/support.rb', line 252

def size
	(@array.size - @offset) / @stride
end