1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package math.geom2d.domain;
28
29 import java.awt.Graphics2D;
30 import java.util.ArrayList;
31 import java.util.Collection;
32
33 import math.geom2d.AffineTransform2D;
34 import math.geom2d.Box2D;
35 import math.geom2d.curve.*;
36
37
38
39
40
41
42
43
44 public class BoundarySet2D<T extends ContinuousBoundary2D>
45 extends CurveArray2D<T> implements Boundary2D {
46
47
48
49
50 public BoundarySet2D() {
51 }
52
53 public BoundarySet2D(int size) {
54 super(size);
55 }
56
57 public BoundarySet2D(T[] curves) {
58 super(curves);
59 }
60
61 public BoundarySet2D(Collection<? extends T> curves) {
62 super(curves);
63 }
64
65 public BoundarySet2D(T curve) {
66 super();
67 this.addCurve(curve);
68 }
69
70
71
72
73
74
75
76
77
78
79 public static <T extends ContinuousBoundary2D> BoundarySet2D<T> create(
80 Collection<T> curves) {
81 return new BoundarySet2D<T>(curves);
82 }
83
84
85
86
87
88
89 public static <T extends ContinuousBoundary2D> BoundarySet2D<T> create(
90 T[] curves) {
91 return new BoundarySet2D<T>(curves);
92 }
93
94
95
96
97 public Collection<ContinuousBoundary2D> getBoundaryCurves() {
98 ArrayList<ContinuousBoundary2D> list =
99 new ArrayList<ContinuousBoundary2D>(curves.size());
100 for (Curve2D curve : this.curves)
101 list.add((ContinuousBoundary2D) curve);
102 return list;
103 }
104
105 public Domain2D getDomain() {
106 return new GenericDomain2D(this);
107 }
108
109 public void fill(Graphics2D g2) {
110 g2.fill(this.getGeneralPath());
111 }
112
113
114
115
116 public double getWindingAngle(java.awt.geom.Point2D point) {
117 double angle = 0;
118 for (OrientedCurve2D curve : this.getCurves())
119 angle += curve.getWindingAngle(point);
120 return angle;
121 }
122
123 public double getSignedDistance(java.awt.geom.Point2D p) {
124 return getSignedDistance(p.getX(), p.getY());
125 }
126
127
128
129
130
131
132 public double getSignedDistance(double x, double y) {
133 double minDist = Double.POSITIVE_INFINITY;
134 double dist = Double.POSITIVE_INFINITY;
135
136 for (OrientedCurve2D curve : this.getCurves()) {
137 dist = Math.min(dist, curve.getSignedDistance(x, y));
138 if (Math.abs(dist)<Math.abs(minDist))
139 minDist = dist;
140 }
141 return minDist;
142 }
143
144 public boolean isInside(java.awt.geom.Point2D point) {
145 return this.getSignedDistance(point.getX(), point.getY())<0;
146 }
147
148
149
150
151 @Override
152 public BoundarySet2D<? extends ContinuousBoundary2D> getReverseCurve() {
153 ContinuousBoundary2D[] curves2 = new ContinuousBoundary2D[curves.size()];
154 int n = curves.size();
155 for (int i = 0; i<n; i++)
156 curves2[i] = (ContinuousBoundary2D)curves.get(n-1-i).getReverseCurve();
157 return new BoundarySet2D<ContinuousBoundary2D>(curves2);
158 }
159
160 @Override
161 public CurveSet2D<? extends ContinuousOrientedCurve2D> getSubCurve(
162 double t0, double t1) {
163
164 CurveSet2D<? extends Curve2D> curveSet = super.getSubCurve(t0, t1);
165
166
167 ArrayList<ContinuousOrientedCurve2D> curves =
168 new ArrayList<ContinuousOrientedCurve2D>();
169 for (Curve2D curve : curveSet.getCurves())
170 curves.add((ContinuousOrientedCurve2D) curve);
171
172
173 return new CurveArray2D<ContinuousOrientedCurve2D>(curves);
174 }
175
176
177
178
179
180
181
182
183
184
185
186 @Override
187 public CurveSet2D<? extends ContinuousOrientedCurve2D> clip(Box2D box) {
188
189 CurveSet2D<? extends Curve2D> set = Curve2DUtils.clipCurve(this, box);
190
191
192 CurveArray2D<ContinuousOrientedCurve2D> result =
193 new CurveArray2D<ContinuousOrientedCurve2D>(set.getCurveNumber());
194
195
196 for (Curve2D curve : set.getCurves()) {
197 if (curve instanceof ContinuousOrientedCurve2D)
198 result.addCurve((ContinuousOrientedCurve2D) curve);
199 }
200 return result;
201 }
202
203 @Override
204 public BoundarySet2D<? extends ContinuousBoundary2D> transform(
205 AffineTransform2D trans) {
206 BoundarySet2D<ContinuousBoundary2D> result =
207 new BoundarySet2D<ContinuousBoundary2D>(curves.size());
208 for (Curve2D curve : curves)
209 result.addCurve((ContinuousBoundary2D) curve.transform(trans));
210 return result;
211 }
212 }