import subprocess as sp import subprocess import glob, os import shutil import pointcloud as pc modelType = "off" samplingRate = 0.003 maxDistance = 0.003 maxAngleDistance = 0.01 errorSigma = 0 k = 16 pointCloudSize = 2048 cutOutProb = 0 maxIterations = 10 toolPath = "C:/Projekte/csg_playground_build/Release/primgen.exe" def getExtension(modType): if("off" in modType): return "off" elif("obj" in modType): return "obj" elif("csg" in modType): return ".json" else: return "" def run(modelPath, outputFolder): executable = "{} {} \"{}\" \"{}\" {} {} {} {} {} {} {} {}".format( toolPath, modelType, modelPath, outputFolder, samplingRate, maxDistance, maxAngleDistance, errorSigma, k, pointCloudSize, cutOutProb, maxIterations) print("Call generator with " + executable) p = sp.Popen(executable, stdout=subprocess.PIPE) for line in p.stdout: print(line) # Wait until process terminates (without using p.wait()) # while p.poll() is None: # Process hasn't exited yet, let's wait some # time.sleep(0.5) # Get return code from process return_code = p.returncode if return_code == -1: print("Unable to generate primtitives for model {}".format(modelPath)); else: print("Done. Exist Code: " + str(return_code)) def extract_clusters(outputFolder, cluster_method="none", **kwargs): if "none" in cluster_method: return all_clusters = [] for subdir, dirs, _ in os.walk(outputFolder): for dir in dirs: path = os.path.join(subdir, dir) for _,_,files in os.walk(path): for file in files: file = os.path.join(path, file.lower()) print(file) if file.endswith("_pc.xyz"): pointcloud = pc.read_pointcloud(file); clusters = [] if "per_primitive" in cluster_method: clusters = pc.cluster_per_column(pointcloud, column=7) # primitive id column elif "cube" in cluster_method: clusters = pc.cluster_cubes(pointcloud, kwargs.get("cluster_dims", [1,1,1])) elif "dbscan" in cluster_method: clusters = pc.cluster_dbscan(pointcloud, selected_indices=kwargs.get("selected_indices", [0,1,2,3,4,5]), eps=kwargs.get("eps", 0.1), min_samples=kwargs.get("min_samples", None) ) for idx, cluster in enumerate(clusters): pos = file.rfind("pc.xyz") new_file = file[:pos] + str(idx) + "_pc.xyz" pc.write_pointcloud(new_file, cluster) # all_clusters.extend(clusters) return all_clusters def runForFolder(modelFolder, outputFolder): os.chdir(modelFolder) modelFiles = glob.glob("*." + getExtension(modelType)) print("Working on {} model files with extension {} from folder {}.".format(len(modelFiles),getExtension(modelType), modelFolder)) folderIdx = 0 for modelFile in modelFiles: try: subfolderPath = outputFolder + str(folderIdx) + "/" folderIdx += 1 print("Check if output sub folder exists...") if os.path.exists(subfolderPath) and os.path.isdir(subfolderPath): shutil.rmtree(subfolderPath) print("Yes => Existing sub folder was deleted.") os.mkdir(subfolderPath) print("Successfully created the directory %s " % subfolderPath) run(modelFolder + modelFile, subfolderPath) except OSError as err: print("Creation of the directory %s failed." % str(err)) if __name__ == "__main__": outputFolder = "D:/output_0/" modelFolder = "C:/Users/friedrich/PycharmProjects/data/models/" # clusters = extract_clusters("C:/Projekte/csg_playground_build/testOFF", "cube", # cluster_dims=[2,2,2]) # #eps=0.1, min_samples=0.01, selected_indices=[7]) # pc.draw_clusters(clusters) runForFolder(modelFolder, outputFolder)