Mercurial > MadButterfly
annotate tools/gen_precomputed_tabs.py @ 1345:e0400a2b7c35
Use trait instead of mixin for component_manager
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Fri, 11 Feb 2011 15:10:37 +0800 |
parents | bd0cfb8666b8 |
children |
rev | line source |
---|---|
1129
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
2 from math import pi, sin, cos, sqrt |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
3 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
4 # #define FRACTION_SHIFT 10 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
5 # |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
6 # #define REF_RADIUS_SHIFT 10 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
7 # #define SLOPE_TAB_SZ 128 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
8 # #define ARC_RADIUS_RATIO_TAB_SZ 128 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
9 # #define ARC_RADIUS_FACTOR_TAB_SZ ARC_RADIUS_RATIO_TAB_SZ |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
10 # #define SIN_TAB_SZ 256 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
11 # static int slope_tab[SLOPE_TAB_SZ]; |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
12 # static int center_shift_tab[SLOPE_TAB_SZ][2]; |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
13 # static int vector_len_factor_tab[SLOPE_TAB_SZ]; |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
14 # static int arc_radius_ratio_tab[ARC_RADIUS_RATIO_TAB_SZ]; |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
15 # static int arc_radius_factor_tab[ARC_RADIUS_FACTOR_TAB_SZ]; |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
16 # static int sin_tab[SIN_TAB_SZ]; |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
17 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
18 class tabs_generator(object): |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
19 _fraction_shift = 10 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
20 _ref_radius_shift = 10 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
21 _slope_tab_sz = 128 |
1134
bd0cfb8666b8
Pass testcases for _calc_center() of shape_path.c.
Thinker K.F. Li <thinker@codemud.net>
parents:
1129
diff
changeset
|
22 _arc_radius_ratio_tab_sz = 256 |
bd0cfb8666b8
Pass testcases for _calc_center() of shape_path.c.
Thinker K.F. Li <thinker@codemud.net>
parents:
1129
diff
changeset
|
23 _arc_radius_factor_tab_sz = 256 |
1129
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
24 _sin_tab_sz = 256 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
25 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
26 def gen_slope_tab(self): |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
27 lines = [] |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
28 line = '''\ |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
29 /*! \\brief The table used to map a slope to an index. |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
30 * |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
31 * The index is used to be a key in other tables. |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
32 * The table is an array of slope values for vectors in 0~(PI/4) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
33 * direction. |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
34 */\ |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
35 ''' |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
36 lines.append(line) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
37 line = 'int slope_tab[SLOPE_TAB_SZ] = {' |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
38 lines.append(line) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
39 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
40 factor = 1 << self._fraction_shift |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
41 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
42 for i in range(self._slope_tab_sz): |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
43 angle = pi / 4 * i / (self._slope_tab_sz - 1) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
44 slope = int(sin(angle) / cos(angle) * factor) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
45 line = ' %d,' % (slope) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
46 lines.append(line) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
47 pass |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
48 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
49 line = ' };' |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
50 lines.append(line) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
51 return lines |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
52 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
53 def gen_center_shift_tab(self): |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
54 lines = [] |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
55 line = '''\ |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
56 /*! \\brief The table maps the slope of an arc to the factors of shifting. |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
57 * |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
58 * Every mapped slope is associated with two factors for x and y |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
59 * axis respective. The are multiplied with length of the arc to |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
60 * get shifting value in x and y axis direction. |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
61 */\ |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
62 ''' |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
63 lines.append(line) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
64 line = 'int center_shift_tab[SLOPE_TAB_SZ][2] = {' |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
65 lines.append(line) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
66 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
67 radius = 1 << (self._ref_radius_shift + self._fraction_shift) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
68 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
69 for i in range(self._slope_tab_sz): |
1134
bd0cfb8666b8
Pass testcases for _calc_center() of shape_path.c.
Thinker K.F. Li <thinker@codemud.net>
parents:
1129
diff
changeset
|
70 angle = pi / 4 * i / (self._slope_tab_sz - 1) + pi / 2 |
bd0cfb8666b8
Pass testcases for _calc_center() of shape_path.c.
Thinker K.F. Li <thinker@codemud.net>
parents:
1129
diff
changeset
|
71 x = int(cos(angle) * radius) |
bd0cfb8666b8
Pass testcases for _calc_center() of shape_path.c.
Thinker K.F. Li <thinker@codemud.net>
parents:
1129
diff
changeset
|
72 y = int(sin(angle) * radius) |
1129
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
73 line = ' {%d, %d},' % (x, y) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
74 lines.append(line) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
75 pass |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
76 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
77 line = ' };' |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
78 lines.append(line) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
79 return lines |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
80 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
81 def gen_vector_len_factor_tab(self): |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
82 lines = [] |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
83 line = '''\ |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
84 /*! \\brief The table maps a slope to a lenght factor for a vector. |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
85 * |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
86 * The factor is used to multipled with one of axis values |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
87 * to get the lenght of the vector. |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
88 * The range of mapped slopes are 0~(PI/4). |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
89 */\ |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
90 ''' |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
91 lines.append(line) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
92 line = 'int vector_len_factor_tab[SLOPE_TAB_SZ] = {' |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
93 lines.append(line) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
94 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
95 frac_factor = 1 << self._fraction_shift |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
96 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
97 for i in range(self._slope_tab_sz): |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
98 angle = pi / 4 * i / (self._slope_tab_sz - 1) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
99 factor = int((1 / cos(angle)) * frac_factor) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
100 line = ' %d,' % (factor) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
101 lines.append(line) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
102 pass |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
103 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
104 line = ' };' |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
105 lines.append(line) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
106 return lines |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
107 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
108 def gen_arc_radius_ratio_tab(self): |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
109 lines = [] |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
110 line = '''\ |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
111 /*! \\brief A table of ratio from an arc to its radius. |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
112 * |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
113 * It is to find an index for a given ratio value. |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
114 */\ |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
115 ''' |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
116 lines.append(line) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
117 line = 'int arc_radius_ratio_tab[ARC_RADIUS_RATIO_TAB_SZ] = {' |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
118 lines.append(line) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
119 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
120 frac_factor = 1 << self._fraction_shift |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
121 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
122 for i in range(self._arc_radius_ratio_tab_sz): |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
123 arc_ratio = 2.0 * i / (self._arc_radius_ratio_tab_sz - 1) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
124 arc_ratio = int(arc_ratio * frac_factor) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
125 line = ' %d,' % (arc_ratio) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
126 lines.append(line) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
127 pass |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
128 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
129 line = ' };' |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
130 lines.append(line) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
131 return lines |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
132 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
133 def gen_arc_radius_factor_tab(self): |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
134 lines = [] |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
135 line = '''\ |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
136 /*! \\brief The table maps an arc-radius ratio to a distance factor. |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
137 * |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
138 * The factor is multiplied with radius to get distance of arc and |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
139 * center. It is in the order of arc_radius_ratio_tab. |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
140 */\ |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
141 ''' |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
142 lines.append(line) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
143 line = 'int arc_radius_factor_tab[ARC_RADIUS_FACTOR_TAB_SZ] = {' |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
144 lines.append(line) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
145 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
146 frac_factor = 1 << self._fraction_shift |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
147 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
148 for i in range(self._arc_radius_factor_tab_sz): |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
149 arc = 2.0 * i / (self._arc_radius_factor_tab_sz - 1) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
150 factor = int(sqrt(1 - (arc / 2) ** 2) * frac_factor) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
151 line = ' %d,' % (factor) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
152 lines.append(line) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
153 pass |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
154 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
155 line = ' };' |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
156 lines.append(line) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
157 return lines |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
158 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
159 def gen_sin_tab(self): |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
160 lines = [] |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
161 line = '/*! \\brief A table of sin() values */' |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
162 lines.append(line) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
163 line = 'int sin_tab[SIN_TAB_SZ] = {' |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
164 lines.append(line) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
165 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
166 frac_factor = 1 << self._fraction_shift |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
167 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
168 for i in range(self._sin_tab_sz): |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
169 angle = i * pi / 2 / (self._sin_tab_sz - 1) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
170 _sin = int(sin(angle) * frac_factor) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
171 line = ' %d,' % (_sin) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
172 lines.append(line) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
173 pass |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
174 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
175 line = ' };' |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
176 lines.append(line) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
177 return lines |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
178 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
179 def gen_definition(self, out): |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
180 line = '/* This file is generated by tools/gen_precomputed_tabs.py */' |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
181 print >> out, line |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
182 print >> out |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
183 lines = self.gen_slope_tab() |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
184 print >> out, '\n'.join(lines) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
185 print >> out |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
186 print >> out |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
187 lines = self.gen_center_shift_tab() |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
188 print >> out, '\n'.join(lines) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
189 print >> out |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
190 print >> out |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
191 lines = self.gen_vector_len_factor_tab() |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
192 print >> out, '\n'.join(lines) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
193 print >> out |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
194 print >> out |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
195 lines = self.gen_arc_radius_ratio_tab() |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
196 print >> out, '\n'.join(lines) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
197 print >> out |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
198 print >> out |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
199 lines = self.gen_arc_radius_factor_tab() |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
200 print >> out, '\n'.join(lines) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
201 print >> out |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
202 print >> out |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
203 lines = self.gen_sin_tab() |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
204 print >> out, '\n'.join(lines) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
205 print >> out |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
206 pass |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
207 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
208 def gen_declaration(self, out): |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
209 line = '''\ |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
210 #define FRACTION_SHIFT %d |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
211 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
212 #define REF_RADIUS_SHIFT %d |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
213 #define SLOPE_TAB_SZ %d |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
214 #define ARC_RADIUS_RATIO_TAB_SZ %d |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
215 #define ARC_RADIUS_FACTOR_TAB_SZ %d |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
216 #define SIN_TAB_SZ %d |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
217 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
218 extern int slope_tab[SLOPE_TAB_SZ]; |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
219 extern int center_shift_tab[SLOPE_TAB_SZ][2]; |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
220 extern int vector_len_factor_tab[SLOPE_TAB_SZ]; |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
221 extern int arc_radius_ratio_tab[ARC_RADIUS_RATIO_TAB_SZ]; |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
222 extern int arc_radius_factor_tab[ARC_RADIUS_FACTOR_TAB_SZ]; |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
223 extern int sin_tab[SIN_TAB_SZ]; |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
224 ''' |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
225 line = line % (self._fraction_shift, self._ref_radius_shift, |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
226 self._slope_tab_sz, self._arc_radius_ratio_tab_sz, |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
227 self._arc_radius_factor_tab_sz, self._sin_tab_sz) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
228 print >> out, line |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
229 pass |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
230 pass |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
231 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
232 if __name__ == '__main__': |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
233 import sys |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
234 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
235 def usage(progname): |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
236 print >> sys.stderr, 'Usage: %s <C file> <header file>' % (progname) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
237 sys.exit(255) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
238 pass |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
239 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
240 if len(sys.argv) != 3: |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
241 usage(sys.argv[0]) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
242 pass |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
243 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
244 cfile = sys.argv[1] |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
245 hfile = sys.argv[2] |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
246 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
247 gen = tabs_generator() |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
248 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
249 cout = file(cfile, 'w+') |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
250 print >> cout, '#include "%s"' % (hfile) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
251 print >> cout |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
252 gen.gen_definition(cout) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
253 cout.close() |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
254 |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
255 hout = file(hfile, 'w+') |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
256 sentinel = '__' + hfile.upper().replace('.', '_') + '_' |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
257 print >> hout, '#ifndef %s' % (sentinel) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
258 print >> hout, '#define %s' % (sentinel) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
259 print >> hout |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
260 gen.gen_declaration(hout) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
261 print >> hout, '#endif /* %s */' % (sentinel) |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
262 hout.close() |
eca737d33a18
Improve performance of function to compute center of an arc.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
263 pass |