View Javadoc

1   // Copyright 2004-2005, FreeHEP.
2   package hep.wired.heprep.util;
3   
4   import hep.wired.util.Matrix3D;
5   import hep.wired.util.XYZindices;
6   
7   /***
8    * Converts primitives into polypoints, for use in drawing as polylines or polygons.
9    *
10   * FIXME WIRED-270
11   * NOTE: arcs could draw ELLIPSEPOINTS divided by the part of the ellipse which they draw
12   * NOTE: linesOnArcs should be redone into faces which can be filled.
13   *
14   * @author Mark Donszelmann
15   * @version $Id: PolyPoint.java 2085 2005-07-20 17:55:45Z duns $
16   */
17  
18  public class PolyPoint implements XYZindices {
19  
20      public static final int ELLIPSEPOINTS = 46;
21  
22      private PolyPoint() {
23          // only static methods
24      }
25  
26      /***
27       * Converts a circle into a polypoint.
28       */
29      public static int circle(double r,
30                               double x, double y, double z,
31                               double phi, double theta,
32                               double[][] xyz) {
33          return arc(r, r, x, y, z, phi, theta, 0, 0, Math.PI*2, xyz);
34      }
35  
36      /***
37       * Converts an ellipse into a polypoint.
38       */
39      public static int ellipse(double rx, double ry,
40                                double x, double y, double z,
41                                double phi, double theta, double omega,
42                                double[][] xyz) {
43          return arc(rx, ry, x, y, z, phi, theta, omega, 0, Math.PI*2, xyz);
44      }
45  
46      /***
47       * Converts an arc into a polypoint.
48       */
49      public static int arc(double r,
50                            double x, double y, double z,
51                            double phi, double theta,
52                            double start, double end,
53                            double[][] xyz) {
54          return arc(r, r, x, y, z, phi, theta, 0, start, end, xyz);
55      }
56  
57      /***
58       * Converts an arc into a polypoint.
59       */
60      public static int arc(double rx, double ry,
61                            double x, double y, double z,
62                            double phi, double theta, double omega,
63                            double start, double end,
64                            double[][] xyz) {
65  
66          int n = Math.min(xyz[X].length, ELLIPSEPOINTS);
67          if (n <= 1) return 0;
68          
69          double delta = (end - start) / (n - 1);
70          for (int i=0; i<n; i++) {
71              double angle = start + i*delta;
72              xyz[X][i] = rx * Math.cos(angle);
73              xyz[Y][i] = ry * Math.sin(angle);
74              xyz[Z][i] = 0;
75          }
76          
77          // FIXME WIRED-249 we still recreate a new Matrix here every time.
78          Matrix3D m = Matrix3D.getRotateInstance(phi, theta, omega);
79          m.translate(x, y, z);
80          xyz = m.transform(xyz, n, false);
81          
82          return n;
83      }
84  
85      /***
86       * Converts the lines between two arcs into number of polypoints.
87       * Returns number of lines generated. Each line is straight, containing start and end point.
88       */
89      public static int linesOnArcs(double rx1, double ry1,
90                                    double x1, double y1, double z1,
91                                    double phi1, double theta1, double omega1,
92                                    double rx2, double ry2,
93                                    double x2, double y2, double z2,
94                                    double phi2, double theta2, double omega2,
95                                    double start, double end,
96                                    int nLines,
97                                    double[][][] xyz) {
98  
99          int n = Math.min(xyz.length, nLines);
100 
101         double delta = (end - start) / (n - 1);
102         double uvw[] = new double[3];
103         
104         // FIXME WIRED-249 we still recreate a new Matrix here every time.
105         Matrix3D m1 = Matrix3D.getRotateInstance(phi1, theta1, omega1);
106         m1.translate(x1, y1, z1);
107         Matrix3D m2 = Matrix3D.getRotateInstance(phi2, theta2, omega2);
108         m2.translate(x2, y2, z2);
109         for (int i=0; i<n; i++) {
110             double angle = start + i*delta;
111             double cosAngle = Math.cos(angle);
112             double sinAngle = Math.sin(angle);
113             
114             uvw[X] = rx1 * cosAngle;
115             uvw[Y] = ry1 * sinAngle;
116             uvw[Z] = 0;
117             uvw = m1.transform(uvw, false);
118             xyz[i][X][0] = uvw[X];
119             xyz[i][Y][0] = uvw[Y];
120             xyz[i][Z][0] = uvw[Z];
121 
122             uvw[X] = rx2 * cosAngle;
123             uvw[Y] = ry2 * sinAngle;
124             uvw[Z] = 0;
125             uvw = m2.transform(uvw, false);
126             xyz[i][X][1] = uvw[X];
127             xyz[i][Y][1] = uvw[Y];
128             xyz[i][Z][1] = uvw[Z];
129         }
130                 
131         return n;
132     }
133 }