{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Draw and transform shapes\n\nIn this demo script, multi shapes are created.\nThese shapes are then rotated and translated by\nvarious angles and distances and displayed in one plot figure.\n\nThe overlay image of the plot is finally set to a masked\nobject, where the masked area ist equal to the union of\nall shapes, visible at the canvas.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\nfrom itom import dataObject\nfrom itom import shape\nfrom itom import plot" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define a ``dataObject`` and plot using it as a \"background\".\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "dObj = dataObject.zeros([1200, 1200], \"uint8\")\ndObj.axisOffsets = (600, 600)\n[i, h] = plot(\n dObj,\n properties={\n \"colorMap\": \"viridis\",\n \"geometricShapesLabelsVisible\": True,\n \"keepAspectRatio\": True,\n },\n)\n\nbase_shapes = []" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is one option to create shapes.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "rect = shape(shape.Rectangle, (400, 240), (470, 270))\nrect.name = \"Rect\"\nellipse = shape(shape.Ellipse, (-300, -300), (-140, -200))\nellipse.name = \"Ellipse\"\nsquare = shape(shape.Square, (220, -300), 60)\nsquare.name = \"Square\"\ncircle = shape(shape.Circle, (-250, 100), 25)\ncircle.name = \"Circle\"\npolygons = np.array([[100, 200, 250, 200, 90], [50, 60, 100, 150, 150]])\npolygon = shape(shape.Polygon, polygons)\npolygon.name = \"Polygon\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is another possibility, using static methods\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "rect = shape.createRectangle(corner1=(400, 240), corner2=(470, 270), name=\"Rect\")\nellipse = shape.createEllipse(corner1=(-300, -300), corner2=(-140, -200), name=\"Ellipse\")\n# rectangle and ellipses can also be created with the arguments center and size:\nellipse = shape.createEllipse(\n center=(0.5 * (-300 - 140), 0.5 * (-300 - 200)),\n size=(-140 + 300, -200 + 300),\n name=\"Ellipse\",\n)\nsquare = shape.createSquare(center=(220, -300), sideLength=60, name=\"Square\")\ncircle = shape.createCircle(center=(-250, 100), radius=25, name=\"Circle\")\npolygons = np.array([[100, 200, 250, 200, 90], [50, 60, 100, 150, 150]])\npolygon = shape.createPolygon(polygons, name=\"Polygon\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Push all base shapes in the tuple base_shapes.\nThese base shapes can not be moved, rotated or resized (and thus not be selected).\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "base_shapes = [rect, ellipse, square, circle, polygon]\n\nfor b in base_shapes:\n b.flags = shape.MoveLock | shape.RotateLock | shape.ResizeLock\n\nall_shapes = base_shapes.copy()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Rotate all base shapes by 60 degree (around its center).\nThese rotated elements can only be rotated.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "rotated_shapes = []\nfor base_shape in base_shapes:\n temp = base_shape.copy()\n temp.rotateDeg(60)\n temp.flags = shape.MoveLock | shape.ResizeLock\n temp.name += \", 60\\xb0\"\n rotated_shapes.append(temp)\nall_shapes += rotated_shapes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Move all rotated shapes by dx = -70, dy = 100.\nThese objects can be rotated, resized and moved.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "rotated_translated_shapes = []\nfor rotated_shape in rotated_shapes:\n temp = rotated_shape.copy()\n temp.translate([-70, 100])\n temp.flags = 0\n temp.name += \",\\n dx:-70, dy:100\"\n rotated_translated_shapes.append(temp)\nall_shapes += rotated_translated_shapes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Display all shapes on the plot figure. \n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "h[\"geometricShapes\"] = all_shapes\n\n# create a mask object (uint8, masked pixels are set to 255, the rest is 0) from all shapes\nmask = dObj.createMask(all_shapes)\n\n# display the mask as overlay of the plot\nh[\"overlayImage\"] = mask" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n\n" ] } ], "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.10" } }, "nbformat": 4, "nbformat_minor": 0 }