Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jabooth/8af674f11e7fec9ecdfb3af37521742e to your computer and use it in GitHub Desktop.
Save jabooth/8af674f11e7fec9ecdfb3af37521742e to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 72,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np\n",
"from menpo.shape import TriMesh\n",
"\n",
"def duplicate_vertices(mesh):\n",
" # generate a new mesh with unique vertices per triangle\n",
" # (i.e. duplicate verts so that each triangle is unique) old_to_new = mesh.trilist.ravel()\n",
" old_to_new = mesh.trilist.ravel()\n",
" new_trilist = np.arange(old_to_new.shape[0]).reshape([-1, 3])\n",
" new_points = mesh.points[old_to_new]\n",
" return TriMesh(new_points, trilist=new_trilist), old_to_new"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import menpo3d.io as m3io\n",
"\n",
"mesh = m3io.import_builtin_asset.james_obj()"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14.]\n"
]
}
],
"source": [
"# switch this out for your unwrapped one!\n",
"mesh_unwrapped = mesh\n",
"\n",
"# and do your logic here...\n",
"id_per_vertex = np.floor(mesh_unwrapped.points[:, 0] / 25)\n",
"id_per_vertex = id_per_vertex - min(id_per_vertex)\n",
"print(np.unique(id_per_vertex))"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# bin the meshes based on the first vertex of each triangle\n",
"v1 = mesh.trilist[:, 0]\n",
"id_per_tri = id_per_vertex[v1]"
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"TriMesh: n_points: 205458, n_dims: 3, n_tris: 68486\n"
]
}
],
"source": [
"mesh_uniq_verts, vertex_map = duplicate_vertices(mesh)\n",
"print(mesh_uniq_verts)"
]
},
{
"cell_type": "code",
"execution_count": 77,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# we know trilist is of form\n",
"# 0, 1, 2\n",
"# 3, 4, 5...\n",
"# -> first 3 vertices are first tri\n",
"# etc etc\n",
"id_per_v_per_t = np.repeat(id_per_tri, 3)\n",
"\n",
"# use this guy in the rasterizer!\n",
"f3v = np.tile(id_per_v_per_t, (3, 1)).T"
]
},
{
"cell_type": "code",
"execution_count": 78,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from menpo3d.rasterize import GLRasterizer\n",
"\n",
"# your rasterizer here sir...\n",
"rasterizer = GLRasterizer()\n",
"rgb_image, id_image = rasterizer.rasterize_mesh_with_f3v_interpolant(mesh_uniq_verts, per_vertex_f3v=f3v)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment