yade-users team mailing list archive
-
yade-users team
-
Mailing list archive
-
Message #22149
Re: [Question #688822]: Exporting a packing geometry made in yade in a stl format
Question #688822 on Yade changed:
https://answers.launchpad.net/yade/+question/688822
Status: Open => Answered
Jan Stránský proposed the following answer:
well, it is not tough (MWE below, probably could be simplified by a meshing/triangulation library)
I just found it unnecessary without enough information
cheers
Jan
###
import stl # pip install stl
class Triangle:
def __init__(self,p1,p2,p3):
self.points = [Vector3(p) for p in (p1,p2,p3)]
def scale(self,factor):
self.points = [p*factor for p in self.points]
return self
def shift(self,v):
self.points = [p+v for p in self.points]
return self
def refine(self): # split 1 triangle into 4
p1,p2,p3 = self.points
p4 = (.5*(p1+p2)).normalized()
p5 = (.5*(p2+p3)).normalized()
p6 = (.5*(p3+p1)).normalized()
return [
Triangle(p1,p4,p6),
Triangle(p2,p5,p4),
Triangle(p3,p6,p5),
Triangle(p4,p5,p6),
]
def toStlFacet(self):
minieigen2stl = lambda v: stl.Vector3d(*tuple(v))
p1,p2,p3 = self.points
normal = (p2-p1).cross(p3-p1).normalized()
normal = minieigen2stl(normal)
vertices = [minieigen2stl(p) for p in self.points]
return stl.Facet(normal,vertices)
def triangulateUnitSphere(): # octahedron, you can use whatever instead, e.g. icosahedron
xm,xp = [(v,0,0) for v in (-1,+1)]
ym,yp = [(0,v,0) for v in (-1,+1)]
zm,zp = [(0,0,v) for v in (-1,+1)]
return [
Triangle(xp,yp,zp),
Triangle(xm,yp,zp),
Triangle(xp,ym,zp),
Triangle(xm,ym,zp),
Triangle(xp,yp,zm),
Triangle(xm,yp,zm),
Triangle(xp,ym,zm),
Triangle(xm,ym,zm),
]
def triangulateOneSphere(sph,smooth=1):
ret = triangulateUnitSphere()
#
for ismooth in range(smooth):
ret2 = []
for t in ret:
ret2.extend(t.refine())
ret = ret2
#
c = sph.state.pos
r = sph.shape.radius
for t in ret:
t.scale(r).shift(c)
#
return ret
def triangulateSpheres(smooth=1):
ret = []
for b in O.bodies:
if not isinstance(b.shape,Sphere): continue
ret.extend(triangulateOneSphere(b,smooth))
return ret
def spheres2stl(fName,smooth=1):
triangles = triangulateSpheres(smooth)
triangles = [t.toStlFacet() for t in triangles]
solid = stl.Solid()
solid.facets = triangles
with open(fName,"w") as f:
solid.write_ascii(f)
O.bodies.append((
sphere((0,0,0),1),
sphere((5,4,3),2),
sphere((0,1,4),3),
))
spheres2stl("sphs.stl")
spheres2stl("sphs-smooth.stl",smooth=3)
###
--
You received this question notification because your team yade-users is
an answer contact for Yade.