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 "transform.h"
|
---|
37 |
|
---|
38 | void transform_multiply(transform_t *res, const transform_t *left, const transform_t *right)
|
---|
39 | {
|
---|
40 | for (int i = 0; i < 3; ++i) {
|
---|
41 | for (int j = 0; j < 3; ++j) {
|
---|
42 | double comb = 0;
|
---|
43 | for (int k = 0; k < 3; ++k) {
|
---|
44 | comb += left->m[i][k] * right->m[k][j];
|
---|
45 | }
|
---|
46 | res->m[i][j] = comb;
|
---|
47 | }
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | void transform_invert(transform_t *t)
|
---|
52 | {
|
---|
53 | double a = t->m[1][1] * t->m[2][2] - t->m[1][2] * t->m[2][1];
|
---|
54 | double b = t->m[1][2] * t->m[2][0] - t->m[2][2] * t->m[1][0];
|
---|
55 | double c = t->m[1][0] * t->m[2][1] - t->m[1][1] * t->m[2][0];
|
---|
56 | double d = t->m[0][2] * t->m[2][1] - t->m[0][1] * t->m[2][2];
|
---|
57 | double e = t->m[0][0] * t->m[2][2] - t->m[0][2] * t->m[2][0];
|
---|
58 | double f = t->m[2][0] * t->m[0][1] - t->m[0][0] * t->m[2][1];
|
---|
59 | double g = t->m[0][1] * t->m[1][2] - t->m[0][2] * t->m[1][1];
|
---|
60 | double h = t->m[0][2] * t->m[1][0] - t->m[0][0] * t->m[1][2];
|
---|
61 | double k = t->m[0][0] * t->m[1][1] - t->m[0][1] * t->m[1][0];
|
---|
62 |
|
---|
63 | double det = t->m[0][0] * a + t->m[0][1] * b + t->m[0][2] * c;
|
---|
64 | det = 1 / det;
|
---|
65 |
|
---|
66 | t->m[0][0] = a * det; t->m[0][1] = d * det; t->m[0][2] = g * det;
|
---|
67 | t->m[1][0] = b * det; t->m[1][1] = e * det; t->m[1][2] = h * det;
|
---|
68 | t->m[2][0] = c * det; t->m[2][1] = f * det; t->m[2][2] = k * det;
|
---|
69 | }
|
---|
70 |
|
---|
71 | void transform_identity(transform_t *t)
|
---|
72 | {
|
---|
73 | t->m[0][0] = 1; t->m[0][1] = 0; t->m[0][2] = 0;
|
---|
74 | t->m[1][0] = 0; t->m[1][1] = 1; t->m[1][2] = 0;
|
---|
75 | t->m[2][0] = 0; t->m[2][1] = 0; t->m[2][2] = 1;
|
---|
76 | }
|
---|
77 |
|
---|
78 | void transform_translate(transform_t *t, double dx, double dy)
|
---|
79 | {
|
---|
80 | transform_t a;
|
---|
81 | a.m[0][0] = 1; a.m[0][1] = 0; a.m[0][2] = dx;
|
---|
82 | a.m[1][0] = 0; a.m[1][1] = 1; a.m[1][2] = dy;
|
---|
83 | a.m[2][0] = 0; a.m[2][1] = 0; a.m[2][2] = 1;
|
---|
84 |
|
---|
85 | transform_t r;
|
---|
86 | transform_multiply(&r, &a, t);
|
---|
87 | *t = r;
|
---|
88 | }
|
---|
89 |
|
---|
90 | void transform_scale(transform_t *t, double qx, double qy)
|
---|
91 | {
|
---|
92 | transform_t a;
|
---|
93 | a.m[0][0] = qx; a.m[0][1] = 0; a.m[0][2] = 0;
|
---|
94 | a.m[1][0] = 0; a.m[1][1] = qy; a.m[1][2] = 0;
|
---|
95 | a.m[2][0] = 0; a.m[2][1] = 0; a.m[2][2] = 1;
|
---|
96 |
|
---|
97 | transform_t r;
|
---|
98 | transform_multiply(&r, &a, t);
|
---|
99 | *t = r;
|
---|
100 | }
|
---|
101 |
|
---|
102 | void transform_rotate(transform_t *t, double rad)
|
---|
103 | {
|
---|
104 | double s, c;
|
---|
105 |
|
---|
106 | // FIXME: temporary solution until there are trigonometric functions in libc
|
---|
107 |
|
---|
108 | while (rad < 0) {
|
---|
109 | rad += (2 * PI);
|
---|
110 | }
|
---|
111 |
|
---|
112 | while (rad > (2 * PI)) {
|
---|
113 | rad -= (2 * PI);
|
---|
114 | }
|
---|
115 |
|
---|
116 | if (rad >= 0 && rad < (PI / 4)) {
|
---|
117 | s = 0; c = 1;
|
---|
118 | } else if (rad >= (PI / 4) && rad < (3 * PI / 4)) {
|
---|
119 | s = 1; c = 0;
|
---|
120 | } else if (rad >= (3 * PI / 4) && rad < (5 * PI / 4)) {
|
---|
121 | s = 0; c = -1;
|
---|
122 | } else if (rad >= (5 * PI / 4) && rad < (7 * PI / 4)) {
|
---|
123 | s = -1; c = 0;
|
---|
124 | } else {
|
---|
125 | s = 0; c = 1;
|
---|
126 | }
|
---|
127 |
|
---|
128 | transform_t a;
|
---|
129 | a.m[0][0] = c; a.m[0][1] = -s; a.m[0][2] = 0;
|
---|
130 | a.m[1][0] = s; a.m[1][1] = c; a.m[1][2] = 0;
|
---|
131 | a.m[2][0] = 0; a.m[2][1] = 0; a.m[2][2] = 1;
|
---|
132 |
|
---|
133 | transform_t r;
|
---|
134 | transform_multiply(&r, &a, t);
|
---|
135 | *t = r;
|
---|
136 | }
|
---|
137 |
|
---|
138 | bool transform_is_fast(transform_t *t)
|
---|
139 | {
|
---|
140 | return (t->m[0][0] == 1) && (t->m[0][1] == 0)
|
---|
141 | && (t->m[1][0] == 0) && (t->m[1][1] == 1)
|
---|
142 | && ((t->m[0][2] - ((long) t->m[0][2])) == 0.0)
|
---|
143 | && ((t->m[1][2] - ((long) t->m[1][2])) == 0.0);
|
---|
144 | }
|
---|
145 |
|
---|
146 | void transform_apply_linear(const transform_t *t, double *x, double *y)
|
---|
147 | {
|
---|
148 | double x_ = *x;
|
---|
149 | double y_ = *y;
|
---|
150 | *x = x_ * t->m[0][0] + y_ * t->m[0][1];
|
---|
151 | *y = x_ * t->m[1][0] + y_ * t->m[1][1];
|
---|
152 | }
|
---|
153 |
|
---|
154 | void transform_apply_affine(const transform_t *t, double *x, double *y)
|
---|
155 | {
|
---|
156 | double x_ = *x;
|
---|
157 | double y_ = *y;
|
---|
158 | *x = x_ * t->m[0][0] + y_ * t->m[0][1] + t->m[0][2];
|
---|
159 | *y = x_ * t->m[1][0] + y_ * t->m[1][1] + t->m[1][2];
|
---|
160 | }
|
---|
161 |
|
---|
162 | /** @}
|
---|
163 | */
|
---|