Exemple #1
0
def test_nodes():
    from rbf.pde.nodes import min_energy_nodes
    from rbf.pde.geometry import contains
    from dentate.alphavol import alpha_shape
    from mayavi import mlab

    obs_u = np.linspace(-0.016 * np.pi, 1.01 * np.pi, 25)
    obs_v = np.linspace(-0.23 * np.pi, 1.425 * np.pi, 25)
    obs_l = np.linspace(-1.0, 1., num=10)

    u, v, l = np.meshgrid(obs_u, obs_v, obs_l, indexing='ij')
    xyz = DG_volume(u, v, l, rotate=[-35., 0., 0.])

    print('Constructing volume...')
    vol = RBFVolume(obs_u, obs_v, obs_l, xyz, order=2)

    print('Constructing volume triangulation...')
    tri = vol.create_triangulation()

    print('Constructing alpha shape...')
    alpha = alpha_shape([], 120., tri=tri)

    # Define the problem domain
    vert = alpha.points
    smp = np.asarray(alpha.bounds, dtype=np.int64)

    N = 10000  # total number of nodes

    # create N quasi-uniformly distributed nodes
    print('Generating nodes...')
    rbf_logger = logging.Logger.manager.loggerDict['rbf.pde.nodes']
    rbf_logger.setLevel(logging.DEBUG)
    out = min_energy_nodes(N, (vert, smp), iterations=10, build_rtree=True)

    nodes = out[0]

    # remove nodes outside of the domain
    in_nodes = nodes[contains(nodes, vert, smp)]

    print('Generated %d interior nodes' % len(in_nodes))

    vol.mplot_surface(color=(0, 1, 0), opacity=0.33, ures=10, vres=10)
    mlab.points3d(*in_nodes.T, color=(1, 1, 0), scale_factor=15.0)

    mlab.show()

    return in_nodes, vol.inverse(in_nodes)
Exemple #2
0
def test_alphavol():
    from mayavi import mlab
    from dentate.alphavol import alpha_shape

    obs_u = np.linspace(-0.016 * np.pi, 1.01 * np.pi, 20)
    obs_v = np.linspace(-0.23 * np.pi, 1.425 * np.pi, 20)
    obs_l = np.linspace(-3.95, 3.2, num=10)

    u, v, l = np.meshgrid(obs_u, obs_v, obs_l, indexing='ij')
    xyz = DG_volume(u, v, l, rotate=[-35., 0., 0.])

    print('Constructing volume...')
    vol = RBFVolume(obs_u, obs_v, obs_l, xyz, order=2)

    print('Constructing volume triangulation...')
    tri = vol.create_triangulation()

    print('Constructing alpha shape...')
    alpha = alpha_shape([], 120., tri=tri)

    vert = alpha.points
    smp = np.asarray(alpha.bounds, dtype=np.int64)

    edges = np.vstack([
        np.column_stack([smp[:, 0], smp[:, 1]]),
        np.column_stack([smp[:, 1], smp[:, 2]])
    ])

    x = vert[:, 0]
    y = vert[:, 1]
    z = vert[:, 2]

    start_idx = edges[:, 0]
    end_idx = edges[:, 1]

    vol.mplot_surface(color=(0, 1, 0), opacity=0.33, ures=10, vres=10)
    mlab.quiver3d(x[start_idx],
                  y[start_idx],
                  z[start_idx],
                  x[end_idx] - x[start_idx],
                  y[end_idx] - y[start_idx],
                  z[end_idx] - z[start_idx],
                  mode='2ddash',
                  scale_factor=1)

    mlab.show()
Exemple #3
0
def make_alpha_shape(min_extent,
                     max_extent,
                     alpha_radius=120.,
                     **volume_kwargs):

    logger.info("Constructing volume...")

    vol = make_volume((min_extent[0], max_extent[0]),
                      (min_extent[1], max_extent[1]),
                      (min_extent[2], max_extent[2]), **volume_kwargs)

    logger.info("Constructing volume triangulation...")

    tri = vol.create_triangulation()

    logger.info("Constructing alpha shape...")

    alpha = alpha_shape([], alpha_radius, tri=tri)

    return alpha
#xvol = RBFInterpolant(obs_uvl,X[obs_u],basis='imq',penalty=0.1,extrapolate=False)

#mlab.mesh(X[obs_u], Y[obs_v], Z[obs_l], representation='wireframe', color=(0, 0, 0))
#mlab.points3d(X[obs_u], Y[obs_v], Z[obs_l], scale_factor=100.0, color=(1, 1, 0))


    
#vol = RBFVolume(np.asarray(obs_u.ravel(), dtype=np.float32), \
#                np.asarray(obs_v.ravel(), dtype=np.float32), \
#                np.asarray(obs_l.ravel(), dtype=np.float32), \
#                obs_xyz, basis='imq', order=1)


#xyz = make_volume()

#vol.mplot_surface(color=(0, 1, 0), ures=2, vres=2, opacity=0.33)

#xyz = np.array([X[obs_u].ravel(), Y[obs_v].ravel(), Z[obs_l].ravel()]).reshape(3, obs_u.size).T

#mlab.points3d(obs_xyz[:,0], obs_xyz[:,1], obs_xyz[:,2], scale_factor=20.0, color=(1, 1, 0))
print(obs_xyz.shape)
print(obs_xyz[:,0])

alpha =  alpha_shape(pts,radius,tri=None)

# Turn off perspective
fig = mlab.gcf()
fig.scene.camera.trait_set(parallel_projection=1)

mlab.show()
Exemple #5
0
def get_volume_distances(ip_vol,
                         origin_spec=None,
                         nsample=1000,
                         alpha_radius=120.,
                         nodeitr=20,
                         comm=None):
    """Computes arc-distances along the dimensions of an `RBFVolume` instance.

    Parameters
    ----------
    ip_vol : RBFVolume
        An interpolated volume instance of class RBFVolume.
    origin_coords : array(float)
        Origin point to use for distance computation.
    nsample : int
        Number of points to sample inside the volume.
    alpha_radius : float
        Parameter for creation of alpha volume that encloses the
        RBFVolume. Smaller values improve the quality of alpha volume,
        but increase the time for sampling points inside the volume.
    nodeitr : int 
        Number of iterations for distributing sampled points inside the volume.
    comm : MPIComm (optional)
        mpi4py MPI communicator: if provided, node generation and distance computation will be performed in parallel
    Returns
    -------
    (Y1, X1, ... , YN, XN) where N is the number of dimensions of the volume.
    X : array of coordinates
        The sampled coordinates.
    Y : array of distances
        The arc-distance from the starting index of the coordinate space to the corresponding coordinates in X.

    """

    size = 1
    rank = 0
    if comm is not None:
        rank = comm.rank
        size = comm.size

    uvl_coords = None
    origin_extent = None
    origin_ranges = None
    origin_pos_um = None
    if rank == 0:
        boundary_uvl_coords = np.array(
            [[ip_vol.u[0], ip_vol.v[0], ip_vol.l[0]],
             [ip_vol.u[0], ip_vol.v[-1], ip_vol.l[0]],
             [ip_vol.u[-1], ip_vol.v[0], ip_vol.l[0]],
             [ip_vol.u[-1], ip_vol.v[-1], ip_vol.l[0]],
             [ip_vol.u[0], ip_vol.v[0], ip_vol.l[-1]],
             [ip_vol.u[0], ip_vol.v[-1], ip_vol.l[-1]],
             [ip_vol.u[-1], ip_vol.v[0], ip_vol.l[-1]],
             [ip_vol.u[-1], ip_vol.v[-1], ip_vol.l[-1]]])

        resample = 10
        span_U, span_V, span_L = ip_vol._resample_uvl(resample, resample,
                                                      resample)

        if origin_spec is None:
            origin_coords = np.asarray(
                [np.median(span_U),
                 np.median(span_V),
                 np.max(span_L)])
        else:
            origin_coords = np.asarray([
                origin_spec['U'](span_U), origin_spec['V'](span_V),
                origin_spec['L'](span_L)
            ])

        logger.info('Origin coordinates: %f %f %f' %
                    (origin_coords[0], origin_coords[1], origin_coords[2]))

        pos, extents = ip_vol.point_position(origin_coords[0],
                                             origin_coords[1],
                                             origin_coords[2])

        origin_pos = pos[0]
        origin_extent = extents[0]
        origin_pos_um = (origin_pos[0] * origin_extent[0],
                         origin_pos[1] * origin_extent[1])
        origin_ranges = ((-(origin_pos[0] * origin_extent[0]),
                          (1.0 - origin_pos[0]) * origin_extent[0]),
                         (-(origin_pos[1] * origin_extent[1]),
                          (1.0 - origin_pos[1]) * origin_extent[1]))

        logger.info(
            'Origin position: %f %f extent: %f %f' %
            (origin_pos[0], origin_pos[1], origin_extent[0], origin_extent[1]))
        logger.info('Origin ranges: %f : %f %f : %f' %
                    (origin_ranges[0][0], origin_ranges[0][1],
                     origin_ranges[1][0], origin_ranges[1][1]))

        logger.info("Creating volume triangulation...")
        tri = ip_vol.create_triangulation()

        logger.info("Constructing alpha shape...")
        alpha = alpha_shape([], alpha_radius, tri=tri)

        xyz_coords = generate_nodes(alpha, nsample, nodeitr)

        logger.info('Inverse interpolation of UVL coordinates...')
        uvl_coords_interp = ip_vol.inverse(xyz_coords)
        xyz_coords_interp = ip_vol(uvl_coords_interp[:, 0],
                                   uvl_coords_interp[:, 1],
                                   uvl_coords_interp[:, 2],
                                   mesh=False).reshape(3, -1).T

        xyz_error_interp = np.abs(np.subtract(xyz_coords, xyz_coords_interp))

        node_uvl_coords = uvl_coords_interp
        uvl_coords = np.vstack([boundary_uvl_coords, node_uvl_coords])

    comm.barrier()
    if comm is not None:
        uvl_coords = comm.bcast(uvl_coords, root=0)
        origin_extent, origin_pos_um, origin_ranges = comm.bcast(
            (origin_extent, origin_pos_um, origin_ranges), root=0)

    if rank == 0:
        logger.info('Computing volume distances...')

    ldists_u = []
    ldists_v = []
    obs_uvls = []
    for i, uvl in enumerate(uvl_coords):
        if i % size == rank:
            sample_U = uvl[0]
            sample_V = uvl[1]
            sample_L = uvl[2]
            pos, extent = ip_vol.point_position(sample_U, sample_V, sample_L)

            uvl_pos = pos[0]
            uvl_extent = extent[0]

            obs_uvls.append(uvl)
            ldists_u.append(uvl_pos[0] * origin_extent[0] - origin_pos_um[0])
            ldists_v.append(uvl_pos[1] * origin_extent[1] - origin_pos_um[1])

    distances_u = np.asarray(ldists_u, dtype=np.float32)
    distances_v = np.asarray(ldists_v, dtype=np.float32)
    obs_uvl = np.asarray(np.vstack(obs_uvls), dtype=np.float32)

    return (origin_ranges, obs_uvl, distances_u, distances_v)