|
| 1 | +####################################################### |
| 2 | +# Copyright (c) 2015, ArrayFire |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# This file is distributed under 3-clause BSD license. |
| 6 | +# The complete license agreement can be obtained at: |
| 7 | +# http://arrayfire.com/licenses/BSD-3-Clause |
| 8 | +######################################################## |
| 9 | + |
| 10 | +from .library import * |
| 11 | +from .array import * |
| 12 | + |
| 13 | +class cell(ct.Structure): |
| 14 | + _fields_ = [("row", ct.c_int), |
| 15 | + ("col", ct.c_int), |
| 16 | + ("title", ct.c_char_p), |
| 17 | + ("cmap", ct.c_int)] |
| 18 | + |
| 19 | + def __init__(self, r, c, title, cmap): |
| 20 | + self.row = r |
| 21 | + self.col = c |
| 22 | + self.title = title if title is not None else ct.c_char_p() |
| 23 | + self.cmap = cmap |
| 24 | + |
| 25 | +class window(object): |
| 26 | + |
| 27 | + def __init__(self, width=None, height=None, title=None): |
| 28 | + self._r = -1 |
| 29 | + self._c = -1 |
| 30 | + self._wnd = ct.c_longlong(0) |
| 31 | + self._cmap = AF_COLORMAP_DEFAULT |
| 32 | + |
| 33 | + _width = 1280 if width is None else width |
| 34 | + _height = 720 if height is None else height |
| 35 | + _title = "ArrayFire" if title is None else title |
| 36 | + |
| 37 | + _title = _title.encode("ascii") |
| 38 | + |
| 39 | + safe_call(clib.af_create_window(ct.pointer(self._wnd),\ |
| 40 | + ct.c_int(_width), ct.c_int(_height), ct.c_char_p(_title))) |
| 41 | + |
| 42 | + def __del__(self): |
| 43 | + safe_call(clib.af_destroy_window(self._wnd)) |
| 44 | + |
| 45 | + def set_pos(self, x, y): |
| 46 | + safe_call(clib.af_set_position(self._wnd, ct.c_int(x), ct.c_int(y))) |
| 47 | + |
| 48 | + def set_title(self, title): |
| 49 | + safe_call(clib.af_set_title(self._wnd, title)) |
| 50 | + |
| 51 | + def set_colormap(self, cmap): |
| 52 | + self._cmap = cmap |
| 53 | + |
| 54 | + def image(self, img, title=None): |
| 55 | + _cell = cell(self._r, self._c, title, self._cmap) |
| 56 | + safe_call(clib.af_draw_image(self._wnd, img.arr, ct.pointer(_cell))) |
| 57 | + |
| 58 | + def plot(self, X, Y, title=None): |
| 59 | + _cell = cell(self._r, self._c, title, self._cmap) |
| 60 | + safe_call(clib.af_draw_plot(self._wnd, X.arr, Y.arr, ct.pointer(_cell))) |
| 61 | + |
| 62 | + def hist(self, X, min_val, max_val, title=None): |
| 63 | + _cell = cell(self._r, self._c, title, self._cmap) |
| 64 | + safe_call(clib.af_draw_hist(self._wnd, X.arr, \ |
| 65 | + ct.c_double(max_val), ct.c_double(min_val),\ |
| 66 | + ct.pointer(_cell))) |
| 67 | + |
| 68 | + def grid(rows, cols): |
| 69 | + safe_call(af_grid(self._wnd, ct.c_int(rows), ct.c_int(cols))) |
| 70 | + |
| 71 | + def show(self): |
| 72 | + safe_call(clib.af_show(self._wnd)) |
| 73 | + |
| 74 | + def close(self): |
| 75 | + tmp = ct.c_bool(True) |
| 76 | + safe_call(clib.af_is_window_closed(ct.pointer(tmp), self._wnd)) |
| 77 | + return tmp |
| 78 | + |
| 79 | + def __getitem__(self, keys): |
| 80 | + if not isinstance(keys, tuple): |
| 81 | + raise IndexError("Window expects indexing along two dimensions") |
| 82 | + if len(keys) != 2: |
| 83 | + raise IndexError("Window expects indexing along two dimensions only") |
| 84 | + if not (is_number(keys[0]) and is_number(keys[1])): |
| 85 | + raise IndexError("Window expects the indices to be numbers") |
| 86 | + self._r = keys[0] |
| 87 | + self._c = keys[1] |
0 commit comments