Skip to content

Instantly share code, notes, and snippets.

@stwind
Last active May 14, 2024 12:20
Show Gist options
  • Save stwind/b0c63bda7db5ac31becbac64bfa27f19 to your computer and use it in GitHub Desktop.
Save stwind/b0c63bda7db5ac31becbac64bfa27f19 to your computer and use it in GitHub Desktop.
libigl.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyNQxMBVI2Fx/cBuq2za8hPN",
"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/stwind/b0c63bda7db5ac31becbac64bfa27f19/libigl.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "jKjPXBf3jTAd",
"outputId": "3d62b705-234a-4e65-f517-c9a6c440ff55"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m16.2/16.2 MB\u001b[0m \u001b[31m33.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25h"
]
}
],
"source": [
"!pip install -Uq libigl"
]
},
{
"cell_type": "code",
"source": [
"!wget -qc --show-progress https://github.com/libigl/libigl-tutorial-data/raw/master/fandisk.off"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "OtBmpTgPjtEE",
"outputId": "41c4b53e-6bed-4d01-f6d6-abd3507baa8d"
},
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"\rfandisk.off 0%[ ] 0 --.-KB/s \rfandisk.off 100%[===================>] 397.76K --.-KB/s in 0.03s \n"
]
}
]
},
{
"cell_type": "code",
"source": [
"import numpy as np\n",
"import igl\n",
"\n",
"def tri_areas(tri):\n",
" pa, pb = tri[:,0] - tri[:,2], tri[:,1] - tri[:,2]\n",
" return np.linalg.norm(np.cross(pa, pb), axis=-1) * .5\n",
"\n",
"def normed(x):\n",
" return x / np.maximum(np.linalg.norm(x, axis=-1, keepdims=True), 1e-8)\n",
"\n",
"def get_angles(pi, pj, pk):\n",
" return np.arccos((normed(pj - pi) * normed(pk - pi)).sum(-1))"
],
"metadata": {
"id": "4uid_ltnj3Fj"
},
"execution_count": 3,
"outputs": []
},
{
"cell_type": "code",
"source": [
"v, f = igl.read_triangle_mesh(\"fandisk.off\")"
],
"metadata": {
"id": "adwzyfqTj9x6"
},
"execution_count": 4,
"outputs": []
},
{
"cell_type": "code",
"source": [
"n_face = igl.per_face_normals(v, f, v)\n",
"n_vert_uniform = igl.per_vertex_normals(v, f, igl.PER_VERTEX_NORMALS_WEIGHTING_TYPE_UNIFORM)\n",
"n_vert_area = igl.per_vertex_normals(v, f, igl.PER_VERTEX_NORMALS_WEIGHTING_TYPE_AREA)\n",
"n_vert_angle = igl.per_vertex_normals(v, f, igl.PER_VERTEX_NORMALS_WEIGHTING_TYPE_ANGLE)"
],
"metadata": {
"id": "_dliYo2QkAjN"
},
"execution_count": 5,
"outputs": []
},
{
"cell_type": "code",
"source": [
"np.testing.assert_allclose(tri_areas(v[f]) * 2, igl.doublearea(v, f))\n",
"np.testing.assert_allclose(\n",
" igl.internal_angles(v,f),\n",
" np.c_[get_angles(v[f[:,0]], v[f[:,1]], v[f[:,2]]),\n",
" get_angles(v[f[:,1]], v[f[:,2]], v[f[:,0]]),\n",
" get_angles(v[f[:,2]], v[f[:,0]], v[f[:,1]])])"
],
"metadata": {
"id": "LwTUgKbftYia"
},
"execution_count": 8,
"outputs": []
},
{
"cell_type": "code",
"source": [
"np.savez_compressed(\"fandisk\",\n",
" v=v.astype(np.float32),\n",
" f=f.astype(np.uint16),\n",
" n_face=n_face.astype(np.float32),\n",
" n_vert_uniform=n_vert_uniform.astype(np.float32),\n",
" n_vert_area=n_vert_area.astype(np.float32),\n",
" n_vert_angle=n_vert_angle.astype(np.float32),\n",
" centroids=v[f].mean(1).astype(np.float32),\n",
" doublearea=igl.doublearea(v, f).astype(np.float32),\n",
" internal_angles=igl.internal_angles(v,f).astype(np.float32)\n",
" )"
],
"metadata": {
"id": "LjXMugMbAHJJ"
},
"execution_count": 9,
"outputs": []
},
{
"cell_type": "code",
"source": [
"!ls -hal"
],
"metadata": {
"id": "LsTvbKDpF23N",
"outputId": "4f48dff7-7f48-4192-8fc6-3464e68246a8",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"execution_count": 10,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"total 1.2M\n",
"drwxr-xr-x 1 root root 4.0K May 14 12:19 .\n",
"drwxr-xr-x 1 root root 4.0K May 14 12:15 ..\n",
"drwxr-xr-x 4 root root 4.0K May 9 13:24 .config\n",
"-rw-r--r-- 1 root root 769K May 14 12:19 fandisk.npz\n",
"-rw-r--r-- 1 root root 398K May 14 12:19 fandisk.off\n",
"drwxr-xr-x 1 root root 4.0K May 9 13:24 sample_data\n"
]
}
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "gYdgBE_sIirw"
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment