Created
May 6, 2016 21:19
-
-
Save gperonato/477d2461fa1e6ffef42c8cdee1ce5721 to your computer and use it in GitHub Desktop.
Qhull access from Grasshopper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Script to export point clouds to QHull and run QConvex | |
#Inputs: P (Tree of points), Run (Boolean) | |
#Outputs: paths (List of paths) | |
import rhinoscriptsyntax as rs | |
import os | |
import subprocess | |
def tree_to_list(input, retrieve_base = lambda x: x[0]): | |
"""Returns a list representation of a Grasshopper DataTree""" | |
# written by Giulio Piacentino, giulio@mcneel.com | |
def extend_at(path, index, simple_input, rest_list): | |
target = path[index] | |
if len(rest_list) <= target: rest_list.extend([None]*(target-len(rest_list)+1)) | |
if index == path.Length - 1: | |
rest_list[target] = list(simple_input) | |
else: | |
if rest_list[target] is None: rest_list[target] = [] | |
extend_at(path, index+1, simple_input, rest_list[target]) | |
all = [] | |
for i in range(input.BranchCount): | |
path = input.Path(i) | |
extend_at(path, 0, input.Branch(path), all) | |
return retrieve_base(all) | |
def qhull(points): | |
s = '3 \n' | |
s += str(len(points)) + '\n' | |
for p in points: | |
s += str(rs.PointCoordinates(p)).replace(","," ") + '\n' | |
return s | |
def write(s,path): | |
path.replace('\\','/') | |
out_file = open(path,"w") | |
out_file.write(s) | |
out_file.close() | |
if Run: | |
points = tree_to_list(P,retrieve_base = lambda P: P) | |
path = r"C:\Users\peronato\Documents\Simulations\trees\qhull\\" | |
commands=[] | |
paths = [] | |
for p in xrange(len(points)): | |
pathin = path+'input_'+str(p)+'.txt' | |
pathout = path+'output_'+str(p)+'.txt' | |
write(qhull(points[p]), pathin) | |
command = r'C:\QHull\qhull-2015.2\bin\qconvex.exe' + ' o TI ' + pathin + ' TO ' + pathout | |
commands.append(command) | |
paths.append(pathout) | |
myString = "\n".join(commands) | |
write(myString,path + 'batch.bat') | |
p = subprocess.Popen("batch.bat", cwd=path, shell=True) | |
stdout, stderr = p.communicate() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Script to import the QConvex output data | |
#Inputs: paths (list of paths), Run (Boolean) | |
#Outputs: vertices (tree of face vertces) | |
def tree_to_list(input, retrieve_base = lambda x: x[0]): | |
"""Returns a list representation of a Grasshopper DataTree""" | |
# written by Giulio Piacentino, giulio@mcneel.com | |
def extend_at(path, index, simple_input, rest_list): | |
target = path[index] | |
if len(rest_list) <= target: rest_list.extend([None]*(target-len(rest_list)+1)) | |
if index == path.Length - 1: | |
rest_list[target] = list(simple_input) | |
else: | |
if rest_list[target] is None: rest_list[target] = [] | |
extend_at(path, index+1, simple_input, rest_list[target]) | |
all = [] | |
for i in range(input.BranchCount): | |
path = input.Path(i) | |
extend_at(path, 0, input.Branch(path), all) | |
return retrieve_base(all) | |
def list_to_tree(input, none_and_holes=True, source=[0]): | |
"""Transforms nestings of lists or tuples to a Grasshopper DataTree""" | |
# written by Giulio Piacentino, giulio@mcneel.com | |
from Grasshopper import DataTree as Tree | |
from Grasshopper.Kernel.Data import GH_Path as Path | |
from System import Array | |
def proc(input,tree,track): | |
path = Path(Array[int](track)) | |
if len(input) == 0 and none_and_holes: tree.EnsurePath(path); return | |
for i,item in enumerate(input): | |
if hasattr(item, '__iter__'): #if list or tuple | |
track.append(i); proc(item,tree,track); track.pop() | |
else: | |
if none_and_holes: tree.Insert(item,path,i) | |
elif item is not None: tree.Add(item,path) | |
if input is not None: t=Tree[object]();proc(input,t,source[:]);return t | |
if run: | |
tree = tree_to_list(paths, retrieve_base = lambda paths: paths) | |
ind = [] | |
pts = [] | |
for i in xrange(len(tree)): | |
npoints = int(tree[i][1].split()[0]) | |
ind.append(tree[i][npoints+2:]) | |
pts.append(tree[i][2:npoints+1]) | |
import rhinoscriptsyntax as rs | |
indices = [] | |
points = [] | |
for i in xrange(len(ind)): | |
indices.append([]) | |
for n in ind[i]: | |
indices[i].append(n[2:].split()) | |
points.append([]) | |
for n in pts[i]: | |
points[i].append(rs.Str2Pt(n.split())) | |
tri = [] | |
for i in xrange(len(indices)): | |
tr= [] | |
for n in xrange(len(indices[i])): | |
t = [] | |
for c in xrange(3): | |
t.append(points[i][int(indices[i][n][c])-1]) | |
tr.append(t) | |
tri.append(tr) | |
vertices = list_to_tree(tri) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment