{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import visualization\n",
    "from math import tan, atan, radians, degrees"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def construct_proj_matrix():\n",
    "    fov = 50.0\n",
    "    # for z coord\n",
    "    f = 1.5  # the near clip, but f in the book\n",
    "    n = 10003  # the far clip, somewhere in the ned of the world\n",
    "    # x coord\n",
    "    H = 1080\n",
    "    W = 1914\n",
    "    r = W * n * tan(radians(fov) / 2) / H\n",
    "    l = -r\n",
    "    # y coord\n",
    "    t = n * tan(radians(fov) / 2)\n",
    "    b = -t\n",
    "    # x00 = 2*n/(r-l)\n",
    "    x00 = H / (tan(radians(fov) / 2) * W)\n",
    "    # x11 = 2*n/(t-b)\n",
    "    x11 = 1 / tan(radians(fov) / 2)\n",
    "    return np.array([\n",
    "        [x00, 0, -(r + l) / (r - l), 0],\n",
    "        [0, x11, -(t + b) / (t - b), 0],\n",
    "        [0, 0, -f / (f - n), -f * n / (f - n)],\n",
    "        [0, 0, -1, 0],\n",
    "    ])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "ini_file = \"gta-postprocessing.ini\"\n",
    "visualization.multi_page = False\n",
    "visualization.ini_file = ini_file\n",
    "\n",
    "conn = visualization.get_connection()\n",
    "cur = conn.cursor()\n",
    "cur.execute(\"\"\"SELECT snapshot_id, imagepath, proj_matrix \\\n",
    "  FROM snapshots \\\n",
    "  WHERE run_id = 6\n",
    "  ORDER BY snapshot_id DESC \\\n",
    "  \"\"\")\n",
    "\n",
    "results = []\n",
    "for row in cur:\n",
    "    res = dict(row)\n",
    "    res['proj_matrix'] = np.array(res['proj_matrix'])\n",
    "    results.append(res)\n",
    "\n",
    "print('There are {} records'.format(len(results)))\n",
    "projs = [i['proj_matrix'] for i in results]\n",
    "\n",
    "# todo: estimate far plane by least squares. The rest is known. Before that, get rid of all outliers\n",
    "# x00 = proj[0, 0]\n",
    "# x11 = proj[1, 1]\n",
    "x22s = np.array([proj[2, 2] for proj in projs])\n",
    "x23s = np.array([proj[2, 3] for proj in projs])\n",
    "\n",
    "ns = -x23s / x22s\n",
    "fs = -x23s / (x22s - 1)\n",
    "\n",
    "# filtering values\n",
    "print('weird x22 value in proj: ')\n",
    "print([proj for proj in projs if abs(proj[2, 2] - (-1)) < 1e-4])\n",
    "\n",
    "# showing plots with values\n",
    "fig, axes = plt.subplots(1, 3)\n",
    "# axes[0].plot(x22s, np.zeros_like(x22s), 'x')\n",
    "axes[0].set_yscale('log', nonposy='clip')\n",
    "axes[0].set_title('$X_{22}$')\n",
    "axes[0].hist(x22s, bins=1000)\n",
    "\n",
    "axes[1].set_yscale('log', nonposy='clip')\n",
    "axes[1].set_title('$X_{23}$')\n",
    "axes[1].hist(x23s, bins=1000)\n",
    "\n",
    "axes[2].set_title('both')\n",
    "axes[2].set_xlabel('$X_{22}$')\n",
    "axes[2].set_ylabel('$X_{23}$')\n",
    "axes[2].loglog(x22s, x23s, 'o')\n",
    "plt.show()\n",
    "\n",
    "# print('n:', n)\n",
    "# print('f:', f)\n",
    "\n",
    "# fov = res['camera_fov']\n",
    "# print('fov: ', fov)\n",
    "# tan(radians(fov))\n",
    "# n = 1.5\n",
    "# t = n * tan(radians(fov) / 2)\n",
    "# print('t: ', t)\n",
    "# W = res['width']\n",
    "# H = res['height']\n",
    "# fov_h = fov * W / H\n",
    "# print('fov_h: ', fov_h)\n",
    "# r = n * tan(radians(fov_h) / 2)\n",
    "# print('r: ', r)\n",
    "# print('W: ', W)\n",
    "# print('H: ', H)\n",
    "# print('near clip: ', near_clip)\n",
    "# print('x00_c: ', 1 / tan(radians(fov_h) / 2))\n",
    "# print('x00_c2: ', H / (W * tan(radians(fov) / 2)))\n",
    "# print('x11_c: ', 1 / tan(radians(fov) / 2))\n",
    "# print('x00: ', x00)\n",
    "# print('x11: ', x11)\n",
    "#\n",
    "# fov_c_v = degrees(atan(1 / x11)) * 2\n",
    "# print('fov_c_v: ', fov_c_v)\n",
    "# fov_c_h = fov_c_v * W / H\n",
    "# print('fov_c_h: ', fov_c_h)\n",
    "# fov_c_h2 = degrees(atan(1 / x00)) * 2\n",
    "# print('fov_c_h2: ', fov_c_h2)\n",
    "#\n",
    "# H = res['height']\n",
    "# # H=res['ui_height']\n",
    "# print('for height {}, width is {}'.format(H, (fov_c_v * H) / fov_c_h2))\n",
    "# print('for height {}, width is {}'.format(H, (tan(radians(fov_c_h2)) * H) / tan(radians(fov_c_v))))\n",
    "# print('ui_width', res['ui_width'])\n",
    "# print('width', res['width'])\n",
    "#\n",
    "# print('aspect ration from matrix:', x11 / x00)\n",
    "# print('H/W:', H / W)\n",
    "# print('aspect ration from db:', res['width'] / res['height'])\n",
    "# print('aspect ration from ui_db:', res['ui_width'] / res['ui_height'])\n",
    "# print('width by db ratio:', (x11 / x00) * res['height'])\n"
   ]
  }
 ],
 "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",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
