pyFM.spectral.projection_utils

Note

Python implementation of:

[1] - “Deblurring and Denoising of Maps between Shapes”, by Danielle Ezuz and Mirela Ben-Chen.

Functions

barycentric_to_precise(faces, face_match, ...)

Transforms set of barycentric coordinates into a precise map

compute_Deltamin(vert_emb, points_emb[, n_jobs])

For each point in the pointcloud gives the distance to the nearest vertex on the mesh.

compute_all_dmin(vert_emb, faces, points_emb)

For each vertex in the pointcloud and each face on the surface, gives the minimum distance to between the vertex and each of the 3 points of the triangle.

compute_dmin(vert_emb, faces, points_emb, ...)

Given a vertex in the pointcloud and each face on the surface, gives the minimum distance to between the vertex and each of the 3 points of the triangle.

compute_lmax(vert_emb, faces)

Computes the maximum edge length

mycdist(X, Y[, sqnormX, sqnormY, squared])

Compute pairwise euclidean distance between two collections of vectors in a k-dimensional space

pointTriangleDistance(TRI, P[, return_bary])

Computes distance between a point and a triangle in a p-dimensional space

point_to_triangles_projection(triangles, point)

This functions projects a p-dimensional point on each of the given p-dimensional triangle.

project_pc_to_triangles(vert_emb, faces, ...)

Project a pointcloud on a set of triangles in p-dimension.

project_to_mesh(vert_emb, faces, points_emb, ...)

Project a pointcloud on a p-dimensional triangle mesh

pyFM.spectral.projection_utils.project_pc_to_triangles(vert_emb, faces, points_emb, precompute_dmin=True, batch_size=None, n_jobs=1, verbose=False)

Project a pointcloud on a set of triangles in p-dimension. Projection is defined as barycentric coordinates on one of the triangle. Line i for the output has 3 non-zero values at indices j,k and l of the vertices of the triangle point i zas projected on.

Parameters:
  • vert_emb – (n1, p) coordinates of the mesh vertices

  • faces – (m1, 3) faces of the mesh defined as indices of vertices

  • points_emb – (n2, p) coordinates of the pointcloud

  • precompute_dmin

    Whether to precompute all the values of delta_min.

    Faster but heavier in memory.

  • batch_size – If precompute_dmin is False, projects batches of points on the surface

  • n_jobs – number of parallel process for nearest neighbor precomputation

Returns:

precise_map – (n2,n1) - precise point to point map.

Return type:

scipy.sparse.csr_matrix

pyFM.spectral.projection_utils.compute_lmax(vert_emb, faces)

Computes the maximum edge length

Parameters:
  • vert_emb – (n1, p) coordinates of the mesh vertices

  • faces – (m1, 3) faces of the mesh defined as indices of vertices

Returns:

lmax – (m1,) maximum edge length

Return type:

np.ndarray

pyFM.spectral.projection_utils.compute_Deltamin(vert_emb, points_emb, n_jobs=1)

For each point in the pointcloud gives the distance to the nearest vertex on the mesh.

Compute Delta_min for each vertex in the target shape \(\min_{v_2} \|A_{v_2,*} - b\|_2\) with notations from “Deblurring and Denoising of Maps between Shapes”.

Corresponds to nearest neighbor seach.

Parameters:
  • vert_emb – (n1, p) coordinates of the mesh vertices

  • points_emb – (n2, p) coordinates of the pointcloud

  • n_jobs – number of paraller processes

Returns:

Delta_min – (n2,) Delta_min for each vertex on the target shape

Return type:

np.ndarray

pyFM.spectral.projection_utils.mycdist(X, Y, sqnormX=None, sqnormY=None, squared=False)

Compute pairwise euclidean distance between two collections of vectors in a k-dimensional space

Parameters:
  • X – (n1, k) first collection

  • Y – (n2, k) second collection or (k,) if single point

  • squared (bool) – whether to compute the squared euclidean distance

Returns:

distmat – (n1, n2) or (n2,) distance matrix

Return type:

np.ndarray

pyFM.spectral.projection_utils.compute_dmin(vert_emb, faces, points_emb, vertind, vert_sqnorms=None, points_sqnorm=None)

Given a vertex in the pointcloud and each face on the surface, gives the minimum distance to between the vertex and each of the 3 points of the triangle.

For a given face on the source shape and vertex on the target shape: \(\delta_min = \min_{i=1\cdots 3} \|A_{c_i,*} - b\|_2\) with notations from “Deblurring and Denoising of Maps between Shapes”.

Parameters:
  • vert_emb – (n1, p) coordinates of the mesh vertices

  • faces – (m1, 3) faces of the mesh defined as indices of vertices

  • points_emb – (n2, p) coordinates of the pointcloud

  • vertind – index of the vertex for which to compute dmin

  • vert_sqnorm – (n1,) squared norm of each vertex

  • points_sqnorm – (n2,) squared norm of each point

Returns:

delta_min – (m1,n2) delta_min for each face on the source shape.

Return type:

np.ndarray

pyFM.spectral.projection_utils.compute_all_dmin(vert_emb, faces, points_emb, vert_sqnorm=None, points_sqnorm=None)

For each vertex in the pointcloud and each face on the surface, gives the minimum distance to between the vertex and each of the 3 points of the triangle.

For a given face on the source shape and vertex on the target shape: \(\delta_min = \min_{i=1\cdots 3} \|A_{c_i,*} - b\|_2\) with notations from “Deblurring and Denoising of Maps between Shapes”.

Parameters:
  • vert_emb – (n1, p) coordinates of the mesh vertices

  • faces – (m1, 3) faces of the mesh defined as indices of vertices

  • points_emb – (n2, p) coordinates of the pointcloud

  • vert_sqnorm – (n1,) squared norm of each vertex

  • points_sqnorm – (n2,) squared norm of each point

Returns:

delta_min – (m1,n2) delta_min for each face on the source shape.

Return type:

np.ndarray

pyFM.spectral.projection_utils.project_to_mesh(vert_emb, faces, points_emb, vertind, lmax, Deltamin, dmin=None, dmin_params=None)

Project a pointcloud on a p-dimensional triangle mesh

Parameters:
  • vert_emb – (n1, p) coordinates of the mesh vertices

  • faces – (m1, 3) faces of the mesh defined as indices of vertices

  • points_emb – (n2, p) coordinates of the pointcloud

  • vertind (int) – index of the vertex to project

  • lmax – (m1,) value of lmax (max edge length for each face)

  • Deltamin – (n2,) value of Deltamin (distance to nearest vertex)

  • dmin

    (m1,n2) - optional - values of dmin (distance to the nearest vertex of each face

    for each vertex). Can be computed on the fly

  • dmin_params (dict, optional) –

    if dmin is None, stores ‘vert_sqnorm’ a (n1,) array of squared norms

    of vertices embeddings, and ‘points_sqnorm’ a (n2,) array of squared norms of points embeddings. Helps speed up computation of dmin

Returns:

  • min_faceind (int) – index of the face on which the vertex is projected

  • min_bary (np.ndarray) – (3,) - barycentric coordinates on the chosen face

pyFM.spectral.projection_utils.barycentric_to_precise(faces, face_match, bary_coord, n_vertices=None)

Transforms set of barycentric coordinates into a precise map

Parameters:
  • faces – (m,3) - Set of faces defined by index of vertices.

  • face_match – (n2,) - indices of the face assigned to each point

  • bary_coord – (n2,3) - barycentric coordinates of each point within the face

  • n_vertices (int) – number of vertices in the target mesh (on which faces are defined)

Returns:

precise_map – (n2,n1) - precise point to point map

Return type:

scipy.sparse.csr_matrix

pyFM.spectral.projection_utils.point_to_triangles_projection(triangles, point, return_bary=False)

This functions projects a p-dimensional point on each of the given p-dimensional triangle.

This is a parallelized version of pointTriangleDistance.

All operations are parallelized, which makes the code quite hard to read. For an easier take, follow the code in the function below (not written by me) for projection on a single triangle.

The algorithm is based on [1]

The algorithm first find for each triangle in which of the following region the projected point lies, then solves for each region.

      ^t
\     |
 \reg2|
  \   |
   \  |
    \ |
     \|
      *P2
      |\
      | \
reg3  |  \ reg1
      |   \
      |reg0\
      |     \
      |      \ P1
——-——-——->s

|P0

reg4 | reg5 reg6

Note

Most notations come from :

[1] “David Eberly, ‘Distance Between Point and Triangle in 3D’, Geometric Tools, LLC, (1999)”

Parameters:
  • triangles – (m,3,p) set of m p-dimensional triangles

  • point – (p,) coordinates of the point

  • return_bary – Whether to return barycentric coordinates inside each triangle

Returns:

  • final_dists – (m,) distance from the point to each of the triangle

  • projections – (m,p) coordinates of the projected point

  • bary_coords – (m,3) barycentric coordinates of the projection within each triangle

pyFM.spectral.projection_utils.pointTriangleDistance(TRI, P, return_bary=False)

Computes distance between a point and a triangle in a p-dimensional space

Note

Based on the implementation in (modified to return barycentric coordinates of the projection): https://gist.github.com/joshuashaffer/99d58e4ccbd37ca5d96e

DESCRIPTION

Calculate the distance of a given point P from a triangle TRI. Point P is a row vector of the form 1x3. The triangle is a matrix formed by three rows of points TRI = [P1;P2;P3] each of size 1x3. dist = pointTriangleDistance(TRI,P) returns the distance of the point P to the triangle TRI. [dist,PP0] = pointTriangleDistance(TRI,P) additionally returns the closest point PP0 to P on the triangle TRI.

The algorithm is based on “David Eberly, ‘Distance Between Point and Triangle in 3D’, Geometric Tools, LLC, (1999)” http:\www.geometrictools.com/Documentation/DistancePoint3Triangle3.pdf

      ^t
\     |
 \reg2|
  \   |
   \  |
    \ |
     \|
      *P2
      |\
      | \
reg3  |  \ reg1
      |   \
      |reg0\
      |     \
      |      \ P1
——-——-——->s

|P0

reg4 | reg5 reg6

Parameters:
  • TRI – (3,p) a p-dimensional triangle

  • P – (p,) coordinates of the point

  • return_bary – Whether to return barycentric coordinates inside each triangle

Returns:

  • dist (float) – distance from the point to each of the triangle

  • projection – (p,) coordinates of the projected point

  • bary_coords – (3,) barycentric coordinates of the projection within each triangle