Skip to content

Instantly share code, notes, and snippets.

@jabooth
Last active September 27, 2018 23:00
Show Gist options
  • Save jabooth/42809960be6482bd021cbc1256934d09 to your computer and use it in GitHub Desktop.
Save jabooth/42809960be6482bd021cbc1256934d09 to your computer and use it in GitHub Desktop.
Quick Guide to Barycentric Coords
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Given we have performed NICP...\n",
"src = TriMesh(...)\n",
"tgt = TexturedTrimesh(...) # in your case, as we are going to sample tcoords\n",
"\n",
"nicp_result = non_rigid_icp(src, tgt)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# rebuild a TriMesh from the result of the NICP using the from_vector machinery\n",
"result = src.from_vector(nicp_result['deformed_source'].flatten())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# view this to see the result\n",
"%matplotlib qt\n",
"result.view()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Now we want to describe where in the target mesh the deformed source\n",
"# is sampled from at every vertex. As we sample in triangles, the natural\n",
"# representation for this is a triangle index + barycentric coordinates for\n",
"# each vertex in result. Handily, we have methods for this on TriMesh\n",
"# in menpo3d.\n",
"#\n",
"# To use:\n",
"\n",
"bc, i = tgt.barycentric_coordinates_of_pointcloud(result)\n",
"\n",
"# These can then be used in \n",
"#\n",
"# TriMesh.project_barycentric_coordinates(bcoords, tri_indices)\n",
"#\n",
"# in order to 'resample' a PointCloud from a corresponding mesh.\n",
"#\n",
"# To make this point clear, (bc, i) is a representation of a pointcloud\n",
"# relative to a given triangulation. Between meshes that are *in correspondence*\n",
"# (bc, i) allows us to transfer a pointcloud between the two.\n",
"#\n",
"# In your case, you want to sample from the texture coordinates on a textured trimesh.\n",
"#\n",
"# That might look something like this:\n",
"\n",
"# Build a 2D TriMesh for the tcoords.\n",
"tcoords_trimesh = TriMesh(tgt.tcoords.points, tgt.trilist)\n",
"\n",
"# This trimmesh is in correspondence with tgt, so we can transfer our\n",
"# relative pointcloud using the BC coords + tri indices\n",
"\n",
"sampled_tcoords_pointcloud = tcoords_trimesh.project_barycentric_coordinates(bc, i)\n",
"\n",
"print('The sampled tcoords are:')\n",
"print(sampled_tcoords_pointcloud.points) "
]
}
],
"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.4.4"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment