View Javadoc

1   package org.catacomb.graph.arbor;
2   
3   
4   import org.catacomb.be.DeReferencable;
5   import org.catacomb.be.Position;
6   import org.catacomb.be.ReReferencable;
7   import org.catacomb.graph.drawing.VectorIcon;
8   import org.catacomb.interlish.structure.*;
9   
10  import java.util.ArrayList;
11  
12  
13  
14  public class SegmentGraph implements ReReferencable, DeReferencable, PointAttachable,
15      MeshInstructor {
16  
17      public ArrayList<SegmentGraphPoint> points;
18      private ArrayList<AttachmentPoint> p_bufAttachmentPoints;
19  
20      private double[] bufLimits;
21  
22  
23      public SegmentGraph() {
24          points = new ArrayList<SegmentGraphPoint>();
25      }
26  
27  
28      public void addInitialGraph() {
29          SegmentGraphPoint sgp1 = addNewPoint(0., 0., null);
30          addNewPoint(10., 0., sgp1);
31      }
32  
33  
34      public void ensureNonEmpty() {
35          if (points.size() == 0) {
36              addInitialGraph();
37          }
38      }
39  
40  
41  
42      // for serialization and deserialization;
43  
44      public void deReference() {
45          // called before serializing to convert private references to storable
46          // strings;
47          indexPoints();
48          for (SegmentGraphPoint sgp : getPoints()) {
49              sgp.deReference();
50          }
51      }
52  
53  
54  
55      public void reReference() {
56          // called after restoring from file to convert stored values to private
57          // references;
58          indexPoints();
59          for (SegmentGraphPoint sgp : getPoints()) {
60              sgp.reReference(points);
61          }
62      }
63  
64  
65  
66      public ArrayList<AttachmentPoint> getAttachmentPoints() {
67          // the ones returned could be all or just the labelled ones;
68          if (p_bufAttachmentPoints == null || p_bufAttachmentPoints.size() != size()) {
69  
70              p_bufAttachmentPoints = new ArrayList<AttachmentPoint>();
71              for (SegmentGraphPoint sgp : getPoints()) {
72                  p_bufAttachmentPoints.add(sgp.makeAttachmentPoint());
73              }
74          }
75          return p_bufAttachmentPoints;
76      }
77  
78  
79  
80      public Mesh makeMesh(MeshBuilder mbuilder) {
81          mbuilder.startMesh();
82  
83          indexPoints();
84          for (SegmentGraphPoint sgp : getPoints()) {
85              Position pos = sgp.getPosition();
86              Object newPoint = mbuilder.newPoint(pos.getX(), pos.getY(),
87                                                  sgp.getZ(), sgp.getR(),
88                                                  sgp.getIndex(), sgp);
89  
90              int i = sgp.getIndex();
91              SegmentGraphPoint[] sgn = sgp.getNeighbors();
92              for (int k = 0; k < sgn.length; k++) {
93                  if (sgn[k].getIndex() < i) {
94                      mbuilder.connectToPeer(newPoint, sgn[k]);
95                  }
96              }
97          }
98  
99          Mesh mesh = mbuilder.getMesh();
100         return mesh;
101 
102     }
103 
104 
105 
106     public int size() {
107         return getPointCount();
108     }
109 
110 
111     public int getPointCount() {
112         return points.size();
113     }
114 
115 
116     public ArrayList<SegmentGraphPoint> getPoints() {
117         return points;
118     }
119 
120 
121     public void indexPoints() {
122         int i = 0;
123         for (SegmentGraphPoint sgp : points) {
124             sgp.setIndex(i);
125             i += 1;
126         }
127     }
128 
129 
130 
131     public SegmentGraphPoint addNewPoint(double x, double y, SegmentGraphPoint psgp) {
132         SegmentGraphPoint sgp = new SegmentGraphPoint(x, y);
133         if (psgp != null) {
134             psgp.connectTo(sgp);
135         }
136         addPoint(sgp);
137         return sgp;
138     }
139 
140 
141 
142     public void addRealize(SegmentGraphPoint sgp) {
143         addPoint(sgp);
144         sgp.realize();
145     }
146 
147 
148 
149     public void clearHighlight() {
150         for (SegmentGraphPoint sgp : points) {
151             sgp.unHighlight();
152         }
153     }
154 
155 
156     public void clearMark() {
157         for (SegmentGraphPoint sgp : points) {
158             sgp.unMark();
159         }
160     }
161 
162 
163     public void highlightPath(SegmentGraphPoint sgpa, SegmentGraphPoint sgpb) {
164         clearHighlight();
165         clearMark();
166         sgpb.highlight();
167         sgpa.recHighlightPath();
168     }
169 
170 
171 
172     public void highlightTree(SegmentGraphPoint sgpa, SegmentGraphPoint sgpb) {
173         clearHighlight();
174         sgpa.highlight();
175         sgpb.recHighlight();
176     }
177 
178 
179     public void highlightTrace(SegmentGraphPoint sgpa) {
180         clearHighlight();
181         sgpa.recHighlight();
182     }
183 
184 
185 
186     public void attachLabelToSelected(String slabel) {
187         for (SegmentGraphPoint sgp : points) {
188             if (sgp.isHighlighted()) {
189                 sgp.setLabel(slabel);
190             }
191         }
192     }
193 
194 
195 
196     public void showLabel(String s) {
197         for (SegmentGraphPoint sgp : points) {
198             sgp.highlightLabel(s);
199         }
200     }
201 
202 
203 
204     public void addPoint(SegmentGraphPoint sgp) {
205         sgp.setIndex(points.size());
206         points.add(sgp);
207         p_bufAttachmentPoints = null;
208 
209     }
210 
211 
212     public void removePoint(SegmentGraphPoint sgp) {
213         points.remove(sgp);
214         p_bufAttachmentPoints = null;
215     }
216 
217 
218     public double[] getLimits() {
219         if (bufLimits == null) {
220             SegmentGraphPoint p0 = points.get(0);
221             double xmin = p0.getX();
222             double ymin = p0.getY();
223             double xmax = xmin;
224             double ymax = ymin;
225             for (SegmentGraphPoint p : points) {
226                 double x = p.getX();
227                 double y = p.getY();
228                 if (x < xmin) {
229                     xmin = x;
230                 }
231                 if (x > xmax) {
232                     xmax = x;
233                 }
234                 if (ymin < y) {
235                     ymin = y;
236                 }
237                 if (ymax > y) {
238                     ymax = y;
239                 }
240             }
241 
242             double[] da = { xmin, ymin, xmax, ymax };
243             bufLimits = da;
244         }
245         return bufLimits;
246     }
247 
248 
249 
250     public VectorIcon getVectorIcon() {
251         double[] xyxy = getLimits();
252         double xmin = xyxy[0];
253         double ymin = xyxy[1];
254         double xmax = xyxy[2];
255         double ymax = xyxy[3];
256         double dx = xmax - xmin + 1.e-4;
257         double dy = ymax - ymin + 1.e-4;
258         double hdx = (dx / 2);
259         double hdy = (dy / 2);
260         double xcen = (xmin + xmax) / 2.;
261         double ycen = (ymin + ymax) / 2.;
262 
263 
264         VectorIcon ret = new VectorIcon();
265         for (SegmentGraphPoint p : points) {
266             double x0 = (p.getX() - xcen) / hdx;
267             double y0 = (p.getY() - ycen) / hdy;
268             for (SegmentGraphPoint q : p.getNeighbors()) {
269                 double x1 = (q.getX() - xcen) / hdx;
270                 double y1 = (q.getY() - ycen) / hdy;
271                 ret.addStraightLine(x0, y0, x1, y1);
272             }
273 
274         }
275 
276         return ret;
277     }
278 
279 }