View Javadoc

1   // Copyright 2004-2005, FreeHEP.
2   package hep.wired.heprep.representation;
3   
4   import java.util.*;
5   
6   import org.freehep.graphics2d.VectorGraphics;
7   import hep.graphics.heprep.HepRepInstance;
8   import hep.graphics.heprep.HepRepPoint;
9   
10  import hep.wired.heprep.services.GraphicsMode;
11  import hep.wired.heprep.services.Attributes;
12  import hep.wired.heprep.services.Projection;
13  import hep.wired.heprep.services.DrawAs;
14  import hep.wired.services.ViewPort;
15  
16  /***
17   * Draws a cubic bezier curve specified by the HepRepPoint control points.
18   *
19   * @author Mark Donszelmann
20   * @version $Id: DrawAsCurve.java 645 2005-02-27 02:23:41Z duns $
21   */
22  
23  public class DrawAsCurve extends AbstractDrawAs {
24  
25      // FIXME  WIRED-253, limited.
26      private double uvw[][] = new double[3][10000];
27  
28      public String getKey() {
29          return "curve";
30      }
31  
32      public String getName() {
33          return "Curve";
34      }
35      
36      public String getDescription() {
37          return "Draws cubic bezier curves.";
38      }
39      
40      private double[][] createCurve(double[][] uvw, int n) {
41          // first point is already correct
42          for (int i=1; i<n; i+=3) {
43              double t = (i % 3.0) / 3.0;
44              double f0 = Math.pow(1-t,3);
45              double f1 = 3 * t * Math.pow(1-t,2);
46              double f2 = 3 * Math.pow(t,2) * (1-t);
47              double f3 = Math.pow(t,3);
48              uvw[X][i] = f0*uvw[X][i-1] + f1*uvw[X][i] + f2*uvw[X][i+1] + f3*uvw[X][i+2];
49              uvw[Y][i] = f0*uvw[Y][i-1] + f1*uvw[Y][i] + f2*uvw[Y][i+1] + f3*uvw[Y][i+2];
50              uvw[Z][i] = f0*uvw[Z][i-1] + f1*uvw[Z][i] + f2*uvw[Z][i+1] + f3*uvw[Z][i+2];
51          }
52          return uvw;
53      }
54          
55      public void draw(VectorGraphics graphics, HepRepInstance instance,
56                       Attributes atts,  GraphicsMode mode, 
57                       Projection projection, ViewPort viewPort) {
58  
59          int n = instance.getPoints(uvw);
60          if (n < 0) {
61              n = 0;
62              for (Iterator i=instance.getPoints().iterator(); i.hasNext(); ) {
63  
64                  HepRepPoint p = (HepRepPoint)i.next();
65                  uvw[U][n] = p.getX();
66                  uvw[V][n] = p.getY();
67                  uvw[W][n] = p.getZ();
68                  n++;
69              }
70          }
71  
72          uvw = createCurve(uvw, n);
73          uvw = projection.transform(viewPort, uvw, n);
74          graphics.drawPolyline(uvw[U], uvw[V], n);
75      }
76  
77      public void frame(VectorGraphics graphics, HepRepInstance instance,
78                        Attributes atts,  GraphicsMode mode, 
79                        Projection projection, ViewPort viewPort) {
80  
81          int n = instance.getPoints(uvw);
82  
83          if (n < 0) {
84              n = 0;
85              for (Iterator i=instance.getPoints().iterator(); i.hasNext(); ) {
86  
87                  HepRepPoint p = (HepRepPoint)i.next();
88                  uvw[U][n] = p.getX();
89                  uvw[V][n] = p.getY();
90                  uvw[W][n] = p.getZ();
91                  n++;
92              }
93          }
94                           
95          uvw = createCurve(uvw, n);
96          uvw = projection.transform(viewPort, uvw, n);        
97  
98          graphics.setColor(atts.getFrameColor());
99          graphics.setLineWidth(atts.getFrameWidth()*2+atts.getLineWidth());
100         graphics.drawPolyline(uvw[U], uvw[V], n);
101         graphics.setLineWidth(atts.getLineWidth());
102         graphics.setColor(atts.getColor());
103     }
104 }