-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcess.java
More file actions
204 lines (182 loc) · 7.15 KB
/
Process.java
File metadata and controls
204 lines (182 loc) · 7.15 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
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Process {
private File workingDirectory = new File(System.getProperty("user.dir"));
public File inputFile = new File( "C:\\Users\\ayush\\eclipse-workspace\\LAB2\\src\\LA2.txt");
public File outputFile = new File( "C:\\Users\\ayush\\eclipse-workspace\\LAB2\\out\\out.txt");
Process(){
}
/**
* Find median as candidate for root
* @return
*/
public String findMedian(){
String median=null;
String inLine;
int counter=0;
ArrayList<String> arrSorted = new ArrayList<String>();
try{
BufferedReader bReader = new BufferedReader(new FileReader(inputFile));
while((inLine=bReader.readLine()) != null){
if(counter < 10000){
//if(pointInMedian(inLine)){
arrSorted.add(inLine);
//}
}else{
Collections.sort(arrSorted, new comparePoints());
break;
}
counter++;
}
bReader.close();
}catch(FileNotFoundException e){
System.out.println(e.toString());
}catch(IOException e){
System.out.println(e.toString());
}
median = arrSorted.get(arrSorted.size()/2);
return median;
}
/**
* Refine values to find the root
* @param str
* @return
*/
public boolean pointInMedian(String str){
boolean inRange = false;
String[] aStr = str.substring(1, str.length()-1).split(",");
if ((Double.parseDouble(aStr[0]) < 550 && Double.parseDouble(aStr[0]) > 450)
&& (Double.parseDouble(aStr[1]) < 550 && Double.parseDouble(aStr[1]) > 450)
&& (Double.parseDouble(aStr[2]) < 550 && Double.parseDouble(aStr[2]) > 450)){
inRange = true;
}
return inRange;
}
public void clearWrkingDirectory(){
File outDir = new File( "C:\\Users\\ayush\\eclipse-workspace\\LAB2\\out");
for(File outFile: outDir.listFiles()){
outFile.delete();
}
}
static class comparePoints implements Comparator<String>{
public int compare(String a, String b){
String[] strA;
String[] strB;
strA = a.substring(1, a.length()-1).split(",");
strB = b.substring(1, b.length()-1).split(",");
int xComp, yComp, zComp;
xComp = Double.compare(Double.parseDouble(strA[0]), Double.parseDouble(strB[0]));
yComp = Double.compare(Double.parseDouble(strA[1]), Double.parseDouble(strB[1]));
zComp = Double.compare(Double.parseDouble(strA[2]), Double.parseDouble(strB[2]));
//return xComp;
if(xComp != 0){
return xComp;
}else{
if(yComp != 0){
return yComp;
}
else{
return zComp;
}
}
}
}
public static void main(String[] args) throws Exception {
String inLine="";
String strMedian;
boolean printFile = false;
//int iSize=10;
//String[] arrSorted = new String[1000];
ArrayList<String> arrSorted = new ArrayList<String>();
String[] strArr = new String[3];
//x,y,x
double[] pointArr = new double[3];
int counter=0;
BufferedReader bReader=null;
BufferedWriter bWriter=null;
Process process = new Process();
process.clearWrkingDirectory();
long startTime = System.currentTimeMillis();
strMedian = process.findMedian();
System.out.print("Root = " + process.findMedian());
//KDTree indexTree = new KDTree(iSize);
Tree indexTree = new Tree();
//Adding root
strArr = strMedian.substring(1, strMedian.length()-1).split(",");
for(int i=0; i<strArr.length;i++){
pointArr[i] = Double.parseDouble(strArr[i]);
}
indexTree.add(pointArr);
try{
bReader = new BufferedReader(new FileReader(process.inputFile));
//bWriter = new BufferedWriter(new FileWriter(process.outputFile));
while((inLine=bReader.readLine()) != null){
if(!strMedian.equals(inLine)){
strArr = inLine.substring(1, inLine.length()-1).split(",");
//Convert to double
for(int i=0; i<strArr.length;i++){
pointArr[i] = Double.parseDouble(strArr[i]);
}
indexTree.add(pointArr);
}
//System.out.println(pointArr[1]);
//counter++;
//if(counter<iSize-1){
// break;
//}
}
//indexTree.preorder();
}catch(FileNotFoundException e){
System.out.println(e.toString());
}catch(IOException e){
System.out.println(e.toString());
}
long endTime = System.currentTimeMillis();
System.out.println("\nTotal time to build index: " + (endTime-startTime) + "ms");
System.out.println("Estimated total size of index:" + ((indexTree.size)*(8*3))/1048576 + " MB");
//System.out.println(" ");
//indexTree.printTree();
//System.out.println(" ");
startTime = System.currentTimeMillis();
double[] from = {64.39683, 7.367402, 408.0358};
double[] to = {455.5008, 60.9156, 580.5400};
ArrayList<Node> aNode = indexTree.searchBetween(from, to);
endTime = System.currentTimeMillis();
System.out.println("Total nodes found: " + aNode.size());
System.out.println("Total time for range search: " + (endTime-startTime) + "ms");
if(printFile == true) {
bWriter = new BufferedWriter(new FileWriter(process.outputFile));
for(int i=0; i < aNode.size();i++) {
bWriter.write(aNode.get(i).toString()+ "\r");
}
bWriter.close();
}
//double[] from = {64.39683, 7.367402, 408.0358};
/*** Nearest Neighbor search ***/
double[] pt = {455.5008, 60.9156, 580.5400};
startTime = System.nanoTime();
//KDNode n = indexTree.find_nearest(to);
double[] n = indexTree.findNearest(pt);
endTime = System.nanoTime();
System.out.println("Nearest: " + n[0] + ", " + n[1] + ", "+ n[2]);
System.out.println("Total time for nearest neighbour search: " + (endTime-startTime) + "ns");
//indexTree.searchBetween(from, to);
/*
double test1[] = {816.5008, 717.9156, 452.0825};
indexTree.search(test1);
System.out.println(" ");
double test2[] = {816.5008, 717.9156, 452.0825};
indexTree.find_nearest(test2);
*/
bReader.close();
//bWriter.close();
}
}