def display_smoothing_constraints(mesh, constraints): """Display current state of constraints on the vertices of a mesh to smooth. Parameters ---------- mesh : Mesh The mesh to apply the constraints to for smoothing. constraints : dict A dictionary of mesh constraints for smoothing as vertex keys pointing to point, curve or surface objects. Returns ------- guid Guid of Rhino points coloured according to the type of constraint applied. """ #color = {vkey: (255, 0, 0) if vkey in constraints and rs.ObjectType(constraints[vkey]) == 1 # else (0, 255, 0) if vkey in constraints and rs.ObjectType(constraints[vkey]) == 4 # else (0, 0, 255) if vkey in constraints and rs.ObjectType(constraints[vkey]) == 8 # else (0, 0, 0) for vkey in mesh.vertices()} guids_index = {guid: i for i, guid in enumerate(list(set(constraints.values())))} n = len(guids_index.keys()) color = {} for vkey in mesh.vertices(): if vkey in constraints: k = float(guids_index[constraints[vkey]]) / float((n - 1)) color[vkey] = (int(255.0 * k), int(255.0 * (1.0 - k)), 0) else: color[vkey] = (0, 0, 0) artist = MeshArtist(mesh) return artist.draw_vertices(color = color)
def mesh_draw_vertices(mesh, keys=None, color=None, layer=None, clear_layer=False, clear_vertices=False, redraw=True): """Draw a selection of vertices of the mesh. Parameters ---------- mesh : compas.datastructures.Mesh A mesh object. keys : list (None) A list of vertex keys identifying which vertices to draw. Default is to draw all vertices. color : str, tuple, dict (None) The color specififcation for the vertices. Colors should be specified in the form of a string (hex colors) or as a tuple of RGB components. To apply the same color to all vertices, provide a single color specification. Individual colors can be assigned using a dictionary of key-color pairs. Missing keys will be assigned the default vertex color (``self.defaults['vertex.color']``). Default is use the color of the parent layer. layer : str (None) The layer in which the vertices are drawn. Default is to draw in the current layer. clear_layer : bool (False) Clear the drawing layer. redraw : bool (True) Redraw the view after adding the vertices. Notes ----- The vertices are named using the following template: ``"{}.vertex.{}".format(self.mesh.attributes['name'], key)``. This name is used afterwards to identify vertices of the meshin the Rhino model. Examples -------- >>> """ artist = MeshArtist(mesh) artist.layer = layer if clear_layer: artist.clear_layer() if clear_vertices: artist.clear_vertices() guids = artist.draw_vertices(color=color) if redraw: artist.redraw() return guids
vec_leaf = Vector.from_start_end(mesh.vertex_coordinates(key), pt) vec_leaf.unitize() vec_leaf.scale(leaf_width) pt = add_vectors(mesh.vertex_coordinates(key), vec_leaf) descdent_tree[key][nbr].update({'lp': current_key}) mesh.add_vertex(current_key) mesh.vertex[current_key].update({'x': pt[0], 'y': pt[1], 'z': pt[2]}) current_key += 1 for key in convex_hull_mesh.vertices(): nbrs = convex_hull_mesh.vertex_neighbors(key, ordered=True) v_keys = nbrs + [nbrs[0]] for a, b in pairwise(v_keys): face = [ descdent_tree[key][a]['lp'], descdent_tree[key][a]['jp'], descdent_tree[key][b]['jp'], descdent_tree[key][b]['lp'] ] mesh.add_face(face) # mesh.to_json('mesh1.json', pretty=True) artist = MeshArtist(mesh) artist.draw_vertices() artist.draw_faces() # artist = MeshArtist(convex_hull_mesh) # artist.draw_faces(color=(255, 0, 0)) # artist.draw_edges(color=(255, 0, 0))
X = mesh.vertices_attributes('xyz') P = mesh.vertices_attributes(['px', 'py', 'pz']) Q = mesh.edges_attribute('q') fixed = [ vertex_index[vertex] for vertex in mesh.vertices_where({'is_anchor': True}) ] edges = [(vertex_index[u], vertex_index[v]) for u, v in mesh.edges()] # compute equilibrium X, Q, F, L, R = fd(X, edges, fixed, Q, P) # update network for vertex in mesh.vertices(): index = vertex_index[vertex] mesh.vertex_attributes(vertex, 'xyz', X[index]) # visualize result artist = MeshArtist(mesh, layer="ITA20::L5::FormFinding") artist.clear_layer() artist.draw_vertices(color={ vertex: (255, 0, 0) for vertex in mesh.vertices_where({'is_anchor': True}) }) artist.draw_edges() artist.draw_faces()
# ============================================================================== # Vertex attributes # ============================================================================== corners = ...(mesh.vertices_where({'...': 2})) high = [0, 35] mesh.set_vertices_attribute('is_fixed', True, keys=corners) mesh.set_vertices_attribute('z', ..., keys=high) # ============================================================================== # Edge attributes # ============================================================================== boundary = list(mesh.edges_on_boundary()) mesh.set_edges_attribute('q', 5.0, keys=boundary) # ============================================================================== # Visualize result # ============================================================================== artist = MeshArtist(mesh, layer="Mesh") artist.clear_layer() artist.draw_vertices( color={key: (..., 0, 0) for key in mesh.vertices_where({'...': True})}) artist.draw_edges() artist.draw_faces()
from compas_rhino.modifiers import EdgeModifier from compas_rhino.selectors import VertexSelector from compas_rhino.selectors import EdgeSelector mesh = Mesh.from_json('../cablenet.json') vertexcolor = { key: (255, 0, 0) for key in mesh.vertices_where({'is_anchor': True}) } edgecolor = {key: (255, 0, 0) for key in mesh.edges_where({'is_joint': True})} artist = MeshArtist(mesh, layer="Cablenet") artist.clear_layer() artist.draw_vertices(color=vertexcolor) artist.draw_edges(color=edgecolor) artist.redraw() # select mesh vertices # update the attributes # redraw mesh if successful while True: selected = VertexSelector.select_vertices(mesh) if not selected: break if VertexModifier.update_vertex_attributes(mesh, selected): artist.clear_layer() artist.draw_vertices(color=vertexcolor) artist.draw_edges(color=edgecolor)
def mesh_draw(mesh, layer=None, clear_layer=False, clear_vertices=False, clear_faces=False, clear_edges=False, show_faces=True, show_vertices=False, show_edges=False, vertexcolor=None, edgecolor=None, facecolor=None): """ Draw a mesh object in Rhino. Parameters ---------- mesh : compas.datastructures.Mesh The mesh object. layer : str (None) The layer to draw in. Default is to draw in the current layer. clear_layer : bool (False) Clear the drawing layer. show_faces : bool (True) Draw the faces. show_vertices : bool (False) Draw the vertices. show_edges : bool (False) Draw the edges. vertexcolor : str, tuple, list, dict (None) The vertex color specification. Default is to use the color of the parent layer. edgecolor : str, tuple, list, dict (None) The edge color specification. Default is to use the color of the parent layer. facecolor : str, tuple, list, dict (None) The face color specification. Default is to use the color of the parent layer. Notes ----- Colors can be specifiedin different ways: * str: A hexadecimal color that will be applied to all elements subject to the specification. * tuple, list: RGB color that will be applied to all elements subject to the specification. * dict: RGB or hex color dict with a specification for some or all of the related elements. Notes ----- RGB colors specified as values between 0 and 255, should be integers. RGB colors specified as values between 0.0 and 1.0, should be floats. """ artist = MeshArtist(mesh) artist.layer = layer if clear_layer: artist.clear_layer() if clear_vertices: artist.clear_vertices() if clear_edges: artist.clear_edges() if clear_faces: artist.clear_faces() if show_faces: artist.draw_faces(color=facecolor) if show_edges: artist.draw_edges(color=edgecolor) if show_vertices: artist.draw_vertices(color=vertexcolor) artist.redraw()
# Fofin run # ============================================================================== xyz, q, f, l, r = fd_numpy(xyz, edges, fixed, q, loads) # ============================================================================== # Fofin update # ============================================================================== for key, attr in mesh.vertices(True): attr['x'] = xyz[key][0] attr['y'] = xyz[key][1] attr['z'] = xyz[key][2] attr['...'] = r[key][...] attr['...'] = r[key][...] attr['...'] = r[key][...] for ..., (u, v, attr) in enumerate(mesh.edges(True)): attr['f'] = ...[index][0] # ============================================================================== # Visualize result # ============================================================================== artist = MeshArtist(mesh, layer="Mesh") artist.clear_layer() artist.draw_vertices( color={key: (255, 0, 0) for key in mesh.vertices_where({'is_fixed': True})}) artist.draw_edges() artist.draw_faces()
def draw_vertices(self, **kwattr): artist = MeshArtist(self) artist.draw_vertices(**kwattr)
import os import compas from compas.datastructures import Mesh from compas_rhino.artists import MeshArtist HERE = os.path.dirname(__file__) DATA = os.path.join(HERE, 'data') FILE = os.path.join(DATA, 'faces.obj') mesh = Mesh.from_obj(FILE) artist = MeshArtist(mesh, layer="Mesh") artist.draw_vertices( color={key: (255, 0, 0) for key in mesh.vertices_on_boundary()}) artist.draw_vertexlabels( text={key: str(mesh.vertex_degree(key)) for key in mesh.vertices()}) artist.draw_edges(keys=list(mesh.edges_on_boundary()), color=(255, 0, 0)) artist.draw_faces( color={ key: (150, 255, 150) for key in mesh.faces() if not mesh.is_face_on_boundary(key) })
unload_modules('shapes') # 2D Subdivision vertices = [[0.5, 0.0, 0.0], [0.0, 1.0, 0.0], [-0.5, 0.0, 0.0], [1.0, 1.0, 0.0]] faces = [[0, 1, 2], [1, 0, 3]] my_mesh = Mesh.from_vertices_and_faces(vertices, faces) artist = MeshArtist(my_mesh, layer="00_my_first mesh") artist.clear_layer() artist.draw_vertices() artist.draw_faces() artist.draw_edges() artist.draw_vertexlabels() artist.draw_facelabels() artist.draw_edgelabels() # iterate through the mesh # for key,attr in my_mesh.vertices(True): # print (key, attr['x'], attr['y'], attr['z']) # for key in my_mesh.faces(): # print(key) # for key in my_mesh.edges(): # print(key)
while True: path.append(current_key) if my_mesh.is_vertex_on_boundary(current_key): break neighbours = my_mesh.vertex_neighbors(current_key, ordered=True) i = neighbours.index(previous_key) previous_key, current_key = current_key, neighbours[i - 2] print(path) #visualize artist = MeshArtist(my_mesh) #artist.draw_vertices( #color={key: (255, 0, 0) for key in boundary_key}) artist.draw_vertices(path, color={key: (255, 0, 0) for key in [start_key]}) #artist.draw_vertices(neighbours) artist.draw_edges() artist.draw_faces() #artist.draw_vertexlabels( #text={key: str(key) for key in boundary_key},color={start_key: (255, 0, 0)}) #artist.draw_vertexlabels( #text={key: str(key) for key in [start_key]},color={start_key: (255, 0, 0)}) artist.draw_vertexlabels(text={key: str(key) for key in path}, color={key: (255, 0, 0) for key in path})
HERE = os.path.dirname(__file__) DATA = os.path.join(HERE, '../data') FILE = os.path.join(DATA, 'mesh.json') mesh = Mesh.from_json(FILE) artist = MeshArtist(mesh, layer="HiLo::Mesh") artist.clear_layer() boundary = mesh.vertices_on_boundary() special5 = list(mesh.vertices_where({'vertex_degree': 5})) special6 = list(mesh.vertices_where({'vertex_degree': 6})) color = {vertex: (255, 0, 0) for vertex in boundary} color.update({vertex: (0, 255, 0) for vertex in special5}) color.update({vertex: (0, 0, 255) for vertex in special6}) artist.draw_vertices( vertices=boundary + special5 + special6, color=color ) color = {face: (255, 0, 0) for face in mesh.faces_on_boundary()} color.update({face: (0, 255, 0) for vertex in special5 for face in mesh.vertex_faces(vertex)}) color.update({face: (0, 0, 255) for vertex in special6 for face in mesh.vertex_faces(vertex)}) artist.draw_faces( color=color )
mesh_quads_to_triangles(mesh) key_index = mesh.key_index() V = mesh.vertices_attributes('xyz') F = [[key_index[key] for key in mesh.face_vertices(fkey)] for fkey in mesh.faces()] # ============================================================================== # Geodistance # ============================================================================== root = mesh.get_any_vertex() # D = igl.trimesh_geodistance_exact(V, F, key_index[root]) D = igl.trimesh_geodistance_heat(V, F, key_index[root]) # ============================================================================== # Visualization # ============================================================================== cmap = Colormap(D, 'red') artist = MeshArtist(mesh, layer="IGL::GeoDistance") artist.clear_layer() artist.draw_mesh() artist.draw_vertices( color={key: cmap(d) for key, d in zip(mesh.vertices(), D)}) artist.draw_vertexlabels(text={root: "root"}, color={0: (255, 255, 255)})