annotate src/coord.c @ 1:b5c0162ccf69

Coordination tranforming
author Thinker K.F. Li <thinker@branda.to>
date Wed, 23 Jul 2008 13:57:33 +0800
parents
children 31402929c587
rev   line source
1
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
1 #include <stdio.h>
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
2 #include <string.h>
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
3
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
4 typedef struct coord {
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
5 int seq;
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
6 float matrix[6];
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
7 float aggr_matrix[6];
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
8 struct coord *parent;
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
9 struct coord *children, *sibling;
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
10 } coord_t;
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
11
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
12 static void mul_matrix(float *m1, float *m2, float *dst) {
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
13 dst[0] = m1[0] * m2[0] + m1[1] * m2[3];
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
14 dst[1] = m1[0] * m2[1] + m1[1] * m2[4];
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
15 dst[2] = m1[0] * m2[2] + m1[1] * m2[5] + m1[2];
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
16 dst[3] = m1[3] * m2[0] + m1[4] * m2[3];
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
17 dst[4] = m1[3] * m2[1] + m1[4] * m2[4];
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
18 dst[5] = m1[3] * m2[2] + m1[4] * m2[5] + m1[5];
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
19 }
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
20
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
21 static void compute_transform_function(coord_t *visit) {
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
22 if(visit->parent)
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
23 mul_matrix(visit->parent->aggr_matrix,
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
24 visit->matrix, visit->aggr_matrix);
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
25 else
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
26 memcpy(visit->aggr_matrix, visit->matrix, sizeof(visit->matrix));
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
27 }
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
28
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
29 /*! \brief Update aggregate matrices of elements under a sub-tree.
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
30 *
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
31 * A subtree is specified by the root of it. All elements in the subtree
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
32 * are effected by that changes of matrix of the subtree root.
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
33 */
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
34 void update_aggr_matrix(coord_t *start) {
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
35 coord_t *visit, *child, *next;
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
36
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
37 compute_transform_function(start);
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
38
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
39 visit = start;
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
40 while(visit) {
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
41 child = visit->children;
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
42 while(child) {
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
43 compute_transform_function(child);
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
44 child = child->sibling;
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
45 }
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
46
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
47 if(visit->children)
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
48 visit = visit->children;
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
49 else if(visit->sibling)
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
50 visit = visit->sibling;
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
51 else {
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
52 next = NULL;
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
53 while(visit->parent && visit->parent != start) {
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
54 visit = visit->parent;
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
55 if(visit->sibling) {
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
56 next = visit->sibling;
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
57 break;
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
58 }
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
59 }
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
60 visit = next;
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
61 }
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
62 }
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
63 }
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
64
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
65 void coord_init(coord_t *co, coord_t *parent) {
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
66 memset(co, 0, sizeof(coord_t));
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
67 if(parent) {
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
68 co->parent = parent;
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
69 co->sibling = parent->children;
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
70 parent->children = co;
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
71 }
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
72 co->matrix[0] = 1;
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
73 co->matrix[4] = 1;
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
74 }
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
75
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
76 #ifdef UNITTEST
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
77
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
78 #include <CUnit/Basic.h>
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
79
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
80 void test_update_aggr_matrix(void) {
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
81 coord_t elms[6];
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
82
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
83 coord_init(elms, NULL);
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
84 coord_init(elms + 1, elms);
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
85 coord_init(elms + 2, elms);
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
86 coord_init(elms + 3, elms + 1);
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
87 coord_init(elms + 4, elms + 1);
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
88 coord_init(elms + 5, elms + 2);
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
89
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
90 /* | 2 -1 0 |
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
91 * | 0 1 0 |
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
92 * | 0 0 1 |
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
93 */
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
94 elms[0].matrix[0] = 2;
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
95 elms[0].matrix[1] = -1;
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
96
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
97 /* | 1 3 0 |
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
98 * | 5 1 0 |
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
99 * | 0 0 1 |
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
100 */
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
101 elms[1].matrix[1] = 3;
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
102 elms[1].matrix[3] = 5;
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
103
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
104 update_aggr_matrix(elms);
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
105
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
106 /* | -3 5 0 |
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
107 * | 5 1 0 |
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
108 * | 0 0 1 |
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
109 */
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
110 CU_ASSERT(elms[3].aggr_matrix[0] == -3);
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
111 CU_ASSERT(elms[3].aggr_matrix[1] == 5);
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
112 CU_ASSERT(elms[3].aggr_matrix[2] == 0);
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
113 CU_ASSERT(elms[3].aggr_matrix[3] == 5);
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
114 CU_ASSERT(elms[3].aggr_matrix[4] == 1);
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
115 CU_ASSERT(elms[3].aggr_matrix[5] == 0);
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
116
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
117 CU_ASSERT(elms[4].aggr_matrix[0] == -3);
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
118 CU_ASSERT(elms[4].aggr_matrix[1] == 5);
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
119 CU_ASSERT(elms[4].aggr_matrix[2] == 0);
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
120 CU_ASSERT(elms[4].aggr_matrix[3] == 5);
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
121 CU_ASSERT(elms[4].aggr_matrix[4] == 1);
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
122 CU_ASSERT(elms[4].aggr_matrix[5] == 0);
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
123
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
124 CU_ASSERT(elms[5].aggr_matrix[0] == 2);
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
125 CU_ASSERT(elms[5].aggr_matrix[1] == -1);
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
126 CU_ASSERT(elms[5].aggr_matrix[2] == 0);
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
127 CU_ASSERT(elms[5].aggr_matrix[3] == 0);
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
128 CU_ASSERT(elms[5].aggr_matrix[4] == 1);
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
129 CU_ASSERT(elms[5].aggr_matrix[5] == 0);
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
130 }
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
131
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
132 CU_pSuite get_coord_suite(void) {
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
133 CU_pSuite suite;
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
134
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
135 suite = CU_add_suite("Suite_coord", NULL, NULL);
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
136 CU_ADD_TEST(suite, test_update_aggr_matrix);
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
137
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
138 return suite;
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
139 }
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
140
b5c0162ccf69 Coordination tranforming
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
141 #endif