libsbml-draw
box.h
Go to the documentation of this file.
1 /* MIT License
2  */
3 
4 //== FILEDOC =========================================================================
5 
10 //== BEGINNING OF CODE ===============================================================
11 
12 #ifndef __SBNW_LAYOUT_BOX_H_
13 #define __SBNW_LAYOUT_BOX_H_
14 
15 //== INCLUDES ========================================================================
16 
17 #include "SagittariusCore.h"
18 #include "min_max.h"
19 
20 //-- C++ code --
21 #ifdef __cplusplus
22 
23 #include "point.h" // says, <graphfab/layout/point.h>, why brackets?
24 
25 // #include <string>
26 
27 #include <iostream>
28 
29 namespace LibsbmlDraw {
30 
31  class Box {
32  public:
33  Box()
34  {}
35 
37  Box(const Point& min, const Point& max)
38  : _min(min), _max(max) { AT(_min.x <= _max.x && _min.y <= _max.y, "Min/max mismatch"); }
39 
41  Box(Real x1, Real y1, Real x2, Real y2)
42  : _min(x1,y1), _max(x2,y2) {
43  if(!(_min.x <= _max.x && _min.y <= _max.y)) {
44  dump(std::cerr);
45  std::cerr << "\n";
46  }
47  AT(_min.x <= _max.x && _min.y <= _max.y, "Min/max mismatch");
48  }
49 
51  const Point& getMin() const { return _min; }
52  Real getMinX() const { return getMin().x; }
53  Real getMinY() const { return getMin().y; }
54 
55  void setMin(const Point& p) { _min = p; }
56 
57  void setMinX(const Real x) { _min.x = x; }
58  void setMinY(const Real y) { _min.y = y; }
59 
61  const Point& getMax() const { return _max; }
62  Real getMaxX() const { return getMax().x; }
63  Real getMaxY() const { return getMax().y; }
64 
65  void setMax(const Point& p) { _max = p; }
66 
67  void setMaxX(const Real x) { _max.x = x; }
68  void setMaxY(const Real y) { _max.y = y; }
69 
70  Point getCenter() const { return (getMax() + getMin())/2.; }
71 
72  Point getFirstQuadCorner() const { return getMax(); }
73  Point getSecondQuadCorner() const { return Point(_min.x, _max.y); }
74  Point getThirdQuadCorner() const { return getMin(); }
75  Point getFourthQuadCorner() const { return Point(_max.x, _min.y); }
76 
78  Point getDiag() const { return getMax() - getMin(); }
79 
81  Real maxDim() const {
82  Real w = _max.x - _min.x;
83  Real h = _max.y - _min.y;
84  return max(w,h);
85  }
86 
88  Real minDim() const {
89  Real w = _max.x - _min.x;
90  Real h = _max.y - _min.y;
91  return min(w,h);
92  }
93 
94  Point getTopRightCorner() const { return Point(_max.x, _min.y); }
95 
96  Point getBottomLeftCorner() const { return Point(_min.x, _max.y); }
97 
99  Real width() const { return _max.x - _min.x; }
100 
102  void setWidth(const Real w) { _max.x = _min.x+w; }
103 
105  Real height() const { return _max.y - _min.y; }
106 
108  void setHeight(const Real w) { _max.y = _min.y+w; }
109 
111  Real area() const {
112  Real w = _max.x - _min.x;
113  Real h = _max.y - _min.y;
114  return w*h;
115  }
116 
118  bool canShrink(const Real v) const {
119  if(_min.x + 2*v <= _max.x && _min.y+2*v <= _max.y)
120  return true;
121  else
122  return false;
123  }
124 
126  Box shrink(const Real v) const {
127  return Box(_min+Point(v,v), _max-Point(v,v));
128  }
129 
131  void shrink_(const Real v) {
132  _min.x += v;
133  _min.y += v;
134  _max.x -= v;
135  _max.y -= v;
136  }
137 
139  Box padded(const Real v) const {
140  return Box(_min-Point(v,v), _max+Point(v,v));
141  }
142 
143  // expand! to contain the other box and this box
144  void expandx(const Box& other) {
145  _min = Point::emin(_min, other._min);
146  _max = Point::emax(_max, other._max);
147  }
148 
149  void displace(const Point& d) {
150  _min += d;
151  _max += d;
152  }
153 
154  void dump(std::ostream& o) const {
155  o << "[" << getMin() << ", " << getMax() << "]";
156  }
157 
158  protected:
160  Point _min, _max;
161  };
162 
164  std::ostream& operator<< (std::ostream& os, const Box& b);
165 
167  std::pair<bool, Point> intersectBoxLine(const Box& b, const Point& u, const Point& v);
168 
169 }
170 
171 #endif
172 
173 #endif
SAGITTARIUS_REAL Real
Make Real visible to C. Consider letting Real lie in top namespace.
Definition: SagittariusCommon.h:112
Min & max for reals.
First file included.