forked from tferr/Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBARlib.groovy
More file actions
74 lines (63 loc) · 2.04 KB
/
BARlib.groovy
File metadata and controls
74 lines (63 loc) · 2.04 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
/* BARlib.groovy
* 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 can host functions to be used across your scripts.
* To load these scripting additions, append the following to your Groovy files:
*
* import bar.Utils
* file = new File(Utils.getLibDir() + "BARlib.groovy")
* BARlib = new GroovyClassLoader(getClass().getClassLoader()).parseClass(file)
*
* Then, initiate the BARlib class, calling methods as usual:
* lib = BARlib.newInstance()
* lib.confirmLoading()
*/
class BARlib {
/////////// UTILITIES ///////////
/** Acknowledges accessibility to this file */
def confirmLoading() {
ij.IJ.showMessage("BAR lib successfully loaded!")
}
/** Returns text from the system clipboard or an empty string if no text was found */
def getClipboardText() {
bar.Utils.getClipboardText()
}
/** Returns a random uuid */
def randomString() {
java.util.UUID.randomUUID().toString()
}
/////////// CALCULATIONS ///////////
/**
* Smooths 1D data according to the specified window. Returns the original
* data if window is not a positive integrer.
*/
def getSimpleMovingAverage(values, window) {
if (window<1 || !(window instanceof Integer))
return values
def svalues= new Object[values.size()]
def lastI = values.size()-1
for (i in 0..lastI) {
svalues[i] = 0; def n = 0
for (j in Math.max(0, i-window)..Math.min(lastI, i+window)) {
svalues[i] += values[j]; n++
}
svalues[i] /= n
}
return svalues
}
/** Returns the greatest common divisor between 2 numbers */
def gcd(a, b) {
(b==0) ? a : gcd(b, a%b)
}
/** Returns the greatest common divisor between 2 numbers using Commons Math */
def gcdCommons(a, b) {
org.apache.commons.math3.util.ArithmeticUtils.gcd(a,b)
}
/** Returns surface area and volume of a sphere of radius r */
def sphereCalc(r) {
def sph_area = 4.0 * Math.PI * (r*r)
def sph_vol = 4.0/3.0 * Math.PI * (r*r*r)
[sph_area, sph_vol]
}
}