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
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
27
28
29 public PointSet3D(Point3D[] points) {
30 for (Point3D element : points)
31 this.points.add(element);
32 }
33
34
35
36
37
38 public PointSet3D(Tuple3d[] points) {
39 for (Tuple3d element : points)
40 this.points.add(new Point3D(element));
41 }
42
43
44
45
46
47
48
49
50 public PointSet3D(Collection<? extends Point3D> points) {
51 for (Point3D point : points) {
52 this.points.add(point);
53 }
54 }
55
56
57
58
59
60
61
62 public void addPoint(Point3D point) {
63 this.points.add(point);
64 }
65
66
67
68
69
70
71
72 public void addPoint(Tuple3d point) {
73 this.points.add(new Point3D(point));
74 }
75
76
77
78
79
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
93
94
95
96 public Iterator<Point3D> getPoints() {
97 return points.iterator();
98 }
99
100
101
102
103 public void clearPoints() {
104 this.points.clear();
105 }
106
107
108
109
110
111
112 public int getPointsNumber() {
113 return points.size();
114 }
115
116
117
118
119
120
121
122
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
156
157
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
185
186
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
197
198
199
200
201
202
203 public Iterator<Point3D> iterator() {
204 return points.iterator();
205 }
206 }