aboutsummaryrefslogtreecommitdiffstats
path: root/lines.c
blob: a2e87be9d2cb7a73bfb21647f078335ce0bfcc34 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST


#ifndef ROWWISE_RESET_INKLEVEL
void
draw_vertical_line_opposite_only(RASTER *restrict raster, size_t x, double y1, double y2, int ydir)
{
	size_t y, start, end;
	CELL *cells;

	cells = &raster->cells[x];

	start = (size_t)y1;
	end = (size_t)y2;

	y = start;
	if (ydir > 0) {
		cells[end * raster->width].opposite_coverage -= 1.0 - (y2 - (double)end);
		cells[start * raster->width].opposite_coverage -= y1 - (double)start;
		cells = &cells[start * raster->width];
		do {
			cells->opposite_coverage += 1.0;
			cells += raster->width;
		} while (y++ != end);
	} else {
		cells[end * raster->width].opposite_coverage += y2 - (double)end;
		cells[start * raster->width].opposite_coverage += 1.0 - (y1 - (double)start);
		cells = &cells[start * raster->width];
		do {
			cells->opposite_coverage -= 1.0;
			cells -= raster->width;
		} while (y-- != end);
	}
}
#endif


void
draw_vertical_line(RASTER *restrict raster, double x1, double y1, double y2, int ydir)
{
	size_t x, y, start, end;
	double cell_coverage;
	CELL *cells;

	x = (size_t)x1;
	cell_coverage = 1.0 - (x1 - (double)x);
	cells = &raster->cells[x];

	start = (size_t)y1;
	end = (size_t)y2;
	start -= (size_t)(start == raster->height);
	end -= (size_t)(end == raster->height);

	y = start;
	if (ydir > 0) {
		cells[end * raster->width].opposite_coverage -= 1.0 - (y2 - (double)end);
		cells[end * raster->width].cell_coverage -= (1.0 - (y2 - (double)end)) * cell_coverage;
		cells[start * raster->width].opposite_coverage -= y1 - (double)start;
		cells[start * raster->width].cell_coverage -= (y1 - (double)start) * cell_coverage;
		cells = &cells[start * raster->width];
		do {
			cells->opposite_coverage += 1.0;
			cells->cell_coverage += cell_coverage;
			cells += raster->width;
		} while (y++ != end);
	} else {
		cells[end * raster->width].opposite_coverage += y2 - (double)end;
		cells[end * raster->width].cell_coverage += (y2 - (double)end) * cell_coverage;
		cells[start * raster->width].opposite_coverage += 1.0 - (y1 - (double)start);
		cells[start * raster->width].cell_coverage += (1.0 - (y1 - (double)start)) * cell_coverage;
		cells = &cells[start * raster->width];
		do {
			cells->opposite_coverage -= 1.0;
			cells->cell_coverage -= cell_coverage;
			cells -= raster->width;
		} while (y-- != end);
	}
}


void
draw_diagonal_line(RASTER *restrict raster, double x1, double y1, double x2, double y2,
                   double dx, double dy, int xdir, int ydir)
{
	/* This code is taken, with rewrites and added documentation,
	 * from Thomas Oltmann's ISC licensed libschrift. */

	ssize_t rowsize;
	CELL *cell;
	size_t startCellX, startCellY, step, nsteps = 0;
	double ydiff, halfdx, one_minus_x1_plus_column;
	double nextProgress, prevProgress;
	double nextXProgress, nextYProgress;
	double xDistanceToProgress, yDistanceToProgress;
	int alongX;

	/* See just beneath */
	xDistanceToProgress = fabs(1.0 / dx);
	yDistanceToProgress = fabs(1.0 / dy);

	/* Get the column (x1, y1) is in. This is nothing
	 * more complicated then the floor of `x1`, however
	 * if `x1 > x2` and `x1` is on a cell edge, we say
	 * that it is in the left cell rather than the
	 * right cell; hence `ceil(x1) - 1`.
	 * Then we figure out how many steps will take
	 * horizontally when drawing the line. If (x1, y1)
	 * and (x2, y2) are in the same column, this is 0.
	 * Hence this the difference between the floor of
	 * min(x1, x2) and ceiling of max(x1, x2), less 1;
	 * less 1 because if (x1, y1) and (x2, y2) are in
	 * the same column the difference is 1: it is how
	 * many cells wide are covered, which is the 1
	 * greater than the number of edges between the
	 * cells.
	 * Third, we calculate (`nextXProgress`) where the
	 * line next (from (x1, y1)) intersections with a
	 * vertical line in the cell grid, and convert
	 * it the horizontal progression towards (x2, y2)
	 * by dividing by the unsigned projection of the
	 * line onto the horizontal raster axis.
	 */
	if (xdir > 0) {
		double x1floor = floor(x1), x2ceil = ceil(x2);
		startCellX = (size_t)x1floor;
		nsteps += (size_t)x2ceil - (size_t)x1floor - 1;
		nextXProgress = (x1floor + 1.0 - x1) * xDistanceToProgress;
	} else {
		double x2floor = floor(x2), x1ceil = ceil(x1);
		startCellX = (size_t)x1ceil - 1;
		nsteps += (size_t)x1ceil - (size_t)x2floor - 1;
		nextXProgress = (x1 + 1.0 - x1ceil) * xDistanceToProgress;
	}

	/* Same as above, but on the vertical axis */
	if (ydir > 0) {
		double y1floor = floor(y1), y2ceil = ceil(y2);
		startCellY = (size_t)y1floor;
		nsteps += (size_t)y2ceil - (size_t)y1floor - 1;
		nextYProgress = (y1floor + 1.0 - y1) * yDistanceToProgress;
	} else {
		double y2floor = floor(y2), y1ceil = ceil(y1);
		startCellY = (size_t)y1ceil - 1;
		nsteps += (size_t)y1ceil - (size_t)y2floor - 1;
		nextYProgress = (y1 + 1.0 - y1ceil) * yDistanceToProgress;
	}

	/* These values are in [0, 1], where 0 corresponds to (x1, y1)
	 * and 1 corresponds to (x2, y2). `prevProgress` is the amount of
	 * work done at the beginning of for-loop body, and `nextProgress`
	 * is the amount of work done at the end of the for-loop body. */
	prevProgress = 0.0;
	nextProgress = fmin(nextXProgress, nextYProgress);

	/* These are just tricks to reduce the number of operations made */
	halfdx = 0.5 * dx;
	one_minus_x1_plus_column = 1.0 - x1 + (double)startCellX;
	cell = &raster->cells[startCellY * raster->width + startCellX];
	rowsize = ydir * (ssize_t)raster->width;

	/* Visit all cells the line intersect (and one extra for every time
	 * the line intersects a cell corner); `nsteps` is the number of
	 * horizontal steps plus the number of vertical steps (Manhattan walk). */
	for (step = 0; step < nsteps; step++) {
		/* `nextProgress` and `prevProgress` are [0, 1] values
		 * measuring the progression from (x1, y1) to (x2, y2),
		 * hence `nextProgress - prevProgress` the the progress
		 * this interation makes, and it multiplied by `dy` is
		 * how much we travel vertically. This is how much
		 * additional ink shall be applied to all cells on this
		 * cells right (in the same row). */
		cell->opposite_coverage += ydiff = (nextProgress - prevProgress) * dy;
		/* This is the mount of ink we need in in this cell
		 * (minus all the sum of `.opposite_coverage` of
		 * all cells on the left in the same row). This is
		 * the area of a right trapezoid made by cells right
		 * edge, the line `y = prevProgress * dy`, the line
		 * `y = nextProgress * dy` and the draw line. */
		cell->cell_coverage += ydiff * (one_minus_x1_plus_column - halfdx * (prevProgress + nextProgress));

		/* Step either vertically or horizontally, whichever is shortest */
		prevProgress = nextProgress;
		alongX = nextXProgress < nextYProgress;
		nextXProgress += alongX ? xDistanceToProgress : 0;
		nextYProgress += alongX ? 0 : yDistanceToProgress;
		cell += alongX ? xdir : 0;
		cell += alongX ? 0 : rowsize;
		one_minus_x1_plus_column += alongX ? xdir : 0;
		nextProgress = fmin(nextXProgress, nextYProgress);
	}

	/* In case we get beyond (x2, y2), that is, if `prevProgress > 1`. */
	cell->opposite_coverage += ydiff = (1.0 - prevProgress) * dy;
	cell->cell_coverage += ydiff * (one_minus_x1_plus_column - halfdx * (prevProgress + 1.0));
}


void
draw_bounded_line(RASTER *restrict raster, double x1, double y1, double x2, double y2)
{
	double dx, dy = y2 - y1;
	int xdir, ydir = SIGNUM(dy);

	if (!ydir)
		return;

	dx = x2 - x1;
	xdir = SIGNUM(dx);

	if (!xdir)
		draw_vertical_line(raster, x1, y1, y2, ydir);
	else
		draw_diagonal_line(raster, x1, y1, x2, y2, dx, dy, xdir, ydir);
}


#else


static RASTER *raster;
static RASTER *refraster;


static double
frand(double a, double b)
{
	int i = rand();
	double f = (double)i / (double)RAND_MAX;
	double max = fmax(a, b);
	double min = fmin(a, b);
	return fma(f, max - min, min);
}


static void
check_against_reference(double x1, double y1, double x2, double y2)
{
	size_t i;
	alarm(5);
	draw_linear_bezier_reference(refraster, x1, y1, x2, y2);
	alarm(0);
	for (i = 0; i < 100; i++) {
		ASSERT(eq(raster->cells[i].cell_coverage, refraster->cells[i].cell_coverage));
		ASSERT(eq(raster->cells[i].opposite_coverage, refraster->cells[i].opposite_coverage));
	}
}


static void
check_vertical_line(double x, double y1, double y2)
{
	alarm(5);
	draw_vertical_line(raster, x, y1, y2, SIGNUM(y2 - y1));
	alarm(0);
	check_against_reference(x, y1, x, y2);
}


static void
check_diagonal_line(double x1, double y1, double x2, double y2)
{
	double dx = x2 - x1;
	double dy = y2 - y1;
	alarm(5);
	draw_diagonal_line(raster, x1, y1, x2, y2, dx, dy, SIGNUM(dx), SIGNUM(dy));
	alarm(0);
	check_against_reference(x1, y1, x2, y2);
}


static void
check_bounded_line(double x1, double y1, double x2, double y2)
{
	alarm(5);
	draw_bounded_line(raster, x1, y1, x2, y2);
	alarm(0);
	check_against_reference(x1, y1, x2, y2);
}


#ifndef ROWWISE_RESET_INKLEVEL
static void
check_vertical_line_opposite_only(double y1, double y2)
{
	alarm(5);
	draw_vertical_line_opposite_only(raster, 9, y1, y2, SIGNUM(y2 - y1));
	alarm(0);
	check_against_reference(20, y1, 20, y2);
}
#endif


static void
reset(void)
{
	rtgrpblib_reset_raster(raster, 10, 10);
	rtgrpblib_reset_raster(refraster, 10, 10);
}


int
main(void)
{
	double x1, x2;
	double y1, y2;
	size_t i;

	raster = rtgrpblib_create_raster(10, 10);
	ASSERT(raster);
	refraster = rtgrpblib_create_raster(10, 10);
	ASSERT(refraster);
	srand((unsigned)(uintptr_t)raster);

	for (i = 0; i < 10000UL; i++) {
		x1 = frand(0, 10);
		y1 = frand(0, 10);
		x2 = frand(0, 10);
		y2 = frand(0, 10);

		check_vertical_line(x1, y1, y2);
		reset();

		if (x1 != x2) {
			check_diagonal_line(x1, y1, x2, y2);
			reset();
		}

		check_bounded_line(x1, y1, x1, y2);
		reset();

		check_bounded_line(x1, y1, x2, y2);
		reset();

#ifndef ROWWISE_RESET_INKLEVEL
		check_vertical_line_opposite_only(y1, y2);
		reset();
#endif
	}

	free(raster);
	free(refraster);
	return 0;
}


#endif