-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgorithm.java
More file actions
178 lines (157 loc) · 6.07 KB
/
Algorithm.java
File metadata and controls
178 lines (157 loc) · 6.07 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
import java.io.*;
import java.math.*;
import java.util.*;
import Jama.*;
public class Algorithm {
public static void main( String[] args ) {
File file = new File( "src\\data.txt" );
int numLines = 0;
try {
Scanner sc = new Scanner( file );
while( sc.hasNextLine() ) {
sc.nextLine();
numLines++;
}
sc.close();
}
catch( FileNotFoundException e ) {
e.printStackTrace();
}
double[][] dataValues = new double[3][numLines];
try {
Scanner sc = new Scanner( file );
int count = 0;
while( sc.hasNextLine() && count < numLines ) {
sc.useDelimiter( ", |\\n" );
Double x = Double.valueOf( sc.next() );
Double y = Double.valueOf( sc.next() );
Double z = Double.valueOf( sc.next() );
dataValues[0][count] = x;
dataValues[1][count] = y;
dataValues[2][count] = z;
count++;
}
sc.close();
}
catch( FileNotFoundException e ) {
e.printStackTrace();
}
Matrix matrix = new Matrix( dataValues );
SingularValueDecomposition svdMatrix = matrix.svd();
Matrix uMatrix = svdMatrix.getU();
double[] uVector = { uMatrix.get( 0 , 0 ) , uMatrix.get( 0 , 1 ) , uMatrix.get( 0 , 2 ) };
double[] vVector = { uMatrix.get( 1 , 0 ) , uMatrix.get( 1 , 1 ) , uMatrix.get( 1 , 2 ) };
double[] normalVector = { uMatrix.get( 2 , 0 ) , uMatrix.get( 2 , 1 ) , uMatrix.get( 2 , 2 ) };
normalVector = getUnitVector( normalVector );
Plane plane = new Plane( uVector , vVector , normalVector );
plane.setPoint( getIntersectionBetweenThreeVectors( plane.getU(), plane.getV(), plane.getNormal() ) );
double[][] arrayOf2DPoints = new double[numLines][2];
try {
FileWriter fw = new FileWriter( "src/dataOutput.txt" );
double[][] points = matrix.getArrayCopy();
for( int i = 0; i < points[0].length; i++ ) {
double[] point = { points[0][i] , points[1][i] , points[2][i] };
double[] pointOnPlane = getCoordsOf3DPointOnPlane( point , plane );
double[] constants = rref( pointOnPlane , plane );
arrayOf2DPoints[i][0] = constants[0];
arrayOf2DPoints[i][1] = constants[1];
// System.out.println( constants[0] + ", " + constants[1] );
fw.write( constants[0] + ", " + constants[1] );
fw.write( System.getProperty( "line.separator" ) );
}
fw.close();
}
catch( Exception e ) {
e.printStackTrace();
}
// System.out.println( Arrays.toString( plane.getU() ) );
// System.out.println( Arrays.toString( plane.getV() ) );
// System.out.println( Arrays.toString( plane.getNormal() ) );
// System.out.println( Arrays.toString( plane.getPoint() ) );
// System.out.println( Arrays.toString( point ) );
// System.out.println( Arrays.toString( pointOnPlane ) );
System.out.println( Arrays.deepToString( arrayOf2DPoints ) );
}
public static double[] rref( double[] pointOnPlane , Plane plane ) {
double[] constants = new double[2];
double[] uVector = plane.getU();
double[] vVector = plane.getV();
double a11 = uVector[0];
double a12 = vVector[0];
double a13 = pointOnPlane[0];
double a21 = uVector[1];
double a22 = vVector[1];
double a23 = pointOnPlane[1];
// double[][] matrixA = { {a11,a12,a13} , {a21,a22,a23} }; initialize matrix
// divide to make first column 1
double[][] matrixA = { {a11/a11,a12/a11,a13/a11} , {a21/a21,a22/a21,a23/a21} };
// subtract row1 from row2
matrixA[1][0] = matrixA[1][0] - matrixA[0][0];
matrixA[1][1] = matrixA[1][1] - matrixA[0][1];
matrixA[1][2] = matrixA[1][2] - matrixA[0][2];
// divide second row to make a22 = 1
double temp = matrixA[1][1];
matrixA[1][1] = matrixA[1][1] / temp;
matrixA[1][2] = matrixA[1][2] / temp;
// subtract multiple of row2 from row1
temp = matrixA[0][1];
matrixA[0][1] = matrixA[0][1] - temp*matrixA[1][1];
matrixA[0][2] = matrixA[0][2] - temp*matrixA[1][2];
constants[0] = matrixA[0][2];
constants[1] = matrixA[1][2];
return constants;
}
public static double[] getCoordsOf3DPointOnPlane( double[] point , Plane plane ) {
double dist = getDistanceFromPointToPlane( point , plane );
double[] normal = plane.getNormal();
double[] vector = { dist*normal[0] , dist*normal[1] , dist*normal[2] };
double[] pointOnPlane = { point[0] - vector[0] , point[1] - vector[1] , point[2] - vector[2] };
return pointOnPlane;
}
public static double getDistanceFromPointToPlane( double[] point , Plane plane ) {
double[] vector = getVector( point , plane.getPoint() );
double[] normal = plane.getNormal();
double dist = ( Math.abs( getDotProduct( normal , vector ) ) / getMagnitude( normal ) );
return dist;
}
public static double[] getIntersectionBetweenThreeVectors( double[] v1 , double[] v2 , double[] v3 ) {
double[][] vectors = { v1 , v2 , v3 };
double[][] case1 = { {0,v1[1],v1[2]} , {0,v2[1],v2[2]} , {0,v3[1],v3[2]} };
double[][] case2 = { {v1[0],0,v1[2]} , {v2[0],0,v2[2]} , {v3[0],0,v3[2]} };
double[][] case3 = { {v1[0],v1[1],0} , {v2[0],v2[1],0} , {v3[0],v3[1],0} };
Matrix matrix = new Matrix( vectors );
Matrix matrix1 = new Matrix( case1 );
Matrix matrix2 = new Matrix( case2 );
Matrix matrix3 = new Matrix( case3 );
double totalDet = matrix.det();
double firstDet = matrix1.det();
double secDet = matrix2.det();
double thirdDet = matrix3.det();
double xCoord = firstDet / totalDet;
double yCoord = secDet / totalDet;
double zCoord = thirdDet / totalDet;
double[] point = { xCoord , yCoord , zCoord };
return point;
}
public static double getDotProduct( double[] u , double[] v ) {
return u[0]*v[0] + u[1]*v[1] + u[2]*v[2];
}
public static double getMagnitude( double[] vector ) {
return Math.sqrt( getDotProduct( vector , vector ) );
}
public static double[] getUnitVector( double[] vector ) {
double[] newVector = new double[3];
double magnitude = getMagnitude( vector );
newVector[0] = vector[0] / magnitude;
newVector[1] = vector[1] / magnitude;
newVector[2] = vector[2] / magnitude;
return newVector;
}
public static double[] getVector( double[] point1 , double[] point2 ) {
double[] vector = new double[3];
vector[0] = point2[0] - point1[0];
vector[1] = point2[1] - point1[1];
vector[2] = point2[2] - point1[2];
return vector;
}
}