3 import ij
.gui
.GenericDialog
;
4 import ini
.trakem2
.display
.Line3D
;
5 import ini
.trakem2
.plugin
.TPlugIn
;
6 import ini
.trakem2
.utils
.Bureaucrat
;
7 import ini
.trakem2
.utils
.IJError
;
8 import ini
.trakem2
.utils
.Utils
;
9 import ini
.trakem2
.utils
.Worker
;
11 import java
.io
.InputStreamReader
;
12 import java
.io
.Reader
;
13 import java
.lang
.reflect
.Method
;
15 public class Identify
implements TPlugIn
{
21 Thread
.currentThread().setContextClassLoader(ij
.IJ
.getClassLoader());
22 Class
<?
> c
= Class
.forName("clojure.lang.Compiler");
23 Method load
= c
.getDeclaredMethod("load", new Class
[]{Reader
.class});
24 load
.invoke(null, new Object
[]{new InputStreamReader(Identify
.class.getResourceAsStream("/lineage/identify.clj"))});
25 // As a side effect, inits clojure runtime
26 } catch (Throwable t
) {
32 * Takes 2 arg (the Pipe or Polyline and the lib-name)
33 * or 5 args: Line3D pipe, String lib-name, double delta, boolean direct and boolean substring
35 static public Object
identify(Object
... args
) {
37 Class
<?
> RT
= Class
.forName("clojure.lang.RT");
38 Method var
= RT
.getDeclaredMethod("var", new Class
[]{String
.class, String
.class});
39 Object fn
= var
.invoke(null, new Object
[]{"lineage.identify", "identify"});
40 Class
<?
>[] cc
= new Class
[args
.length
];
41 for (int i
=0; i
<cc
.length
; i
++) cc
[i
] = Object
.class;
42 Method invoke
= Class
.forName("clojure.lang.Var").getDeclaredMethod("invoke", cc
);
43 return invoke
.invoke(fn
, args
);
44 } catch (Throwable e
) {
50 static private class Params
{
52 private final String
[] libs
= {"Drosophila-3rd-instar"};
54 private double delta
= 1.0;
55 private int lib_index
= 0;
56 private boolean direct
= true;
57 private boolean substring
= false;
59 synchronized public Params
clone() {
60 Params p
= new Params();
62 p
.lib_index
= lib_index
;
64 p
.substring
= substring
;
68 synchronized private boolean setup() {
69 GenericDialog gd
= new GenericDialog("Setup NIT");
70 gd
.addNumericField("delta:", delta
, 2);
71 gd
.addCheckbox("direct", direct
);
72 gd
.addCheckbox("substring", substring
);
73 gd
.addChoice("Library: ", libs
, libs
[0]);
75 if (gd
.wasCanceled()) return false;
76 double d
= gd
.getNextNumber();
77 if (Double
.isNaN(d
)) {
78 Utils
.log("Invalid delta value!");
83 lib_index
= gd
.getNextChoiceIndex();
84 direct
= gd
.getNextBoolean();
85 substring
= gd
.getNextBoolean();
90 /** Store start up values. New instances take the current values. */
91 static private Params PARAMS
= new Params();
93 private Params params
= PARAMS
.clone();
95 public boolean setup(Object
... args
) {
96 return params
.setup();
99 public Bureaucrat
identify(final Line3D pipe
) {
100 return Bureaucrat
.createAndStart(new Worker
.Task("Identifying " + pipe
) {
102 if (null == pipe
) return;
103 Params p
= params
.clone();
104 identify(pipe
, p
.libs
[p
.lib_index
], p
.delta
, p
.direct
, p
.substring
);
106 }, pipe
.getProject());
109 public Object
invoke(Object
... args
) {
110 if (null == args
|| args
.length
< 1 || null == args
[0] || !(args
[0] instanceof Line3D
)) return null;
111 Line3D pipe
= (Line3D
) args
[0];
112 Params p
= params
.clone();
113 return identify(pipe
, p
.libs
[p
.lib_index
], p
.delta
, p
.direct
, p
.substring
);
116 public boolean applies(final Object ob
) {
117 return null != ob
&& ob
instanceof Line3D
;