/* * Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/* * @test %W% %E% * @bug 6504874 * @summary This test verifies the operation (and performance) of the * various CAG operations on the internal Region class. * @modules java.desktop/sun.java2d.pipe * @run main RegionOps
*/
long start = System.currentTimeMillis();
RectListImpl rlist[] = new RectListImpl[numregions]; int totalrects = 0; for (int i = 0; i < rlist.length; i++) {
RectListImpl rli = RectListImpl.getInstance(); int numsubrects =
minsubrects + rand.nextInt(maxsubrects - minsubrects + 1); for (int j = 0; j < numsubrects; j++) {
addRectTo(rli);
totalrects++;
}
rlist[i] = rli;
} long end = System.currentTimeMillis();
System.out.println((end-start)+"ms to create "+
rlist.length+" regions containing "+
totalrects+" subrectangles");
start = System.currentTimeMillis(); for (int i = 0; i < rlist.length; i++) {
RectListImpl a = rlist[i];
testTranslate(a); for (int j = i; j < rlist.length; j++) {
RectListImpl b = rlist[j]; if (doUnion) testUnion(a, b); if (doSubtract) testDifference(a, b); if (doIntersect) testIntersection(a, b); if (doXor) testExclusiveOr(a, b);
}
}
end = System.currentTimeMillis();
System.out.println(numops+" ops took "+(end-start)+"ms");
publicstaticvoid testTranslate(RectListImpl a, int dx, int dy, boolean isNonDestructive, int xmin, int xmax, int ymin, int ymax)
{
RectListImpl theTrans = a.getTranslation(dx, dy); numops++; if (skipCheck) return;
RectListImpl unTrans = theTrans.getTranslation(-dx, -dy); if (isNonDestructive) checkEqual(a, unTrans, "Translate"); for (int x = xmin; x < xmax; x++) { for (int y = ymin; y < ymax; y++) { boolean inside = a.contains(x, y); if (theTrans.contains(x+dx, y+dy) != inside) {
error(a, null, "translation failed for "+
dx+", "+dy+" at "+x+", "+y);
}
}
}
}
publicstaticvoid testUnion(RectListImpl a, RectListImpl b) {
RectListImpl aUb = a.getUnion(b); numops++;
RectListImpl bUa = b.getUnion(a); numops++; if (skipCheck) return;
checkEqual(aUb, bUa, "Union");
testUnion(a, b, aUb);
testUnion(a, b, bUa);
}
publicstaticvoid testUnion(RectListImpl a, RectListImpl b,
RectListImpl theUnion)
{ for (int x = MINCOORD; x < MAXCOORD; x++) { for (int y = MINCOORD; y < MAXCOORD; y++) { boolean inside = (a.contains(x, y) || b.contains(x, y)); if (theUnion.contains(x, y) != inside) {
error(a, b, "union failed at "+x+", "+y);
}
}
}
}
publicstaticvoid testDifference(RectListImpl a, RectListImpl b) {
RectListImpl aDb = a.getDifference(b); numops++;
RectListImpl bDa = b.getDifference(a); numops++; if (skipCheck) return; // Note that difference is not commutative so we cannot check equals // checkEqual(a, b, "Difference");
testDifference(a, b, aDb);
testDifference(b, a, bDa);
}
publicstaticvoid testDifference(RectListImpl a, RectListImpl b,
RectListImpl theDifference)
{ for (int x = MINCOORD; x < MAXCOORD; x++) { for (int y = MINCOORD; y < MAXCOORD; y++) { boolean inside = (a.contains(x, y) && !b.contains(x, y)); if (theDifference.contains(x, y) != inside) {
error(a, b, "difference failed at "+x+", "+y);
}
}
}
}
publicstaticvoid testIntersection(RectListImpl a, RectListImpl b) {
RectListImpl aIb = a.getIntersection(b); numops++;
RectListImpl bIa = b.getIntersection(a); numops++; if (skipCheck) return;
checkEqual(aIb, bIa, "Intersection");
testIntersection(a, b, aIb);
testIntersection(a, b, bIa);
}
publicstaticvoid testIntersection(RectListImpl a, RectListImpl b,
RectListImpl theIntersection)
{ for (int x = MINCOORD; x < MAXCOORD; x++) { for (int y = MINCOORD; y < MAXCOORD; y++) { boolean inside = (a.contains(x, y) && b.contains(x, y)); if (theIntersection.contains(x, y) != inside) {
error(a, b, "intersection failed at "+x+", "+y);
}
}
}
}
publicstaticvoid testExclusiveOr(RectListImpl a, RectListImpl b) {
RectListImpl aXb = a.getExclusiveOr(b); numops++;
RectListImpl bXa = b.getExclusiveOr(a); numops++; if (skipCheck) return;
checkEqual(aXb, bXa, "ExclusiveOr");
testExclusiveOr(a, b, aXb);
testExclusiveOr(a, b, bXa);
}
publicstaticvoid testExclusiveOr(RectListImpl a, RectListImpl b,
RectListImpl theExclusiveOr)
{ for (int x = MINCOORD; x < MAXCOORD; x++) { for (int y = MINCOORD; y < MAXCOORD; y++) { boolean inside = (a.contains(x, y) != b.contains(x, y)); if (theExclusiveOr.contains(x, y) != inside) {
error(a, b, "xor failed at "+x+", "+y);
}
}
}
}
publicstaticclass AreaImpl extends RectListImpl {
Area theArea;
public AreaImpl() {
}
public AreaImpl(Area a) {
theArea = a;
}
publicvoid addRect(int lox, int loy, int hix, int hiy) {
Area a2 = new Area(new Rectangle(lox, loy, hix-lox, hiy-loy)); if (theArea == null) {
theArea = a2;
} else {
theArea.add(a2);
}
}
public RectListImpl getTranslation(int dx, int dy) {
AffineTransform at = AffineTransform.getTranslateInstance(dx, dy); returnnew AreaImpl(theArea.createTransformedArea(at));
}
public RectListImpl getIntersection(RectListImpl rli) {
Area a2 = new Area(theArea);
a2.intersect(((AreaImpl) rli).theArea); returnnew AreaImpl(a2);
}
public RectListImpl getExclusiveOr(RectListImpl rli) {
Area a2 = new Area(theArea);
a2.exclusiveOr(((AreaImpl) rli).theArea); returnnew AreaImpl(a2);
}
public RectListImpl getDifference(RectListImpl rli) {
Area a2 = new Area(theArea);
a2.subtract(((AreaImpl) rli).theArea); returnnew AreaImpl(a2);
}
public RectListImpl getUnion(RectListImpl rli) {
Area a2 = new Area(theArea);
a2.add(((AreaImpl) rli).theArea); returnnew AreaImpl(a2);
}
// Used for making sure that 3xMAX translates yields an empty region publicboolean checkTransEmpty() { // Area objects will actually survive 3 MAX translates so just // pretend that it had the intended effect... returntrue;
}
publicboolean contains(int x, int y) { return theArea.contains(x, y);
}
publicint hashCode() { // Area does not override hashCode... return 0;
}
public String toString() { return theArea.toString();
}
}
publicstaticclass RegionImpl extends RectListImpl {
Region theRegion;
public RegionImpl() {
}
public RegionImpl(Region r) {
theRegion = r;
}
publicvoid addRect(int lox, int loy, int hix, int hiy) {
Region r2 = Region.getInstanceXYXY(lox, loy, hix, hiy); if (theRegion == null) {
theRegion = r2;
} else {
theRegion = theRegion.getUnion(r2);
}
}
public RectListImpl getTranslation(int dx, int dy) { returnnew RegionImpl(theRegion.getTranslatedRegion(dx, dy));
}
public RectListImpl getIntersection(RectListImpl rli) {
Region r2 = ((RegionImpl) rli).theRegion;
r2 = theRegion.getIntersection(r2); returnnew RegionImpl(r2);
}
public RectListImpl getExclusiveOr(RectListImpl rli) {
Region r2 = ((RegionImpl) rli).theRegion;
r2 = theRegion.getExclusiveOr(r2); returnnew RegionImpl(r2);
}
public RectListImpl getDifference(RectListImpl rli) {
Region r2 = ((RegionImpl) rli).theRegion;
r2 = theRegion.getDifference(r2); returnnew RegionImpl(r2);
}
public RectListImpl getUnion(RectListImpl rli) {
Region r2 = ((RegionImpl) rli).theRegion;
r2 = theRegion.getUnion(r2); returnnew RegionImpl(r2);
}
// Used for making sure that 3xMAX translates yields an empty region publicboolean checkTransEmpty() { // Region objects should be empty after 3 MAX translates... return theRegion.isEmpty();
}
publicboolean contains(int x, int y) { return theRegion.contains(x, y);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.