{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Quick run through example\nThis example will show the basic commands required to run the 2d trajectory \nmatching and 3D position triangulation.\n\nAuthor: Thejasvi Beleyur, March 2022\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import uuid\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nfrom track2trajectory.synthetic_data import generate_two_synthetic_cameras_version2\nfrom track2trajectory.synthetic_data import make_brownian_particles\nfrom track2trajectory.projection import project_to_2d_and_3d, calcFundamentalMatrix\nfrom track2trajectory.match3d import match_2dpoints_to_3dtrajectories"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Generating synthetic data\nLet's first generate 1) 2 cameras located at `xyz` [-1,0,0] and [1,0,0].\nand 2) calculate the fundmental matrix mapping points between the two cameras.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "cam1, cam2 = generate_two_synthetic_cameras_version2()\nF = calcFundamentalMatrix(cam1, cam2)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "two cameras - such that the particles stay within the common fields of view\nof both cameras most of the time. \n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "num_particles  = 10\nstepsize = 0.1 # in m\nbounding_box = [[-4,4],\n                [7,10],\n                [-1,1]] # set the XYZ min-max limits for the bounding box\nxyz_particles = make_brownian_particles(num_particles, bounding_box)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "cam1_2dpoints, cam1_fails = project_to_2d_and_3d(xyz_particles, cam1)\ncam2_2dpoints, cam2_fails = project_to_2d_and_3d(xyz_particles, cam2)\n# Change the object IDs on cam2 - just to show that the object IDs can be \n# any type alphanumeric code. \n\nnew_codes = [str(uuid.uuid4())[-4:] for each in range(num_particles)]\nreplacement = { each:new_codes[i]  for i,each in enumerate(cam2_2dpoints['oid'].unique())}\ncam2_2dpoints['oid'] = cam2_2dpoints['oid'].replace(replacement)\n\ncam2_2dpoints"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Running trajectory matching\nLet's now run the :code:`match_2dpoints_to_3dtrajectories` function, which\nchecks for correspondences between points on each camera. If a correspondence\nis found, then a 3D point is calculated from it.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "threed_matches = match_2dpoints_to_3dtrajectories(cam1, cam2, \n                                                  cam1_2dpoints,\n                                                  cam2_2dpoints, F)\n\nthreed_matches"
      ]
    }
  ],
  "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.7.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}