<commit_before>package com.google.android.libraries.motionphotoreader;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.google.android.libraries.motionphotoreader.Constants.EPS;
import static com.google.android.libraries.motionphotoreader.Constants.IDENTITY;

/**
 * Represents a 3x3 homography transformation in row-major matrix form.
 */
class HomographyMatrix {

    private List<Float> matrix = new ArrayList<>();

    public HomographyMatrix() {
        for (float f : IDENTITY) {
            matrix.add(f);
        }
    }

    public HomographyMatrix(List<Float> matrix) {
        if (matrix.size() != 9) {
            throw new RuntimeException("List has incorrect number of elements: " + matrix.size());
        } else {
            this.matrix = matrix;
        }
    }

    public HomographyMatrix leftMultiplyBy(HomographyMatrix otherMatrix) {
        List<Float> product = new ArrayList<>();
        for (int r = 0; r < 3; r++) {
            for (int c = 0; c < 3; c++) {
                float matrixElement = otherMatrix.get(r, 0) * this.get(0, c) +
                        otherMatrix.get(r, 1) * this.get(1, c) +
                        otherMatrix.get(r, 2) * this.get(2, c);
                product.add(matrixElement);
            }
        }
        return new HomographyMatrix(product);
    }

    public HomographyMatrix rightMultiplyBy(HomographyMatrix otherMatrix) {
        List<Float> product = new ArrayList<>();
        for (int r = 0; r < 3; r++) {
            for (int c = 0; c < 3; c++) {
                float matrixElement = this.get(r, 0) * otherMatrix.get(0, c) +
                        this.get(r, 1) * otherMatrix.get(1, c) +
                        this.get(r, 2) * otherMatrix.get(2, c);
                product.add(matrixElement);
            }
        }
        return new HomographyMatrix(product);
    }

    public float get(int r, int c) {
        return matrix.get(3 * r + c);
    }

    public void set(int r, int c, float val) {
        matrix.set(3 * r + c, val);
    }

    public HomographyMatrix add(HomographyMatrix otherMatrix) {
        List<Float> sum = new ArrayList<>();
        for (int r = 0; r < 3; r++) {
            for (int c = 0; c < 3; c++) {
                float matrixElement = otherMatrix.get(r, c) + this.get(r, c);
                sum.add(matrixElement);
            }
        }
        return new HomographyMatrix(sum);
    }

    public HomographyMatrix multiplyScalar(float s) {
        List<Float> scaled = new ArrayList<>();
        for (int r = 0; r < 3; r++) {
            for (int c = 0; c < 3; c++) {
                float matrixElement = s * this.get(r, c);
                scaled.add(matrixElement);
            }
        }
        return new HomographyMatrix(scaled);

    }

    public static HomographyMatrix createRotationMatrix(float degrees) {
        float cosThetaA = (float) Math.cos(Math.toRadians(degrees));
        float sinThetaA = (float) Math.sin(Math.toRadians(degrees));
        float cosThetaB = (float) Math.cos(Math.toRadians(degrees));
        float sinThetaB = (float) Math.sin(Math.toRadians(degrees));

        HomographyMatrix rotationMatrix = new HomographyMatrix();
        rotationMatrix.set(0, 0, cosThetaA);
        rotationMatrix.set(0, 1, -sinThetaA);
        rotationMatrix.set(1, 0, sinThetaA);
        rotationMatrix.set(1, 1, cosThetaA);

        return rotationMatrix;
    }

    public HomographyMatrix convertFromImageToGL(int imageWidth, int imageHeight) {
        float halfW = imageWidth * 1.0f / 2.0f;
        float halfH = imageHeight * 1.0f / 2.0f;

        float[] t1 = {
                halfW,  0.0f, halfW,
                0.0f, -halfH, halfH,
                0.0f,   0.0f, 1.0f
        };
        float[] t2 = {
                1.0f / halfW,  0.0f, -1.0f,
                0.0f, -1.0f / halfH,  1.0f,
                0.0f,          0.0f,  1.0f<commit_msg>Add scale matrix creator in HomographyMatrix.<commit_after>package com.google.android.libraries.motionphotoreader;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.google.android.libraries.motionphotoreader.Constants.EPS;
import static com.google.android.libraries.motionphotoreader.Constants.IDENTITY;

/**
 * Represents a 3x3 homography transformation in row-major matrix form.
 */
class HomographyMatrix {

    private List<Float> matrix = new ArrayList<>();

    public HomographyMatrix() {
        for (float f : IDENTITY) {
            matrix.add(f);
        }
    }

    public HomographyMatrix(List<Float> matrix) {
        if (matrix.size() != 9) {
            throw new RuntimeException("List has incorrect number of elements: " + matrix.size());
        } else {
            this.matrix = matrix;
        }
    }

    public HomographyMatrix leftMultiplyBy(HomographyMatrix otherMatrix) {
        List<Float> product = new ArrayList<>();
        for (int r = 0; r < 3; r++) {
            for (int c = 0; c < 3; c++) {
                float matrixElement = otherMatrix.get(r, 0) * this.get(0, c) +
                        otherMatrix.get(r, 1) * this.get(1, c) +
                        otherMatrix.get(r, 2) * this.get(2, c);
                product.add(matrixElement);
            }
        }
        return new HomographyMatrix(product);
    }

    public HomographyMatrix rightMultiplyBy(HomographyMatrix otherMatrix) {
        List<Float> product = new ArrayList<>();
        for (int r = 0; r < 3; r++) {
            for (int c = 0; c < 3; c++) {
                float matrixElement = this.get(r, 0) * otherMatrix.get(0, c) +
                        this.get(r, 1) * otherMatrix.get(1, c) +
                        this.get(r, 2) * otherMatrix.get(2, c);
                product.add(matrixElement);
            }
        }
        return new HomographyMatrix(product);
    }

    public float get(int r, int c) {
        return matrix.get(3 * r + c);
    }

    public void set(int r, int c, float val) {
        matrix.set(3 * r + c, val);
    }

    public HomographyMatrix add(HomographyMatrix otherMatrix) {
        List<Float> sum = new ArrayList<>();
        for (int r = 0; r < 3; r++) {
            for (int c = 0; c < 3; c++) {
                float matrixElement = otherMatrix.get(r, c) + this.get(r, c);
                sum.add(matrixElement);
            }
        }
        return new HomographyMatrix(sum);
    }

    public HomographyMatrix multiplyScalar(float s) {
        List<Float> scaled = new ArrayList<>();
        for (int r = 0; r < 3; r++) {
            for (int c = 0; c < 3; c++) {
                float matrixElement = s * this.get(r, c);
                scaled.add(matrixElement);
            }
        }
        return new HomographyMatrix(scaled);

    }

    public static HomographyMatrix createRotationMatrix(float degrees) {
        float cosThetaA = (float) Math.cos(Math.toRadians(degrees));
        float sinThetaA = (float) Math.sin(Math.toRadians(degrees));
        float cosThetaB = (float) Math.cos(Math.toRadians(degrees));
        float sinThetaB = (float) Math.sin(Math.toRadians(degrees));

        HomographyMatrix rotationMatrix = new HomographyMatrix();
        rotationMatrix.set(0, 0, cosThetaA);
        rotationMatrix.set(0, 1, -sinThetaA);
        rotationMatrix.set(1, 0, sinThetaA);
        rotationMatrix.set(1, 1, cosThetaA);

        return rotationMatrix;
    }

    public static HomographyMatrix createScaleMatrix(float xScale, float yScale) {
        HomographyMatrix scaleMatrix = new HomographyMatrix();
        scaleMatrix.set(0,  0, xScale);
        scaleMatrix.set(1,  1, yScale);
        return scaleMatrix;
    }

    public HomographyMatrix convertFromImageToGL(int imageWidth, int imageHeight) {
        float halfW = imageWidth * 1.0f / 2.0f;
        float halfH = imageHeight * 1.0f / 2.0f;

        float[] t1 = {
                halfW,  0.0f, halfW,
                0.0f, -halfH, halfH,
                0.0f,   0.0f, 1.0f
        };
        float[] t2 = {
                1.0f / halfW,  0.0f, -1.0f,
                0.0f, -1.0f / halfH,  1.0f,
                0.0f,          0.0f,  1.0f
{'fraction_non_alphanumeric': 0.12195121951219512, 'fraction_numerical': 0.019855374433141317, 'mean_word_length': 1.9059829059829059, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5401602', 'n_tokens_mistral': 2607, 'n_tokens_neox': 2461, 'n_words': 621}