50344
|
1 |
/*
|
|
2 |
* Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
* @test
|
|
26 |
* @bug 4276812
|
|
27 |
* @summary Verifies that the Area.getBounds2D() method returns a very tight
|
|
28 |
* bounding box
|
|
29 |
*/
|
|
30 |
|
|
31 |
import java.awt.geom.Area;
|
|
32 |
import java.awt.geom.CubicCurve2D;
|
|
33 |
import java.awt.geom.GeneralPath;
|
|
34 |
import java.awt.geom.Point2D;
|
|
35 |
import java.awt.geom.QuadCurve2D;
|
|
36 |
import java.awt.geom.Rectangle2D;
|
|
37 |
|
|
38 |
public class TightBBox {
|
|
39 |
public static void main(String argv[]) {
|
|
40 |
Point2D points[] = {
|
|
41 |
new Point2D.Double(0.0, 0.0),
|
|
42 |
new Point2D.Double(1.0, 0.0),
|
|
43 |
new Point2D.Double(1.0, 1.0),
|
|
44 |
new Point2D.Double(0.0, 1.0),
|
|
45 |
};
|
|
46 |
for (int i = 0; i < 4; i++) {
|
|
47 |
testCubic(points);
|
|
48 |
testQuad(points);
|
|
49 |
testLines(points);
|
|
50 |
rotate(points);
|
|
51 |
}
|
|
52 |
}
|
|
53 |
|
|
54 |
public static void testCubic(Point2D points[]) {
|
|
55 |
CubicCurve2D cubic =
|
|
56 |
new CubicCurve2D.Double(points[0].getX(), points[0].getY(),
|
|
57 |
points[1].getX(), points[1].getY(),
|
|
58 |
points[2].getX(), points[2].getY(),
|
|
59 |
points[3].getX(), points[3].getY());
|
|
60 |
Area a = new Area(cubic);
|
|
61 |
if (!cubic.getBounds2D().contains(a.getBounds2D())) {
|
|
62 |
throw new RuntimeException("Area bbox is larger than cubic");
|
|
63 |
}
|
|
64 |
double x = (points[0].getX() +
|
|
65 |
(points[1].getX() + points[2].getX()) * 3.0 +
|
|
66 |
points[3].getX()) / 8.0;
|
|
67 |
double y = (points[0].getY() +
|
|
68 |
(points[1].getY() + points[2].getY()) * 3.0 +
|
|
69 |
points[3].getY()) / 8.0;
|
|
70 |
Rectangle2D r =
|
|
71 |
new Rectangle2D.Double(points[0].getX(), points[0].getY(), 0, 0);
|
|
72 |
r.add(points[3].getX(), points[3].getY());
|
|
73 |
r.add(x, y);
|
|
74 |
checkBox(r, a.getBounds2D());
|
|
75 |
}
|
|
76 |
|
|
77 |
public static void testQuad(Point2D points[]) {
|
|
78 |
QuadCurve2D quad =
|
|
79 |
new QuadCurve2D.Double(points[0].getX(), points[0].getY(),
|
|
80 |
(points[1].getX() + points[2].getX()) / 2.0,
|
|
81 |
(points[1].getY() + points[2].getY()) / 2.0,
|
|
82 |
points[3].getX(), points[3].getY());
|
|
83 |
Area a = new Area(quad);
|
|
84 |
if (!quad.getBounds2D().contains(a.getBounds2D())) {
|
|
85 |
throw new RuntimeException("Area bbox is larger than quad");
|
|
86 |
}
|
|
87 |
// p0 + 2cp + p1 == p0 + 2(cp0+cp1)/2 + p1 == p0 + cp0 + cp1 + p1
|
|
88 |
double x = (points[0].getX() +
|
|
89 |
points[1].getX() + points[2].getX() +
|
|
90 |
points[3].getX()) / 4.0;
|
|
91 |
double y = (points[0].getY() +
|
|
92 |
points[1].getY() + points[2].getY() +
|
|
93 |
points[3].getY()) / 4.0;
|
|
94 |
Rectangle2D r =
|
|
95 |
new Rectangle2D.Double(points[0].getX(), points[0].getY(), 0, 0);
|
|
96 |
r.add(points[3].getX(), points[3].getY());
|
|
97 |
r.add(x, y);
|
|
98 |
checkBox(r, a.getBounds2D());
|
|
99 |
}
|
|
100 |
|
|
101 |
public static void testLines(Point2D points[]) {
|
|
102 |
GeneralPath gp = new GeneralPath();
|
|
103 |
gp.moveTo((float) points[0].getX(), (float) points[0].getY());
|
|
104 |
gp.lineTo((float) points[2].getX(), (float) points[2].getY());
|
|
105 |
gp.lineTo((float) points[1].getX(), (float) points[1].getY());
|
|
106 |
gp.lineTo((float) points[3].getX(), (float) points[3].getY());
|
|
107 |
gp.closePath();
|
|
108 |
Area a = new Area(gp);
|
|
109 |
if (!gp.getBounds2D().contains(a.getBounds2D())) {
|
|
110 |
throw new RuntimeException("Area bbox is larger than poly");
|
|
111 |
}
|
|
112 |
Rectangle2D r =
|
|
113 |
new Rectangle2D.Double(points[3].getX(), points[3].getY(), 0, 0);
|
|
114 |
for (int i = 0; i < 3; i++) {
|
|
115 |
r.add(points[i].getX(), points[i].getY());
|
|
116 |
}
|
|
117 |
checkBox(r, a.getBounds2D());
|
|
118 |
}
|
|
119 |
|
|
120 |
public static void checkBox(Rectangle2D r1, Rectangle2D r2) {
|
|
121 |
checkVal(r1.getMinX(), r2.getMinX());
|
|
122 |
checkVal(r1.getMinY(), r2.getMinY());
|
|
123 |
checkVal(r2.getMaxX(), r1.getMaxX());
|
|
124 |
checkVal(r2.getMaxY(), r1.getMaxY());
|
|
125 |
}
|
|
126 |
|
|
127 |
/*
|
|
128 |
* v1 and v2 should be roughly equal. The amount of computation
|
|
129 |
* involved in calculating bounding boxes is complex enough that
|
|
130 |
* you might expect the values to be slightly off and this should
|
|
131 |
* perhaps be a loose comparison, but our runtime seems to produce
|
|
132 |
* values that are exactly equal (even with double arithmetic),
|
|
133 |
* so this test will enforce exact equality for now.
|
|
134 |
*/
|
|
135 |
public static void checkVal(double v1, double v2) {
|
|
136 |
if (v1 != v2) {
|
|
137 |
throw new RuntimeException("bounding box not minimal");
|
|
138 |
}
|
|
139 |
}
|
|
140 |
|
|
141 |
public static void rotate(Point2D[] points) {
|
|
142 |
Point2D p = points[0];
|
|
143 |
System.arraycopy(points, 1, points, 0, 3);
|
|
144 |
points[3] = p;
|
|
145 |
}
|
|
146 |
}
|