Skip to content

Commit

Permalink
Does K3D allow plotting multiple texts as single object? #391
Browse files Browse the repository at this point in the history
  • Loading branch information
artur-trzesiok committed Dec 17, 2022
1 parent 3c2e453 commit 9bcf754
Show file tree
Hide file tree
Showing 17 changed files with 778 additions and 452 deletions.
46 changes: 44 additions & 2 deletions examples/labels.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,48 @@
"plot.mode = 'callback'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Multipile labels in single object"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"N = 20\n",
"positions = np.random.randn(N,3).astype(np.float32)\n",
"\n",
"plot2 = k3d.plot()\n",
"obj = k3d.label([\"lorem ipsum\"] * N, positions, is_html=True, mode='dynamic')\n",
"# obj = k3d.label([\"lorem ipsum\"] * N, positions, is_html=True, mode='local')\n",
"# obj = k3d.label([\"lorem ipsum\"] * N, positions, is_html=True, mode='side')\n",
"plot2 += obj\n",
"plot2.display()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"obj.text= list(map(lambda a: str(a), list(range(N))))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"obj.position = np.random.randn(N,3).astype(np.float32)"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -138,7 +180,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -152,7 +194,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
"version": "3.9.12"
}
},
"nbformat": 4,
Expand Down
41 changes: 39 additions & 2 deletions examples/text.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,43 @@
"plot.objects[0].label_box = False"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Multipile texts in single object\n",
"\n",
"import numpy as np\n",
"\n",
"N = 20\n",
"positions = np.random.randn(N,3).astype(np.float32)\n",
"\n",
"plot2 = k3d.plot()\n",
"obj = k3d.text([\"lorem ipsum\"] * N, positions, is_html=True)\n",
"plot2 += obj\n",
"plot2.display()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"obj.text= list(map(lambda a: str(a), list(range(N))))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"obj.position = np.random.randn(N,3).astype(np.float32)"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -38,7 +75,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -52,7 +89,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
"version": "3.9.12"
}
},
"nbformat": 4,
Expand Down
41 changes: 39 additions & 2 deletions examples/text2d.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,43 @@
"plot.objects[0].label_box = False"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Multipile texts in single object\n",
"\n",
"import numpy as np\n",
"\n",
"N = 20\n",
"positions = np.random.randn(N,2).astype(np.float32)\n",
"\n",
"plot2 = k3d.plot()\n",
"obj = k3d.text2d([\"lorem ipsum\"] * N, positions, is_html=True)\n",
"plot2 += obj\n",
"plot2.display()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"obj.text= list(map(lambda a: str(a), list(range(N))))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"obj.position = np.random.randn(N,2).astype(np.float32)"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -37,7 +74,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -51,7 +88,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
"version": "3.9.12"
}
},
"nbformat": 4,
Expand Down
73 changes: 69 additions & 4 deletions examples/texture_text.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,24 @@
"cells": [
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "bc508473743d49a181b15c0e0e61ff22",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Output()"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import k3d\n",
"import numpy as np\n",
Expand All @@ -18,6 +33,56 @@
"plot.display()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "86bac1ad01384939a8e99d36299d0725",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Output()"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Multipile texts in single object\n",
"\n",
"N = 20\n",
"positions = np.random.randn(N,3).astype(np.float32)\n",
"\n",
"plot2 = k3d.plot()\n",
"obj = k3d.texture_text([\"lorem ipsum\"] * N, positions, is_html=True)\n",
"plot2 += obj\n",
"plot2.display()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"obj.text = list(map(lambda a: str(a), list(range(N))))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"obj.position = np.random.randn(N,3).astype(np.float32)"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -28,7 +93,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -42,7 +107,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
"version": "3.9.12"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "k3d",
"version": "2.15.0",
"version": "2.15.1",
"description": "3D visualization library",
"author": "k3d team",
"main": "src/index.js",
Expand Down
7 changes: 4 additions & 3 deletions js/src/core/Core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1209,10 +1209,11 @@ function K3D(provider, targetDOMNode, parameters) {
}
this.autoRendering = false;

world.K3DObjects.children.forEach((obj) => {
removeObjectFromScene(obj.K3DIdentifier);
delete world.ObjectsListJson[obj.K3DIdentifier];
Object.keys(world.ObjectsListJson).forEach(function (K3DIdentifier) {
removeObjectFromScene(K3DIdentifier);
delete world.ObjectsListJson[K3DIdentifier];
});

world.cleanup();

if (fpsMeter) {
Expand Down
2 changes: 1 addition & 1 deletion js/src/core/lib/objectsGUIprovider.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ function update(K3D, json, GUI, changes) {
);
break;
case 'text':
if (json.type !== 'STL') {
if (json.type !== 'STL' && !Array.isArray(json.text)) {
addController(K3D.gui_map[json.id], json, param).onChange(
changeParameter.bind(this, K3D, json, param),
);
Expand Down
1 change: 1 addition & 0 deletions js/src/lodash.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ module.exports = {
isObject: require('lodash/isObject'),
isEmpty: require('lodash/isEmpty'),
isEqual: require('lodash/isEqual'),
isArray: require('lodash/isArray')
};
10 changes: 4 additions & 6 deletions js/src/providers/threejs/initializers/Camera.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const THREE = require('three');
const { cameraModes } = require('../../../core/lib/cameraMode');
const { recalculateFrustum } = require('../helpers/Fn');
const {cameraModes} = require('../../../core/lib/cameraMode');
const {recalculateFrustum} = require('../helpers/Fn');

/**
* Camera initializer for Three.js library
Expand Down Expand Up @@ -31,7 +31,7 @@ module.exports = function (K3D) {
if (typeof (this.camera.fov) !== 'undefined' && typeof (this.axesHelper.camera.fov) !== 'undefined') {
this.camera.fov = fov;
this.axesHelper.camera.fov = fov;
this.controls.dispatchEvent({ type: 'change' });
this.controls.dispatchEvent({type: 'change'});
}
}

Expand Down Expand Up @@ -60,9 +60,7 @@ module.exports = function (K3D) {
return;
}

if (this.K3DObjects.children.length > 0) {
sceneBoundingBox = K3D.getSceneBoundingBox() || sceneBoundingBox;
}
sceneBoundingBox = K3D.getSceneBoundingBox() || sceneBoundingBox;

if (typeof (factor) === 'undefined') {
factor = 1.5;
Expand Down
Loading

0 comments on commit 9bcf754

Please sign in to comment.