{
  "nbformat_minor": 0, 
  "nbformat": 4, 
  "cells": [
    {
      "execution_count": null, 
      "cell_type": "code", 
      "source": [
        "%matplotlib inline"
      ], 
      "outputs": [], 
      "metadata": {
        "collapsed": false
      }
    }, 
    {
      "source": [
        "\nEllipsoid example\n-----------------\n\nUpdating the size and shape of an object interactively\n\n"
      ], 
      "cell_type": "markdown", 
      "metadata": {}
    }, 
    {
      "execution_count": null, 
      "cell_type": "code", 
      "source": [
        "from __future__ import absolute_import\nfrom soma import aims\nimport anatomist.direct.api as anatomist\nimport time\nimport numpy\nimport sys\nfrom six.moves import range\n\n\na = anatomist.Anatomist()\n\n\ndef rotation_3d(a1, a2, a3):\n    '''\n    Return matrix 4x4 of rotation along 3 canonic axis based on 3 angles.\n\n    - a1, a2, a3 : rotation angles.\n    '''\n    c1, s1 = numpy.cos(a1), numpy.sin(a1)\n    c2, s2 = numpy.cos(a2), numpy.sin(a2)\n    c3, s3 = numpy.cos(a3), numpy.sin(a3)\n    m1 = numpy.matrix([\n        [c1, -s1, 0, 0],\n        [s1, c1,  0, 0],\n        [0,   0,  1, 0],\n        [0,   0,  0, 1]])\n    m2 = numpy.matrix([\n        [1,  0,   0, 0],\n        [0, c2, -s2, 0],\n        [0, s2,  c2, 0],\n        [0,  0,   0, 1]])\n    m3 = numpy.matrix([\n        [c3, 0, -s3, 0],\n        [0,  1,   0, 0],\n        [s3, 0,  c3, 0],\n        [0,  0,   0, 1]])\n    return m1 * m2 * m3\n\n\ndef apply_transform(mesh, scaling, rot, translate):\n    '''\n    Transform mesh with scaling, translation and rotation matrix.\n    '''\n    mesh2 = aims.AimsTimeSurface_3(mesh)\n    # create transformation\n    scale = numpy.asmatrix(numpy.diag(scaling))\n    rot[:, 3] = numpy.asmatrix(translate).T\n    cov = rot * scale * rot.I\n    # apply transformation\n    motion = aims.Motion(numpy.asarray(cov).flatten())\n    aims.SurfaceManip.meshTransform(mesh2, motion)\n    # update mesh in anatomist object\n    asphere.setSurface(mesh2)\n\n\ndef transform(mesh, angles, scaling, translate):\n    '''\n    Create and apply transformation to mesh.\n    '''\n    angles[0] += 0.02\n    angles[1] += 0.03\n    angles[2] += 0.05\n    a1, a2, a3 = angles\n    scaling = numpy.array([1.1 + numpy.cos(a1 * 3), 1.1 + numpy.cos(a2 * 2),\n                           1.1 + numpy.cos(a3), 0]) * 0.5\n    scaling[3] = 1.\n    rot = rotation_3d(a1, a2, a3)\n    apply_transform(mesh, scaling, rot, translate)\n\n\nmesh = aims.SurfaceGenerator.sphere(aims.Point3df(0, 0, 0), 1, 500)\nasphere = a.toAObject(mesh)\nasphere.releaseAppRef()\naw = a.createWindow('3D')\naw.setHasCursor(0)\na.addObjects([asphere], [aw])\n\n\nif __name__ == '__main__':\n    from soma.qt_gui import qt_backend\n    from soma.qt_gui.qt_backend import QtGui\n    angles = numpy.array([0., 0., 0.])\n    scaling = numpy.array([1., 1., 1., 0.])\n    translate = numpy.array([0, 0, 0, 1])\n    if 'sphinx_gallery' in sys.modules:\n        for i in range(90):\n            transform(mesh, angles, scaling, translate)\n        asphere.setChanged()\n        asphere.notifyObservers()\n        QtGui.qApp.processEvents()\n        # display in matplotlib for sphinx_gallery\n        import matplotlib\n        matplotlib.use('agg', force=True)  # force agg\n        aw.imshow(show=True)\n\n    else:\n        while a.getControlWindow().isVisible():\n            transform(mesh, angles, scaling, translate)\n            asphere.setChanged()\n            asphere.notifyObservers()\n            QtGui.qApp.processEvents()\n            time.sleep(0.01)\n\n    del aw, asphere, mesh"
      ], 
      "outputs": [], 
      "metadata": {
        "collapsed": false
      }
    }
  ], 
  "metadata": {
    "kernelspec": {
      "display_name": "Python 2", 
      "name": "python2", 
      "language": "python"
    }, 
    "language_info": {
      "mimetype": "text/x-python", 
      "nbconvert_exporter": "python", 
      "name": "python", 
      "file_extension": ".py", 
      "version": "2.7.17", 
      "pygments_lexer": "ipython2", 
      "codemirror_mode": {
        "version": 2, 
        "name": "ipython"
      }
    }
  }
}