Skip to content

Commit

Permalink
More matrix stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
thegreystone committed Jun 18, 2017
1 parent b050683 commit ad0e839
Show file tree
Hide file tree
Showing 3 changed files with 316 additions and 20 deletions.
40 changes: 20 additions & 20 deletions robo4j-math/src/main/java/com/robo4j/math/geometry/Matrix4d.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,25 @@
* @author Miroslav Wengner (@miragemiko)
*/
public class Matrix4d {
public float m11;
public float m12;
public float m13;
public float m14;
public float m21;
public float m22;
public float m23;
public float m24;
public float m31;
public float m32;
public float m33;
public float m34;
public float m41;
public float m42;
public float m43;
public float m44;
public double m11;
public double m12;
public double m13;
public double m14;
public double m21;
public double m22;
public double m23;
public double m24;
public double m31;
public double m32;
public double m33;
public double m34;
public double m41;
public double m42;
public double m43;
public double m44;

public Matrix4d(float m11, float m12, float m13, float m14, float m21, float m22, float m23, float m24, float m31, float m32, float m33,
float m34, float m41, float m42, float m43, float m44) {
public Matrix4d(double m11, double m12, double m13, double m14, double m21, double m22, double m23, double m24, double m31, double m32, double m33,
double m34, double m41, double m42, double m43, double m44) {
this.m11 = m11;
this.m12 = m12;
this.m13 = m13;
Expand All @@ -61,7 +61,7 @@ public Matrix4d(float m11, float m12, float m13, float m14, float m21, float m22
this.m44 = m34;
}

public Matrix4d(float[] matrix) {
public Matrix4d(double[] matrix) {
if (matrix.length != 16) {
throw new IllegalArgumentException("Array argument for Matrix3f must be 9 elements long");
}
Expand Down Expand Up @@ -100,7 +100,7 @@ public void transform(Tuple4d tuple) {
* Transposes the matrix.
*/
public void transpose() {
float tmp = m12;
double tmp = m12;
m12 = m21;
m21 = tmp;
tmp = m13;
Expand Down
148 changes: 148 additions & 0 deletions robo4j-math/src/main/java/com/robo4j/math/geometry/Matrix4f.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* Copyright (c) 2014, 2017, Marcus Hirt, Miroslav Wengner
*
* Robo4J is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Robo4J is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Robo4J. If not, see <http://www.gnu.org/licenses/>.
*/
package com.robo4j.math.geometry;

/**
* A four dimensional matrix. (The things we do to save the array size field
* bytes... ;))
*
* @author Marcus Hirt (@hirt)
* @author Miroslav Wengner (@miragemiko)
*/
public class Matrix4f {
public float m11;
public float m12;
public float m13;
public float m14;
public float m21;
public float m22;
public float m23;
public float m24;
public float m31;
public float m32;
public float m33;
public float m34;
public float m41;
public float m42;
public float m43;
public float m44;

public Matrix4f(float m11, float m12, float m13, float m14, float m21, float m22, float m23, float m24, float m31, float m32, float m33,
float m34, float m41, float m42, float m43, float m44) {
this.m11 = m11;
this.m12 = m12;
this.m13 = m13;
this.m14 = m14;
this.m21 = m21;
this.m22 = m22;
this.m23 = m23;
this.m24 = m24;
this.m31 = m31;
this.m32 = m32;
this.m33 = m33;
this.m34 = m34;
this.m41 = m31;
this.m42 = m32;
this.m43 = m33;
this.m44 = m34;
}

public Matrix4f(float[] matrix) {
if (matrix.length != 16) {
throw new IllegalArgumentException("Array argument for Matrix3f must be 9 elements long");
}
m11 = matrix[0];
m12 = matrix[1];
m13 = matrix[2];
m14 = matrix[3];
m21 = matrix[4];
m22 = matrix[5];
m23 = matrix[6];
m24 = matrix[7];
m31 = matrix[8];
m32 = matrix[9];
m33 = matrix[10];
m34 = matrix[11];
m41 = matrix[12];
m42 = matrix[13];
m43 = matrix[14];
m44 = matrix[15];
}

/**
* Transforms the tuple by multiplying with this matrix.
*
* @param tuple
* the tuple to multiply with this matrix.
*/
public void transform(Tuple4f tuple) {
tuple.set(m11 * tuple.x + m12 * tuple.y + m13 * tuple.z + m14 * tuple.t,
m21 * tuple.x + m22 * tuple.y + m23 * tuple.z + m24 * tuple.t,
m31 * tuple.x + m32 * tuple.y + m33 * tuple.z + m34 * tuple.t,
m41 * tuple.x + m42 * tuple.y + m43 * tuple.z + m44 * tuple.t);
}

/**
* Transposes the matrix.
*/
public void transpose() {
float tmp = m12;
m12 = m21;
m21 = tmp;
tmp = m13;
m13 = m31;
m31 = tmp;
tmp = m14;
m14 = m41;
m41 = tmp;
tmp = m23;
m23 = m32;
m32 = tmp;
tmp = m34;
m34 = m43;
m43 = tmp;
}

/**
* Like transform, but creating a new tuple without changing the old one.
*
* @param tuple
* the tuple to multiply with.
* @return the result from multiplying this matrix with the tuple.
*/
public Tuple4f multiply(Tuple4f tuple) {
float x = m11 * tuple.x + m12 * tuple.y + m13 * tuple.z + m14 * tuple.t;
float y = m21 * tuple.x + m22 * tuple.y + m23 * tuple.z + m24 * tuple.t;
float z = m31 * tuple.x + m32 * tuple.y + m33 * tuple.z + m34 * tuple.t;
float t = m41 * tuple.x + m42 * tuple.y + m43 * tuple.z + m44 * tuple.t;
return new Tuple4f(x, y, z, t);
}

/**
* Creates an identity matrix.
*/
public static Matrix4f createIdentity() {
return new Matrix4f(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
}

@Override
public String toString() {
return String.format(
"m11:%f, m12:%f, m13:%f, m14:%f, m21:%f, m22:%f, m23:%f, m24:%f, m31:%f, m32:%f, m33:%f, m34:%f, m41:%f, 42:%f, m43:%f, m44:%f",
m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, m41, m42, m43, m44);
}
}
148 changes: 148 additions & 0 deletions robo4j-math/src/main/java/com/robo4j/math/geometry/Matrix4i.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* Copyright (c) 2014, 2017, Marcus Hirt, Miroslav Wengner
*
* Robo4J is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Robo4J is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Robo4J. If not, see <http://www.gnu.org/licenses/>.
*/
package com.robo4j.math.geometry;

/**
* A four dimensional matrix. (The things we do to save the array size field
* bytes... ;))
*
* @author Marcus Hirt (@hirt)
* @author Miroslav Wengner (@miragemiko)
*/
public class Matrix4i {
public int m11;
public int m12;
public int m13;
public int m14;
public int m21;
public int m22;
public int m23;
public int m24;
public int m31;
public int m32;
public int m33;
public int m34;
public int m41;
public int m42;
public int m43;
public int m44;

public Matrix4i(int m11, int m12, int m13, int m14, int m21, int m22, int m23, int m24, int m31, int m32, int m33,
int m34, int m41, int m42, int m43, int m44) {
this.m11 = m11;
this.m12 = m12;
this.m13 = m13;
this.m14 = m14;
this.m21 = m21;
this.m22 = m22;
this.m23 = m23;
this.m24 = m24;
this.m31 = m31;
this.m32 = m32;
this.m33 = m33;
this.m34 = m34;
this.m41 = m31;
this.m42 = m32;
this.m43 = m33;
this.m44 = m34;
}

public Matrix4i(int[] matrix) {
if (matrix.length != 16) {
throw new IllegalArgumentException("Array argument for Matrix3f must be 9 elements long");
}
m11 = matrix[0];
m12 = matrix[1];
m13 = matrix[2];
m14 = matrix[3];
m21 = matrix[4];
m22 = matrix[5];
m23 = matrix[6];
m24 = matrix[7];
m31 = matrix[8];
m32 = matrix[9];
m33 = matrix[10];
m34 = matrix[11];
m41 = matrix[12];
m42 = matrix[13];
m43 = matrix[14];
m44 = matrix[15];
}

/**
* Transforms the tuple by multiplying with this matrix.
*
* @param tuple
* the tuple to multiply with this matrix.
*/
public void transform(Tuple4f tuple) {
tuple.set(m11 * tuple.x + m12 * tuple.y + m13 * tuple.z + m14 * tuple.t,
m21 * tuple.x + m22 * tuple.y + m23 * tuple.z + m24 * tuple.t,
m31 * tuple.x + m32 * tuple.y + m33 * tuple.z + m34 * tuple.t,
m41 * tuple.x + m42 * tuple.y + m43 * tuple.z + m44 * tuple.t);
}

/**
* Transposes the matrix.
*/
public void transpose() {
int tmp = m12;
m12 = m21;
m21 = tmp;
tmp = m13;
m13 = m31;
m31 = tmp;
tmp = m14;
m14 = m41;
m41 = tmp;
tmp = m23;
m23 = m32;
m32 = tmp;
tmp = m34;
m34 = m43;
m43 = tmp;
}

/**
* Like transform, but creating a new tuple without changing the old one.
*
* @param tuple
* the tuple to multiply with.
* @return the result from multiplying this matrix with the tuple.
*/
public Tuple4i multiply(Tuple4i tuple) {
int x = m11 * tuple.x + m12 * tuple.y + m13 * tuple.z + m14 * tuple.t;
int y = m21 * tuple.x + m22 * tuple.y + m23 * tuple.z + m24 * tuple.t;
int z = m31 * tuple.x + m32 * tuple.y + m33 * tuple.z + m34 * tuple.t;
int t = m41 * tuple.x + m42 * tuple.y + m43 * tuple.z + m44 * tuple.t;
return new Tuple4i(x, y, z, t);
}

/**
* Creates an identity matrix.
*/
public static Matrix4i createIdentity() {
return new Matrix4i(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
}

@Override
public String toString() {
return String.format(
"m11:%f, m12:%f, m13:%f, m14:%f, m21:%f, m22:%f, m23:%f, m24:%f, m31:%f, m32:%f, m33:%f, m34:%f, m41:%f, 42:%f, m43:%f, m44:%f",
m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, m41, m42, m43, m44);
}
}

0 comments on commit ad0e839

Please sign in to comment.