Tesseract 3.02

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
xform2d.cpp
Go to the documentation of this file.
1 /******************************************************************************
2 ** Filename: xform2d.c
3 ** Purpose: Library routines for performing 2D point transformations
4 ** Author: Dan Johnson
5 ** History: Fri Sep 22 09:54:17 1989, DSJ, Created.
6 **
7 ** (c) Copyright Hewlett-Packard Company, 1988.
8 ** Licensed under the Apache License, Version 2.0 (the "License");
9 ** you may not use this file except in compliance with the License.
10 ** You may obtain a copy of the License at
11 ** http://www.apache.org/licenses/LICENSE-2.0
12 ** Unless required by applicable law or agreed to in writing, software
13 ** distributed under the License is distributed on an "AS IS" BASIS,
14 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 ** See the License for the specific language governing permissions and
16 ** limitations under the License.
17 ******************************************************************************/
21 #include "xform2d.h"
22 #include <math.h>
23
28 void InitMatrix(MATRIX_2D *M) {
29 M->a = 1;
30 M->b = 0;
31 M->c = 0;
32 M->d = 1;
33 M->tx = 0;
34 M->ty = 0;
35 }
36
37 void CopyMatrix(MATRIX_2D *A, MATRIX_2D *B) {
38 B->a = A->a;
39 B->b = A->b;
40 B->c = A->c;
41 B->d = A->d;
42 B->tx = A->tx;
43 B->ty = A->ty;
44 }
45
46 void TranslateMatrix(MATRIX_2D *M, FLOAT32 X, FLOAT32 Y) {
47 M->tx += M->a * X + M->c * Y;
48 M->ty += M->b * X + M->d * Y;
49 }
50
51 void ScaleMatrix(MATRIX_2D *M, FLOAT32 X, FLOAT32 Y) {
52 M->a *= X;
53 M->b *= X;
54 M->c *= Y;
55 M->d *= Y;
56 }
57
58 void MirrorMatrixInX(MATRIX_2D *M) {ScaleMatrix(M, -1, 1);}
59 void MirrorMatrixInY(MATRIX_2D *M) {ScaleMatrix(M, 1, -1);}
60 void MirrorMatrixInXY(MATRIX_2D *M) {ScaleMatrix(M, -1, -1);}
61
62 FLOAT32 MapX(MATRIX_2D *M, FLOAT32 X, FLOAT32 Y) {
63 return M->a * (X) + (M)->c * (Y) + (M)->tx;
64 }
65
66 FLOAT32 MapY(MATRIX_2D *M, FLOAT32 X, FLOAT32 Y) {
67 return M->b * X + M->d * Y + M->ty;
68 }
69
70 void MapPoint(MATRIX_2D *M, const FPOINT &A, FPOINT* B) {
71 B->x = MapX(M, A.x, A.y);
72 B->y = MapY(M, A.x, A.y);
73 }
74
75 FLOAT32 MapDx(MATRIX_2D *M, FLOAT32 DX, FLOAT32 DY) {
76 return M->a * DX + M->c * DY;
77 }
78
79 FLOAT32 MapDy(MATRIX_2D *M, FLOAT32 DX, FLOAT32 DY) {
80 return M->b * DX + M->d * DY;
81 }
82
83
84 /*---------------------------------------------------------------------------*/
85 void RotateMatrix(MATRIX_2D_PTR Matrix, FLOAT32 Angle) {
86 /*
87 ** Parameters:
88 ** Matrix transformation matrix to rotate
89 ** Angle angle to rotate matrix
90 ** Globals: none
91 ** Operation:
92 ** Rotate the coordinate system (as specified by Matrix) about
93 ** its origin by Angle radians. In matrix notation the
94 ** effect is as follows:
95 **
96 ** Matrix = R X Matrix
97 **
98 ** where R is the following matrix
99 **
100 ** cos Angle sin Angle 0
101 ** -sin Angle cos Angle 0
102 ** 0 0 1
103 ** Return: none
104 ** Exceptions: none
105 ** History: 7/27/89, DSJ, Create.
106 */
107 FLOAT32 Cos, Sin;
108 FLOAT32 NewA, NewB;
109
110 Cos = cos ((double) Angle);
111 Sin = sin ((double) Angle);
112
113 NewA = Matrix->a * Cos + Matrix->c * Sin;
114 NewB = Matrix->b * Cos + Matrix->d * Sin;
115 Matrix->c = Matrix->a * -Sin + Matrix->c * Cos;
116 Matrix->d = Matrix->b * -Sin + Matrix->d * Cos;
117 Matrix->a = NewA;
118 Matrix->b = NewB;
119
120 } /* RotateMatrix */