Skip to content

Commit

Permalink
Release v0.9.0
Browse files Browse the repository at this point in the history
- Add support for texture from remote host
- Add object removal ability
- Add ability to fetch_data from front-end
- Add camera auto-fit support
- Fix loosing order information on object update
  • Loading branch information
skremiec committed Dec 2, 2015
1 parent e8107c4 commit 4210478
Show file tree
Hide file tree
Showing 9 changed files with 272 additions and 41 deletions.
4 changes: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "k3d-jupyter",
"version": "0.8.0",
"version": "0.9.0",
"authors": [
"Artur Trzesiok <[email protected]>",
"Sebastian Kremiec <[email protected]>"
],
"description": "Juyter notebook extension for K3D visualization library.",
"license": "GPLv2",
"dependencies": {
"k3d": "~0.11.0",
"k3d": "~0.12.0",
"pako": "~0.2.8"
}
}
33 changes: 30 additions & 3 deletions examples/line.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"outputs": [],
"source": [
"from k3d import K3D\n",
"from ipywidgets import interact, FloatSlider\n",
"\n",
"view_matrix = (\n",
" 0.9, 0.1, -0.4, 0.0,\n",
Expand All @@ -32,9 +33,14 @@
"line = K3D.line((0, 0, 0, 5, 10, 15))\n",
"lines = K3D.line(positions, view_matrix=view_matrix, color=0xff0000, width=2.5) + line\n",
"\n",
"plot = K3D(antialias=True)\n",
"plot = K3D(antialias=True, camera_auto_fit=True)\n",
"plot += lines\n",
"plot.display()"
"plot.display()\n",
"\n",
"# plot.camera_auto_fit = False # uncomment in order to keep camera at fixed distance on changes\n",
"@interact(width=FloatSlider(value=line.line_width, min=0.1, max=4.0, step=0.1))\n",
"def update(width):\n",
" line.line_width = width"
]
},
{
Expand All @@ -45,9 +51,30 @@
},
"outputs": [],
"source": [
"line.line_width = 3\n",
"line.color = 0xFFFF00"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"plot - line"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"plot - lines"
]
}
],
"metadata": {
Expand Down
15 changes: 14 additions & 1 deletion examples/texture.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,23 @@
"source": [
"from k3d import K3D\n",
"\n",
"texture = K3D.texture('assets/texture.png')\n",
"\n",
"plot = K3D()\n",
"plot += K3D.texture('assets/texture.png')\n",
"plot + texture\n",
"plot.display()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"texture.image = 'http://i.stack.imgur.com/ILTQq.png'"
]
}
],
"metadata": {
Expand Down
86 changes: 86 additions & 0 deletions examples/voxels_edit.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from k3d import K3D\n",
"\n",
"width = height = length = 3\n",
"\n",
"color_map = (0xFF0000,) * width * height * length\n",
"voxels = (1,) * width * height * length\n",
"obj = K3D.voxels(voxels, color_map, width=width, height=height, length=length)\n",
"\n",
"plot = K3D()\n",
"plot += obj\n",
"plot.display()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"obj.voxels.flatten() # initial data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Edit object (add/remove some voxels)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"obj.fetch_data() # this is an async operation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"obj.voxels.flatten() # updated data"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
37 changes: 33 additions & 4 deletions k3d/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from ipywidgets import DOMWidget
from IPython.display import display
from traitlets import Unicode, Bytes, Dict
from traitlets import Unicode, Bytes, Dict, Bool
from functools import partial
from .factory import Factory
from .objects import Drawable
from .queue import Queue
from .version import version # noqa, pylint: disable=no-name-in-module
from .version import version
import base64
import json
import zlib
Expand All @@ -15,19 +16,24 @@ class K3D(DOMWidget, Factory):
_view_name = Unicode('K3DView', sync=True)
_model_module = Unicode('nbextensions/k3d_widget/model', sync=True)
_model_name = Unicode('K3DModel', sync=True)
version = version

COMPRESSION_LEVEL = 1

camera_auto_fit = Bool(sync=True)
data = Bytes(sync=True)
parameters = Dict(sync=True)

def __init__(self, antialias=False, background_color=0xFFFFFF, height=512):
def __init__(self, antialias=False, background_color=0xFFFFFF, camera_auto_fit=True, height=512):
super(K3D, self).__init__()
self.on_msg(self.__on_msg)

self.__queue = Queue(self.__show)
self.__display_strategy = self.__display
self.__update_strategy = self.__pass
self.__on_msg_strategy = self.__pass

self.camera_auto_fit = camera_auto_fit
self.parameters = {
'antialias': antialias,
'backgroundColor': background_color,
Expand All @@ -42,15 +48,38 @@ def __add__(self, obj):

return self

def __sub__(self, objs):
assert isinstance(objs, Drawable)

for obj in objs:
if obj.unset_plot(self):
self.__update_strategy(obj.id)

return self

def fetch_data(self, obj):
self.__on_msg_strategy = partial(self.__on_data, obj=obj)
self.send(obj.id)

def __on_msg(self, *args):
self.__on_msg_strategy(args[1])

def __on_data(self, data, obj):
self.__on_msg_strategy = self.__pass
obj.update(data)

def update(self, obj_id, attr):
self.__update_strategy(obj_id, attr)

def __update(self, obj_id, attr):
def __update(self, obj_id, attr=None):
data = {
'id': obj_id,
'attr': attr,
}

if attr is None:
del data['attr']

self.data = self.__to_compressed_base64(self.__to_json(data))

def __show(self, objs):
Expand Down
14 changes: 6 additions & 8 deletions k3d/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def surface(cls, heights, xmin=-.5, xmax=.5, ymin=-.5, ymax=.5, view_matrix=nump
'color': color,
'width': width,
'height': height,
'heights': cls.__to_ndarray(heights, order='F'),
'heights': cls.__to_ndarray(heights),
})

@classmethod
Expand All @@ -57,7 +57,7 @@ def marching_cubes(cls, scalars_field, level, xmin=-.5, xmax=.5, ymin=-.5, ymax=
'length': length,
'color': color,
'level': level,
'scalars_field': cls.__to_ndarray(scalars_field, order='F'),
'scalars_field': cls.__to_ndarray(scalars_field),
})

@classmethod
Expand Down Expand Up @@ -96,7 +96,7 @@ def vectors_fields(cls, vectors, colors=(), color=DEFAULT_COLOR, xmin=-.5, xmax=
return objects.VectorsFields(**{
'use_head': use_head,
'model_view_matrix': cls.__get_view_matrix(view_matrix, xmin, xmax, ymin, ymax, zmin, zmax),
'vectors': cls.__to_ndarray(vectors, order='F'),
'vectors': cls.__to_ndarray(vectors),
'width': width,
'height': height,
'colors': cls.__to_ndarray(colors, numpy.uint32),
Expand Down Expand Up @@ -129,14 +129,12 @@ def voxels(cls, voxels, color_map, xmin=-.5, xmax=.5, ymin=-.5, ymax=.5, zmin=-.
'height': height,
'length': length,
'color_map': cls.__to_ndarray(color_map, numpy.uint32),
'voxels': cls.__to_ndarray(voxels, numpy.uint8, order='F'),
'voxels': cls.__to_ndarray(voxels, numpy.uint8),
})

@staticmethod
def __to_ndarray(array_like, dtype=numpy.float32, order='C'):
array = numpy.array(array_like, dtype, order='C')

return array.T.copy() if order == 'F' else array
def __to_ndarray(array_like, dtype=numpy.float32):
return numpy.array(array_like, dtype, order='C')

@staticmethod
def __get_dimensions(shape, *dimensions):
Expand Down
Loading

0 comments on commit 4210478

Please sign in to comment.