1 package com.github.celldynamics.quimp.plugin.utils;
2
3 import java.awt.Point;
4 import java.awt.geom.Point2D;
5 import java.util.ArrayList;
6 import java.util.Collection;
7 import java.util.List;
8
9 import org.scijava.vecmath.Point2d;
10 import org.scijava.vecmath.Tuple2d;
11
12 import com.github.celldynamics.quimp.BoaException;
13 import com.github.celldynamics.quimp.Outline;
14 import com.github.celldynamics.quimp.Shape;
15 import com.github.celldynamics.quimp.Snake;
16
17 import ij.util.Tools;
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 public class QuimpDataConverter {
34
35 private double[] xc;
36 private double[] yc;
37
38
39
40
41 public QuimpDataConverter() {
42 xc = new double[0];
43 yc = new double[0];
44 }
45
46
47
48
49
50
51
52
53 public QuimpDataConverter(final List<? extends Tuple2d> input) {
54 this();
55 if (input != null) {
56 toArrays(input);
57 }
58 }
59
60
61
62
63
64
65
66 public QuimpDataConverter(final Collection<? extends Point2D> input) {
67 this();
68 if (input != null) {
69 toArrays(input);
70 }
71 }
72
73
74
75
76
77
78
79 public QuimpDataConverter(final double[] x, final double[] y) {
80 this();
81 if (x.length != y.length) {
82 throw new IllegalArgumentException("Arrays have different lengths");
83 }
84 this.xc = x;
85 this.yc = y;
86 }
87
88
89
90
91
92
93
94 public QuimpDataConverter(final float[] x, final float[] y) {
95 this();
96 if (x.length != y.length) {
97 throw new IllegalArgumentException("Arrays have different lengths");
98 }
99 this.xc = Tools.toDouble(x);
100 this.yc = Tools.toDouble(y);
101 }
102
103
104
105
106
107
108
109 public QuimpDataConverter(final Shape<?> s) {
110 this();
111 if (s != null) {
112 toArrays(s.asList());
113 }
114 }
115
116
117
118
119
120
121
122 private void toArrays(final List<? extends Tuple2d> input) {
123 int i = 0;
124 if (input != null) {
125 xc = new double[input.size()];
126 yc = new double[input.size()];
127 for (Tuple2d el : input) {
128 xc[i] = el.getX();
129 yc[i] = el.getY();
130 i++;
131 }
132 } else {
133 xc = new double[0];
134 yc = new double[0];
135 }
136 }
137
138
139
140
141
142
143
144 private void toArrays(final Collection<? extends Point2D> input) {
145 int i = 0;
146 if (input != null) {
147 xc = new double[input.size()];
148 yc = new double[input.size()];
149 for (Point2D el : input) {
150 xc[i] = el.getX();
151 yc[i] = el.getY();
152 i++;
153 }
154 } else {
155 xc = new double[0];
156 yc = new double[0];
157 }
158 }
159
160
161
162
163
164
165 public double[] getX() {
166 return xc;
167 }
168
169
170
171
172
173
174 public double[] getY() {
175 return yc;
176 }
177
178
179
180
181
182
183
184
185
186
187
188
189 public List<Point2d> getList() {
190 ArrayList<Point2d> list = new ArrayList<>();
191 for (int i = 0; i < xc.length; i++) {
192 list.add(new Point2d(xc[i], yc[i]));
193 }
194 return list;
195 }
196
197
198
199
200
201
202
203
204
205
206
207
208 public List<Point2D> getListofIntPoints() {
209 ArrayList<Point2D> list = new ArrayList<>();
210 for (int i = 0; i < xc.length; i++) {
211 list.add(new Point((int) Math.round(xc[i]), (int) Math.round(yc[i])));
212 }
213 return list;
214 }
215
216
217
218
219
220
221
222
223
224
225
226
227 public List<Point2D> getListofDoublePoints() {
228 ArrayList<Point2D> list = new ArrayList<>();
229 for (int i = 0; i < xc.length; i++) {
230 list.add(new Point2D.Double(xc[i], yc[i]));
231 }
232 return list;
233 }
234
235
236
237
238
239
240 public float[] getFloatX() {
241 float[] xf = new float[xc.length];
242 for (int i = 0; i < xc.length; i++) {
243 xf[i] = (float) xc[i];
244 }
245 return xf;
246 }
247
248
249
250
251
252
253 public float[] getFloatY() {
254 float[] yf = new float[yc.length];
255 for (int i = 0; i < yc.length; i++) {
256 yf[i] = (float) yc[i];
257 }
258 return yf;
259 }
260
261
262
263
264
265
266 public int size() {
267 return xc.length;
268 }
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285 public Snake getSnake(int id) throws BoaException {
286 Snake ret = null;
287 if (xc.length == 0 || yc.length == 0) {
288 return ret;
289 } else {
290 ret = new Snake(xc, yc, id);
291 return ret;
292 }
293 }
294
295
296
297
298
299
300
301
302
303
304
305
306
307 public Outline getOutline() {
308 Outline ret = null;
309 if (xc.length == 0 || yc.length == 0) {
310 return ret;
311 } else {
312 ret = new Outline(xc, yc);
313 return ret;
314 }
315 }
316
317 }