1ec4b1923b059479923f0247e70c4e92eecc4bb7
[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 junit.framework.TestCase;
20
21 /**
22  * @author Sean Owen
23  */
24 public final class PerspectiveTransformTestCase extends TestCase {
25
26   private static final float EPSILON = 0.0001f;
27
28   public void testSquareToQuadrilateral() {
29     PerspectiveTransform pt = PerspectiveTransform.squareToQuadrilateral(
30         2.0f, 3.0f, 10.0f, 4.0f, 16.0f, 15.0f, 4.0f, 9.0f);
31     assertPointEquals(2.0f, 3.0f, 0.0f, 0.0f, pt);
32     assertPointEquals(10.0f, 4.0f, 1.0f, 0.0f, pt);
33     assertPointEquals(4.0f, 9.0f, 0.0f, 1.0f, pt);
34     assertPointEquals(16.0f, 15.0f, 1.0f, 1.0f, pt);
35     assertPointEquals(6.535211f, 6.8873234f, 0.5f, 0.5f, pt);
36     assertPointEquals(48.0f, 42.42857f, 1.5f, 1.5f, pt);
37   }
38
39   public void testQuadrilateralToQuadrilateral() {
40     PerspectiveTransform pt = PerspectiveTransform.quadrilateralToQuadrilateral(
41         2.0f, 3.0f, 10.0f, 4.0f, 16.0f, 15.0f, 4.0f, 9.0f,
42         103.0f, 110.0f, 300.0f, 120.0f, 290.0f, 270.0f, 150.0f, 280.0f);
43     assertPointEquals(103.0f, 110.0f, 2.0f, 3.0f, pt);
44     assertPointEquals(300.0f, 120.0f, 10.0f, 4.0f, pt);
45     assertPointEquals(290.0f, 270.0f, 16.0f, 15.0f, pt);
46     assertPointEquals(150.0f, 280.0f, 4.0f, 9.0f, pt);
47     assertPointEquals(7.1516876f, -64.60185f, 0.5f, 0.5f, pt);
48     assertPointEquals(328.09116f, 334.16385f, 50.0f, 50.0f, pt);
49   }
50
51   private static void assertPointEquals(float expectedX, float expectedY, float sourceX, float sourceY, PerspectiveTransform pt) {
52     float[] points = {sourceX, sourceY};
53     pt.transformPoints(points);
54     assertEquals(expectedX, points[0], EPSILON);
55     assertEquals(expectedY, points[1], EPSILON);
56   }
57
58 }