about summary refs log tree commit diff
path: root/src/Quaternions.h
blob: d8acf87bebaefd1fb0e874ffb90ace656faca24b (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
#ifndef BLACKSHADES_QUATERNIONS_H
#define BLACKSHADES_QUATERNIONS_H

struct XYZ {
	float x, y, z;
};

const struct XYZ DIRECTIONS[] = {
	{-130.400f, 0.000f, 120.800f},
	{-130.400f, 0.000f, -120.800f},
	{-124.800f, 0.000f, -129.600f},
	{121.600f, 0.000f, -128.800f},
	{127.200f, 0.000f, -120.000f},
	{125.600f, 0.000f, 119.200f},
	{120.800f, 0.000f, 127.200f},
	{-122.400f, 0.000f, 125.600f},
};

#ifdef __cplusplus
constexpr bool operator==(const XYZ& u, const XYZ& v)
{
	return u.x == v.x && u.y == v.y && u.z == v.z;
}
constexpr XYZ operator+(XYZ u, const XYZ& v)
{
	return {u.x + v.x, u.y + v.y, u.z + v.z};
}
constexpr XYZ operator-(XYZ u, const XYZ& v)
{
	return {u.x - v.x, u.y - v.y, u.z - v.z};
}
constexpr XYZ operator*(XYZ u, const XYZ& v)
{
	return {u.x * v.x, u.y * v.y, u.z * v.z};
}
constexpr XYZ operator*(XYZ u, float k)
{
	return {u.x * k, u.y * k, u.z * k};
}
constexpr XYZ operator/(XYZ u, float k)
{
	return {u.x / k, u.y / k, u.z / k};
}

constexpr XYZ& operator+=(XYZ& u, const XYZ& v) { return u = u + v; }
constexpr XYZ& operator-=(XYZ& u, const XYZ& v) { return u = u - v; }
constexpr XYZ& operator*=(XYZ& u, const XYZ& v) { return u = u * v; }
constexpr XYZ& operator*=(XYZ& u, float k) { return u = u * k; }
constexpr XYZ& operator/=(XYZ& u, float k) { return u = u / k; }
#endif // __cplusplus

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
	float sqrlen(struct XYZ);
	float len(struct XYZ);
	struct XYZ crossProduct(struct XYZ, struct XYZ);
	struct XYZ normalize(struct XYZ);
	void reflect(struct XYZ*, struct XYZ);
	bool segCrossSphere(struct XYZ, struct XYZ, struct XYZ, float);
	bool segCrossTrigon(struct XYZ p1, struct XYZ p2,
		struct XYZ *pa, struct XYZ *pb, struct XYZ *pc,
		struct XYZ *n, struct XYZ *p);
	struct XYZ rotate(struct XYZ, float, float, float);

	void setFrustum(float (*)[4], float*, float*);
	int cubeInFrustum(float (*)[4], float, float, float, float);
	int sphereInFrustum(float (*)[4], float, float, float, float);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus

#endif // BLACKSHADES_QUATERNIONS_H