File: AABB.hpp

package info (click to toggle)
spring 106.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 55,260 kB
  • sloc: cpp: 543,946; ansic: 44,800; python: 12,575; java: 12,201; awk: 5,889; sh: 1,796; asm: 1,546; xml: 655; perl: 405; php: 211; objc: 194; makefile: 76; sed: 2
file content (70 lines) | stat: -rw-r--r-- 1,904 bytes parent folder | download | duplicates (3)
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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifndef AABB_H
#define AABB_H

#include "System/float3.h"
#include "System/type2.h"
#include "System/Matrix44f.h"

struct AABB {
public:
	static bool RangeOverlap(const float2& a, const float2& b) {
		uint8_t n = 0;

		n += (b.x >= a.x && b.x <= a.y);
		n += (b.y >= a.x && b.y <= a.y);
		n += (a.x >= b.x && a.x <= b.y);
		n += (a.y >= b.x && a.y <= b.y);

		return (n > 0);
	}

	bool Intersects(const AABB& b) const {
		uint8_t n = 0;

		n += RangeOverlap({mins.x, maxs.x}, {b.mins.x, b.maxs.x});
		n += RangeOverlap({mins.y, maxs.y}, {b.mins.y, b.maxs.y});
		n += RangeOverlap({mins.z, maxs.z}, {b.mins.z, b.maxs.z});

		return (n == 3);
	}

	bool Contains(const float3& p) const {
		uint8_t n = 0;

		n += (p.x >= mins.x && p.x <= maxs.x);
		n += (p.y >= mins.y && p.y <= maxs.y);
		n += (p.z >= mins.z && p.z <= maxs.z);

		return (n == 3);
	};

	void CalcCorners(float3 verts[8]) const { CalcCorners(CMatrix44f::Identity(), verts); }
	void CalcCorners(const CMatrix44f& mat, float3 verts[8]) const {
		// bottom
		verts[0] = mat * float3{mins.x, mins.y, mins.z};
		verts[1] = mat * float3{mins.x, mins.y, maxs.z};
		verts[2] = mat * float3{maxs.x, mins.y, mins.z};
		verts[3] = mat * float3{maxs.x, mins.y, maxs.z};
		// top
		verts[4] = mat * float3{mins.x, maxs.y, mins.z};
		verts[5] = mat * float3{mins.x, maxs.y, maxs.z};
		verts[6] = mat * float3{maxs.x, maxs.y, mins.z};
		verts[7] = mat * float3{maxs.x, maxs.y, maxs.z};
	}

	float3 CalcCenter(const CMatrix44f& mat) const { return (mat * CalcCenter()); }
	float3 CalcCenter() const { return ((maxs + mins) * 0.5f); }
	float3 CalcScales() const { return ((maxs - mins) * 0.5f); }

	float CalcRadiusSq() const { return (CalcScales().SqLength()); }
	float CalcRadius() const { return (CalcScales().Length()); }

public:
	float3 mins;
	float3 maxs;
};

#endif