summaryrefslogtreecommitdiff
path: root/src/util/aabb.h
blob: 9dc4b20989b66e0f1722685520bcf01fabd857eb (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
#pragma once

#include "vector.h"

struct AABB {
  VEC3 min;
  VEC3 max;
};

inline VEC3 aabb_center( const AABB& aabb ) { return (aabb.min + aabb.max) * 0.5f; }
inline VEC3 aabb_half( const AABB& aabb ) { return (aabb.max - aabb.min) * 0.5f; }
inline F32 aabb_support_radius( const AABB& aabb, const VEC3& dir ) {
  VEC3 half = aabb_half( aabb );
  return fabsf(dir.x) * half.x + fabsf(dir.y) * half.y + fabsf(dir.z) * half.z;
}

inline U8 aabb_intersects( const AABB& aabb, const AABB& other ) {
  if (aabb.min.x > other.max.x || aabb.max.x < other.min.x)
    return 0;
  if (aabb.min.y > other.max.y || aabb.max.y < other.min.y)
    return 0;
  if (aabb.min.z > other.max.z || aabb.max.z < other.min.z)
    return 0;
  return 1;
}

inline U8 aabb_contains_point( const AABB& aabb, const VEC3& point ) {
  if (point.x < aabb.min.x || point.x > aabb.max.x)
    return 0;
  if (point.y < aabb.min.y || point.y > aabb.max.y)
    return 0;
  if (point.z < aabb.min.z || point.z > aabb.max.z)
    return 0;
  return 1;
}