Tesseract  3.02
tesseract-ocr/classify/xform2d.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002  ** Filename:    xform2d.c
00003  ** Purpose:     Library routines for performing 2D point transformations
00004  ** Author:      Dan Johnson
00005  ** History:     Fri Sep 22 09:54:17 1989, DSJ, Created.
00006  **
00007  ** (c) Copyright Hewlett-Packard Company, 1988.
00008  ** Licensed under the Apache License, Version 2.0 (the "License");
00009  ** you may not use this file except in compliance with the License.
00010  ** You may obtain a copy of the License at
00011  ** http://www.apache.org/licenses/LICENSE-2.0
00012  ** Unless required by applicable law or agreed to in writing, software
00013  ** distributed under the License is distributed on an "AS IS" BASIS,
00014  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  ** See the License for the specific language governing permissions and
00016  ** limitations under the License.
00017  ******************************************************************************/
00021 #include "xform2d.h"
00022 #include <math.h>
00023 
00028 void InitMatrix(MATRIX_2D *M) {
00029   M->a = 1;
00030   M->b = 0;
00031   M->c = 0;
00032   M->d = 1;
00033   M->tx = 0;
00034   M->ty = 0;
00035 }
00036 
00037 void CopyMatrix(MATRIX_2D *A, MATRIX_2D *B) {
00038   B->a = A->a;
00039   B->b = A->b;
00040   B->c = A->c;
00041   B->d = A->d;
00042   B->tx = A->tx;
00043   B->ty = A->ty;
00044 }
00045 
00046 void TranslateMatrix(MATRIX_2D *M, FLOAT32 X, FLOAT32 Y) {
00047   M->tx += M->a * X + M->c * Y;
00048   M->ty += M->b * X + M->d * Y;
00049 }
00050 
00051 void ScaleMatrix(MATRIX_2D *M, FLOAT32 X, FLOAT32 Y) {
00052   M->a *= X;
00053   M->b *= X;
00054   M->c *= Y;
00055   M->d *= Y;
00056 }
00057 
00058 void MirrorMatrixInX(MATRIX_2D *M)  {ScaleMatrix(M, -1, 1);}
00059 void MirrorMatrixInY(MATRIX_2D *M)  {ScaleMatrix(M, 1, -1);}
00060 void MirrorMatrixInXY(MATRIX_2D *M) {ScaleMatrix(M, -1, -1);}
00061 
00062 FLOAT32 MapX(MATRIX_2D *M, FLOAT32 X, FLOAT32 Y) {
00063   return M->a * (X) + (M)->c * (Y) + (M)->tx;
00064 }
00065 
00066 FLOAT32 MapY(MATRIX_2D *M, FLOAT32 X, FLOAT32 Y) {
00067   return M->b * X + M->d * Y + M->ty;
00068 }
00069 
00070 void MapPoint(MATRIX_2D *M, const FPOINT &A, FPOINT* B) {
00071   B->x = MapX(M, A.x, A.y);
00072   B->y = MapY(M, A.x, A.y);
00073 }
00074 
00075 FLOAT32 MapDx(MATRIX_2D *M, FLOAT32 DX, FLOAT32 DY) {
00076   return M->a * DX + M->c * DY;
00077 }
00078 
00079 FLOAT32 MapDy(MATRIX_2D *M, FLOAT32 DX, FLOAT32 DY) {
00080   return M->b * DX + M->d * DY;
00081 }
00082 
00083 
00084 /*---------------------------------------------------------------------------*/
00085 void RotateMatrix(MATRIX_2D_PTR Matrix, FLOAT32 Angle) {
00086 /*
00087  ** Parameters:
00088  **   Matrix    transformation matrix to rotate
00089  **   Angle   angle to rotate matrix
00090  ** Globals: none
00091  ** Operation:
00092  **   Rotate the coordinate system (as specified by Matrix) about
00093  **   its origin by Angle radians.  In matrix notation the
00094  **   effect is as follows:
00095  **
00096  **     Matrix = R X Matrix
00097  **
00098  **   where R is the following matrix
00099  **
00100  **     cos Angle sin Angle 0
00101  **     -sin Angle  cos Angle 0
00102  **     0   0   1
00103  ** Return: none
00104  ** Exceptions: none
00105  ** History: 7/27/89, DSJ, Create.
00106  */
00107   FLOAT32 Cos, Sin;
00108   FLOAT32 NewA, NewB;
00109 
00110   Cos = cos ((double) Angle);
00111   Sin = sin ((double) Angle);
00112 
00113   NewA = Matrix->a * Cos + Matrix->c * Sin;
00114   NewB = Matrix->b * Cos + Matrix->d * Sin;
00115   Matrix->c = Matrix->a * -Sin + Matrix->c * Cos;
00116   Matrix->d = Matrix->b * -Sin + Matrix->d * Cos;
00117   Matrix->a = NewA;
00118   Matrix->b = NewB;
00119 
00120 }                                /* RotateMatrix */