1 package org.catacomb.graph.arbor;
2
3
4
5 import org.catacomb.be.Position;
6 import org.catacomb.graph.gui.PickablePoint;
7 import org.catacomb.interlish.content.ConnectionFlavor;
8 import org.catacomb.interlish.structure.AttachmentPoint;
9 import org.catacomb.report.E;
10
11 import java.awt.Color;
12 import java.util.ArrayList;
13
14
15
16 public class SegmentGraphPoint extends PickablePoint {
17
18
19
20
21 int p_status;
22 final static int PROTO = 1;
23 final static int REAL = 2;
24
25
26 public double x;
27 public double y;
28 public double radius;
29 public String label;
30
31
32
33 ArrayList<SegmentGraphPoint> p_neighbors;
34 SegmentGraphPoint[] p_nbrCache;
35
36 public int[] nbrIndexes;
37
38
39
40
41 SegmentGraphPoint[] p_protoNbrs;
42 SegmentGraphPoint p_extensionProto;
43
44
45
46
47 SegmentGraphPoint p_pN1;
48 SegmentGraphPoint p_pN2;
49
50
51
52 int p_index;
53
54
55
56 boolean p_highlight;
57 boolean p_mark;
58
59
60
61 public SegmentGraphPoint() {
62 this(0., 0.);
63 }
64
65
66 public SegmentGraphPoint(double x, double y) {
67 this(x, y, REAL);
68 }
69
70
71 public SegmentGraphPoint(double x, double y, int stat) {
72 super(x, y);
73
74 p_status = stat;
75
76
77 if (p_status == REAL) {
78 setColor(Color.green);
79 } else {
80 setColor(Color.magenta);
81 }
82
83
84 radius = 1.;
85 p_neighbors = new ArrayList<SegmentGraphPoint>();
86
87 if (isReal()) {
88 syncExtensionProto();
89 }
90 }
91
92
93 public double getX() {
94 return x;
95 }
96
97 public double getY() {
98 return y;
99 }
100
101 public void deReference() {
102 SegmentGraphPoint[] pa = getNeighbors();
103 nbrIndexes = new int[pa.length];
104 for (int i = 0; i < pa.length; i++) {
105 nbrIndexes[i] = pa[i].getIndex();
106 }
107 Position pos = getPosition();
108 x = pos.getX();
109 y = pos.getY();
110 }
111
112
113
114 public void reReference(ArrayList<SegmentGraphPoint> apa) {
115
116 setPosition(x, y);
117
118 p_neighbors = new ArrayList<SegmentGraphPoint>();
119 if (nbrIndexes != null) {
120 for (int i = 0; i < nbrIndexes.length; i++) {
121 p_neighbors.add(apa.get(nbrIndexes[i]));
122 }
123 }
124 syncExtensionProto();
125 }
126
127
128
129 public AttachmentPoint makeAttachmentPoint() {
130 SGAttachmentPoint sgap = new SGAttachmentPoint();
131
132 sgap.setPosition(getPosition());
133 sgap.setFlavor(new ConnectionFlavor("CellProbe"));
134 sgap.setID("" + p_index);
135
136 return sgap;
137 }
138
139
140
141 public SegmentGraphPoint[] getNeighbors() {
142 if (p_nbrCache == null) {
143 p_nbrCache = (p_neighbors.toArray(new SegmentGraphPoint[0]));
144 }
145 return p_nbrCache;
146 }
147
148
149
150 public void setLabel(String s) {
151 label = s;
152 }
153
154
155 public void highlightLabel(String s) {
156 if (s.equals(label)) {
157 p_highlight = true;
158 } else {
159 p_highlight = false;
160 }
161 }
162
163
164 public boolean isReal() {
165 return (p_status == REAL);
166 }
167
168
169 public void unHighlight() {
170 p_highlight = false;
171 }
172
173
174 public void highlight() {
175 p_highlight = true;
176 }
177
178
179 public boolean isHighlighted() {
180 return p_highlight;
181 }
182
183
184 public void unMark() {
185 p_mark = false;
186 }
187
188
189 public void mark() {
190 p_mark = true;
191 }
192
193
194 public boolean isMarked() {
195 return p_mark;
196 }
197
198
199
200 public void setIndex(int ind) {
201 p_index = ind;
202 }
203
204
205 public int getIndex() {
206 return p_index;
207 }
208
209
210
211 public void setRadius(double r) {
212 radius = r;
213 }
214
215
216 public double getRadius() {
217 return radius;
218 }
219
220
221
222 public double getR() {
223 return getRadius();
224 }
225
226
227 public double getZ() {
228 return 0.0;
229 }
230
231
232 public SegmentGraphPoint[] getProtoNeighbors() {
233 if (p_protoNbrs == null) {
234 SegmentGraphPoint[] pa = getNeighbors();
235 p_protoNbrs = new SegmentGraphPoint[pa.length];
236 for (int i = 0; i < pa.length; i++) {
237 if (getIndex() < pa[i].getIndex()) {
238 SegmentGraphPoint psgp = new SegmentGraphPoint(0., 0., PROTO);
239 psgp.setProtoNeighbors(this, pa[i]);
240 p_protoNbrs[i] = psgp;
241 }
242 }
243 }
244
245 return p_protoNbrs;
246 }
247
248
249
250 public void recHighlight() {
251 SegmentGraphPoint[] nbrs = getNeighbors();
252
253 highlight();
254 for (int i = 0; i < nbrs.length; i++) {
255 if (nbrs[i].isHighlighted()) {
256
257 } else {
258 nbrs[i].recHighlight();
259 }
260 }
261 }
262
263
264
265 public boolean recHighlightPath() {
266 SegmentGraphPoint[] nbrs = getNeighbors();
267
268 mark();
269 boolean bret = false;
270
271 if (isHighlighted()) {
272 bret = true;
273
274 } else {
275 for (int i = 0; i < nbrs.length; i++) {
276 if (nbrs[i].isMarked()) {
277
278
279 } else {
280 if (nbrs[i].recHighlightPath()) {
281 bret = true;
282 break;
283 }
284 }
285 }
286 }
287 if (bret) {
288 highlight();
289 }
290 return bret;
291 }
292
293
294
295 public void move(Position pos) {
296 SegmentGraphPoint[] pa = getNeighbors();
297 setPosition(pos);
298 if (isReal()) {
299 p_extensionProto.updateProtoPosition();
300
301 updateProtos();
302 for (int i = 0; i < pa.length; i++) {
303 pa[i].updateProtos();
304 }
305 }
306
307 }
308
309
310 public void updateProtos() {
311 SegmentGraphPoint[] ppa = getProtoNeighbors();
312 for (int i = 0; i < ppa.length; i++) {
313 if (ppa[i] != null) {
314 ppa[i].updateProtoPosition();
315 }
316 }
317 }
318
319
320 public void realize() {
321 if (isReal()) {
322 E.error(" - tried to realized real pt");
323 return;
324 }
325
326 p_status = REAL;
327 setColor(Color.green);
328
329 if (p_pN2 == null) {
330 setRadius(p_pN1.getRadius());
331
332 connectTo(p_pN1);
333 p_pN1.syncExtensionProto();
334
335
336 } else {
337 p_pN1.removeNeighbor(p_pN2);
338 p_pN2.removeNeighbor(p_pN1);
339
340 connectTo(p_pN1);
341 connectTo(p_pN2);
342 setRadius((p_pN1.getRadius() + p_pN2.getRadius()) / 2.);
343 }
344 p_pN1 = null;
345 p_pN2 = null;
346
347 syncExtensionProto();
348 }
349
350
351
352 public void connectTo(SegmentGraphPoint sgp) {
353 addNeighbor(sgp);
354 sgp.addNeighbor(this);
355 }
356
357
358
359 public SegmentGraphPoint getExtensionProto() {
360 return p_extensionProto;
361 }
362
363
364 public void removeExtensionProto() {
365 p_extensionProto = null;
366 }
367
368
369 public void syncExtensionProto() {
370 if (p_extensionProto == null || p_extensionProto.isReal()) {
371 Position ppos = getPosition();
372 p_extensionProto = new SegmentGraphPoint(ppos.getX(), ppos.getY() - 1.5 * radius, PROTO);
373 p_extensionProto.setProtoNeighbors(this, null);
374 }
375 p_extensionProto.updateProtoPosition();
376 }
377
378
379 public void setProtoNeighbors(SegmentGraphPoint sgp1, SegmentGraphPoint sgp2) {
380 p_pN1 = sgp1;
381 p_pN2 = sgp2;
382 updateProtoPosition();
383 }
384
385
386
387 public void updateProtoPosition() {
388 if (isReal()) {
389 E.error(" - called update proto on real point");
390 } else {
391 Position p1 = p_pN1.getPosition();
392 double x1 = p1.getX();
393 double y1 = p1.getY();
394 double r1 = p_pN1.getRadius();
395
396 if (p_pN2 == null) {
397 setPosition(x1, y1 - 1.5 * r1);
398 } else {
399 Position p2 = p_pN2.getPosition();
400 double x2 = p2.getX();
401 double y2 = p2.getY();
402 setPosition((x1 + x2) / 2., (y1 + y2) / 2.);
403 }
404 }
405 }
406
407
408
409 private void addNeighbor(SegmentGraphPoint sgp) {
410 p_neighbors.add(sgp);
411 p_nbrCache = null;
412 p_protoNbrs = null;
413 }
414
415
416 private void removeNeighbor(SegmentGraphPoint sgp) {
417 p_neighbors.remove(sgp);
418 p_nbrCache = null;
419 p_protoNbrs = null;
420 }
421
422
423
424 }