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