{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import k3d\n", "import numpy as np\n", "from k3d.helpers import download\n", "\n", "# credits: https://github.com/eladrich/pix2vertex.pytorch\n", "\n", "pix2vertex_file = download('http://k3d-jupyter.org/DATA/pix2vertex_output.npy')\n", "pix2vertex_image_file = download('http://k3d-jupyter.org/DATA/pix2vertex.jpg')\n", "data = np.load(pix2vertex_file, allow_pickle=True).tolist()\n", "image = open(pix2vertex_image_file, 'rb').read()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "heights = data['Z_surface'].T\n", "where = np.where(~np.isnan(heights))\n", "\n", "vertices = np.dstack(where)[0].astype(np.float32)\n", "vertices = np.append(vertices, np.zeros((vertices.shape[0], 1), dtype=np.float32), 1)\n", "vertices[:,2] = heights[where]\n", "\n", "pic2index = np.zeros((heights.shape[0], heights.shape[1]), dtype=np.int64)\n", "pic2index[where] = np.arange(vertices.shape[0], dtype=np.int64)\n", "\n", "#top-left triangle\n", "tlt = np.where(pic2index[0:-2, 0:-2] * pic2index[1:-1,0:-2] * pic2index[0:-2, 1:-1])\n", "#bottom-right triangle\n", "brt = np.where(pic2index[1:-1, 1:-1] * pic2index[0:-2,1:-1] * pic2index[1:-1, 0:-2])\n", "\n", "indices = np.dstack([pic2index[tlt], \n", " pic2index[(tlt[0] + 1, tlt[1])], \n", " pic2index[(tlt[0], tlt[1] + 1)] \n", " ])[0]\n", "\n", "indices = np.append(indices,\n", " np.dstack([pic2index[(brt[0] + 1, brt[1] + 1)], \n", " pic2index[(brt[0], brt[1] + 1)], \n", " pic2index[(brt[0] + 1, brt[1])]])[0])\n", "\n", "uvs = vertices[:, 0:2].copy()\n", "uvs[:, 0] /= heights.shape[0]\n", "uvs[:, 1] /= heights.shape[1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot = k3d.plot()\n", "\n", "obj = k3d.mesh(vertices.astype(np.float32), indices.astype(np.uint32), \n", " flat_shading=False, color=0xb2ccff, side='double')\n", "\n", "plot += obj\n", "\n", "plot.display()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot.camera = [126, 295, 413,\n", " 209, 319,-12,\n", " 0, -1, 0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "obj.uvs = uvs\n", "obj.texture = image\n", "obj.texture_file_format = 'jpg'\n", "obj.color = 0xffffff" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot.lighting = 0" ] }, { "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.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }