From 7192aabe55f35ccd9b13500beef308486bee15d1 Mon Sep 17 00:00:00 2001 From: daid303 Date: Fri, 12 Apr 2013 10:03:09 +0200 Subject: [PATCH] Add lay-flat code. --- Cura/util/mesh.py | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/Cura/util/mesh.py b/Cura/util/mesh.py index 4aacc30f..c9d2236e 100644 --- a/Cura/util/mesh.py +++ b/Cura/util/mesh.py @@ -137,7 +137,47 @@ class printableObject(object): self.applyMatrix(numpy.matrix([[x,0,0],[0,y,0],[0,0,z]], numpy.float64)) def layFlat(self): - pass + transformedVertexes = (numpy.matrix(self._meshList[0].vertexes, copy = False) * self._matrix).getA() + minZvertex = transformedVertexes[transformedVertexes.argmin(0)[2]] + dotMin = 1.0 + dotV = None + for v in transformedVertexes: + diff = v - minZvertex + len = math.sqrt(diff[0] * diff[0] + diff[1] * diff[1] + diff[2] * diff[2]) + if len < 5: + continue + dot = (diff[2] / len) + if dotMin > dot: + dotMin = dot + dotV = diff + if dotV is None: + return + rad = -math.atan2(dotV[1], dotV[0]) + self._matrix *= numpy.matrix([[math.cos(rad), math.sin(rad), 0], [-math.sin(rad), math.cos(rad), 0], [0,0,1]], numpy.float64) + rad = -math.asin(dotMin) + self._matrix *= numpy.matrix([[math.cos(rad), 0, math.sin(rad)], [0,1,0], [-math.sin(rad), 0, math.cos(rad)]], numpy.float64) + + + transformedVertexes = (numpy.matrix(self._meshList[0].vertexes, copy = False) * self._matrix).getA() + minZvertex = transformedVertexes[transformedVertexes.argmin(0)[2]] + dotMin = 1.0 + dotV = None + for v in transformedVertexes: + diff = v - minZvertex + len = math.sqrt(diff[1] * diff[1] + diff[2] * diff[2]) + if len < 5: + continue + dot = (diff[2] / len) + if dotMin > dot: + dotMin = dot + dotV = diff + if dotV is None: + return + if dotV[1] < 0: + rad = math.asin(dotMin) + else: + rad = -math.asin(dotMin) + self.applyMatrix(numpy.matrix([[1,0,0], [0, math.cos(rad), math.sin(rad)], [0, -math.sin(rad), math.cos(rad)]], numpy.float64)) def scaleUpTo(self, size): vMin = self._transformedMin -- 2.30.2