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.heprep.util.PolyPoint;
15  import hep.wired.services.ViewPort;
16  
17  /***
18   * Draws one or more cylinders, where a couple of HepRepPoints specify the faces centers, 
19   * with given "Radius", positioned using "Phi" and "Theta".
20   *
21   * @author Mark Donszelmann
22   * @version $Id: DrawAsCylinder.java 645 2005-02-27 02:23:41Z duns $
23   */
24  
25  public class DrawAsCylinder extends AbstractDrawAs {
26  
27      private static final int MAX_FACES = 64;
28      
29      private double circle1[][] = new double[3][PolyPoint.ELLIPSEPOINTS];
30      private double circle2[][] = new double[3][PolyPoint.ELLIPSEPOINTS];
31      private double side[][][]  = new double[MAX_FACES][3][2];
32  
33      public String getKey() {
34          return "cylinder";
35      }
36  
37      public String getName() {
38          return "Cylinder";
39      }
40      
41      public String getDescription() {
42          return "Draws a HepRepInstance as a Cylinder.";
43      }
44  
45      private void createCylinders(double x1, double y1, double z1,
46                                   double r1, double phi1, double theta1,
47                                   double x2, double y2, double z2,
48                                   double r2, double phi2, double theta2, 
49                                   int faces) {
50          double dx = x2 - x1;
51          double dy = y2 - y1;
52          double dz = z2 - z1;
53          double dt = Math.sqrt(dx*dx + dz*dz);
54          double phi = Math.atan2(dx, dz);
55          double theta = -Math.atan2(dy, dt);
56                  
57          PolyPoint.circle(r1, x1, y1, z1, phi + phi1, theta + theta1, circle1);
58          PolyPoint.circle(r2, x2, y2, z2, phi + phi2, theta + theta2, circle2);
59          PolyPoint.linesOnArcs(r1, r1, x1, y1, z1, phi + phi1, theta + theta1, 0, 
60                                r2, r2, x2, y2, z2, phi + phi2, theta + theta2, 0, 0, Math.PI*2, faces, side);
61      }
62  
63      public void draw(VectorGraphics graphics, HepRepInstance instance,
64                       Attributes atts,  GraphicsMode mode, 
65                       Projection projection, ViewPort viewPort) {
66  
67          Iterator iterator = instance.getPoints().iterator();
68          if (!iterator.hasNext()) return;
69          HepRepPoint p = (HepRepPoint)iterator.next();
70          int faces     = Math.min(p.getAttValue("faces").getInteger(), MAX_FACES);
71          double x0     = p.getX();
72          double y0     = p.getY();
73          double z0     = p.getZ();
74          double r0     = p.getAttValue("radius").getDouble();
75          double phi0   = p.getAttValue("phi").getDouble();
76          double theta0 = p.getAttValue("theta").getDouble();
77          while (iterator.hasNext()) {
78              p = (HepRepPoint)iterator.next();  
79              double x     = p.getX();
80              double y     = p.getY();
81              double z     = p.getZ();
82              double r     = p.getAttValue("radius").getDouble();
83              double phi   = p.getAttValue("phi").getDouble();
84              double theta = p.getAttValue("theta").getDouble();
85         
86              createCylinders(x0, y0, z0, r0, phi0, theta0,
87                              x, y, z, r, phi, theta, 
88                              faces);
89  
90              circle1 = projection.transform(viewPort, circle1, PolyPoint.ELLIPSEPOINTS);
91              circle2 = projection.transform(viewPort, circle2, PolyPoint.ELLIPSEPOINTS);
92  
93              if (atts.isFilled()) {
94                  graphics.setColor(atts.getFillColor());
95                  graphics.fillPolygon(circle1[U], circle1[V], PolyPoint.ELLIPSEPOINTS);
96                  graphics.fillPolygon(circle2[U], circle2[V], PolyPoint.ELLIPSEPOINTS);
97                  graphics.setColor(atts.getColor());
98              }
99              graphics.drawPolygon(circle1[U], circle1[V], PolyPoint.ELLIPSEPOINTS);
100             graphics.drawPolygon(circle2[U], circle2[V], PolyPoint.ELLIPSEPOINTS);
101             for (int i=0; i<faces; i++) {
102                 side[i] = projection.transform(viewPort, side[i], 2);
103                 graphics.drawPolyline(side[i][U], side[i][V], 2);
104             }            
105 
106             x0 = x; y0 = y; z0 = z;
107             r0 = r; phi0 = phi; theta0 = theta;
108         }
109     }
110 
111     public void frame(VectorGraphics graphics, HepRepInstance instance,
112                       Attributes atts,  GraphicsMode mode, 
113                       Projection projection, ViewPort viewPort) {
114 
115         graphics.setColor(atts.getFrameColor());
116         graphics.setLineWidth(atts.getFrameWidth()*2+atts.getLineWidth());
117 
118         Iterator iterator = instance.getPoints().iterator();
119         if (!iterator.hasNext()) return;
120         HepRepPoint p = (HepRepPoint)iterator.next();
121         int faces     = Math.min(p.getAttValue("faces").getInteger(), MAX_FACES);
122         double x0     = p.getX();
123         double y0     = p.getY();
124         double z0     = p.getZ();
125         double r0     = p.getAttValue("radius").getDouble();
126         double phi0   = p.getAttValue("phi").getDouble();
127         double theta0 = p.getAttValue("theta").getDouble();
128         while (iterator.hasNext()) {
129             p = (HepRepPoint)iterator.next();  
130             double x     = p.getX();
131             double y     = p.getY();
132             double z     = p.getZ();
133             double r     = p.getAttValue("radius").getDouble();
134             double phi   = p.getAttValue("phi").getDouble();
135             double theta = p.getAttValue("theta").getDouble();
136        
137             createCylinders(x0, y0, z0, r0, phi0, theta0,
138                             x, y, z, r, phi, theta, 
139                             faces);
140 
141             circle1 = projection.transform(viewPort, circle1, PolyPoint.ELLIPSEPOINTS);
142             circle1 = viewPort.transform  (circle1, PolyPoint.ELLIPSEPOINTS);
143             circle2 = projection.transform(viewPort, circle2, PolyPoint.ELLIPSEPOINTS);
144             circle2 = viewPort.transform  (circle2, PolyPoint.ELLIPSEPOINTS);
145 
146             graphics.drawPolygon(circle1[U], circle1[V], PolyPoint.ELLIPSEPOINTS);
147             graphics.drawPolygon(circle2[U], circle2[V], PolyPoint.ELLIPSEPOINTS);
148             for (int i=0; i<faces; i++) {
149                 side[i] = projection.transform(viewPort, side[i], 2);
150                 graphics.drawPolyline(side[i][U], side[i][V], 2);
151             }            
152 
153             x0 = x; y0 = y; z0 = z;
154             r0 = r; phi0 = phi; theta0 = theta;
155         }
156         
157         graphics.setLineWidth(atts.getLineWidth());
158         graphics.setColor(atts.getColor());
159     }
160 }