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