3551N/A/*
3909N/A * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
3551N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3551N/A *
3551N/A * This code is free software; you can redistribute it and/or modify it
3551N/A * under the terms of the GNU General Public License version 2 only, as
3551N/A * published by the Free Software Foundation.
3551N/A *
3551N/A * This code is distributed in the hope that it will be useful, but WITHOUT
3551N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3551N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3551N/A * version 2 for more details (a copy is included in the LICENSE file that
3551N/A * accompanied this code).
3551N/A *
3551N/A * You should have received a copy of the GNU General Public License version
3551N/A * 2 along with this work; if not, write to the Free Software Foundation,
3551N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3551N/A *
3551N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3551N/A * or visit www.oracle.com if you need additional information or have any
3551N/A * questions.
3551N/A */
3551N/A
1045N/A/*
1045N/A * @test %W% %E%
1045N/A * @bug 6504874
1045N/A * @summary This test verifies the operation (and performance) of the
1045N/A * various CAG operations on the internal Region class.
1045N/A * @run main RegionOps
1045N/A */
1045N/A
1045N/Aimport java.awt.Rectangle;
1045N/Aimport java.awt.geom.Area;
1045N/Aimport java.awt.geom.AffineTransform;
1045N/Aimport java.awt.image.BufferedImage;
1045N/Aimport java.util.Random;
1045N/Aimport sun.java2d.pipe.Region;
1045N/A
1045N/Apublic class RegionOps {
1045N/A public static final int DEFAULT_NUMREGIONS = 50;
1045N/A public static final int DEFAULT_MINSUBRECTS = 1;
1045N/A public static final int DEFAULT_MAXSUBRECTS = 10;
1045N/A
1045N/A public static final int MINCOORD = -20;
1045N/A public static final int MAXCOORD = 20;
1045N/A
1045N/A public static boolean useArea;
1045N/A
1045N/A static int numops;
1045N/A static int numErrors;
1045N/A static Random rand = new Random();
1045N/A static boolean skipCheck;
1045N/A static boolean countErrors;
1045N/A
1045N/A static {
1045N/A // Instantiating BufferedImage initializes sun.java2d
1045N/A BufferedImage bimg =
1045N/A new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
1045N/A }
1045N/A
1045N/A public static void usage(String error) {
1045N/A if (error != null) {
1045N/A System.err.println("Error: "+error);
1045N/A }
1045N/A System.err.println("Usage: java RegionOps "+
1045N/A "[-regions N] [-rects M] "+
1045N/A "[-[min|max]rects M] [-area]");
1045N/A System.err.println(" "+
1045N/A "[-add|union] [-sub|diff] "+
1045N/A "[-int[ersect]] [-xor]");
1045N/A System.err.println(" "+
1045N/A "[-seed S] [-nocheck] [-count[errors]] [-help]");
1045N/A System.exit((error != null) ? 1 : 0);
1045N/A }
1045N/A
1045N/A public static void error(RectListImpl a, RectListImpl b, String problem) {
1045N/A System.err.println("Operating on: "+a);
1045N/A if (b != null) {
1045N/A System.err.println("and: "+b);
1045N/A }
1045N/A if (countErrors) {
1045N/A System.err.println(problem);
1045N/A numErrors++;
1045N/A } else {
1045N/A throw new RuntimeException(problem);
1045N/A }
1045N/A }
1045N/A
1045N/A public static void main(String argv[]) {
1045N/A int numregions = DEFAULT_NUMREGIONS;
1045N/A int minsubrects = DEFAULT_MINSUBRECTS;
1045N/A int maxsubrects = DEFAULT_MAXSUBRECTS;
1045N/A boolean doUnion = false;
1045N/A boolean doIntersect = false;
1045N/A boolean doSubtract = false;
1045N/A boolean doXor = false;
1045N/A
1045N/A for (int i = 0; i < argv.length; i++) {
1045N/A String arg = argv[i];
1045N/A if (arg.equalsIgnoreCase("-regions")) {
1045N/A if (i+1 >= argv.length) {
1045N/A usage("missing arg for -regions");
1045N/A }
1045N/A numregions = Integer.parseInt(argv[++i]);
1045N/A } else if (arg.equalsIgnoreCase("-rects")) {
1045N/A if (i+1 >= argv.length) {
1045N/A usage("missing arg for -rects");
1045N/A }
1045N/A minsubrects = maxsubrects = Integer.parseInt(argv[++i]);
1045N/A } else if (arg.equalsIgnoreCase("-minrects")) {
1045N/A if (i+1 >= argv.length) {
1045N/A usage("missing arg for -minrects");
1045N/A }
1045N/A minsubrects = Integer.parseInt(argv[++i]);
1045N/A } else if (arg.equalsIgnoreCase("-maxrects")) {
1045N/A if (i+1 >= argv.length) {
1045N/A usage("missing arg for -maxrects");
1045N/A }
1045N/A maxsubrects = Integer.parseInt(argv[++i]);
1045N/A } else if (arg.equalsIgnoreCase("-area")) {
1045N/A useArea = true;
1045N/A } else if (arg.equalsIgnoreCase("-add") ||
1045N/A arg.equalsIgnoreCase("-union"))
1045N/A {
1045N/A doUnion = true;
1045N/A } else if (arg.equalsIgnoreCase("-sub") ||
1045N/A arg.equalsIgnoreCase("-diff"))
1045N/A {
1045N/A doSubtract = true;
1045N/A } else if (arg.equalsIgnoreCase("-int") ||
1045N/A arg.equalsIgnoreCase("-intersect"))
1045N/A {
1045N/A doIntersect = true;
1045N/A } else if (arg.equalsIgnoreCase("-xor")) {
1045N/A doXor = true;
1045N/A } else if (arg.equalsIgnoreCase("-seed")) {
1045N/A if (i+1 >= argv.length) {
1045N/A usage("missing arg for -seed");
1045N/A }
1045N/A rand.setSeed(Long.decode(argv[++i]).longValue());
1045N/A } else if (arg.equalsIgnoreCase("-nocheck")) {
1045N/A skipCheck = true;
1045N/A } else if (arg.equalsIgnoreCase("-count") ||
1045N/A arg.equalsIgnoreCase("-counterrors"))
1045N/A {
1045N/A countErrors = true;
1045N/A } else if (arg.equalsIgnoreCase("-help")) {
1045N/A usage(null);
1045N/A } else {
1045N/A usage("Unknown argument: "+arg);
1045N/A }
1045N/A }
1045N/A
1045N/A if (maxsubrects < minsubrects) {
1045N/A usage("maximum number of subrectangles less than minimum");
1045N/A }
1045N/A
1045N/A if (minsubrects <= 0) {
1045N/A usage("minimum number of subrectangles must be positive");
1045N/A }
1045N/A
1045N/A if (!doUnion && !doSubtract && !doIntersect && !doXor) {
1045N/A doUnion = doSubtract = doIntersect = doXor = true;
1045N/A }
1045N/A
1045N/A long start = System.currentTimeMillis();
1045N/A RectListImpl rlist[] = new RectListImpl[numregions];
1045N/A int totalrects = 0;
1045N/A for (int i = 0; i < rlist.length; i++) {
1045N/A RectListImpl rli = RectListImpl.getInstance();
1045N/A int numsubrects =
1045N/A minsubrects + rand.nextInt(maxsubrects - minsubrects + 1);
1045N/A for (int j = 0; j < numsubrects; j++) {
1045N/A addRectTo(rli);
1045N/A totalrects++;
1045N/A }
1045N/A rlist[i] = rli;
1045N/A }
1045N/A long end = System.currentTimeMillis();
1045N/A System.out.println((end-start)+"ms to create "+
1045N/A rlist.length+" regions containing "+
1045N/A totalrects+" subrectangles");
1045N/A
1045N/A start = System.currentTimeMillis();
1045N/A for (int i = 0; i < rlist.length; i++) {
1045N/A RectListImpl a = rlist[i];
1045N/A testTranslate(a);
1045N/A for (int j = i; j < rlist.length; j++) {
1045N/A RectListImpl b = rlist[j];
1045N/A if (doUnion) testUnion(a, b);
1045N/A if (doSubtract) testDifference(a, b);
1045N/A if (doIntersect) testIntersection(a, b);
1045N/A if (doXor) testExclusiveOr(a, b);
1045N/A }
1045N/A }
1045N/A end = System.currentTimeMillis();
1045N/A System.out.println(numops+" ops took "+(end-start)+"ms");
1045N/A
1045N/A if (numErrors > 0) {
1045N/A throw new RuntimeException(numErrors+" errors encountered");
1045N/A }
1045N/A }
1045N/A
1045N/A public static void addRectTo(RectListImpl rli) {
1045N/A int lox = MINCOORD + rand.nextInt(MAXCOORD - MINCOORD + 1);
1045N/A int hix = MINCOORD + rand.nextInt(MAXCOORD - MINCOORD + 1);
1045N/A int loy = MINCOORD + rand.nextInt(MAXCOORD - MINCOORD + 1);
1045N/A int hiy = MINCOORD + rand.nextInt(MAXCOORD - MINCOORD + 1);
1045N/A rli.addRect(lox, loy, hix, hiy);
1045N/A }
1045N/A
1045N/A public static void checkEqual(RectListImpl a, RectListImpl b,
1045N/A String optype)
1045N/A {
1045N/A if (a.hashCode() != b.hashCode()) {
1045N/A error(a, b, "hashcode failed for "+optype);
1045N/A }
1045N/A if (!a.equals(b)) {
1045N/A error(a, b, "equals failed for "+optype);
1045N/A }
1045N/A }
1045N/A
1045N/A public static void testTranslate(RectListImpl a) {
1045N/A RectListImpl maxTrans =
1045N/A a.getTranslation(Integer.MAX_VALUE, Integer.MAX_VALUE)
1045N/A .getTranslation(Integer.MAX_VALUE, Integer.MAX_VALUE)
1045N/A .getTranslation(Integer.MAX_VALUE, Integer.MAX_VALUE);
1045N/A if (!maxTrans.checkTransEmpty()) {
1045N/A error(maxTrans, null, "overflow translated RectList not empty");
1045N/A }
1045N/A RectListImpl minTrans =
1045N/A a.getTranslation(Integer.MIN_VALUE, Integer.MIN_VALUE)
1045N/A .getTranslation(Integer.MIN_VALUE, Integer.MIN_VALUE)
1045N/A .getTranslation(Integer.MIN_VALUE, Integer.MIN_VALUE);
1045N/A if (!minTrans.checkTransEmpty()) {
1045N/A error(minTrans, null, "overflow translated RectList not empty");
1045N/A }
1045N/A testTranslate(a, Integer.MAX_VALUE, Integer.MAX_VALUE, false,
1045N/A MINCOORD, 0, MINCOORD, 0);
1045N/A testTranslate(a, Integer.MAX_VALUE, Integer.MIN_VALUE, false,
1045N/A MINCOORD, 0, 0, MAXCOORD);
1045N/A testTranslate(a, Integer.MIN_VALUE, Integer.MAX_VALUE, false,
1045N/A 0, MAXCOORD, MINCOORD, 0);
1045N/A testTranslate(a, Integer.MIN_VALUE, Integer.MIN_VALUE, false,
1045N/A 0, MAXCOORD, 0, MAXCOORD);
1045N/A for (int dy = -100; dy <= 100; dy += 50) {
1045N/A for (int dx = -100; dx <= 100; dx += 50) {
1045N/A testTranslate(a, dx, dy, true,
1045N/A MINCOORD, MAXCOORD,
1045N/A MINCOORD, MAXCOORD);
1045N/A }
1045N/A }
1045N/A }
1045N/A
1045N/A public static void testTranslate(RectListImpl a, int dx, int dy,
1045N/A boolean isNonDestructive,
1045N/A int xmin, int xmax,
1045N/A int ymin, int ymax)
1045N/A {
1045N/A RectListImpl theTrans = a.getTranslation(dx, dy); numops++;
1045N/A if (skipCheck) return;
1045N/A RectListImpl unTrans = theTrans.getTranslation(-dx, -dy);
1045N/A if (isNonDestructive) checkEqual(a, unTrans, "Translate");
1045N/A for (int x = xmin; x < xmax; x++) {
1045N/A for (int y = ymin; y < ymax; y++) {
1045N/A boolean inside = a.contains(x, y);
1045N/A if (theTrans.contains(x+dx, y+dy) != inside) {
1045N/A error(a, null, "translation failed for "+
1045N/A dx+", "+dy+" at "+x+", "+y);
1045N/A }
1045N/A }
1045N/A }
1045N/A }
1045N/A
1045N/A public static void testUnion(RectListImpl a, RectListImpl b) {
1045N/A RectListImpl aUb = a.getUnion(b); numops++;
1045N/A RectListImpl bUa = b.getUnion(a); numops++;
1045N/A if (skipCheck) return;
1045N/A checkEqual(aUb, bUa, "Union");
1045N/A testUnion(a, b, aUb);
1045N/A testUnion(a, b, bUa);
1045N/A }
1045N/A
1045N/A public static void testUnion(RectListImpl a, RectListImpl b,
1045N/A RectListImpl theUnion)
1045N/A {
1045N/A for (int x = MINCOORD; x < MAXCOORD; x++) {
1045N/A for (int y = MINCOORD; y < MAXCOORD; y++) {
1045N/A boolean inside = (a.contains(x, y) || b.contains(x, y));
1045N/A if (theUnion.contains(x, y) != inside) {
1045N/A error(a, b, "union failed at "+x+", "+y);
1045N/A }
1045N/A }
1045N/A }
1045N/A }
1045N/A
1045N/A public static void testDifference(RectListImpl a, RectListImpl b) {
1045N/A RectListImpl aDb = a.getDifference(b); numops++;
1045N/A RectListImpl bDa = b.getDifference(a); numops++;
1045N/A if (skipCheck) return;
1045N/A // Note that difference is not commutative so we cannot check equals
1045N/A // checkEqual(a, b, "Difference");
1045N/A testDifference(a, b, aDb);
1045N/A testDifference(b, a, bDa);
1045N/A }
1045N/A
1045N/A public static void testDifference(RectListImpl a, RectListImpl b,
1045N/A RectListImpl theDifference)
1045N/A {
1045N/A for (int x = MINCOORD; x < MAXCOORD; x++) {
1045N/A for (int y = MINCOORD; y < MAXCOORD; y++) {
1045N/A boolean inside = (a.contains(x, y) && !b.contains(x, y));
1045N/A if (theDifference.contains(x, y) != inside) {
1045N/A error(a, b, "difference failed at "+x+", "+y);
1045N/A }
1045N/A }
1045N/A }
1045N/A }
1045N/A
1045N/A public static void testIntersection(RectListImpl a, RectListImpl b) {
1045N/A RectListImpl aIb = a.getIntersection(b); numops++;
1045N/A RectListImpl bIa = b.getIntersection(a); numops++;
1045N/A if (skipCheck) return;
1045N/A checkEqual(aIb, bIa, "Intersection");
1045N/A testIntersection(a, b, aIb);
1045N/A testIntersection(a, b, bIa);
1045N/A }
1045N/A
1045N/A public static void testIntersection(RectListImpl a, RectListImpl b,
1045N/A RectListImpl theIntersection)
1045N/A {
1045N/A for (int x = MINCOORD; x < MAXCOORD; x++) {
1045N/A for (int y = MINCOORD; y < MAXCOORD; y++) {
1045N/A boolean inside = (a.contains(x, y) && b.contains(x, y));
1045N/A if (theIntersection.contains(x, y) != inside) {
1045N/A error(a, b, "intersection failed at "+x+", "+y);
1045N/A }
1045N/A }
1045N/A }
1045N/A }
1045N/A
1045N/A public static void testExclusiveOr(RectListImpl a, RectListImpl b) {
1045N/A RectListImpl aXb = a.getExclusiveOr(b); numops++;
1045N/A RectListImpl bXa = b.getExclusiveOr(a); numops++;
1045N/A if (skipCheck) return;
1045N/A checkEqual(aXb, bXa, "ExclusiveOr");
1045N/A testExclusiveOr(a, b, aXb);
1045N/A testExclusiveOr(a, b, bXa);
1045N/A }
1045N/A
1045N/A public static void testExclusiveOr(RectListImpl a, RectListImpl b,
1045N/A RectListImpl theExclusiveOr)
1045N/A {
1045N/A for (int x = MINCOORD; x < MAXCOORD; x++) {
1045N/A for (int y = MINCOORD; y < MAXCOORD; y++) {
1045N/A boolean inside = (a.contains(x, y) != b.contains(x, y));
1045N/A if (theExclusiveOr.contains(x, y) != inside) {
1045N/A error(a, b, "xor failed at "+x+", "+y);
1045N/A }
1045N/A }
1045N/A }
1045N/A }
1045N/A
1045N/A public abstract static class RectListImpl {
1045N/A public static RectListImpl getInstance() {
1045N/A if (useArea) {
1045N/A return new AreaImpl();
1045N/A } else {
1045N/A return new RegionImpl();
1045N/A }
1045N/A }
1045N/A
1045N/A public abstract void addRect(int lox, int loy, int hix, int hiy);
1045N/A
1045N/A public abstract RectListImpl getTranslation(int dx, int dy);
1045N/A
1045N/A public abstract RectListImpl getIntersection(RectListImpl rli);
1045N/A public abstract RectListImpl getExclusiveOr(RectListImpl rli);
1045N/A public abstract RectListImpl getDifference(RectListImpl rli);
1045N/A public abstract RectListImpl getUnion(RectListImpl rli);
1045N/A
1045N/A // Used for making sure that 3xMAX translates yields an empty region
1045N/A public abstract boolean checkTransEmpty();
1045N/A
1045N/A public abstract boolean contains(int x, int y);
1045N/A
1045N/A public abstract int hashCode();
1045N/A public abstract boolean equals(RectListImpl other);
1045N/A }
1045N/A
1045N/A public static class AreaImpl extends RectListImpl {
1045N/A Area theArea;
1045N/A
1045N/A public AreaImpl() {
1045N/A }
1045N/A
1045N/A public AreaImpl(Area a) {
1045N/A theArea = a;
1045N/A }
1045N/A
1045N/A public void addRect(int lox, int loy, int hix, int hiy) {
1045N/A Area a2 = new Area(new Rectangle(lox, loy, hix-lox, hiy-loy));
1045N/A if (theArea == null) {
1045N/A theArea = a2;
1045N/A } else {
1045N/A theArea.add(a2);
1045N/A }
1045N/A }
1045N/A
1045N/A public RectListImpl getTranslation(int dx, int dy) {
1045N/A AffineTransform at = AffineTransform.getTranslateInstance(dx, dy);
1045N/A return new AreaImpl(theArea.createTransformedArea(at));
1045N/A }
1045N/A
1045N/A public RectListImpl getIntersection(RectListImpl rli) {
1045N/A Area a2 = new Area(theArea);
1045N/A a2.intersect(((AreaImpl) rli).theArea);
1045N/A return new AreaImpl(a2);
1045N/A }
1045N/A
1045N/A public RectListImpl getExclusiveOr(RectListImpl rli) {
1045N/A Area a2 = new Area(theArea);
1045N/A a2.exclusiveOr(((AreaImpl) rli).theArea);
1045N/A return new AreaImpl(a2);
1045N/A }
1045N/A
1045N/A public RectListImpl getDifference(RectListImpl rli) {
1045N/A Area a2 = new Area(theArea);
1045N/A a2.subtract(((AreaImpl) rli).theArea);
1045N/A return new AreaImpl(a2);
1045N/A }
1045N/A
1045N/A public RectListImpl getUnion(RectListImpl rli) {
1045N/A Area a2 = new Area(theArea);
1045N/A a2.add(((AreaImpl) rli).theArea);
1045N/A return new AreaImpl(a2);
1045N/A }
1045N/A
1045N/A // Used for making sure that 3xMAX translates yields an empty region
1045N/A public boolean checkTransEmpty() {
1045N/A // Area objects will actually survive 3 MAX translates so just
1045N/A // pretend that it had the intended effect...
1045N/A return true;
1045N/A }
1045N/A
1045N/A public boolean contains(int x, int y) {
1045N/A return theArea.contains(x, y);
1045N/A }
1045N/A
1045N/A public int hashCode() {
1045N/A // Area does not override hashCode...
1045N/A return 0;
1045N/A }
1045N/A
1045N/A public boolean equals(RectListImpl other) {
1045N/A return theArea.equals(((AreaImpl) other).theArea);
1045N/A }
1045N/A
1045N/A public String toString() {
1045N/A return theArea.toString();
1045N/A }
1045N/A }
1045N/A
1045N/A public static class RegionImpl extends RectListImpl {
1045N/A Region theRegion;
1045N/A
1045N/A public RegionImpl() {
1045N/A }
1045N/A
1045N/A public RegionImpl(Region r) {
1045N/A theRegion = r;
1045N/A }
1045N/A
1045N/A public void addRect(int lox, int loy, int hix, int hiy) {
1045N/A Region r2 = Region.getInstanceXYXY(lox, loy, hix, hiy);
1045N/A if (theRegion == null) {
1045N/A theRegion = r2;
1045N/A } else {
1045N/A theRegion = theRegion.getUnion(r2);
1045N/A }
1045N/A }
1045N/A
1045N/A public RectListImpl getTranslation(int dx, int dy) {
1045N/A return new RegionImpl(theRegion.getTranslatedRegion(dx, dy));
1045N/A }
1045N/A
1045N/A public RectListImpl getIntersection(RectListImpl rli) {
1045N/A Region r2 = ((RegionImpl) rli).theRegion;
1045N/A r2 = theRegion.getIntersection(r2);
1045N/A return new RegionImpl(r2);
1045N/A }
1045N/A
1045N/A public RectListImpl getExclusiveOr(RectListImpl rli) {
1045N/A Region r2 = ((RegionImpl) rli).theRegion;
1045N/A r2 = theRegion.getExclusiveOr(r2);
1045N/A return new RegionImpl(r2);
1045N/A }
1045N/A
1045N/A public RectListImpl getDifference(RectListImpl rli) {
1045N/A Region r2 = ((RegionImpl) rli).theRegion;
1045N/A r2 = theRegion.getDifference(r2);
1045N/A return new RegionImpl(r2);
1045N/A }
1045N/A
1045N/A public RectListImpl getUnion(RectListImpl rli) {
1045N/A Region r2 = ((RegionImpl) rli).theRegion;
1045N/A r2 = theRegion.getUnion(r2);
1045N/A return new RegionImpl(r2);
1045N/A }
1045N/A
1045N/A // Used for making sure that 3xMAX translates yields an empty region
1045N/A public boolean checkTransEmpty() {
1045N/A // Region objects should be empty after 3 MAX translates...
1045N/A return theRegion.isEmpty();
1045N/A }
1045N/A
1045N/A public boolean contains(int x, int y) {
1045N/A return theRegion.contains(x, y);
1045N/A }
1045N/A
1045N/A public int hashCode() {
1045N/A return theRegion.hashCode();
1045N/A }
1045N/A
1045N/A public boolean equals(RectListImpl other) {
1045N/A return theRegion.equals(((RegionImpl) other).theRegion);
1045N/A }
1045N/A
1045N/A public String toString() {
1045N/A return theRegion.toString();
1045N/A }
1045N/A }
1045N/A}