Cuberite
A lightweight, fast and extensible game server for Minecraft
Cuboid.cpp
Go to the documentation of this file.
1 
2 #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
3 
4 #include "Cuboid.h"
5 
6 
7 
8 
9 
11 // cCuboid:
12 
13 void cCuboid::Assign(Vector3i a_Point1, Vector3i a_Point2)
14 {
15  p1 = a_Point1;
16  p2 = a_Point2;
17 }
18 
19 
20 
21 
22 
23 void cCuboid::Sort(void)
24 {
25  if (p1.x > p2.x)
26  {
27  std::swap(p1.x, p2.x);
28  }
29  if (p1.y > p2.y)
30  {
31  std::swap(p1.y, p2.y);
32  }
33  if (p1.z > p2.z)
34  {
35  std::swap(p1.z, p2.z);
36  }
37 }
38 
39 
40 
41 
42 
43 int cCuboid::GetVolume(void) const
44 {
45  int DifX = abs(p2.x - p1.x) + 1;
46  int DifY = abs(p2.y - p1.y) + 1;
47  int DifZ = abs(p2.z - p1.z) + 1;
48  return DifX * DifY * DifZ;
49 }
50 
51 
52 
53 
54 
55 bool cCuboid::IsCompletelyInside(const cCuboid & a_Outer) const
56 {
57  ASSERT(IsSorted());
58  ASSERT(a_Outer.IsSorted());
59 
60  return (
61  (p1.x >= a_Outer.p1.x) &&
62  (p2.x <= a_Outer.p2.x) &&
63  (p1.y >= a_Outer.p1.y) &&
64  (p2.y <= a_Outer.p2.y) &&
65  (p1.z >= a_Outer.p1.z) &&
66  (p2.z <= a_Outer.p2.z)
67  );
68 }
69 
70 
71 
72 
73 
74 void cCuboid::Move(Vector3i a_Offset)
75 {
76  p1.Move(a_Offset);
77  p2.Move(a_Offset);
78 }
79 
80 
81 
82 
83 
84 void cCuboid::Expand(int a_SubMinX, int a_AddMaxX, int a_SubMinY, int a_AddMaxY, int a_SubMinZ, int a_AddMaxZ)
85 {
86  if (p1.x < p2.x)
87  {
88  p1.x -= a_SubMinX;
89  p2.x += a_AddMaxX;
90  }
91  else
92  {
93  p2.x -= a_SubMinX;
94  p1.x += a_AddMaxX;
95  }
96 
97  if (p1.y < p2.y)
98  {
99  p1.y -= a_SubMinY;
100  p2.y += a_AddMaxY;
101  }
102  else
103  {
104  p2.y -= a_SubMinY;
105  p1.y += a_AddMaxY;
106  }
107 
108  if (p1.z < p2.z)
109  {
110  p1.z -= a_SubMinZ;
111  p2.z += a_AddMaxZ;
112  }
113  else
114  {
115  p2.z -= a_SubMinZ;
116  p1.z += a_AddMaxZ;
117  }
118 }
119 
120 
121 
122 
123 
124 void cCuboid::Clamp(const cCuboid & a_Limits)
125 {
126  ASSERT(IsSorted());
127  ASSERT(a_Limits.IsSorted());
128 
129  p1.x = ::Clamp(p1.x, a_Limits.p1.x, a_Limits.p2.x);
130  p1.y = ::Clamp(p1.y, a_Limits.p1.y, a_Limits.p2.y);
131  p1.z = ::Clamp(p1.z, a_Limits.p1.z, a_Limits.p2.z);
132  p2.x = ::Clamp(p2.x, a_Limits.p1.x, a_Limits.p2.x);
133  p2.y = ::Clamp(p2.y, a_Limits.p1.y, a_Limits.p2.y);
134  p2.z = ::Clamp(p2.z, a_Limits.p1.z, a_Limits.p2.z);
135 }
136 
137 
138 
139 
140 
142 {
143  ASSERT(IsSorted());
144 
145  if (p2.x - p1.x > a_MaxSize.x)
146  {
147  p2.x = p1.x + a_MaxSize.x;
148  }
149  if (p2.y - p1.y > a_MaxSize.y)
150  {
151  p2.y = p1.y + a_MaxSize.y;
152  }
153  if (p2.z - p1.z > a_MaxSize.z)
154  {
155  p2.z = p1.z + a_MaxSize.z;
156  }
157 }
158 
159 
160 
161 
162 
163 void cCuboid::ClampX(int a_MinX, int a_MaxX)
164 {
165  p1.x = ::Clamp(p1.x, a_MinX, a_MaxX);
166  p2.x = ::Clamp(p2.x, a_MinX, a_MaxX);
167 }
168 
169 
170 
171 
172 
173 void cCuboid::ClampY(int a_MinY, int a_MaxY)
174 {
175  p1.y = ::Clamp(p1.y, a_MinY, a_MaxY);
176  p2.y = ::Clamp(p2.y, a_MinY, a_MaxY);
177 }
178 
179 
180 
181 
182 
183 void cCuboid::ClampZ(int a_MinZ, int a_MaxZ)
184 {
185  p1.z = ::Clamp(p1.z, a_MinZ, a_MaxZ);
186  p2.z = ::Clamp(p2.z, a_MinZ, a_MaxZ);
187 }
188 
189 
190 
191 
192 
193 bool cCuboid::IsSorted(void) const
194 {
195  return (
196  (p1.x <= p2.x) &&
197  (p1.y <= p2.y) &&
198  (p1.z <= p2.z)
199  );
200 }
201 
202 
203 
204 
205 
207 {
208  if (a_Point.x < p1.x)
209  {
210  p1.x = a_Point.x;
211  }
212  else if (a_Point.x > p2.x)
213  {
214  p2.x = a_Point.x;
215  }
216 
217  if (a_Point.y < p1.y)
218  {
219  p1.y = a_Point.y;
220  }
221  else if (a_Point.y > p2.y)
222  {
223  p2.y = a_Point.y;
224  }
225 
226  if (a_Point.z < p1.z)
227  {
228  p1.z = a_Point.z;
229  }
230  else if (a_Point.z > p2.z)
231  {
232  p2.z = a_Point.z;
233  }
234 }
235 
236 
237 
238 
#define ASSERT(x)
Definition: Globals.h:276
Definition: Cuboid.h:10
void Expand(int a_SubMinX, int a_AddMaxX, int a_SubMinY, int a_AddMaxY, int a_SubMinZ, int a_AddMaxZ)
Expands the cuboid by the specified amount in each direction.
Definition: Cuboid.cpp:84
void ClampSize(Vector3i a_MaxSize)
Clamps this cuboid's p2 so that the cuboid's size doesn't exceed the specified max size.
Definition: Cuboid.cpp:141
Vector3i p2
Definition: Cuboid.h:13
int DifX(void) const
Definition: Cuboid.h:33
int GetVolume(void) const
Returns the volume of the cuboid, in blocks.
Definition: Cuboid.cpp:43
void Assign(Vector3i a_Point1, Vector3i a_Point2)
Definition: Cuboid.cpp:13
void ClampX(int a_MinX, int a_MaxX)
Clamps both X coords to the specified range.
Definition: Cuboid.cpp:163
bool IsSorted(void) const
Returns true if the coords are properly sorted (lesser in p1, greater in p2)
Definition: Cuboid.cpp:193
void Clamp(const cCuboid &a_Limits)
Clamps this cuboid, so that it doesn't reach outside of a_Limits in any direction.
Definition: Cuboid.cpp:124
void ClampZ(int a_MinZ, int a_MaxZ)
Clamps both Z coords to the specified range.
Definition: Cuboid.cpp:183
void Move(Vector3i a_Offset)
Moves the cuboid by the specified offset.
Definition: Cuboid.cpp:74
int DifZ(void) const
Definition: Cuboid.h:35
void Sort(void)
Definition: Cuboid.cpp:23
void Engulf(Vector3i a_Point)
If needed, expands the cuboid so that it contains the specified point.
Definition: Cuboid.cpp:206
int DifY(void) const
Definition: Cuboid.h:34
Vector3i p1
Definition: Cuboid.h:13
bool IsCompletelyInside(const cCuboid &a_Outer) const
Returns true if this cuboid is completely inside the specified cuboid (in all 6 coords).
Definition: Cuboid.cpp:55
void ClampY(int a_MinY, int a_MaxY)
Clamps both Y coords to the specified range.
Definition: Cuboid.cpp:173
T x
Definition: Vector3.h:17
void Move(T a_X, T a_Y, T a_Z)
Definition: Vector3.h:162
T y
Definition: Vector3.h:17
T z
Definition: Vector3.h:17