-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnTuple.java
More file actions
237 lines (212 loc) · 6.71 KB
/
nTuple.java
File metadata and controls
237 lines (212 loc) · 6.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* nTuple.java
* Created by: William Tyas
* Date: 8/9/17
* Description: A vector, with various methods to implement vector
* operations.
*/
public class nTuple {
private float x;
private float y;
private float z;
public float getX() { return this.x; }
public float getY() { return this.y; }
public float getZ() { return this.z; }
public nTuple() {
this.x = 0.0f;
this.y = 0.0f;
this.z = 0.0f;
}
public nTuple(float x, float y, float z) {
this.x = x;
this.y = y;
this.z = z;
}
public nTuple(nTuple other) {
this.x = other.getX();
this.y = other.getY();
this.z = other.getZ();
}
public void setNTuple(nTuple other) {
this.x = other.getX();
this.y = other.getY();
this.z = other.getZ();
}
@Override
public String toString() {
return(this.x + "," + this.y + "," + this.z);
}
//////////////////////////////////////////////////////////////////
// BASIC VECTOR OPERATIONS //
// Dot product, scalar multiplication, addition, subtraction, //
// normalize //
//////////////////////////////////////////////////////////////////
public float dot(nTuple other) {
float newX = this.getX() * other.getX();
float newY = this.getY() * other.getY();
float newZ = this.getZ() * other.getZ();
return (newX + newY + newZ);
}
public nTuple scale(float factor) {
float newX = this.x * factor;
float newY = this.y * factor;
float newZ = this.z * factor;
return new nTuple(newX, newY, newZ);
}
// Compute the product of two matrices: a 3x3 and a 3x1
public nTuple product(nTuple a, nTuple b, nTuple c) {
float x = a.dot(this);
float y = b.dot(this);
float z = c.dot(this);
return new nTuple(x, y, z);
}
public nTuple add(nTuple other) {
float newX = this.x + other.getX();
float newY = this.y + other.getY();
float newZ = this.z + other.getZ();
return new nTuple(newX, newY, newZ);
}
public nTuple subtract(nTuple other) {
float newX = this.x - other.getX();
float newY = this.y - other.getY();
float newZ = this.z - other.getZ();
return new nTuple(newX, newY, newZ);
}
public nTuple normalize() {
float len = (float) Math.sqrt(this.dot(this));
return new nTuple(this.x / len, this.y / len, this.z / len);
}
//////////////////////////////////////////////////////////////////
// GAUSS-JORDAN ELIMINATION //
//////////////////////////////////////////////////////////////////
// Calculate the reduced row echelon form of a matrix
public static void rref(float[][] matrix) {
// Forward phase - results in reduced echelon form
for (int i = 0; i < matrix.length; i++) {
// Find next leftmost nonzero column from rows below i
int colIndex = nonZeroCol(matrix, i, matrix.length - 1);
if (colIndex >= 0.0f) {
// Bring nonzero entry to top of column
if (matrix[i][colIndex] == 0.0f) {
int j = nextNonZeroRow(matrix, i, colIndex);
rowSwap(matrix, i, j);
}
float pivot = matrix[i][colIndex];
// Multiply row to make pivot a 1
if (pivot != 1.0f && !closeEnough(pivot, 0.0f)) {
pivot = 1.0f / matrix[i][colIndex];
for (int k = 0; k < matrix[0].length; k++) {
float newValue = matrix[i][k] * pivot;
if (closeEnough(newValue, Math.round(newValue))) {
matrix[i][k] = Math.round(newValue);
} else {
matrix[i][k] = newValue;
}
}
}
// Add multiples of row to others below
for (int k = (i + 1); k < matrix.length; k++) {
float value = matrix[k][colIndex];
if (value != 0.0f) {
float first = matrix[k][colIndex];
for (int el = 0; el < (matrix[0].length); el++) {
float newValue = -first * matrix[i][el] + matrix[k][el];
if (closeEnough(newValue, Math.round(newValue))) {
matrix[k][el] = Math.round(newValue);
} else {
matrix[k][el] = newValue;
}
}
}
}
}
}
// Backward phase - Add multiples of row to others above
// Results in rref
int lastNonZeroRow = lastNonZeroRow(matrix);
for (int k = lastNonZeroRow; k > 0; k--) {
int firstNonZeroCol = nonZeroCol(matrix, k, k);
for (int i = 0; i < k; i++) {
float value = matrix[i][firstNonZeroCol];
if (value != 0.0f) {
float first = matrix[i][firstNonZeroCol];
for (int el = 0; el < (matrix[0].length); el++) {
float newValue = -first * matrix[k][el] + matrix[i][el];
if (closeEnough(newValue, Math.round(newValue))) {
matrix[i][el] = Math.round(newValue);
} else {
matrix[i][el] = newValue;
}
}
}
}
}
}
public static boolean closeEnough(float pivot, float limit) {
return (Math.abs(limit - pivot) < 0.001f);
}
// Finds the next nonzero column in a matrix between minRow and maxRow
public static int nonZeroCol(float[][] matrix, int minRow, int maxRow) {
for (int i = 0; i < matrix[0].length; i++) {
for (int j = minRow; j <= maxRow; j++) {
if (matrix[j][i] != 0.0f) {
return i;
}
}
}
return -1;
}
// Finds next row below rowIndex with nonzero entry in column colIndex
public static int nextNonZeroRow(float[][] matrix, int rowIndex, int colIndex) {
for (int i = (rowIndex + 1); i < matrix.length; i++) {
if (matrix[i][colIndex] != 0.0f) {
return i;
}
}
return -1;
}
// Finds the last nonzero row in a matrix
public static int lastNonZeroRow(float[][] matrix) {
boolean zeroRow = true;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] != 0.0f) {
zeroRow = false;
}
}
if (zeroRow == true) {
return (i - 1);
} else if (i == (matrix.length - 1)) { // last row
return i;
}
zeroRow = true;
}
return -1;
}
public static void rowSwap(float[][] matrix, int first, int second) {
for (int i = 0; i < matrix[0].length; i++) {
float tmp = matrix[first][i];
matrix[first][i] = matrix[second][i];
matrix[second][i] = tmp;
}
}
// Find coordinates of d in coordinate system defined by basis
// vectors a, b, and c
public nTuple coordChange(nTuple a, nTuple b, nTuple c, nTuple d) {
float[][] basisChange = new float[3][4];
basisChange[0][0] = a.getX();
basisChange[0][1] = b.getX();
basisChange[0][2] = c.getX();
basisChange[0][3] = d.getX();
basisChange[1][0] = a.getY();
basisChange[1][1] = b.getY();
basisChange[1][2] = c.getY();
basisChange[1][3] = d.getY();
basisChange[2][0] = a.getZ();
basisChange[2][1] = b.getZ();
basisChange[2][2] = c.getZ();
basisChange[2][3] = d.getZ();
this.rref(basisChange);
return new nTuple(basisChange[0][3], basisChange[1][3], basisChange[2][3]);
}
}