Debian Codegraph @

generator/pamtris/utils.c

raw
1 /*=============================================================================
2 utils.c
3 ===============================================================================
4 Utility functions
5 =============================================================================*/
6
7 #include <stdlib.h>
8 #include <stdint.h>
9 #include <math.h>
10
11 #include "limits_pamtris.h"
12 #include "varying.h"
13
14 #include "utils.h"
15
16
17
18 void
19 prepare_for_interpolation(const varying * const begin,
20 const varying * const end,
21 varying * const out,
22 int32_t num_steps,
23 uint8_t const elements) {
24
25 double inverse_num_steps;
26 unsigned int i;
27
28 if (num_steps < 1) {
29 num_steps = 1;
30 }
31
32 inverse_num_steps = 1.0 / num_steps;
33
34 for (i = 0; i < elements; i++) {
35 out[i].v = begin[i].v;
36 out[i].s = (end[i].v - begin[i].v) * inverse_num_steps;
37 }
38 }
39
40
41
42 varying
43 compute_varying_z(int32_t const input_z) {
44
45 varying retval;
46
47 retval.v = 1.0 / (1 + input_z - MIN_COORD);
48 retval.s = 0.0;
49
50 return retval;
51 }
52
53
54
55 void
56 multiply_varying_array_by_varying(varying * const vars,
57 varying const multiplier,
58 uint8_t const elements) {
59
60 unsigned int i;
61
62 for (i = 0; i < elements; i++) {
63 vars[i].v *= multiplier.v;
64 vars[i].s = 0.0;
65 }
66 }
67
68
69 void
70 divide_varying_array_by_varying(varying * const vars,
71 varying const divisor,
72 uint8_t const elements) {
73
74 double const inverse_divisor = 1.0 / divisor.v;
75
76 unsigned int i;
77
78 for (i = 0; i < elements; i++) {
79 vars[i].v *= inverse_divisor;
80 vars[i].s = 0.0;
81 }
82 }
83
84
85
86 varying
87 inverse_varying(varying const var) {
88
89 varying retval;
90
91 retval.v = 1.0 / var.v;
92 retval.s = 0.0;
93
94 return retval;
95 }
96
97
98
99 varying
100 multiply_varyings(varying const a,
101 varying const b) {
102
103 varying retval;
104
105 retval.v = a.v * b.v;
106 retval.s = 0.0;
107
108 return retval;
109 }
110
111
112
113 void
114 step_up(varying * const vars,
115 uint8_t const elements) {
116
117 unsigned int i;
118
119 for (i = 0; i < elements; i++) {
120 vars[i].v += vars[i].s;
121 }
122 }
123
124
125
126 void
127 multi_step_up(varying * const vars,
128 int32_t const times,
129 uint8_t const elements) {
130
131 unsigned int i;
132
133 for (i = 0; i < elements; i++) {
134 vars[i].v += times * vars[i].s;
135 }
136 }
137
138
139
140 void
141 int32_to_varying_array(const int32_t * const in,
142 varying * const out,
143 uint8_t const elements) {
144
145 unsigned int i;
146
147 for (i = 0; i < elements; i++) {
148 out[i].v = in[i];
149 out[i].s = 0.0;
150 }
151 }
152
153
154
155 /* static int64_t
156 abs64(int64_t x)
157 {
158
159 int64_t const nm = ~geq_mask64(x, 0);
160
161 return (-x & nm) | (x & ~nm);
162 } */
163
164
165
166 int32_t
167 round_varying(varying const var) {
168
169 return round(var.v);
170 }
171
172
173
174 int64_t
175 geq_mask64(int64_t a, int64_t b) {
176
177 uint64_t const diff = a - b;
178
179 return -((~diff) >> 63);
180 }
181
182
183
184 static void
185 swap(uint8_t * const a,
186 uint8_t * const b) {
187 /*----------------------------------------------------------------------------
188 Swap the contents pointed to by a and b.
189 -----------------------------------------------------------------------------*/
190 uint8_t const temp = *a;
191
192 *a = *b;
193 *b = temp;
194 }
195
196
197
198 void
199 sort3(uint8_t * const index_array,
200 const int32_t * const y_array,
201 const int32_t * const x_array) {
202 /*----------------------------------------------------------------------------
203 Sort an index array of 3 elements. This function is used to sort vertices
204 with regard to relative row from top to bottom, but instead of sorting
205 an array of vertices with all their coordinates, we simply sort their
206 indices. Each element in the array pointed to by "index_array" should
207 contain one of the numbers 0, 1 or 2, and each one of them should be
208 different. "y_array" should point to an array containing the corresponding
209 Y coordinates (row) of each vertex and "x_array" should point to an array
210 containing the corresponding X coordinates (column) of each vertex.
211
212 If the Y coordinates are all equal, the indices are sorted with regard to
213 relative X coordinate from left to right. If only the top two vertex have
214 the same Y coordinate, the array is sorted normally with regard to relative
215 Y coordinate, but the first two indices are then sorted with regard to
216 relative X coordinate. Finally, If only the bottom two vertex have the same
217 Y coordinate, the array is sorted normally with regard to relative Y
218 coordinate, but the last two indices are then sorted with regard to relative
219 X coordinate.
220 -----------------------------------------------------------------------------*/
221 uint8_t * const ia = index_array;
222
223 const int32_t * ya;
224 const int32_t * xa;
225
226 ya = y_array; /* initial value */
227 xa = x_array; /* initial value */
228
229 if (ya[0] == ya[1] && ya[1] == ya[2]) {
230 /* In case the vertices represent a degenerate horizontal triangle, we
231 sort according to relative X coordinate, as opposed to Y.
232 */
233 ya = xa;
234 }
235
236 if (ya[ia[2]] < ya[ia[1]]) {
237 swap(ia, ia + 2);
238 if (ya[ia[2]] < ya[ia[1]]) {
239 swap(ia + 1, ia + 2);
240 if (ya[ia[1]] < ya[ia[0]]) {
241 swap(ia, ia + 1);
242 }
243 }
244 } else if (ya[ia[1]] < ya[ia[0]]) {
245 swap(ia, ia + 1);
246 if (ya[ia[2]] < ya[ia[1]]) {
247 swap(ia + 1, ia + 2);
248 }
249 }
250
251 if (ya == xa) {
252 return;
253 }
254
255 if (ya[ia[0]] == ya[ia[1]]) {
256 if (xa[ia[1]] < xa[ia[0]]) {
257 swap(ia, ia + 1);
258 }
259 } else if (ya[ia[1]] == ya[ia[2]]) {
260 if (xa[ia[2]] < xa[ia[1]]) {
261 swap(ia + 1, ia + 2);
262 }
263 }
264 }
265
266