def update_points(self): """If self.vectors has been modified (either resigned to a new array or the array's contents have been altered) then this be called after.""" vertices = flatten_all_but_last(self.vectors) self.temp.append(vertices) self.points.SetData(numpy_to_vtk(vertices))
def __init__(self, x, y, z, scalars=None, color=None, opacity=None, texture_map=None, fig="gcf"): super().__init__(fig) points = nuts_and_bolts.zip_axes(x, y, z) flat_points = nuts_and_bolts.flatten_all_but_last(points) shape = points.shape[:-1] unflatten_map = np.arange(np.prod(shape)).reshape(shape) corners = ( unflatten_map[:-1, :-1], unflatten_map[1:, :-1], unflatten_map[1:, 1:], unflatten_map[:-1, 1:], ) args = np.concatenate([i[..., np.newaxis] for i in corners], axis=-1) self.polydata.points = flat_points self.polydata.polygons = args self.polydata.texture_map = texture_map self.colors = scalars self.add_to_plot() self.color_opacity(color, opacity)
def orthogonal_bases(vector0): vector0 = np.asarray(vector0) old_shape = vector0.shape vector0 = nuts_and_bolts.flatten_all_but_last(vector0) vector0 = normalised(vector0) if not np.isfinite(vector0).all(): raise ValueError("vector0 must have non zero magnitude") to_do_mask = np.ones(vector0.shape[:-1], bool) vector1 = np.empty_like(vector0) vector2 = np.empty_like(vector0) while to_do_mask.any(): temp = np.random.uniform(-1, 1, 3) vector1[to_do_mask] = np.cross(temp, vector0[to_do_mask]) vector1[to_do_mask] /= distance(vector1)[..., np.newaxis] vector2[to_do_mask] = np.cross(vector0[to_do_mask], vector1[to_do_mask]) vector2[to_do_mask] /= distance(vector2)[..., np.newaxis] to_do_mask &= np.logical_not(np.isfinite(vector2).all(-1)) return vector0.reshape(old_shape), vector1.reshape( old_shape), vector2.reshape(old_shape)
def __init__(self, vertices, color=None, opacity=None, fig="gcf"): super().__init__(fig) # The implementation of this is actually exactly the same as Lines plot # but sets args to polydata.polygons rather than polydata.lines shape = vertices.shape[:-1] points = _numpy_vtk.contiguous_safe( nuts_and_bolts.flatten_all_but_last(vertices)) self.temp.append(points) args = nuts_and_bolts.flatten_all_but_last( np.arange(np.prod(shape)).reshape(shape)) self.polydata.points = points self.polydata.polygons = args self.add_to_plot() self.color_opacity(color, opacity)
def __init__(self, vertices, color=None, opacity=None, line_width=1.0, join_ends=False, fig="gcf"): super().__init__(fig) shape = vertices.shape[:-1] points = _numpy_vtk.contiguous_safe(nuts_and_bolts.flatten_all_but_last(vertices)) self.temp.append(points) args = nuts_and_bolts.flatten_all_but_last(np.arange(np.prod(shape)).reshape(shape)) self.polydata.points = points if join_ends: self.polydata.lines = join_line_ends(args) else: self.polydata.lines = args # assert np.array_equal(points[args], vertices) self.add_to_plot() self.color_opacity(color, opacity) self.property.SetLineWidth(line_width)
def pack_lengths(itr_of_arrays): if not isinstance(itr_of_arrays, (list, np.ndarray)): itr_of_arrays = list(itr_of_arrays) itr_of_arrays = np.asarray(itr_of_arrays) if itr_of_arrays.dtype == object: parts = itertools.chain(*[([len(i)], i) for i in itr_of_arrays.flat]) return np.concatenate(list(parts)) else: from vtkplotlib.nuts_and_bolts import flatten_all_but_last itr_of_arrays = flatten_all_but_last(itr_of_arrays) n, m = itr_of_arrays.shape lengths = np.empty((n, 1), int) lengths[:] = m return np.concatenate([lengths, itr_of_arrays], axis=1)
def _iter_colors(colors, shape): """Check if colors is a single value or is to be iterated over. If it is single then creates a generator that yields that value repeatedly.""" size = int(np.prod(shape)) if colors is None: return (None for i in range(size)) if isinstance(colors, (tuple, list, str)): return (colors for i in range(size)) colors = np.asarray(colors) if colors.dtype == object: raise ValueError("colors type not understood") if colors.shape[:-1] == shape: return nuts_and_bolts.flatten_all_but_last(colors) else: raise ValueError("colors type not understood")
def colors(self, s): if s is not None and s.ndim > 2: s = nuts_and_bolts.flatten_all_but_last(s) self.polydata.point_colors = s
def _iter_points(points): """Fixes the array shape to (n, 3).""" points = np.asarray(points) return nuts_and_bolts.flatten_all_but_last(points)