View Javadoc

1   package org.lcsim.detector.solids;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   import org.lcsim.detector.ITransform3D;
6   
7   /**
8    *
9    * @author tknelson
10   */
11  public class Polygon3D extends Plane3D
12  {
13      
14      private List<Point3D> _points; // ordered list of points bounding the polygon
15      
16      /**
17       * Creates a new instance of Polygon3D
18       */
19      public Polygon3D(List<Point3D> points)
20      {
21          super(points);
22          _points = points;
23          checkNormal();
24      }
25      
26      public int nSides()
27      {
28          return _points.size();
29      }
30      
31      public Plane3D getPlane()
32      {
33          return (Plane3D)this;
34      }
35      
36      public List<Point3D> getVertices()
37      {
38          return _points;
39      }
40      
41      public List<Point3D> getClosedVertices()
42      {
43          List<Point3D> closed_points = new ArrayList<Point3D>(_points);
44          closed_points.add(_points.get(0));
45          return closed_points;
46      }
47      
48      public void checkNormal()
49      {
50          Plane3D test_plane = new Plane3D(_points);
51          if (!GeomOp3D.equals(test_plane,getPlane()))
52          {
53              System.out.println("Normal to points: "+test_plane.getNormal());
54              System.out.println("Normal to plane: "+getNormal());
55              throw new RuntimeException("Normal to points does not match plane");
56          }
57      }
58    
59  //    public void reverseNormal()
60  //    {
61  //        super.reverseNormal();
62  //        checkNormal();
63  //    }  
64      
65  //    public void faceOutward()
66  //    {
67  //        if (getDistance() < 0) 
68  //        {
69  //            reverseNormal();
70  //        }
71  //    }
72  //   
73      
74      public void reverseNormal()
75      {
76          
77  //        System.out.println("Reversing points!!!!!!!!!!!!!!!!!");
78  //        if (1 == 1) throw new RuntimeException();
79          
80          super.reverseNormal();
81  
82          List<Point3D> reversed_points = new ArrayList<Point3D>();
83          for (int ipoint = _points.size()-1; ipoint >= 0; ipoint--)
84          {
85              reversed_points.add(_points.get(ipoint));
86          }
87          
88          _points = reversed_points;
89          
90          checkNormal();
91          
92      }
93      
94      public List<LineSegment3D> getEdges()
95      {
96          List<LineSegment3D> edges = new ArrayList<LineSegment3D>();
97          
98          List<Point3D> points = getClosedVertices();
99          for (int ipoint = 0; ipoint<points.size()-1; ipoint++)
100         {
101             edges.add(new LineSegment3D(points.get(ipoint),points.get(ipoint+1)));
102         }
103         
104         return edges;
105     }
106     
107     public void transform(ITransform3D transform)
108     {
109         
110         // can these get out of sync?
111         
112         super.transform(transform);
113         for (Point3D point : _points)
114         {
115             point.transform(transform);
116         }
117         
118         checkNormal();
119         
120 //        Plane3D new_plane = new Plane3D(_points);
121 //        this._distance = new_plane.getDistance();
122 //        this._normal = new_plane.getNormal();
123     }
124     
125     public Polygon3D transformed(ITransform3D transform)
126     {
127         
128 //        System.out.println("Polygon before transformation: "+this);
129         
130         List<Point3D> transformed_points = new ArrayList<Point3D>();
131         for (Point3D point : _points)
132         {
133             transformed_points.add(point.transformed(transform));
134         }
135         
136         Polygon3D transformed_polygon = new Polygon3D(transformed_points);
137         
138 //        System.out.println("Polygon after transformation: "+transformed_polygon);
139         
140         checkNormal();
141         
142         return transformed_polygon;
143         
144 //        return new Polygon3D(transformed_points);
145     }
146     
147     public String toString()
148     {
149         String newline = System.getProperty("line.separator");
150         String output = "Polygon3D: "+newline+
151                 "   Normal:     "+this.getNormal()+newline+
152                 "   Distance:   "+this.getDistance()+newline+
153                 "   Vertices:"+newline;
154         for (Point3D vertex : this.getVertices())
155         {
156             output += vertex.getHep3Vector()+newline;
157         }
158         return output;
159     }
160     
161 }