Logo
Aabb.h
1#pragma once
2
3#include "Vector3.h"
4#include "Plane.h"
5
6namespace SharpKmyMath
7{
8 public ref class Aabb
9 {
10 public:
12 {
13 }
14
15 Aabb(const Vector3 min, const Vector3 max)
16 {
17 construct(min, max);
18 }
19
20 Aabb(const Aabb^ o)
21 {
22 center = o->center;
23 hsize = o->hsize;
24 }
25
26 Vector3 getCenter() { return center; }
27 void setCenter(Vector3 v) { center = v; }
28 Vector3 getSize() { return Vector3(hsize.x * 2, hsize.y * 2, hsize.z * 2); }
29 void setSize(Vector3 v) { hsize = v * 0.5f; }
30
31 Vector3 getMin() { return center + -hsize; }
32 Vector3 getMax() { return center + hsize; }
33 float getVolume() { return hsize.x * 2 * hsize.y * 2 * hsize.z * 2; }
34
35 void inflate(float v)
36 {
37 hsize += Vector3(v);
38 }
39
40 void inflateRate(float r)
41 {
42 hsize *= r;
43 }
44
45 bool intersect(Aabb^ p)
46 {
47 Vector3 min = getMin();
48 Vector3 max = getMax();
49 Vector3 pmin = p->getMin();
50 Vector3 pmax = p->getMax();
51
52 return
53 (min.x <= pmax.x && max.x >= pmin.x) &&
54 (min.y <= pmax.y && max.y >= pmin.y) &&
55 (min.z <= pmax.z && max.z >= pmin.z);
56 }
57
58 bool intersectAabb(Aabb^ p, Aabb^res)
59 {
60 Vector3 min = getMin();
61 Vector3 max = getMax();
62 Vector3 pmin = p->getMin();
63 Vector3 pmax = p->getMax();
64
65 Vector3 _min(0), _max(0);
66
67 res->construct(_min, _max);
68
69 if (min.x > pmax.x || max.x < pmin.x)
70 return (res->getVolume() > 0);
71 else
72 {
73 _min.x = (min.x > pmin.x) ? min.x : pmin.x;
74 _max.x = (max.x < pmax.x) ? max.x : pmax.x;
75 }
76
77 if (min.y > pmax.y || max.y < pmin.y)
78 return (res->getVolume() > 0);
79 else
80 {
81 _min.y = (min.y > pmin.y) ? min.y : pmin.y;
82 _max.y = (max.y < pmax.y) ? max.y : pmax.y;
83 }
84
85 if (min.z > pmax.z || max.z < pmin.z)
86 return (res->getVolume() > 0);
87 else
88 {
89 _min.z = (min.z > pmin.z) ? min.z : pmin.z;
90 _max.z = (max.z < pmax.z) ? max.z : pmax.z;
91 }
92 res->construct(_min, _max);
93 return (res->getVolume() > 0);
94 }
95
96 bool intersectPlane(Plane^ plane)
97 {
98 Vector3 min = getMin();
99 Vector3 max = getMax();
100 array<Vector3>^ points = gcnew array<Vector3>(8);
101 points[0] = Vector3(min.x, min.y, min.z);
102 points[1] = Vector3(max.x, min.y, min.z);
103 points[2] = Vector3(min.x, min.y, max.z);
104 points[3] = Vector3(max.x, min.y, max.z);
105 points[4] = Vector3(min.x, max.y, min.z);
106 points[5] = Vector3(max.x, max.y, min.z);
107 points[6] = Vector3(min.x, max.y, max.z);
108 points[7] = Vector3(max.x, max.y, max.z);
109
110 bool over = false;
111 bool under = false;
112
113 for (int i = 0; i < 8; i++)
114 {
115 if (plane->distanceTo(points[i]) > 0)
116 over = true;
117 else
118 under = true;
119 }
120
121 return under && over;
122 }
123
124 void expand(const Vector3 p)
125 {
126 Vector3 min = getMin();
127 Vector3 max = getMax();
128
129 if (p.x < min.x) min.x = p.x;
130 if (p.y < min.y) min.x = p.y;
131 if (p.z < min.z) min.x = p.z;
132
133 if (p.x > max.x) max.x = p.x;
134 if (p.y > max.y) max.x = p.y;
135 if (p.z > max.z) max.x = p.z;
136
137 construct(min, max);
138 }
139
140 void expand(Aabb^ aabb)
141 {
142 Vector3 min = getMin();
143 Vector3 max = getMax();
144 Vector3 amin = aabb->getMin();
145 Vector3 amax = aabb->getMax();
146
147 if (amin.x < min.x) min.x = amin.x;
148 if (amin.y < min.y) min.x = amin.y;
149 if (amin.z < min.z) min.x = amin.z;
150
151 if (amax.x > max.x) max.x = amax.x;
152 if (amax.y > max.y) max.x = amax.y;
153 if (amax.z > max.z) max.x = amax.z;
154
155 construct(min, max);
156 }
157
158 private:
159
160 void construct(const Vector3 min, const Vector3 max)
161 {
162 hsize = (max + -min) * 0.5f;
163 center = min + hsize;
164 hsize = Vector3(fabs(hsize.x), fabs(hsize.y), fabs(hsize.z));
165 }
166
167 Vector3 center;
168 Vector3 hsize;
169 };
170
171}
Definition: Aabb.h:9
void inflateRate(float r)
Definition: Aabb.h:40
void expand(Aabb^ aabb)
Definition: Aabb.h:140
void setCenter(Vector3 v)
Definition: Aabb.h:27
void expand(const Vector3 p)
Definition: Aabb.h:124
Vector3 getMin()
Definition: Aabb.h:31
bool intersectPlane(Plane^ plane)
Definition: Aabb.h:96
Vector3 getMax()
Definition: Aabb.h:32
bool intersectAabb(Aabb^ p, Aabb^res)
Definition: Aabb.h:58
Aabb(const Vector3 min, const Vector3 max)
Definition: Aabb.h:15
float getVolume()
Definition: Aabb.h:33
bool intersect(Aabb^ p)
Definition: Aabb.h:45
void setSize(Vector3 v)
Definition: Aabb.h:29
void inflate(float v)
Definition: Aabb.h:35
Vector3 getSize()
Definition: Aabb.h:28
Aabb(const Aabb^ o)
Definition: Aabb.h:20
Vector3 getCenter()
Definition: Aabb.h:26
Aabb()
Definition: Aabb.h:11
Definition: Math/Plane.h:8
float distanceTo(Vector3 point)
Definition: Math/Plane.h:22
Definition: RefCapture.h:3
Definition: Vector3.h:8
float y
Definition: Vector3.h:11
float z
Definition: Vector3.h:11
float x
Definition: Vector3.h:11