aboutsummaryrefslogtreecommitdiff
path: root/rframe.c
blob: aee3c03dcb5f4349d257bcfe0b04ded4c4547301 (plain)
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
#include <u.h>
#include <libc.h>
#include <geometry.h>

/*
 * implicit identity origin rframes
 *
 * 	static RFrame IRF2 = {
 * 		.p  = {0,0,1},
 * 		.bx = {1,0,0},
 * 		.by = {0,1,0},
 * 	};
 * 	
 * 	static RFrame3 IRF3 = {
 * 		.p  = {0,0,0,1},
 * 		.bx = {1,0,0,0},
 * 		.by = {0,1,0,0},
 * 		.bz = {0,0,1,0},
 * 	};
 *
 * these rframes are used on every xform to keep the points in the
 * correct plane (i.e. with proper w values); they are written here as a
 * reference for future changes. the bases are ignored since they turn
 * into an unnecessary identity xform.
 *
 * the implicitness comes from the fact that using the _irf* filters
 * makes the rframexform equivalent to:
 * 	rframexform(invrframexform(p, IRF), rf);
 * and the invrframexform to:
 * 	rframexform(invrframexform(p, rf), IRF);
 */

static Point2
_irfxform(Point2 p)
{
	p.w--;
	return p;
}

static Point2
_irfxform⁻¹(Point2 p)
{
	p.w++;
	return p;
}

static Point3
_irfxform3(Point3 p)
{
	p.w--;
	return p;
}

static Point3
_irfxform3⁻¹(Point3 p)
{
	p.w++;
	return p;
}

Point2
rframexform(Point2 p, RFrame rf)
{
	Matrix m = {
		rf.bx.x, rf.by.x, 0,
		rf.bx.y, rf.by.y, 0,
		0, 0, 1,
	};
	invm(m);
	return xform(subpt2(_irfxform⁻¹(p), rf.p), m);
}

Point3
rframexform3(Point3 p, RFrame3 rf)
{
	Matrix3 m = {
		rf.bx.x, rf.by.x, rf.bz.x, 0,
		rf.bx.y, rf.by.y, rf.bz.y, 0,
		rf.bx.z, rf.by.z, rf.bz.z, 0,
		0, 0, 0, 1,
	};
	invm3(m);
	return xform3(subpt3(_irfxform3⁻¹(p), rf.p), m);
}

Point2
invrframexform(Point2 p, RFrame rf)
{
	Matrix m = {
		rf.bx.x, rf.by.x, 0,
		rf.bx.y, rf.by.y, 0,
		0, 0, 1,
	};
	return _irfxform(addpt2(xform(p, m), rf.p));
}

Point3
invrframexform3(Point3 p, RFrame3 rf)
{
	Matrix3 m = {
		rf.bx.x, rf.by.x, rf.bz.x, 0,
		rf.bx.y, rf.by.y, rf.bz.y, 0,
		rf.bx.z, rf.by.z, rf.bz.z, 0,
		0, 0, 0, 1,
	};
	return _irfxform3(addpt3(xform3(p, m), rf.p));
}