pysdic.compute_projected_uvmap#

compute_projected_uvmap(mesh, camera, *, warp_by=None)[source]#

Compute the UV map of a mesh projected on the sensor of a camera: the texture coordinates that map the image of the camera onto the elements of the mesh.

  1. The vertices of the (deformed) mesh are projected on the sensor (see pysdic.Camera.project()), giving the image points \((x_k, y_k)\) in pixels (\(x\) along the columns, \(y\) along the rows, the centers of the pixels at the integer coordinates).

  2. The image points are converted to UV coordinates with the OpenGL texture mapping convention (see https://learnopengl.com/Getting-started/Textures): the whole sensor of height \(H\) and width \(W\), from \(-0.5\) to \(W - 0.5\) along \(x\) and from \(-0.5\) to \(H - 0.5\) along \(y\), is mapped to \([0, 1]^2\), with \(v\) pointing upwards:

    \[u_k = \frac{x_k + 0.5}{W}, \qquad v_k = 1 - \frac{y_k + 0.5}{H}\]
  3. The UV coordinates of the vertices are gathered element by element.

The corners of the image are then:

  • lower-left corner, pixel image[H - 1, 0]: \((u, v) = (0, 0)\);

  • upper-left corner, pixel image[0, 0]: \((u, v) = (0, 1)\);

  • lower-right corner, pixel image[H - 1, W - 1]: \((u, v) = (1, 0)\);

  • upper-right corner, pixel image[0, W - 1]: \((u, v) = (1, 1)\).

This is the convention of the UV maps of pysdic (see pysdic.Mesh.uvmap and pysdic.interpolate_texture(), \(x = u W - 0.5\) and \(y = (1 - v) H - 0.5\)): with the image of the camera as texture, each vertex shows the gray level of the pixel where it is projected. For example, to paint the image of a view onto the mesh:

mesh.uvmap = compute_projected_uvmap(mesh, view)
mesh.quick_visualize(texture=view.image.image)

Note

The UV coordinates of a vertex projected out of the image (\((u, v)\) out of \([0, 1]^2\), i.e. out of \([-0.5, W - 0.5] \times [-0.5, H - 0.5]\)) or with a non-finite projection are NaN (both \(u\) and \(v\)). pysdic.Mesh.uvmap only accepts finite values: check numpy.isfinite(uvmap).all() before setting it, or use the UV map of the elements entirely in the image only. The occlusions are not taken into account: a hidden element also gets the texture of the pixels where it is projected (see compute_visibility()). Inside an element, the texture coordinates are interpolated linearly between the vertices, which approximates the perspective and the distortion over the element.

Parameters:
  • mesh (pysdic.Mesh) – The mesh, of dimension \(E\) equal to the input dimension of the camera (the element type is not used).

  • camera (Union[pysdic.Camera, pysdic.View]) – The camera (or a view, whose camera is used).

  • warp_by (Optional[Union[str, ArrayLike]], optional) – The displacement of the vertices (name of a vertex property or array of shape \((N_v, E)\)), e.g. the displacement of a frame of an analysis.

Returns:

The UV map, an array of shape \((N_e, 2 N_{vpe})\) storing, for each element, \((u_1, v_1, u_2, v_2, \dots, u_{N_{vpe}}, v_{N_{vpe}})\) in the order of the vertices of the element. NaN for the vertices projected out of the image.

Return type:

numpy.ndarray

Raises:
  • TypeError – If an input has a wrong type.

  • ValueError – If the dimension of the mesh is not the input dimension of the camera.

Examples

1from pysdic import compute_projected_uvmap
2
3uvmap = compute_projected_uvmap(mesh, camera)                                    # (N_e, 2 N_vpe)
4uvmap_1 = compute_projected_uvmap(mesh, view, warp_by=analysis.get_displacement(1))  # Frame 1