Class: Collada::Parser::Geometry::Mesh::Polygons
- Inherits:
-
Object
- Object
- Collada::Parser::Geometry::Mesh::Polygons
- Includes:
- Enumerable
- Defined in:
- lib/collada/parser/scene.rb,
lib/collada/parser/geometry.rb
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Element count:.
-
#elements ⇒ Object
readonly
Per-element data:.
-
#indices ⇒ Object
readonly
Returns the value of attribute indices.
-
#inputs ⇒ Object
readonly
Returns the value of attribute inputs.
-
#size ⇒ Object
readonly
Number of polygons.
-
#stride ⇒ Object
readonly
Number of indices consumed per vertex:.
-
#vertices ⇒ Object
readonly
Number of vertices per element:.
Class Method Summary collapse
- .parse(doc, element, sources = {}) ⇒ Object
- .parse_indices(doc, element) ⇒ Object
- .parse_inputs(doc, element, sources = {}) ⇒ Object
Instance Method Summary collapse
-
#[](index) ⇒ Object
Vertices by index:.
- #each ⇒ Object
- #each_indices ⇒ Object
-
#initialize(inputs, indices, size, elements) ⇒ Polygons
constructor
A new instance of Polygons.
-
#vertex(index) ⇒ Object
Vertices by index:.
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
#count ⇒ Object (readonly)
Element count:
113 114 115 |
# File 'lib/collada/parser/scene.rb', line 113 def count @count end |
#elements ⇒ Object (readonly)
Per-element data:
111 112 113 |
# File 'lib/collada/parser/geometry.rb', line 111 def elements @elements end |
#indices ⇒ Object (readonly)
Returns the value of attribute indices.
110 111 112 |
# File 'lib/collada/parser/scene.rb', line 110 def indices @indices end |
#inputs ⇒ Object (readonly)
Returns the value of attribute inputs.
109 110 111 |
# File 'lib/collada/parser/scene.rb', line 109 def inputs @inputs end |
#size ⇒ Object (readonly)
Number of polygons
108 109 110 |
# File 'lib/collada/parser/geometry.rb', line 108 def size @count end |
#stride ⇒ Object (readonly)
Number of indices consumed per vertex:
119 120 121 |
# File 'lib/collada/parser/scene.rb', line 119 def stride @stride end |
#vertices ⇒ Object (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 |
#each ⇒ Object
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_indices ⇒ Object
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 |