Mercurial > MadButterfly
comparison src/coord.c @ 2:31402929c587
Transform position
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Wed, 23 Jul 2008 16:07:37 +0800 |
parents | b5c0162ccf69 |
children | 164162781a7a |
comparison
equal
deleted
inserted
replaced
1:b5c0162ccf69 | 2:31402929c587 |
---|---|
1 /*! \brief Implement coordination tranform mechanism. | |
2 * \file | |
3 * This file implements coordination transforming for containers. | |
4 */ | |
1 #include <stdio.h> | 5 #include <stdio.h> |
2 #include <string.h> | 6 #include <string.h> |
3 | 7 |
8 typedef float co_aix; | |
9 /*! \brief A coordination system. | |
10 * | |
11 * It have a transform function defined by matrix to transform | |
12 * coordination from source space to target space. | |
13 * Source space is where the contained is drawed, and target space | |
14 * is where the coordination of parent container of the element | |
15 * represented by this coord object. | |
16 */ | |
4 typedef struct coord { | 17 typedef struct coord { |
5 int seq; | 18 int seq; |
6 float matrix[6]; | 19 co_aix matrix[6]; |
7 float aggr_matrix[6]; | 20 co_aix aggr_matrix[6]; |
8 struct coord *parent; | 21 struct coord *parent; |
9 struct coord *children, *sibling; | 22 struct coord *children, *sibling; |
10 } coord_t; | 23 } coord_t; |
11 | 24 |
12 static void mul_matrix(float *m1, float *m2, float *dst) { | 25 #define MUL(a, b) ((a) * (b)) |
13 dst[0] = m1[0] * m2[0] + m1[1] * m2[3]; | 26 #define ADD(a, b) ((a) + (b)) |
14 dst[1] = m1[0] * m2[1] + m1[1] * m2[4]; | 27 #define DIV(a, b) ((a) / (b)) |
15 dst[2] = m1[0] * m2[2] + m1[1] * m2[5] + m1[2]; | 28 #define SUB(a, b) ((a) - (b)) |
16 dst[3] = m1[3] * m2[0] + m1[4] * m2[3]; | 29 |
17 dst[4] = m1[3] * m2[1] + m1[4] * m2[4]; | 30 static void mul_matrix(co_aix *m1, co_aix *m2, co_aix *dst) { |
18 dst[5] = m1[3] * m2[2] + m1[4] * m2[5] + m1[5]; | 31 dst[0] = ADD(MUL(m1[0], m2[0]), MUL(m1[1], m2[3])); |
32 dst[1] = ADD(MUL(m1[0], m2[1]), MUL(m1[1], m2[4])); | |
33 dst[2] = ADD(ADD(MUL(m1[0], m2[2]), MUL(m1[1], m2[5])), m1[2]); | |
34 dst[3] = ADD(MUL(m1[3], m2[0]), MUL(m1[4], m2[3])); | |
35 dst[4] = ADD(MUL(m1[3], m2[1]), MUL(m1[4], m2[4])); | |
36 dst[5] = ADD(ADD(MUL(m1[3], m2[2]), MUL(m1[4], m2[5])), m1[5]); | |
19 } | 37 } |
20 | 38 |
39 /*! \brief Compute agrregated transform function. | |
40 * | |
41 * Base on parent's aggregated matrix if it is existed, or use transform | |
42 * matrix as aggregated matrix. | |
43 */ | |
21 static void compute_transform_function(coord_t *visit) { | 44 static void compute_transform_function(coord_t *visit) { |
22 if(visit->parent) | 45 if(visit->parent) |
23 mul_matrix(visit->parent->aggr_matrix, | 46 mul_matrix(visit->parent->aggr_matrix, |
24 visit->matrix, visit->aggr_matrix); | 47 visit->matrix, visit->aggr_matrix); |
25 else | 48 else |
71 } | 94 } |
72 co->matrix[0] = 1; | 95 co->matrix[0] = 1; |
73 co->matrix[4] = 1; | 96 co->matrix[4] = 1; |
74 } | 97 } |
75 | 98 |
99 void coord_trans_pos(coord_t *co, co_aix *x, co_aix *y) { | |
100 co_aix nx, ny; | |
101 | |
102 nx = ADD(ADD(MUL(co->aggr_matrix[0], *x), | |
103 MUL(co->aggr_matrix[1], *y)), | |
104 co->aggr_matrix[2]); | |
105 ny = ADD(ADD(MUL(co->aggr_matrix[3], *x), | |
106 MUL(co->aggr_matrix[4], *y)), | |
107 co->aggr_matrix[5]); | |
108 *x = nx; | |
109 *y = ny; | |
110 } | |
111 | |
76 #ifdef UNITTEST | 112 #ifdef UNITTEST |
77 | 113 |
78 #include <CUnit/Basic.h> | 114 #include <CUnit/Basic.h> |
79 | 115 |
80 void test_update_aggr_matrix(void) { | 116 void test_update_aggr_matrix(void) { |
81 coord_t elms[6]; | 117 coord_t elms[6]; |
118 co_aix x, y; | |
82 | 119 |
83 coord_init(elms, NULL); | 120 coord_init(elms, NULL); |
84 coord_init(elms + 1, elms); | 121 coord_init(elms + 1, elms); |
85 coord_init(elms + 2, elms); | 122 coord_init(elms + 2, elms); |
86 coord_init(elms + 3, elms + 1); | 123 coord_init(elms + 3, elms + 1); |
125 CU_ASSERT(elms[5].aggr_matrix[1] == -1); | 162 CU_ASSERT(elms[5].aggr_matrix[1] == -1); |
126 CU_ASSERT(elms[5].aggr_matrix[2] == 0); | 163 CU_ASSERT(elms[5].aggr_matrix[2] == 0); |
127 CU_ASSERT(elms[5].aggr_matrix[3] == 0); | 164 CU_ASSERT(elms[5].aggr_matrix[3] == 0); |
128 CU_ASSERT(elms[5].aggr_matrix[4] == 1); | 165 CU_ASSERT(elms[5].aggr_matrix[4] == 1); |
129 CU_ASSERT(elms[5].aggr_matrix[5] == 0); | 166 CU_ASSERT(elms[5].aggr_matrix[5] == 0); |
167 | |
168 x = 50; | |
169 y = 99; | |
170 coord_trans_pos(elms + 5, &x, &y); | |
171 CU_ASSERT(x == 1); | |
172 CU_ASSERT(y == 99); | |
130 } | 173 } |
131 | 174 |
132 CU_pSuite get_coord_suite(void) { | 175 CU_pSuite get_coord_suite(void) { |
133 CU_pSuite suite; | 176 CU_pSuite suite; |
134 | 177 |