ScummVM API documentation
box.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef ULTIMA8_MISC_BOX_H
23 #define ULTIMA8_MISC_BOX_H
24 
25 #include "common/scummsys.h"
26 
27 namespace Ultima {
28 namespace Ultima8 {
29 
36 struct Box {
37  int32 _x, _y, _z;
38  int32 _xd, _yd, _zd;
39 
40  Box() : _x(0), _y(0), _z(0), _xd(0), _yd(0), _zd(0) {}
41  Box(int nx, int ny, int nz, int nxd, int nyd, int nzd)
42  : _x(nx), _y(ny), _z(nz), _xd(nxd), _yd(nyd), _zd(nzd) {}
43 
44  // Check if the Box is empty (its width, height, or depth is 0) or invalid (its width, height, or depth are negative).
45  bool isEmpty() const {
46  return _xd <= 0 || _yd <= 0 || _zd <= 0;
47  }
48 
49  // Check to see if a Box is 'valid'
50  bool isValid() const {
51  return _xd >= 0 && _yd >= 0 && _zd >= 0;
52  }
53 
54  // Check to see if a point is within the Box
55  bool contains(int32 px, int32 py, int32 pz) const {
56  return px > _x - _xd && px <= _x &&
57  py > _y - _yd && py <= _y &&
58  pz >= _z && pz < _z + _zd;
59  }
60 
61  // Check to see if a 2d point is within the XY of the Box
62  bool containsXY(int32 px, int32 py) const {
63  return px > _x - _xd && px <= _x &&
64  py > _y - _yd && py <= _y;
65  }
66 
67  // Check to see if the box is below a point
68  bool isBelow(int32 px, int32 py, int32 pz) const {
69  return px > _x - _xd && px <= _x &&
70  py > _y - _yd && py <= _y &&
71  pz >= _z + _zd;
72  }
73 
74  // Move the Box (Relative)
75  void translate(int32 dx, int32 dy, int32 dz) {
76  _x += dx;
77  _y += dy;
78  _z += dz;
79  }
80 
81  // Move the Box (Absolute)
82  void moveTo(int32 nx, int32 ny, int32 nz) {
83  _x = nx;
84  _y = ny;
85  _z = nz;
86  }
87 
88  // Resize the Box (Absolute)
89  void resize(int32 nxd, int32 nyd, int32 nzd) {
90  _xd = nxd;
91  _yd = nyd;
92  _zd = nzd;
93  }
94 
95  bool overlaps(const Box &o) const {
96  if (_x <= o._x - o._xd || o._x <= _x - _xd)
97  return false;
98  if (_y <= o._y - o._yd || o._y <= _y - _yd)
99  return false;
100  if (_z + _zd <= o._z || o._z + o._zd <= _z)
101  return false;
102  return true;
103  }
104 
105  bool overlapsXY(const Box& o) const {
106  if (_x <= o._x - o._xd || o._x <= _x - _xd)
107  return false;
108  if (_y <= o._y - o._yd || o._y <= _y - _yd)
109  return false;
110  return true;
111  }
112 
113  void extend(const Box &o) {
114  int32 x2 = MIN(_x - _xd, o._x - o._xd);
115  int32 y2 = MIN(_y - _yd, o._y - o._yd);
116  int32 z2 = MAX(_z + _zd, o._z + o._zd);
117 
118  _x = MAX(_x, o._x);
119  _y = MAX(_y, o._y);
120  _z = MIN(_z, o._z);
121  _xd = _x - x2;
122  _yd = _y - y2;
123  _zd = z2 - _z;
124  }
125 
126  bool operator==(const Box &rhs) const { return equals(rhs); }
127  bool operator!=(const Box &rhs) const { return !equals(rhs); }
128 
129  bool equals(const Box &o) const {
130  return (_x == o._x && _y == o._y && _z == o._z &&
131  _xd == o._xd && _yd == o._yd && _zd == o._zd);
132  }
133 
134 };
135 
136 } // End of namespace Ultima8
137 } // End of namespace Ultima
138 
139 #endif
Definition: box.h:36
Definition: detection.h:27
T MIN(T a, T b)
Definition: util.h:59
T MAX(T a, T b)
Definition: util.h:62