Changes in uspace/lib/softrend/transform.c [071fefec:7dba813] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/softrend/transform.c
r071fefec r7dba813 34 34 */ 35 35 36 #include <math.h>37 36 #include "transform.h" 38 37 39 void transform_product(transform_t *res, const transform_t *a, 40 const transform_t *b) 38 void transform_multiply(transform_t *res, const transform_t *left, const transform_t *right) 41 39 { 42 for ( unsigned int i = 0; i < TRANSFORM_MATRIX_DIM; i++) {43 for ( unsigned int j = 0; j < TRANSFORM_MATRIX_DIM; j++) {40 for (int i = 0; i < 3; ++i) { 41 for (int j = 0; j < 3; ++j) { 44 42 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; 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; 50 47 } 51 48 } 52 49 } 53 50 54 void transform_invert(transform_t *t rans)51 void transform_invert(transform_t *t) 55 52 { 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; 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; 89 69 } 90 70 91 void transform_identity(transform_t *t rans)71 void transform_identity(transform_t *t) 92 72 { 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; 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; 104 76 } 105 77 106 void transform_translate(transform_t *t rans, double dx, double dy)78 void transform_translate(transform_t *t, double dx, double dy) 107 79 { 108 80 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); 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; 125 88 } 126 89 127 void transform_scale(transform_t *t rans, double qx, double qy)90 void transform_scale(transform_t *t, double qx, double qy) 128 91 { 129 92 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); 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; 146 100 } 147 101 148 void transform_rotate(transform_t *t rans, double angle)102 void transform_rotate(transform_t *t, double rad) 149 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 150 128 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); 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; 167 136 } 168 137 169 bool transform_is_fast(transform_t *t rans)138 bool transform_is_fast(transform_t *t) 170 139 { 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));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); 175 144 } 176 145 177 void transform_apply_linear(const transform_t *t rans, double *x, double *y)146 void transform_apply_linear(const transform_t *t, double *x, double *y) 178 147 { 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]; 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]; 184 152 } 185 153 186 void transform_apply_affine(const transform_t *t rans, double *x, double *y)154 void transform_apply_affine(const transform_t *t, double *x, double *y) 187 155 { 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]; 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]; 195 160 } 196 161
Note:
See TracChangeset
for help on using the changeset viewer.