comparison src/coord.c @ 12:79e9edf4c00a

Add redraw manager
author Thinker K.F. Li <thinker@branda.to>
date Mon, 28 Jul 2008 17:45:36 +0800
parents 9c331ec9e210
children ed55009d96d3
comparison
equal deleted inserted replaced
11:128af06c876c 12:79e9edf4c00a
3 * This file implements coordination transforming for containers. 3 * This file implements coordination transforming for containers.
4 */ 4 */
5 #include <stdio.h> 5 #include <stdio.h>
6 #include <string.h> 6 #include <string.h>
7 #include "mb_types.h" 7 #include "mb_types.h"
8
9
10 #define ASSERT(x)
8 11
9 /* To keep possibility of changing type of aix */ 12 /* To keep possibility of changing type of aix */
10 #define MUL(a, b) ((a) * (b)) 13 #define MUL(a, b) ((a) * (b))
11 #define ADD(a, b) ((a) + (b)) 14 #define ADD(a, b) ((a) + (b))
12 #define DIV(a, b) ((a) / (b)) 15 #define DIV(a, b) ((a) / (b))
44 47
45 compute_transform_function(start); 48 compute_transform_function(start);
46 49
47 visit = start; 50 visit = start;
48 while(visit) { 51 while(visit) {
49 child = visit->children; 52 child = STAILQ_HEAD(visit->children);
50 while(child) { 53 while(child) {
51 compute_transform_function(child); 54 compute_transform_function(child);
52 child = child->sibling; 55 child = STAILQ_NEXT(coord_t, sibling, child);
53 } 56 }
54 57
55 if(visit->children) 58 if(STAILQ_HEAD(visit->children))
56 visit = visit->children; 59 visit = STAILQ_HEAD(visit->children);
57 else if(visit->sibling) 60 else if(STAILQ_NEXT(coord_t, sibling, visit))
58 visit = visit->sibling; 61 visit = STAILQ_NEXT(coord_t, sibling, visit);
59 else { 62 else {
60 next = NULL; 63 next = NULL;
61 while(visit->parent && visit->parent != start) { 64 while(visit->parent && visit->parent != start) {
62 visit = visit->parent; 65 visit = visit->parent;
63 if(visit->sibling) { 66 if(STAILQ_NEXT(coord_t, sibling, visit)) {
64 next = visit->sibling; 67 next = STAILQ_NEXT(coord_t, sibling, visit);
65 break; 68 break;
66 } 69 }
67 } 70 }
68 visit = next; 71 visit = next;
69 } 72 }
70 } 73 }
71 } 74 }
72 75
76 /*! \brief Initialize a coord object.
77 *
78 * The object is cleared and matrix was initialized to ID.
79 * The object is be a children of specified parent.
80 */
73 void coord_init(coord_t *co, coord_t *parent) { 81 void coord_init(coord_t *co, coord_t *parent) {
74 memset(co, 0, sizeof(coord_t)); 82 memset(co, 0, sizeof(coord_t));
75 if(parent) { 83 if(parent) {
84 /* insert at tail of children list. */
76 co->parent = parent; 85 co->parent = parent;
77 co->sibling = parent->children; 86 STAILQ_INS_TAIL(parent->children, coord_t, sibling, co);
78 parent->children = co;
79 } 87 }
80 co->matrix[0] = 1; 88 co->matrix[0] = 1;
81 co->matrix[4] = 1; 89 co->matrix[4] = 1;
82 } 90 }
83 91
90 ny = ADD(ADD(MUL(co->aggr_matrix[3], *x), 98 ny = ADD(ADD(MUL(co->aggr_matrix[3], *x),
91 MUL(co->aggr_matrix[4], *y)), 99 MUL(co->aggr_matrix[4], *y)),
92 co->aggr_matrix[5]); 100 co->aggr_matrix[5]);
93 *x = nx; 101 *x = nx;
94 *y = ny; 102 *y = ny;
103 }
104
105 coord_t *preorder_coord_tree(coord_t *last) {
106 coord_t *next;
107
108 ASSERT(last == NULL);
109
110 if(STAILQ_HEAD(last->children))
111 next = STAILQ_HEAD(last->children);
112 else {
113 next = last;
114 while(next != NULL && STAILQ_NEXT(coord_t, sibling, next) == NULL)
115 next = next->parent;
116 if(next)
117 next = STAILQ_NEXT(coord_t, sibling, next);
118 }
119
120 return next;
121 }
122
123 void sh_attach_coord(shape_t *sh, coord_t *coord) {
124 STAILQ_INS_TAIL(coord->members, shape_t, coord_mem_next, sh);
125 sh->coord = coord;
126 }
127
128 void sh_detach_coord(shape_t *sh) {
129 STAILQ_REMOVE(sh->coord->members, shape_t, coord_mem_next, sh);
130 sh->coord = NULL;
95 } 131 }
96 132
97 #ifdef UNITTEST 133 #ifdef UNITTEST
98 134
99 #include <CUnit/Basic.h> 135 #include <CUnit/Basic.h>
155 coord_trans_pos(elms + 5, &x, &y); 191 coord_trans_pos(elms + 5, &x, &y);
156 CU_ASSERT(x == 1); 192 CU_ASSERT(x == 1);
157 CU_ASSERT(y == 99); 193 CU_ASSERT(y == 99);
158 } 194 }
159 195
196 void test_preorder_coord_tree(void) {
197 coord_t elms[6];
198 coord_t *last;
199
200 coord_init(elms, NULL);
201 coord_init(elms + 1, elms);
202 coord_init(elms + 2, elms);
203 coord_init(elms + 3, elms + 1);
204 coord_init(elms + 4, elms + 1);
205 coord_init(elms + 5, elms + 2);
206
207 last = elms;
208 last = preorder_coord_tree(last);
209 CU_ASSERT(last == elms + 1);
210 last = preorder_coord_tree(last);
211 CU_ASSERT(last == elms + 3);
212 last = preorder_coord_tree(last);
213 CU_ASSERT(last == elms + 4);
214 last = preorder_coord_tree(last);
215 CU_ASSERT(last == elms + 2);
216 last = preorder_coord_tree(last);
217 CU_ASSERT(last == elms + 5);
218 last = preorder_coord_tree(last);
219 CU_ASSERT(last == NULL);
220 }
221
160 CU_pSuite get_coord_suite(void) { 222 CU_pSuite get_coord_suite(void) {
161 CU_pSuite suite; 223 CU_pSuite suite;
162 224
163 suite = CU_add_suite("Suite_coord", NULL, NULL); 225 suite = CU_add_suite("Suite_coord", NULL, NULL);
164 CU_ADD_TEST(suite, test_update_aggr_matrix); 226 CU_ADD_TEST(suite, test_update_aggr_matrix);
227 CU_ADD_TEST(suite, test_preorder_coord_tree);
165 228
166 return suite; 229 return suite;
167 } 230 }
168 231
169 #endif 232 #endif