/[loom]/loom/org/fenfire/loom/WheelView.java
ViewVC logotype

Diff of /loom/org/fenfire/loom/WheelView.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by benja, Mon Mar 31 22:41:23 2003 UTC revision 1.2 by humppake, Thu Apr 3 08:20:18 2003 UTC
# Line 44  import com.hp.hpl.mesa.rdf.jena.model.*; Line 44  import com.hp.hpl.mesa.rdf.jena.model.*;
44   */   */
45  public class WheelView implements View {  public class WheelView implements View {
46    
47        public static boolean dbg = false;
48        private static void p(String s) { System.out.println(s); }
49    
50      /** The view used to show the individual nodes.      /** The view used to show the individual nodes.
51       */       */
52      protected NodeView nodeView;      protected NodeView nodeView;
# Line 61  public class WheelView implements View { Line 64  public class WheelView implements View {
64      protected int gapx = 50, gapy = 30;      protected int gapx = 50, gapy = 30;
65    
66      protected int radius = sizex + gapx;      protected int radius = sizex + gapx;
67        protected int recursionDepth = 0;
68    
69      /** Maximum rotation angle. */      /** Maximum rotation angle. */
70      protected double maxrota = Math.PI / 9;      protected double maxrota = Math.PI / 9;
# Line 69  public class WheelView implements View { Line 73  public class WheelView implements View {
73       *  Render either posward or negward connections       *  Render either posward or negward connections
74       *  (or onward, maybe).       *  (or onward, maybe).
75       */       */
76        /*
77      public void renderSomewardConnections(VobScene sc, int into, Cursor c,      public void renderSomewardConnections(VobScene sc, int into, Cursor c,
78                                            Resource node, int before,                                            Resource node, int before,
79                                            int dir, double rota, float r,                                            int dir, double rota, float r,
# Line 97  public class WheelView implements View { Line 102  public class WheelView implements View {
102              before--;              before--;
103          }          }
104      }      }
105        */
106    
107        /**
108         *  Render either posward or negward connections
109         *  (or onward, maybe).
110         * @param recursion Recursions steps left. Giving zero will cause
111         *                  no recursion at all.
112         * @param dir -1 for negative, 1 for positive, 0 for both
113         */
114        public void renderSomewardConnections(VobScene sc, int into, Cursor c,
115                                              Resource node, int dir,  double cura,
116                                              double [] rotas, float r,  int focusCs,
117                                              int midx, int midy, int depth, int recursion) {
118            if (dbg) p("Render, cura: "+cura+", r: "+r+", recursion: "+recursion);
119            int connCount = 0;
120    
121            SortedSet connSet;
122            if (dir == 0) {
123                connSet = c.getConnections(node, -1);
124                connSet.addAll(c.getConnections(node, 1));
125            } else connSet = c.getConnections(node, dir);
126    
127            for(Iterator j=connSet.iterator(); j.hasNext();) {
128                if (dbg) p("Render, cura: "+cura+", connCount: "+connCount);
129                
130                Statement stmt = (Statement)j.next();
131                RDFNode current = (dir<0) ? stmt.getSubject() : stmt.getObject();
132                
133                NodeView.Nodespec spec =
134                    new NodeView.Nodespec(stmt, dir);
135                int x = midx+x(cura, r)-sizex/2;
136                int y = midy+y(cura, r)-sizey/2;
137                int cs2 = sc.orthoBoxCS(into, spec, Math.abs(depth),
138                                        x, y, 1, 1, sizex, sizey);
139                sc.coords.activate(cs2);
140                nodeView.render(sc, cs2, spec);
141                    
142                if(dir > 0)
143                    sc.map.put(conn, focusCs, cs2);    
144                else if (dir < 0)
145                    sc.map.put(conn, cs2, focusCs);
146                
147                if (recursion > 0 && current != null && current instanceof Resource) {
148                    double suba;
149                    suba = rotas[connCount];
150                    double[] subas = getRotationAngles(c, (Resource)current, suba, 0,
151                                                       recursion - 1);
152                    renderSomewardConnections(sc, into, c, (Resource)current,
153                                              0, cura-(rotas[connCount]/2), subas,
154                                              r + gapx + sizex,
155                                              cs2, midx, midy, depth, recursion - 1);
156                }
157    
158                cura+=rotas[connCount];
159                connCount++;
160                depth--;
161            }
162        }
163    
164      public void render(VobScene sc, int into, Cursor c) {      public void render(VobScene sc, int into, Cursor c) {
165          int midx = sc.size.width/2, midy = sc.size.height/2;          int midx = sc.size.width/2, midy = sc.size.height/2;
# Line 105  public class WheelView implements View { Line 168  public class WheelView implements View {
168                                 1, 1, sizex, sizey);                                 1, 1, sizex, sizey);
169          sc.coords.activate(cs);          sc.coords.activate(cs);
170          nodeView.render(sc, cs, spec);          nodeView.render(sc, cs, spec);
171    
172            /** Radius = gapx * sizex */
173            int r = gapx + sizex;
174    
175            int negConnCount = c.getConnections(-1).size();
176            int posConnCount = c.getConnections(1).size();
177            int negRotIndex = c.getRotationIndex(-1);
178            int posRotIndex = c.getRotationIndex(1);
179            double[] negRotAngles = getRotationAngles(c, c.focus, Math.PI/2, -1,
180                                                   recursionDepth);
181            double[] posRotAngles = getRotationAngles(c, c.focus, Math.PI/2, 1,
182                                                   recursionDepth);
183    
184            /** Negwards connections */
185            double cura = Math.PI;
186            if (negRotAngles != null) {
187                if (negRotIndex < 0)
188                    for (int i = posConnCount+negRotIndex; i < posConnCount; i++)
189                        cura += posRotAngles[i];
190                else {
191                    if (negRotIndex > negConnCount)
192                        for (int i = negRotIndex-negConnCount-1; i >= 0; i--)
193                            cura -= posRotAngles[i];
194                    for (int i = 0; i < negRotIndex && i < negConnCount; i++)
195                        cura -= negRotAngles[i];
196                }
197                if (dbg) p("Negwards, cura: "+cura+", rotationIndex: "+negRotIndex);
198                renderSomewardConnections(sc, into, c, c.focus,
199                                          -1, cura, negRotAngles, r,
200                                          cs, midx, midy, negRotIndex,
201                                          recursionDepth);
202            }
203    
204            /** Poswards connections */
205            cura = 0;
206            if (posRotAngles != null) {
207                if (posRotIndex < 0)
208                    for (int i = negConnCount+posRotIndex; i < negConnCount; i++)
209                        cura += negRotAngles[i];
210                else {
211                    if (posRotIndex > posConnCount)
212                        for (int i = posRotIndex-posConnCount-1; i >= 0; i--)
213                            cura -= negRotAngles[i];
214                    for (int i = 0; i < posRotIndex && i < posConnCount; i++)
215                        cura -= posRotAngles[i];
216                }
217                if (dbg) p("Poswards, cura: "+cura+", rotationIndex: "+posRotIndex);
218                renderSomewardConnections(sc, into, c, c.focus,
219                                          1, cura, posRotAngles, r,
220                                          cs, midx, midy, posRotIndex,
221                                          recursionDepth);
222            }
223                    
224          int totpos = c.getConnections(1).size();          //      int totpos = c.getConnections(1).size();
225          int totneg = c.getConnections(-1).size();          //      int totneg = c.getConnections(-1).size();
226    
227          /** Rotation angle = 2PI / 2(greater of totpos and totgen). */          /** Rotation angle */
228          double rota;          //      double rota = (Math.PI) / c.getConnectionCount();
         if (totpos < totneg) rota = 2 * totneg;  
         else rota = 2 * totpos;  
         rota = (2 * Math.PI) / rota;  
229    
230          /** Rotation angle should be no bigger than defined maximu. */          /** Rotation angle should be no bigger than defined maximum. */
231          if (rota > maxrota) rota = maxrota;          //      if (rota > maxrota) rota = maxrota;
232    
         /** Radius = gapx * sizex */  
         int r = gapx + sizex;  
233    
234          /** Poswards connections */          /** Poswards connections */
235          renderSomewardConnections(sc, into, c, c.focus,          //      renderSomewardConnections(sc, into, c, c.focus,
236                                    c.getRotationIndex(1), 1,          //                                c.getRotationIndex(1), 1,
237                                    rota, r, cs, 1, midx, midy);          //                                rota, r, cs, 1, midx, midy);
238    
239          /** Negward connections */          /** Negward connections */
240          renderSomewardConnections(sc, into, c, c.focus,          //      renderSomewardConnections(sc, into, c, c.focus,
241                                    c.getRotationIndex(-1), -1,          //                                c.getRotationIndex(-1), -1,
242                                    rota, r, cs, 1, midx, midy);          //                                rota, r, cs, 1, midx, midy);
243        }
244    
245        /**
246         * @param recursion Recursions steps left. Giving zero will cause
247         *                  no recursion at all.
248         * @param dir -1 for negative, 1 for positive, 0 for both
249         */
250        protected double[] getRotationAngles(Cursor c, Resource node,
251                                              double angle, int dir,
252                                              int recursion) {
253            if (dbg) p("getRotationAngles, angle: "+angle+", dir: "+dir+", recursion: "+recursion);
254    
255            int conns = 0;
256            if (dir == 0) {
257                conns = c.getConnections(node, -1).size() +
258                    c.getConnections(node, 1).size();
259            } else conns = c.getConnections(node, dir).size();
260    
261            if (dbg) p("getRotationAngles, conns: "+conns);
262            if (conns == 0) return null;
263    
264            double[] angles = new double[conns];
265    
266            if (recursion == 0) {
267                double rota = angle / conns;
268                if (dbg) p("getRotationAngle, no recursion, rota: "+rota);
269                double shift = 0;
270                if (rota > maxrota) rota = maxrota;
271                for (int i = 0; i < conns; i++) angles[i] = rota;
272                if (dbg) p("getRotationAngle, no recursion, angles: "+dArrayToString(angles));
273                return angles;
274            }
275    
276            int connCount = 0;
277            int totalsubconns = 0;
278            int[] subconns = new int[conns];
279    
280            SortedSet connSet;
281            if (dir == 0) {
282                connSet = c.getConnections(node, -1);
283                connSet.addAll(c.getConnections(node, 1));
284            } else connSet = c.getConnections(node, dir);
285    
286            for(Iterator j=connSet.iterator(); j.hasNext();) {
287                Statement stmt = (Statement)j.next();
288                RDFNode current = (dir<0) ? stmt.getSubject() : stmt.getObject();
289                if(current != null && current instanceof Resource) {
290                    subconns[connCount] = getConnectionCount(c, (Resource)current,
291                                                             dir, recursion - 1);
292                    totalsubconns += subconns[connCount];
293                    if (subconns[connCount] == 0) subconns[connCount] = 1;
294                } else subconns[connCount] = 1;
295                connCount++;
296            }
297    
298            if ((angle / totalsubconns) >= maxrota)
299                angle = totalsubconns * maxrota;
300            for (int i = 0; i < conns; i++)
301                angles[i] = angle * ((double)subconns[i] / (double)totalsubconns);
302    
303            if (dbg) p("getRotationAngle, angles: "+dArrayToString(angles));
304    
305            return angles;
306      }      }
307    
308      int x(double angle, float radius) {      protected int getConnectionCount(Cursor c, Resource node,
309                                     int dir, int recursion) {
310            if (dbg) p("getConnectionCount, dir: "+dir+", recursion: "+recursion);
311            int conns = 0;
312            if (dir == 0) {
313                conns = c.getConnections(node, -1).size() +
314                    c.getConnections(node, 1).size();
315            } else conns = c.getConnections(node, dir).size();
316    
317            if (dbg) p("getConnectionCount, conns: "+conns);
318    
319            if (recursion == 0) return conns;
320    
321            int subconns = 0;
322            
323            SortedSet connSet;
324            if (dir == 0) {
325                connSet = c.getConnections(node, -1);
326                connSet.addAll(c.getConnections(node, 1));
327            } else connSet = c.getConnections(node, dir);
328    
329            for(Iterator j=connSet.iterator(); j.hasNext();) {
330                Statement stmt = (Statement)j.next();
331                RDFNode current = (dir<0) ? stmt.getSubject() : stmt.getObject();
332                if(current != null && current instanceof Resource)
333                    subconns += getConnectionCount(c, (Resource)current,
334                                                   dir, recursion - 1);
335            }
336    
337            if (dbg) p("getConnectionCount, subconns: "+subconns);  
338            return (conns<subconns) ? subconns : conns;
339        }
340    
341        private String dArrayToString(double[] a) {
342            if (dbg) p("dArrayToString, length: " + a.length);
343            String s = "";
344            for (int i = 0; i < a.length; i++)
345                s += "" + a[i] + " ";
346            return s;
347        }
348    
349        private int x(double angle, float radius) {
350          return (int)(Math.cos(angle) * radius);          return (int)(Math.cos(angle) * radius);
351      }      }
352                    
353      int y(double angle, float radius) {      private int y(double angle, float radius) {
354          return (int)(Math.sin(angle) * radius);          return (int)(Math.sin(angle) * radius);
355      }      }
356  }  }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26