Mercurial > MadButterfly
annotate src/coord.c @ 17:41f0907b27ac
Unittest for rdman_redraw_changed().
geo_init() is seperated into geo_init() and geo_from_positions().
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Fri, 01 Aug 2008 23:32:22 +0800 |
parents | e17e12b112c4 |
children | da770188a44d |
rev | line source |
---|---|
2 | 1 /*! \brief Implement coordination tranform mechanism. |
2 * \file | |
3 * This file implements coordination transforming for containers. | |
4 */ | |
1 | 5 #include <stdio.h> |
6 #include <string.h> | |
5
9c331ec9e210
SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
3
diff
changeset
|
7 #include "mb_types.h" |
1 | 8 |
12 | 9 |
10 #define ASSERT(x) | |
11 | |
3
164162781a7a
Test cairo with Xlib surface
Thinker K.F. Li <thinker@branda.to>
parents:
2
diff
changeset
|
12 /* To keep possibility of changing type of aix */ |
2 | 13 #define MUL(a, b) ((a) * (b)) |
14 #define ADD(a, b) ((a) + (b)) | |
15 #define DIV(a, b) ((a) / (b)) | |
16 #define SUB(a, b) ((a) - (b)) | |
17 | |
18 static void mul_matrix(co_aix *m1, co_aix *m2, co_aix *dst) { | |
19 dst[0] = ADD(MUL(m1[0], m2[0]), MUL(m1[1], m2[3])); | |
20 dst[1] = ADD(MUL(m1[0], m2[1]), MUL(m1[1], m2[4])); | |
21 dst[2] = ADD(ADD(MUL(m1[0], m2[2]), MUL(m1[1], m2[5])), m1[2]); | |
22 dst[3] = ADD(MUL(m1[3], m2[0]), MUL(m1[4], m2[3])); | |
23 dst[4] = ADD(MUL(m1[3], m2[1]), MUL(m1[4], m2[4])); | |
24 dst[5] = ADD(ADD(MUL(m1[3], m2[2]), MUL(m1[4], m2[5])), m1[5]); | |
1 | 25 } |
26 | |
2 | 27 /*! \brief Compute agrregated transform function. |
28 * | |
29 * Base on parent's aggregated matrix if it is existed, or use transform | |
30 * matrix as aggregated matrix. | |
31 */ | |
1 | 32 static void compute_transform_function(coord_t *visit) { |
33 if(visit->parent) | |
34 mul_matrix(visit->parent->aggr_matrix, | |
35 visit->matrix, visit->aggr_matrix); | |
36 else | |
37 memcpy(visit->aggr_matrix, visit->matrix, sizeof(visit->matrix)); | |
38 } | |
39 | |
16
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
40 void compute_aggr_of_coord(coord_t *coord) { |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
41 compute_transform_function(coord); |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
42 } |
e17e12b112c4
A simple animation using rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
15
diff
changeset
|
43 |
1 | 44 /*! \brief Update aggregate matrices of elements under a sub-tree. |
45 * | |
46 * A subtree is specified by the root of it. All elements in the subtree | |
47 * are effected by that changes of matrix of the subtree root. | |
48 */ | |
49 void update_aggr_matrix(coord_t *start) { | |
50 coord_t *visit, *child, *next; | |
51 | |
52 compute_transform_function(start); | |
53 | |
54 visit = start; | |
55 while(visit) { | |
12 | 56 child = STAILQ_HEAD(visit->children); |
1 | 57 while(child) { |
58 compute_transform_function(child); | |
12 | 59 child = STAILQ_NEXT(coord_t, sibling, child); |
1 | 60 } |
61 | |
12 | 62 if(STAILQ_HEAD(visit->children)) |
63 visit = STAILQ_HEAD(visit->children); | |
64 else if(STAILQ_NEXT(coord_t, sibling, visit)) | |
65 visit = STAILQ_NEXT(coord_t, sibling, visit); | |
1 | 66 else { |
67 next = NULL; | |
68 while(visit->parent && visit->parent != start) { | |
69 visit = visit->parent; | |
12 | 70 if(STAILQ_NEXT(coord_t, sibling, visit)) { |
71 next = STAILQ_NEXT(coord_t, sibling, visit); | |
1 | 72 break; |
73 } | |
74 } | |
75 visit = next; | |
76 } | |
77 } | |
78 } | |
79 | |
12 | 80 /*! \brief Initialize a coord object. |
81 * | |
82 * The object is cleared and matrix was initialized to ID. | |
83 * The object is be a children of specified parent. | |
84 */ | |
1 | 85 void coord_init(coord_t *co, coord_t *parent) { |
86 memset(co, 0, sizeof(coord_t)); | |
87 if(parent) { | |
12 | 88 /* insert at tail of children list. */ |
1 | 89 co->parent = parent; |
12 | 90 STAILQ_INS_TAIL(parent->children, coord_t, sibling, co); |
1 | 91 } |
92 co->matrix[0] = 1; | |
93 co->matrix[4] = 1; | |
17
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
94 co->aggr_matrix[0] = 1; |
41f0907b27ac
Unittest for rdman_redraw_changed().
Thinker K.F. Li <thinker@branda.to>
parents:
16
diff
changeset
|
95 co->aggr_matrix[4] = 1; |
15
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
13
diff
changeset
|
96 co->cur_area = &co->areas[0]; |
c2ce186a5c37
X_main uses rdman_redraw_all()
Thinker K.F. Li <thinker@branda.to>
parents:
13
diff
changeset
|
97 co->last_area = &co->areas[1]; |
1 | 98 } |
99 | |
2 | 100 void coord_trans_pos(coord_t *co, co_aix *x, co_aix *y) { |
101 co_aix nx, ny; | |
102 | |
103 nx = ADD(ADD(MUL(co->aggr_matrix[0], *x), | |
104 MUL(co->aggr_matrix[1], *y)), | |
105 co->aggr_matrix[2]); | |
106 ny = ADD(ADD(MUL(co->aggr_matrix[3], *x), | |
107 MUL(co->aggr_matrix[4], *y)), | |
108 co->aggr_matrix[5]); | |
109 *x = nx; | |
110 *y = ny; | |
111 } | |
112 | |
13 | 113 coord_t *preorder_coord_subtree(coord_t *root, coord_t *last) { |
12 | 114 coord_t *next; |
115 | |
116 ASSERT(last == NULL); | |
117 | |
118 if(STAILQ_HEAD(last->children)) | |
119 next = STAILQ_HEAD(last->children); | |
120 else { | |
121 next = last; | |
13 | 122 while(next != root && STAILQ_NEXT(coord_t, sibling, next) == NULL) |
12 | 123 next = next->parent; |
13 | 124 if(next == root) |
125 next = NULL; | |
12 | 126 if(next) |
127 next = STAILQ_NEXT(coord_t, sibling, next); | |
128 } | |
129 | |
130 return next; | |
131 } | |
132 | |
133 void sh_attach_coord(shape_t *sh, coord_t *coord) { | |
134 STAILQ_INS_TAIL(coord->members, shape_t, coord_mem_next, sh); | |
135 sh->coord = coord; | |
136 } | |
137 | |
138 void sh_detach_coord(shape_t *sh) { | |
139 STAILQ_REMOVE(sh->coord->members, shape_t, coord_mem_next, sh); | |
140 sh->coord = NULL; | |
141 } | |
142 | |
1 | 143 #ifdef UNITTEST |
144 | |
145 #include <CUnit/Basic.h> | |
146 | |
147 void test_update_aggr_matrix(void) { | |
148 coord_t elms[6]; | |
2 | 149 co_aix x, y; |
1 | 150 |
151 coord_init(elms, NULL); | |
152 coord_init(elms + 1, elms); | |
153 coord_init(elms + 2, elms); | |
154 coord_init(elms + 3, elms + 1); | |
155 coord_init(elms + 4, elms + 1); | |
156 coord_init(elms + 5, elms + 2); | |
157 | |
158 /* | 2 -1 0 | | |
159 * | 0 1 0 | | |
160 * | 0 0 1 | | |
161 */ | |
162 elms[0].matrix[0] = 2; | |
163 elms[0].matrix[1] = -1; | |
164 | |
165 /* | 1 3 0 | | |
166 * | 5 1 0 | | |
167 * | 0 0 1 | | |
168 */ | |
169 elms[1].matrix[1] = 3; | |
170 elms[1].matrix[3] = 5; | |
171 | |
172 update_aggr_matrix(elms); | |
173 | |
174 /* | -3 5 0 | | |
175 * | 5 1 0 | | |
176 * | 0 0 1 | | |
177 */ | |
178 CU_ASSERT(elms[3].aggr_matrix[0] == -3); | |
179 CU_ASSERT(elms[3].aggr_matrix[1] == 5); | |
180 CU_ASSERT(elms[3].aggr_matrix[2] == 0); | |
181 CU_ASSERT(elms[3].aggr_matrix[3] == 5); | |
182 CU_ASSERT(elms[3].aggr_matrix[4] == 1); | |
183 CU_ASSERT(elms[3].aggr_matrix[5] == 0); | |
184 | |
185 CU_ASSERT(elms[4].aggr_matrix[0] == -3); | |
186 CU_ASSERT(elms[4].aggr_matrix[1] == 5); | |
187 CU_ASSERT(elms[4].aggr_matrix[2] == 0); | |
188 CU_ASSERT(elms[4].aggr_matrix[3] == 5); | |
189 CU_ASSERT(elms[4].aggr_matrix[4] == 1); | |
190 CU_ASSERT(elms[4].aggr_matrix[5] == 0); | |
191 | |
192 CU_ASSERT(elms[5].aggr_matrix[0] == 2); | |
193 CU_ASSERT(elms[5].aggr_matrix[1] == -1); | |
194 CU_ASSERT(elms[5].aggr_matrix[2] == 0); | |
195 CU_ASSERT(elms[5].aggr_matrix[3] == 0); | |
196 CU_ASSERT(elms[5].aggr_matrix[4] == 1); | |
197 CU_ASSERT(elms[5].aggr_matrix[5] == 0); | |
2 | 198 |
199 x = 50; | |
200 y = 99; | |
201 coord_trans_pos(elms + 5, &x, &y); | |
202 CU_ASSERT(x == 1); | |
203 CU_ASSERT(y == 99); | |
1 | 204 } |
205 | |
13 | 206 void test_preorder_coord_subtree(void) { |
12 | 207 coord_t elms[6]; |
208 coord_t *last; | |
209 | |
210 coord_init(elms, NULL); | |
211 coord_init(elms + 1, elms); | |
212 coord_init(elms + 2, elms); | |
213 coord_init(elms + 3, elms + 1); | |
214 coord_init(elms + 4, elms + 1); | |
215 coord_init(elms + 5, elms + 2); | |
216 | |
217 last = elms; | |
13 | 218 last = preorder_coord_subtree(elms, last); |
12 | 219 CU_ASSERT(last == elms + 1); |
13 | 220 last = preorder_coord_subtree(elms, last); |
12 | 221 CU_ASSERT(last == elms + 3); |
13 | 222 last = preorder_coord_subtree(elms, last); |
12 | 223 CU_ASSERT(last == elms + 4); |
13 | 224 last = preorder_coord_subtree(elms, last); |
12 | 225 CU_ASSERT(last == elms + 2); |
13 | 226 last = preorder_coord_subtree(elms, last); |
12 | 227 CU_ASSERT(last == elms + 5); |
13 | 228 last = preorder_coord_subtree(elms, last); |
12 | 229 CU_ASSERT(last == NULL); |
230 } | |
231 | |
1 | 232 CU_pSuite get_coord_suite(void) { |
233 CU_pSuite suite; | |
234 | |
235 suite = CU_add_suite("Suite_coord", NULL, NULL); | |
236 CU_ADD_TEST(suite, test_update_aggr_matrix); | |
13 | 237 CU_ADD_TEST(suite, test_preorder_coord_subtree); |
1 | 238 |
239 return suite; | |
240 } | |
241 | |
242 #endif |