Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
octet truss cubes with two levels of hierarchical organization to improve the speed of the union operation
import scipy as sp
import itertools as it
class rcc_shape():
def __init__(self, name, origin, height, radius):
self.name = name
self.origin = origin
self.height = height
self.radius = radius
def mged_string(self):
return("in %s rcc %g %g %g %g %g %g %g\n" % (self.name, self.origin[0], self.origin[1], self.origin[2], self.height[0], self.height[1], self.height[2], self.radius))
class octet_unitcell():
def __init__(self, name, origin, length, radius):
self.name = name
self.origin = origin
self.length = length # length of members
self.radius = radius # radius of members
self.unit = sp.sqrt(length**2 + length**2) # unit cube side length
self.x1 = sp.array([0.0, 0.0, 0.0])
self.x2 = sp.array([self.length, 0.0, 0.0])
self.x3 = sp.array([0.0, self.length, 0.0])
self.x4 = sp.array([self.length, self.length, 0.0])
self.x5 = sp.array([self.length/2.0, self.length/2.0, self.length*sp.sin(sp.pi/4.0)])
self.x6 = sp.array([self.length/2.0, self.length/2.0, -self.length*sp.sin(sp.pi/4.0)])
def shapes(self):
"""return list of primitives"""
shape_list = []
shape_list.append( rcc_shape("%s1.s"%self.name, x1+self.origin, x2, radius) )
shape_list.append( rcc_shape("%s2.s"%self.name, x1+self.origin, x3, radius) )
shape_list.append( rcc_shape("%s3.s"%self.name, x2+self.origin, x3, radius) )
shape_list.append( rcc_shape("%s4.s"%self.name, x3+self.origin, x2, radius) )
shape_list.append( rcc_shape("%s5.s"%self.name, x1+self.origin, x5, radius) )
shape_list.append( rcc_shape("%s6.s"%self.name, x2+self.origin, x5-x2, radius) )
shape_list.append( rcc_shape("%s7.s"%self.name, x3+self.origin, x5-x3, radius) )
shape_list.append( rcc_shape("%s8.s"%self.name, x4+self.origin, x5-x4, radius) )
shape_list.append( rcc_shape("%s9.s"%self.name, x1+self.origin, x6, radius) )
shape_list.append( rcc_shape("%s10.s"%self.name, x2+self.origin, x6-x2, radius) )
shape_list.append( rcc_shape("%s11.s"%self.name, x3+self.origin, x6-x3, radius) )
shape_list.append( rcc_shape("%s12.s"%self.name, x4+self.origin, x6-x4, radius) )
shape_list.append( rcc_shape("%s13.s"%self.name, x5+self.origin, x2, radius) )
shape_list.append( rcc_shape("%s14.s"%self.name, x5+self.origin, -x2, radius) )
shape_list.append( rcc_shape("%s15.s"%self.name, x5+self.origin, x3, radius) )
shape_list.append( rcc_shape("%s16.s"%self.name, x5+self.origin, -x3, radius) )
shape_list.append( rcc_shape("%s17.s"%self.name, x6+self.origin, x2, radius) )
shape_list.append( rcc_shape("%s18.s"%self.name, x6+self.origin, -x2, radius) )
shape_list.append( rcc_shape("%s19.s"%self.name, x6+self.origin, x3, radius) )
shape_list.append( rcc_shape("%s20.s"%self.name, x6+self.origin, -x3, radius) )
shape_list.append( rcc_shape("%s21.s"%self.name, x1+self.origin, x5-x3, radius) )
shape_list.append( rcc_shape("%s22.s"%self.name, x2+self.origin, x5-x3-x2, radius) )
shape_list.append( rcc_shape("%s23.s"%self.name, x1+self.origin, x6-x3, radius) )
shape_list.append( rcc_shape("%s24.s"%self.name, x2+self.origin, x6-x3-x2, radius) )
shape_list.append( rcc_shape("%s25.s"%self.name, x1+self.origin, x5-x2, radius) )
shape_list.append( rcc_shape("%s26.s"%self.name, x1+self.origin, x6-x2, radius) )
shape_list.append( rcc_shape("%s27.s"%self.name, x3+self.origin, x5-x2-x3, radius) )
shape_list.append( rcc_shape("%s28.s"%self.name, x3+self.origin, x6-x2-x3, radius) )
shape_list.append( rcc_shape("%s29.s"%self.name, x3+self.origin, x5, radius) )
shape_list.append( rcc_shape("%s30.s"%self.name, x3+self.origin, x6, radius) )
shape_list.append( rcc_shape("%s31.s"%self.name, x4+self.origin, x5-x2, radius) )
shape_list.append( rcc_shape("%s32.s"%self.name, x4+self.origin, x6-x2, radius) )
shape_list.append( rcc_shape("%s33.s"%self.name, x4+self.origin, x5-x3, radius) )
shape_list.append( rcc_shape("%s34.s"%self.name, x4+self.origin, x6-x3, radius) )
shape_list.append( rcc_shape("%s35.s"%self.name, x2+self.origin, x5, radius) )
shape_list.append( rcc_shape("%s36.s"%self.name, x2+self.origin, x6, radius) )
return(shape_list)
class octprism():
""" octagonal prism primitive using brlcad arbn 10 """
def __init__(self, name, origin, height, radius):
self.name = name
self.origin = origin # base of the prism
self.height = height # vector to the other end of the prism
self.radius = radius
self.x = [sp.array(f) for f in it.product([1,-1,0],[1,-1,0],[0])]
self.x[8] = sp.array([0,0,1])
self.x.append(sp.array([0,0,-1]))
self.r = self.radius * sp.ones(len(self.x))
self.r[8] = self.height/2.0
self.r[9] = self.height/2.0
def mged_string(self):
formtup = [self.name]
for f in zip(self.x, self.r):
formtup.append(f[0][0])
formtup.append(f[0][1])
formtup.append(f[0][2])
formtup.append(f[1])
return("in %s arbn 10 %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g\n" % tuple(formtup))
if __name__ == "__main__":
import os
# Path to the mged executables:
MGED_PATH = "~/brlcad/.build/bin/"
# octet truss parameters, member radius and length
radius = 0.4
length = 8.0
x1 = sp.array([0.0, 0.0, 0.0])
x2 = sp.array([length, 0.0, 0.0])
x3 = sp.array([0.0, length, 0.0])
x4 = sp.array([length, length, 0.0])
x5 = sp.array([length/2.0, length/2.0, length*sp.sin(sp.pi/4.0)])
x6 = sp.array([length/2.0, length/2.0, -length*sp.sin(sp.pi/4.0)])
offset = length * sp.array([1.0,1.0,0.0])
offset2 = length * sp.array([1.0,-1.0,0.0])
offset3 = sp.sqrt(2*(length**2)) * sp.array([0.0,0.0,1.0])
angle = sp.pi / 4.0
rotation = sp.array([[sp.cos(angle), -sp.sin(angle), 0],[sp.sin(angle), sp.cos(angle), 0],[0, 0, 1.0]])
# command file:
mged_shape_commands = "octet3_prim_%s_%s_shapes.mged" % (radius, length)
mged_region_commands = "octet3_prim_%s_%s_regions.mged" % (radius, length)
mged_group_commands = "octet3_prim_%s_%s_group.mged" % (radius, length)
brlcad_dbase = "octet3_prim_%s_%s.g" % (radius, length)
stl_file = "octet3_prim_%s_%s.stl" % (radius, length)
rname = "cell0.r"
gname = "octetcube.c"
ot_cells = []
ni,nj,nk = (11, 11, 11)
for i in range(ni):
for j in range(nj):
for k in range(nk):
ot_cells.append(octet_unitcell("%d%d%d"%(i,j,k), i*offset + j*offset2 + k*offset3, length, radius))
# make each octet truss unit cell a region
shapes = []
cells = []
group_string = "g " + gname
for i,c in enumerate(ot_cells):
cellr_string = "r cell%d.r" % i
for s in c.shapes():
cellr_string = cellr_string + " u " + s.name
shapes.append(s.mged_string())
cells.append(cellr_string)
# group each region (unit cell) into the final assembly combination
group_string = group_string + " cell%d.r" %i
fout = open(mged_shape_commands, "w")
for s in shapes:
fout.write(s)
fout.write("\n")
fout.close()
fout = open(mged_region_commands, "w")
for r in cells:
fout.write(r + "\n")
fout.close()
fout = open(mged_group_commands, "w")
fout.write(group_string + "\n")
fout.close()
# # command to make a region out of all the parts in the shape list:
# mgedr = "r region1.r"
# # for s in shape_list:
# # mgedr = mgedr + " u " + s.name # union parts to region
# for c in ot_cells:
# for s in c.shapes():
# mgedr = mgedr + " u " + s.name
# # for f in framelist:
# # mgedr = mgedr + " u " + f.name
# fout.write(mgedr + "\n")
# fout.write("B region1.r\n")
# fout.write("oed / region1.r/0001.s\n")
# fout.write("rot 0 0 45\n")
# fout.write("translate %g %g %g\n" % (0,length*sp.sin(sp.pi/4.0),length*sp.sin(sp.pi/4.0)))
# fout.write("accept\n")
# fout.close()
# run the commands in mged
retval = os.system("cat %s | %smged -c %s" % (mged_shape_commands, MGED_PATH, brlcad_dbase) )
retval = os.system("cat %s | %smged -c %s" % (mged_region_commands, MGED_PATH, brlcad_dbase) )
retval = os.system("cat %s | %smged -c %s" % (mged_group_commands, MGED_PATH, brlcad_dbase) )
# export the region to an stl file
atol = 0.1 # absolute tolerance
D = 0.000001 #0.000001 # calculation accuracy
retval = os.system("%sg-stl -b -a %f -D %f -o %s %s %s" % (MGED_PATH, atol, D, stl_file, brlcad_dbase, gname))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.