Skip to content

Instantly share code, notes, and snippets.

@jabooth
Created July 25, 2014 14:06
Show Gist options
  • Save jabooth/a045d5dfb33e54ad6b85 to your computer and use it in GitHub Desktop.
Save jabooth/a045d5dfb33e54ad6b85 to your computer and use it in GitHub Desktop.
How to filter a triangle list with a mask
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "",
"signature": "sha256:82e0b70f57890813f7621480dbf68475ce2b1e25a7310d8fe23080e7021dcc4c"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"import menpo\n",
"import numpy as np"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"trilist = np.array([0, 1, 2, 0, 3, 2, 2, 5, 4, 4, 5, 6]).reshape([4, 3])\n",
"points = np.random.rand(7, 3)\n",
"mask = np.array([True, False, True, True, True, True, True])"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print points"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[[ 0.81988993 0.71700209 0.50835648]\n",
" [ 0.79093236 0.36038279 0.84154055]\n",
" [ 0.22104768 0.03970721 0.4352194 ]\n",
" [ 0.25306475 0.81060459 0.05632058]\n",
" [ 0.00627229 0.03531831 0.72232639]\n",
" [ 0.27690353 0.82088957 0.90024519]\n",
" [ 0.34770358 0.62584806 0.22801873]]\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print trilist"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[[0 1 2]\n",
" [0 3 2]\n",
" [2 5 4]\n",
" [4 5 6]]\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# What point indices are left?\n",
"i = np.arange(points.shape[0])\n",
"i_left = i[mask]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# filter triangles removing any that contain a missing point.\n",
"tri_kept_mask = ~np.any(~np.in1d(trilist, i_left).reshape(trilist.shape), axis=1)\n",
"tri_kept = trilist[tri_kept_mask]\n",
"# only remaining indices in tri_kept survive\n",
"i_kept = np.sort(np.unique(tri_kept))"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# need to re-index. Build a new index\n",
"i_new = np.arange(i_kept.size)\n",
"# i_to_new[i] = i_new\n",
"i_to_new = i.copy()\n",
"i_to_new[i_kept] = i_new"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# use i_to_new, tri_kept to build new trilist and points\n",
"new_trilist = i_to_new[tri_kept.ravel()].reshape([-1, 3])\n",
"new_points = points[i_kept]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print new_trilist"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[[0 2 1]\n",
" [1 4 3]\n",
" [3 4 5]]\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print new_points"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[[ 0.81988993 0.71700209 0.50835648]\n",
" [ 0.22104768 0.03970721 0.4352194 ]\n",
" [ 0.25306475 0.81060459 0.05632058]\n",
" [ 0.00627229 0.03531831 0.72232639]\n",
" [ 0.27690353 0.82088957 0.90024519]\n",
" [ 0.34770358 0.62584806 0.22801873]]\n"
]
}
],
"prompt_number": 10
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment