From 4e41e88462100d33fcc26585400791fa6cec0e2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Bastian?= Date: Wed, 7 Feb 2018 17:59:41 +0100 Subject: [PATCH] Add a cuboid structure --- common_structures.cpp | 34 ++++++++++++++++++++++++++++++++++ common_structures.hpp | 31 +++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 common_structures.cpp diff --git a/common_structures.cpp b/common_structures.cpp new file mode 100644 index 0000000..37efd04 --- /dev/null +++ b/common_structures.cpp @@ -0,0 +1,34 @@ +#include "common_structures.hpp" + +Cuboid::Cuboid(Point bd1, Point bd2) + : lowBound(0, 0, 0), highBound(0, 0, 0) +{ + lowBound = Point( + std::min(bd1.x, bd2.x), + std::min(bd1.y, bd2.y), + std::min(bd1.z, bd2.z)); + highBound = Point( + std::max(bd1.x, bd2.x), + std::max(bd1.y, bd2.y), + std::max(bd1.z, bd2.z)); +} + +double Cuboid::low(unsigned dim) const { + assert(dim < 3); + return lowBound[dim]; +} + +double Cuboid::high(unsigned dim) const { + assert(dim < 3); + return highBound[dim]; +} + +double Cuboid::volume() const { + return (high(0) - low(0)) + * (high(1) - low(1)) + * (high(2) - low(2)); +} + +bool Cuboid::is_empty() const { + return volume() < 1e-8; +} diff --git a/common_structures.hpp b/common_structures.hpp index adf9d9f..9a8971e 100644 --- a/common_structures.hpp +++ b/common_structures.hpp @@ -45,6 +45,16 @@ struct Point { scalar * pt.y, scalar * pt.z); } + + bool operator<(const Point& pt) const { + /// Lexicographic order on (x, y, z) + if(x == pt.x) { + if(y == pt.y) + return z < pt.z; + return y < pt.y; + } + return x < pt.x; + } }; struct Face { @@ -63,6 +73,27 @@ struct Face { } }; +class Cuboid { + /// A 3D box + + public: + static Cuboid empty() { + return Cuboid(Point(0, 0, 0), Point(0, 0, 0)); + } + + Cuboid(Point bd1, Point bd2); + Cuboid(const Cuboid& oth) + : lowBound(oth.lowBound), highBound(oth.highBound) {} + + double low(unsigned dim) const; ///< Lower bound for a dimension + double high(unsigned dim) const; ///< Higher bound for a dimension + + double volume() const; + bool is_empty() const; + private: + Point lowBound, highBound; +}; + namespace std { template<> struct hash {