summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/Cuboid.cpp82
-rw-r--r--src/Cuboid.h27
2 files changed, 105 insertions, 4 deletions
diff --git a/src/Cuboid.cpp b/src/Cuboid.cpp
index ea6f7c453..782837b23 100644
--- a/src/Cuboid.cpp
+++ b/src/Cuboid.cpp
@@ -58,6 +58,18 @@ void cCuboid::Sort(void)
+int cCuboid::GetVolume(void) const
+{
+ int DifX = abs(p2.x - p1.x) + 1;
+ int DifY = abs(p2.y - p1.y) + 1;
+ int DifZ = abs(p2.z - p1.z) + 1;
+ return DifX * DifY * DifZ;
+}
+
+
+
+
+
bool cCuboid::DoesIntersect(const cCuboid & a_Other) const
{
// In order for cuboids to intersect, each of their coord intervals need to intersect
@@ -103,6 +115,76 @@ void cCuboid::Move(int a_OfsX, int a_OfsY, int a_OfsZ)
+void cCuboid::Expand(int a_SubMinX, int a_AddMaxX, int a_SubMinY, int a_AddMaxY, int a_SubMinZ, int a_AddMaxZ)
+{
+ if (p1.x < p2.x)
+ {
+ p1.x -= a_SubMinX;
+ p2.x += a_AddMaxX;
+ }
+ else
+ {
+ p2.x -= a_SubMinX;
+ p1.x += a_AddMaxX;
+ }
+
+ if (p1.y < p2.y)
+ {
+ p1.y -= a_SubMinY;
+ p2.y += a_AddMaxY;
+ }
+ else
+ {
+ p2.y -= a_SubMinY;
+ p1.y += a_AddMaxY;
+ }
+
+ if (p1.z < p2.z)
+ {
+ p1.z -= a_SubMinZ;
+ p2.z += a_AddMaxZ;
+ }
+ else
+ {
+ p2.z -= a_SubMinZ;
+ p1.z += a_AddMaxZ;
+ }
+}
+
+
+
+
+
+void cCuboid::ClampX(int a_MinX, int a_MaxX)
+{
+ p1.x = Clamp(p1.x, a_MinX, a_MaxX);
+ p2.x = Clamp(p2.x, a_MinX, a_MaxX);
+}
+
+
+
+
+
+void cCuboid::ClampY(int a_MinY, int a_MaxY)
+{
+ p1.y = Clamp(p1.y, a_MinY, a_MaxY);
+ p2.y = Clamp(p2.y, a_MinY, a_MaxY);
+}
+
+
+
+
+
+void cCuboid::ClampZ(int a_MinZ, int a_MaxZ)
+{
+ p1.z = Clamp(p1.z, a_MinZ, a_MaxZ);
+ p2.z = Clamp(p2.z, a_MinZ, a_MaxZ);
+}
+
+
+
+
+
bool cCuboid::IsSorted(void) const
{
return (
diff --git a/src/Cuboid.h b/src/Cuboid.h
index 44db7b98e..51ccf799b 100644
--- a/src/Cuboid.h
+++ b/src/Cuboid.h
@@ -29,7 +29,12 @@ public:
int DifY(void) const { return p2.y - p1.y; }
int DifZ(void) const { return p2.z - p1.z; }
- /// Returns true if the cuboids have at least one voxel in common. Both coords are considered inclusive.
+ /** Returns the volume of the cuboid, in blocks.
+ Note that the volume considers both coords inclusive.
+ Works on unsorted cuboids, too. */
+ int GetVolume(void) const;
+
+ /** Returns true if the cuboids have at least one voxel in common. Both coords are considered inclusive. */
bool DoesIntersect(const cCuboid & a_Other) const;
bool IsInside(const Vector3i & v) const
@@ -59,13 +64,27 @@ public:
);
}
- /// Returns true if this cuboid is completely inside the specifie cuboid (in all 6 coords)
+ /** Returns true if this cuboid is completely inside the specifie cuboid (in all 6 coords) */
bool IsCompletelyInside(const cCuboid & a_Outer) const;
- /// Moves the cuboid by the specified offsets in each direction
+ /** Moves the cuboid by the specified offsets in each direction */
void Move(int a_OfsX, int a_OfsY, int a_OfsZ);
+
+ /** Expands the cuboid by the specified amount in each direction.
+ Works on unsorted cuboids as well.
+ Note that this function doesn't check for underflows. */
+ void Expand(int a_SubMinX, int a_AddMaxX, int a_SubMinY, int a_AddMaxY, int a_SubMinZ, int a_AddMaxZ);
+
+ /** Clamps both X coords to the specified range. Works on unsorted cuboids, too. */
+ void ClampX(int a_MinX, int a_MaxX);
+
+ /** Clamps both Y coords to the specified range. Works on unsorted cuboids, too. */
+ void ClampY(int a_MinY, int a_MaxY);
+
+ /** Clamps both Z coords to the specified range. Works on unsorted cuboids, too. */
+ void ClampZ(int a_MinZ, int a_MaxZ);
- /// Returns true if the coords are properly sorted (lesser in p1, greater in p2)
+ /** Returns true if the coords are properly sorted (lesser in p1, greater in p2) */
bool IsSorted(void) const;
} ;
// tolua_end