Skip to content

Instantly share code, notes, and snippets.

@l0g1c-80m8
Last active January 26, 2024 22:16
Show Gist options
  • Save l0g1c-80m8/04059dffc2fcc8151e2e8d86c643a5f7 to your computer and use it in GitHub Desktop.
Save l0g1c-80m8/04059dffc2fcc8151e2e8d86c643a5f7 to your computer and use it in GitHub Desktop.
geometry-processing.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/l0g1c-80m8/04059dffc2fcc8151e2e8d86c643a5f7/geometryprocessing.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"source": [
"from google.colab import drive\n",
"drive.mount(\"/content/drive\", force_remount=True)\n",
"from PIL import Image\n",
"!pip install trimesh\n",
"!pip install open3d"
],
"metadata": {
"id": "gNVnImLlRmT4"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# **Introduction**\n",
"\n",
"# What is Geometry Processing, and why do we do it?\n",
"\n",
"Geometry processing involves applying an operation to a geometrical representation, often for the purpose of reconstruction, analysis, manipulation, and simulation. We can process a geometrical representation to add detail, remove detail, fix irregularities, modify its shape, convert it to a different representation, etc.\n",
"\n",
"Much of the time we want to process geometry as it does not suit our goals in its current form. Consider the Stanford Bunny:"
],
"metadata": {
"id": "_9h2qQe-MUzM"
}
},
{
"cell_type": "code",
"source": [
"im = Image.open(\"drive/MyDrive/bunny.png\")\n",
"im"
],
"metadata": {
"id": "ArBkVUa8R2u2"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Both of these models are derived from the same object, but each might be more/less useful in certain situations.\n",
"\n",
"As well, we might want to use different geometry representations for different reasons:"
],
"metadata": {
"id": "GXMJLWykPeG-"
}
},
{
"cell_type": "code",
"source": [
"im = Image.open(\"drive/MyDrive/bunny2.png\")\n",
"im"
],
"metadata": {
"id": "UelW2raUSXjy"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# Let's See some Examples!"
],
"metadata": {
"id": "J2UUFqlzU5Dh"
}
},
{
"cell_type": "markdown",
"source": [
"#Lets start with a mesh"
],
"metadata": {
"id": "7fPK3WqihkEy"
}
},
{
"cell_type": "code",
"source": [
"# prompt: code to open and display mesh in open3d\n",
"\n",
"import open3d as o3d\n",
"import trimesh\n",
"import numpy as np\n",
"\n",
"# Load a mesh from a file\n",
"mesh = o3d.io.read_triangle_mesh(\"drive/MyDrive/cow.obj\")\n",
"\n",
"# Visualize the mesh\n",
"print(mesh)\n",
"tmesh = trimesh.Trimesh(np.array(mesh.vertices),np.array(mesh.triangles))\n",
"tmesh.show()\n"
],
"metadata": {
"id": "t5vfCptKVUQX"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"#Let's *Simplify* this mesh"
],
"metadata": {
"id": "UT-OEa3Zhpbc"
}
},
{
"cell_type": "code",
"source": [
"# prompt: simplify mesh using o3d\n",
"\n",
"N = 1000\n",
"simplified_mesh = mesh.simplify_quadric_decimation(N)\n",
"print(simplified_mesh)\n",
"\n",
"tmesh = trimesh.Trimesh(np.array(simplified_mesh.vertices),np.array(simplified_mesh.triangles))\n",
"tmesh.show()\n"
],
"metadata": {
"id": "pudYk0XnWAiV"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"#Let's find the *convex hull*"
],
"metadata": {
"id": "urqx_Cbvh5Cs"
}
},
{
"cell_type": "code",
"source": [
"convex_mesh,_ = mesh.compute_convex_hull()\n",
"print(convex_mesh)\n",
"\n",
"tmesh = trimesh.Trimesh(np.array(convex_mesh.vertices),np.array(convex_mesh.triangles))\n",
"tmesh.show()"
],
"metadata": {
"id": "3RkpTMk-gcZj"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"#Let's *smooth* this mesh"
],
"metadata": {
"id": "ynhtMj6Bh__V"
}
},
{
"cell_type": "code",
"source": [
"# prompt: use filter smooth laplacian on mesh and show\n",
"\n",
"smooth_mesh = mesh.filter_smooth_laplacian(number_of_iterations=10)\n",
"smooth_mesh = mesh.filter_smooth_simple(number_of_iterations=10)\n",
"print(smooth_mesh)\n",
"\n",
"tmesh = trimesh.Trimesh(np.array(smooth_mesh.vertices),np.array(smooth_mesh.triangles))\n",
"tmesh.show()"
],
"metadata": {
"id": "EpL8RTA9hMB1"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"#Let's add more detail to this mesh through *subdivision*"
],
"metadata": {
"id": "D5YCCiB2iv1C"
}
},
{
"cell_type": "code",
"source": [
"# prompt: use loop subdivide on the mesh then display it\n",
"\n",
"subdivided_mesh = mesh.subdivide_loop(number_of_iterations=1)\n",
"print(subdivided_mesh)\n",
"\n",
"tmesh = trimesh.Trimesh(np.array(subdivided_mesh.vertices),np.array(subdivided_mesh.triangles))\n",
"tmesh.show()\n"
],
"metadata": {
"id": "gL1_5mhSi9wd"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"#Conclusion\n",
"\n",
"There are a *ton* of operations we can do here. Most of the operations we will see and use on geometry models will involve meshes, but you will sometimes see operations on pointclouds or other representations.\n",
"\n",
"Check out this link to see some common processing functions implemented by the Open3d library that you might need\n",
"\n",
"[Open3d Triangle Mesh Documentation](https://www.open3d.org/docs/release/python_api/open3d.geometry.TriangleMesh.html#open3d.geometry.TriangleMesh.filter_smooth_laplacian)"
],
"metadata": {
"id": "E-Jc823RiHpO"
}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment