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 package com.jsaragih;
44
45 import java.io.BufferedWriter;
46 import java.io.FileInputStream;
47 import java.io.FileNotFoundException;
48 import java.io.IOException;
49 import java.io.InputStream;
50 import java.util.Locale;
51 import java.util.Scanner;
52
53 import org.openimaj.image.FImage;
54
55 import Jama.Matrix;
56
57
58
59
60
61
62
63 public class IO {
64 static {
65 Tracker.init();
66 }
67
68
69
70
71 public enum Types {
72
73 PDM,
74
75 PAW,
76
77 PATCH,
78
79 MPATCH,
80
81 CLM,
82
83 FDET,
84
85 FCHECK,
86
87 MFCHECK,
88
89 TRACKER
90 };
91
92
93
94
95
96
97
98 public static Matrix readMat(Scanner s) {
99 s.useLocale(Locale.UK);
100
101 final int r = s.nextInt();
102 final int c = s.nextInt();
103 s.nextInt();
104
105 final Matrix M = new Matrix(r, c);
106 final double[][] Mv = M.getArray();
107
108 for (int rr = 0; rr < r; rr++)
109 for (int cc = 0; cc < c; cc++)
110 Mv[rr][cc] = s.nextDouble();
111
112 return M;
113 }
114
115 static void writeMat(BufferedWriter s, Matrix M) throws IOException {
116 final int r = M.getRowDimension();
117 final int c = M.getColumnDimension();
118
119 s.write(r + " " + c + " 0");
120
121
122 final double[][] Mv = M.getArray();
123 for (int rr = 0; rr < r; rr++)
124 for (int cc = 0; cc < c; cc++)
125 s.write(Mv[rr][cc] + " ");
126 }
127
128 static void writeImg(BufferedWriter s, FImage img) throws IOException {
129 final int r = img.height;
130 final int c = img.width;
131
132 s.write(r + " " + c + " 0");
133
134
135 final float[][] Mv = img.pixels;
136 for (int rr = 0; rr < r; rr++)
137 for (int cc = 0; cc < c; cc++)
138 s.write(Mv[rr][cc] + " ");
139 }
140
141 static FImage readImg(Scanner s) {
142 s.useLocale(Locale.UK);
143
144 final int r = s.nextInt();
145 final int c = s.nextInt();
146 s.nextInt();
147
148 final FImage M = new FImage(c, r);
149 final float[][] Mv = M.pixels;
150
151 for (int rr = 0; rr < r; rr++)
152 for (int cc = 0; cc < c; cc++)
153 Mv[rr][cc] = s.nextFloat();
154
155 return M;
156 }
157
158 static void writeIntArray(BufferedWriter s, int[][] arr) throws IOException {
159 final int r = arr.length;
160 final int c = arr[0].length;
161
162 s.write(r + " " + c + " 0");
163
164
165 for (int rr = 0; rr < r; rr++)
166 for (int cc = 0; cc < c; cc++)
167 s.write(arr[rr][cc] + " ");
168 }
169
170 static int[][] readIntArray(Scanner s) {
171 s.useLocale(Locale.UK);
172
173 final int r = s.nextInt();
174 final int c = s.nextInt();
175 s.nextInt();
176
177 final int[][] M = new int[r][c];
178 for (int rr = 0; rr < r; rr++)
179 for (int cc = 0; cc < c; cc++)
180 M[rr][cc] = s.nextInt();
181
182 return M;
183 }
184
185 static int[][] loadCon(final String fname) throws FileNotFoundException {
186 return loadCon(new FileInputStream(fname));
187 }
188
189
190
191
192
193
194
195 public static int[][] loadCon(final InputStream in) {
196 final Scanner s = new Scanner(in);
197 s.useLocale(Locale.UK);
198
199 while (true) {
200 final String str = s.next();
201 if ("n_connections:".equals(str))
202 break;
203 }
204
205 final int n = s.nextInt();
206 final int[][] con = new int[2][n];
207
208 while (true) {
209 final String c = s.next();
210 if (c.equals("{"))
211 break;
212 }
213
214 for (int i = 0; i < n; i++) {
215 con[0][i] = s.nextInt();
216 con[1][i] = s.nextInt();
217 }
218 s.close();
219
220 return con;
221 }
222
223 static int[][] loadTri(final String fname) throws FileNotFoundException {
224 return loadTri(new FileInputStream(fname));
225 }
226
227
228
229
230
231
232
233 public static int[][] loadTri(final InputStream in) {
234 final Scanner s = new Scanner(in);
235 s.useLocale(Locale.UK);
236
237 while (true) {
238 final String str = s.next();
239 if ("n_tri:".equals(str))
240 break;
241 }
242
243 final int n = s.nextInt();
244 final int[][] tri = new int[n][3];
245
246 while (true) {
247 final String c = s.next();
248 if (c.equals("{"))
249 break;
250 }
251
252 for (int i = 0; i < n; i++) {
253 tri[i][0] = s.nextInt();
254 tri[i][1] = s.nextInt();
255 tri[i][2] = s.nextInt();
256 }
257 s.close();
258
259 return tri;
260 }
261
262 static FImage readImgByte(Scanner s) {
263 s.useLocale(Locale.UK);
264
265 final int r = s.nextInt();
266 final int c = s.nextInt();
267 s.nextInt();
268
269 final FImage M = new FImage(c, r);
270 final float[][] Mv = M.pixels;
271
272 for (int rr = 0; rr < r; rr++)
273 for (int cc = 0; cc < c; cc++)
274 Mv[rr][cc] = s.next("[^ ]").codePointAt(0);
275
276 return M;
277 }
278 }