001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Denis M. Kishenko
019: * @version $Revision$
020: */package org.apache.harmony.awt.gl;
021:
022: import java.awt.Color;
023: import java.awt.Rectangle;
024: import java.awt.Tools;
025: import java.awt.geom.AffineTransform;
026: import java.awt.geom.PathIterator;
027: import java.awt.image.BufferedImage;
028: import java.awt.image.DataBufferInt;
029: import java.util.ArrayList;
030:
031: import org.apache.harmony.awt.gl.MultiRectArea;
032:
033: public class MultiRectAreaTest extends MultiRectAreaTestCase {
034:
035: static int MAX_ERR_COUNT = 5;
036: static int STEP = 3;
037:
038: MultiRectArea area;
039:
040: OperationTest opIntersect = new OperationTest.Intersect();
041: OperationTest opUnion = new OperationTest.Union();
042: OperationTest opSubtract = new OperationTest.Subtract();
043:
044: static {
045: SERIALIZATION_TEST = false;
046: }
047:
048: public MultiRectAreaTest(String name) {
049: super (name);
050: }
051:
052: @Override
053: protected void setUp() throws Exception {
054: area = new MultiRectArea();
055: }
056:
057: @Override
058: protected void tearDown() throws Exception {
059: area = null;
060: super .tearDown();
061: }
062:
063: void fillRect(int[] buf, int width, Rectangle rect, int inc) {
064: for (int x = rect.x; x < rect.x + rect.width; x++) {
065: for (int y = rect.y; y < rect.y + rect.height; y++) {
066: buf[x + y * width] += inc;
067: }
068: }
069: }
070:
071: void fillMultiRectArea(int[] buf, int width, MultiRectArea area,
072: int inc) {
073: Rectangle[] rect = area.getRectangles();
074: for (Rectangle element : rect) {
075: fillRect(buf, width, element, inc);
076: }
077: }
078:
079: static abstract class OperationTest {
080:
081: static class Intersect extends OperationTest {
082:
083: @Override
084: String getName() {
085: return "Intersect";
086: }
087:
088: @Override
089: MultiRectArea getResult(MultiRectArea mra1,
090: MultiRectArea mra2) {
091: return MultiRectArea.intersect(mra1, mra2);
092: }
093:
094: @Override
095: boolean isValid(int[] buf, int index) {
096: return buf[index] == 0 || buf[index] == 1
097: || buf[index] == 2 || buf[index] == 7;
098: }
099:
100: }
101:
102: static class Union extends OperationTest {
103:
104: @Override
105: String getName() {
106: return "Union";
107: }
108:
109: @Override
110: MultiRectArea getResult(MultiRectArea mra1,
111: MultiRectArea mra2) {
112: return MultiRectArea.union(mra1, mra2);
113: }
114:
115: @Override
116: boolean isValid(int[] buf, int index) {
117: return buf[index] == 0 || buf[index] == 5
118: || buf[index] == 6 || buf[index] == 7;
119: }
120:
121: }
122:
123: static class Subtract extends OperationTest {
124:
125: @Override
126: String getName() {
127: return "Subtract";
128: }
129:
130: @Override
131: MultiRectArea getResult(MultiRectArea mra1,
132: MultiRectArea mra2) {
133: return MultiRectArea.subtract(mra1, mra2);
134: }
135:
136: @Override
137: boolean isValid(int[] buf, int index) {
138: return buf[index] == 0 || buf[index] == 2
139: || buf[index] == 3 || buf[index] == 5;
140: }
141:
142: }
143:
144: abstract String getName();
145:
146: abstract MultiRectArea getResult(MultiRectArea mra1,
147: MultiRectArea mra2);
148:
149: abstract boolean isValid(int[] buf, int index);
150: }
151:
152: void check(OperationTest op, MultiRectArea area1,
153: MultiRectArea area2, String name) {
154:
155: int[] color = new int[] { Color.white.getRGB(), // 0
156: Color.green.getRGB(), // 1
157: Color.blue.getRGB(), // 2
158: Color.yellow.getRGB(), // 3
159: Color.black.getRGB(), // 4
160: Color.red.getRGB(), // 5
161: Color.red.getRGB(), // 6
162: Color.red.getRGB() // 7
163: };
164:
165: Rectangle bounds1 = area1.getBounds();
166: Rectangle bounds2 = area2.getBounds();
167: int width = bounds1.width + bounds2.width + 10;
168: int height = bounds1.height + bounds2.height + 10;
169:
170: area1.translate(-bounds1.x, -bounds1.y);
171: area2.translate(bounds1.width - bounds2.x + 5, bounds1.height
172: - bounds2.y + 5);
173:
174: int bufWidth = width + bounds1.width;
175: int bufHeight = height + bounds1.height;
176:
177: int errCount = 0;
178:
179: outer: for (int j = 0; j < height; j += STEP) {
180: for (int i = 0; i < width; i += STEP) {
181: BufferedImage img = new BufferedImage(bufWidth,
182: bufHeight, BufferedImage.TYPE_INT_RGB);
183: int[] buf = ((DataBufferInt) img.getRaster()
184: .getDataBuffer()).getData();
185:
186: MultiRectArea area3 = op.getResult(area1, area2);
187: fillMultiRectArea(buf, bufWidth, area1, 1);
188: fillMultiRectArea(buf, bufWidth, area2, 2);
189: fillMultiRectArea(buf, bufWidth, area3, 4);
190:
191: boolean error = false;
192:
193: for (int k = 0; k < buf.length; k++) {
194: if (!error) {
195: error = !op.isValid(buf, k);
196: }
197:
198: if (buf[k] > 7) {
199: buf[k] = Color.black.getRGB();
200: } else {
201: buf[k] = color[buf[k]];
202: }
203: }
204:
205: if (error) {
206: errCount++;
207: Tools.BufferedImage.saveIcon(img, outputPath + name
208: + "(" + i + "," + j + ").ico");
209: if (errCount > MAX_ERR_COUNT) {
210: break outer;
211: }
212: }
213:
214: if (i + STEP < width) {
215: area1.translate(STEP, 0);
216: } else {
217: area1.translate(-i, STEP);
218: }
219: }
220: }
221:
222: if (errCount > 0) {
223: fail(op.getName() + " failed");
224: }
225:
226: }
227:
228: void check(OperationTest op, String name1, String name2) {
229: check(op,
230: Tools.MultiRectArea.load(shapePath + name1 + ".rect"),
231: Tools.MultiRectArea.load(shapePath + name2 + ".rect"),
232: op.getName() + "_" + name1 + "VS" + name2);
233: }
234:
235: public void testIntersect() {
236: check(opIntersect, "star", "twist");
237: check(opIntersect, "chess", "star");
238: check(opIntersect, "double", "rect");
239: /*
240: check(
241: new OperationTest.Intersect(),
242: Tools.MultiRectArea.load(shapePath + "star.rect"),
243: Tools.MultiRectArea.load(shapePath + "twist.rect"),
244: "int_startVStwist");
245: check(
246: new OperationTest.Intersect(),
247: Tools.MultiRectArea.load(shapePath + "chess.rect"),
248: Tools.MultiRectArea.load(shapePath + "star.rect"),
249: "int_chessVSstart");
250: */
251: }
252:
253: public void testUnion() {
254: check(opUnion, "star", "twist");
255: check(opUnion, "chess", "star");
256: check(opUnion, "double", "rect");
257: /*
258: check(
259: new OperationTest.Union(),
260: Tools.MultiRectArea.load(shapePath + "star.rect"),
261: Tools.MultiRectArea.load(shapePath + "twist.rect"),
262: "union_startVStwist");
263: check(
264: new OperationTest.Union(),
265: Tools.MultiRectArea.load(shapePath + "chess.rect"),
266: Tools.MultiRectArea.load(shapePath + "star.rect"),
267: "union_chessVSstart");
268: check(
269: new OperationTest.Union(),
270: Tools.MultiRectArea.load(shapePath + "double.rect"),
271: Tools.MultiRectArea.load(shapePath + "rect.rect"),
272: "union_chessVSstart");
273: */
274: }
275:
276: public void testSubtract() {
277: check(opSubtract, "star", "twist");
278: check(opSubtract, "chess", "star");
279: check(opSubtract, "double", "rect");
280: /*
281: check(
282: new OperationTest.Subtract(),
283: Tools.MultiRectArea.load(shapePath + "star.rect"),
284: Tools.MultiRectArea.load(shapePath + "twist.rect"),
285: "sub_startVStwist");
286: check(
287: new OperationTest.Subtract(),
288: Tools.MultiRectArea.load(shapePath + "chess.rect"),
289: Tools.MultiRectArea.load(shapePath + "star.rect"),
290: "sub_chessVSstart");
291: check(
292: new OperationTest.Subtract(),
293: Tools.MultiRectArea.load(shapePath + "double.rect"),
294: Tools.MultiRectArea.load(shapePath + "rect.rect"),
295: "sub_doubleVSrect");
296: */
297: }
298:
299: Rectangle[] createRects(int[][] rect) {
300: Rectangle[] r = new Rectangle[rect.length];
301: for (int i = 0; i < rect.length; i++) {
302: r[i] = new Rectangle(rect[i][0], rect[i][1], rect[i][2]
303: - rect[i][0] + 1, rect[i][3] - rect[i][1] + 1);
304: }
305: return r;
306: }
307:
308: ArrayList<Rectangle> createList(int[][] rect) {
309: ArrayList<Rectangle> r = new ArrayList<Rectangle>();
310: for (int i = 0; i < rect.length; i++) {
311: r.add(i, new Rectangle(rect[i][0], rect[i][1], rect[i][2]
312: - rect[i][0] + 1, rect[i][3] - rect[i][1] + 1));
313: }
314: return r;
315: }
316:
317: public void testCreateIntersected() {
318: int[][] initial = new int[][] { { 2, 2, 4, 3 }, { 4, 3, 5, 4 } };
319: int[][] expected = new int[][] { { 2, 2, 4, 2 },
320: { 2, 3, 3, 3 }, { 4, 3, 5, 4 } };
321: assertEquals(createRects(expected), new MultiRectArea(
322: createRects(initial)).getRectangles());
323: assertEquals(createRects(expected), new MultiRectArea(
324: createList(initial)).getRectangles());
325: }
326:
327: public void testCreateNonintersected() {
328: int[][] initial = new int[][] { { 2, 2, 4, 2 }, { 4, 3, 5, 4 } };
329: assertEquals(createRects(initial), new MultiRectArea(
330: createRects(initial)).getRectangles());
331: assertEquals(createRects(initial), new MultiRectArea(
332: createList(initial)).getRectangles());
333: }
334:
335: public void testIntersectEmpty() {
336: MultiRectArea mra = new MultiRectArea(new Rectangle(10, 10, 30,
337: 40));
338: MultiRectArea empty = new MultiRectArea();
339:
340: assertEquals(empty, MultiRectArea.intersect(mra, empty));
341: assertEquals(empty, MultiRectArea.intersect(empty, mra));
342: assertEquals(empty, MultiRectArea.intersect(mra, null));
343: assertEquals(empty, MultiRectArea.intersect(null, mra));
344:
345: assertEquals(empty, MultiRectArea.intersect(empty, empty));
346: assertEquals(empty, MultiRectArea.intersect(null, empty));
347: assertEquals(empty, MultiRectArea.intersect(empty, null));
348: assertEquals(empty, MultiRectArea.intersect(null, null));
349: }
350:
351: public void testUnionEmpty() {
352: MultiRectArea mra = new MultiRectArea(new Rectangle(10, 10, 30,
353: 40));
354: MultiRectArea empty = new MultiRectArea();
355:
356: assertEquals(mra, MultiRectArea.union(mra, empty));
357: assertEquals(mra, MultiRectArea.union(empty, mra));
358: assertEquals(mra, MultiRectArea.union(mra, null));
359: assertEquals(mra, MultiRectArea.union(null, mra));
360:
361: assertEquals(empty, MultiRectArea.union(empty, empty));
362: assertEquals(empty, MultiRectArea.union(null, empty));
363: assertEquals(empty, MultiRectArea.union(empty, null));
364: assertEquals(empty, MultiRectArea.union(null, null));
365: }
366:
367: public void testSubtractEmpty() {
368: MultiRectArea mra = new MultiRectArea(new Rectangle(10, 10, 30,
369: 40));
370: MultiRectArea empty = new MultiRectArea();
371:
372: assertEquals(mra, MultiRectArea.subtract(mra, empty));
373: assertEquals(empty, MultiRectArea.subtract(empty, mra));
374: assertEquals(mra, MultiRectArea.subtract(mra, null));
375: assertEquals(empty, MultiRectArea.subtract(null, mra));
376:
377: assertEquals(empty, MultiRectArea.subtract(empty, empty));
378: assertEquals(empty, MultiRectArea.subtract(null, empty));
379: assertEquals(empty, MultiRectArea.subtract(empty, null));
380: assertEquals(empty, MultiRectArea.subtract(null, null));
381: }
382:
383: void checkPathIteratorDouble(PathIterator p, double[] values) {
384: checkPathRule(p, PathIterator.WIND_NON_ZERO);
385: for (int i = 0; i < values.length;) {
386: int j = i % 8;
387: if (j == 0) {
388: checkPathMove(p, false, values[i++], values[i++], 0.0);
389: } else if (j < 6) {
390: checkPathLine(p, false, values[i++], values[i++], 0.0);
391: } else {
392: checkPathLine(p, false, values[i++], values[i++], 0.0);
393: checkPathClose(p, i >= values.length);
394: }
395: }
396: checkPathDone(p, true);
397: }
398:
399: void checkPathIteratorFloat(PathIterator p, float[] values) {
400: checkPathRule(p, PathIterator.WIND_NON_ZERO);
401: for (int i = 0; i < values.length;) {
402: int j = i % 8;
403: if (j == 0) {
404: checkPathMove(p, false, values[i++], values[i++], 0.0f);
405: } else if (j < 6) {
406: checkPathLine(p, false, values[i++], values[i++], 0.0f);
407: } else {
408: checkPathLine(p, false, values[i++], values[i++], 0.0f);
409: checkPathClose(p, i >= values.length);
410: }
411: }
412: checkPathDone(p, true);
413: }
414:
415: public void testGetPathIteratorDouble() {
416: MultiRectArea mra = new MultiRectArea();
417: checkPathIteratorDouble(mra.getPathIterator(null),
418: new double[] {});
419:
420: mra = new MultiRectArea(1, 2, 4, 6);
421: checkPathIteratorDouble(mra.getPathIterator(null),
422: new double[] { 1, 2, 4, 2, 4, 6, 1, 6 });
423:
424: mra.addRect(4, 1, 6, 4);
425: checkPathIteratorDouble(mra.getPathIterator(null),
426: new double[] { 1, 2, 4, 2, 4, 6, 1, 6, 4, 1, 6, 1, 6,
427: 4, 4, 4 });
428:
429: checkPathIteratorDouble(mra.getPathIterator(null, 0),
430: new double[] { 1, 2, 4, 2, 4, 6, 1, 6, 4, 1, 6, 1, 6,
431: 4, 4, 4 });
432:
433: checkPathIteratorDouble(mra.getPathIterator(AffineTransform
434: .getTranslateInstance(1, 2)), new double[] { 2, 4, 5,
435: 4, 5, 8, 2, 8, 5, 3, 7, 3, 7, 6, 5, 6 });
436: }
437:
438: public void testGetPathIteratorFloat() {
439: MultiRectArea mra = new MultiRectArea();
440: checkPathIteratorFloat(mra.getPathIterator(null),
441: new float[] {});
442:
443: mra = new MultiRectArea(1, 2, 4, 6);
444: checkPathIteratorFloat(mra.getPathIterator(null), new float[] {
445: 1, 2, 4, 2, 4, 6, 1, 6 });
446:
447: mra.addRect(4, 1, 6, 4);
448: checkPathIteratorFloat(mra.getPathIterator(null), new float[] {
449: 1, 2, 4, 2, 4, 6, 1, 6, 4, 1, 6, 1, 6, 4, 4, 4 });
450: }
451:
452: /*
453:
454: public void testGetPathIteratorFloat() {
455: checkPathIteratorFloat(
456: r.getPathIterator(null),
457: new float[]{1, 2, 4, 2, 4, 6, 1, 6});
458: }
459:
460: public void testGetPathIteratorDoubleFlat() {
461: checkPathIteratorDouble(
462: r.getPathIterator(null, 2),
463: new double[]{1, 2, 4, 2, 4, 6, 1, 6});
464: }
465:
466: public void testGetPathIteratorFloatFlat() {
467: checkPathIteratorFloat(
468: r.getPathIterator(null, 5),
469: new float[]{1, 2, 4, 2, 4, 6, 1, 6});
470: }
471:
472: public void testGetPathIteratorDoubleAffine() {
473: checkPathIteratorDouble(
474: r.getPathIterator(AffineTransform.getTranslateInstance(3, 1)),
475: new double[]{4, 3, 7, 3, 7, 7, 4, 7});
476: }
477:
478: public void testGetPathIteratorFloatAffine() {
479: checkPathIteratorFloat(
480: r.getPathIterator(AffineTransform.getTranslateInstance(3, 1)),
481: new float[]{4, 3, 7, 3, 7, 7, 4, 7});
482: }
483:
484: */
485: public static void main(String[] args) {
486: junit.textui.TestRunner.run(MultiRectAreaTest.class);
487: }
488:
489: }
|