At last update to JUnit 4.x
[zxing.git] / core / test / src / com / google / zxing / common / PerspectiveTransformTestCase.java
1 /*
2  * Copyright 2007 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing.common;
18
19 import org.junit.Assert;
20 import org.junit.Test;
21
22 /**
23  * @author Sean Owen
24  */
25 public final class PerspectiveTransformTestCase extends Assert {
26
27   private static final float EPSILON = 0.0001f;
28
29   @Test
30   public void testSquareToQuadrilateral() {
31     PerspectiveTransform pt = PerspectiveTransform.squareToQuadrilateral(
32         2.0f, 3.0f, 10.0f, 4.0f, 16.0f, 15.0f, 4.0f, 9.0f);
33     assertPointEquals(2.0f, 3.0f, 0.0f, 0.0f, pt);
34     assertPointEquals(10.0f, 4.0f, 1.0f, 0.0f, pt);
35     assertPointEquals(4.0f, 9.0f, 0.0f, 1.0f, pt);
36     assertPointEquals(16.0f, 15.0f, 1.0f, 1.0f, pt);
37     assertPointEquals(6.535211f, 6.8873234f, 0.5f, 0.5f, pt);
38     assertPointEquals(48.0f, 42.42857f, 1.5f, 1.5f, pt);
39   }
40
41   @Test
42   public void testQuadrilateralToQuadrilateral() {
43     PerspectiveTransform pt = PerspectiveTransform.quadrilateralToQuadrilateral(
44         2.0f, 3.0f, 10.0f, 4.0f, 16.0f, 15.0f, 4.0f, 9.0f,
45         103.0f, 110.0f, 300.0f, 120.0f, 290.0f, 270.0f, 150.0f, 280.0f);
46     assertPointEquals(103.0f, 110.0f, 2.0f, 3.0f, pt);
47     assertPointEquals(300.0f, 120.0f, 10.0f, 4.0f, pt);
48     assertPointEquals(290.0f, 270.0f, 16.0f, 15.0f, pt);
49     assertPointEquals(150.0f, 280.0f, 4.0f, 9.0f, pt);
50     assertPointEquals(7.1516876f, -64.60185f, 0.5f, 0.5f, pt);
51     assertPointEquals(328.09116f, 334.16385f, 50.0f, 50.0f, pt);
52   }
53
54   private static void assertPointEquals(float expectedX,
55                                         float expectedY,
56                                         float sourceX,
57                                         float sourceY,
58                                         PerspectiveTransform pt) {
59     float[] points = {sourceX, sourceY};
60     pt.transformPoints(points);
61     assertEquals(expectedX, points[0], EPSILON);
62     assertEquals(expectedY, points[1], EPSILON);
63   }
64
65 }