forked from tferr/Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBARlib.bsh
More file actions
76 lines (66 loc) · 2.06 KB
/
BARlib.bsh
File metadata and controls
76 lines (66 loc) · 2.06 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
/* BARlib.bsh
* IJ BAR: https://github.com/tferr/Scripts#scripts
*
* Common BAR library (https://github.com/tferr/Scripts/tree/master/lib#lib) to be
* placed in BAR/lib. This file hosts functions to be used across all your scripts.
* To load these scripting additions, append the following to your BeanShell files:
*
* addClassPath(bar.Utils.getBARDir());
* importCommands("lib/");
* BARlib();
*
* Then, initiate the BARlib class, calling methods as usual:
* lib = new BARlib();
* lib.confirmLoading();
*/
BARlib() {
public class BARlib {
/////////// UTILITIES ///////////
/** Acknowledges accessibility to this file */
void confirmLoading() {
ij.IJ.showMessage("BAR lib successfully loaded!");
}
/** Returns text from the system clipboard or an empty string if no text was found */
String getClipboardText() {
return bar.Utils.getClipboardText();
}
/** Returns a random uuid */
String randomString() {
import java.util.UUID;
return UUID.randomUUID().toString();
}
/////////// CALCULATIONS ///////////
/**
* Smooths 1D data according to the specified window. Returns the original
* data if window is not a positive integrer.
*/
double[] getSimpleMovingAverage(double[] values, int window) {
if (window<1) return values;
svalues = new double[values.length];
for (i=0; i<values.length; i++) {
svalues[i] = 0; n = 0;
for (j=Math.max(0, i-window); j<Math.min(values.length, i+window); j++) {
svalues[i] += values[j]; n++;
}
svalues[i] /= n;
}
return svalues;
}
/** Returns the greatest common divisor between 2 numbers */
int gcd(a, b) {
return (b==0) ? a : gcd(b, a%b);
}
/** Returns the greatest common divisor between 2 numbers using Commons Math */
int gcdCommons(a, b) {
import org.apache.commons.math3.util.ArithmeticUtils;
return ArithmeticUtils.gcd(a,b);
}
/** Returns surface area and volume of a sphere of radius r */
double[] sphereCalc(r) {
import java.Math;
sph_area = 4 * Math.PI * (r*r);
sph_vol = 4/3.0 * Math.PI * (r*r*r);
return new double[]{sph_area, sph_vol};
}
}
}