def write_projection_render(pfp, v, f, map):

    CAM2WORLD = np.array([[0.85408425, 0.31617427, -0.375678, 0.56351697 * 2],
                          [0., -0.72227067, -0.60786998, 0.91180497 * 2],
                          [-0.52013469, 0.51917219, -0.61688, 0.92532003 * 2],
                          [0., 0., 0., 1.]],
                         dtype=np.float32)

    # rotate the mesh elevation by 30 degrees
    Rx = np.array(
        [[1, 0, 0, 0], [0., np.cos(np.pi / 6), -np.sin(np.pi / 6), 0],
         [0, np.sin(np.pi / 6), np.cos(np.pi / 6), 0], [0., 0., 0., 1.]],
        dtype=np.float32)

    CAM2WORLD = np.matmul(Rx, CAM2WORLD)

    RENDER_INFO = {
        'Height': 480,
        'Width': 640,
        'fx': 575,
        'fy': 575,
        'cx': 319.5,
        'cy': 239.5
    }
    # TODO ---------- Render Stub -----------#
    context = render.SetMesh(v, f)
    for i_ang, _ in enumerate(np.linspace(0, 2 * np.pi, map.num_angs)):
        render.render(context, map.world2cam_mats[i_ang])
        # depth = render.getDepth(RENDER_INFO)
        vindices, _, _ = render.getVMap(context, RENDER_INFO)
        mask = np.unique(vindices)

        # Only kept the mask option
        np.savez(f"{pfp}_mask_{i_ang:03d}", mask=mask)
def voxelized_pointcloud_sampling(path):
    try:
        out_file = path + '/voxelized_point_cloud_{}res_{}points.npz'.format(
            args.res, args.num_points)

        if os.path.exists(out_file):
            if args.write_over:
                print('overwrite ', out_file)
            else:
                print('File exists. Done.')
                return
        off_path = path + '/isosurf_scaled.off'

        # mesh = trimesh.load(off_path)
        # point_cloud = mesh.sample(args.num_points)
        V, F = objloader.LoadOff(off_path)
        # set up camera information
        info = {
            'Height': 480,
            'Width': 640,
            'fx': 575,
            'fy': 575,
            'cx': 319.5,
            'cy': 239.5
        }
        render.setup(info)

        # set up mesh buffers in cuda
        context = render.SetMesh(V, F)

        # cam2world = np.array([[ 0.85408425,  0.31617427, -0.375678  ,  0.56351697 * 2],
        #     [ 0.        , -0.72227067, -0.60786998,  0.91180497 * 2],
        #     [-0.52013469,  0.51917219, -0.61688   ,  0.92532003 * 2+3],
        #     [ 0.        ,  0.        ,  0.        ,  1.        ]], dtype=np.float32)
        cam2world = np.array(
            [[0.85408425, 0.31617427, -0.375678, 0.56351697 * 2],
             [0., -0.72227067, -0.60786998, 0.91180497 * 2],
             [-0.52013469, 0.51917219, -0.61688, 0.92532003 * 2],
             [0., 0., 0., 1.]],
            dtype=np.float32)
        world2cam = np.linalg.inv(cam2world).astype('float32')
        # the actual rendering process
        render.render(context, world2cam)

        # get information of mesh rendering
        # vindices represents 3 vertices related to pixels
        vindices, vweights, findices = render.getVMap(context, info)
        visible_indices = np.unique(vindices)
        # visible_points = V[visible_indices]
        # visible_faces =  F[np.unique(findices)]
        num_visible = visible_indices.shape[0]

        # # faces for partial boun
        # for i in range(num_visible):
        #     origin_index = visible_indices[i]
        #     visible_faces = np.where(visible_faces==origin_index,i,visible_faces)

        # max_index = num_visible - 1
        # faces_valid = np.ones((visible_faces.shape[0]),dtype=bool)
        # for i in range(visible_faces.shape[0]):
        #     face = visible_faces[i]
        #     if face[0] > max_index or face[1] > max_index or face[2] > max_index:
        #         faces_valid[i] = False
        # visible_faces = visible_faces[faces_valid]

        # for visualization
        # vis_face = findices.astype('float32') / np.max(findices)
        # sio.imsave(path + '/face.png',vis_face)
        # sio.imsave(path + '/vertex.png',vweights)
        # return num_visible

        if num_visible < args.num_points:
            print("visible_num is ", num_visible)
            raise Exception
        sample_idx = np.random.choice(num_visible,
                                      args.num_points,
                                      replace=False)
        valid_indices = visible_indices[sample_idx]
        point_cloud = V[valid_indices]
        # num_valid = point_cloud.shape[0]

        # for i in range(num_valid):
        #     origin_index = valid_indices[i]
        #     visible_faces = np.where(visible_faces==origin_index,i,visible_faces)

        # max_index = num_valid - 1
        # faces_valid = np.ones((visible_faces.shape[0]),dtype=bool)
        # for i in range(visible_faces.shape[0]):
        #     face = visible_faces[i]
        #     if face[0] > max_index or face[1] > max_index or face[2] > max_index:
        #         faces_valid[i] = False
        # visible_faces = visible_faces[faces_valid]

        occupancies = np.zeros(len(grid_points), dtype=np.int8)

        _, idx = kdtree.query(point_cloud)
        occupancies[idx] = 1

        compressed_occupancies = np.packbits(occupancies)

        np.savez(out_file,
                 point_cloud=point_cloud,
                 compressed_occupancies=compressed_occupancies,
                 bb_min=bb_min,
                 bb_max=bb_max,
                 res=args.res)

        print('Finished {}'.format(path))

        # prepare data for boundary sampling

        # visible_
        # visible_mesh = trimesh.base.Trimesh(vertices=point_cloud,faces=visible_faces)
        # visible_mesh.export(path + '/vis_mesh.obj')
        # boundary_sampling(path,visible_mesh)
    except Exception as err:
        print('Error with {}: {}'.format(path, traceback.format_exc()))
Esempio n. 3
0
import sys
import skimage.io as sio
import os
#from gen_poses import GetPoses,WavePose
import shutil
from objloader import LoadTextureOBJ
import render
import objloader


input_obj = sys.argv[1]
V, F, VT, FT, VN, FN, face_mat, kdmap = objloader.LoadTextureOBJ(input_obj)

# set up camera information
info = {'Height':960, 'Width':1280, 'fx':575*2, 'fy':575*2, 'cx':640, 'cy':480}
render.setup(info)

context = render.SetMesh(V, F)

cam2world = np.array([[ 0.85408425,  0.31617427, -0.375678  ,  0.56351697 * 2],
       [ 0.        , -0.72227067, -0.60786998,  0.91180497 * 2],
       [-0.52013469,  0.51917219, -0.61688   ,  0.92532003 * 2],
       [ 0.        ,  0.        ,  0.        ,  1.        ]], dtype=np.float32)

world2cam = np.linalg.inv(cam2world).astype('float32')
render.render(context, world2cam)
depth = render.getDepth(info)
vindices, vweights, findices = render.getVMap(context, info)

sio.imsave('depth.png', depth / np.max(depth))
sio.imsave('vweights.png', vweights)