{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [],
   "source": [
    "from nerfstudio.cameras import camera_utils\n",
    "\n",
    "import torch\n",
    "import numpy as np\n",
    "import json\n",
    "import copy\n",
    "import math"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load transformation matrices\n",
    "transform_matrix = './site1_large/transform_matrix.json'\n",
    "with open(transform_matrix, 'r') as file:\n",
    "    t_matrix = json.load(file)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# BIM to NeRF"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [],
   "source": [
    "bim_path = './site1_large/bim_camera_path.json'\n",
    "with open(bim_path, 'r') as file:\n",
    "    bim_camera = json.load(file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [],
   "source": [
    "camera_world_transforms = []\n",
    "\n",
    "for frame in bim_camera[\"frames\"]:\n",
    "    bim_transform_4x4 = np.array(frame['transform_matrix'])\n",
    "\n",
    "    matrix_1 = np.array(bim_transform_4x4)\n",
    "\n",
    "    # fix the rotation matrix\n",
    "    matrix_1[:3,:3] = np.array([[ 4.95865612e-16,  5.72622142e+00,  5.72622142e+00],\n",
    "       [ 8.09810000e+00, -3.50629937e-16, -3.50629937e-16],\n",
    "       [ 0.00000000e+00,  5.72622142e+00, -5.72622142e+00]])\n",
    "\n",
    "    matrix_2 = np.array(t_matrix['b2c_transform'])\n",
    "\n",
    "    camera_world_transform_4x4 = np.dot(matrix_2, matrix_1)\n",
    "    \n",
    "    t = camera_world_transform_4x4[:3, 3]\n",
    "\n",
    "    camera_world_transform_4x4[0, 3] = float(-t[0])\n",
    "    camera_world_transform_4x4[1, 3] = float(-t[1])\n",
    "    camera_world_transform_4x4[2, 3] = float(t[2])\n",
    "\n",
    "    camera_world_transforms.append(list(list(row) for row in camera_world_transform_4x4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [],
   "source": [
    "normed_camera_world_transforms = []\n",
    "\n",
    "for transform in camera_world_transforms:\n",
    "\n",
    "    matrix_1 = np.array(transform)\n",
    "    matrix_2 = np.array(t_matrix['c2n_transform'])\n",
    "\n",
    "    normed_camera_world_transform_4x4 = np.dot(matrix_2, matrix_1)\n",
    "    normed_camera_world_transform_4x4[:3, 3] *= t_matrix['norm_scale']\n",
    "\n",
    "    normed_camera_world_transforms.append(list(list(row) for row in normed_camera_world_transform_4x4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [],
   "source": [
    "nerf_template = './nerf_template.json'\n",
    "with open(nerf_template, 'r') as file:\n",
    "    tmp = json.load(file)\n",
    "\n",
    "\n",
    "stretched_matrix = np.array(normed_camera_world_transforms).reshape(-1,16)\n",
    "\n",
    "fps = 1.0\n",
    "seconds = math.ceil(len(stretched_matrix) / fps)\n",
    "\n",
    "\n",
    "keyframes = []\n",
    "for matrix in stretched_matrix:\n",
    "    temp_dic = {}\n",
    "    temp_dic[\"matrix\"] = list(matrix)\n",
    "    temp_dic[\"fov\"] = 95.2\n",
    "    temp_dic[\"aspect\"] = 0.5625\n",
    "    # temp_dic[\"aspect\"] = 1.777777\n",
    "    temp_dic[\"override_transition_enabled\"] = False\n",
    "    temp_dic[\"override_transition_sec\"] = None\n",
    "\n",
    "    keyframes.append(temp_dic)\n",
    "\n",
    "camera_path = []\n",
    "for matrix in stretched_matrix:\n",
    "    temp_dic = {}\n",
    "    temp_dic[\"camera_to_world\"] = list(matrix)\n",
    "    temp_dic[\"fov\"] = 95.2\n",
    "    temp_dic[\"aspect\"] = 0.5625\n",
    "    # temp_dic[\"aspect\"] = 1.777777\n",
    "\n",
    "    camera_path.append(temp_dic)\n",
    "\n",
    "tmp[\"default_fov\"] = 95.2\n",
    "tmp[\"fps\"] = fps\n",
    "tmp[\"seconds\"] = seconds\n",
    "tmp[\"keyframes\"] = keyframes\n",
    "tmp[\"camera_path\"] = camera_path\n",
    "\n",
    "tmp[\"render_height\"] = 1920\n",
    "tmp[\"render_width\"] = 1080\n",
    "\n",
    "# Write the updated dictionary back to the JSON file\n",
    "nerf_template = './site1_large/nerf_camera_path2.json'\n",
    "with open(nerf_template, 'w') as f:\n",
    "    json.dump(tmp, f, indent=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [],
   "source": [
    "# nerf_template = './nerf_template.json'\n",
    "# with open(nerf_template, 'r') as file:\n",
    "#     tmp = json.load(file)\n",
    "\n",
    "\n",
    "# stretched_matrix = np.array(normed_camera_world_transforms).reshape(-1,16)\n",
    "\n",
    "# fps = 1.0\n",
    "# seconds = math.ceil(len(stretched_matrix) / fps)\n",
    "\n",
    "\n",
    "# keyframes = []\n",
    "# for matrix in stretched_matrix:\n",
    "#     temp_dic = {}\n",
    "#     temp_dic[\"matrix\"] = list(matrix)\n",
    "#     temp_dic[\"fov\"] = 95.2\n",
    "#     # temp_dic[\"aspect\"] = 0.5625\n",
    "#     temp_dic[\"aspect\"] = 1.777777\n",
    "#     temp_dic[\"override_transition_enabled\"] = False\n",
    "#     temp_dic[\"override_transition_sec\"] = None\n",
    "\n",
    "#     keyframes.append(temp_dic)\n",
    "\n",
    "# camera_path = []\n",
    "# for matrix in stretched_matrix:\n",
    "#     temp_dic = {}\n",
    "#     temp_dic[\"camera_to_world\"] = list(matrix)\n",
    "#     temp_dic[\"fov\"] = 95.2\n",
    "#     # temp_dic[\"aspect\"] = 0.5625\n",
    "#     temp_dic[\"aspect\"] = 1.777777\n",
    "\n",
    "#     camera_path.append(temp_dic)\n",
    "\n",
    "# tmp[\"default_fov\"] = 95.2\n",
    "# tmp[\"fps\"] = fps\n",
    "# tmp[\"seconds\"] = seconds\n",
    "# tmp[\"keyframes\"] = keyframes\n",
    "# tmp[\"camera_path\"] = camera_path\n",
    "\n",
    "# tmp[\"render_height\"] = 1080\n",
    "# tmp[\"render_width\"] = 1920\n",
    "\n",
    "# # Write the updated dictionary back to the JSON file\n",
    "# nerf_template = './site1_large/nerf_camera_path2.json'\n",
    "# with open(nerf_template, 'w') as f:\n",
    "#     json.dump(tmp, f, indent=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# NeRF to BIM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load NeRF Render\n",
    "normed_camera_world_transforms = '/home/worker/yt/autocon/dataset/site1_large/camera_paths/minus_x_view.json'\n",
    "# normed_camera_world_transforms = '/home/worker/yt/autocon/dataset/site1_large/camera_paths/nerf_camera_path.json'\n",
    "with open(normed_camera_world_transforms, 'r') as file:\n",
    "    normed_path = json.load(file)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Norm to Camera\n",
    "camera_world_transforms = []\n",
    "\n",
    "for frame in normed_path['camera_path']:\n",
    "    normed_transform_4x4 = np.array(frame['camera_to_world']).reshape(4,4)\n",
    "    normed_transform_4x4[:3, 3] /= t_matrix['norm_scale']\n",
    "\n",
    "    # normed_transform_4x4 = np.vstack([normed_transform_3x4, [0., 0., 0., 1.0]])\n",
    "\n",
    "    matrix_1 = np.linalg.inv(np.array(t_matrix['c2n_transform']))\n",
    "    matrix_2 = normed_transform_4x4\n",
    "\n",
    "    camera_world_transform_4x4 = np.dot(matrix_1, matrix_2)\n",
    "    camera_world_transforms.append(list(list(row) for row in camera_world_transform_4x4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# Camera to BIM\n",
    "bim_transforms = []\n",
    "\n",
    "for transform in camera_world_transforms:\n",
    "    camera_world_transform_4x4 = np.array(transform)\n",
    "\n",
    "    t = camera_world_transform_4x4[:3, 3]\n",
    "\n",
    "    camera_world_transform_4x4[0, 3] = float(-t[0])\n",
    "    camera_world_transform_4x4[1, 3] = float(-t[1])\n",
    "    camera_world_transform_4x4[2, 3] = float(t[2])\n",
    "\n",
    "    # Compute the inverse of b2c_transform\n",
    "    b2c_transform_inverse = np.linalg.inv(np.array(t_matrix['b2c_transform']))\n",
    "\n",
    "    # Apply the inverse of b2c_transform\n",
    "    bim_transform_4x4_reversed = np.dot(b2c_transform_inverse, camera_world_transform_4x4)\n",
    "\n",
    "    # Append the reversed transformation for verification\n",
    "    bim_transforms.append(list(list(row) for row in bim_transform_4x4_reversed))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define the filename\n",
    "bim_path_temp_name = 'bim_path_template.json'\n",
    "\n",
    "# Write the updated dictionary back to the JSON file\n",
    "with open(bim_path_temp_name, 'r') as f:\n",
    "    bim_path_temp = json.load(f)\n",
    "\n",
    "new_frames = []\n",
    "\n",
    "for matrix in bim_transforms:\n",
    "    temp_dic = {}\n",
    "    temp_dic[\"transform_matrix\"] = matrix\n",
    "    new_frames.append(temp_dic)\n",
    "\n",
    "bim_path_temp[\"frames\"] = new_frames"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write the updated dictionary back to the JSON file\n",
    "bim_path_name = './site1_large/bim_camera_path.json'\n",
    "with open(bim_path_name, 'w') as f:\n",
    "    json.dump(bim_path_temp, f, indent=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
