| 1 |
// |
| 2 |
// aabb.h |
| 3 |
// CubicVR2 |
| 4 |
// |
| 5 |
// Created by Charles J. Cliffe on 2013-02-22. |
| 6 |
// Copyright (c) 2013 Charles J. Cliffe. All rights reserved. |
| 7 |
// |
| 8 |
|
| 9 |
#ifndef __CubicVR2__aabb__ |
| 10 |
#define __CubicVR2__aabb__ |
| 11 |
|
| 12 |
#include <iostream> |
| 13 |
#include "vec3.h" |
| 14 |
|
| 15 |
namespace CubicVR { |
| 16 |
|
| 17 |
enum class aabb_intersect { AABB_DISJOINT, AABB_A_INSIDE_B, AABB_B_INSIDE_A, AABB_INTERSECT }; |
| 18 |
|
| 19 |
struct aabb { |
| 20 |
vec3 min, max; |
| 21 |
|
| 22 |
aabb(vec3 min_in, vec3 max_in) { |
| 23 |
min=min_in; |
| 24 |
max=max_in; |
| 25 |
} |
| 26 |
|
| 27 |
aabb() { |
| 28 |
min=max=vec3(0,0,0); |
| 29 |
} |
| 30 |
|
| 31 |
aabb engulf(aabb aabb, vec3 point) { |
| 32 |
if (aabb.min[0] > point[0]) { |
| 33 |
aabb.min[0] = point[0]; |
| 34 |
} |
| 35 |
if (aabb.min[1] > point[1]) { |
| 36 |
aabb.min[1] = point[1]; |
| 37 |
} |
| 38 |
if (aabb.min[2] > point[2]) { |
| 39 |
aabb.min[2] = point[2]; |
| 40 |
} |
| 41 |
if (aabb.max[0] < point[0]) { |
| 42 |
aabb.max[0] = point[0]; |
| 43 |
} |
| 44 |
if (aabb.max[1] < point[1]) { |
| 45 |
aabb.max[1] = point[1]; |
| 46 |
} |
| 47 |
if (aabb.max[2] < point[2]) { |
| 48 |
aabb.max[2] = point[2]; |
| 49 |
} |
| 50 |
return aabb; |
| 51 |
}; |
| 52 |
|
| 53 |
static aabb reset(aabb aabb, vec3 point=vec3(0.0f,0.0f,0.0f)) { |
| 54 |
|
| 55 |
aabb.min[0] = point[0]; |
| 56 |
aabb.min[1] = point[1]; |
| 57 |
aabb.min[2] = point[2]; |
| 58 |
aabb.max[0] = point[0]; |
| 59 |
aabb.max[1] = point[1]; |
| 60 |
aabb.max[2] = point[2]; |
| 61 |
|
| 62 |
return aabb; |
| 63 |
}; |
| 64 |
|
| 65 |
static vec3 size(aabb aabb) { |
| 66 |
__float x = aabb.min[0] < aabb.max[0] ? aabb.max[0] - aabb.min[0] : aabb.min[0] - aabb.max[0]; |
| 67 |
__float y = aabb.min[1] < aabb.max[1] ? aabb.max[1] - aabb.min[1] : aabb.min[1] - aabb.max[1]; |
| 68 |
__float z = aabb.min[2] < aabb.max[2] ? aabb.max[2] - aabb.min[2] : aabb.min[2] - aabb.max[2]; |
| 69 |
return vec3(x,y,z); |
| 70 |
}; |
| 71 |
/** |
| 72 |
Returns positive integer if intersect between A and B, 0 otherwise. |
| 73 |
For more detailed intersect result check value: |
| 74 |
CubicVR.enums.aabb.INTERSECT if AABBs intersect |
| 75 |
CubicVR.enums.aabb.A_INSIDE_B if boxA is inside boxB |
| 76 |
CubicVR.enums.aabb.B_INSIDE_A if boxB is inside boxA |
| 77 |
CubicVR.enums.aabb.DISJOINT if AABBs are disjoint (do not intersect) |
| 78 |
*/ |
| 79 |
aabb_intersect intersects(aabb boxA, aabb boxB) { |
| 80 |
// Disjoint |
| 81 |
if( boxA.min[0] > boxB.max[0] || boxA.max[0] < boxB.min[0] ){ |
| 82 |
return aabb_intersect::AABB_DISJOINT; |
| 83 |
} |
| 84 |
if( boxA.min[1] > boxB.max[1] || boxA.max[1] < boxB.min[1] ){ |
| 85 |
return aabb_intersect::AABB_DISJOINT; |
| 86 |
} |
| 87 |
if( boxA.min[2] > boxB.max[2] || boxA.max[2] < boxB.min[2] ){ |
| 88 |
return aabb_intersect::AABB_DISJOINT; |
| 89 |
} |
| 90 |
|
| 91 |
// boxA is inside boxB. |
| 92 |
if( boxA.min[0] >= boxB.min[0] && boxA.max[0] <= boxB.max[0] && |
| 93 |
boxA.min[1] >= boxB.min[1] && boxA.max[1] <= boxB.max[1] && |
| 94 |
boxA.min[2] >= boxB.min[2] && boxA.max[2] <= boxB.max[2]) { |
| 95 |
return aabb_intersect::AABB_A_INSIDE_B; |
| 96 |
} |
| 97 |
// boxB is inside boxA. |
| 98 |
if( boxB.min[0] >= boxA.min[0] && boxB.max[0] <= boxA.max[0] && |
| 99 |
boxB.min[1] >= boxA.min[1] && boxB.max[1] <= boxA.max[1] && |
| 100 |
boxB.min[2] >= boxA.min[2] && boxB.max[2] <= boxA.max[2]) { |
| 101 |
return aabb_intersect::AABB_B_INSIDE_A; |
| 102 |
} |
| 103 |
|
| 104 |
// Otherwise AABB's intersect. |
| 105 |
return aabb_intersect::AABB_INTERSECT; |
| 106 |
} |
| 107 |
}; |
| 108 |
}; |
| 109 |
|
| 110 |
#endif /* defined(__CubicVR2__aabb__) */ |