def from_meshgrid(cls, dx, nx, dy=None, ny=None): """Create a mesh from faces and vertices on a regular grid. Parameters ---------- dx : float The size of the grid in the X direction. nx : int The number of faces in the X direction. dy : float, optional The size of the grid in the Y direction, if different form X. ny : int, optional The number of faces in the Y direction, if different from Y. Returns ------- Mesh A mesh object. """ dy = dy or dx ny = ny or nx vertices = [[x, y, 0.0] for x, y in product(linspace(0, dx, nx + 1), linspace(0, dy, ny + 1))] faces = [[ i * (ny + 1) + j, (i + 1) * (ny + 1) + j, (i + 1) * (ny + 1) + j + 1, i * (ny + 1) + j + 1 ] for i, j in product(range(nx), range(ny))] return cls.from_vertices_and_faces(vertices, faces)
def from_three_colors(cls, c1, c2, c3): """Construct a color map from three colors. Parameters ---------- c1 : :class:`compas.colors.Color` The first color. c2 : :class:`compas.colors.Color` The second color. c3 : :class:`compas.colors.Color` The third color. Returns ------- :class:`compas.colors.ColorMap` """ colors = [] for i in linspace(0, 1.0, 128): r = c1[0] * (1 - i) + c2[0] * i g = c1[1] * (1 - i) + c2[1] * i b = c1[2] * (1 - i) + c2[2] * i colors.append(Color(r, g, b)) for i in linspace(0, 1.0, 128): r = c2[0] * (1 - i) + c3[0] * i g = c2[1] * (1 - i) + c3[1] * i b = c2[2] * (1 - i) + c3[2] * i colors.append(Color(r, g, b)) return cls(colors)
def from_meshgrid(cls, dx, nx, dy=None, ny=None): """Create a mesh from faces and vertices on a regular grid. Parameters ---------- dx : float The size of the grid in the X direction. nx : int The number of faces in the X direction. dy : float, optional The size of the grid in the Y direction. Defaults to the value of `dx`. ny : int, optional The number of faces in the Y direction. Defaults to the value of `nx`. Returns ------- :class:`compas.datastructures.Mesh` A mesh object. """ dy = dy or dx ny = ny or nx vertices = [[x, y, 0.0] for x, y in product(linspace(0, dx, nx + 1), linspace(0, dy, ny + 1))] faces = [[ i * (ny + 1) + j, (i + 1) * (ny + 1) + j, (i + 1) * (ny + 1) + j + 1, i * (ny + 1) + j + 1 ] for i, j in product(range(nx), range(ny))] return cls.from_vertices_and_faces(vertices, faces)
def from_rgb(cls): """Construct a color map from the complete rgb color space. Returns ------- :class:`compas.colors.Color` """ colors = [] for i in linspace(0, 1.0, 256): colors.append(Color.from_i(i)) return cls(colors)
def from_meshgrid(cls, nu=10, nv=10): """Construct a NURBS surface from a mesh grid. Parameters ---------- nu : int, optional Number of control points in the U direction. nv : int, optional Number of control points in the V direction. Returns ------- :class:`compas.geometry.NurbsSurface` """ UU, VV = meshgrid(linspace(0, nu, nu + 1), linspace(0, nv, nv + 1)) points = [] for U, V in zip(UU, VV): row = [] for u, v in zip(U, V): row.append(Point(u, v, 0.0)) points.append(row) return cls.from_points(points=points)
def v_space(self, n=10): """Compute evenly spaced parameters over the surface domain in the V direction. Parameters ---------- n : int, optional The number of parameters. Returns ------- list of float """ vmin, vmax = self.v_domain return linspace(vmin, vmax, n)
def from_two_colors(cls, c1, c2, diverging=False): """Create a color map from two colors. Parameters ---------- c1 : :class:`compas.colors.Color` The first color. c2 : :class:`compas.colors.Color` The second color. diverging : bool, optional If True, use white as transition color in the middle. Returns ------- :class:`compas.colors.ColorMap` """ colors = [] if diverging: for i in linspace(0, 1.0, 128): r = c1[0] * (1 - i) + 1.0 * i g = c1[1] * (1 - i) + 1.0 * i b = c1[2] * (1 - i) + 1.0 * i colors.append(Color(r, g, b)) for i in linspace(0, 1.0, 128): r = 1.0 * (1 - i) + c2[0] * i g = 1.0 * (1 - i) + c2[1] * i b = 1.0 * (1 - i) + c2[2] * i colors.append(Color(r, g, b)) else: for i in linspace(0, 1, 256): r = c1[0] * (1 - i) + c2[0] * i g = c1[1] * (1 - i) + c2[1] * i b = c1[2] * (1 - i) + c2[2] * i colors.append(Color(r, g, b)) return cls(colors)
def u_space(self, n=10): """Compute evenly spaced parameters over the surface domain in the U direction. Parameters ---------- n : int, optional The number of parameters. Returns ------- list[float] """ umin, umax = self.u_domain return linspace(umin, umax, n)
def space(self, n=10): """Compute evenly spaced parameters over the curve domain. Parameters ---------- n : int, optional The number of values in the parameter space. Returns ------- list[float] """ start, end = self.domain return linspace(start, end, n)
artist.draw() # cutting process compas_rhino.rs.Redraw() for i, j in pairwise(range(len(left_poly.points))): left_start = left_poly.points[i] left_stop = left_poly.points[j] left_vector = left_stop - left_start right_start = right_poly.points[i] right_stop = right_poly.points[j] right_vector = right_stop - right_start for i in linspace(0, 1, 50): a = left_start + left_vector * i b = right_start + right_vector * i line = Line(a, b) artist = LineArtist(line, color=(255, 255, 255), layer="ITA20::HotWire::CutLines") artist.clear_layer() artist.draw() compas_rhino.rs.Redraw() compas_rhino.wait() polygon = Polygon([left_start, right_start, right_stop, left_stop]) artist = PolygonArtist(polygon, color=(255, 255, 255), layer="ITA20::HotWire::CutPlane")
controlpoints = [ Point(0, 0, 0), Point(4, 2.5, 0), Point(6, -2.5, 0), Point(10, 0, 0) ] controlpoly = Polyline(controlpoints) curve = Bezier(controlpoints) poly = Polyline(curve.locus()) poly1 = Polyline(offset_polyline(poly, +0.15)) poly2 = Polyline(offset_polyline(poly, -0.15)) points = [poly.point(t) for t in linspace(0, 1, 20)] tangents = [(c - a).unitized() for a, b, c in window(points, 3) if a and c] normals = [Vector(0, 0, 1).cross(t) for t in tangents] lines = [[point, point + normal] for point, normal in zip(points[1:-1], normals)] points1 = [intersection_line_polyline(line, poly1) for line in lines] points2 = [intersection_line_polyline(line, poly2) for line in lines] frames = [] for a, b in pairwise(points[1:-1]): p = (a + b) * 0.5 t = (b - a).unitized() n = Vector(0, 0, 1).cross(t) frame = Frame(p, t, n) frames.append(frame)
Joint.CONTINUOUS, wrist, hand, origin=Frame(Point(0, 0, 0), Vector(1, 0, 0), Vector(0, 1, 0)), axis=Vector(0, 0, 1)) # ============================================================================== # States # ============================================================================== names = robot.get_configurable_joint_names() joints = list(robot.iter_joints()) motions = [] space = list(linspace(0, 1, 100)) for joint in joints: if joint.limit: lower = joint.limit.lower upper = joint.limit.upper else: lower = 0 upper = 2 * math.pi motion = remap_values(space, target_min=lower, target_max=upper) motions.append(motion) for values in zip(*motions): state = dict(zip(names, values)) transformations = robot.compute_transformations(state)
# ============================================================================== # From frame # ============================================================================== base_joint = robot.get_joint_by_name('base-shoulder') frame_from = base_joint.origin # ============================================================================== # State # ============================================================================== names = robot.get_configurable_joint_names() values = [+0.25 * math.pi, -0.25 * math.pi, +0.5 * math.pi, 0, +0.25 * math.pi] state = dict(zip(names, values)) for i in linspace(0, 10, 100): frame_to = Frame([i * 0.3, i * 0.3, 0], [1, 0, 0], [0, 1, 0]) T = Transformation.from_frame_to_frame(frame_from, frame_to) transformations = robot.compute_transformations(state, parent_transformation=T) frames = [] axes = [] for joint in robot.iter_joints(): frame = joint.origin.transformed(transformations[joint.name]) frame.name = joint.name frames.append(frame) axis = joint.axis.transformed(transformations[joint.name])
from compas_plotters import GeometryPlotter controlpoints = [ Point(0, 0, 0), Point(4, 2.5, 0), Point(6, -2.5, 0), Point(10, 0, 0) ] controlpoly = Polyline(controlpoints) curve = Bezier(controlpoints) poly = Polyline(curve.locus()) poly1 = Polyline(offset_polyline(poly, +0.15)) poly2 = Polyline(offset_polyline(poly, -0.15)) points = [curve.point(t) for t in linspace(0, 1, 20)] tangents = [curve.tangent(t) for t in linspace(0, 1, 20)] normals = [Vector(0, 0, 1).cross(t) for t in tangents] # ============================================================================== # Visualization # ============================================================================== plotter = GeometryPlotter(figsize=(16, 9)) plotter.add(controlpoly, linestyle='dotted', linewidth=1.0, color=(0.5, 0.5, 0.5)) for point in controlpoints: plotter.add(point, edgecolor=(1.0, 0.0, 0.0))
artist = PolylineArtist(right_poly, color=(0, 255, 0), layer="ITA20::HotWire::RightCut") artist.draw() compas_rhino.rs.Redraw() for i, j in pairwise(range(len(left_poly.points))): left_start = left_poly.points[i] left_stop = left_poly.points[j] left_vector = left_stop - left_start right_start = right_poly.points[i] right_stop = right_poly.points[j] right_vector = right_stop - right_start for n in linspace(0, 1, 50): a = left_start + left_vector * n b = right_start + right_vector * n line = Line(a, b) artist = LineArtist(line, color=(255, 255, 255), layer="ITA20::HotWire::CutLines") artist.clear_layer() artist.draw() compas_rhino.rs.Redraw() compas_rhino.wait() a = left_blank[i] b = right_blank[i] c = right_blank[j] d = left_blank[j] polygon = Polygon([a, b, c, d])
from itertools import combinations from compas.datastructures import Network from compas.utilities import linspace, meshgrid from compas_rhino.artists import NetworkArtist X, Y = meshgrid(linspace(0, 10, 10), linspace(0, 5, 5)) points = [] for z in linspace(0, 3, 3): for xs, ys in zip(X, Y): for x, y in zip(xs, ys): points.append([x, y, z]) network = Network() for point in points: network.add_node(x=point[0], y=point[1], z=point[2]) for a, b in combinations(network.nodes(), 2): if network.node_attribute(a, 'z') != network.node_attribute(b, 'z'): network.add_edge(a, b) artist = NetworkArtist(network, layer="ITA20::Network") artist.clear_layer() artist.draw_nodes() artist.draw_edges()
def space(self, n=10): """Compute evenly spaced parameters over the curve domain.""" start, end = self.domain return linspace(start, end, n)
from compas.geometry import Point from compas.geometry import Polyline from compas.utilities import meshgrid, linspace from compas.geometry import NurbsSurface from compas.artists import Artist UU, VV = meshgrid(linspace(0, 8, 9), linspace(0, 5, 6)) Z = 0.5 points = [] for i, (U, V) in enumerate(zip(UU, VV)): row = [] for j, (u, v) in enumerate(zip(U, V)): if i == 0 or i == 5 or j == 0 or j == 8: z = 0.0 elif i < 2 or i > 3: z = -1.0 else: if j < 2 or j > 6: z = -1.0 else: z = Z row.append(Point(u, v, z)) points.append(row) surface = NurbsSurface.from_points(points=points) # ============================================================================== # Visualisation # ==============================================================================
# ============================================================================== # Curves # ============================================================================== Z = Vector(0, 0, 1) controlpoints = [Point(0, 0, 0), Point(4, 2.5, 0), Point(6, -2.5, 0), Point(10, 0, 0)] controlpoly = Polyline(controlpoints) curve = Bezier(controlpoints) poly0 = Polyline(curve.locus()) poly1 = Polyline(offset_polyline(poly0, +0.15)) poly2 = Polyline(offset_polyline(poly0, -0.15)) points0 = [poly0.point(t) for t in linspace(0, 1, 20)] tangents0 = [(c - a).unitized() for a, b, c in window(points0, 3) if a and c] normals0 = [Z.cross(t) for t in tangents0] lines = [[point, point + normal] for point, normal in zip(points0[1:-1], normals0)] points1 = [intersection_line_polyline(line, poly1) for line in lines] points2 = [intersection_line_polyline(line, poly2) for line in lines] # ============================================================================== # Blocks # ============================================================================== frames = [] blocks = [] polygons = []