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 with elliptical faces, centered at HepRepPoints, with given 
19   * "Radius" and "Radius2", positioned using "Phi", "Theta" and "Omega".
20   *
21   * @author Mark Donszelmann
22   * @version $Id: DrawAsEllipsePrism.java 645 2005-02-27 02:23:41Z duns $
23   */
24  
25  public class DrawAsEllipsePrism extends AbstractDrawAs {
26  
27      private static final int MAX_FACES = 64;
28      
29      private double ellipse1[][] = new double[3][PolyPoint.ELLIPSEPOINTS];
30      private double ellipse2[][] = new double[3][PolyPoint.ELLIPSEPOINTS];
31      private double side[][][] = new double[MAX_FACES][3][2];
32  
33      public String getKey() {
34          return "ellipseprism";
35      }
36  
37      public String getName() {
38          return "EllipsePrism";
39      }
40      
41      public String getDescription() {
42          return "Draws a HepRepInstance as a Cylinder with Elliptical ends.";
43      }
44  
45      private void createEllipsePrism(double x1, double y1, double z1,
46                                      double r11, double r12, double phi1, double theta1, double omega1,
47                                      double x2, double y2, double z2,
48                                      double r21, double r22, double phi2, double theta2, double omega2,
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.ellipse(r11, r12, x1, y1, z1, phi + phi1, theta + theta1, omega1, ellipse1);
58          PolyPoint.ellipse(r21, r22, x2, y2, z2, phi + phi2, theta + theta2, omega2, ellipse2);
59          PolyPoint.linesOnArcs(r11, r12, x1, y1, z1, phi + phi1, theta + theta1, omega1, 
60                                r21, r22, x2, y2, z2, phi + phi2, theta + theta2, omega2, 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 r01    = p.getAttValue("radius").getDouble();
75          double r02    = p.getAttValue("radius2").getDouble();
76          double phi0   = p.getAttValue("phi").getDouble();
77          double theta0 = p.getAttValue("theta").getDouble();
78          double omega0 = p.getAttValue("omega").getDouble();
79          while (iterator.hasNext()) {
80              p = (HepRepPoint)iterator.next();  
81              double x     = p.getX();
82              double y     = p.getY();
83              double z     = p.getZ();
84              double r1    = p.getAttValue("radius").getDouble();
85              double r2    = p.getAttValue("radius2").getDouble();
86              double phi   = p.getAttValue("phi").getDouble();
87              double theta = p.getAttValue("theta").getDouble();
88              double omega = p.getAttValue("omega").getDouble();
89         
90              createEllipsePrism(x0, y0, z0, r01, r02, phi0, theta0, omega0,
91                                 x, y, z, r1, r2, phi, theta, omega,
92                                 faces);
93  
94              ellipse1 = projection.transform(viewPort, ellipse1, PolyPoint.ELLIPSEPOINTS);
95              ellipse2 = projection.transform(viewPort, ellipse2, PolyPoint.ELLIPSEPOINTS);
96  
97              if (atts.isFilled()) {
98                  graphics.setColor(atts.getFillColor());
99                  graphics.fillPolygon(ellipse1[U], ellipse1[V], PolyPoint.ELLIPSEPOINTS);
100                 graphics.fillPolygon(ellipse2[U], ellipse2[V], PolyPoint.ELLIPSEPOINTS);
101                 graphics.setColor(atts.getColor());
102             }
103             graphics.drawPolygon(ellipse1[U], ellipse1[V], PolyPoint.ELLIPSEPOINTS);
104             graphics.drawPolygon(ellipse2[U], ellipse2[V], PolyPoint.ELLIPSEPOINTS);
105             for (int i=0; i<faces; i++) {
106                 side[i] = projection.transform(viewPort, side[i], 2);
107                 graphics.drawPolyline(side[i][U], side[i][V], 2);
108             }            
109 
110             x0 = x; y0 = y; z0 = z;
111             r01 = r1; r02 = r2; phi0 = phi; theta0 = theta; omega0 = omega;
112         }
113     }
114 
115     public void frame(VectorGraphics graphics, HepRepInstance instance,
116                       Attributes atts,  GraphicsMode mode, 
117                       Projection projection, ViewPort viewPort) {
118 
119         graphics.setColor(atts.getFrameColor());
120         graphics.setLineWidth(atts.getFrameWidth()*2+atts.getLineWidth());
121 
122         Iterator iterator = instance.getPoints().iterator();
123         if (!iterator.hasNext()) return;
124         HepRepPoint p = (HepRepPoint)iterator.next();
125         int faces     = Math.min(p.getAttValue("faces").getInteger(), MAX_FACES);
126         double x0     = p.getX();
127         double y0     = p.getY();
128         double z0     = p.getZ();
129         double r01    = p.getAttValue("radius").getDouble();
130         double r02    = p.getAttValue("radius2").getDouble();
131         double phi0   = p.getAttValue("phi").getDouble();
132         double theta0 = p.getAttValue("theta").getDouble();
133         double omega0 = p.getAttValue("omega").getDouble();
134         while (iterator.hasNext()) {
135             p = (HepRepPoint)iterator.next();  
136             double x     = p.getX();
137             double y     = p.getY();
138             double z     = p.getZ();
139             double r1    = p.getAttValue("radius").getDouble();
140             double r2    = p.getAttValue("radius2").getDouble();
141             double phi   = p.getAttValue("phi").getDouble();
142             double theta = p.getAttValue("theta").getDouble();
143             double omega = p.getAttValue("omega").getDouble();
144        
145             createEllipsePrism(x0, y0, z0, r01, r02, phi0, theta0, omega0,
146                                x, y, z, r1, r2, phi, theta, omega, 
147                                faces);
148 
149             ellipse1 = projection.transform(viewPort, ellipse1, PolyPoint.ELLIPSEPOINTS);
150             ellipse2 = projection.transform(viewPort, ellipse2, PolyPoint.ELLIPSEPOINTS);
151 
152             graphics.drawPolygon(ellipse1[U], ellipse1[V], PolyPoint.ELLIPSEPOINTS);
153             graphics.drawPolygon(ellipse2[U], ellipse2[V], PolyPoint.ELLIPSEPOINTS);
154             for (int i=0; i<faces; i++) {
155                 side[i] = projection.transform(viewPort, side[i], 2);
156                 graphics.drawPolyline(side[i][U], side[i][V], 2);
157             }            
158 
159             x0 = x; y0 = y; z0 = z;
160             r01 = r1; r02 = r2; phi0 = phi; theta0 = theta; omega0 = omega;
161         }
162         
163         graphics.setLineWidth(atts.getLineWidth());
164         graphics.setColor(atts.getColor());
165     }
166 }