def main(): import argparse from lace.mesh import Mesh parser = argparse.ArgumentParser() parser.add_argument('-p', '--path', help='filepath to mesh', required=True) parser.add_argument('-c', '--cloud', help='display point cloud', required=False, default=False, action='store_true') parser.add_argument('-d', '--direction', help='direction of connected component', choices=['N', 'S', 'E', 'W'], default=None, required=False) args = parser.parse_args() path_to_mesh = args.path mesh = Mesh(filename=path_to_mesh, vc='SteelBlue') point_on_plane = np.array([0., 1., 0.]) n1 = np.array([0., 1., 0.]) p1 = Plane(point_on_plane, n1) n2 = np.array([1., 0., 0.]) p2 = Plane(point_on_plane, n2) n3 = np.array([1., 1., 0.]) n3 /= np.linalg.norm(n3) p3 = Plane(point_on_plane, n3) n4 = np.array([-1., 1., 0.]) n4 /= np.linalg.norm(n4) p4 = Plane(point_on_plane, n4) dirmap = { 'N': [0., +100., 0.], 'S': [0., -100., 0.], 'E': [+100., 0., 0.], 'W': [-100., 0., 0.], None: None, } neighborhood = dirmap[args.direction] if neighborhood != None: neighborhood = np.array([neighborhood]) xs1 = p1.mesh_xsection(mesh, neighborhood=neighborhood) xs2 = p2.mesh_xsection(mesh, neighborhood=neighborhood) xs3 = p3.mesh_xsection(mesh, neighborhood=neighborhood) xs4 = p4.mesh_xsection(mesh, neighborhood=neighborhood) lines = [ polyline.as_lines() for polyline in [xs1, xs2, xs3, xs4] ] if args.cloud: mesh.f = [] from lace.meshviewer import MeshViewer mv = MeshViewer(keepalive=True) mv.set_dynamic_meshes([mesh], blocking=True) mv.set_dynamic_lines(lines)
def _load(f, mesh=None): import numpy as np from lace.mesh import Mesh from lace.serialization.ply import plyutils res = plyutils.read(f.name) if not mesh: mesh = Mesh() mesh.v = np.array(res['pts'], dtype=np.float64).T.copy() mesh.f = np.array(res['tri'], dtype=np.uint32).T.copy() if not mesh.f.shape[0]: mesh.f = None if 'color' in res: mesh.vc = np.array(res['color']).T.copy() / 255 if 'normals' in res: mesh.vn = np.array(res['normals']).T.copy() return mesh
def _load(f, mesh=None): import numpy as np from lace.mesh import Mesh if not mesh: mesh = Mesh() faces = [] facenormals = [] verts = [] head = f.readline().strip() if head.startswith("solid"): # ascii STL format #name = head[6:] current_face = [] for line in f: line = line.split() if line[0] == "endsolid": break elif line[0] == "facet": current_face = [] if line[1] == "normal": try: facenormals.append([float(x) for x in line[2:]]) except: # pylint: disable=bare-except facenormals.append([np.nan, np.nan, np.nan]) else: facenormals.append([np.nan, np.nan, np.nan]) elif line[0] == "endfacet": faces.append(current_face) current_face = [] elif line[0:2] == ["outer", "loop"]: pass elif line[0] == "endloop": pass elif line[0] == "vertex": current_face.append(len(verts)) try: verts.append([float(x) for x in line[1:]]) except: # pylint: disable=bare-except verts.append([np.nan, np.nan, np.nan]) else: raise ValueError( "Badly formatted STL file. I don't understand the line %s" % line) else: raise Exception( "Looks like this is a binary STL file; you're going to have to implement that" ) # format docs are here: http://en.wikipedia.org/wiki/STL_(file_format) mesh.v = np.array(verts, dtype=np.float64).copy() mesh.f = np.array(faces, dtype=np.uint32).copy() mesh.fn = np.array(facenormals, dtype=np.float64).copy() return mesh
def _load(f, mesh=None): from lace.mesh import Mesh try: v, f, vn, vc = _parse_wrl(f) except Exception: import traceback tb = traceback.format_exc() raise ParseError("Unable to parse wrl file with exception : \n[\n%s]" % tb) if not mesh: mesh = Mesh() if v.size != 0: mesh.v = v if f.size != 0: mesh.f = f if vn.size != 0: mesh.vn = vn if vc.size != 0: mesh.vc = vc return mesh
def test_f_converts_datatype(self): m = Mesh() m.f = np.array([[1, 1, 1]], dtype=np.float64) self.assertEqual(m.f.dtype, np.uint64)
def _load(fd, mesh=None): from collections import OrderedDict from baiji import s3 from lace.mesh import Mesh from lace.cache import sc import lace.serialization.obj.objutils as objutils # pylint: disable=no-name-in-module v, vt, vn, vc, f, ft, fn, mtl_path, landm, segm = objutils.read(fd.name) if not mesh: mesh = Mesh() if v.size != 0: mesh.v = v if f.size != 0: mesh.f = f if vn.size != 0: mesh.vn = vn if vt.size != 0: mesh.vt = vt if vc.size != 0: mesh.vc = vc if fn.size != 0: mesh.fn = fn if ft.size != 0: mesh.ft = ft if segm: mesh.segm = OrderedDict([(k, v if isinstance(v, list) else v.tolist()) for k, v in segm.items()]) def path_relative_to_mesh(filename): # The OBJ file we're loading may have come from a local path, an s3 url, # or a file cached by sc. Since OBJ defines materials and texture files # with paths relative to the OBJ itself, we need to cope with the various # possibilities and if it's a cached file make sure that the material and # texture have been downloaded as well. # # If an absolute path is given and the file is missing, try looking in the same directory; # this lets you find the most common intention when an abs path is used. # # NB: We do not support loading material & texture info from objs read # from filelike objects without a location on the filesystem; what would # the relative file names mean in that case, anyway? (unless we're given # a valid absolute path, in which case go for it) import os import re # The second term here let's us detect windows absolute paths when we're running on posix if filename == os.path.abspath(filename) or re.match( r'^.\:(\\|/)', filename): if s3.exists(filename): return filename else: filename = s3.path.basename(filename) if hasattr(fd, 'remotename'): mesh_path = fd.remotename elif hasattr(fd, 'name'): mesh_path = fd.name else: return None path = s3.path.join(s3.path.dirname(mesh_path), filename) if sc.is_cachefile(mesh_path): try: return sc(path) except s3.KeyNotFound: return None return path mesh.materials_filepath = None if mtl_path: materials_filepath = path_relative_to_mesh(mtl_path.strip()) if materials_filepath and s3.exists(materials_filepath): with s3.open(materials_filepath, 'r') as f: mesh.materials_file = f.readlines() mesh.materials_filepath = materials_filepath if hasattr(mesh, 'materials_file'): mesh.texture_filepaths = { line.split(None, 1)[0].strip(): path_relative_to_mesh(line.split(None, 1)[1].strip()) for line in mesh.materials_file if line.startswith('map_K') } if 'map_Ka' in mesh.texture_filepaths: mesh.texture_filepath = mesh.texture_filepaths['map_Ka'] elif 'map_Kd' in mesh.texture_filepaths: mesh.texture_filepath = mesh.texture_filepaths['map_Kd'] if landm: mesh.landm = landm return mesh