def setToArtOfIllusionDictionary(self): "Set the shape of this carvable object info." vertexElement = self.elementNode.getFirstChildByLocalName('vertex') vertexPointElements = vertexElement.getChildNodesByLocalName('bf:Elem') for vertexPointElement in vertexPointElements: coordinateElement = vertexPointElement.getFirstChildByLocalName( 'r') vertex = Vector3(float(coordinateElement.attributes['x']), float(coordinateElement.attributes['y']), float(coordinateElement.attributes['z'])) self.vertexes.append(vertex) edgeElement = self.elementNode.getFirstChildByLocalName('edge') edgeSubelements = edgeElement.getChildNodesByLocalName('bf:Elem') for edgeSubelementIndex in xrange(len(edgeSubelements)): edgeSubelement = edgeSubelements[edgeSubelementIndex] vertexIndexes = [ int(edgeSubelement.attributes['v1']), int(edgeSubelement.attributes['v2']) ] edge = face.Edge().getFromVertexIndexes(edgeSubelementIndex, vertexIndexes) self.edges.append(edge) faceElement = self.elementNode.getFirstChildByLocalName('face') faceSubelements = faceElement.getChildNodesByLocalName('bf:Elem') for faceSubelementIndex in xrange(len(faceSubelements)): faceSubelement = faceSubelements[faceSubelementIndex] edgeIndexes = [ int(faceSubelement.attributes['e1']), int(faceSubelement.attributes['e2']), int(faceSubelement.attributes['e3']) ] self.faces.append(face.Face().getFromEdgeIndexes( edgeIndexes, self.edges, faceSubelementIndex)) removeListArtOfIllusionFromDictionary(self.elementNode.attributes, ['closed', 'smoothingMethod'])
def getRemainingLoopAddFace(faces, remainingLoop): "Get the remaining loop and add face." for indexedVertexIndex, indexedVertex in enumerate(remainingLoop): nextIndex = (indexedVertexIndex + 1) % len(remainingLoop) previousIndex = (indexedVertexIndex + len(remainingLoop) - 1) % len(remainingLoop) nextVertex = remainingLoop[nextIndex] previousVertex = remainingLoop[previousIndex] remainingPath = euclidean.getAroundLoop( (indexedVertexIndex + 2) % len(remainingLoop), previousIndex, remainingLoop) if len(remainingLoop) < 4 or getIsPathEntirelyOutsideTriangle( previousVertex, indexedVertex, nextVertex, remainingPath): faceConvex = face.Face() faceConvex.index = len(faces) faceConvex.vertexIndexes.append(indexedVertex.index) faceConvex.vertexIndexes.append(nextVertex.index) faceConvex.vertexIndexes.append(previousVertex.index) faces.append(faceConvex) return euclidean.getAroundLoop(nextIndex, indexedVertexIndex, remainingLoop) print( 'Warning, could not decompose polygon in getRemainingLoopAddFace in trianglemesh for:' ) print(remainingLoop) return []
def addFacesGivenLine(faces, line): "Add face given line index and lines." parts = map(lambda p: p.split('/')[0], line.split()) for idx in xrange(1, len(parts) - 2): addface = face.Face() addface.index = len(faces) addface.vertexIndexes.append(int(parts[1]) - 1) addface.vertexIndexes.append(int(parts[idx + 1]) - 1) addface.vertexIndexes.append(int(parts[idx + 2]) - 1) faces.append(addface)
def getFaceGivenLine(line, triangleMesh): "Add face given line index and lines." faceGivenLine = face.Face() faceGivenLine.index = len(triangleMesh.faces) splitLine = line.split() for vertexStringIndex in xrange(1, 4): vertexString = splitLine[vertexStringIndex] vertexStringWithSpaces = vertexString.replace('/', ' ') vertexStringSplit = vertexStringWithSpaces.split() vertexIndex = int(vertexStringSplit[0]) - 1 faceGivenLine.vertexIndexes.append(vertexIndex) return faceGivenLine
def addFacesFromConvex(faces, indexedLoop): "Add faces from a convex polygon." if len(indexedLoop) < 1: return zeroIndex = indexedLoop[0].index for indexedPointIndex in xrange(1, len(indexedLoop) - 1): faceFromConvex = face.Face() faceFromConvex.index = len(faces) faceFromConvex.vertexIndexes.append(zeroIndex) faceFromConvex.vertexIndexes.append(indexedLoop[indexedPointIndex].index) faceFromConvex.vertexIndexes.append(indexedLoop[(indexedPointIndex + 1) % len(indexedLoop) ].index) faces.append(faceFromConvex)
def load(self, filename): try: zfile = zipfile.ZipFile(filename) xml = zfile.read(zfile.namelist()[0]) zfile.close() except zipfile.BadZipfile: f = open(filename, "r") xml = f.read() f.close() amf = ElementTree.fromstring(xml) if 'unit' in amf.attrib: unit = amf.attrib['unit'].lower() else: unit = 'millimeter' if unit == 'millimeter': scale = 1.0 elif unit == 'meter': scale = 1000.0 elif unit == 'inch': scale = 25.4 elif unit == 'feet': scale = 304.8 elif unit == 'micron': scale = 0.001 else: print "Unknown unit in amf: %s" % (unit) scale = 1.0 for obj in amf.iter('object'): for mesh in obj.iter('mesh'): startIndex = len(self.vertexes) for vertices in mesh.iter('vertices'): for vertex in vertices.iter('vertex'): for coordinates in vertex.iter('coordinates'): v = [0.0, 0.0, 0.0] for t in coordinates: if t.tag == 'x': v[0] = float(t.text) elif t.tag == 'y': v[1] = float(t.text) elif t.tag == 'z': v[2] = float(t.text) self.vertexes.append(Vector3(v[0], v[1], v[2])) for volume in mesh.iter('volume'): for triangle in volume.iter('triangle'): f = face.Face() f.index = len(self.faces) for t in triangle: if t.tag == 'v1' or t.tag == 'v2' or t.tag == 'v3': f.vertexIndexes.append(startIndex + int(t.text)) self.faces.append(f) return self
def getFaceGivenLines( triangleMesh, vertexStartIndex, vertexIndexTable, vertexes ): "Add face given line index and lines." faceGivenLines = face.Face() faceGivenLines.index = len( triangleMesh.faces ) for vertexIndex in xrange( vertexStartIndex, vertexStartIndex + 3 ): vertex = vertexes[vertexIndex] vertexUniqueIndex = len( vertexIndexTable ) if str(vertex) in vertexIndexTable: vertexUniqueIndex = vertexIndexTable[ str(vertex) ] else: vertexIndexTable[ str(vertex) ] = vertexUniqueIndex triangleMesh.vertexes.append(vertex) faceGivenLines.vertexIndexes.append( vertexUniqueIndex ) return faceGivenLines
def addFacesByConvex(faces, indexedLoop): 'Add faces from a convex polygon.' if len(indexedLoop) < 3: return indexBegin = indexedLoop[0].index for indexedPointIndex in xrange(1, len(indexedLoop) - 1): indexCenter = indexedLoop[indexedPointIndex].index indexEnd = indexedLoop[(indexedPointIndex + 1) % len(indexedLoop) ].index if indexBegin != indexCenter and indexCenter != indexEnd and indexEnd != indexBegin: faceFromConvex = face.Face() faceFromConvex.index = len(faces) faceFromConvex.vertexIndexes.append(indexBegin) faceFromConvex.vertexIndexes.append(indexCenter) faceFromConvex.vertexIndexes.append(indexEnd) faces.append(faceFromConvex)
def addFacesGivenVertexes( triangleMesh, vertexIndexTable, vertexes ): "Add faces given stl text." for vertexIndex in xrange( 0, len(vertexes), 3 ): faceGivenLines = face.Face() faceGivenLines.index = len( triangleMesh.faces ) for i in xrange( vertexIndex, vertexIndex + 3 ): vertex = vertexes[i] vertexUniqueIndex = len( vertexIndexTable ) if str(vertex) in vertexIndexTable: vertexUniqueIndex = vertexIndexTable[ str(vertex) ] else: vertexIndexTable[ str(vertex) ] = vertexUniqueIndex triangleMesh.vertexes.append(vertex) faceGivenLines.vertexIndexes.append( vertexUniqueIndex ) triangleMesh.faces.append( faceGivenLines )
def getFromGNUTriangulatedSurfaceText(gnuTriangulatedSurfaceText, triangleMesh): "Initialize from a GNU Triangulated Surface Text." if gnuTriangulatedSurfaceText == '': return None lines = archive.getTextLines(gnuTriangulatedSurfaceText) linesWithoutComments = [] for line in lines: if len(line) > 0: firstCharacter = line[0] if firstCharacter != '#' and firstCharacter != '!': linesWithoutComments.append(line) splitLine = linesWithoutComments[0].split() numberOfVertexes = int(splitLine[0]) numberOfEdges = int(splitLine[1]) numberOfFaces = int(splitLine[2]) faceTriples = [] for vertexIndex in xrange(numberOfVertexes): line = linesWithoutComments[vertexIndex + 1] splitLine = line.split() vertex = Vector3(float(splitLine[0]), float(splitLine[1]), float(splitLine[2])) triangleMesh.vertexes.append(vertex) edgeStart = numberOfVertexes + 1 for edgeIndex in xrange(numberOfEdges): line = linesWithoutComments[edgeIndex + edgeStart] splitLine = line.split() vertexIndexes = [] for word in splitLine[:2]: vertexIndexes.append(int(word) - 1) edge = face.Edge().getFromVertexIndexes(edgeIndex, vertexIndexes) triangleMesh.edges.append(edge) faceStart = edgeStart + numberOfEdges for faceIndex in xrange(numberOfFaces): line = linesWithoutComments[faceIndex + faceStart] splitLine = line.split() edgeIndexes = [] for word in splitLine[:3]: edgeIndexes.append(int(word) - 1) triangleMesh.faces.append(face.Face().getFromEdgeIndexes( edgeIndexes, triangleMesh.edges, faceIndex)) return triangleMesh
def _ProcessNode2(self, node, matrix=None): if 'matrix' in node: oldMatrix = matrix matrix = map(float, node['matrix'][0]['__data'].split()) if oldMatrix != None: newMatrix = [0] * 16 newMatrix[0] = oldMatrix[0] * matrix[0] + oldMatrix[1] * matrix[ 4] + oldMatrix[2] * matrix[8] + oldMatrix[3] * matrix[12] newMatrix[1] = oldMatrix[0] * matrix[1] + oldMatrix[1] * matrix[ 5] + oldMatrix[2] * matrix[9] + oldMatrix[3] * matrix[13] newMatrix[2] = oldMatrix[0] * matrix[2] + oldMatrix[1] * matrix[ 6] + oldMatrix[2] * matrix[10] + oldMatrix[3] * matrix[14] newMatrix[3] = oldMatrix[0] * matrix[3] + oldMatrix[1] * matrix[ 7] + oldMatrix[2] * matrix[11] + oldMatrix[3] * matrix[15] newMatrix[4] = oldMatrix[4] * matrix[0] + oldMatrix[5] * matrix[ 4] + oldMatrix[6] * matrix[8] + oldMatrix[7] * matrix[12] newMatrix[5] = oldMatrix[4] * matrix[1] + oldMatrix[5] * matrix[ 5] + oldMatrix[6] * matrix[9] + oldMatrix[7] * matrix[13] newMatrix[6] = oldMatrix[4] * matrix[2] + oldMatrix[5] * matrix[ 6] + oldMatrix[6] * matrix[10] + oldMatrix[7] * matrix[14] newMatrix[7] = oldMatrix[4] * matrix[3] + oldMatrix[5] * matrix[ 7] + oldMatrix[6] * matrix[11] + oldMatrix[7] * matrix[15] newMatrix[8] = oldMatrix[8] * matrix[0] + oldMatrix[9] * matrix[ 4] + oldMatrix[10] * matrix[8] + oldMatrix[11] * matrix[12] newMatrix[9] = oldMatrix[8] * matrix[1] + oldMatrix[9] * matrix[ 5] + oldMatrix[10] * matrix[9] + oldMatrix[11] * matrix[13] newMatrix[10] = oldMatrix[8] * matrix[2] + oldMatrix[ 9] * matrix[6] + oldMatrix[10] * matrix[10] + oldMatrix[ 11] * matrix[14] newMatrix[11] = oldMatrix[8] * matrix[3] + oldMatrix[ 9] * matrix[7] + oldMatrix[10] * matrix[11] + oldMatrix[ 11] * matrix[15] newMatrix[12] = oldMatrix[12] * matrix[0] + oldMatrix[ 13] * matrix[4] + oldMatrix[14] * matrix[8] + oldMatrix[ 15] * matrix[12] newMatrix[13] = oldMatrix[12] * matrix[1] + oldMatrix[ 13] * matrix[5] + oldMatrix[14] * matrix[9] + oldMatrix[ 15] * matrix[13] newMatrix[14] = oldMatrix[12] * matrix[2] + oldMatrix[ 13] * matrix[6] + oldMatrix[14] * matrix[10] + oldMatrix[ 15] * matrix[14] newMatrix[15] = oldMatrix[12] * matrix[3] + oldMatrix[ 13] * matrix[7] + oldMatrix[14] * matrix[11] + oldMatrix[ 15] * matrix[15] matrix = newMatrix if 'node' in node: for n in node['node']: self._ProcessNode2(n, matrix) if 'instance_geometry' in node: for instance_geometry in node['instance_geometry']: mesh = self._idMap[instance_geometry['_url']]['mesh'][0] if 'triangles' in mesh: for triangles in mesh['triangles']: for input in triangles['input']: if input['_semantic'] == 'VERTEX': vertices = self._idMap[input['_source']] for input in vertices['input']: if input['_semantic'] == 'POSITION': vertices = self._idMap[input['_source']] indexList = map(int, triangles['p'][0]['__data'].split()) positionList = map( float, vertices['float_array'][0]['__data'].split()) startIndex = len(self.vertexes) for idx in xrange(0, len(positionList) / 3): x = positionList[idx * 3] * self._scale y = positionList[idx * 3 + 1] * self._scale z = positionList[idx * 3 + 2] * self._scale if matrix != None: self.vertexes.append( Vector3( x * matrix[0] + y * matrix[1] + z * matrix[2] + matrix[3], x * matrix[4] + y * matrix[5] + z * matrix[6] + matrix[7], x * matrix[8] + y * matrix[9] + z * matrix[10] + matrix[11])) else: self.vertexes.append(Vector3(x, y, z)) stepSize = len(indexList) / (int(triangles['_count']) * 3) for i in xrange(0, int(triangles['_count'])): idx = i * stepSize * 3 f = face.Face() f.index = len(self.faces) f.vertexIndexes.append(startIndex + indexList[idx]) f.vertexIndexes.append(startIndex + indexList[idx + stepSize]) f.vertexIndexes.append(startIndex + indexList[idx + stepSize * 2]) self.faces.append(f) if 'instance_node' in node: for instance_node in node['instance_node']: self._ProcessNode2(self._idMap[instance_node['_url']], matrix)