psychopy.tools.mathtools.surfaceNormal

psychopy.tools.mathtools.surfaceNormal(tri, norm=True, out=None, dtype=None)[source]

Compute the surface normal of a given triangle.

Parameters
  • tri (array_like) – Triangle vertices as 2D (3x3) array [p0, p1, p2] where each vertex is a length 3 array [vx, xy, vz]. The input array can be 3D (Nx3x3) to specify multiple triangles.

  • norm (bool, optional) – Normalize computed surface normals if True, default is True.

  • out (ndarray, optional) – Optional output array. Must have one fewer dimensions than tri. The shape of the last dimension must be 3.

  • dtype (dtype or str, optional) – Data type for computations can either be ‘float32’ or ‘float64’. If out is specified, the data type of out is used and this argument is ignored. If out is not provided, ‘float64’ is used by default.

Returns

Surface normal of triangle tri.

Return type

ndarray

Examples

Compute the surface normal of a triangle:

vertices = [[-1., 0., 0.], [0., 1., 0.], [1, 0, 0]]
norm = surfaceNormal(vertices)

Find the normals for multiple triangles, and put results in a pre-allocated array:

vertices = [[[-1., 0., 0.], [0., 1., 0.], [1, 0, 0]],  # 2x3x3
            [[1., 0., 0.], [0., 1., 0.], [-1, 0, 0]]]
normals = np.zeros((2, 3))  # normals from two triangles triangles
surfaceNormal(vertices, out=normals)

Back to top