View Javadoc

1   /**
2    * 
3    */
4   
5   package math.geom3d;
6   
7   import java.util.ArrayList;
8   import java.util.Collection;
9   import java.util.Iterator;
10  
11  import javax.vecmath.Tuple3d;
12  
13  import math.geom3d.transform.AffineTransform3D;
14  
15  /**
16   * @author dlegland
17   */
18  public class PointSet3D implements Shape3D, Iterable<Point3D> {
19  
20      protected Collection<Point3D> points = new ArrayList<Point3D>();
21  
22      public PointSet3D() {
23      }
24  
25      /**
26       * Instances of Point3D are directly added, other Point are converted to
27       * Point3D with the same location.
28       */
29      public PointSet3D(Point3D[] points) {
30          for (Point3D element : points)
31              this.points.add(element);
32      }
33      
34      /**
35       * Instances of Point3D are directly added, other Point are converted to
36       * Point3D with the same location.
37       */
38      public PointSet3D(Tuple3d[] points) {
39          for (Tuple3d element : points)
40              this.points.add(new Point3D(element));
41      }
42  
43      /**
44       * Points must be a collection of java.awt.Point. Instances of Point3D are
45       * directly added, other Point are converted to Point3D with the same
46       * location.
47       * 
48       * @param points
49       */
50      public PointSet3D(Collection<? extends Point3D> points) {
51          for (Point3D point : points) {
52              this.points.add(point);
53          }
54      }
55  
56      /**
57       * Adds a new point to the set of point. If point is not an instance of
58       * Point3D, a Point3D with same location is added instead of point.
59       * 
60       * @param point
61       */
62      public void addPoint(Point3D point) {
63          this.points.add(point);
64      }
65      
66      /**
67       * Adds a new point to the set of point. If point is not an instance of
68       * Point3D, a Point3D with same location is added instead of point.
69       * 
70       * @param point
71       */
72      public void addPoint(Tuple3d point) {
73          this.points.add(new Point3D(point));
74      }
75  
76      /**
77       * Add a series of points
78       * 
79       * @param points an array of points
80       */
81      public void addPoints(Point3D[] points) {
82          for (Point3D element : points) {
83              this.addPoint(element);
84          }
85      }
86  
87      public void addPoints(Collection<Point3D> points) {
88          this.points.addAll(points);
89      }
90  
91      /**
92       * Returns an iterator on the internal point collection.
93       * 
94       * @return the collection of points
95       */
96      public Iterator<Point3D> getPoints() {
97          return points.iterator();
98      }
99  
100     /**
101      * Removes all points of the set.
102      */
103     public void clearPoints() {
104         this.points.clear();
105     }
106 
107     /**
108      * Returns the number of points in the set.
109      * 
110      * @return the number of points
111      */
112     public int getPointsNumber() {
113         return points.size();
114     }
115 
116     // ===================================================================
117     // methods implementing the Shape3D interface
118 
119     /*
120      * (non-Javadoc)
121      * 
122      * @see math.geom3d.Shape3D#clip(math.geom3d.Box3D)
123      */
124     public Shape3D clip(Box3D box) {
125         PointSet3D res = new PointSet3D();
126         Shape3D clipped;
127         for (Point3D point : points) {
128             clipped = point.clip(box);
129             if (clipped!=Shape3D.EMPTY_SET)
130                 res.addPoint((Point3D) clipped);
131         }
132         return res;
133     }
134 
135     public Box3D getBoundingBox() {
136         double xmin = Double.MAX_VALUE;
137         double ymin = Double.MAX_VALUE;
138         double zmin = Double.MAX_VALUE;
139         double xmax = Double.MIN_VALUE;
140         double ymax = Double.MIN_VALUE;
141         double zmax = Double.MIN_VALUE;
142 
143         for (Point3D point : points) {
144             xmin = Math.min(xmin, point.getX());
145             ymin = Math.min(ymin, point.getY());
146             zmin = Math.min(zmin, point.getZ());
147             xmax = Math.max(xmax, point.getX());
148             ymax = Math.max(ymax, point.getY());
149             zmax = Math.max(zmax, point.getZ());
150         }
151         return new Box3D(xmin, xmax, ymin, ymax, zmin, zmax);
152     }
153 
154     /*
155      * (non-Javadoc)
156      * 
157      * @see math.geom3d.Shape3D#getDistance(math.geom3d.Point3D)
158      */
159     public double getDistance(Point3D p) {
160         if (points.isEmpty())
161             return Double.POSITIVE_INFINITY;
162         double dist = Double.POSITIVE_INFINITY;
163         for (Point3D point : points)
164             dist = Math.min(dist, point.getDistance(p));
165         return dist;
166     }
167 
168     public boolean contains(Point3D point) {
169         for (Point3D p : points)
170             if (point.getDistance(p)<Shape3D.ACCURACY)
171                 return true;
172         return false;
173     }
174 
175     public boolean isEmpty() {
176         return points.size()==0;
177     }
178 
179     public boolean isBounded() {
180         return true;
181     }
182 
183     /*
184      * (non-Javadoc)
185      * 
186      * @see math.geom3d.Shape3D#transform(math.geom3d.AffineTransform3D)
187      */
188     public Shape3D transform(AffineTransform3D trans) {
189         PointSet3D res = new PointSet3D();
190         for (Point3D point : points)
191             res.addPoint(point.transform(trans));
192         return res;
193     }
194 
195     // ===================================================================
196     // methods implementing the Iterable interface
197 
198     /*
199      * (non-Javadoc)
200      * 
201      * @see java.lang.Iterable#iterator()
202      */
203     public Iterator<Point3D> iterator() {
204         return points.iterator();
205     }
206 }