Open Broadcaster Software
Free, open source software for live streaming and recording
bounds.h
Go to the documentation of this file.
1 /******************************************************************************
2  Copyright (C) 2013 by Hugh Bailey <obs.jim@gmail.com>
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16 ******************************************************************************/
17 
18 #pragma once
19 
20 #include "math-defs.h"
21 #include "vec3.h"
22 
23 /*
24  * Axis Aligned Bounding Box
25  */
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 #define BOUNDS_MAX_X 1
32 #define BOUNDS_MAX_Y 2
33 #define BOUNDS_MAX_Z 4
34 
35 #define BOUNDS_OUTSIDE 1
36 #define BOUNDS_INSIDE 2
37 #define BOUNDS_PARTIAL 3
38 
39 struct bounds {
40  struct vec3 min, max;
41 };
42 
43 static inline void bounds_zero(struct bounds *dst)
44 {
45  vec3_zero(&dst->min);
46  vec3_zero(&dst->max);
47 }
48 
49 static inline void bounds_copy(struct bounds *dst, const struct bounds *b)
50 {
51  vec3_copy(&dst->min, &b->min);
52  vec3_copy(&dst->max, &b->max);
53 }
54 
55 EXPORT void bounds_move(struct bounds *dst, const struct bounds *b,
56  const struct vec3 *v);
57 
58 EXPORT void bounds_scale(struct bounds *dst, const struct bounds *b,
59  const struct vec3 *v);
60 
61 EXPORT void bounds_merge(struct bounds *dst, const struct bounds *b1,
62  const struct bounds *b2);
63 EXPORT void bounds_merge_point(struct bounds *dst, const struct bounds *b,
64  const struct vec3 *v);
65 
66 EXPORT void bounds_get_point(struct vec3 *dst, const struct bounds *b,
67  unsigned int i);
68 EXPORT void bounds_get_center(struct vec3 *dst, const struct bounds *b);
69 
74 EXPORT void bounds_transform(struct bounds *dst, const struct bounds *b,
75  const struct matrix4 *m);
76 EXPORT void bounds_transform3x4(struct bounds *dst, const struct bounds *b,
77  const struct matrix3 *m);
78 
79 EXPORT bool bounds_intersection_ray(const struct bounds *b,
80  const struct vec3 *orig,
81  const struct vec3 *dir, float *t);
82 EXPORT bool bounds_intersection_line(const struct bounds *b,
83  const struct vec3 *p1,
84  const struct vec3 *p2, float *t);
85 
86 EXPORT bool bounds_plane_test(const struct bounds *b, const struct plane *p);
87 EXPORT bool bounds_under_plane(const struct bounds *b, const struct plane *p);
88 
89 static inline bool bounds_inside(const struct bounds *b,
90  const struct bounds *test)
91 {
92  return test->min.x >= b->min.x && test->min.y >= b->min.y &&
93  test->min.z >= b->min.z && test->max.x <= b->max.x &&
94  test->max.y <= b->max.y && test->max.z <= b->max.z;
95 }
96 
97 static inline bool bounds_vec3_inside(const struct bounds *b,
98  const struct vec3 *v)
99 {
100  return v->x >= (b->min.x - EPSILON) && v->x <= (b->max.x + EPSILON) &&
101  v->y >= (b->min.y - EPSILON) && v->y <= (b->max.y + EPSILON) &&
102  v->z >= (b->min.z - EPSILON) && v->z <= (b->max.z + EPSILON);
103 }
104 
105 EXPORT bool bounds_intersects(const struct bounds *b, const struct bounds *test,
106  float epsilon);
107 EXPORT bool bounds_intersects_obb(const struct bounds *b,
108  const struct bounds *test,
109  const struct matrix4 *m, float epsilon);
110 EXPORT bool bounds_intersects_obb3x4(const struct bounds *b,
111  const struct bounds *test,
112  const struct matrix3 *m, float epsilon);
113 
114 static inline bool bounds_intersects_ray(const struct bounds *b,
115  const struct vec3 *orig,
116  const struct vec3 *dir)
117 {
118  float t;
119  return bounds_intersection_ray(b, orig, dir, &t);
120 }
121 
122 static inline bool bounds_intersects_line(const struct bounds *b,
123  const struct vec3 *p1,
124  const struct vec3 *p2)
125 {
126  float t;
127  return bounds_intersection_line(b, p1, p2, &t);
128 }
129 
130 EXPORT float bounds_min_dist(const struct bounds *b, const struct plane *p);
131 
132 #ifdef __cplusplus
133 }
134 #endif
EXPORT void bounds_merge(struct bounds *dst, const struct bounds *b1, const struct bounds *b2)
EXPORT void bounds_scale(struct bounds *dst, const struct bounds *b, const struct vec3 *v)
EXPORT void bounds_transform(struct bounds *dst, const struct bounds *b, const struct matrix4 *m)
Definition: vec3.h:34
EXPORT bool bounds_intersects_obb(const struct bounds *b, const struct bounds *test, const struct matrix4 *m, float epsilon)
EXPORT void bounds_get_point(struct vec3 *dst, const struct bounds *b, unsigned int i)
EXPORT bool bounds_under_plane(const struct bounds *b, const struct plane *p)
Definition: matrix3.h:31
EXPORT void bounds_get_center(struct vec3 *dst, const struct bounds *b)
EXPORT void bounds_move(struct bounds *dst, const struct bounds *b, const struct vec3 *v)
float z
Definition: vec3.h:37
#define EPSILON
Definition: math-defs.h:34
#define EXPORT
Definition: c99defs.h:37
EXPORT bool bounds_plane_test(const struct bounds *b, const struct plane *p)
struct vec3 min max
Definition: bounds.h:40
__m128 m
Definition: vec3.h:40
EXPORT bool bounds_intersection_line(const struct bounds *b, const struct vec3 *p1, const struct vec3 *p2, float *t)
Definition: matrix4.h:32
EXPORT bool bounds_intersects(const struct bounds *b, const struct bounds *test, float epsilon)
EXPORT bool bounds_intersection_ray(const struct bounds *b, const struct vec3 *orig, const struct vec3 *dir, float *t)
float x
Definition: vec3.h:37
Definition: bounds.h:39
float y
Definition: vec3.h:37
EXPORT void bounds_merge_point(struct bounds *dst, const struct bounds *b, const struct vec3 *v)
EXPORT float bounds_min_dist(const struct bounds *b, const struct plane *p)
Definition: plane.h:30
EXPORT void bounds_transform3x4(struct bounds *dst, const struct bounds *b, const struct matrix3 *m)
EXPORT bool bounds_intersects_obb3x4(const struct bounds *b, const struct bounds *test, const struct matrix3 *m, float epsilon)