TORCS  1.3.10
The Open Racing Car Simulator
Loading...
Searching...
No Matches
BBox.h
Go to the documentation of this file.
1/*
2 SOLID - Software Library for Interference Detection
3 Copyright (C) 1997-1998 Gino van den Bergen
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 Please send remarks, questions and bug reports to gino@win.tue.nl,
20 or write to:
21 Gino van den Bergen
22 Department of Mathematics and Computing Science
23 Eindhoven University of Technology
24 P.O. Box 513, 5600 MB Eindhoven, The Netherlands
25*/
26
27#ifndef _BBOX_H_
28#define _BBOX_H_
29
30#ifdef _MSC_VER
31#pragma warning(disable:4786) // identifier was truncated to '255'
32#endif // _MSC_VER
33
34#include <3D/Point.h>
35
36class BBox {
37public:
38 BBox() {}
39 BBox(const Point& min, const Point& max) { setValue(min, max); }
40
41 const Point& getCenter() const { return center; }
42 const Vector& getExtent() const { return extent; }
43
44 void setCenter(const Point& p) { center = p; }
45 void setExtent(const Vector& v) { extent = v; }
46
47 void setValue(const Point& min, const Point& max) {
48 extent = (max - min) / 2;
49 center = min + extent;
50 }
51
52 void enclose(const BBox& a, const BBox& b) {
53 Point lower(min(a.getLower(X), b.getLower(X)),
54 min(a.getLower(Y), b.getLower(Y)),
55 min(a.getLower(Z), b.getLower(Z)));
56 Point upper(max(a.getUpper(X), b.getUpper(X)),
57 max(a.getUpper(Y), b.getUpper(Y)),
58 max(a.getUpper(Z), b.getUpper(Z)));
59 setValue(lower, upper);
60 }
61
62 void setEmpty() {
63 center.setValue(0, 0, 0);
64 extent.setValue(-INFINITY_, -INFINITY_, -INFINITY_);
65 }
66
67 void include (const Point& p) {
68 Point lower(min(getLower(X), p[X]),
69 min(getLower(Y), p[Y]),
70 min(getLower(Z), p[Z]));
71 Point upper(max(getUpper(X), p[X]),
72 max(getUpper(Y), p[Y]),
73 max(getUpper(Z), p[Z]));
74 setValue(lower, upper);
75 }
76
77 void include (const BBox& b) { enclose(*this, b); }
78
79 Scalar getLower(int i) const { return center[i] - extent[i]; }
80 Scalar getUpper(int i) const { return center[i] + extent[i]; }
81
82 Scalar size() const { return max(max(extent[X], extent[Y]), extent[Z]); }
83 int longestAxis() const { return extent.closestAxis(); }
84
85 friend bool intersect(const BBox& a, const BBox& b);
86
87private:
90};
91
92inline bool intersect(const BBox& a, const BBox& b) {
93 return fabs(a.center[X] - b.center[X]) <= a.extent[X] + b.extent[X] &&
94 fabs(a.center[Y] - b.center[Y]) <= a.extent[Y] + b.extent[Y] &&
95 fabs(a.center[Z] - b.center[Z]) <= a.extent[Z] + b.extent[Z];
96}
97
98#endif
99
100
bool intersect(const BBox &a, const BBox &b)
Definition BBox.h:92
Scalar min(Scalar x, Scalar y)
Definition Basic.h:49
const Scalar INFINITY_
Definition Basic.h:41
@ Y
Definition Basic.h:58
@ X
Definition Basic.h:58
@ Z
Definition Basic.h:58
Scalar max(Scalar x, Scalar y)
Definition Basic.h:50
#define Scalar
Definition Basic.h:34
static Point p[4]
Definition Convex.cpp:54
Definition BBox.h:36
void enclose(const BBox &a, const BBox &b)
Definition BBox.h:52
void include(const BBox &b)
Definition BBox.h:77
Point center
Definition BBox.h:88
friend bool intersect(const BBox &a, const BBox &b)
Definition BBox.h:92
Scalar size() const
Definition BBox.h:82
Scalar getUpper(int i) const
Definition BBox.h:80
const Point & getCenter() const
Definition BBox.h:41
BBox(const Point &min, const Point &max)
Definition BBox.h:39
Vector extent
Definition BBox.h:89
const Vector & getExtent() const
Definition BBox.h:42
int longestAxis() const
Definition BBox.h:83
BBox()
Definition BBox.h:38
void include(const Point &p)
Definition BBox.h:67
Scalar getLower(int i) const
Definition BBox.h:79
void setValue(const Point &min, const Point &max)
Definition BBox.h:47
void setEmpty()
Definition BBox.h:62
void setExtent(const Vector &v)
Definition BBox.h:45
void setCenter(const Point &p)
Definition BBox.h:44
Definition Point.h:34