Commit 094c6a1a authored by Kliment Yanev's avatar Kliment Yanev

Move normal calculation into stltool

Fix inverted Y coordinates in normals
parent 16fd9fd0
import sys, struct import sys, struct, math
def cross(v1,v2):
return [v1[1]*v2[2]-v1[2]*v2[1],v1[2]*v2[0]-v1[0]*v2[2],v1[0]*v2[1]-v1[1]*v2[0]]
def genfacet(v):
veca=[v[1][0]-v[0][0],v[1][1]-v[0][1],v[1][2]-v[0][2]]
vecb=[v[2][0]-v[1][0],v[2][1]-v[1][1],v[2][2]-v[1][2]]
vecx=cross(veca,vecb)
vlen=math.sqrt(sum(map(lambda x:x*x,vecx)))
normal=map(lambda x:x/vlen, vecx)
return [normal,v]
I=[ I=[
[1,0,0,0], [1,0,0,0],
...@@ -8,13 +19,16 @@ I=[ ...@@ -8,13 +19,16 @@ I=[
] ]
def transpose(matrix): def transpose(matrix):
return [[v[i] for v in matrix] for i in xrange(len(matrix[0]))] return zip(*matrix)
#return [[v[i] for v in matrix] for i in xrange(len(matrix[0]))]
def multmatrix(vector,matrix): def multmatrix(vector,matrix):
return map(sum, transpose(map(lambda x:[x[0]*p for p in x[1]], zip(vector, transpose(matrix))))) return map(sum, transpose(map(lambda x:[x[0]*p for p in x[1]], zip(vector, transpose(matrix)))))
def applymatrix(facet,matrix=I): def applymatrix(facet,matrix=I):
return [multmatrix(facet[0]+[1],matrix)[:3],map(lambda x:multmatrix(x+[1],matrix)[:3],facet[1])] #return facet
#return [map(lambda x:-1.0*x,multmatrix(facet[0]+[1],matrix)[:3]),map(lambda x:multmatrix(x+[1],matrix)[:3],facet[1])]
return genfacet(map(lambda x:multmatrix(x+[1],matrix)[:3],facet[1]))
f=[[0,0,0],[[-3.022642, 0.642482, -9.510565],[-3.022642, 0.642482, -9.510565],[-3.022642, 0.642482, -9.510565]]] f=[[0,0,0],[[-3.022642, 0.642482, -9.510565],[-3.022642, 0.642482, -9.510565],[-3.022642, 0.642482, -9.510565]]]
m=[ m=[
...@@ -182,9 +196,9 @@ class stl: ...@@ -182,9 +196,9 @@ class stl:
self.facet[1][self.facetloc]=map(float,l.split()[1:]) self.facet[1][self.facetloc]=map(float,l.split()[1:])
self.facetloc+=1 self.facetloc+=1
return 1 return 1
if __name__=="__main__" and 0: if __name__=="__main__":
s=stl("sphere.stl") s=stl("../../Downloads/frame-vertex-neo-foot-x4.stl")
for i in xrange(-10,11): for i in xrange(11,11):
working=s.facets[:] working=s.facets[:]
for j in reversed(sorted(s.facetsminz)): for j in reversed(sorted(s.facetsminz)):
if(j[0]>i): if(j[0]>i):
...@@ -198,5 +212,5 @@ if __name__=="__main__" and 0: ...@@ -198,5 +212,5 @@ if __name__=="__main__" and 0:
break break
print i,len(working) print i,len(working)
emitstl("sphereout.stl",s.facets,"emitted_object") emitstl("../../Downloads/frame-vertex-neo-foot-x4-a.stl",s.facets,"emitted_object")
#stl("../prusamendel/stl/mendelplate.stl") #stl("../prusamendel/stl/mendelplate.stl")
import stltool,wx,math import wx,math
from stltool import *
a=wx.App() a=wx.App()
def cross(v1,v2):
return [v1[1]*v2[2]-v1[2]*v2[1],v1[0]*v2[2]-v1[2]*v2[0],v1[0]*v2[1]-v1[1]*v2[0]]
def genfacet(v):
veca=[v[1][0]-v[0][0],v[1][1]-v[0][1],v[1][2]-v[0][2]]
vecb=[v[2][0]-v[0][0],v[2][1]-v[0][1],v[2][2]-v[0][2]]
vecx=cross(veca,vecb)
vlen=math.sqrt(sum(map(lambda x:x*x,vecx)))
normal=map(lambda x:x/vlen, vecx)
return [normal,v]
def genscape(data=[[0,1,0,0],[1,0,2,0],[1,0,0,0],[0,1,0,1]],pscale=1.0,bheight=1.0,zscale=1.0): def genscape(data=[[0,1,0,0],[1,0,2,0],[1,0,0,0],[0,1,0,1]],pscale=1.0,bheight=1.0,zscale=1.0):
o=stltool.stl(None) o=stl(None)
datal=len(data) datal=len(data)
datah=len(data[0]) datah=len(data[0])
#create bottom: #create bottom:
...@@ -55,8 +45,8 @@ def zimage(name,out): ...@@ -55,8 +45,8 @@ def zimage(name,out):
data=[] data=[]
for i in xrange(s[0]): for i in xrange(s[0]):
data+=[b[i*s[1]:(i+1)*s[1]]] data+=[b[i*s[1]:(i+1)*s[1]]]
data=[i[::5] for i in data[::5]] #data=[i[::5] for i in data[::5]]
stltool.emitstl(out,genscape(data,zscale=0.1).facets,name) emitstl(out,genscape(data,zscale=0.1).facets,name)
""" """
class scapewin(wx.Frame): class scapewin(wx.Frame):
...@@ -69,12 +59,12 @@ class scapewin(wx.Frame): ...@@ -69,12 +59,12 @@ class scapewin(wx.Frame):
""" """
if __name__ == '__main__': if __name__ == '__main__':
""" """
app = wx.App(False) app = wx.App(False)
main = scapewin() main = scapewin()
main.Show() main.Show()
app.MainLoop() app.MainLoop()
""" """
zimage("testimg.png","testobj.stl") zimage("catposthtmap2.jpg","testobj.stl")
del a del a
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment