Add a cuboid structure
This commit is contained in:
parent
6c095dba0b
commit
4e41e88462
2 changed files with 65 additions and 0 deletions
34
common_structures.cpp
Normal file
34
common_structures.cpp
Normal file
|
@ -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;
|
||||
}
|
|
@ -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<Face>
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue