|
| 1 | +from abc import ABC, abstractmethod |
| 2 | + |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +from autoarray.structures.triangles.abstract import HEIGHT_FACTOR, AbstractTriangles |
| 6 | +from autoconf import cached_property |
| 7 | + |
| 8 | + |
| 9 | +class AbstractCoordinateArray(ABC): |
| 10 | + def __init__( |
| 11 | + self, |
| 12 | + coordinates: np.ndarray, |
| 13 | + side_length: float, |
| 14 | + x_offset: float = 0.0, |
| 15 | + y_offset: float = 0.0, |
| 16 | + flipped: bool = False, |
| 17 | + ): |
| 18 | + """ |
| 19 | + Represents a set of triangles by integer coordinates. |
| 20 | +
|
| 21 | + Parameters |
| 22 | + ---------- |
| 23 | + coordinates |
| 24 | + Integer x y coordinates for each triangle. |
| 25 | + side_length |
| 26 | + The side length of the triangles. |
| 27 | + flipped |
| 28 | + Whether the triangles are flipped upside down. |
| 29 | + y_offset |
| 30 | + An y_offset to apply to the y coordinates so that up-sampled triangles align. |
| 31 | + """ |
| 32 | + self.coordinates = coordinates |
| 33 | + self.side_length = side_length |
| 34 | + self.flipped = flipped |
| 35 | + |
| 36 | + self.scaling_factors = np.array( |
| 37 | + [0.5 * side_length, HEIGHT_FACTOR * side_length] |
| 38 | + ) |
| 39 | + self.x_offset = x_offset |
| 40 | + self.y_offset = y_offset |
| 41 | + |
| 42 | + @cached_property |
| 43 | + def triangles(self) -> np.ndarray: |
| 44 | + """ |
| 45 | + The vertices of the triangles as an Nx3x2 array. |
| 46 | + """ |
| 47 | + centres = self.centres |
| 48 | + return np.stack( |
| 49 | + ( |
| 50 | + centres |
| 51 | + + self.flip_array |
| 52 | + * np.array( |
| 53 | + [0.0, 0.5 * self.side_length * HEIGHT_FACTOR], |
| 54 | + ), |
| 55 | + centres |
| 56 | + + self.flip_array |
| 57 | + * np.array( |
| 58 | + [0.5 * self.side_length, -0.5 * self.side_length * HEIGHT_FACTOR] |
| 59 | + ), |
| 60 | + centres |
| 61 | + + self.flip_array |
| 62 | + * np.array( |
| 63 | + [-0.5 * self.side_length, -0.5 * self.side_length * HEIGHT_FACTOR] |
| 64 | + ), |
| 65 | + ), |
| 66 | + axis=1, |
| 67 | + ) |
| 68 | + |
| 69 | + @property |
| 70 | + def centres(self) -> np.ndarray: |
| 71 | + """ |
| 72 | + The centres of the triangles. |
| 73 | + """ |
| 74 | + return self.scaling_factors * self.coordinates + np.array( |
| 75 | + [self.x_offset, self.y_offset] |
| 76 | + ) |
| 77 | + |
| 78 | + @cached_property |
| 79 | + def flip_mask(self) -> np.ndarray: |
| 80 | + """ |
| 81 | + A mask for the triangles that are flipped. |
| 82 | +
|
| 83 | + Every other triangle is flipped so that they tessellate. |
| 84 | + """ |
| 85 | + mask = (self.coordinates[:, 0] + self.coordinates[:, 1]) % 2 != 0 |
| 86 | + if self.flipped: |
| 87 | + mask = ~mask |
| 88 | + return mask |
| 89 | + |
| 90 | + @cached_property |
| 91 | + @abstractmethod |
| 92 | + def flip_array(self) -> np.ndarray: |
| 93 | + """ |
| 94 | + An array of 1s and -1s to flip the triangles. |
| 95 | + """ |
| 96 | + |
| 97 | + def __iter__(self): |
| 98 | + return iter(self.triangles) |
| 99 | + |
| 100 | + @cached_property |
| 101 | + @abstractmethod |
| 102 | + def _vertices_and_indices(self): |
| 103 | + pass |
| 104 | + |
| 105 | + @property |
| 106 | + def vertices(self) -> np.ndarray: |
| 107 | + """ |
| 108 | + The unique vertices of the triangles. |
| 109 | + """ |
| 110 | + return self._vertices_and_indices[0] |
| 111 | + |
| 112 | + @property |
| 113 | + def indices(self) -> np.ndarray: |
| 114 | + """ |
| 115 | + The indices of the vertices of the triangles. |
| 116 | + """ |
| 117 | + return self._vertices_and_indices[1] |
| 118 | + |
| 119 | + def with_vertices(self, vertices: np.ndarray) -> AbstractTriangles: |
| 120 | + """ |
| 121 | + Create a new set of triangles with the vertices replaced. |
| 122 | +
|
| 123 | + Parameters |
| 124 | + ---------- |
| 125 | + vertices |
| 126 | + The new vertices to use. |
| 127 | +
|
| 128 | + Returns |
| 129 | + ------- |
| 130 | + The new set of triangles with the new vertices. |
| 131 | + """ |
| 132 | + |
| 133 | + @classmethod |
| 134 | + def for_limits_and_scale( |
| 135 | + cls, |
| 136 | + x_min: float, |
| 137 | + x_max: float, |
| 138 | + y_min: float, |
| 139 | + y_max: float, |
| 140 | + scale: float = 1.0, |
| 141 | + **_, |
| 142 | + ): |
| 143 | + coordinates = [] |
| 144 | + x = x_min |
| 145 | + while x < x_max: |
| 146 | + y = y_min |
| 147 | + while y < y_max: |
| 148 | + coordinates.append([x, y]) |
| 149 | + y += scale |
| 150 | + x += scale |
| 151 | + |
| 152 | + x_mean = (x_min + x_max) / 2 |
| 153 | + y_mean = (y_min + y_max) / 2 |
| 154 | + |
| 155 | + return cls( |
| 156 | + coordinates=np.array(coordinates), |
| 157 | + side_length=scale, |
| 158 | + x_offset=x_mean, |
| 159 | + y_offset=y_mean, |
| 160 | + ) |
| 161 | + |
| 162 | + @property |
| 163 | + def means(self): |
| 164 | + return np.mean(self.triangles, axis=1) |
| 165 | + |
| 166 | + @property |
| 167 | + def area(self): |
| 168 | + return (3**0.5 / 4 * self.side_length**2) * len(self) |
| 169 | + |
| 170 | + def __len__(self): |
| 171 | + return np.count_nonzero(~np.isnan(self.coordinates).any(axis=1)) |
0 commit comments