File size: 7,302 Bytes
5236ead
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
{
 "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
}