Skip to content

Commit 54f3a2f

Browse files
authored
Add files via upload
1 parent e0e634d commit 54f3a2f

File tree

5 files changed

+798
-0
lines changed

5 files changed

+798
-0
lines changed

Matrix3x3.java

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
package trrt.rendering3d.primitives;
2+
public class Matrix3x3
3+
{
4+
//R means row and C means column. R2C3 would be second row third column.
5+
public final double R1C1, R1C2, R1C3, R2C1, R2C2, R2C3, R3C1, R3C2, R3C3;
6+
7+
//overloaded constructor which accepts three Vector3s.
8+
public Matrix3x3(Vector3 column1, Vector3 column2, Vector3 column3)
9+
{
10+
R1C1 = column1.x; R1C2 = column2.x; R1C3 = column3.x;
11+
R2C1 = column1.y; R2C2 = column2.y; R2C3 = column3.y;
12+
R3C1 = column1.z; R3C2 = column2.z; R3C3 = column3.z;
13+
}
14+
15+
//overloaded constructor which allows all 9 values of the matrix.
16+
public Matrix3x3(double r1c1, double r1c2, double r1c3, double r2c1, double r2c2, double r2c3, double r3c1, double r3c2, double r3c3)
17+
{
18+
R1C1 = r1c1; R1C2 = r1c2; R1C3 = r1c3;
19+
R2C1 = r2c1; R2C2 = r2c2; R2C3 = r2c3;
20+
R3C1 = r3c1; R3C2 = r3c2; R3C3 = r3c3;
21+
}
22+
23+
//formats the values in the matrix into a string.
24+
public String toString()
25+
{
26+
return String.format("\n|%39s\n|%10.2f%10.2f%10.2f%9s\n|%39s\n|%10.2f%10.2f%10.2f%9s\n|%39s\n|%10.2f%10.2f%10.2f%9s\n|%39s\n",
27+
"|", R1C1, R1C2, R1C3, "|", "|", R2C1, R2C2, R2C3, "|", "|", R3C1, R3C2, R3C3, "|", "|");
28+
}
29+
30+
//returns the determinant of the 3x3 matrix.
31+
public double getDeterminant()
32+
{
33+
return R1C1*(R2C2*R3C3-R2C3*R3C2)-R1C2*(R2C1*R3C3-R2C3*R3C1)+R1C3*(R2C1*R3C2-R2C2*R3C1);
34+
}
35+
36+
//returns the cofactor matrix.
37+
public Matrix3x3 getCofactorMatrix()
38+
{
39+
return new Matrix3x3
40+
(
41+
R2C2*R3C3-R2C3*R3C2, -(R2C1*R3C3-R2C3*R3C1), R2C1*R3C2-R2C2*R3C1,
42+
-(R1C2*R3C3-R1C3*R3C2), R1C1*R3C3-R1C3*R3C1, -(R1C1*R3C2-R1C2*R3C1),
43+
R1C2*R2C3-R1C3*R2C2, -(R1C1*R2C3-R1C3*R2C1), R1C1*R2C2-R1C2*R2C1
44+
);
45+
}
46+
47+
//returns the adjugate matrix, basically just the transposed cofactor matrix.
48+
public Matrix3x3 getAdjugateMatrix()
49+
{
50+
return new Matrix3x3
51+
(
52+
R2C2*R3C3-R2C3*R3C2, -(R1C2*R3C3-R1C3*R3C2), R1C2*R2C3-R1C3*R2C2,
53+
-(R2C1*R3C3-R2C3*R3C1), R1C1*R3C3-R1C3*R3C1, -(R1C1*R2C3-R1C3*R2C1),
54+
R2C1*R3C2-R2C2*R3C1, -(R1C1*R3C2-R1C2*R3C1), R1C1*R2C2-R1C2*R2C1
55+
);
56+
}
57+
58+
//returns the inverse of the matrix, which is just the adjugate/det
59+
public Matrix3x3 getInverse()
60+
{
61+
return Matrix3x3.multiply(getAdjugateMatrix(), 1/getDeterminant());
62+
}
63+
64+
//#region ----------- static methods -------------
65+
66+
//applies matrix m1 to matrix m2 and returns the resulting matrix. order matters!
67+
public static Matrix3x3 multiply(Matrix3x3 m1, Matrix3x3 m2)
68+
{
69+
return new Matrix3x3(
70+
m1.R1C1*m2.R1C1 + m1.R1C2*m2.R2C1 + m1.R1C3*m2.R3C1, m1.R1C1*m2.R1C2 + m1.R1C2*m2.R2C2 + m1.R1C3*m2.R3C2, m1.R1C1*m2.R1C3 + m1.R1C2*m2.R2C3 + m1.R1C3*m2.R3C3,
71+
m1.R2C1*m2.R1C1 + m1.R2C2*m2.R2C1 + m1.R2C3*m2.R3C1, m1.R2C1*m2.R1C2 + m1.R2C2*m2.R2C2 + m1.R2C3*m2.R3C2, m1.R2C1*m2.R1C3 + m1.R2C2*m2.R2C3 + m1.R2C3*m2.R3C3,
72+
m1.R3C1*m2.R1C1 + m1.R3C2*m2.R2C1 + m1.R3C3*m2.R3C1, m1.R3C1*m2.R1C2 + m1.R3C2*m2.R2C2 + m1.R3C3*m2.R3C2, m1.R3C1*m2.R1C3 + m1.R3C2*m2.R2C3 + m1.R3C3*m2.R3C3);
73+
}
74+
75+
//multiplies a matrix by a scalar value
76+
public static Matrix3x3 multiply(Matrix3x3 matrix, double scalar)
77+
{
78+
return new Matrix3x3
79+
(
80+
matrix.R1C1*scalar, matrix.R1C2*scalar, matrix.R1C3*scalar,
81+
matrix.R2C1*scalar, matrix.R2C2*scalar, matrix.R2C3*scalar,
82+
matrix.R3C1*scalar, matrix.R3C2*scalar, matrix.R3C3*scalar
83+
);
84+
}
85+
86+
//
87+
public static Matrix3x3 rotationMatrixAxisX(double angle)
88+
{
89+
//local variables to mitigate preforming the same slow trig function multiple times.
90+
double sinAngle = Math.sin(angle);
91+
double cosAngle = Math.sqrt(1-sinAngle*sinAngle); //same as math.cos function
92+
93+
/* | cos -sin 0 |
94+
| sin cos 0 |
95+
| 0 0 1 | */
96+
97+
return new Matrix3x3
98+
(
99+
1, 0, 0,
100+
0, cosAngle, -sinAngle,
101+
0, sinAngle, cosAngle
102+
);
103+
}
104+
105+
public static Matrix3x3 rotationMatrixAxisY(double angle)
106+
{
107+
//local variables to mitigate preforming the same slow trig function multiple times.
108+
double sinAngle = Math.sin(angle);
109+
double cosAngle = Math.sqrt(1-sinAngle*sinAngle); //same as math.cos function
110+
111+
/* | cos 0 sin |
112+
| 0 1 0 |
113+
|-sin 0 cos | */
114+
115+
return new Matrix3x3
116+
(
117+
cosAngle, 0, sinAngle,
118+
0, 1, 0,
119+
-sinAngle, 0, cosAngle
120+
);
121+
}
122+
123+
public static Matrix3x3 rotationMatrixAxisZ(double angle)
124+
{
125+
//local variables to mitigate preforming the same slow trig function multiple times.
126+
double sinAngle = Math.sin(angle);
127+
double cosAngle = Math.sqrt(1-sinAngle*sinAngle); //same as math.cos function
128+
129+
/* | cos -sin 0 |
130+
| sin cos 0 |
131+
| 0 0 1 | */
132+
133+
return new Matrix3x3
134+
(
135+
cosAngle, -sinAngle, 0,
136+
sinAngle, cosAngle, 0,
137+
0, 0, 1
138+
);
139+
}
140+
141+
//returns a matrix which can preform a rotation "angle" radians about "axis"
142+
public static Matrix3x3 axisAngleMatrix(Vector3 axis, double angle)
143+
{
144+
axis = axis.getNormalized();
145+
146+
//local variables to mitigate preforming the same slow trig function multiple times.
147+
double sin = Math.sin(angle);
148+
double cos = Math.sqrt(1-sin*sin); //same as math.cos function
149+
double cos1 = 1-cos;
150+
151+
return new Matrix3x3
152+
(
153+
cos+axis.x*axis.x*cos1, axis.x*axis.y*cos1-axis.z*sin, axis.x*axis.z*cos1+axis.y*sin,
154+
axis.y*axis.x*cos1+axis.z*sin, cos+axis.y*axis.y*cos1, axis.y*axis.z*cos1-axis.x*sin,
155+
axis.z*axis.x*cos1-axis.y*sin, axis.z*axis.y*cos1+axis.x*sin, cos+axis.z*axis.z*cos1
156+
);
157+
}
158+
159+
//#endregion
160+
}

Plane.java

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package trrt.rendering3d.primitives;
2+
public class Plane
3+
{
4+
//just for reference if ever needed.
5+
public static final Plane XZ_PLANE = new Plane(new Vector3(0, 0, 0), new Vector3(0, 1, 0));
6+
public static final Plane XY_PLANE = new Plane(new Vector3(0, 0, 0), new Vector3(0, 0, 1));
7+
public static final Plane ZY_PLANE = new Plane(new Vector3(0, 0, 0), new Vector3(1, 0, 0));
8+
9+
/** a vector normal to the plane */
10+
public Vector3 normal;
11+
12+
/** a point on the plane */
13+
public Vector3 pointOnPlane;
14+
15+
/**
16+
* creates a plane object with point {@code pointIn} that lies on the plane and
17+
* {@code normalVectorIn} which is normal to the plane.
18+
* @param pointIn a point located on the plane
19+
* @param normalVectorIn a vector normal to the plane
20+
*/
21+
public Plane(Vector3 pointIn, Vector3 normalVectorIn)
22+
{
23+
normal = normalVectorIn;
24+
pointOnPlane = pointIn;
25+
}
26+
27+
/**
28+
* a slightly slower constructor which accepts 3 points on the plane , mainly because of normalization.
29+
* @param point1
30+
* @param point2
31+
* @param point3
32+
*/
33+
public Plane(Vector3 point1, Vector3 point2, Vector3 point3)
34+
{
35+
normal = Vector3.crossProduct(Vector3.subtract(point1, point2), Vector3.subtract(point2, point3)).getNormalized();
36+
pointOnPlane = point1;
37+
}
38+
39+
/**
40+
* returns the {@code d} value of the standard equation for planes: <pre>
41+
* ax + by + cz + d = 0 </pre>
42+
* @return the {@code d} value of the standard equation for planes
43+
*/
44+
public double getDValue()
45+
{
46+
return -normal.x*pointOnPlane.x - normal.y*pointOnPlane.y - normal.z*pointOnPlane.z;
47+
}
48+
}

Quaternion.java

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package trrt.rendering3d.primitives;
2+
3+
import java.io.Serializable;
4+
5+
public class Quaternion implements Serializable
6+
{
7+
private static final long serialVersionUID = 1;
8+
9+
public static final Quaternion IDENTITY = new Quaternion(0, new Vector3(0, 0, 1));
10+
11+
/** scalar part of the quaternion */
12+
public final double w;
13+
14+
public final double x;
15+
public final double y;
16+
public final double z;
17+
18+
/**
19+
* creates a quaternion which represents a rotation around {@code axis}
20+
* by {@code angle} degrees
21+
* @param axis the axis of rotation
22+
* @param angle angle in radians
23+
*/
24+
public Quaternion(double angle, Vector3 axis)
25+
{
26+
axis = (axis.getSqrMagnitude() != 1)? axis.getNormalized() : axis;
27+
angle = Math.sin(angle/2);
28+
w = Math.sqrt(1-angle*angle); //equal to doing Math.cos() of the origonal angle but faster
29+
30+
double sinAngle = angle;
31+
x = axis.x*sinAngle;
32+
y = axis.y*sinAngle;
33+
z = axis.z*sinAngle;
34+
}
35+
36+
/**
37+
* creates a quaternion with specified components. warning: it doesn't normalize the values
38+
* so be careful that only correct values are entered.
39+
* @param wIn scalar real part of the quaternion
40+
* @param iIn imaginary i
41+
* @param jIn imaginary j
42+
* @param kIn imaginary k
43+
*/
44+
public Quaternion(double wIn, double iIn, double jIn, double kIn)
45+
{
46+
w = wIn;
47+
48+
x = iIn;
49+
y = jIn;
50+
z = kIn;
51+
}
52+
53+
/**
54+
* @return the inverse of the quaternion
55+
*/
56+
public Quaternion getInverse()
57+
{
58+
return new Quaternion(w, -x, -y, -z);
59+
}
60+
61+
/**
62+
* formats to string
63+
*/
64+
public String toString()
65+
{
66+
return new String(String.format("[%.3f, %.3f, %.3f, %.3f]", w, x, y, z));
67+
}
68+
69+
/**
70+
* multiplies {@code q1} by {@code q2}
71+
* @param q1
72+
* @param q2
73+
* @return the multiplied result
74+
*/
75+
public static Quaternion multiply(Quaternion q1, Quaternion q2)
76+
{
77+
return new Quaternion(
78+
q1.w*q2.w-(q1.x*q2.x+q1.y*q2.y+q1.z*q2.z), //w
79+
q2.w*q1.x + q1.w*q2.x + q1.y*q2.z-q1.z*q2.y, //x
80+
q2.w*q1.y + q1.w*q2.y + q1.z*q2.x-q1.x*q2.z, //y
81+
q2.w*q1.z + q1.w*q2.z + q1.x*q2.y-q1.y*q2.x); //z
82+
83+
}
84+
85+
/**
86+
* converts an euler angle into quaternion
87+
* @param pitch
88+
* @param yaw
89+
* @param roll
90+
* @return the quaternion equivilant
91+
*/
92+
public static Quaternion toQuaternion(double pitch, double yaw, double roll)
93+
{
94+
double sy = Math.sin(roll * 0.5);
95+
double cy = Math.sqrt(1-sy*sy);
96+
double sp = Math.sin(yaw * 0.5);
97+
double cp = Math.sqrt(1-sp*sp);
98+
double sr = Math.sin(pitch * 0.5);
99+
double cr = Math.sqrt(1-sr*sr);
100+
101+
return new Quaternion
102+
(
103+
cr * cp * cy + sr * sp * sy,
104+
sr * cp * cy - cr * sp * sy,
105+
cr * sp * cy + sr * cp * sy,
106+
cr * cp * sy - sr * sp * cy
107+
);
108+
}
109+
}

Vector2.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package trrt.rendering3d.primitives;
2+
/**
3+
* an object for representing 2d points or directions with
4+
* double precision.
5+
*/
6+
public class Vector2
7+
{
8+
public static final Vector2 ZERO = new Vector2(0, 0);
9+
/**
10+
* x component of the vector2 with double accuracy
11+
*/
12+
public final double x;
13+
14+
/**
15+
* y component of the vector2 with double accuracy
16+
*/
17+
public final double y;
18+
19+
public Vector2 (double xIn, double yIn)
20+
{
21+
x = xIn;
22+
y = yIn;
23+
}
24+
}

0 commit comments

Comments
 (0)