|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# ******************************************************* |
| 3 | +# ____ _ _ |
| 4 | +# / ___|___ _ __ ___ ___| |_ _ __ ___ | | |
| 5 | +# | | / _ \| '_ ` _ \ / _ \ __| | '_ ` _ \| | |
| 6 | +# | |__| (_) | | | | | | __/ |_ _| | | | | | | |
| 7 | +# \____\___/|_| |_| |_|\___|\__(_)_| |_| |_|_| |
| 8 | +# |
| 9 | +# Sign up for free at https://www.comet.com |
| 10 | +# Copyright (C) 2015-2023 Comet ML INC |
| 11 | +# ******************************************************* |
| 12 | + |
| 13 | +import math |
| 14 | + |
| 15 | + |
| 16 | +def identity(): |
| 17 | + """ |
| 18 | + Return matrix for identity (no transforms). |
| 19 | + """ |
| 20 | + return [ |
| 21 | + [1, 0, 0, 0], |
| 22 | + [0, 1, 0, 0], |
| 23 | + [0, 0, 1, 0], |
| 24 | + [0, 0, 0, 1], |
| 25 | + ] |
| 26 | + |
| 27 | + |
| 28 | +def rotate_x(angle): |
| 29 | + """ |
| 30 | + Return transform matrix for rotation around x axis. |
| 31 | + """ |
| 32 | + radians = angle * math.pi / 180 |
| 33 | + return [ |
| 34 | + [1, 0, 0, 0], |
| 35 | + [0, math.cos(radians), -math.sin(radians), 0], |
| 36 | + [0, math.sin(radians), math.cos(radians), 0], |
| 37 | + [0, 0, 0, 1], |
| 38 | + ] |
| 39 | + |
| 40 | + |
| 41 | +def rotate_y(angle): |
| 42 | + """ |
| 43 | + Return transform matrix for rotation around y axis. |
| 44 | + """ |
| 45 | + radians = angle * math.pi / 180 |
| 46 | + return [ |
| 47 | + [math.cos(radians), 0, math.sin(radians), 0], |
| 48 | + [0, 1, 0, 0], |
| 49 | + [-math.sin(radians), 0, math.cos(radians), 0], |
| 50 | + [0, 0, 0, 1], |
| 51 | + ] |
| 52 | + |
| 53 | + |
| 54 | +def rotate_z(angle): |
| 55 | + """ |
| 56 | + Return transform matrix for rotation around z axis. |
| 57 | + """ |
| 58 | + radians = angle * math.pi / 180 |
| 59 | + return [ |
| 60 | + [math.cos(radians), -math.sin(radians), 0, 0], |
| 61 | + [math.sin(radians), math.cos(radians), 0, 0], |
| 62 | + [0, 0, 1, 0], |
| 63 | + [0, 0, 0, 1], |
| 64 | + ] |
| 65 | + |
| 66 | + |
| 67 | +def translate_xyz(x, y, z): |
| 68 | + """ |
| 69 | + Return transform matrix for translation (linear moving). |
| 70 | + """ |
| 71 | + return [ |
| 72 | + [1, 0, 0, x], |
| 73 | + [0, 1, 0, y], |
| 74 | + [0, 0, 1, z], |
| 75 | + [0, 0, 0, 1], |
| 76 | + ] |
| 77 | + |
| 78 | + |
| 79 | +def scale_xyz(x, y, z): |
| 80 | + """ |
| 81 | + Return transform matrix for scaling. |
| 82 | + """ |
| 83 | + return [ |
| 84 | + [x, 0, 0, 0], |
| 85 | + [0, y, 0, 0], |
| 86 | + [0, 0, z, 0], |
| 87 | + [0, 0, 0, 1], |
| 88 | + ] |
| 89 | + |
| 90 | + |
| 91 | +def matmul(a, b): |
| 92 | + """ |
| 93 | + Multiply two matrices. Written in Pure Python |
| 94 | + to avoid dependency on numpy. |
| 95 | + """ |
| 96 | + c = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] |
| 97 | + for x in range(4): |
| 98 | + for y in range(4): |
| 99 | + acc = 0 |
| 100 | + for n in range(4): |
| 101 | + acc += a[n][x] * b[y][n] |
| 102 | + c[y][x] = acc |
| 103 | + return c |
| 104 | + |
| 105 | + |
| 106 | +def multiply_point_by_matrix(matrix, point): |
| 107 | + """ |
| 108 | + Multiply a point by a matrix. Written in Pure Python |
| 109 | + to avoid dependency on numpy. |
| 110 | + """ |
| 111 | + return [ |
| 112 | + (point[0] * matrix[0][0]) |
| 113 | + + (point[1] * matrix[0][1]) |
| 114 | + + (point[2] * matrix[0][2]) |
| 115 | + + (1 * matrix[0][3]), |
| 116 | + (point[0] * matrix[1][0]) |
| 117 | + + (point[1] * matrix[1][1]) |
| 118 | + + (point[2] * matrix[1][2]) |
| 119 | + + (1 * matrix[1][3]), |
| 120 | + (point[0] * matrix[2][0]) |
| 121 | + + (point[1] * matrix[2][1]) |
| 122 | + + (point[2] * matrix[2][2]) |
| 123 | + + (1 * matrix[2][3]), |
| 124 | + ] |
| 125 | + |
| 126 | + |
| 127 | +def point_to_canvas(size, point, z=False): |
| 128 | + """ |
| 129 | + Convert to screen coordinates (flip horizontally) |
| 130 | + Only return the first two values [x, y] of point |
| 131 | + """ |
| 132 | + if z: |
| 133 | + return [int(size[0] - point[0]), int(point[1]), point[2]] |
| 134 | + else: |
| 135 | + return [int(size[0] - point[0]), int(point[1])] |
| 136 | + |
| 137 | + |
| 138 | +def draw_line(size, canvas, transform, a, b, color): |
| 139 | + """ |
| 140 | + Draw a line on the canvas given two points and transform. |
| 141 | + """ |
| 142 | + ta = point_to_canvas(size, multiply_point_by_matrix(transform, a)) |
| 143 | + tb = point_to_canvas(size, multiply_point_by_matrix(transform, b)) |
| 144 | + canvas.line(ta + tb, fill=color) |
| 145 | + |
| 146 | + |
| 147 | +def draw_point(size, canvas, transform, point, color): |
| 148 | + """ |
| 149 | + Draw a point on the canvas given the transform. |
| 150 | + """ |
| 151 | + p = point_to_canvas(size, multiply_point_by_matrix(transform, point)) |
| 152 | + canvas.point(p, fill=color) |
| 153 | + |
| 154 | + |
| 155 | +def draw_point_fake(size, fcanvas, transform, point, color): |
| 156 | + """ |
| 157 | + Draw a point on the canvas given the transform. |
| 158 | + """ |
| 159 | + p = point_to_canvas(size, multiply_point_by_matrix(transform, point), z=True) |
| 160 | + location = fcanvas[(p[0], p[1])] |
| 161 | + if location is None or location["z"] < p[2]: |
| 162 | + fcanvas[(p[0], p[1])] = {"z": p[2], "color": color} |
0 commit comments