1 | /*
|
---|
2 | * Copyright (c) 2011 Petr Koupy
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup softrend
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <math.h>
|
---|
37 | #include "transform.h"
|
---|
38 |
|
---|
39 | void transform_product(transform_t *res, const transform_t *a,
|
---|
40 | const transform_t *b)
|
---|
41 | {
|
---|
42 | for (unsigned int i = 0; i < TRANSFORM_MATRIX_DIM; i++) {
|
---|
43 | for (unsigned int j = 0; j < TRANSFORM_MATRIX_DIM; j++) {
|
---|
44 | double comb = 0;
|
---|
45 |
|
---|
46 | for (unsigned int k = 0; k < TRANSFORM_MATRIX_DIM; k++)
|
---|
47 | comb += a->matrix[i][k] * b->matrix[k][j];
|
---|
48 |
|
---|
49 | res->matrix[i][j] = comb;
|
---|
50 | }
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | void transform_invert(transform_t *trans)
|
---|
55 | {
|
---|
56 | double a = trans->matrix[1][1] * trans->matrix[2][2] -
|
---|
57 | trans->matrix[1][2] * trans->matrix[2][1];
|
---|
58 | double b = trans->matrix[1][2] * trans->matrix[2][0] -
|
---|
59 | trans->matrix[2][2] * trans->matrix[1][0];
|
---|
60 | double c = trans->matrix[1][0] * trans->matrix[2][1] -
|
---|
61 | trans->matrix[1][1] * trans->matrix[2][0];
|
---|
62 | double d = trans->matrix[0][2] * trans->matrix[2][1] -
|
---|
63 | trans->matrix[0][1] * trans->matrix[2][2];
|
---|
64 | double e = trans->matrix[0][0] * trans->matrix[2][2] -
|
---|
65 | trans->matrix[0][2] * trans->matrix[2][0];
|
---|
66 | double f = trans->matrix[2][0] * trans->matrix[0][1] -
|
---|
67 | trans->matrix[0][0] * trans->matrix[2][1];
|
---|
68 | double g = trans->matrix[0][1] * trans->matrix[1][2] -
|
---|
69 | trans->matrix[0][2] * trans->matrix[1][1];
|
---|
70 | double h = trans->matrix[0][2] * trans->matrix[1][0] -
|
---|
71 | trans->matrix[0][0] * trans->matrix[1][2];
|
---|
72 | double k = trans->matrix[0][0] * trans->matrix[1][1] -
|
---|
73 | trans->matrix[0][1] * trans->matrix[1][0];
|
---|
74 |
|
---|
75 | double det = 1 / (a * trans->matrix[0][0] + b * trans->matrix[0][1] +
|
---|
76 | c * trans->matrix[0][2]);
|
---|
77 |
|
---|
78 | trans->matrix[0][0] = a * det;
|
---|
79 | trans->matrix[1][0] = b * det;
|
---|
80 | trans->matrix[2][0] = c * det;
|
---|
81 |
|
---|
82 | trans->matrix[0][1] = d * det;
|
---|
83 | trans->matrix[1][1] = e * det;
|
---|
84 | trans->matrix[2][1] = f * det;
|
---|
85 |
|
---|
86 | trans->matrix[0][2] = g * det;
|
---|
87 | trans->matrix[1][2] = h * det;
|
---|
88 | trans->matrix[2][2] = k * det;
|
---|
89 | }
|
---|
90 |
|
---|
91 | void transform_identity(transform_t *trans)
|
---|
92 | {
|
---|
93 | trans->matrix[0][0] = 1;
|
---|
94 | trans->matrix[1][0] = 0;
|
---|
95 | trans->matrix[2][0] = 0;
|
---|
96 |
|
---|
97 | trans->matrix[0][1] = 0;
|
---|
98 | trans->matrix[1][1] = 1;
|
---|
99 | trans->matrix[2][1] = 0;
|
---|
100 |
|
---|
101 | trans->matrix[0][2] = 0;
|
---|
102 | trans->matrix[1][2] = 0;
|
---|
103 | trans->matrix[2][2] = 1;
|
---|
104 | }
|
---|
105 |
|
---|
106 | void transform_translate(transform_t *trans, double dx, double dy)
|
---|
107 | {
|
---|
108 | transform_t a;
|
---|
109 |
|
---|
110 | a.matrix[0][0] = 1;
|
---|
111 | a.matrix[1][0] = 0;
|
---|
112 | a.matrix[2][0] = 0;
|
---|
113 |
|
---|
114 | a.matrix[0][1] = 0;
|
---|
115 | a.matrix[1][1] = 1;
|
---|
116 | a.matrix[2][1] = 0;
|
---|
117 |
|
---|
118 | a.matrix[0][2] = dx;
|
---|
119 | a.matrix[1][2] = dy;
|
---|
120 | a.matrix[2][2] = 1;
|
---|
121 |
|
---|
122 | transform_t b = *trans;
|
---|
123 |
|
---|
124 | transform_product(trans, &a, &b);
|
---|
125 | }
|
---|
126 |
|
---|
127 | void transform_scale(transform_t *trans, double qx, double qy)
|
---|
128 | {
|
---|
129 | transform_t a;
|
---|
130 |
|
---|
131 | a.matrix[0][0] = qx;
|
---|
132 | a.matrix[1][0] = 0;
|
---|
133 | a.matrix[2][0] = 0;
|
---|
134 |
|
---|
135 | a.matrix[0][1] = 0;
|
---|
136 | a.matrix[1][1] = qy;
|
---|
137 | a.matrix[2][1] = 0;
|
---|
138 |
|
---|
139 | a.matrix[0][2] = 0;
|
---|
140 | a.matrix[1][2] = 0;
|
---|
141 | a.matrix[2][2] = 1;
|
---|
142 |
|
---|
143 | transform_t b = *trans;
|
---|
144 |
|
---|
145 | transform_product(trans, &a, &b);
|
---|
146 | }
|
---|
147 |
|
---|
148 | void transform_rotate(transform_t *trans, double angle)
|
---|
149 | {
|
---|
150 | transform_t a;
|
---|
151 |
|
---|
152 | a.matrix[0][0] = cos(angle);
|
---|
153 | a.matrix[1][0] = sin(angle);
|
---|
154 | a.matrix[2][0] = 0;
|
---|
155 |
|
---|
156 | a.matrix[0][1] = -sin(angle);
|
---|
157 | a.matrix[1][1] = cos(angle);
|
---|
158 | a.matrix[2][1] = 0;
|
---|
159 |
|
---|
160 | a.matrix[0][2] = 0;
|
---|
161 | a.matrix[1][2] = 0;
|
---|
162 | a.matrix[2][2] = 1;
|
---|
163 |
|
---|
164 | transform_t b = *trans;
|
---|
165 |
|
---|
166 | transform_product(trans, &a, &b);
|
---|
167 | }
|
---|
168 |
|
---|
169 | bool transform_is_fast(transform_t *trans)
|
---|
170 | {
|
---|
171 | return ((trans->matrix[0][0] == 1) && (trans->matrix[0][1] == 0) &&
|
---|
172 | (trans->matrix[1][0] == 0) && (trans->matrix[1][1] == 1) &&
|
---|
173 | ((trans->matrix[0][2] - trunc(trans->matrix[0][2])) == 0.0) &&
|
---|
174 | ((trans->matrix[1][2] - trunc(trans->matrix[1][2])) == 0.0));
|
---|
175 | }
|
---|
176 |
|
---|
177 | void transform_apply_linear(const transform_t *trans, double *x, double *y)
|
---|
178 | {
|
---|
179 | double old_x = *x;
|
---|
180 | double old_y = *y;
|
---|
181 |
|
---|
182 | *x = old_x * trans->matrix[0][0] + old_y * trans->matrix[0][1];
|
---|
183 | *y = old_x * trans->matrix[1][0] + old_y * trans->matrix[1][1];
|
---|
184 | }
|
---|
185 |
|
---|
186 | void transform_apply_affine(const transform_t *trans, double *x, double *y)
|
---|
187 | {
|
---|
188 | double old_x = *x;
|
---|
189 | double old_y = *y;
|
---|
190 |
|
---|
191 | *x = old_x * trans->matrix[0][0] + old_y * trans->matrix[0][1] +
|
---|
192 | trans->matrix[0][2];
|
---|
193 | *y = old_x * trans->matrix[1][0] + old_y * trans->matrix[1][1] +
|
---|
194 | trans->matrix[1][2];
|
---|
195 | }
|
---|
196 |
|
---|
197 | /** @}
|
---|
198 | */
|
---|