Commit 3d120f3e authored by Jayant Khatkar's avatar Jayant Khatkar
Browse files

cleanup and export voxels as csv

parent c92134ad
...@@ -12,12 +12,5 @@ def main(fname): ...@@ -12,12 +12,5 @@ def main(fname):
if __name__ == '__main__': if __name__ == '__main__':
fname = 'tensile.vtk' fname = 'tensile.vtk'
pl = Planner(fname, 1, 1) #0.25) pl = Voxelizer(fname, 1, 1) #0.25)
#pl.export_voxels('tensile')
def test(cen):
plotter = pv.Plotter()
plotter.add_mesh(pl.grid, opacity=0.2)
plotter.add_mesh(pv.PolyData(cen))
plotter.show()
test(pl.centers)
from .planner import Planner from .planner import Voxelizer
...@@ -7,9 +7,10 @@ import trimesh as tm ...@@ -7,9 +7,10 @@ import trimesh as tm
import pymeshfix as pf import pymeshfix as pf
from scipy.spatial import KDTree from scipy.spatial import KDTree
from scipy.interpolate import LinearNDInterpolator from scipy.interpolate import LinearNDInterpolator
import pandas as pd
class Planner: class Voxelizer:
""" """
Track all relevant mesh objects for pin insertion Track all relevant mesh objects for pin insertion
""" """
...@@ -21,28 +22,25 @@ class Planner: ...@@ -21,28 +22,25 @@ class Planner:
): ):
# record params # record params
s = time.time()
self.voxel_dim = voxel_dim self.voxel_dim = voxel_dim
self.lheight = layer_height self.lheight = layer_height
# read mesh data
s = time.time()
print("Reading mesh") print("Reading mesh")
self.mesh = pv.read(mesh_file_name) self.mesh = pv.read(mesh_file_name)
self.boundary = self.mesh.decimate_boundary() self.boundary = self.mesh.decimate_boundary()
v = time.time()
print(v-s)
#voxelize print("Voxelizing mesh")
hwratio = voxel_dim/self.lheight hwratio = voxel_dim/self.lheight
m = self.boundary.copy() m = self.boundary.copy()
m.points[:,2] *= hwratio m.points[:,2] *= hwratio
v = time.time()
print(v-s)
print("Voxelizing mesh")
self.grid = pv.voxelize(m, density=self.voxel_dim) self.grid = pv.voxelize(m, density=self.voxel_dim)
self.grid.points[:,2] *= 1/hwratio self.grid.points[:,2] *= 1/hwratio
# get voxel centers
c = time.time() c = time.time()
print(c-s) print(c-s)
print("Getting Voxel centers") print("Getting Voxel centers")
points_to_add = [] points_to_add = []
temptree = KDTree(self.grid.points, leafsize=5) #kd tree for fast lookup of adjacent grid points temptree = KDTree(self.grid.points, leafsize=5) #kd tree for fast lookup of adjacent grid points
...@@ -52,16 +50,17 @@ class Planner: ...@@ -52,16 +50,17 @@ class Planner:
d2, ii = temptree.query(self.grid.points + [voxel_dim, 0, layer_height]) d2, ii = temptree.query(self.grid.points + [voxel_dim, 0, layer_height])
d3, ii = temptree.query(self.grid.points + [0, voxel_dim, 0]) d3, ii = temptree.query(self.grid.points + [0, voxel_dim, 0])
self.centers = self.grid.points[d1 + d2 + d3 < 1e-4] + [voxel_dim/2, voxel_dim/2, layer_height/2] self.centers = self.grid.points[d1 + d2 + d3 < 1e-4] + [voxel_dim/2, voxel_dim/2, layer_height/2]
# move grid points to center of each voxel and remove grid points which are not contained in a voxel
gs = time.time() gs = time.time()
print(gs-c) print(gs-c)
print("Interpolating pointcloud to grid stresses")
# get mean stress at each voxel
print("Interpolating pointcloud to grid stresses")
self.interpolator = LinearNDInterpolator(self.mesh.points, self.mesh['StressValues']) self.interpolator = LinearNDInterpolator(self.mesh.points, self.mesh['StressValues'])
print("Completed generating LinearND lookup")
print(time.time()-gs) # up to 100s for mesh with 300k points print(time.time()-gs) # up to 100s for mesh with 300k points
print("Performing interpolations")
self.cstress = self.interpolator(self.centers)
def check_interp1s(self, node_is): def check_interp1s(self, node_is):
""" """
...@@ -78,3 +77,13 @@ class Planner: ...@@ -78,3 +77,13 @@ class Planner:
e = time.time() e = time.time()
print(e-s) print(e-s)
return node_i_pred_stress, node_i_stress return node_i_pred_stress, node_i_stress
def export_voxels(self, fname):
"""
Export voxel center locations and voxel stress to csv
"""
df = pd.DataFrame(np.hstack((self.centers,self.cstress.reshape(-1,1))), columns=['x','y','z','stress'])
csv_name = "{}-{}-{}.csv".format(fname, self.voxel_dim, self.lheight)
df.to_csv(csv_name)
return csv_name
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