Cuberite
A lightweight, fast and extensible game server for Minecraft
Cuboid.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 
5 
6 
7 
8 // tolua_begin
9 class cCuboid
10 {
11 public:
12  // p1 is expected to have the smaller of the coords; Sort() swaps coords to match this
14 
15  cCuboid(void) {}
16  cCuboid(Vector3i a_p1, Vector3i a_p2) : p1(a_p1), p2(a_p2) {}
17  cCuboid(int a_X1, int a_Y1, int a_Z1) : p1(a_X1, a_Y1, a_Z1), p2(a_X1, a_Y1, a_Z1) {}
18 
19  #ifdef TOLUA_EXPOSITION // tolua isn't aware of implicitly generated copy constructors
20  cCuboid(const cCuboid & a_Cuboid);
21  #endif
22 
23  // tolua_end
24  // Exported in ManualBindings.cpp to support the old deprecated coord-based overload.
25 
26  void Assign(Vector3i a_Point1, Vector3i a_Point2);
27  void Assign(const cCuboid & a_SrcCuboid) { *this = a_SrcCuboid; }
28 
29  // tolua_begin
30 
31  void Sort(void);
32 
33  int DifX(void) const { return p2.x - p1.x; }
34  int DifY(void) const { return p2.y - p1.y; }
35  int DifZ(void) const { return p2.z - p1.z; }
36 
40  int GetVolume(void) const;
41 
44  inline bool DoesIntersect(const cCuboid & a_Other) const
45  {
46  ASSERT(IsSorted());
47  ASSERT(a_Other.IsSorted());
48 
49  // In order for cuboids to intersect, each of their coord intervals need to intersect
50  return (
51  DoIntervalsIntersect(p1.x, p2.x, a_Other.p1.x, a_Other.p2.x) &&
52  DoIntervalsIntersect(p1.y, p2.y, a_Other.p1.y, a_Other.p2.y) &&
53  DoIntervalsIntersect(p1.z, p2.z, a_Other.p1.z, a_Other.p2.z)
54  );
55  }
56 
57  // tolua_end
58  // Exported in ManualBindings.cpp to support the old deprecated coord-based overload.
59 
60  bool IsInside(Vector3i v) const
61  {
62  return (
63  (v.x >= p1.x) && (v.x <= p2.x) &&
64  (v.y >= p1.y) && (v.y <= p2.y) &&
65  (v.z >= p1.z) && (v.z <= p2.z)
66  );
67  }
68 
69  bool IsInside(Vector3d v) const
70  {
71  return (
72  (v.x >= p1.x) && (v.x <= p2.x) &&
73  (v.y >= p1.y) && (v.y <= p2.y) &&
74  (v.z >= p1.z) && (v.z <= p2.z)
75  );
76  }
77 
78  // tolua_begin
79 
82  bool IsCompletelyInside(const cCuboid & a_Outer) const;
83 
84  // tolua_end
85 
88  void Move(Vector3i a_Offset);
89 
90  // tolua_begin
91 
95  void Expand(int a_SubMinX, int a_AddMaxX, int a_SubMinY, int a_AddMaxY, int a_SubMinZ, int a_AddMaxZ);
96 
99  void Clamp(const cCuboid & a_Limits);
100 
103  void ClampSize(Vector3i a_MaxSize);
104 
106  void ClampX(int a_MinX, int a_MaxX);
107 
109  void ClampY(int a_MinY, int a_MaxY);
110 
112  void ClampZ(int a_MinZ, int a_MaxZ);
113 
115  bool IsSorted(void) const;
116 
118  void Engulf(Vector3i a_Point);
119 
120  // tolua_end
121 
123  bool operator == (const cCuboid & aOther) const
124  {
125  return (
126  (p1.x == aOther.p1.x) &&
127  (p1.y == aOther.p1.y) &&
128  (p1.z == aOther.p1.z) &&
129  (p2.x == aOther.p2.x) &&
130  (p2.y == aOther.p2.y) &&
131  (p2.z == aOther.p2.z)
132  );
133  }
134 
135  bool operator != (const cCuboid & aOther) const
136  {
137  return !operator ==(aOther);
138  }
139 
140 
141  // tolua_begin
142 
143 private:
144 
146  inline static bool DoIntervalsIntersect(int a_Min1, int a_Max1, int a_Min2, int a_Max2)
147  {
148  ASSERT(a_Min1 <= a_Max1);
149  ASSERT(a_Min2 <= a_Max2);
150  return ((a_Min1 <= a_Max2) && (a_Max1 >= a_Min2));
151  }
152 
153 } ;
154 // tolua_end
155 
156 
157 
158 
cCuboid::p2
Vector3i p2
Definition: Cuboid.h:13
Vector3::x
T x
Definition: Vector3.h:17
cCuboid::DifX
int DifX(void) const
Definition: Cuboid.h:33
cCuboid::Assign
void Assign(const cCuboid &a_SrcCuboid)
Definition: Cuboid.h:27
cCuboid::IsInside
bool IsInside(Vector3d v) const
Definition: Cuboid.h:69
cCuboid::IsSorted
bool IsSorted(void) const
Returns true if the coords are properly sorted (lesser in p1, greater in p2)
Definition: Cuboid.cpp:193
cCuboid::ClampZ
void ClampZ(int a_MinZ, int a_MaxZ)
Clamps both Z coords to the specified range.
Definition: Cuboid.cpp:183
cCuboid::cCuboid
cCuboid(int a_X1, int a_Y1, int a_Z1)
Definition: Cuboid.h:17
cCuboid::operator!=
bool operator!=(const cCuboid &aOther) const
Definition: Cuboid.h:135
cCuboid::DifY
int DifY(void) const
Definition: Cuboid.h:34
ASSERT
#define ASSERT(x)
Definition: Globals.h:273
Vector3::z
T z
Definition: Vector3.h:17
cCuboid::DoIntervalsIntersect
static bool DoIntervalsIntersect(int a_Min1, int a_Max1, int a_Min2, int a_Max2)
Returns true if the two specified intervals have a non-empty union.
Definition: Cuboid.h:146
cCuboid::operator==
bool operator==(const cCuboid &aOther) const
Checks the two cuboids for equality.
Definition: Cuboid.h:123
cCuboid::Engulf
void Engulf(Vector3i a_Point)
If needed, expands the cuboid so that it contains the specified point.
Definition: Cuboid.cpp:206
cCuboid::Clamp
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
cCuboid::Assign
void Assign(Vector3i a_Point1, Vector3i a_Point2)
Definition: Cuboid.cpp:13
cCuboid::ClampX
void ClampX(int a_MinX, int a_MaxX)
Clamps both X coords to the specified range.
Definition: Cuboid.cpp:163
cCuboid::cCuboid
cCuboid(void)
Definition: Cuboid.h:15
cCuboid::p1
Vector3i p1
Definition: Cuboid.h:13
cCuboid::DoesIntersect
bool DoesIntersect(const cCuboid &a_Other) const
Returns true if the cuboids have at least one voxel in common.
Definition: Cuboid.h:44
cCuboid::DifZ
int DifZ(void) const
Definition: Cuboid.h:35
cCuboid::cCuboid
cCuboid(Vector3i a_p1, Vector3i a_p2)
Definition: Cuboid.h:16
cCuboid::Sort
void Sort(void)
Definition: Cuboid.cpp:23
cCuboid::IsInside
bool IsInside(Vector3i v) const
Definition: Cuboid.h:60
cCuboid::ClampY
void ClampY(int a_MinY, int a_MaxY)
Clamps both Y coords to the specified range.
Definition: Cuboid.cpp:173
cCuboid::Move
void Move(Vector3i a_Offset)
Moves the cuboid by the specified offset.
Definition: Cuboid.cpp:74
cCuboid::IsCompletelyInside
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
Vector3::y
T y
Definition: Vector3.h:17
Vector3< int >
cCuboid::GetVolume
int GetVolume(void) const
Returns the volume of the cuboid, in blocks.
Definition: Cuboid.cpp:43
cCuboid::ClampSize
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
cCuboid
Definition: Cuboid.h:9
cCuboid::Expand
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