Skip to content

Instantly share code, notes, and snippets.

@daeken
Created August 25, 2018 22:32
Show Gist options
  • Save daeken/8836c512fff20e2bb52cae6cf7ad7c3c to your computer and use it in GitHub Desktop.
Save daeken/8836c512fff20e2bb52cae6cf7ad7c3c to your computer and use it in GitHub Desktop.
def vmul(a, b):
return (a[0] * b, a[1] * b, a[2] * b)
def vmin(*x):
cur = x[0]
for v in x[1:]:
cur = min(v[0], cur[0]), min(v[1], cur[1]), min(v[2], cur[2])
return cur
def vmax(*x):
cur = x[0]
for v in x[1:]:
cur = max(v[0], cur[0]), max(v[1], cur[1]), max(v[2], cur[2])
return cur
def aabb(tmin, tmax):
box(
(tmin[0], tmin[1], tmin[2]),
(tmax[0], tmin[1], tmin[2]),
(tmax[0], tmax[1], tmin[2]),
(tmin[0], tmax[1], tmin[2]),
(tmin[0], tmin[1], tmax[2]),
(tmax[0], tmin[1], tmax[2]),
(tmax[0], tmax[1], tmax[2]),
(tmin[0], tmax[1], tmax[2])
)
# a b
# d c
# | |
# e f
# h g
def box(a, b, c, d, e, f, g, h):
quad(a, b, c, d) # Top
quad(a, d, h, e) # Left
quad(a, b, f, e) # Back
quad(b, c, g, f) # Right
quad(d, c, g, h) # Front
quad(e, f, g, h) # Bottom
def quad(a, b, c, d):
tri(a, b, c)
tri(c, d, a)
def tri(a, b, c):
print 'facet normal 0 0 0'
print 'outer loop'
point(a)
point(b)
point(c)
print 'endloop'
print 'endfacet'
def withbox(func):
def sub(*x):
aabb(vmin(*x), vmax(*x))
return func(*x)
return sub
def point(x):
print 'vertex %f %f %f' % x
def stl(func):
print 'solid obj'
func()
print 'endsolid obj'
@stl
def test():
withbox(tri)((-2501.938, -1.40625, 180.5312), (-2501.469, 65.25, 168.0625), (-2501.938, 65.25, 168.0312))
aabb((-2803.812, -2693.156, -16179.89), (-43.29688, 36.5625, 417.9531))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment