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: package java.awt.geom;
018:
019: import java.awt.Point;
020: import java.awt.Rectangle;
021:
022: public class QuadCurve2DTest extends ShapeTestCase {
023:
024: static final double ROOT_DELTA = 0.0001;
025:
026: // Test subdivide()
027: double[][][] curves1 = new double[][][] {
028: // Curve, left subdivision, right subdivision
029: { { 1, 2, 3, 4, 5, 6 }, { 1, 2, 2, 3, 3, 4 },
030: { 3, 4, 4, 5, 5, 6 } },
031: { { 0, 0, 2, 6, 6, 2 }, { 0, 0, 1, 3, 2.5, 3.5 },
032: { 2.5, 3.5, 4, 4, 6, 2 } } };
033:
034: double[][][] curves2 = new double[][][] {
035: // Curve, left subdivision, right subdivision
036: // First number in array means offset
037: { { -2, 8, 1, 2, 3, 4, 5, 6 }, { -1, 1, 2, 2, 3, 3, 4 },
038: { 3, 4, 4, 5, 5, 6 } },
039: { { 0, 0, 2, 6, 6, 2 }, { -3, 5, 6, 0, 0, 1, 3, 2.5, 3.5 },
040: { -2, 8, 9, 2.5, 3.5, 4, 4, 6, 2 } } };
041:
042: // Test solveQuaratic
043: double[][][] equations = new double[][][] {
044: // Equation (c, b, a), number of roots, roots
045: { { 2, 2, 1 }, { 0 } }, { { 4, -4, 1 }, { 1, 2 } },
046: { { 12, -10, 2 }, { 2, 3, 2 } }, { { 6, 2, 0 }, { 1, -3 } } };
047:
048: // Test getFlatness
049: double[][] curves3 = new double[][] {
050: // Curve, flatnnes^2
051: { 1, 2, 3, 4, 5, 6, 0 }, { 1, 1, 3, 4, 5, 1, 9 },
052: { 1, 1, 2, 4, 7, 4, 5 } };
053:
054: // Test getBounds
055: public static double[][][] bounds = new double[][][] {
056: { { 1, 2, 3, 4, 5, 6 }, { 1, 2, 4, 4 } },
057: { { 1, 1, 2, 4, 7, 4 }, { 1, 1, 6, 3 } },
058: { { 1, 1, 3, 4, 5, 1 }, { 1, 1, 4, 3 } } };
059:
060: QuadCurve2D q;
061:
062: public QuadCurve2DTest(String name) {
063: super (name);
064: // filterImage = createFilter("^(quad).*([.]ico)$", "(.*)((affine)|(flat)|(bounds))(.*)");
065: }
066:
067: @Override
068: protected void setUp() throws Exception {
069: super .setUp();
070: q = new QuadCurve2D.Double(1, 2, 3, 4, 5, 6);
071: }
072:
073: @Override
074: protected void tearDown() throws Exception {
075: q = null;
076: super .tearDown();
077: }
078:
079: public void testSetCurve1() {
080: double coords[] = new double[] { 7, 8, 9, 10, 11, 12, 13, 14,
081: 15, 16, 17 };
082: q.setCurve(coords, 0);
083: assertEquals(new QuadCurve2D.Double(7, 8, 9, 10, 11, 12), q,
084: 0.0);
085: q.setCurve(coords, 3);
086: assertEquals(new QuadCurve2D.Double(10, 11, 12, 13, 14, 15), q,
087: 0.0);
088: }
089:
090: public void testSetCurve2() {
091: Point2D[] points = new Point[] { new Point(7, 8),
092: new Point(9, 10), new Point(11, 12), new Point(13, 14),
093: new Point(15, 16) };
094: q.setCurve(points, 0);
095: assertEquals(new QuadCurve2D.Double(7, 8, 9, 10, 11, 12), q,
096: 0.0);
097: q.setCurve(points, 2);
098: assertEquals(new QuadCurve2D.Double(11, 12, 13, 14, 15, 16), q,
099: 0.0);
100: }
101:
102: public void testSetCurve3() {
103: q
104: .setCurve(new Point(7, 8), new Point(9, 10), new Point(
105: 11, 12));
106: assertEquals(new QuadCurve2D.Double(7, 8, 9, 10, 11, 12), q,
107: 0.0);
108: }
109:
110: public void testSetCurve4() {
111: q.setCurve(new QuadCurve2D.Double(7, 8, 9, 10, 11, 12));
112: assertEquals(new QuadCurve2D.Double(7, 8, 9, 10, 11, 12), q,
113: 0.0);
114: }
115:
116: public void testGetFlatnessSq1() {
117: for (double[] element : curves3) {
118: QuadCurve2D curve = new QuadCurve2D.Double();
119: curve.setCurve(element, 0);
120: assertEquals(quadToStr(curve), element[6], curve
121: .getFlatnessSq(), 0.0);
122: }
123: }
124:
125: public void testGetFlatnessSq2() {
126: for (double[] element : curves3) {
127: QuadCurve2D curve = new QuadCurve2D.Double();
128: curve.setCurve(element, 0);
129: assertEquals(quadToStr(curve), element[6], QuadCurve2D
130: .getFlatnessSq(element[0], element[1], element[2],
131: element[3], element[4], element[5]), 0.0);
132: }
133: }
134:
135: public void testGetFlatnessSq3() {
136: for (double[] element : curves3) {
137: QuadCurve2D curve = new QuadCurve2D.Double();
138: curve.setCurve(element, 0);
139: assertEquals(quadToStr(curve), element[6], QuadCurve2D
140: .getFlatnessSq(element, 0), 0.0);
141: }
142: }
143:
144: public void testGetFlatness1() {
145: for (double[] element : curves3) {
146: QuadCurve2D curve = new QuadCurve2D.Double();
147: curve.setCurve(element, 0);
148: assertEquals(quadToStr(curve), Math.sqrt(element[6]), curve
149: .getFlatness(), 0.0);
150: }
151: }
152:
153: public void testGetFlatness2() {
154: for (double[] element : curves3) {
155: QuadCurve2D curve = new QuadCurve2D.Double();
156: curve.setCurve(element, 0);
157: assertEquals(quadToStr(curve), Math.sqrt(element[6]),
158: QuadCurve2D.getFlatness(element[0], element[1],
159: element[2], element[3], element[4],
160: element[5]), 0.0);
161: }
162: }
163:
164: public void testGetFlatness3() {
165: for (double[] element : curves3) {
166: QuadCurve2D curve = new QuadCurve2D.Double();
167: curve.setCurve(element, 0);
168: assertEquals(quadToStr(curve), Math.sqrt(element[6]),
169: QuadCurve2D.getFlatness(element, 0), 0.0);
170: }
171: }
172:
173: public void testSubdivide1() {
174: for (double[][] element : curves1) {
175: QuadCurve2D src1 = new QuadCurve2D.Double();
176: QuadCurve2D left1 = new QuadCurve2D.Double();
177: QuadCurve2D right1 = new QuadCurve2D.Double();
178: src1.setCurve(element[0], 0);
179: left1.setCurve(element[1], 0);
180: right1.setCurve(element[2], 0);
181:
182: QuadCurve2D src2 = new QuadCurve2D.Double();
183: QuadCurve2D left2 = new QuadCurve2D.Double();
184: QuadCurve2D right2 = new QuadCurve2D.Double();
185: src2.setCurve(element[0], 0);
186: QuadCurve2D.subdivide(src2, left2, right2);
187:
188: assertEquals(src1, src2, 0.0);
189: assertEquals(left1, left2, 0.0);
190: assertEquals(right1, right2, 0.0);
191: }
192: }
193:
194: public void testSubdivide2() {
195: for (double[][] element : curves2) {
196: int srcOff = element[0][0] < 0.0 ? (int) -element[0][0] : 0;
197: int leftOff = element[1][0] < 0.0 ? (int) -element[1][0]
198: : 0;
199: int rightOff = element[2][0] < 0.0 ? (int) -element[2][0]
200: : 0;
201:
202: double[] src = new double[10];
203: double[] left = new double[10];
204: double[] right = new double[10];
205:
206: System.arraycopy(src, 0, element[0], 0, element[0].length);
207: System.arraycopy(left, 0, element[1], 0, element[1].length);
208: System
209: .arraycopy(right, 0, element[2], 0,
210: element[2].length);
211:
212: QuadCurve2D.subdivide(src, srcOff, left, leftOff, right,
213: rightOff);
214:
215: assertEquals(element[0], src, element[0].length, 0.0);
216: assertEquals(element[1], left, element[1].length, 0.0);
217: assertEquals(element[2], right, element[2].length, 0.0);
218: }
219: }
220:
221: public void testSolveQuadratic1() {
222: for (double[][] element : equations) {
223: double eqn[] = new double[3];
224: System.arraycopy(element[0], 0, eqn, 0, 3);
225: String seqn = "[" + doubleToStr(eqn[0]) + ","
226: + doubleToStr(eqn[1]) + "," + doubleToStr(eqn[2])
227: + "]";
228: int roots = QuadCurve2D.solveQuadratic(eqn);
229: assertEquals(seqn + " roots count", (int) element[1][0],
230: roots);
231: for (int j = 0; j < roots; j++) {
232: assertEquals(seqn + " root(" + j + ")",
233: element[1][1 + j], eqn[j], ROOT_DELTA);
234: }
235: }
236: }
237:
238: public void testSolveQuadratic2() {
239: for (double[][] element : equations) {
240: double res[] = new double[3];
241: double eqn[] = new double[3];
242: System.arraycopy(element[0], 0, eqn, 0, 3);
243: String seqn = "[" + doubleToStr(eqn[0]) + ","
244: + doubleToStr(eqn[1]) + "," + doubleToStr(eqn[2])
245: + "]";
246: int roots = QuadCurve2D.solveQuadratic(eqn, res);
247: assertEquals(seqn + " roots count", (int) element[1][0],
248: roots);
249: assertEquals(element[0], eqn, 3, 0.0);
250: for (int j = 0; j < roots; j++) {
251: assertEquals(seqn + " root(" + j + ")",
252: element[1][1 + j], res[j], ROOT_DELTA);
253: }
254: }
255: }
256:
257: public void testClone() {
258: assertEquals(q, (QuadCurve2D) q.clone(), 0.0);
259: }
260:
261: public void testGetBounds() {
262: for (double[][] element : bounds) {
263: QuadCurve2D curve = new QuadCurve2D.Double();
264: curve.setCurve(element[0], 0);
265: assertEquals(quadToStr(curve), new Rectangle(
266: (int) element[1][0], (int) element[1][1],
267: (int) element[1][2], (int) element[1][3]), curve
268: .getBounds());
269: }
270: }
271:
272: void checkPathIteratorDouble(PathIterator p, double[] values) {
273: checkPathRule(p, PathIterator.WIND_NON_ZERO);
274: checkPathMove(p, false, values[0], values[1], 0.0);
275: checkPathQuad(p, true, values[2], values[3], values[4],
276: values[5], 0.0);
277: }
278:
279: void checkPathIteratorFloat(PathIterator p, float[] values) {
280: checkPathRule(p, PathIterator.WIND_NON_ZERO);
281: checkPathMove(p, false, values[0], values[1], 0.0f);
282: checkPathQuad(p, true, values[2], values[3], values[4],
283: values[5], 0.0f);
284: }
285:
286: public void testGetPathIteratorDouble() {
287: checkPathIteratorDouble(q.getPathIterator(null), new double[] {
288: 1, 2, 3, 4, 5, 6 });
289: }
290:
291: public void testGetPathIteratorFloat() {
292: checkPathIteratorFloat(q.getPathIterator(null), new float[] {
293: 1, 2, 3, 4, 5, 6 });
294: }
295:
296: public void testGetPathIteratorDoubleFlat() {
297: PathIterator p = new QuadCurve2D.Double(0, 0, 2, 6, 6, 2)
298: .getPathIterator(null, 2);
299: checkPathRule(p, PathIterator.WIND_NON_ZERO);
300: checkPathMove(p, false, 0, 0, 0.0);
301: checkPathLine(p, false, 2.5, 3.5, 0.0);
302: checkPathLine(p, true, 6, 2, 0.0);
303: }
304:
305: public void testGetPathIteratorFloatFlat() {
306: PathIterator p = new QuadCurve2D.Double(0, 0, 2, 6, 6, 2)
307: .getPathIterator(null, 2);
308: checkPathRule(p, PathIterator.WIND_NON_ZERO);
309: checkPathMove(p, false, 0, 0, 0.0f);
310: checkPathLine(p, false, 2.5, 3.5, 0.0f);
311: checkPathLine(p, true, 6, 2, 0.0f);
312: }
313:
314: public void testGetPathIteratorDoubleAffine() {
315: checkPathIteratorDouble(q.getPathIterator(AffineTransform
316: .getTranslateInstance(2, 1)), new double[] { 3, 3, 5,
317: 5, 7, 7 });
318: }
319:
320: public void testGetPathIteratorFloatAffine() {
321: checkPathIteratorFloat(q.getPathIterator(AffineTransform
322: .getTranslateInstance(2, 1)), new float[] { 3, 3, 5, 5,
323: 7, 7 });
324: }
325:
326: public static void main(String[] args) {
327: junit.textui.TestRunner.run(QuadCurve2DTest.class);
328: }
329:
330: }
|