def plot(self): """Plot a force diagram with a plotter with all the default settings.""" from compas_plotters import MeshPlotter plotter = MeshPlotter(self, figsize=(12, 8), tight=True) plotter.draw_vertices(radius=0.05) plotter.draw_edges() plotter.show()
def visualize_mesh_traversal() -> None: ''' Datastructures task ''' mesh = Mesh.from_obj(get('faces.obj')) x_values = {} for vkey in mesh.vertices_on_boundary(): x_values[vkey] = mesh.vertex_coordinates(vkey)[0] max_x = max(x_values.values()) print("Vertices on the right edge of the mesh:") print([key for key in x_values if x_values[key] == max_x]) start_key = int(input("\nSelect start vertex: ")) path_verts = traverse_mesh(mesh, start_key) print('\nPath calculated, starting MeshPlotter.') plotter = MeshPlotter(mesh, figsize=(16, 10)) plotter.draw_vertices(text={key: key for key in path_verts}, radius=0.2, facecolor={key: '#ff0000' for key in path_verts}) plotter.draw_edges() plotter.draw_faces() plotter.show()
def plot_mesh(mesh): """ Compas Mesh Plotter """ all_keys=list(mesh.vertices()) plotter=MeshPlotter(mesh) plotter.draw_vertices(text='key', keys=all_keys, radius=0.01) plotter.draw_faces() plotter.show()
def plot(self): """Plot a form diagram with a plotter with all the default settings.""" from compas_plotters import MeshPlotter plotter = MeshPlotter(self, figsize=(12, 8), tight=True) vertexcolor = {} vertexcolor.update({key: '#00ff00' for key in self.vertices_where({'is_fixed': True})}) vertexcolor.update({key: '#ff0000' for key in self.vertices_where({'is_anchor': True})}) plotter.draw_vertices(facecolor=vertexcolor) plotter.draw_edges(keys=list(self.edges_where({'_is_edge': True}))) plotter.draw_faces(keys=list(self.faces_where({'_is_loaded': True}))) plotter.show()
def plot_mesh(mesh_info_dict, current_vertex_info_dict, edges_flag=1, faces_flag=1): """ This function plot the mesh and the path dynamically. But the problem is that it seems that plotter.close() have not been defined in the current version of the COMPAS (0.10.0) """ mesh = mesh_info_dict['mesh'] # define the facecolor dictionaries for vertices facecolor = {} # facecolor for all on-baoundary vertices facecolor.update({ key: (200, 200, 200) for key in mesh_info_dict['non_on_boundry_vertices'] }) # facecolor for all non-on-baoundary vertices facecolor.update({ key: (150, 150, 150) for key in mesh_info_dict['on_boundry_vertices'] }) # facecolor for all on-baoundary neighborhood vertices facecolor.update({ key: (0, 255, 0) for key in current_vertex_info_dict['non_on_boundry_neighbs_vertices'] }) # facecolor for all non-on-baoundary neighborhood vertices facecolor.update({ key: (0, 0, 255) for key in current_vertex_info_dict['on_boundry_neighbs_vertices'] }) # facecolor for the current vertes facecolor.update({current_vertex_info_dict['current_vertex']: (255, 0, 0)}) # define important vertices keys = mesh_info_dict['all_vertices'] # instantiate the MeshPlotter plotter = MeshPlotter(mesh=mesh, figsie=(4, 4)) # draw vertices plotter.draw_vertices(radius=0.3, text='key', keys=keys, facecolor=facecolor) # draw edges if edges_flag == 1: plotter.draw_edges() # draw faces if faces_flag == 1: plotter.draw_faces() # show mesh plot plotter.show()
def traverse_boundary_to_boundary(mesh): """ traverse the mesh from boundary to boundary in a "straight" line and visulize the results mesh: mesh data structure return a list of ordered vertex keys for the path and the mesh plotter with highlighted path (use plotter.show() to visualize)) """ bound_keys=mesh.vertices_on_boundary() # randomly pick a boundary key ind=randrange(len(bound_keys)) key=bound_keys[ind] pick_keys=[key] prev_fkeys=set() # non-corner vertices if mesh.vertex_degree(key)>2: f_keys=mesh.vertex_faces(key) adj_keys=mesh.face_adjacency_vertices(f_keys[0], f_keys[1]) next_key=list(set(adj_keys)-set(pick_keys))[0] pick_keys.append(next_key) prev_fkeys.update(f_keys) while next_key not in bound_keys: f_keys=mesh.vertex_faces(next_key) f_keys=list(set(f_keys)-prev_fkeys) adj_keys=mesh.face_adjacency_vertices(f_keys[0], f_keys[1]) next_key=list(set(adj_keys)-set(pick_keys))[0] pick_keys.append(next_key) prev_fkeys.update(f_keys) # corner vertices elif mesh.vertex_degree(key)==2: f_keys=mesh.vertex_faces(key) next_key=mesh.face_vertex_ancestor(f_keys[0], key) # one way of moving on the boundary! pick_keys.append(next_key) prev_fkeys.update(f_keys) while mesh.vertex_degree(next_key)!=2: f_keys=mesh.vertex_faces(next_key) f_keys=list(set(f_keys)-prev_fkeys) next_key=mesh.face_vertex_ancestor(f_keys[0], next_key) pick_keys.append(next_key) prev_fkeys.update(f_keys) # mesh plotter with highlighted path plotter = MeshPlotter(mesh, figsize=(8, 5)) plotter.draw_vertices(text='key', fontsize=10.0, radius=0.02) plotter.draw_edges() plotter.highlight_path(pick_keys, edgecolor=(255, 0, 0), edgewidth=2.0) return pick_keys, plotter
def plot(self): from compas_plotters import MeshPlotter plotter = MeshPlotter(self, figsize=(12, 8), tight=True) vertexcolor = {} vertexcolor.update({ key: '#00ff00' for key in self.vertices_where({'is_fixed': True}) }) vertexcolor.update({ key: '#0000ff' for key in self.vertices_where({'_is_external': True}) }) vertexcolor.update({ key: '#ff0000' for key in self.vertices_where({'is_anchor': True}) }) plotter.draw_vertices(facecolor=vertexcolor) plotter.draw_edges(keys=list(self.edges_where({'_is_edge': True}))) plotter.draw_faces(keys=list(self.faces_where({'_is_loaded': True}))) plotter.show()
from compas.datastructures import Mesh from compas.utilities import geometric_key from compas_plotters import MeshPlotter from compas.geometry import delaunay_triangulation points = [[2.994817685045075, 12.855606612493078, 0.0], [4.185204599300653, 9.527867361977242, 0.0], [4.414125159734419, 10.718254276232818, 0.0], [5.925000858597267, 9.344730913630228, 0.0], [8.900968144236211, 10.809822500406325, 0.0], [9.496161601363999, 8.566401008155429, 0.0], [7.710581229980631, 7.9254234389408875, 0.0], [7.847933566240888, 6.414547740078039, 0.0], [3.9104999267801377, 4.9036720412151915, 0.0], [5.2909301507195865, 6.342692886748852, 0.0]] vertices, faces = delaunay_triangulation(points, 'q30a1D') mesh = Mesh.from_vertices_and_faces(vertices, faces) plotter = MeshPlotter(mesh, figsize=(8, 5)) plotter.defaults['vertex.fontsize'] = 6 plotter.draw_faces() plotter.draw_vertices(text='key') plotter.show()
# return voronoi # ============================================================================== # Main # ============================================================================== if __name__ == "__main__": from compas.datastructures import Mesh from compas.geometry import pointcloud_xy from compas_plotters import MeshPlotter points = pointcloud_xy(200, (0, 50)) faces = delaunay_from_points(points) delaunay = Mesh.from_vertices_and_faces(points, faces) plotter = MeshPlotter(delaunay, figsize=(8, 5)) facecolor = { fkey: (255, 0, 0) if delaunay.face_normal(fkey)[2] > 0 else (0, 0, 255) for fkey in delaunay.faces() } plotter.draw_vertices(keys=list(delaunay.vertices_on_boundary()), radius=0.5) plotter.draw_faces(facecolor=facecolor) plotter.draw_edges(keys=list(delaunay.edges_on_boundary())) plotter.show()
# ============================================================================== # Compute equilibrium # ============================================================================== update_xyz_numpy(cablenet) # ============================================================================== # Visualize # ============================================================================== heights = cablenet.vertices_attribute('z') cmap = Colormap(heights, 'black') vertexcolor = {key: cmap(z) for key, z in zip(cablenet.vertices(), heights)} forces = cablenet.edges_attribute('f') cmap = Colormap(forces, 'rgb') edgecolor = {key: cmap(f) for key, f in zip(cablenet.edges(), forces)} plotter = MeshPlotter(cablenet, figsize=(16, 9)) plotter.draw_vertices(facecolor=vertexcolor, radius=0.05) plotter.draw_edges(width=2.0, color=edgecolor) plotter.save(FILE_P, dpi=150) # plotter.show() # ============================================================================== # Export # ============================================================================== cablenet.to_json(FILE_O)
import compas_libigl as igl HERE = os.path.dirname(__file__) FILE = os.path.join(HERE, '..', 'data', 'tubemesh.json') mesh = Mesh.from_json(FILE) mesh_quads_to_triangles(mesh) key_index = mesh.key_index() index_key = mesh.index_key() V = mesh.vertices_attributes('xyz') F = [[key_index[key] for key in mesh.face_vertices(fkey)] for fkey in mesh.faces()] root = mesh.get_any_vertex() # D = igl.trimesh_geodistance_exact(V, F, 0) D = igl.trimesh_geodistance_heat(V, F, key_index[root]) cmap = Colormap(D, 'red') plotter = MeshPlotter(mesh, figsize=(8, 5)) plotter.draw_faces() plotter.draw_vertices( text={root: 'root'}, radius=0.2, facecolor={key: cmap(d) for key, d in zip(mesh.vertices(), D)}) plotter.show()
from compas.datastructures import Mesh from compas_plotters import MeshPlotter from compas.utilities import i_to_rgb mesh = Mesh.from_obj(compas.get('hypar.obj')) for key, attr in mesh.vertices(True): attr['is_fixed'] = mesh.vertex_degree(key) == 2 fixed = [key for key in mesh.vertices_where({'is_fixed': True})] radius = {key: (0.05 if key in fixed else 0.01) for key in mesh.vertices()} plotter = MeshPlotter(mesh, figsize=(10, 7)) plotter.draw_vertices(radius=radius) plotter.draw_faces() plotter.draw_edges() def callback(k, args): print(k) if k % 100 == 0: dev = mesh_flatness(mesh, maxdev=0.02) plotter.update_vertices(radius=radius) plotter.update_faces( facecolor={fkey: i_to_rgb(dev[fkey]) for fkey in mesh.faces()}) plotter.update_edges()
attr['y'] = y attr['z'] = z # mesh = Mesh.from_obj(compas.get('faces.obj')) # mesh_quads_to_triangles(mesh) # mesh = Mesh.from_polyhedron(12) # mesh_quads_to_triangles(mesh) index_key = mesh.index_key() sources = [0] d = mesh_geodesic_distances(mesh, sources, m=1.0).tolist() dmin = min(d) dmax = max(d) drange = dmax - dmin facecolor = { key: i_to_blue(1 - (d[i] - dmin) / drange) for i, key in enumerate(mesh.vertices()) } for i in sources: facecolor[index_key[i]] = '#ff0000' plotter = MeshPlotter(mesh, figsize=(6, 4)) plotter.draw_vertices(facecolor=facecolor, radius=0.5) plotter.draw_faces() plotter.show()
from compas.utilities import Colormap from compas_plotters import MeshPlotter # ============================================================================== # Input geometry # ============================================================================== mesh = Mesh.from_off(igl.get('tubemesh.off')) mesh.quads_to_triangles() # ============================================================================== # Mass matrix # ============================================================================== mass = igl.trimesh_massmatrix(mesh) # ============================================================================== # Visualisation # ============================================================================== cmap = Colormap(mass, 'red') plotter = MeshPlotter(mesh, figsize=(8, 5)) plotter.draw_vertices(radius=0.2, facecolor={ key: cmap(mass[index]) for index, key in enumerate(mesh.vertices()) }) plotter.draw_faces() plotter.show()
scale = vertical_from_zmax(form, 3.0) # ============================================================================== # visualise # ============================================================================== z = form.vertices_attribute('z') zmin = min(z) zmax = max(z) plotter = MeshPlotter(form, figsize=(12, 8), tight=True) plotter.draw_vertices( keys=list(form.vertices_where({'is_external': False})), facecolor={ key: i_to_black((attr['z'] - zmin) / (zmax - zmin)) for key, attr in form.vertices_where({'is_external': False}, True) }, radius=0.1) plotter.draw_edges( keys=list(form.edges_where({'is_edge': True})), color={key: '#00ff00' for key in form.edges_where({'is_external': True})}, width={key: 2.0 for key in form.edges_where({'is_external': True})}) plotter.draw_faces(keys=list(form.faces_where({'is_loaded': True}))) plotter.show()
lines = [] for u, v in mesh.edges(): lines.append({ 'start': mesh.vertex_coordinates(u, 'xy'), 'end': mesh.vertex_coordinates(v, 'xy'), 'color': '#cccccc', 'width': 0.5 }) plotter.draw_lines(lines) # draw the vertices and edges in the starting configuration # and pause for a second before starting the dynamic visualization plotter.draw_vertices(facecolor={ key: '#000000' for key in mesh.vertices() if mesh.vertex_degree(key) == 2 }) plotter.draw_edges() plotter.update(pause=0.5) # run the smoother xyz = smooth_centroid_cpp(vertices, adjacency, fixed, kmax=kmax, callback=callback) # keep the plot alive
vertices = mesh.get_vertices_attributes('xyz') faces = [[key_index[key] for key in mesh.face_vertices(fkey)] for fkey in mesh.faces()] V = numpy.array(vertices, dtype=numpy.float64) F = numpy.array(faces, dtype=numpy.int32) heat_obj = igl.HeatObject() heat_obj.setMesh(V, F) d = heat_obj.getDistanceHeat(0) plotter = MeshPlotter(mesh, figsize=(8, 5)) plotter.draw_faces() plotter.draw_vertices( text={key: "{:.0f}".format(d[key_index[key]]) for key in mesh.vertices()}) plotter.show() # Option 2: pass compas mesh object to C++ and use it there "directly": # (in this case, the data is still copied on the c++ side, but if the data within the mesh object # would directly be compatible with the igl function, then it could be done without # copying or conversion) heat_obj2 = igl.HeatObject() heat_obj2.setMesh(mesh) d2 = heat_obj2.getDistanceHeat(0) plotter2 = MeshPlotter(mesh, figsize=(8, 5)) plotter2.draw_faces() plotter2.draw_vertices(
import os import compas from compas.datastructures import Mesh from compas_plotters import MeshPlotter HERE = os.path.dirname(__file__) DATA = os.path.join(HERE, 'data') FILE = os.path.join(DATA, 'faces.obj') mesh = Mesh.from_obj(FILE) plotter = MeshPlotter(mesh, figsize=(16, 10)) plotter.draw_vertices( facecolor={key: (255, 0, 0) for key in mesh.vertices_on_boundary()}, text={key: str(mesh.vertex_degree(key)) for key in mesh.vertices()}, radius=0.2) plotter.draw_edges(keys=list(mesh.edges_on_boundary()), color=(255, 0, 0)) plotter.draw_faces( facecolor={ key: (150, 255, 150) for key in mesh.faces() if not mesh.is_face_on_boundary(key) }) plotter.show()
if not mesh.is_vertex_on_boundary(n): previous = index index = n break while True: path.append(index) if mesh.is_vertex_on_boundary(index): break neighbors = mesh.vertex_neighbors(index, ordered=True) i = neighbors.index(previous) previous = index index = neighbors[i + 2] return path # Test start = 3 path = TraverseMesh(mesh, start) print(path) plotter = MeshPlotter(mesh, figsize=(4, 4)) plotter.draw_vertices(radius=0.4, text='key', keys=path, facecolor=(100, 100, 0)) plotter.draw_edges() plotter.draw_faces() plotter.show()
Returns ------- mesh The joined and welded mesh. """ return mesh_weld(meshes_join(meshes, cls=cls), precision=precision) # ============================================================================== # Main # ============================================================================== if __name__ == "__main__": from compas.datastructures import Mesh from compas.datastructures import mesh_weld from compas_plotters import MeshPlotter vertices = [[0, 0, 0], [0.04, 0, 0], [1.0, 0, 0], [1.0, 1.0, 0], [0, 1.0, 0]] faces = [[0, 1, 2, 3, 4]] mesh = Mesh.from_vertices_and_faces(vertices, faces) mesh = mesh_weld(mesh, precision='1f') plotter = MeshPlotter(mesh, figsize=(10, 7)) plotter.draw_vertices(text={key: "{:.3f}".format(mesh.vertex[key]['x']) for key in mesh.vertices()}, radius=0.03) plotter.draw_edges() plotter.show()
mesh = unrolled_SOUTH[0] bbox = oriented_bounding_box_xy_numpy(mesh.get_vertices_attributes('xyz')) points = [] lines = [] for index, (x, y) in enumerate(bbox[0]): point = Point(x, y, 0) points.append({ 'pos': point, 'radius': 0.01, }) for u, v in pairwise(bbox[0] + bbox[0][:1]): lines.append({'start': u, 'end': v, 'width': 0.1}) plotter.mesh = mesh plotter.draw_vertices(keys=mesh.attributes['corners'], radius=0.05, text='key') plotter.draw_faces() plotter.draw_points(points) plotter.draw_lines(lines) coldjoint = list(mesh.edges_where({'flap': 'coldjoint'})) boundary = list(mesh.edges_where({'flap': 'boundary'})) edges = coldjoint + boundary plotter.draw_edges(keys=edges, color={key: '#ff0000' for key in boundary}) plotter.show() # ============================================================================== # Export # ==============================================================================
HERE = os.path.dirname(__file__) DATA = os.path.join(HERE, 'data') FILE = os.path.join(DATA, 'faces.obj') mesh = Mesh.from_obj(FILE) # set random boundary vertex as start deg3_vertices = [] for key in mesh.vertices(): if mesh.vertex_degree(key) == 3: deg3_vertices.append(key) start_vertex = random.choice(deg3_vertices) # compute straight path path = get_straight_path(mesh, start_vertex) # visualize mesh, vertices and path plotter = MeshPlotter(mesh, figsize=(16, 10)) plotter.draw_faces() plotter.draw_edges() plotter.draw_vertices(text='key', radius=0.15) plotter.draw_vertices(facecolor=(0, 255, 255), text='key', keys=path[:1], radius=0.2) plotter.draw_vertices(facecolor=(255, 0, 255), text='key', keys=path[1:], radius=0.2) plotter.show()
# boundary.append(boundary_edges[boundary[-1]]) # boundary_edges.pop(boundary[-2]) # boundaries.append(boundary[: -1]) # return boundaries # ============================================================================== # Main # ============================================================================== if __name__ == '__main__': import compas from compas.datastructures import Network from compas_plotters import MeshPlotter network = Network.from_obj(compas.get('lines.obj')) lines = network.to_lines() mesh = BaseMesh.from_lines(lines, delete_boundary_face=False) mesh.summary() plotter = MeshPlotter(mesh, figsize=(8, 5)) plotter.draw_vertices() plotter.draw_faces() plotter.draw_edges() plotter.show()
def plot(self): from compas_plotters import MeshPlotter plotter = MeshPlotter(self, figsize=(12, 8), tight=True) plotter.draw_vertices(radius=0.05) plotter.draw_edges() plotter.show()
import compas from compas.datastructures import Mesh from compas_plotters import MeshPlotter mesh = Mesh.from_obj(compas.get('faces.obj')) plotter = MeshPlotter(mesh, figsize=(12, 7.5)) plotter.draw_vertices(text={ vertex: str(mesh.vertex_degree(vertex)) for vertex in mesh.vertices() }, radius=0.2) plotter.draw_faces() plotter.show()
E = mesh.get_edges_attribute('E') radius = mesh.get_edges_attribute('radius') lines = [] for u, v in mesh.edges(): lines.append({ 'start': mesh.vertex_coordinates(u, 'xy'), 'end': mesh.vertex_coordinates(v, 'xy'), 'color': '#cccccc', 'width': 0.5 }) plotter = MeshPlotter(mesh, figsize=(10, 7), fontsize=6) plotter.draw_lines(lines) plotter.draw_vertices(facecolor={key: '#000000' for key in mesh.vertices_where({'is_fixed': True})}) plotter.draw_edges() plotter.update(pause=1.0) def callback(k, xyz, crits, args): print(k) plotter.update_vertices() plotter.update_edges() plotter.update(pause=0.001) for key, attr in mesh.vertices(True): index = k_i[key] attr['x'] = xyz[index, 0] attr['y'] = xyz[index, 1]
# ============================================================================== # Visualize # ============================================================================== edges = list(cablenet.edges_where({'is_edge': True})) stress = [cablenet.stress(key) for key in edges] cmap = Colormap(stress, 'rgb') edgecolor = {key: cmap(s) for key, s in zip(edges, stress)} utilization = [cablenet.stress(key) / cablenet.edge_attribute(key, 'yield') for key in edges] cmap = Colormap(utilization, 'red') edgecolor = {key: cmap(u) for key, u in zip(edges, utilization)} print(min(utilization)) print(max(utilization)) plotter = MeshPlotter(cablenet, figsize=(16, 9)) plotter.draw_vertices(radius=0.05, facecolor={key: (0.0, 0.0, 0.0) for key in cablenet.vertices_where({'is_anchor': True})}) plotter.draw_edges(width=2.0, color=edgecolor, keys=edges) plotter.save(FILE_P, dpi=150) # plotter.show() # ============================================================================== # Export # ============================================================================== cablenet.to_json(FILE_O)
arrows.append({ 'start': cablenet.vertex_coordinates(u), 'end': cablenet.vertex_coordinates(v), 'color': (0.0, 0.0, 0.0) }) facecolor = { key: i_to_blue(index / len(faces)) for index, key in enumerate(faces) } plotter = MeshPlotter(cablenet, figsize=(10, 7)) plotter.defaults['vertex.radius'] = 0.04 plotter.defaults['edge.width'] = 0.5 plotter.draw_edges(width={key: 3.0 for edges in cables for key in edges}) plotter.draw_vertices(radius={key: 0.06 for key in chain}, facecolor=vertexcolor) plotter.draw_arrows2(arrows) plotter.draw_faces(facecolor=facecolor, keys=faces, text={key: str(index) for index, key in enumerate(faces)}) plotter.show() # ============================================================================== # Export # ============================================================================== cablenet.to_json(FILE_O)
from compas_plotters import MeshPlotter mesh = Mesh.from_obj(compas.get('hypar.obj')) target = mesh.copy() points = mesh.vertices_attributes('xyz') points[:] = [[x, y, 0] for x, y, z in points] mesh_quads_to_triangles(target) pulled = trimesh_pull_points_numpy(target, points) plotter = MeshPlotter(mesh, figsize=(8, 5)) plotter.defaults['vertex.fontsize'] = 6 plotter.draw_vertices(text={ key: "{:.1f}".format(attr['z']) for key, attr in mesh.vertices(True) }, radius=0.08) plotter.draw_faces() plotter.draw_points([{ 'pos': [x, y, z], 'text': "{:.1f}".format(z), 'radius': 0.08, 'facecolor': '#00ff00', 'fontsize': 6 } for x, y, z in pulled]) plotter.show()
q = mesh.get_edges_attribute('q') fixed = mesh.vertices_where({'is_anchor': True}) fixed = [k_i[k] for k in fixed] edges = [(k_i[u], k_i[v]) for u, v in mesh.edges()] # compute equilibrium # update the mesh geometry xyz, q, f, l, r = fd_numpy(xyz, edges, fixed, q, loads) for key, attr in mesh.vertices(True): index = k_i[key] attr['x'] = xyz[index, 0] attr['y'] = xyz[index, 1] attr['z'] = xyz[index, 2] # visualisae the result # color the vertices according to their elevation plotter = MeshPlotter(mesh) zmax = max(mesh.get_vertices_attribute('z')) plotter.draw_vertices(facecolor={ key: i_to_black(attr['z'] / zmax) for key, attr in mesh.vertices(True) }) plotter.draw_faces() plotter.draw_edges() plotter.show()