annotate src/geo.c @ 5:9c331ec9e210

SVG path is partially supported
author Thinker K.F. Li <thinker@branda.to>
date Sat, 26 Jul 2008 01:37:35 +0800
parents
children 7cfecdce94cc
rev   line source
5
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
1 /*! \brief Determine who should be re-drawed.
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
2 * \file
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
3 * When part of graphic are chagned, not mater size, shape, or position,
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
4 * the components effected or overlaid should be re-drawed. This module
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
5 * figures out components that should be re-drawed.
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
6 */
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
7 #include <stdio.h>
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
8 #include "mb_types.h"
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
9
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
10 struct _geo {
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
11 co_aix x, y;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
12 co_aix w, h;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
13 co_aix rel_x, rel_y;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
14 co_aix orig_x, orig_y;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
15 int n_subs;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
16 struct _geo *subs;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
17 };
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
18
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
19 static int is_pos_in(co_aix x, co_aix y,
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
20 co_aix rectx, co_aix recty,
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
21 co_aix rectw, co_aix recth) {
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
22 co_aix rel_x, rel_y;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
23
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
24 rel_x = x - rectx;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
25 if(rel_x < 0 || rel_x >= rectw)
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
26 return 0;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
27 rel_y = y - recty;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
28 if(rel_y < 0 || rel_y >= recth)
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
29 return 0;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
30 return 1;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
31 }
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
32
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
33 static int is_scale_overlay(co_aix x1, co_aix w1, co_aix x2, co_aix w2) {
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
34 if(x1 > x2) {
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
35 if((x1 - x2) >= w2)
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
36 return 0;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
37 } else {
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
38 if((x2 - x1) >= w1)
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
39 return 0;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
40 }
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
41 return 1;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
42 }
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
43
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
44 static int is_overlay(geo_t *r1, geo_t *r2) {
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
45 co_aix rel_x, rel_y;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
46
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
47 if(!is_scale_overlay(r1->x, r1->w, r2->x, r2->w))
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
48 return 0;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
49 if(!is_scale_overlay(r1->y, r1->h, r2->y, r2->h))
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
50 return 0;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
51
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
52 return 1;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
53 }
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
54
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
55 void geo_init(geo_t *g, int n_pos, co_aix pos[][2]) {
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
56 co_aix min_x, max_x;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
57 co_aix min_y, max_y;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
58 co_aix x, y;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
59 int i;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
60
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
61 min_x = max_x = pos[0][0];
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
62 min_y = max_y = pos[0][1];
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
63 for(i = 1; i < n_pos; i++) {
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
64 x = pos[i][0];
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
65 if(x < min_x)
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
66 min_x = x;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
67 else if(x > max_x)
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
68 max_x = x;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
69 y = pos[i][1];
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
70 if(y < min_y)
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
71 min_y = y;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
72 else if(y > max_y)
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
73 max_y = y;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
74 }
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
75 g->x = min_x;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
76 g->w = max_x - min_x;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
77 g->y = min_y;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
78 g->h = max_y - min_y;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
79 }
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
80
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
81 void geo_mark_overlay(geo_t *g, int n_others, geo_t **others,
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
82 int *n_overlays, geo_t **overlays) {
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
83 int i, ov_idx;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
84
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
85 ov_idx = 0;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
86 for(i = 0; i < n_others; i++) {
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
87 if(is_overlay(g, others[i]))
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
88 overlays[ov_idx++] = others[i];
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
89 }
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
90 *n_overlays = ov_idx;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
91 }
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
92
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
93
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
94 #ifdef UNITTEST
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
95
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
96 #include <CUnit/Basic.h>
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
97
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
98 void test_geo_init(void) {
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
99 co_aix data[][2] = {
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
100 {33, 25}, {49, 12},
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
101 {14, 28}, {39, 56}};
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
102 geo_t g;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
103
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
104 geo_init(&g, 4, data);
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
105 CU_ASSERT(g.x == 14);
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
106 CU_ASSERT(g.w == 35);
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
107 CU_ASSERT(g.y == 12);
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
108 CU_ASSERT(g.h == 44);
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
109 }
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
110
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
111 void test_geo_mark_overlay(void) {
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
112 geo_t _geos[3], *geos[3], *overlays[3];
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
113 geo_t g;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
114 int i, n_ov;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
115
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
116 for(i = 0; i < 3; i++) {
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
117 _geos[i].x = i * 50;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
118 _geos[i].y = i * 50;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
119 _geos[i].w = 55;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
120 _geos[i].h = 66;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
121 geos[i] = _geos + i;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
122 }
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
123 g.x = 88;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
124 g.y = 79;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
125 g.w = 70;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
126 g.h = 70;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
127
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
128 /* overlay with geos[1] and geos[2] */
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
129 geo_mark_overlay(&g, 3, geos, &n_ov, overlays);
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
130 CU_ASSERT(n_ov == 2);
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
131 CU_ASSERT(overlays[0] == geos[1]);
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
132 CU_ASSERT(overlays[1] == geos[2]);
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
133
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
134 /* right side of geos[1], and up side of geos[2] */
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
135 g.x = 105;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
136 g.y = 50;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
137 g.w = 50;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
138 g.h = 51;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
139 geo_mark_overlay(&g, 3, geos, &n_ov, overlays);
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
140 CU_ASSERT(n_ov == 1);
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
141 CU_ASSERT(overlays[0] == geos[2]);
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
142 }
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
143
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
144 CU_pSuite get_geo_suite(void) {
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
145 CU_pSuite suite;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
146
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
147 suite = CU_add_suite("Suite_geo", NULL, NULL);
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
148 CU_ADD_TEST(suite, test_geo_init);
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
149 CU_ADD_TEST(suite, test_geo_mark_overlay);
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
150
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
151 return suite;
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
152 }
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
153
9c331ec9e210 SVG path is partially supported
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
154 #endif