chiark / gitweb /
Change how AMF is saved. Make better use of the index vertexes.
authordaid <daid303@gmail.com>
Thu, 27 Jun 2013 08:03:23 +0000 (10:03 +0200)
committerdaid <daid303@gmail.com>
Thu, 27 Jun 2013 08:03:23 +0000 (10:03 +0200)
Cura/util/mesh.py
Cura/util/meshLoaders/amf.py

index 5d58f280b7d0296958b17d14ee76fd9c1de74f33..f1415fa6d65d951dd29f6f4a401264d5fdab49fa 100644 (file)
@@ -200,12 +200,39 @@ class printableObject(object):
                if scale > 0:
                        self.applyMatrix(numpy.matrix([[scale,0,0],[0,scale,0],[0,0,scale]], numpy.float64))
 
+       #Split splits an object with multiple meshes into different objects, where each object is a part of the original mesh that has
+       # connected faces. This is useful to split up plate STL files.
        def split(self, callback):
                ret = []
                for oriMesh in self._meshList:
                        ret += oriMesh.split(callback)
                return ret
 
+       #getVertexIndexList returns an array of vertexes, and an integer array for each mesh in this object.
+       # the integer arrays are indexes into the vertex array for each triangle in the model.
+       def getVertexIndexList(self):
+               vertexMap = {}
+               vertexList = []
+               meshList = []
+               for m in self._meshList:
+                       verts = m.getTransformedVertexes(True)
+                       meshIdxList = []
+                       for idx in xrange(0, len(verts)):
+                               v = verts[idx]
+                               hashNr = int(v[0] * 100) | int(v[1] * 100) << 10 | int(v[2] * 100) << 20
+                               vIdx = None
+                               if hashNr in vertexMap:
+                                       for idx2 in vertexMap[hashNr]:
+                                               if numpy.linalg.norm(v - vertexList[idx2]) < 0.001:
+                                                       vIdx = idx2
+                               if vIdx is None:
+                                       vIdx = len(vertexList)
+                                       vertexMap[hashNr] = [vIdx]
+                                       vertexList.append(v)
+                               meshIdxList.append(vIdx)
+                       meshList.append(numpy.array(meshIdxList, numpy.int32))
+               return numpy.array(vertexList, numpy.float32), meshList
+
 class mesh(object):
        def __init__(self, obj):
                self.vertexes = None
index f2b407b429aa6a5cc606186dff331228ff193edc..377e1023db1a1f72d1acf5367c92d82c54e5ba53 100644 (file)
@@ -88,28 +88,26 @@ def saveScene(filename, objects):
                xml.write('  <object id="%d">\n' % (n))
                xml.write('    <mesh>\n')
                xml.write('      <vertices>\n')
-               for m in obj._meshList:
-                       for v in m.getTransformedVertexes(True):
-                               xml.write('        <vertex>\n')
-                               xml.write('          <coordinates>\n')
-                               xml.write('            <x>%f</x>\n' % (v[0]))
-                               xml.write('            <y>%f</y>\n' % (v[1]))
-                               xml.write('            <z>%f</z>\n' % (v[2]))
-                               xml.write('          </coordinates>\n')
-                               xml.write('        </vertex>\n')
+               vertexList, meshList = obj.getVertexIndexList()
+               for v in vertexList:
+                       xml.write('        <vertex>\n')
+                       xml.write('          <coordinates>\n')
+                       xml.write('            <x>%f</x>\n' % (v[0]))
+                       xml.write('            <y>%f</y>\n' % (v[1]))
+                       xml.write('            <z>%f</z>\n' % (v[2]))
+                       xml.write('          </coordinates>\n')
+                       xml.write('        </vertex>\n')
                xml.write('      </vertices>\n')
 
-               idxOffset = 0
-               for m in obj._meshList:
+               for m in meshList:
                        xml.write('      <volume>\n')
-                       for idx in xrange(0, len(m.vertexes), 3):
+                       for idx in xrange(0, len(m), 3):
                                xml.write('        <triangle>\n')
-                               xml.write('          <v1>%i</v1>\n' % (idx + idxOffset))
-                               xml.write('          <v2>%i</v2>\n' % (idx + idxOffset + 1))
-                               xml.write('          <v3>%i</v3>\n' % (idx + idxOffset + 2))
+                               xml.write('          <v1>%i</v1>\n' % (m[idx]))
+                               xml.write('          <v2>%i</v2>\n' % (m[idx+1]))
+                               xml.write('          <v3>%i</v3>\n' % (m[idx+2]))
                                xml.write('        </triangle>\n')
                        xml.write('      </volume>\n')
-                       idxOffset += len(m.vertexes)
                xml.write('    </mesh>\n')
                xml.write('  </object>\n')