/
polylist.py
319 lines (264 loc) · 13.9 KB
/
polylist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
####################################################################
# #
# THIS FILE IS PART OF THE pycollada LIBRARY SOURCE CODE. #
# USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS #
# GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE #
# IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. #
# #
# THE pycollada SOURCE CODE IS (C) COPYRIGHT 2011 #
# by Jeff Terrace and contributors #
# #
####################################################################
"""Module containing classes and functions for the <polylist> primitive."""
import numpy
from lxml import etree as ElementTree
import primitive
import types
import triangleset
from util import toUnitVec, checkSource
from collada import DaeIncompleteError, DaeBrokenRefError, DaeMalformedError, \
DaeUnsupportedError, tag, E
class Polygon(object):
"""Single polygon representation. Represents a polygon of N points."""
def __init__(self, indices, vertices, normals, texcoords, material):
"""A Polygon should not be created manually."""
self.vertices = vertices
"""A (N, 3) float array containing the points in the polygon."""
self.normals = normals
"""A (N, 3) float array with the normals for points in the polygon. Can be None."""
self.texcoords = texcoords
"""A tuple where entries are numpy float arrays of size (N, 2) containing
the texture coordinates for the points in the polygon for each texture
coordinate set. Can be length 0 if there are no texture coordinates."""
self.material = material
"""If coming from an unbound :class:`collada.polylist.Polylist`, contains a
string with the material symbol. If coming from a bound
:class:`collada.polylist.BoundPolylist`, contains the actual
:class:`collada.material.Effect` the line is bound to."""
self.indices = indices
def triangles(self):
"""This triangulates the polygon using a simple fanning method.
:rtype: generator of :class:`collada.polylist.Polygon`
"""
npts = len(self.vertices)
for i in range(npts-2):
tri_indices = numpy.array([
self.indices[0], self.indices[i+1], self.indices[i+2]
])
tri_vertices = numpy.array([
self.vertices[0], self.vertices[i+1], self.vertices[i+2]
])
if self.normals is None:
tri_normals = None
else:
tri_normals = numpy.array([
self.normals[0], self.normals[i+1], self.normals[i+2]
])
tri_texcoords = []
for texcoord in self.texcoords:
tri_texcoords.append([texcoord[0], texcoord[i+1], texcoord[i+2]])
tri_texcoords = numpy.array(tri_texcoords)
tri = triangleset.Triangle(tri_indices, tri_vertices, tri_normals, tri_texcoords, self.material)
yield tri
def __repr__(self):
return '<Polygon vertices=%d>' % len(self.vertices)
def __str__(self): return repr(self)
class Polylist(primitive.Primitive):
"""Class containing the data COLLADA puts in a <polylist> tag, a collection of
polygons. The Polylist object is read-only. To modify a Polylist, create a new
instance using :meth:`collada.geometry.Geometry.createPolylist`.
* If ``P`` is an instance of :class:`collada.polylist.Polylist`, then ``len(P)``
returns the number of polygons in the set. ``P[i]`` returns the i\ :sup:`th`
polygon in the set.
"""
def __init__(self, sources, material, index, vcounts, xmlnode=None):
"""A Polylist should not be created manually. Instead, call the
:meth:`collada.geometry.Geometry.createPolylist` method after
creating a geometry instance.
"""
if len(sources) == 0: raise DaeIncompleteError('A polylist set needs at least one input for vertex positions')
if not 'VERTEX' in sources: raise DaeIncompleteError('Polylist requires vertex input')
#find max offset
max_offset = max([ max([input[0] for input in input_type_array])
for input_type_array in sources.itervalues() if len(input_type_array) > 0])
self.material = material
self.index = index
self.indices = self.index
self.nindices = max_offset + 1
self.vcounts = vcounts
self.sources = sources
self.index.shape = (-1, self.nindices)
self.npolygons = len(self.vcounts)
self.nvertices = numpy.sum(self.vcounts) if len(self.index) > 0 else 0
self.polyends = numpy.cumsum(self.vcounts)
self.polystarts = self.polyends - self.vcounts
self.polyindex = numpy.dstack((self.polystarts, self.polyends))[0]
if len(self.index) > 0:
self._vertex = sources['VERTEX'][0][4].data
self._vertex_index = self.index[:,sources['VERTEX'][0][0]]
self.maxvertexindex = numpy.max( self._vertex_index )
checkSource(sources['VERTEX'][0][4], ('X', 'Y', 'Z'), self.maxvertexindex)
else:
self._vertex = None
self._vertex_index = None
self.maxvertexindex = -1
if 'NORMAL' in sources and len(sources['NORMAL']) > 0 and len(self.index) > 0:
self._normal = sources['NORMAL'][0][4].data
self._normal_index = self.index[:,sources['NORMAL'][0][0]]
self.maxnormalindex = numpy.max( self._normal_index )
checkSource(sources['NORMAL'][0][4], ('X', 'Y', 'Z'), self.maxnormalindex)
else:
self._normal = None
self._normal_index = None
self.maxnormalindex = -1
if 'TEXCOORD' in sources and len(sources['TEXCOORD']) > 0 and len(self.index) > 0:
self._texcoordset = tuple([texinput[4].data for texinput in sources['TEXCOORD']])
self._texcoord_indexset = tuple([ self.index[:,sources['TEXCOORD'][i][0]]
for i in xrange(len(sources['TEXCOORD'])) ])
self.maxtexcoordsetindex = [ numpy.max(each) for each in self._texcoord_indexset ]
for i, texinput in enumerate(sources['TEXCOORD']):
checkSource(texinput[4], ('S', 'T'), self.maxtexcoordsetindex[i])
else:
self._texcoordset = tuple()
self._texcoord_indexset = tuple()
self.maxtexcoordsetindex = -1
if xmlnode is not None:
self.xmlnode = xmlnode
"""ElementTree representation of the line set."""
else:
txtindices = ' '.join(map(str, self.indices.flatten().tolist()))
acclen = len(self.indices)
self.xmlnode = E.polylist(count=str(self.npolygons), material=self.material)
all_inputs = []
for semantic_list in self.sources.itervalues():
all_inputs.extend(semantic_list)
for offset, semantic, sourceid, set, src in all_inputs:
inpnode = E.input(offset=str(offset), semantic=semantic, source=sourceid)
if set is not None:
inpnode.set('set', str(set))
self.xmlnode.append(inpnode)
vcountnode = E.vcount(' '.join(map(str, self.vcounts)))
self.xmlnode.append(vcountnode)
self.xmlnode.append(E.p(txtindices))
def __len__(self): return self.npolygons
def __getitem__(self, i):
polyrange = self.polyindex[i]
vertindex = self._vertex_index[polyrange[0]:polyrange[1]]
v = self._vertex[vertindex]
if self.normal is None:
n = None
else:
n = self._normal[ self._normal_index[polyrange[0]:polyrange[1]] ]
uv = []
for j, uvindex in enumerate(self._texcoord_indexset):
uv.append( self._texcoordset[j][ uvindex[polyrange[0]:polyrange[1]] ] )
return Polygon(vertindex, v, n, uv, self.material)
_triangleset = None
def triangleset(self):
"""This performs a simple triangulation of the polylist using the fanning method.
:rtype: :class:`collada.triangleset.TriangleSet`
"""
if self._triangleset is None:
indexselector = numpy.zeros(self.nvertices) == 0
indexselector[self.polyindex[:,1]-1] = False
indexselector[self.polyindex[:,1]-2] = False
indexselector = numpy.arange(self.nvertices)[indexselector]
firstpolyindex = numpy.arange(self.nvertices)
firstpolyindex = firstpolyindex - numpy.repeat(self.polyends - self.vcounts, self.vcounts)
firstpolyindex = firstpolyindex[indexselector]
triindex = numpy.dstack( (self.index[indexselector-firstpolyindex],
self.index[indexselector+1],
self.index[indexselector+2]) )
triindex = numpy.swapaxes(triindex, 1,2).flatten()
triset = triangleset.TriangleSet(self.sources, self.material, triindex, self.xmlnode)
self._triangleset = triset
return self._triangleset
@staticmethod
def load( collada, localscope, node ):
indexnode = node.find(tag('p'))
if indexnode is None: raise DaeIncompleteError('Missing index in polylist')
vcountnode = node.find(tag('vcount'))
if vcountnode is None: raise DaeIncompleteError('Missing vcount in polylist')
try:
if vcountnode.text is None:
vcounts = numpy.array([], dtype=numpy.int32)
else:
vcounts = numpy.fromstring(vcountnode.text, dtype=numpy.int32, sep=' ')
except ValueError, ex: raise DaeMalformedError('Corrupted vcounts in polylist')
all_inputs = primitive.Primitive._getInputs(localscope, node.findall(tag('input')))
try:
if indexnode.text is None:
index = numpy.array([], dtype=numpy.int32)
else:
index = numpy.fromstring(indexnode.text, dtype=numpy.int32, sep=' ')
except: raise DaeMalformedError('Corrupted index in polylist')
polylist = Polylist(all_inputs, node.get('material'), index, vcounts, node)
return polylist
def bind(self, matrix, materialnodebysymbol):
"""Create a bound polylist from this polylist, transform and material mapping"""
return BoundPolylist( self, matrix, materialnodebysymbol)
def __str__(self): return '<Polylist length=%d>' % len(self)
def __repr__(self): return str(self)
class BoundPolylist(primitive.BoundPrimitive):
"""A polylist bound to a transform matrix and materials mapping.
* If ``P`` is an instance of :class:`collada.polylist.BoundPolylist`, then ``len(P)``
returns the number of polygons in the set. ``P[i]`` returns the i\ :sup:`th`
polygon in the set.
"""
def __init__(self, pl, matrix, materialnodebysymbol):
"""Create a bound polylist from a polylist, transform and material mapping.
This gets created when a polylist is instantiated in a scene. Do not create this manually."""
M = numpy.asmatrix(matrix).transpose()
self._vertex = None if pl._vertex is None else numpy.asarray(pl._vertex * M[:3,:3]) + matrix[:3,3]
self._normal = None if pl._normal is None else numpy.asarray(pl._normal * M[:3,:3])
self._texcoordset = pl._texcoordset
matnode = materialnodebysymbol.get( pl.material )
if matnode:
self.material = matnode.target
self.inputmap = dict([ (sem, (input_sem, set)) for sem, input_sem, set in matnode.inputs ])
else: self.inputmap = self.material = None
self.index = pl.index
self.nvertices = pl.nvertices
self._vertex_index = pl._vertex_index
self._normal_index = pl._normal_index
self._texcoord_indexset = pl._texcoord_indexset
self.polyindex = pl.polyindex
self.npolygons = pl.npolygons
self.matrix = matrix
self.materialnodebysymbol = materialnodebysymbol
self.original = pl
def __len__(self): return len(self.index)
def __getitem__(self, i):
polyrange = self.polyindex[i]
vertindex = self._vertex_index[polyrange[0]:polyrange[1]]
v = self._vertex[vertindex]
if self.normal is None:
n = None
else:
n = self._normal[ self._normal_index[polyrange[0]:polyrange[1]] ]
uv = []
for j, uvindex in enumerate(self._texcoord_indexset):
uv.append( self._texcoordset[j][ uvindex[polyrange[0]:polyrange[1]] ] )
return Polygon(vertindex, v, n, uv, self.material)
_triangleset = None
def triangleset(self):
"""This performs a simple triangulation of the polylist using the fanning method.
:rtype: :class:`collada.triangleset.BoundTriangleSet`
"""
if self._triangleset is None:
triset = self.original.triangleset()
boundtriset = triset.bind(self.matrix, self.materialnodebysymbol)
self._triangleset = boundtriset
return self._triangleset
def polygons(self):
"""Iterate through all the polygons contained in the set.
:rtype: generator of :class:`collada.polylist.Polygon`
"""
for i in xrange(self.npolygons): yield self[i]
def shapes(self):
"""Iterate through all the polygons contained in the set.
:rtype: generator of :class:`collada.polylist.Polygon`
"""
return self.polygons()
def __str__(self): return '<BoundPolylist length=%d>' % len(self)
def __repr__(self): return str(self)