def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('solver_path')
    parser.add_argument('output_path')
    parser.add_argument('--N', type=int, default=-1)
    args = parser.parse_args()

    print '<- %s' % args.solver_path

    solver = load(args.solver_path)
    if args.N < 0:
        args.N = solver.n
    solve_arap = solver.get_arap_solver()

    print '... ',

    states = []
    for i in xrange(args.N):
        Xi = solver.get_instance_rotations(i)
        Ri = map(lambda x: rotationMatrix(quat(x)), Xi)
        Vi = solve_arap(Ri)
        s = np.array([[1.0]], dtype=np.float64)
        Xg = np.array([[0.0, 0.0, 0.0]], dtype=np.float64)
        states.append(dict(T=solver.T, V=Vi, X=Xi, s=s, Xg=Xg,
                           image=solver.frames[i]))

        print '%d ' % i,
        sys.stdout.flush()

    sys.stdout.write('\n')

    print '-> %s' % args.output_path
    dump(args.output_path, dict(states=states, has_states=True))
Exemplo n.º 2
0
def save_solver(working, iteration, solver, verbose=True):
    full_path = os.path.join(working, str(iteration) + '.dat')

    if verbose:
        print '-> %s' % full_path

    pickle_.dump(full_path, solver)
Exemplo n.º 3
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))
    def save_solver(self):
        path = QFileDialog.getSaveFileName(self, 'Save', self.solver_path,
                                           '*.dat')

        if path.isEmpty():
            return

        dump(str(path), self.solver)
        self.solver_path = path
Exemplo n.º 5
0
def save_states(working, iteration, index, states, verbose=True):
    path = os.path.join(working, str(iteration))
    if not os.path.exists(path):
        os.makedirs(path)

    full_path = os.path.join(path, str(index) + '.dat')

    if verbose:
        print '-> %s' % full_path

    pickle_.dump(full_path, states)
def main_solve_multiple():
    args = parse_args()
    pprint(args.__dict__)

    arap_solver = np.load(args.solver)

    N = len(arap_solver.frames)
    T = arap_solver.T
    V = arap_solver._s.V1[args.i]

    Vb = [V.copy(), np.zeros_like(V)]

    s = map(lambda i: np.r_[1.].reshape(-1,1), xrange(N))
    xg = map(lambda i: np.r_[0., 0., 0.].reshape(-1,3), xrange(N))
    vd = map(lambda i: np.r_[0., 0., 0.].reshape(-1,3), xrange(N))
    y = map(lambda i: np.r_[1.0].reshape(-1,1), xrange(N))

    U = map(lambda i: arap_solver._s.U[i].copy(), xrange(N))
    L = map(lambda i: arap_solver._s.L[i].copy(), xrange(N))

    C = arap_solver.C
    P = arap_solver.P
    S = arap_solver.S
    SN = arap_solver.SN

    for i in xrange(args.max_restarts):
        status = solve_multiple(
            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:'
        pprint(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
def main():
    args = parse_args()
    pprint(args.__dict__)

    arap_solver = np.load(args.solver)
    lbs_solver = BareLBSSolver(
        arap_solver.T,
        arap_solver.V0.copy(),
        arap_solver.S,
        arap_solver.SN,
        arap_solver.C,
        arap_solver.P,
        arap_solver.frames)

    lbs_solver.setup(arap_solver._s.U,
                     arap_solver._s.L,
                     D=1)

    states = []
    for i in xrange(2):
        status, _states, time_taken = lbs_solver(
            args.lambdas, 
            args.preconditioners,
            args.max_restarts,
            args.narrowband,
            **args.solver_options)

        print status
        print 'Time taken: %.3fs' % time_taken

        lbs_solver.add_basis()

        states += _states

    if args.output is not None:
        print '-> %s' % args.output
        dump(args.output, lbs_solver)
    
    if args.output_dir is not None:
        lbs_solver.export_states(states, args.output_dir)
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()
Exemplo n.º 9
0
def main():
    args = parse_args()
    pprint(args.__dict__)

    solver = pickle_.load(args.solver)

    update_keys = ['solver_options', 'max_restarts', 'lambdas',
                   'preconditioners', 
                   'use_creasing_silhouette',
                   'use_area_weighted_silhouette']

    new_settings = {k:getattr(args,k) for k in update_keys}

    for key, arr in new_settings.iteritems():
        if arr is None:
            continue
        setattr(solver, key, arr)

    solver._setup_lambdas()

    if args.initial_Xgb is not None:
        solver._setup_global_rotations(kg=solver.kg, initial_Xgb=args.initial_Xgb)
    if args.initial_Xb is not None:
        solver._setup_rotations(ki=solver.ki, initial_Xb=args.initial_Xb)

    save_solver_states = partial(save_states, args.working, verbose=True)
    save_intermediate_solver = partial(save_solver, args.working, verbose=True)

    overall_time = Timer()

    if args.action == 'update_silhouette':
        solver.parallel_solve_silhouettes(n=args.num_processes)
    else:
        # first forward-pass with no silhouette used
        if args.outer_loops[0] == -1:
            print '[# -1]'
            for i in xrange(solver.n):
                if i > 0:
                    solver._s.V1[i].flat = solver._s.V1[i-1].flat

                callback, states = solver.solve_instance_callback(i)

                solver.solve_instance(i, fixed_global_rotation=False,
                               fixed_scale=True,
                               no_silhouette=True,
                               callback=callback)

                solver.solve_instance(i, fixed_global_rotation=False,
                               fixed_scale=False,
                               no_silhouette=True,
                               callback=callback)

                save_solver_states(-1, i, states)

            save_intermediate_solver(-1, solver)

            args.outer_loops.pop(0)

            if args.initialise_silhouette:
                solver.parallel_solve_silhouettes(n=args.num_processes,
                                                  chunksize=max(
                                                    solver.n / args.num_processes, 
                                                    1),
                                                  verbose=True)

        for l in args.outer_loops:
            print '[# %d] Complete:' % l

            outer_timer = Timer()

            # solve for the core geometry
            callback, core_states = solver.solve_core_callback()

            print '[# %d] Core:' % l
            t = solver.solve_core(fixed_Xgb=args.fixed_Xgb, callback=callback)

            # print '[# %d] Core: %.3fs' % (l, t)
            solver.solver_options['verbosenessLevel'] = 1

            save_solver_states(l, 'core', core_states)

            if l > args.solve_silhouette_after:
                solver.parallel_solve_silhouettes(n=args.num_processes,
                                                  chunksize=max(
                                                    solver.n / args.num_processes, 
                                                    1),
                                                  verbose=True)
                
            # solve for each instance
            for i in xrange(solver.n):
                callback, states = solver.solve_instance_callback(i)

                print '[# %d] Instance: %d' % (l, i)
                t = solver.solve_instance(
                    i, fixed_global_rotation=False,
                    fixed_scale=False,
                    no_silhouette=False,
                    callback=callback)
                print '[# %d] Instance: %d, %.3fs' % (l, i, t)
                save_solver_states(l, i, states)

            print '[# %d] Complete: %.3fs' % (l, outer_timer())

            save_intermediate_solver(l, solver)

    print 'Complete time taken: %.3fs' % overall_time()

    head, tail = os.path.split(args.solver)
    root, ext = os.path.splitext(tail)
    args_path = os.path.join(args.working, root + '_ARGS.dat')

    print '-> %s' % args_path
    pickle_.dump(args_path, args.__dict__)