aboutsummaryrefslogtreecommitdiffstats
path: root/src/blind-vector-projection.c
blob: 8d65738d78f0408e6fd9c0c94cc977585579cd62 (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
/* See LICENSE file for copyright and license details. */
#ifndef TYPE
#include "common.h"

USAGE("[-r | -s] plane-stream")

static int level = 1;

#define FILE "blind-vector-projection.c"
#include "define-functions.h"

int
main(int argc, char *argv[])
{
	struct stream left, right;
	void (*process)(struct stream *left, struct stream *right, size_t n);

	ARGBEGIN {
	case 'r':
		if (level == 0)
			usage();
		level = 2;
		break;
	case 's':
		if (level == 2)
			usage();
		level = 0;
		break;
	default:
		usage();
	} ARGEND;

	if (argc != 1)
		usage();

	eopen_stream(&left, NULL);
	eopen_stream(&right, argv[0]);

	SELECT_PROCESS_FUNCTION(&left);
	CHECK_ALPHA_CHAN(&left);
	CHECK_N_CHAN(&left, 4, 4);

	fprint_stream_head(stdout, &left);
	efflush(stdout, "<stdout>");
	process_two_streams(&left, &right, STDOUT_FILENO, "<stdout>", process);
	return 0;
}

#else

static void
PROCESS(struct stream *left, struct stream *right, size_t n)
{
	size_t i;
	TYPE *lx, *ly, *lz, *la, rx, ry, rz, ra, x, y, z, a, norm;
	for (i = 0; i < n; i += 4 * sizeof(TYPE)) {
		lx = ((TYPE *)(left->buf + i)) + 0, rx = ((TYPE *)(right->buf + i))[0];
		ly = ((TYPE *)(left->buf + i)) + 1, ry = ((TYPE *)(right->buf + i))[1];
		lz = ((TYPE *)(left->buf + i)) + 2, rz = ((TYPE *)(right->buf + i))[2];
		la = ((TYPE *)(left->buf + i)) + 3, ra = ((TYPE *)(right->buf + i))[3];
		norm = rx * rx + ry * ry + rz * rz + ra * ra;
		norm = sqrt(norm);
		x = y = z = a = *lx * rx + *ly * ry + *lz * rz + *la * ra;
		if (level) {
			x *= rx;
			y *= ry;
			z *= rz;
			a *= rz;
			if (level > 1) {
				x = *lx - x;
				y = *ly - y;
				z = *lz - z;
				a = *la - a;
			}
		}
		*lx = x;
		*ly = y;
		*lz = z;
		*la = a;
	}
}

#endif