Example #1
0
def castalia_reconstruction(img_path):
    """Incrementally modify an ellipse into a low resolution verision of castalia
    by adding vertices and modifying the mesh
    """
    surf_area = 0.01
    a = 0.22
    delta = 0.01

    # load a low resolution ellipse to start
    ast = asteroid.Asteroid('castalia', 0, 'obj')
    ellipsoid = surface_mesh.SurfMesh(ast.axes[0], ast.axes[1], ast.axes[2],
                                      10, 0.025, 0.5)

    ve, fe = ellipsoid.verts(), ellipsoid.faces()
    vc, fc = ast.V, ast.F

    # sort the vertices in in order (x component)
    vc = vc[vc[:, 0].argsort()]

    # uncertainty for each vertex in meters (1/variance)
    vert_weight = np.full(ve.shape[0], (np.pi * np.max(ast.axes))**2)
    # calculate maximum angle as function of surface area
    max_angle = wavefront.spherical_surface_area(np.max(ast.axes), surf_area)

    # loop and create many figures
    mfig = graphics.mayavi_figure(offscreen=False)
    mesh = graphics.mayavi_addMesh(mfig, ve, fe)
    ms = mesh.mlab_source
    index = 0

    for ii, pt in enumerate(vc):
        index += 1
        filename = os.path.join(
            img_path, 'castalia_reconstruct_' + str(index).zfill(7) + '.jpg')
        # graphics.mlab.savefig(filename, magnification=4)
        ve, vert_weight = wavefront.spherical_incremental_mesh_update(
            pt, ve, fe, vertex_weight=vert_weight, max_angle=max_angle)

        ms.reset(x=ve[:, 0], y=ve[:, 1], z=ve[:, 2], triangles=fe)
        graphics.mayavi_addPoint(mfig, pt, radius=0.01)

    graphics.mayavi_points3d(mfig, ve, scale_factor=0.01, color=(1, 0, 0))

    return 0
Example #2
0
from kinematics import attitude
import numpy as np
import scipy.io

import pdb

# load data from sphere and ellipsoid
sphere_data = scipy.io.loadmat('./data/sphere_distmesh.mat')
ellipsoid_data = scipy.io.loadmat('./data/ellipsoid_distmesh.mat')

vs, fs = sphere_data['v'], sphere_data['f']
ve, fe = ellipsoid_data['v'], ellipsoid_data['f']

# now use cgal
sphere = surface_mesh.SurfMesh(0.5, 0.5, 0.5, 10, 0.015, 0.5)
ellipsoid = surface_mesh.SurfMesh(1, 2, 3, 10, 0.06, 0.5)

print("Some statisitics")
print("Distmesh Sphere: Vertices: {} Faces: {}".format(vs.shape[0],
                                                       fs.shape[0]))
print("Distmesh Ellipsoid: Vertices: {} Faces: {}".format(
    ve.shape[0], fe.shape[0]))
print("SurfMesh Sphere: Vertices: {} Faces: {}".format(
    sphere.verts().shape[0],
    sphere.faces().shape[0]))
print("SurfMesh Ellipsoid: Vertices: {} Faces: {}".format(
    ellipsoid.verts().shape[0],
    ellipsoid.faces().shape[0]))

mfig_distmesh = graphics.mayavi_figure()
Example #3
0
def asteroid_reconstruct_generate_data(output_filename,
                                       asteroid_name='castalia'):
    """Generate all the data for an example for reconstructing asteroid 
    """
    asteroid_type = 'obj'
    asteroid_faces = 0

    if asteroid_name == 'castalia':
        ellipsoid_min_angle = 10
        ellipsoid_max_radius = 0.03
        ellipsoid_max_distance = 0.5
        step = 1
        surf_area = 0.01
    elif asteroid_name == 'itokawa':
        ellipsoid_min_angle = 10
        ellipsoid_max_radius = 0.003
        ellipsoid_max_distance = 0.5
        step = 1
        surf_area = 0.0001

    # load asteroid castalia
    ast = asteroid.Asteroid(asteroid_name, asteroid_faces, asteroid_type)
    ellipsoid = surface_mesh.SurfMesh(ast.axes[0], ast.axes[1], ast.axes[2],
                                      ellipsoid_min_angle,
                                      ellipsoid_max_radius,
                                      ellipsoid_max_distance)
    ve, fe = ellipsoid.verts(), ellipsoid.faces()
    vc, fc = ast.V, ast.F

    # cort the truth vertices in increasing order of x component
    vc = vc[vc[:, 0].argsort()]
    vc = vc[::step, :]
    # np.random.shuffle(vc)

    # define initial uncertainty for our estimate
    vert_weight = np.full(ve.shape[0], (np.pi * np.max(ast.axes))**2)

    # calculate the maximum angle as a function of desired surface area
    max_angle = wavefront.spherical_surface_area(np.max(ast.axes), surf_area)

    # generate a mesh and reconstruct object from c++
    mesh = mesh_data.MeshData(ve, fe)
    rmesh = reconstruct.ReconstructMesh(ve, fe, vert_weight)

    # loop over all the points and save the data
    output_path = os.path.join(output_filename)

    with h5py.File(output_path, 'w') as fout:
        reconstructed_vertex = fout.create_group('reconstructed_vertex')
        reconstructed_weight = fout.create_group('reconstructed_weight')
        reconstructed_vertex.attrs['asteroid_name'] = np.string_(asteroid_name)
        reconstructed_vertex.attrs['asteroid_faces'] = asteroid_faces
        reconstructed_vertex.attrs['asteroid_type'] = np.string_(asteroid_type)
        reconstructed_vertex.attrs['ellipsoid_axes'] = ast.axes
        reconstructed_vertex.attrs['ellipsoid_min_angle'] = ellipsoid_min_angle
        reconstructed_vertex.attrs[
            'ellipsoid_max_radius'] = ellipsoid_max_radius
        reconstructed_vertex.attrs[
            'ellipsoid_max_distance'] = ellipsoid_max_distance
        reconstructed_vertex.attrs['surf_area'] = surf_area

        fout.create_dataset('truth_vertex', data=vc)
        fout.create_dataset('truth_faces', data=fc)
        fout.create_dataset('estimate_faces', data=fe)

        fout.create_dataset('initial_vertex', data=ve)
        fout.create_dataset('initial_faces', data=fe)
        fout.create_dataset('initial_weight', data=vert_weight)

        for ii, pt in enumerate(vc):
            # ve, vert_weight = wavefront.spherical_incremental_mesh_update(pt, ve, fe,
            #                                                               vertex_weight=vert_weight,
            #                                                               max_angle=max_angle)
            rmesh.update(pt, max_angle)
            ve = rmesh.get_verts()
            vert_weight = np.squeeze(rmesh.get_weights())
            # save the current array and weight to htpy
            reconstructed_vertex.create_dataset(str(ii),
                                                data=ve,
                                                compression='lzf')
            reconstructed_weight.create_dataset(str(ii),
                                                data=vert_weight,
                                                compression='lzf')

    print('Finished generating data. Saved to {}'.format(output_path))

    return 0
Example #4
0
def incremental_reconstruction(input_filename,
                               output_filename,
                               asteroid_name='castalia'):
    """Incrementally update the mesh

    Now we'll use the radial mesh reconstruction.

    """
    logger = logging.getLogger(__name__)
    # output_filename = './data/raycasting/20180226_castalia_reconstruct_highres_45deg_cone.hdf5'

    logger.info('Loading {}'.format(input_filename))
    data = np.load(input_filename)
    point_cloud = data['point_cloud'][()]

    # define the asteroid and dumbbell objects

    asteroid_faces = 0
    asteroid_type = 'obj'
    m1, m2, l = 500, 500, 0.003

    ellipsoid_min_angle = 10
    ellipsoid_max_radius = 0.03
    ellipsoid_max_distance = 0.5

    surf_area = 0.01

    ast = asteroid.Asteroid(asteroid_name, asteroid_faces, asteroid_type)
    dum = dumbbell.Dumbbell(m1=m1, m2=m2, l=l)

    logger.info('Creating ellipsoid mesh')
    # define a simple mesh to start
    ellipsoid = surface_mesh.SurfMesh(ast.axes[0], ast.axes[1], ast.axes[2],
                                      ellipsoid_min_angle,
                                      ellipsoid_max_radius,
                                      ellipsoid_max_distance)
    v_est, f_est = ellipsoid.verts(), ellipsoid.faces()

    vert_weight = np.full(v_est.shape[0], (np.pi * np.max(ast.axes))**2)

    max_angle = wavefront.spherical_surface_area(np.max(ast.axes), surf_area)

    # extract out all the points in the asteroid frame
    time = point_cloud['time'][::1]
    ast_ints = point_cloud['ast_ints'][::1]
    logger.info('Create HDF5 file {}'.format(output_filename))
    with h5py.File(output_filename, 'w') as fout:
        # store some extra data about teh simulation
        v_group = fout.create_group('reconstructed_vertex')
        f_group = fout.create_group('reconstructed_face')
        w_group = fout.create_group('reconstructed_weight')

        sim_data = fout.create_group('simulation_data')
        sim_data.attrs['asteroid_name'] = np.string_(asteroid_name)
        sim_data.attrs['asteroid_faces'] = asteroid_faces
        sim_data.attrs['asteroid_type'] = np.string_(asteroid_type)
        sim_data.attrs['m1'] = dum.m1
        sim_data.attrs['m2'] = dum.m2
        sim_data.attrs['l'] = dum.l

        sim_data.attrs['ellipsoid_axes'] = ast.axes
        sim_data.attrs['ellipsoid_min_angle'] = ellipsoid_min_angle
        sim_data.attrs['ellipsoid_max_radius'] = ellipsoid_max_radius
        sim_data.attrs['ellipsoid_max_distance'] = ellipsoid_max_distance
        sim_data.attrs['surf_area'] = surf_area

        sim_data.attrs['max_angle'] = max_angle

        fout.create_dataset('truth_vertex', data=ast.V)
        fout.create_dataset('truth_faces', data=ast.F)
        fout.create_dataset('estimate_faces', data=f_est)

        fout.create_dataset('initial_vertex', data=v_est)
        fout.create_dataset('initial_faces', data=f_est)
        fout.create_dataset('initial_weight', data=vert_weight)

        logger.info('Starting loop over point cloud')
        for ii, (t, points) in enumerate(zip(time, ast_ints)):
            # check if points is empty
            logger.info('Current : t = {} with {} points'.format(
                t, len(points)))
            for pt in points:
                # incremental update for each point in points
                # check to make sure each pt is not nan
                if not np.any(np.isnan(pt)):
                    v_est, vert_weight = wavefront.spherical_incremental_mesh_update(
                        pt,
                        v_est,
                        f_est,
                        vertex_weight=vert_weight,
                        max_angle=max_angle)

            # use HD5PY instead
            # save every so often and delete v_array,f_array to save memory
            if (ii % 1) == 0:
                logger.info('Saving data to HDF5. ii = {}, t = {}'.format(
                    ii, t))
                v_group.create_dataset(str(ii), data=v_est)
                f_group.create_dataset(str(ii), data=f_est)
                w_group.create_dataset(str(ii), data=vert_weight)

    logger.info('Completed the reconstruction')

    return 0
"""Uniform mesh plots for dissertation

Some examples of variations in a initial mesh estimate for the paper

Author
------
Shankar Kulumani		GWU		[email protected]
"""

import numpy as np
from lib import surface_mesh
from visualization import graphics

# generate two triaxial ellipsoids
smesh_1 = surface_mesh.SurfMesh(1, 0.5, 0.5, 10, 0.2, 0.1)
smesh_2 = surface_mesh.SurfMesh(1, 0.5, 0.5, 10, 0.1, 0.1)

view =(45.00000000000001,
       54.73561031724535,
       2.705956013687095,
       np.array([ 0.12708219, -0.05595058, -0.07152687]))
 
mfig = graphics.mayavi_figure(offscreen=False)
mesh = graphics.mayavi_addMesh(mfig, smesh_1.get_verts(), smesh_1.get_faces(), representation='wireframe')
graphics.mlab.view(*view)
graphics.mlab.savefig('/tmp/uniform_mesh_coarse.jpg', magnification=4)

ms = mesh.mlab_source
ms.reset(x=smesh_2.get_verts()[:, 0], y=smesh_2.get_verts()[:, 1], z=smesh_2.get_verts()[:, 2],
         triangles=smesh_2.get_faces())
graphics.mlab.view(*view)
Example #6
0
"""Test out creating and plotting a geodesic waypoint (great circle) on the 
sphere
"""
from point_cloud import wavefront
from kinematics import sphere
from visualization import graphics

from lib import geodesic, surface_mesh

import numpy as np

# generate a sphere for plotting
sphere_mesh = surface_mesh.SurfMesh(1, 1, 1, 10, 0.15, 0.5)
vs, fs = sphere_mesh.verts(), sphere_mesh.faces()

# create two random points on the sphere
initial_point_cartesian = sphere.rand(2)
final_point_cartesian = sphere.rand(2)

# compute waypoints inbetween
waypoints_cartesian = geodesic.sphere_waypoint(initial_point_cartesian,
                                               final_point_cartesian, 5)

# plot everythign on a mayavi figure
mfig = graphics.mayavi_figure()
graphics.mayavi_axes(mfig, [-1, 1, -1, 1, -1, 1])
graphics.mayavi_addMesh(mfig, vs, fs, color=(0, 0, 1), opacity=0.2)
graphics.mayavi_points3d(mfig, waypoints_cartesian, color=(0, 0, 1))
graphics.mayavi_addPoint(mfig, initial_point_cartesian, color=(0, 1, 0))
graphics.mayavi_addPoint(mfig, final_point_cartesian, color=(1, 0, 0))