Search in sources :

Example 11 with Pair

use of org.apache.commons.math3.util.Pair in project jstructure by JonStargaryen.

the class SVDSuperimposer method align.

@Override
public StructureAlignmentResult align(AtomContainer reference, AtomContainer query) {
    AtomContainer originalReference = reference;
    AtomContainer originalCandidate = query;
    Pair<GroupContainer, GroupContainer> atomContainerPair = AbstractAlignmentAlgorithm.comparableGroupContainerPair(reference, query, minimalSetOfAtomNames, maximalSetOfAtomNames);
    reference = atomContainerPair.getLeft();
    query = atomContainerPair.getRight();
    // calculate centroids
    double[] centroid1 = reference.calculate().centroid().getValue();
    double[] centroid2 = query.calculate().centroid().getValue();
    // center atoms
    reference.calculate().center();
    query.calculate().center();
    // compose covariance matrix and calculate SVD
    RealMatrix matrix1 = convertToMatrix(reference);
    RealMatrix matrix2 = convertToMatrix(query);
    RealMatrix covariance = matrix2.transpose().multiply(matrix1);
    SingularValueDecomposition svd = new SingularValueDecomposition(covariance);
    // R = (V * U')'
    RealMatrix ut = svd.getU().transpose();
    RealMatrix rotationMatrix = svd.getV().multiply(ut).transpose();
    // check if reflection
    if (new LUDecomposition(rotationMatrix).getDeterminant() < 0) {
        RealMatrix v = svd.getV().transpose();
        v.setEntry(2, 0, (0 - v.getEntry(2, 0)));
        v.setEntry(2, 1, (0 - v.getEntry(2, 1)));
        v.setEntry(2, 2, (0 - v.getEntry(2, 2)));
        rotationMatrix = v.transpose().multiply(ut).transpose();
    }
    double[][] rotation = rotationMatrix.getData();
    // calculate translation
    double[] translation = LinearAlgebra.on(centroid1).subtract(LinearAlgebra.on(centroid2).multiply(rotation)).getValue();
    logger.trace("rotation matrix\n{}\ntranslation vector\n{}", Arrays.deepToString(rotationMatrix.getData()), Arrays.toString(translation));
    /* transform 2nd atom select - employ neutral translation (3D vector of zeros), because the atoms are already
        * centered and calculate RMSD */
    query.calculate().transform(new Transformation(rotation));
    double rmsd = calculateRmsd(reference, query);
    // return alignment
    return new StructureAlignmentResult(originalReference, originalCandidate, query, rmsd, translation, rotation);
}
Also used : Transformation(de.bioforscher.jstructure.mathematics.Transformation) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) RealMatrix(org.apache.commons.math3.linear.RealMatrix) AtomContainer(de.bioforscher.jstructure.model.structure.container.AtomContainer) LUDecomposition(org.apache.commons.math3.linear.LUDecomposition) SingularValueDecomposition(org.apache.commons.math3.linear.SingularValueDecomposition) GroupContainer(de.bioforscher.jstructure.model.structure.container.GroupContainer)

Example 12 with Pair

use of org.apache.commons.math3.util.Pair in project kdeconnect-android by KDE.

the class RemoteKeyboardPlugin method handleSpecialKey.

private boolean handleSpecialKey(int key, boolean shift, boolean ctrl, boolean alt) {
    int keyEvent = specialKeyMap.get(key, 0);
    if (keyEvent == 0)
        return false;
    InputConnection inputConn = RemoteKeyboardService.instance.getCurrentInputConnection();
    // special sequences:
    if (ctrl && (keyEvent == KeyEvent.KEYCODE_DPAD_RIGHT)) {
        // Ctrl + right -> next word
        ExtractedText extractedText = inputConn.getExtractedText(new ExtractedTextRequest(), 0);
        int pos = getCharPos(extractedText, ' ', keyEvent == KeyEvent.KEYCODE_DPAD_RIGHT);
        if (pos == -1)
            pos = currentTextLength(extractedText);
        else
            pos++;
        int startPos = pos;
        int endPos = pos;
        if (shift) {
            // Shift -> select word (otherwise jump)
            Pair<Integer, Integer> sel = currentSelection(extractedText);
            int cursor = currentCursorPos(extractedText);
            //                Log.d("RemoteKeyboardPlugin", "Selection (to right): " + sel.first + " / " + sel.second + " cursor: " + cursor);
            startPos = cursor;
            if (// active selection from left to right -> grow
            sel.first < cursor || // active selection from right to left -> shrink
            sel.first > sel.second)
                startPos = sel.first;
        }
        inputConn.setSelection(startPos, endPos);
    } else if (ctrl && keyEvent == KeyEvent.KEYCODE_DPAD_LEFT) {
        // Ctrl + left -> previous word
        ExtractedText extractedText = inputConn.getExtractedText(new ExtractedTextRequest(), 0);
        int pos = getCharPos(extractedText, ' ', keyEvent == KeyEvent.KEYCODE_DPAD_RIGHT);
        if (pos == -1)
            pos = 0;
        else
            pos++;
        int startPos = pos;
        int endPos = pos;
        if (shift) {
            Pair<Integer, Integer> sel = currentSelection(extractedText);
            int cursor = currentCursorPos(extractedText);
            //                Log.d("RemoteKeyboardPlugin", "Selection (to left): " + sel.first + " / " + sel.second + " cursor: " + cursor);
            startPos = cursor;
            if (// active selection from right to left -> grow
            cursor < sel.first || // active selection from right to left -> shrink
            sel.first < sel.second)
                startPos = sel.first;
        }
        inputConn.setSelection(startPos, endPos);
    } else if (shift && (keyEvent == KeyEvent.KEYCODE_DPAD_LEFT || keyEvent == KeyEvent.KEYCODE_DPAD_RIGHT || keyEvent == KeyEvent.KEYCODE_DPAD_UP || keyEvent == KeyEvent.KEYCODE_DPAD_DOWN || keyEvent == KeyEvent.KEYCODE_MOVE_HOME || keyEvent == KeyEvent.KEYCODE_MOVE_END)) {
        // Shift + up/down/left/right/home/end
        long now = SystemClock.uptimeMillis();
        inputConn.sendKeyEvent(new KeyEvent(now, now, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0));
        inputConn.sendKeyEvent(new KeyEvent(now, now, KeyEvent.ACTION_DOWN, keyEvent, 0, KeyEvent.META_SHIFT_LEFT_ON));
        inputConn.sendKeyEvent(new KeyEvent(now, now, KeyEvent.ACTION_UP, keyEvent, 0, KeyEvent.META_SHIFT_LEFT_ON));
        inputConn.sendKeyEvent(new KeyEvent(now, now, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0));
    } else if (keyEvent == KeyEvent.KEYCODE_NUMPAD_ENTER || keyEvent == KeyEvent.KEYCODE_ENTER) {
        // Enter key
        EditorInfo editorInfo = RemoteKeyboardService.instance.getCurrentInputEditorInfo();
        //            Log.d("RemoteKeyboardPlugin", "Enter: " + editorInfo.imeOptions);
        if (editorInfo != null && (((editorInfo.imeOptions & EditorInfo.IME_FLAG_NO_ENTER_ACTION) == 0) || ctrl)) {
            // Ctrl+Return overrides IME_FLAG_NO_ENTER_ACTION (FIXME: make configurable?)
            // check for special DONE/GO/etc actions first:
            int[] actions = { EditorInfo.IME_ACTION_GO, EditorInfo.IME_ACTION_NEXT, EditorInfo.IME_ACTION_SEND, EditorInfo.IME_ACTION_SEARCH, // note: DONE should be last or we might hide the ime instead of "go"
            EditorInfo.IME_ACTION_DONE };
            for (int i = 0; i < actions.length; i++) {
                if ((editorInfo.imeOptions & actions[i]) == actions[i]) {
                    //                        Log.d("RemoteKeyboardPlugin", "Enter-action: " + actions[i]);
                    inputConn.performEditorAction(actions[i]);
                    return true;
                }
            }
        } else {
            // else: fall back to regular Enter-event:
            //                Log.d("RemoteKeyboardPlugin", "Enter: normal keypress");
            inputConn.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, keyEvent));
            inputConn.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, keyEvent));
        }
    } else {
        // default handling:
        inputConn.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, keyEvent));
        inputConn.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, keyEvent));
    }
    return true;
}
Also used : InputConnection(android.view.inputmethod.InputConnection) KeyEvent(android.view.KeyEvent) EditorInfo(android.view.inputmethod.EditorInfo) ExtractedTextRequest(android.view.inputmethod.ExtractedTextRequest) ExtractedText(android.view.inputmethod.ExtractedText) Pair(android.support.v4.util.Pair)

Example 13 with Pair

use of org.apache.commons.math3.util.Pair in project Rocket.Chat.Android by RocketChat.

the class MessageOptionsDialogFragment method setUpDialog.

private void setUpDialog(final BottomSheetDialog bottomSheetDialog, String messageId) {
    RocketChatCache cache = new RocketChatCache(bottomSheetDialog.getContext());
    String hostname = cache.getSelectedServerHostname();
    EditMessageInteractor editMessageInteractor = getEditMessageInteractor(hostname);
    MessageRepository messageRepository = new RealmMessageRepository(hostname);
    Disposable disposable = messageRepository.getById(messageId).flatMap(it -> {
        if (!it.isPresent()) {
            return Single.just(Pair.<Message, Boolean>create(null, false));
        }
        Message message = it.get();
        return Single.zip(Single.just(message), editMessageInteractor.isAllowed(message), Pair::create);
    }).subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())).observeOn(AndroidSchedulers.mainThread()).subscribe(pair -> {
        if (pair.second) {
            bottomSheetDialog.findViewById(R.id.message_options_info).setVisibility(View.GONE);
            View editView = bottomSheetDialog.findViewById(R.id.message_options_edit_action);
            editView.setVisibility(View.VISIBLE);
            editView.setOnClickListener(view -> internalListener.onEdit(pair.first));
        } else {
            ((TextView) bottomSheetDialog.findViewById(R.id.message_options_info)).setText(R.string.message_options_no_permissions_info);
        }
    }, throwable -> {
        ((TextView) bottomSheetDialog.findViewById(R.id.message_options_info)).setText(R.string.message_options_no_message_info);
        Logger.report(throwable);
    });
    compositeDisposable.add(disposable);
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) MessageRepository(chat.rocket.core.repositories.MessageRepository) BottomSheetDialog(android.support.design.widget.BottomSheetDialog) RealmPublicSettingRepository(chat.rocket.persistence.realm.repositories.RealmPublicSettingRepository) Bundle(android.os.Bundle) UserRepository(chat.rocket.core.repositories.UserRepository) RocketChatCache(chat.rocket.android.RocketChatCache) RoomRepository(chat.rocket.core.repositories.RoomRepository) Dialog(android.app.Dialog) NonNull(android.support.annotation.NonNull) Single(io.reactivex.Single) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) RealmRoomRoleRepository(chat.rocket.persistence.realm.repositories.RealmRoomRoleRepository) PermissionRepository(chat.rocket.core.repositories.PermissionRepository) PublicSettingRepository(chat.rocket.core.repositories.PublicSettingRepository) PermissionInteractor(chat.rocket.core.interactors.PermissionInteractor) View(android.view.View) RoomRoleRepository(chat.rocket.core.repositories.RoomRoleRepository) R(chat.rocket.android.R) RealmRoomRepository(chat.rocket.persistence.realm.repositories.RealmRoomRepository) BottomSheetDialogFragment(android.support.design.widget.BottomSheetDialogFragment) DialogInterface(android.content.DialogInterface) Logger(chat.rocket.android.helper.Logger) EditMessageInteractor(chat.rocket.core.interactors.EditMessageInteractor) Message(chat.rocket.core.models.Message) RealmMessageRepository(chat.rocket.persistence.realm.repositories.RealmMessageRepository) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) TextView(android.widget.TextView) Pair(android.support.v4.util.Pair) RealmUserRepository(chat.rocket.persistence.realm.repositories.RealmUserRepository) BackgroundLooper(chat.rocket.android.BackgroundLooper) RealmPermissionRepository(chat.rocket.persistence.realm.repositories.RealmPermissionRepository) Message(chat.rocket.core.models.Message) EditMessageInteractor(chat.rocket.core.interactors.EditMessageInteractor) RocketChatCache(chat.rocket.android.RocketChatCache) MessageRepository(chat.rocket.core.repositories.MessageRepository) RealmMessageRepository(chat.rocket.persistence.realm.repositories.RealmMessageRepository) RealmMessageRepository(chat.rocket.persistence.realm.repositories.RealmMessageRepository) TextView(android.widget.TextView) View(android.view.View) TextView(android.widget.TextView)

Example 14 with Pair

use of org.apache.commons.math3.util.Pair in project Douya by DreaminginCodeZH.

the class TransitionUtils method makeActivityOptionsBundle.

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static Bundle makeActivityOptionsBundle(Activity activity, View... sharedViews) {
    if (!shouldEnableTransition()) {
        return null;
    }
    ArrayList<Pair<View, String>> sharedElementList = new ArrayList<>();
    for (View sharedView : sharedViews) {
        sharedElementList.add(Pair.create(sharedView, sharedView.getTransitionName()));
    }
    View appbar = activity.findViewById(R.id.appBarWrapper);
    if (appbar != null) {
        sharedElementList.add(Pair.create(appbar, appbar.getTransitionName()));
    }
    //noinspection unchecked
    Pair<View, String>[] sharedElements = sharedElementList.toArray(new Pair[sharedElementList.size()]);
    //noinspection unchecked
    return ActivityOptionsCompat.makeSceneTransitionAnimation(activity, sharedElements).toBundle();
}
Also used : ArrayList(java.util.ArrayList) View(android.view.View) Pair(android.support.v4.util.Pair) TargetApi(android.annotation.TargetApi)

Example 15 with Pair

use of org.apache.commons.math3.util.Pair in project AntennaPod by AntennaPod.

the class CustomMRControllerDialog method fetchArt.

private Pair<Bitmap, Integer> fetchArt(@NonNull MediaDescriptionCompat description) {
    Bitmap iconBitmap = description.getIconBitmap();
    Uri iconUri = description.getIconUri();
    Bitmap art = null;
    if (iconBitmap != null) {
        art = iconBitmap;
    } else if (iconUri != null) {
        try {
            art = Glide.with(getContext().getApplicationContext()).load(iconUri.toString()).asBitmap().diskCacheStrategy(ApGlideSettings.AP_DISK_CACHE_STRATEGY).into(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).get();
        } catch (InterruptedException | ExecutionException e) {
            Log.e(TAG, "Image art load failed", e);
        }
    }
    int backgroundColor = 0;
    if (art != null && art.getWidth() * 9 < art.getHeight() * 16) {
        // Portrait art requires dominant color as background color.
        Palette palette = new Palette.Builder(art).maximumColorCount(1).generate();
        backgroundColor = palette.getSwatches().isEmpty() ? 0 : palette.getSwatches().get(0).getRgb();
    }
    return new Pair<>(art, backgroundColor);
}
Also used : Palette(android.support.v7.graphics.Palette) Bitmap(android.graphics.Bitmap) Uri(android.net.Uri) Pair(android.support.v4.util.Pair)

Aggregations

Pair (android.support.v4.util.Pair)32 ArrayList (java.util.ArrayList)17 IntStream (java.util.stream.IntStream)16 View (android.view.View)14 Collectors (java.util.stream.Collectors)14 ActivityOptionsCompat (android.support.v4.app.ActivityOptionsCompat)12 Logger (org.apache.logging.log4j.Logger)12 ParamUtils (org.broadinstitute.hellbender.utils.param.ParamUtils)12 Intent (android.content.Intent)10 IOException (java.io.IOException)10 java.util (java.util)10 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)10 Pair (org.apache.commons.lang3.tuple.Pair)10 RealMatrix (org.apache.commons.math3.linear.RealMatrix)10 Pair (org.apache.commons.math3.util.Pair)10 File (java.io.File)9 Array2DRowRealMatrix (org.apache.commons.math3.linear.Array2DRowRealMatrix)8 UserException (org.broadinstitute.hellbender.exceptions.UserException)8 org.broadinstitute.hellbender.tools.exome (org.broadinstitute.hellbender.tools.exome)8 VisibleForTesting (com.google.common.annotations.VisibleForTesting)7