Skip to content

Commit

Permalink
Added examples
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeBell committed Feb 21, 2025
1 parent ddce711 commit 4c24da0
Show file tree
Hide file tree
Showing 2 changed files with 217 additions and 0 deletions.
103 changes: 103 additions & 0 deletions ExampleMultipleSonarReadings.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from Raytrace.TriangleMesh import Ray\n",
"from Raytrace.BVHMesh import BVHMesh\n",
"from Raytrace.SideScan import SideScan, ScanReading\n",
"import numpy as np\n",
"import time"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"stl_path = 'sample_Hull_Mesh.stl'\n",
"start_time = time.time()\n",
"mesh = BVHMesh(stl_path = stl_path, min_node_size = 100)\n",
"end_time = time.time()\n",
"\n",
"print('Build Time:', end_time - start_time, 'seconds')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"facing = np.array([1, 0, 0])\n",
"min_angle = 0\n",
"max_angle = -np.pi/2\n",
"sample_ray_count = 1000\n",
"\n",
"origin_start = np.array([5, -1, 1])\n",
"origin_end = np.array([9, -1, 1])\n",
"readings_count = 100\n",
"# liniar interpolation between the two for this simple demo\n",
"origins = [origin_start * (1-i) + origin_end * i for i in np.arange(0, 1+1/(readings_count-1)/2, 1/(readings_count-1))]\n",
"\n",
"orientations = [Ray(facing, origin) for origin in origins]\n",
"\n",
"rays_list = [SideScan.generate_rays(orientation, min_angle, max_angle, sample_ray_count) for orientation in orientations]\n",
"\n",
"readings:list[ScanReading] = []\n",
"for n,rays in enumerate(rays_list):\n",
" print(n + 1, len(rays_list), sep='/', end='\\r')\n",
" readings.append(SideScan(mesh).scan_rays(rays))\n",
"\n",
"print()\n",
"\n",
"# 100 readings at 1000 rays each takes just over 2 minutes on my machine\n",
"print('Done')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import plotly.graph_objs as go\n",
"from Raytrace.PlotRays import plot_readings_heatmap"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plot_readings_heatmap(readings)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
114 changes: 114 additions & 0 deletions ExampleSingleSonarReading.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from Raytrace.TriangleMesh import Ray\n",
"from Raytrace.BVHMesh import BVHMesh\n",
"from Raytrace.SideScan import SideScan\n",
"import numpy as np\n",
"import time"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"stl_path = 'sample_Hull_Mesh.stl'\n",
"start_time = time.time()\n",
"mesh = BVHMesh(stl_path = stl_path, min_node_size = 100)\n",
"end_time = time.time()\n",
"\n",
"print('Build Time:', end_time - start_time, 'seconds')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"origin = np.array([5, -1, 1])\n",
"facing = np.array([1, 0, 0])\n",
"min_angle = 0\n",
"max_angle = -np.pi/2\n",
"sample_ray_count = 1000\n",
"\n",
"orientation = Ray(facing, origin)\n",
"\n",
"rays = SideScan.generate_rays(orientation, min_angle, max_angle, sample_ray_count)\n",
"\n",
"reading = SideScan(mesh).scan_rays(rays)\n",
"\n",
"print('Triangles:', mesh.triangles.shape[0])\n",
"reading.print_summary()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import plotly.graph_objs as go\n",
"from PlotRays import plot_mesh, plot_rays, plot_reading"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"mesh_plot = plot_mesh(mesh, color='lightpink', opacity=1)\n",
"rays_plot = plot_rays(reading, line={'color':'Blue'})"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig = go.Figure(data=[mesh_plot, rays_plot])\n",
"fig.update_layout(autosize=False, width=1000, height=500, margin=dict(l=20, r=20, t=20, b=20), scene = {'aspectmode':'data'})\n",
"fig.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plot_reading(reading)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 4c24da0

Please sign in to comment.