22 #ifndef TLP_RECTANGLE_H 23 #define TLP_RECTANGLE_H 24 #include <tulip/Vector.h> 25 #include <tulip/BoundingBox.h> 37 template <
typename Obj,
typename OTYPE =
double>
38 struct Rectangle :
public Array<Vector<Obj, 2, OTYPE>, 2> {
52 Rectangle(
const Obj xmin,
const Obj ymin,
const Obj xmax,
const Obj ymax) {
64 (*this)[0][0] = b[0][0];
65 (*this)[0][1] = b[0][1];
66 (*this)[1][0] = b[1][0];
67 (*this)[1][1] = b[1][1];
75 Rectangle(
const Vector<Obj, 2, OTYPE> &min,
const Vector<Obj, 2, OTYPE> &max) {
84 bool intersect(
const Rectangle &r)
const {
85 assert(this->isValid());
88 if ((*
this)[0][0] > r[1][0])
91 if ((*
this)[1][0] < r[0][0])
94 if ((*
this)[0][1] > r[1][1])
97 if ((*
this)[1][1] < r[0][1])
107 bool intersect(
const Rectangle &r, Rectangle &intersection)
const {
108 assert(this->isValid());
111 if (!this->intersect(r))
114 intersection[0][0] = std::max((*
this)[0][0], r[0][0]);
115 intersection[1][0] = std::min((*
this)[1][0], r[1][0]);
116 intersection[0][1] = std::max((*
this)[0][1], r[0][1]);
117 intersection[1][1] = std::min((*
this)[1][1], r[1][1]);
124 bool isValid()
const {
125 return (*
this)[0][0] <= (*this)[1][0] && (*this)[0][1] <= (*this)[1][1];
131 bool isInside(
const Vector<Obj, 2, OTYPE> &p)
const {
134 if (p[0] > (*
this)[1][0])
137 if (p[0] < (*
this)[0][0])
140 if (p[1] > (*
this)[1][1])
143 if (p[1] < (*
this)[0][1])
152 bool isInside(
const Rectangle &r)
const {
156 if ((*
this)[0] == r[0] && (*this)[1] == r[1])
159 if (this->isInside(r[0]) && this->isInside(r[1]))
169 void translate(
const tlp::Vector<Obj, 2, OTYPE> &v) {
180 return (*
this)[1][0] - (*this)[0][0];
188 return (*
this)[1][1] - (*this)[0][1];
194 Obj surface()
const {
196 return height() * width();
203 Obj aspectRatio()
const {
206 if (std::max(height(), width()) < std::numeric_limits<Obj>::epsilon())
209 return std::min(height(), width()) / std::max(height(), width());
215 Vector<Obj, 2, OTYPE> center()
const {
217 return ((*
this)[0] + (*
this)[1]) / Obj(2);
221 typedef Rectangle<double, long double> Rectd;
222 typedef Rectangle<float, double> Rectf;
223 typedef Rectangle<int, double> Recti;
224 typedef Rectangle<unsigned int, double> Rectui;
This class represents the 3D bounding box of an object. It is mostly used to determine whether or not...