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