Line data Source code
1 0 : /*
2 :
3 : OOBoundingBox.h
4 :
5 : Mathematical framework for Oolite.
6 :
7 : Oolite
8 : Copyright (C) 2004-2013 Giles C Williams and contributors
9 :
10 : This program is free software; you can redistribute it and/or
11 : modify it under the terms of the GNU General Public License
12 : as published by the Free Software Foundation; either version 2
13 : of the License, or (at your option) any later version.
14 :
15 : This program is distributed in the hope that it will be useful,
16 : but WITHOUT ANY WARRANTY; without even the implied warranty of
17 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 : GNU General Public License for more details.
19 :
20 : You should have received a copy of the GNU General Public License
21 : along with this program; if not, write to the Free Software
22 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23 : MA 02110-1301, USA.
24 :
25 : */
26 :
27 :
28 : #ifndef INCLUDED_OOMATHS_h
29 : #error Do not include OOBoundingBox.h directly; include OOMaths.h.
30 : #else
31 :
32 :
33 : typedef struct
34 : {
35 : Vector min;
36 : Vector max;
37 : } BoundingBox;
38 :
39 :
40 : extern const BoundingBox kZeroBoundingBox; /* (0, 0, 0), (0, 0, 0) */
41 :
42 :
43 : /* Extend bounding box to contain specified point. */
44 : OOINLINE void bounding_box_add_vector(BoundingBox *box, Vector vec) ALWAYS_INLINE_FUNC NONNULL_FUNC;
45 : OOINLINE void bounding_box_add_xyz(BoundingBox *box, GLfloat x, GLfloat y, GLfloat z) ALWAYS_INLINE_FUNC NONNULL_FUNC;
46 :
47 : /* Reset bounding box to kZeroBoundingBox. */
48 : OOINLINE void bounding_box_reset(BoundingBox *box) NONNULL_FUNC;
49 :
50 : /* Reset bounding box to a zero-sized box surrounding specified vector. */
51 : OOINLINE void bounding_box_reset_to_vector(BoundingBox *box, Vector vec) ALWAYS_INLINE_FUNC NONNULL_FUNC;
52 :
53 : OOINLINE void bounding_box_get_dimensions(BoundingBox bb, GLfloat *xSize, GLfloat *ySize, GLfloat *zSize) ALWAYS_INLINE_FUNC;
54 :
55 : OOINLINE Vector OOBoundingBoxCenter(BoundingBox bb) INLINE_CONST_FUNC;
56 :
57 : Vector OORandomPositionInBoundingBox(BoundingBox bb);
58 : HPVector OOHPRandomPositionInBoundingBox(BoundingBox bb);
59 :
60 :
61 :
62 : /*** Only inline definitions beyond this point ***/
63 :
64 : OOINLINE void bounding_box_add_vector(BoundingBox *box, Vector vec)
65 : {
66 : assert(box != NULL);
67 : box->min.x = fmin(box->min.x, vec.x);
68 : box->max.x = fmax(box->max.x, vec.x);
69 : box->min.y = fmin(box->min.y, vec.y);
70 : box->max.y = fmax(box->max.y, vec.y);
71 : box->min.z = fmin(box->min.z, vec.z);
72 : box->max.z = fmax(box->max.z, vec.z);
73 : }
74 :
75 :
76 : OOINLINE void bounding_box_add_xyz(BoundingBox *box, GLfloat x, GLfloat y, GLfloat z)
77 : {
78 : assert(box != NULL);
79 : box->min.x = fmin(box->min.x, x);
80 : box->max.x = fmax(box->max.x, x);
81 : box->min.y = fmin(box->min.y, y);
82 : box->max.y = fmax(box->max.y, y);
83 : box->min.z = fmin(box->min.z, z);
84 : box->max.z = fmax(box->max.z, z);
85 : }
86 :
87 :
88 : OOINLINE void bounding_box_reset(BoundingBox *box)
89 : {
90 : assert(box != NULL);
91 : *box = kZeroBoundingBox;
92 : }
93 :
94 :
95 : OOINLINE void bounding_box_reset_to_vector(BoundingBox *box, Vector vec)
96 : {
97 : assert(box != NULL);
98 : box->min = vec;
99 : box->max = vec;
100 : }
101 :
102 :
103 : OOINLINE void bounding_box_get_dimensions(BoundingBox bb, GLfloat *xSize, GLfloat *ySize, GLfloat *zSize)
104 : {
105 : if (xSize != NULL) *xSize = bb.max.x - bb.min.x;
106 : if (ySize != NULL) *ySize = bb.max.y - bb.min.y;
107 : if (zSize != NULL) *zSize = bb.max.z - bb.min.z;
108 : }
109 :
110 :
111 : OOINLINE Vector OOBoundingBoxCenter(BoundingBox bb)
112 : {
113 : return vector_multiply_scalar(vector_add(bb.min, bb.max), 0.5f);
114 : }
115 :
116 : #endif
|