{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2bc3579f-381e-4c88-bf83-547e17cfb91c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import PIL\n",
    "from PIL import Image\n",
    "import json\n",
    "import os\n",
    "from scipy.optimize import minimize\n",
    "from scipy.spatial import cKDTree"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "project_name = 'site1_large'\n",
    "\n",
    "img_checkerboard_set = []\n",
    "bim_checkerboard_set = []\n",
    "\n",
    "chessboard_json_name = 'checkerboard_position_site.json'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "30871ed1",
   "metadata": {},
   "outputs": [],
   "source": [
    "convert_transform = [[7.932288, -1.635200, 0.009167, -270.570648],\n",
    "                    [1.635213, 7.932279, -0.013477, -19.173040],\n",
    "                    [-0.006257, 0.015051, 8.099068, 159.666779],\n",
    "                    [0.000000, 0.000000, 0.000000, 1.000000]]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "39894394",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pred Camera Coordinate: [-129.74430505  -28.86012319    1.22925065]\n",
      "gt Camera Coordinate: [-129.5, -28.8, 1.25, 1.0]\n"
     ]
    }
   ],
   "source": [
    "# Validate\n",
    "camera_point_gt = [-129.5, -28.8, 1.25, 1.0]\n",
    "bim_point = [-1252.5365, -460.27573, 170.0, 1.0]\n",
    "\n",
    "# Compute the inverse of the transformation matrix\n",
    "inverse_transform = np.linalg.inv(np.array(convert_transform))\n",
    "\n",
    "# Perform the inverse transformation\n",
    "camera_point_homogeneous = np.dot(inverse_transform, bim_point)\n",
    "\n",
    "# Convert back from homogeneous coordinates\n",
    "camera_point_cartesian = camera_point_homogeneous[:3] \n",
    "\n",
    "print(\"pred Camera Coordinate:\", camera_point_cartesian)\n",
    "print(\"gt Camera Coordinate:\", camera_point_gt)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a455bd4",
   "metadata": {},
   "source": [
    "# Save Transform"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "BIM_camera_transform = {\"b2c_transform\": list(list(row) for row in inverse_transform)}\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define the filename\n",
    "filename = os.path.join(project_name,'transform_matrix.json')\n",
    "\n",
    "# Write the updated dictionary back to the JSON file\n",
    "with open(filename, 'w') as f:\n",
    "    json.dump(BIM_camera_transform, f, indent=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "aeb44b34",
   "metadata": {},
   "source": [
    "## Change transform matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "ff3f67dd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Transformation applied and updated JSON saved as 'transform_updated.json'.\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "import numpy as np\n",
    "\n",
    "# New transformation matrix\n",
    "convert_transform = np.array([\n",
    "    [7.932288, -1.635200, 0.009167, -270.570648],\n",
    "    [1.635213, 7.932279, -0.013477, -19.173040],\n",
    "    [-0.006257, 0.015051, 8.099068, 159.666779],\n",
    "    [0.000000, 0.000000, 0.000000, 1.000000]\n",
    "])\n",
    "\n",
    "# Function to apply the transformation\n",
    "def apply_transformation(transform_matrix, convert_transform):\n",
    "    original_matrix = np.array(transform_matrix)\n",
    "    \n",
    "    transformed_matrix = np.dot(convert_transform, original_matrix)\n",
    "    \n",
    "    return transformed_matrix\n",
    "\n",
    "# Load the JSON data\n",
    "with open('./site1_large/transforms.json', 'r') as f:\n",
    "    data = json.load(f)\n",
    "\n",
    "# Apply the transformation to each frame\n",
    "for frame in data['frames']:\n",
    "    transform_matrix = np.array(frame['transform_matrix'])\n",
    "    updated_matrix = apply_transformation(transform_matrix, convert_transform)\n",
    "    frame['transform_matrix'] = updated_matrix.tolist()\n",
    "\n",
    "# Save the updated JSON data\n",
    "with open('transform_updated.json', 'w') as f:\n",
    "    json.dump(data, f, indent=4)\n",
    "\n",
    "print(\"Transformation applied and updated JSON saved as 'transform_updated.json'.\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "77245c2c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "nerfstudio2",
   "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.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
