|
| 1 | +from abc import ABC, abstractmethod |
| 2 | +from typing import * |
| 3 | +from pygfx import Color |
| 4 | +import numpy as np |
| 5 | + |
| 6 | + |
| 7 | +class GraphicFeature(ABC): |
| 8 | + def __init__(self, parent, data: Any): |
| 9 | + self._parent = parent |
| 10 | + self._data = data |
| 11 | + |
| 12 | + @abstractmethod |
| 13 | + def __getitem__(self, item): |
| 14 | + pass |
| 15 | + |
| 16 | + @abstractmethod |
| 17 | + def __setitem__(self, key, value): |
| 18 | + pass |
| 19 | + |
| 20 | + @abstractmethod |
| 21 | + def _update_range(self, key): |
| 22 | + pass |
| 23 | + |
| 24 | + |
| 25 | +class ColorFeature(GraphicFeature): |
| 26 | + def __init__(self, parent, data): |
| 27 | + data = parent.geometry.colors.data |
| 28 | + super(ColorFeature, self).__init__(parent, data) |
| 29 | + |
| 30 | + self._bounds = data.shape[0] |
| 31 | + |
| 32 | + def __setitem__(self, key, value): |
| 33 | + if abs(key.start) > self._bounds or abs(key.stop) > self._bounds: |
| 34 | + raise IndexError |
| 35 | + |
| 36 | + if isinstance(key, slice): |
| 37 | + start = key.start |
| 38 | + stop = key.stop |
| 39 | + step = key.step |
| 40 | + if step is None: |
| 41 | + step = 1 |
| 42 | + |
| 43 | + indices = range(start, stop, step) |
| 44 | + |
| 45 | + elif isinstance(key, int): |
| 46 | + indices = [key] |
| 47 | + |
| 48 | + else: |
| 49 | + raise TypeError("Graphic features only support integer and numerical fancy indexing") |
| 50 | + |
| 51 | + new_data_size = len(indices) |
| 52 | + |
| 53 | + if not isinstance(value, np.ndarray): |
| 54 | + new_colors = np.repeat(np.array([Color(value)]), new_data_size, axis=0) |
| 55 | + |
| 56 | + elif isinstance(value, np.ndarray): |
| 57 | + if value.shape == (4,): |
| 58 | + new_colors = value.astype(np.float32) |
| 59 | + if new_data_size > 1: |
| 60 | + new_colors = np.repeat(np.array([new_colors]), new_data_size, axis=0) |
| 61 | + |
| 62 | + elif value.shape[1] == 4 and value.ndim == 2: |
| 63 | + if not value.shape[0] == new_data_size: |
| 64 | + raise ValueError("numpy array passed to color must be of shape (4,) or (n_colors_modify, 4)") |
| 65 | + if new_data_size == 1: |
| 66 | + new_colors = value.ravel().astype(np.float32) |
| 67 | + else: |
| 68 | + new_colors = value.astype(np.float32) |
| 69 | + |
| 70 | + else: |
| 71 | + raise ValueError("numpy array passed to color must be of shape (4,) or (n_colors_modify, 4)") |
| 72 | + |
| 73 | + self._parent.geometry.colors.data[key] = new_colors |
| 74 | + |
| 75 | + self._update_range(key) |
| 76 | + |
| 77 | + def _update_range(self, key): |
| 78 | + if isinstance(key, int): |
| 79 | + self._parent.geometry.colors.update_range(key, size=1) |
| 80 | + if key.step is None: |
| 81 | + # update range according to size using the offset |
| 82 | + self._parent.geometry.colors.update_range(offset=key.start, size=key.stop - key.start) |
| 83 | + |
| 84 | + else: |
| 85 | + step = key.step |
| 86 | + ixs = range(key.start, key.stop, step) |
| 87 | + # convert slice to indices |
| 88 | + for ix in ixs: |
| 89 | + self._parent.geometry.colors.update_range(ix, size=1) |
| 90 | + |
| 91 | + def __getitem__(self, item): |
| 92 | + return self._parent.geometry.colors.data[item] |
0 commit comments