Skip to content

random_points_on_mesh

random_points_on_mesh(V, F, n, rng=np.random.default_rng(), distribution='uniform', return_indices=False)

Samples a mesh V,F according to a given distribution. Valid meshes are polylines or triangle meshes.

Parameters:

Name Type Description Default
V (n_v,d) numpy array

vertex list of vertex positions

required
F (m,k) numpy int array

if k==2, interpret input as ordered polyline; if k==3 numpy int array, interpred as face index list of a triangle mesh

required
rng numpy rng, optional (default

which numpy random number generator to use

np.random.default_rng()
n int

how many points to sample

required
method string, optional (default

According to which distribution to sample. Currently, only uniform is supported.

required
return_indices bool, optional (default

Whether to return the indices for each element along with the barycentric coordinates of the sampled points within each element

False

Returns:

Name Type Description
x (n,d) numpy array

the n sampled points

I (n,) numpy int array

element indices where sampled points lie (if requested)

u (n,k) numpy array

barycentric coordinates for sampled points in I

Examples:

TODO

Source code in src/gpytoolbox/random_points_on_mesh.py
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
def random_points_on_mesh(V,F,
    n,
    rng=np.random.default_rng(),
    distribution='uniform',
    return_indices=False):
    """Samples a mesh V,F according to a given distribution.
    Valid meshes are polylines or triangle meshes.

    Parameters
    ----------
    V : (n_v,d) numpy array
        vertex list of vertex positions
    F : (m,k) numpy int array
        if k==2, interpret input as ordered polyline;
        if k==3 numpy int array, interpred as face index list of a triangle
        mesh
    rng : numpy rng, optional (default: new `np.random.default_rng()`)
        which numpy random number generator to use
    n : int
        how many points to sample
    method : string, optional (default: 'uniform')
        According to which distribution to sample.
        Currently, only uniform is supported.
    return_indices : bool, optional (default: False)
        Whether to return the indices for each element along with the
        barycentric coordinates of the sampled points within each element

    Returns
    -------
    x : (n,d) numpy array
        the n sampled points
    I : (n,) numpy int array
        element indices where sampled points lie (if requested)
    u : (n,k) numpy array
        barycentric coordinates for sampled points in I


    Examples
    --------
    TODO

    """

    assert n>=0

    if n==0:
        if return_indices:
            return np.zeros((0,), dtype=V.dtype), \
            np.zeros((0,), dtype=F.dtype), \
            np.zeros((0,), dtype=V.dtype)
        else:
            return np.zeros((0,), dtype=V.dtype)

    k = F.shape[1]
    if k==2:
        if distribution=='uniform':
            I,u = _uniform_sample_polyline(V,F,n,rng,distribution)
        else:
            assert False, "distribution not supported"
        x = u[:,0][:,None]*V[F[I,0],:] + \
        u[:,1][:,None]*V[F[I,1],:]
    elif k==3:
        if distribution=='uniform':
            I,u = _uniform_sample_triangle_mesh(V,F,n,rng,distribution)
        else:
            assert False, "distribution not supported"
        x = u[:,0][:,None]*V[F[I,0],:] + \
        u[:,1][:,None]*V[F[I,1],:] + \
        u[:,2][:,None]*V[F[I,2],:]
    else:
        assert False, "Only polylines and triangles supported"

    if return_indices:
        return x, I, u
    else:
        return x