1 package com.github.celldynamics.quimp.utils;
2
3 import java.io.IOException;
4 import java.net.URL;
5 import java.text.SimpleDateFormat;
6 import java.time.Instant;
7 import java.time.format.DateTimeFormatter;
8 import java.time.temporal.TemporalAccessor;
9 import java.util.Arrays;
10 import java.util.Date;
11 import java.util.Enumeration;
12 import java.util.jar.Attributes;
13 import java.util.jar.Manifest;
14
15 import org.apache.commons.lang3.text.WordUtils;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 import com.github.celldynamics.quimp.Outline;
20 import com.github.celldynamics.quimp.PropertyReader;
21 import com.github.celldynamics.quimp.QuimP;
22 import com.github.celldynamics.quimp.QuimpVersion;
23 import com.github.celldynamics.quimp.Vert;
24
25
26
27
28
29
30
31 public class QuimpToolsCollection {
32
33
34
35
36 static final Logger LOGGER = LoggerFactory.getLogger(QuimpToolsCollection.class.getName());
37
38
39
40 public static final String defNote = "0.0.0";
41
42
43
44
45
46
47
48
49 public String getQuimPversion() {
50 QuimpVersion quimpBuildInfo = getQuimPBuildInfo();
51 return getFormattedQuimPversion(quimpBuildInfo);
52 }
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68 public static String getFormattedQuimPversion(QuimpVersion quimpBuildInfo) {
69
70 String infoPlate = "---------------------------------------------------------\n"
71 + "| QuimP, by |\n"
72 + "| Richard Tyson (richard.tyson@warwick.ac.uk) |\n"
73 + "| Till Bretschneider (Till.Bretschneider@warwick.ac.uk) |\n"
74 + "| Piotr Baniukiewicz (P.Baniukiewicz@warwick.ac.uk) |\n"
75 + "| Web page: "
76 + new PropertyReader().readProperty("quimpconfig.properties", "webPage")
77 + " |\n"
78 + "---------------------------------------------------------\n";
79
80 infoPlate = infoPlate.concat("\n");
81 infoPlate = infoPlate.concat("QuimP version: " + quimpBuildInfo.getVersion());
82 infoPlate = infoPlate.concat("\n");
83 infoPlate = infoPlate.concat("Build by: " + quimpBuildInfo.getBuildstamp());
84 infoPlate = infoPlate.concat("\n");
85 infoPlate = infoPlate.concat("Internal name: " + quimpBuildInfo.getName());
86 infoPlate = infoPlate.concat("\n");
87 return infoPlate;
88 }
89
90
91
92
93
94
95 public QuimpVersion getQuimPBuildInfo() {
96 String[] ret = new String[3];
97 try {
98 Enumeration<URL> resources = getClass().getClassLoader().getResources("META-INF/MANIFEST.MF");
99
100 String iname = new PropertyReader().readProperty("quimpconfig.properties", "internalName");
101 while (resources.hasMoreElements()) {
102 URL reselement = resources.nextElement();
103 if (!reselement.toString().contains("/" + iname)) {
104 continue;
105 }
106 Manifest manifest = new Manifest(reselement.openStream());
107 Attributes attributes = manifest.getMainAttributes();
108 try {
109 String date = attributes.getValue("Implementation-Date");
110
111 ret[1] = attributes.getValue("Built-By") + " on: " + implementationDateConverter(date);
112 ret[0] = attributes.getValue("Implementation-Version");
113 ret[2] = attributes.getValue("Implementation-Title");
114 LOGGER.trace(Arrays.toString(ret));
115 } catch (Exception e) {
116 ;
117 }
118 }
119 } catch (IOException e) {
120 ;
121 }
122
123 ret[0] = ret[0] == null ? defNote : ret[0];
124 ret[1] = ret[1] == null ? defNote : ret[1];
125 ret[2] = ret[2] == null ? defNote : ret[2];
126
127 QuimpVersionuimpVersion.html#QuimpVersion">QuimpVersion retmap = new QuimpVersion(ret[0], ret[1], ret[2]);
128 return retmap;
129 }
130
131
132
133
134
135
136
137 public static String implementationDateConverter(String dateString) {
138 DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ");
139 TemporalAccessor accessor = timeFormatter.parse(dateString);
140 Date date = Date.from(Instant.from(accessor));
141 SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
142 return dt.format(date);
143 }
144
145
146
147
148
149
150
151 public static double s2d(String s) {
152 Double d;
153 try {
154 d = new Double(s);
155 } catch (NumberFormatException e) {
156 d = null;
157 }
158 if (d != null) {
159 return (d.doubleValue());
160 } else {
161 return (0.0);
162 }
163 }
164
165
166
167
168
169
170
171 public static String removeExtension(String filename) {
172
173
174 int dotI = filename.lastIndexOf(".");
175 if (dotI > 0 && filename.length() - dotI <= 6) {
176 filename = filename.substring(0, dotI);
177 }
178 return filename;
179 }
180
181
182
183
184
185
186
187 public static String getFileExtension(String filename) {
188
189
190 int dotI = filename.lastIndexOf(".");
191 if (dotI > 0) {
192 filename = filename.substring(dotI + 1, filename.length());
193 }
194 return filename;
195 }
196
197
198
199
200
201
202
203
204 public static Vert closestFloor(Outline o, double target) {
205
206
207 Vert v = o.getHead();
208 double coordA;
209 double coordB;
210
211 do {
212
213
214 coordA = v.coord;
215 coordB = v.getNext().coord;
216
217
218 if ((coordA > coordB)) {
219 if (coordA <= target && coordB + 1 > target) {
220 break;
221 }
222
223 if (coordA - 1 <= target && coordB > target) {
224 break;
225 }
226
227 } else {
228 if (coordA <= target && coordB > target) {
229 break;
230 }
231 }
232 v = v.getNext();
233 } while (!v.isHead());
234
235 return v;
236 }
237
238
239
240
241
242
243
244
245 public static double distanceToScale(double value, double scale) {
246
247 return value * scale;
248 }
249
250
251
252
253
254
255
256
257 public static double distanceToScale(int value, double scale) {
258 return value * scale;
259 }
260
261
262
263
264
265
266
267
268 public static double areaToScale(double value, double scale) {
269
270 return value * (scale * scale);
271 }
272
273
274
275
276
277
278
279
280
281 public static double speedToScale(double value, double scale, double frameInterval) {
282 return (value * scale) / frameInterval;
283 }
284
285
286
287
288
289
290
291
292
293 public static double speedToScale(int value, double scale, double frameInterval) {
294 return (value * scale) / frameInterval;
295 }
296
297
298
299
300
301
302
303
304 public static double distanceFromScale(double value, double scale) {
305 return value / scale;
306 }
307
308
309
310
311
312
313 public static String dateAsString() {
314 SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
315 Date date = new Date();
316 return formatter.format(date);
317 }
318
319
320
321
322
323
324
325 public static double[] setLimitsEqual(double[] migLimits) {
326 if (migLimits.length < 2) {
327 LOGGER.warn("Array to short. Needs a min and max");
328 return migLimits;
329 }
330
331 if (migLimits[1] < 0) {
332 migLimits[1] = -migLimits[0];
333 }
334 if (migLimits[0] > 0) {
335 migLimits[0] = -migLimits[1];
336 }
337
338
339 if (migLimits[0] < -migLimits[1]) {
340 migLimits[1] = -migLimits[0];
341 } else {
342 migLimits[0] = -migLimits[1];
343 }
344
345 return migLimits;
346 }
347
348
349
350
351
352
353
354
355
356
357 public static String stringWrap(String in, int len) {
358 return stringWrap(in, len, "\n");
359 }
360
361
362
363
364
365
366
367
368
369 public static String stringWrap(String in) {
370 return stringWrap(in, QuimP.LINE_WRAP, "\n");
371 }
372
373
374
375
376
377
378
379
380
381
382
383
384
385 public static String stringWrap(String in, int len, String brek) {
386 String[] sp = in.split("\n");
387 String str = "";
388 for (String s : sp) {
389 s = s.concat("\n");
390 str = str.concat(WordUtils.wrap(s, len, brek, false, "( |/|\\\\)"));
391 }
392 str = str.trim();
393
394 return str;
395 }
396
397 }