{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Jupyter environment detected. Enabling Open3D WebVisualizer.\n", "[Open3D INFO] WebRTC GUI backend enabled.\n", "[Open3D INFO] WebRTCWindowSystem: HTTP handshake server disabled.\n", "(50, 3)\n" ] } ], "source": [ "import open3d as o3d\n", "import numpy as np\n", "\n", "GT = False\n", "\n", "if GT: ply_path = \"source.ply\"\n", "else: ply_path = \"target.ply\"\n", "pcd = o3d.io.read_point_cloud(ply_path)\n", "\n", "pcd_array = np.asarray(pcd.points)\n", "print(pcd_array.shape)\n", "\n", "o3d.visualization.draw_geometries([pcd])" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "CHECK_PERTURB = not GT\n", "\n", "def random_rotation_matrix():\n", " \"\"\"\n", " Generate a random 3x3 rotation matrix (SO(3) matrix).\n", " \n", " Uses the method described by James Arvo in \"Fast Random Rotation Matrices\" (1992):\n", " 1. Generate a random unit vector for rotation axis\n", " 2. Generate a random angle\n", " 3. Create rotation matrix using Rodriguez rotation formula\n", " \n", " Returns:\n", " numpy.ndarray: A 3x3 random rotation matrix\n", " \"\"\"\n", " # Generate random angle between 0 and 2π\n", " theta = np.random.uniform(0.5 * np.pi, np.pi)/5\n", " \n", " # Generate random unit vector for rotation axis\n", " phi = np.random.uniform(0, 2 * np.pi)/5\n", " cos_theta = np.random.uniform(-1, 1)\n", " sin_theta = np.sqrt(1 - cos_theta**2)\n", " \n", " axis = np.array([\n", " sin_theta * np.cos(phi),\n", " sin_theta * np.sin(phi),\n", " cos_theta\n", " ])\n", " \n", " # Normalize to ensure it's a unit vector\n", " axis = axis / np.linalg.norm(axis)\n", " \n", " # Create the cross-product matrix K\n", " K = np.array([\n", " [0, -axis[2], axis[1]],\n", " [axis[2], 0, -axis[0]],\n", " [-axis[1], axis[0], 0]\n", " ])\n", " \n", " # Rodriguez rotation formula: R = I + sin(θ)K + (1-cos(θ))K²\n", " R = (np.eye(3) + \n", " np.sin(theta) * K + \n", " (1 - np.cos(theta)) * np.dot(K, K))\n", " \n", " return R\n", "\n", "if CHECK_PERTURB:\n", " R_pert = random_rotation_matrix()\n", " t_pert = np.random.rand(3, 1)*3 #* 10\n", " perturbed_pcd_array = np.dot(R_pert, pcd_array.T).T + t_pert.T\n", "\n", " perturbed_pcd = o3d.geometry.PointCloud()\n", " perturbed_pcd.points = o3d.utility.Vector3dVector(perturbed_pcd_array)\n", "\n", " o3d.visualization.draw_geometries([perturbed_pcd])" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "def write_ply(points, output_path):\n", " \"\"\"\n", " Write points and parameters to a PLY file\n", " \n", " Parameters:\n", " points: numpy array of shape (N, 3) containing point coordinates\n", " output_path: path to save the PLY file\n", " \"\"\"\n", " with open(output_path, 'w') as f:\n", " # Write header\n", " f.write(\"ply\\n\")\n", " f.write(\"format ascii 1.0\\n\")\n", " \n", " # Write vertex element\n", " f.write(f\"element vertex {len(points)}\\n\")\n", " f.write(\"property float x\\n\")\n", " f.write(\"property float y\\n\")\n", " f.write(\"property float z\\n\")\n", " \n", " # Write camera element\n", " f.write(\"element camera 1\\n\")\n", " f.write(\"property float view_px\\n\")\n", " f.write(\"property float view_py\\n\")\n", " f.write(\"property float view_pz\\n\")\n", " f.write(\"property float x_axisx\\n\")\n", " f.write(\"property float x_axisy\\n\")\n", " f.write(\"property float x_axisz\\n\")\n", " f.write(\"property float y_axisx\\n\")\n", " f.write(\"property float y_axisy\\n\")\n", " f.write(\"property float y_axisz\\n\")\n", " f.write(\"property float z_axisx\\n\")\n", " f.write(\"property float z_axisy\\n\")\n", " f.write(\"property float z_axisz\\n\")\n", " \n", " # Write phoxi frame parameters\n", " f.write(\"element phoxi_frame_params 1\\n\")\n", " f.write(\"property uint32 frame_width\\n\")\n", " f.write(\"property uint32 frame_height\\n\")\n", " f.write(\"property uint32 frame_index\\n\")\n", " f.write(\"property float frame_start_time\\n\")\n", " f.write(\"property float frame_duration\\n\")\n", " f.write(\"property float frame_computation_duration\\n\")\n", " f.write(\"property float frame_transfer_duration\\n\")\n", " f.write(\"property int32 total_scan_count\\n\")\n", " \n", " # Write camera matrix\n", " f.write(\"element camera_matrix 1\\n\")\n", " for i in range(9):\n", " f.write(f\"property float cm{i}\\n\")\n", " \n", " # Write distortion matrix\n", " f.write(\"element distortion_matrix 1\\n\")\n", " for i in range(14):\n", " f.write(f\"property float dm{i}\\n\")\n", " \n", " # Write camera resolution\n", " f.write(\"element camera_resolution 1\\n\")\n", " f.write(\"property float width\\n\")\n", " f.write(\"property float height\\n\")\n", " \n", " # Write frame binning\n", " f.write(\"element frame_binning 1\\n\")\n", " f.write(\"property float horizontal\\n\")\n", " f.write(\"property float vertical\\n\")\n", " \n", " # End header\n", " f.write(\"end_header\\n\")\n", " \n", " # Write vertex data\n", " for point in points:\n", " f.write(f\"{point[0]} {point[1]} {point[2]}\\n\")\n", "\n", " print(True)\n", "\n", "if GT: write_ply(pcd_array, \"gt_filtered.ply\")\n", "else: write_ply(perturbed_pcd_array, \"noisy_filtered.ply\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "vision", "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.9.20" } }, "nbformat": 4, "nbformat_minor": 2 }