def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('input', type=str)
    parser.add_argument('-c', dest='camera_actions', type=str, default=[],
                        action='append', help='camera actions')

    args = parser.parse_args()
    z = np.load(args.input)

    vis = VisualiseMesh()

    # V0 (purple)
    V0 = z['V0']
    vis.add_mesh(V0, z['T'], actor_name='V0')
    lut = vis.actors['V0'].GetMapper().GetLookupTable()
    lut.SetTableValue(0, *int2dbl(255, 0, 255))

    # V0 w/ X (green)
    T_ = faces_to_cell_array(z['T'])
    adj, W = weights.weights(V0, T_, weights_type='cotan')
    solveV0_X = ARAPVertexSolver(adj, W, V0)

    rotM = lambda x: quat.rotationMatrix(quat.quat(x))
    V0_X = solveV0_X(map(rotM, z['X']))
    V0_X += register.displacement(V0_X, z['V'])

    vis.add_mesh(V0_X, z['T'], actor_name='V0_X')
    lut = vis.actors['V0_X'].GetMapper().GetLookupTable()
    lut.SetTableValue(0, *int2dbl(0, 255, 0))

    # V0 w/ Xg (yellow)
    if 'Xg' in z.keys():
        Xg = z['Xg'][0]
        qg = quat.quat(Xg)
        Q = [quat.quatMultiply(quat.quat(x), qg) for x in z['X']]
        V0_Xg = solveV0_X([quat.rotationMatrix(q) for q in Q])
        V0_Xg += register.displacement(V0_Xg, z['V'])
        vis.add_mesh(V0_Xg, z['T'], actor_name='V0_Xg')
        lut = vis.actors['V0_Xg'].GetMapper().GetLookupTable()
        lut.SetTableValue(0, *int2dbl(255, 255, 0))

    # V (blue)
    vis.add_mesh(z['V'], z['T'], actor_name='V')

    # input frame
    vis.add_image(z['input_frame'])

    # projection constraints
    vis.add_projection(z['C'], z['P'])

    # apply camera actions sequentially
    for action in args.camera_actions:
        method, tup, save_after = parse_camera_action(action)
        print '%s(*%s), save_after=%s' % (method, tup, save_after)
        vis.camera_actions((method, tup))

    vis.execute()
Esempio n. 2
0
    def export_states(self, states, output_dir):
        Z = map(lambda i: [], xrange(self.N)) 

        for n, s in enumerate(states):
            s = Bunch(s)
            for i in xrange(self.N):
                V = s.Vb[0]
                if s.y[i].size > 0:
                    V = V + reduce(add, map(mul, s.y[i], s.Vb[1:]))

                q = quaternion.quat(s.Xg[i].ravel())
                R = quaternion.rotationMatrix(q)
                Rt = np.transpose(R)
                V = s.s[i][0][0] * np.dot(V, Rt) + s.Vd[i][0]
                    
                Q = geometry.path2pos(V, self.T, s.L[i], s.U[i])

                d = dict(T=self.T, V=V,
                         C=self.C[i], P=self.P[i],
                         Q=Q, S=self.S[i],
                         image=self.frames[i],
                         s=s.s[i], Xg=s.Xg[i].ravel())

                Z[i].append(d)

        if not os.path.exists(output_dir):
            os.makedirs(output_dir)

        for i, z in enumerate(Z):
            output_path = os.path.join(output_dir, '%d.dat' % i)
            print '-> %s' % output_path
            dump(output_path, dict(has_states=True, states=z))
Esempio n. 3
0
    def V_from_X1_X2(a, b):
        X12 = map(lambda x: axis_angle.axMakeInterpolated(a, x[0], b, x[1]),
                  izip(X1, X2))

        V12 = solve_V_X(map(lambda x: quat.rotationMatrix(quat.quat(x)), X12))

        # Register bottom fixed layers
        A, d = right_multiply_rigid_transform(V12[C[L1:]], V[C[L1:]])
        V12 = np.dot(V12, A) + d

        return V12
Esempio n. 4
0
def main_compositional_basis():
    parser = argparse.ArgumentParser()
    parser.add_argument('model', type=str)
    parser.add_argument('initial_geometry', type=str)
    parser.add_argument('--use_linear_transform',
                        action='store_true',
                        default=False)

    parser.add_argument('axes', type=str)
    parser.add_argument('rotations', type=str)
    parser.add_argument('--in_radians', action='store_true', default=False)

    args = parser.parse_args()

    for key in ['axes', 'rotations']:
        setattr(args, key, eval(getattr(args, key)))

    vis = VisualiseMesh()

    T = load_input_mesh(args.model)
    V = load_input_geometry(args.initial_geometry, args.use_linear_transform)
    V -= np.mean(V, axis=0)
    vis.add_mesh(V,
                 T,
                 actor_name='base',
                 color=(31, 120, 180),
                 compute_normals=True)

    axes = np.asarray(args.axes)
    assert axes.shape == (2, 3)
    axes = normalise(axes)

    rotations = np.atleast_2d(args.rotations)
    assert rotations.shape[1] == 2

    if args.in_radians:
        rotations = np.rad2deg(rotations)

    n = len(rotations)
    colors = cm.autumn(np.linspace(0., 1., n, endpoint=True), bytes=True)

    for i in xrange(n):
        x = reduce(axis_angle.axAdd, axes * rotations[i][:, np.newaxis])
        q = quaternion.quat(x)
        R = quaternion.rotationMatrix(q)
        V1 = np.dot(V, np.transpose(R))
        vis.add_mesh(V1,
                     T,
                     actor_name='rotation_%d' % i,
                     color=colors[i],
                     compute_normals=True)

    vis.execute()
def main_solve_single_silhouette():
    args = parse_args()
    pprint(args.__dict__)

    arap_solver = np.load(args.solver)

    T = arap_solver.T
    V = arap_solver._s.V1[args.i]
    Vb = [V.copy(),
          np.zeros_like(V)]

    s = np.r_[1.].reshape(-1,1)
    xg = np.r_[0., 0., 0.].reshape(-1,3)
    vd = np.r_[0., 0., 0.].reshape(-1,3)

    # y = np.array(tuple(), dtype=np.float64).reshape(0,1)
    y = np.r_[1.0].reshape(-1,1)

    C = arap_solver.C[args.i]
    P = arap_solver.P[args.i]
    S = arap_solver.S[args.i]
    SN = arap_solver.SN[args.i]

    U = arap_solver._s.U[args.i].copy()
    L = arap_solver._s.L[args.i].copy()

    for i in xrange(args.max_restarts):
        status = solve_single_silhouette(
            T, Vb, s, xg, vd, y, U, L, 
            C, P, S, SN, args.lambdas, args.preconditioners,
            args.narrowband,
            debug=False,
            **args.solver_options)
        print status[1]

        print 'y:'
        print y

        if status[0] not in (0, 4):
            break

    if args.output is not None:
        print '-> %s' % args.output
        dump(args.output, dict(Vb=Vb, y=y, T=T))
        return
        
    V = Vb[0]
    if y.size > 0:
        V = V + reduce(add, map(mul, y, Vb[1:]))

    q = quaternion.quat(xg[0])
    R = quaternion.rotationMatrix(q)
    Rt = np.transpose(R)
    V = s[0][0] * np.dot(V, Rt) + vd[0]

    vis = VisualiseMesh()
    vis.add_mesh(V, T)
    vis.add_image(arap_solver.frames[args.i])

    Q = geometry.path2pos(V, T, L, U)
    vis.add_projection(C, P)
    vis.add_quick_silhouette(Q, S)

    vis.camera_actions(('SetParallelProjection', (True,)))
    vis.execute()
Esempio n. 6
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('input', type=str)
    parser.add_argument('-c',
                        dest='camera_actions',
                        type=str,
                        default=[],
                        action='append',
                        help='camera actions')
    parser.add_argument('--mean_centre', action='store_true', default=False)

    args = parser.parse_args()
    print 'args.input:', args.input
    files = filter(lambda s: s.endswith('.npz'), os.listdir(args.input))

    def sort_key(filename):
        root, ext = os.path.splitext(filename)
        try:
            return int(root)
        except ValueError:
            return -1

    files = map(lambda f: os.path.join(args.input, f),
                sorted(files, key=sort_key))

    print 'input:'
    pprint(files)

    # `V0` taken from first frame
    print 'V0 <- ', files[0]
    z = np.load(files[0])
    V0 = z['V']
    if args.mean_centre:
        V0 -= np.mean(V0, axis=0)
    T = z['T']

    vis = VisualiseMesh()

    # V0 (purple)
    vis.add_mesh(V0, T, actor_name='V0')
    lut = vis.actors['V0'].GetMapper().GetLookupTable()
    lut.SetTableValue(0, *int2dbl(255, 0, 255))

    # setup solveV0_X
    T_ = faces_to_cell_array(z['T'])
    adj, W = weights.weights(V0, T_, weights_type='cotan')
    solveV0_X = ARAPVertexSolver(adj, W, V0)

    # setup color map
    cmap = cm.jet(np.linspace(0., 1., len(files) - 1))

    # function to translate axis-angle to rotation matrices
    rotM = lambda x: quat.rotationMatrix(quat.quat(x))

    # add each actor
    for i, file_ in enumerate(files[1:]):
        # load instance file
        print 'V0_X[%d] <- ' % i, file_
        z = np.load(file_)

        # output scale and global rotation (axis-angle)
        def safe_print(var):
            try:
                v = z[var].squeeze()
            except KeyError:
                return

            print ' `%s`:' % var, np.around(v, decimals=3)

        safe_print('s')
        safe_print('Xg')

        # solve for the new coordinates and mean centre
        V0_X = solveV0_X(map(rotM, z['X']))
        if args.mean_centre:
            V0_X -= np.mean(V0_X, axis=0)

        # add actor and adjust lookup table for visualisation
        actor_name = 'V0_X_%d' % i
        vis.add_mesh(V0_X, z['T'], actor_name=actor_name)

        lut = vis.actors[actor_name].GetMapper().GetLookupTable()
        lut.SetTableValue(0, *cmap[i, :3])

    # apply camera actions sequentially
    for action in args.camera_actions:
        method, tup, save_after = parse_camera_action(action)
        print '%s(*%s), save_after=%s' % (method, tup, save_after)
        vis.camera_actions((method, tup))

    vis.execute()

    return

    # V0 w/ X (green)

    V0_X = solveV0_X([quat.rotationMatrix(quat.quat(x)) for x in z['X']])
    vis.add_mesh(V0_X, z['T'], actor_name='V0_X')
    lut = vis.actors['V0_X'].GetMapper().GetLookupTable()
    lut.SetTableValue(0, *int2dbl(0, 255, 0))

    # V0 w/ Xg (yellow)
    qg = quat.quat(z['Xg'][0])
    Q = [quat.quatMultiply(quat.quat(x), qg) for x in z['X']]
    V0_Xg = solveV0_X([quat.rotationMatrix(q) for q in Q])
    vis.add_mesh(V0_Xg, z['T'], actor_name='V0_Xg')
    lut = vis.actors['V0_Xg'].GetMapper().GetLookupTable()
    lut.SetTableValue(0, *int2dbl(255, 255, 0))

    # V (blue)
    vis.add_mesh(z['V'], z['T'], actor_name='V')

    # input frame
    vis.add_image(z['input_frame'])

    # projection constraints
    vis.add_projection(z['C'], z['P'])
def main():
    # Input
    lambdas = np.r_[1e0, 1e2].astype(np.float64)
    preconditioners = np.r_[1.0, 1.0, 128.0, 128.0, 128.0].astype(np.float64)

    V, T = box_model(3, 50, 1.0, 1.0)
    N = V.shape[0]

    # Setup no rotations (fixed) except through middle of the bar
    heights = np.unique(V[:,2])
    middle = np.mean(heights)
    delta = 6.0
    I = np.argwhere(((middle - delta) <= V[:,2]) &
                    (V[:,2] < (middle + delta))).flatten()

    M = I.shape[0]

    K = np.zeros((N, 2), dtype=np.int32)
    K[I,0] = -1
    K[I,1] = np.arange(M)

    # Setup variables for solver
    Xg = np.zeros((1, 3), dtype=np.float64)
    s = np.ones((1, 1), dtype=np.float64)

    Xb = np.array([], dtype=np.float64).reshape(0, 3)
    y = np.array([], dtype=np.float64).reshape(0, 1)

    X = np.zeros((M, 3), dtype=np.float64)

    V1 = V.copy()

    # Setup `C`, `P1` and `P2`
    C1 = np.argwhere(V[:,2] >= heights[-3]).flatten()
    L1 = C1.shape[0]
    C2 = np.argwhere(V[:,2] <= heights[2]).flatten()
    L2 = C2.shape[0]
    C = np.r_[C1, C2]

    # Rotate `C1` vertices about their mean (by -30 on the x-axis)
    x = np.r_[np.deg2rad(-30), 0., 0.]
    R = quat.rotationMatrix(quat.quat(x))

    Q = V[C1]
    c = np.mean(Q, axis=0)
    Q = np.dot(Q - c, np.transpose(R)) + c

    Q[:, 0] += 20
    Q[:, 1] += 30
    Q[:, 2] -= 20

    P = np.ascontiguousarray(np.r_[Q, V[C2]])

    # Solve for `V1`
    status = solve_forward_sectioned_arap_proj(
        T, V, Xg, s, Xb, y, X, V1, K, C, P, lambdas, preconditioners, 
        isProjection=False,
        uniformWeights=False,
        fixedScale=True,
        maxIterations=100, 
        verbosenessLevel=1)

    # View
    # vis = VisualiseMesh()
    # vis.add_mesh(V1, T)
    # vis.add_points(V1[I], sphere_radius=0.2, actor_name='I', color=(0., 0., 1.))
    # vis.add_points(V1[C], sphere_radius=0.2, actor_name='C', color=(1., 0., 0.))
    # vis.camera_actions(('SetParallelProjection', (True,)))
    # vis.execute(magnification=4)

    # Rotate `C1` vertices about their mean (by -180 on the x-axis)
    V1 = V.copy()

    x = np.r_[np.deg2rad(+180), 0., 0.]
    R = quat.rotationMatrix(quat.quat(x))

    Q = V[C1]
    c = np.mean(Q, axis=0)
    Q = np.dot(Q - c, np.transpose(R)) + c

    Q[:, 1] += 10
    Q[:, 2] -= Q[-1, 2]

    P = np.ascontiguousarray(np.r_[Q, V[C2]])
    print 'P:'
    print np.around(P, decimals=3)

    # Assign a single rotation to the upper variables
    I1 = np.setdiff1d(np.argwhere(V[:,2] >= (middle + delta)).flatten(), 
                      C1, assume_unique=True)

    K[I1, 0] = -1
    K[I1, 1] = M

    Xg = np.zeros((1, 3), dtype=np.float64)
    X = np.zeros((M+1, 3), dtype=np.float64)

    # Solve for `V1`
    status = solve_forward_sectioned_arap_proj(
        T, V, Xg, s, Xb, y, X, V1, K, C, P, lambdas, preconditioners, 
        isProjection=False,
        uniformWeights=False,
        fixedScale=True,
        maxIterations=100, 
        verbosenessLevel=1)

    print 's:', s
    print 'X[M] (%.2f):' % np.rad2deg(norm(X[M])), np.around(X[M], decimals=3)

    # View
    # vis = VisualiseMesh()
    # vis.add_mesh(V1, T)
    # vis.add_points(V1[I], sphere_radius=0.2, actor_name='I', color=(0., 0., 1.))
    # vis.add_points(V1[C], sphere_radius=0.2, actor_name='C', color=(1., 0., 0.))
    # vis.camera_actions(('SetParallelProjection', (True,)))
    # vis.execute(magnification=4)

    # Deflect absolute positions further
    Q[:, 0] += 20

    # Assign a single basis rotation to the middle variables
    K[I, 0] = np.arange(M) + 1
    K[I, 1] = 0

    # Assign a single free rotation to the upper variables
    K[I1, 0] = -1
    K[I1, 1] = 0

    V1 = V.copy()

    Xg = np.zeros((1, 3), dtype=np.float64)
    X = np.zeros((1, 3), dtype=np.float64)

    Xb = np.zeros((1, 3), dtype=np.float64)
    y = np.ones((M, 1), dtype=np.float64)

    # Solve for `V1`
    status = solve_forward_sectioned_arap_proj(
        T, V, Xg, s, Xb, y, X, V1, K, C, P, lambdas, preconditioners, 
        isProjection=False,
        uniformWeights=False,
        fixedScale=True,
        maxIterations=100, 
        verbosenessLevel=1)

    print 'X:', np.around(X, decimals=3)
    print 'Xb:', np.around(Xb, decimals=3)
    print 'y:', np.around(y, decimals=3)

    vis = VisualiseMesh()
    vis.add_mesh(V1, T)
    vis.add_points(V1[I], sphere_radius=0.2, actor_name='I', color=(0., 0., 1.))
    vis.add_points(V1[C], sphere_radius=0.2, actor_name='C', color=(1., 0., 0.))
    vis.camera_actions(('SetParallelProjection', (True,)))
    vis.execute(magnification=4)
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('input', type=str)
    parser.add_argument('aux_scales', type=str)
    parser.add_argument('-c',
                        dest='camera_actions',
                        type=str,
                        default=[],
                        action='append',
                        help='camera actions')
    parser.add_argument('-o',
                        dest='output_directory',
                        type=str,
                        default=None,
                        help='output directory')
    parser.add_argument('--rotations_index',
                        type=int,
                        default=None,
                        help='rotations index')
    parser.add_argument('--magnification',
                        type=int,
                        default=1,
                        help='magnification')
    parser.add_argument('--rigidly_register',
                        action='store_true',
                        default=False)
    parser.add_argument('--normalise_rotations',
                        action='store_true',
                        default=False)

    args = parser.parse_args()

    print ctime()
    print 'args:'
    pprint(args.__dict__)

    z = np.load(args.input)

    vis = VisualiseMesh()

    # V0 (purple)
    V0 = z['V']
    vis.add_mesh(V0, z['T'], actor_name='V0')
    lut = vis.actors['V0'].GetMapper().GetLookupTable()
    lut.SetTableValue(0, *int2dbl(255, 0, 255))

    # setup `solveV0_X`
    T_ = faces_to_cell_array(z['T'])
    adj, W = weights.weights(V0, T_, weights_type='cotan')
    solveV0_X = ARAPVertexSolver(adj, W, V0)

    # load X
    base_X = z['X']
    if args.rotations_index is not None:
        base_X = base_X[args.rotations_index]
    if args.normalise_rotations:
        max_ = np.amax(np.sqrt(np.sum(base_X * base_X, axis=1)))
        base_X /= max_

    # show additional scales
    aux_scales = eval(args.aux_scales)
    print 'aux_scales: ', aux_scales

    rotM = lambda x: quat.rotationMatrix(quat.quat(x))

    N = len(aux_scales)
    cmap = cm.jet(np.linspace(0., 1., N, endpoint=True))

    for i, scale in enumerate(aux_scales):
        print 'scale:', scale
        X = map(lambda x: axScale(scale, x), base_X)
        V0_X = solveV0_X(map(rotM, X))

        if args.rigidly_register:
            A, d = right_multiply_rigid_uniform_scale_transform(V0_X, V0)
            V0_X = np.dot(V0_X, A) + d

        actor_name = 'V0_X_%d' % i
        print 'actor_name:', actor_name
        vis.add_mesh(V0_X, z['T'], actor_name=actor_name)
        lut = vis.actors[actor_name].GetMapper().GetLookupTable()
        lut.SetTableValue(0, *cmap[i, :3])
        vis.actor_properties(actor_name, ('SetRepresentation', (3, )))

    # input frame
    try:
        input_frame = z['input_frame']
    except KeyError:
        pass
    else:
        vis.add_image(input_frame)

    # is visualisation interface or to file?
    interactive_session = args.output_directory is None

    # setup output directory
    if not interactive_session and not os.path.exists(args.output_directory):
        print 'Creating directory: ', args.output_directory
        os.makedirs(args.output_directory)

    # n is the index for output files
    n = count(0)

    # apply camera actions sequentially
    for action in args.camera_actions:
        method, tup, save_after = parse_camera_action(action)
        print '%s(*%s), save_after=%s' % (method, tup, save_after)

        vis.camera_actions((method, tup))

        # save if required
        if not interactive_session and save_after:
            full_path = os.path.join(args.output_directory, '%d.png' % next(n))
            print 'Output: ', full_path
            vis.write(full_path, magnification=args.magnification)

    # show if interactive
    if interactive_session:
        print 'Interactive'
        vis.execute(magnification=args.magnification)