Open Broadcaster Software
Free, open source software for live streaming and recording
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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, const struct vec3 *dir, float *t);
81 EXPORT bool bounds_intersection_line(const struct bounds *b,
82  const struct vec3 *p1, const struct vec3 *p2, float *t);
83 
84 EXPORT bool bounds_plane_test(const struct bounds *b, const struct plane *p);
85 EXPORT bool bounds_under_plane(const struct bounds *b,
86  const struct plane *p);
87 
88 static inline bool bounds_inside(const struct bounds *b,
89  const struct bounds *test)
90 {
91  return test->min.x >= b->min.x &&
92  test->min.y >= b->min.y &&
93  test->min.z >= b->min.z &&
94  test->max.x <= b->max.x &&
95  test->max.y <= b->max.y &&
96  test->max.z <= b->max.z;
97 }
98 
99 static inline bool bounds_vec3_inside(const struct bounds *b,
100  const struct vec3 *v)
101 {
102  return v->x >= (b->min.x-EPSILON) &&
103  v->x <= (b->max.x+EPSILON) &&
104  v->y >= (b->min.y-EPSILON) &&
105  v->y <= (b->max.y+EPSILON) &&
106  v->z >= (b->min.z-EPSILON) &&
107  v->z <= (b->max.z+EPSILON);
108 }
109 
110 EXPORT bool bounds_intersects(const struct bounds *b,
111  const struct bounds *test, float epsilon);
112 EXPORT bool bounds_intersects_obb(const struct bounds *b,
113  const struct bounds *test, const struct matrix4 *m,
114  float epsilon);
115 EXPORT bool bounds_intersects_obb3x4(const struct bounds *b,
116  const struct bounds *test, const struct matrix3 *m,
117  float epsilon);
118 
119 static inline bool bounds_intersects_ray(const struct bounds *b,
120  const struct vec3 *orig, const struct vec3 *dir)
121 {
122  float t;
123  return bounds_intersection_ray(b, orig, dir, &t);
124 }
125 
126 static inline bool bounds_intersects_line(const struct bounds *b,
127  const struct vec3 *p1, const struct vec3 *p2)
128 {
129  float t;
130  return bounds_intersection_line(b, p1, p2, &t);
131 }
132 
133 EXPORT float bounds_min_dist(const struct bounds *b, const struct plane *p);
134 
135 #ifdef __cplusplus
136 }
137 #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:33
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:36
#define EPSILON
Definition: math-defs.h:34
#define EXPORT
Definition: c99defs.h:49
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:39
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:36
Definition: bounds.h:39
float y
Definition: vec3.h:36
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)