{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import k3d\n", "import numpy as np\n", "\n", "dim = 128\n", "data = np.zeros((dim, dim, dim))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "N = 100000\n", "paths = [np.cumsum(np.random.randn(N,3).astype(np.float32), axis=0) for _ in range(3)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for i in range(len(paths)):\n", " path = paths[i]\n", " minimum = np.min(path, axis=0)\n", " maximum = np.max(path, axis=0)\n", " \n", " paths[i] = (path - minimum) / np.max(maximum-minimum)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot = k3d.plot()\n", "plot.display()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lines = []\n", "for i, path in enumerate(paths):\n", " lines.append(k3d.line(100.0 * path, width=0.001, color=k3d.nice_colors[i]))\n", " plot += lines[i]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for i, path in enumerate(paths):\n", " indices = np.fix((dim-1) * path).astype(np.uint16)\n", " data[(indices[:,2], indices[:,1], indices[:,0])] = i + 1\n", "\n", "dense_data = data.astype(np.uint8)\n", "dense_voxels = k3d.voxels(dense_data, bounds=[0, 100, 0, 100, 0, 100], compression_level=1)\n", "plot += dense_voxels" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for i, path in enumerate(paths):\n", " plot -= lines[i]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sparse_data = []\n", "\n", "for val in np.unique(dense_data):\n", " if val != 0:\n", " x, y, z = np.where(dense_data==val)\n", " sparse_data.append(np.dstack((x, y, z, np.full(x.shape, val))).reshape(-1,4).astype(np.uint16))\n", " \n", "sparse_data = np.vstack(sparse_data)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot -= dense_voxels\n", "dense_data.nbytes / (1024 ** 2), sparse_data.nbytes / (1024 ** 2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sparse_voxels = k3d.sparse_voxels(sparse_data, [dim, dim, dim], bounds=[0, 100, 0, 100, 0, 100], compression_level=1)\n", "plot += sparse_voxels" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Edit object (add/remove some voxels)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sparse_voxels.fetch_data('sparse_voxels')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sparse_voxels.sparse_voxels.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sparse_voxels.sparse_voxels" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.5" }, "nbTranslate": { "displayLangs": [ "en", "pl" ], "hotkey": "alt-t", "langInMainMenu": true, "sourceLang": "pl", "targetLang": "en", "useGoogleTranslate": true }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 4 }