From 4d0cd2870fc6cbfcb255201cb130c2dd2f82ca1e Mon Sep 17 00:00:00 2001 From: Dario Seyb Date: Fri, 23 Feb 2018 16:36:10 +0100 Subject: [PATCH] added some basic bounds --- examples/BoundsExample.ipynb | 51 +++++++++++++++++++++++++++++++++++ examples/ClippedDrawing.ipynb | 13 ++++++++- meshvis/context.py | 37 +++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 examples/BoundsExample.ipynb diff --git a/examples/BoundsExample.ipynb b/examples/BoundsExample.ipynb new file mode 100644 index 0000000..b752d5e --- /dev/null +++ b/examples/BoundsExample.ipynb @@ -0,0 +1,51 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import meshvis\n", + "import openmesh as om\n", + "import numpy as np\n", + "\n", + "m = om.PolyMesh()\n", + "\n", + "om.read_mesh(m, 'models/bunny.obj')\n", + "\n", + "vertices = m.points()\n", + "faces = m.face_vertex_indices()\n", + "\n", + "\n", + "mesh = meshvis.Mesh(vertices, faces, \n", + " normals = meshvis.calculateFaceNormals(vertices, faces))\n", + "\n", + "ctx = meshvis.Context()\n", + "ctx.draw(mesh, shading='flat', clipping_planes=[meshvis.Plane((1.0, 0.0, 0.0) , 0)])\n", + "ctx.showBounds()\n", + "ctx.display()" + ] + } + ], + "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" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/examples/ClippedDrawing.ipynb b/examples/ClippedDrawing.ipynb index 27815ac..ddb0d90 100644 --- a/examples/ClippedDrawing.ipynb +++ b/examples/ClippedDrawing.ipynb @@ -21,8 +21,19 @@ "mesh = meshvis.Mesh(vertices, faces, \n", " normals = meshvis.calculateFaceNormals(vertices, faces))\n", "\n", - "meshvis.display(mesh, shading='flat', clipping_planes=[meshvis.Plane((1.0, 0.0, 0.0) , 0)])" + "ctx = meshvis.Context()\n", + "ctx.draw(mesh, shading='flat', clipping_planes=[meshvis.Plane((1.0, 0.0, 0.0) , 0)])\n", + "ctx.display()\n", + "\n", + "ctx.setCameraPosition([0.2, 0, 0])" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { diff --git a/meshvis/context.py b/meshvis/context.py index f719de3..f23651d 100644 --- a/meshvis/context.py +++ b/meshvis/context.py @@ -41,6 +41,8 @@ class Context(object): self.click_picker.observe(on_picked, names=['point']) + self.show_bounds = False + def draw(self, obj, shading='flat', z_offset=0.5, texture=None, point_size = 1, perspective = False, line_width = 1, clipping_planes = []): """ Draw the given object. Not all named parameters are relevant for all object types. @@ -234,6 +236,27 @@ class Context(object): self.scene.add(mesh_obj) return self + def setCameraPosition(self, position): + self.camera.position = tuple(position) + + def showBounds(self): + self.show_bounds = True + + def hideBounds(self): + self.show_bounds = False + + def setBounds(self, val): + self.show_bounds = val + + def draw_text(self, text, position=(0, 0, 0), color='white', size=100, height=1): + """ + Draw a text object at the specified location with a given height + """ + height *= len(text) + sm = three.SpriteMaterial(map=three.TextTexture(string=text, color=color, size=size, squareTexture=True)) + sprite = three.Sprite(material=sm, position=tuple(position), scaleToTexture=True, scale=[height, height, 1]) + self.scene.add(sprite) + def display(self): """ Display a window containing all the previous draw calls. @@ -244,4 +267,18 @@ class Context(object): self.camera.position = tuple( np.array(self.camera.position) * distance * 5) self.orbit_controls.target = tuple(center) + if self.show_bounds: + extends = self.maxCorner - self.minCorner + boxEdges = [self.minCorner, + self.minCorner + [extends[0], 0, 0], + self.minCorner + [0, extends[1], 0], + self.minCorner + [0, 0, extends[2]]] + + self.draw_edges(boxEdges, [[0, 1], [0, 2], [0, 3]], FaceAttribute([[1, 0, 0], [0, 1, 0], [0, 0, 1]])) + self.draw_text(str(map( lambda v: round(v, 3), self.minCorner)), self.minCorner - [0, extends[1]*0.1, 0], color = 'black', height=extends[1]/25) + + self.draw_text(str(round(extends[0], 3)), (boxEdges[0] * 0.25 + boxEdges[1] * 0.75), color = 'black', height=extends[1]/20) + self.draw_text(str(round(extends[1], 3)), (boxEdges[0] * 0.25 + boxEdges[2] * 0.75), color = 'black', height=extends[1]/20) + self.draw_text(str(round(extends[2], 3)), (boxEdges[0] * 0.25 + boxEdges[3] * 0.75), color = 'black', height=extends[1]/20) + display(self.renderer) \ No newline at end of file -- GitLab