"""Geometry for the assignment renders.""" import math import mcubes import torch from pytorch3d.renderer import TexturesVertex from pytorch3d.structures import Meshes, Pointclouds, join_meshes_as_scene from pytorch3d.utils import ico_sphere BLUE = (0.1, 0.3, 0.9) ORANGE = (1.0, 0.45, 0.1) def mesh(vertices, faces, colors=(0.65, 0.7, 0.9)): vertices = torch.as_tensor(vertices, dtype=torch.float32) faces = torch.as_tensor(faces, dtype=torch.int64) colors = torch.as_tensor(colors, dtype=torch.float32).expand_as(vertices) return Meshes([vertices], [faces], textures=TexturesVertex([colors])) def gradient_colors(vertices): z = vertices[:, 2:3] alpha = (z - z.min()) / (z.max() - z.min()) return (1 - alpha) * torch.tensor(BLUE) + alpha * torch.tensor(ORANGE) def position_colors(vertices): return (vertices - vertices.min(0).values) / ( vertices.max(0).values - vertices.min(0).values ).clamp_min(1e-8) def tetrahedron(): vertices = [[1, 1, 1], [-1, -1, 1], [-1, 1, -1], [1, -1, -1]] faces = [[0, 2, 1], [0, 1, 3], [0, 3, 2], [1, 2, 3]] return mesh(torch.tensor(vertices) * 0.65, faces) def cube(): vertices = [[-1, -1, -1], [1, -1, -1], [1, 1, -1], [-1, 1, -1], [-1, -1, 1], [1, -1, 1], [1, 1, 1], [-1, 1, 1]] faces = [[0, 2, 1], [0, 3, 2], [4, 5, 6], [4, 6, 7], [0, 1, 5], [0, 5, 4], [3, 7, 6], [3, 6, 2], [0, 4, 7], [0, 7, 3], [1, 2, 6], [1, 6, 5]] return mesh(torch.tensor(vertices) * 0.65, faces, (0.25, 0.65, 0.55)) def parametric_points(shape, samples=100): u = torch.arange(samples) * (2 * math.pi / samples) v = (torch.arange(samples) * (2 * math.pi / samples) if shape == "torus" else torch.linspace(0, math.pi, samples)) u, v = torch.meshgrid(u, v, indexing="ij") if shape == "torus": x = (1 + 0.35 * v.cos()) * u.cos() y = 0.35 * v.sin() z = (1 + 0.35 * v.cos()) * u.sin() else: x = 0.8 * v.sin() * u.cos() y = 1.2 * v.cos() z = 0.65 * v.sin() * u.sin() return torch.stack((x, y, z), dim=-1).reshape(-1, 3) def point_cloud(points, colors=None): if colors is None: colors = position_colors(points) if colors.shape[1] == 3: colors = torch.cat((colors, torch.ones_like(colors[:, :1])), dim=1) return Pointclouds(points=[points], features=[colors]) def implicit_mesh(shape, resolution=48): axis = torch.linspace(-1.6, 1.6, resolution) x, y, z = torch.meshgrid(axis, axis, axis, indexing="ij") if shape == "torus": field = (torch.sqrt(x*x + z*z) - 1)**2 + y*y - 0.35**2 else: field = (x / 0.8)**2 + (y / 1.2)**2 + (z / 0.65)**2 - 1 vertices, faces = mcubes.marching_cubes(field.numpy(), 0) # Marching cubes returns voxel indices, including both ends of the grid. vertices = torch.tensor(vertices, dtype=torch.float32) * (3.2 / (resolution - 1)) - 1.6 # PyMCubes orders these faces toward the negative side of the field. return mesh(vertices, faces[:, ::-1].copy(), position_colors(vertices)) def sample_surface(source, count): vertices, faces = source.get_mesh_verts_faces(0) triangles = vertices[faces] areas = torch.linalg.vector_norm(torch.cross( triangles[:, 1] - triangles[:, 0], triangles[:, 2] - triangles[:, 0], dim=1 ), dim=1) / 2 chosen = torch.multinomial(areas, count, replacement=True) u, v = torch.rand(count, 2).unbind(1) # sqrt(u) gives uniform area density inside the chosen triangle. root = u.sqrt() weights = torch.stack((1 - root, root * (1 - v), root * v), dim=1) points = (triangles[chosen] * weights[..., None]).sum(1) colors = source.textures.verts_features_list()[0][faces[chosen]] colors = (colors * weights[..., None]).sum(1) return point_cloud(points, colors) def snowman(): parts = [] def ball(center, radius, color, level=2): sphere = ico_sphere(level) vertices = sphere.verts_packed() * radius + torch.tensor(center) parts.append(mesh(vertices, sphere.faces_packed(), color)) def cylinder(radius, bottom, top): angles = torch.arange(32) * (2 * math.pi / 32) ring = torch.stack((radius * angles.cos(), torch.zeros(32), radius * angles.sin()), 1) vertices = torch.cat((ring + torch.tensor([0, bottom, 0]), ring + torch.tensor([0, top, 0]), torch.tensor([[0, bottom, 0], [0, top, 0]]))) faces = [] for i in range(32): j = (i + 1) % 32 faces += [[i, i + 32, j], [j, i + 32, j + 32], [64, i, j], [65, j + 32, i + 32]] parts.append(mesh(vertices, faces, (0.08, 0.09, 0.12))) white, black = (0.88, 0.93, 1.0), (0.06, 0.06, 0.08) ball((0, -0.75, 0), 0.78, white) ball((0, 0.35, 0), 0.58, white) ball((0, 1.25, 0), 0.42, white) for y in (-0.9, -0.5): ball((0, y, -math.sqrt(0.78**2 - (y + 0.75)**2)), 0.065, black, 1) for y in (0.2, 0.55): ball((0, y, -math.sqrt(0.58**2 - (y - 0.35)**2)), 0.055, black, 1) for x in (-0.13, 0.13): ball((x, 1.34, -0.38), 0.045, black, 1) cylinder(0.48, 1.6, 1.68) cylinder(0.3, 1.68, 2.04) angles = torch.arange(16) * (2 * math.pi / 16) ring = torch.stack((0.09 * angles.cos(), 1.23 + 0.09 * angles.sin(), torch.full((16,), -0.39)), 1) vertices = torch.cat((ring, torch.tensor([[0, 1.23, -0.87], [0, 1.23, -0.39]]))) faces = [[i, 16, (i + 1) % 16] for i in range(16)] faces += [[17, i, (i + 1) % 16] for i in range(16)] parts.append(mesh(vertices, faces, ORANGE)) return join_meshes_as_scene(parts)