Commit baf8dddd authored by Jayant Khatkar's avatar Jayant Khatkar
Browse files

get area of section voxel sits in

parent 5a665667
import sys
import matplotlib.pyplot as plt
import numpy as np
import time
import pandas as pd
......@@ -11,6 +12,19 @@ import pandas as pd
import json
import utils as tu
from os.path import exists
import reebgraph
import pymeshfix as pf
import trimesh as tm
from shapely.geometry import Point
def to_trimesh(mesh):
"""
Converts a mesh into a trimesh format
"""
mesh_fix = pf.MeshFix(mesh)
trimesh_mesh = tm.base.Trimesh(vertices=mesh_fix.v, faces=mesh_fix.f)
return trimesh_mesh
def voxelised_csv(mesh_file_name,
......@@ -116,14 +130,56 @@ def contour2dict(c):
return {'pos':c.pos, 'time':c.time}
def vox_section_area(voxels, mesh_file_name):
mesh = pv.read(mesh_file_name)
tu.transform_to_bed_centre(mesh, [110, 110, 0])
tu.transform_to_z0(mesh)
tm_mesh = to_trimesh(mesh)
SecArea = np.zeros(len(voxels))
z = 0
i = 0
while 0 in SecArea:
i = np.where(SecArea==0)[0][0]
c = voxels.iloc[i,1:4].to_numpy()
print(c)
# if new slice required
if c[2] != z:
sl = tm_mesh.section(plane_origin=c, plane_normal=[0,0,1])
if sl is None:
SecArea[i] = 1e7
continue
sl2d,_ =sl.to_planar()
z = c[2]
for e in sl2d.split():
e.apply_translation(-sl2d.centroid)
e.apply_translation(sl.centroid[0:2])
poly = e.polygons_full[0]
if poly.contains(Point(c[:2])):
SecArea[i] = poly.area
if SecArea[i] == 0:
# TODO HACK so it uses neighbour's SecArea
SecArea[i] = 1e7
if z>3:
break
if __name__ == '__main__':
obj = 'models/M9'
#check_data(obj + '.stl', obj + '_raw.csv', from_inches=True)
#out = voxelised_csv(obj + '.stl', obj + '_raw.csv', voxels=obj+'_vox_centers.csv', from_inches=True)
#out.to_csv(obj + '_voxels.csv')
visualise_voxels(obj + '_voxels.csv')
#visualise_voxels(obj + '_voxels.csv')
voxels = pd.read_csv(obj + '_voxels.csv')
vox_section_area(voxels, obj + '.stl')
#contours = gc.decode_gcode(obj + '.gcode')
#outfile = open(obj + 'contours.json','w')
#json.dump([contour2dict(c) for c in contours], outfile)
outfile.close()
#outfile.close()
Supports Markdown
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