2 * Copyright 2007 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.common;
19 import org.junit.Assert;
20 import org.junit.Test;
25 public final class PerspectiveTransformTestCase extends Assert {
27 private static final float EPSILON = 0.0001f;
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);
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);
54 private static void assertPointEquals(float expectedX,
58 PerspectiveTransform pt) {
59 float[] points = {sourceX, sourceY};
60 pt.transformPoints(points);
61 assertEquals(expectedX, points[0], EPSILON);
62 assertEquals(expectedY, points[1], EPSILON);