libsbml-draw
transform.h
Go to the documentation of this file.
1 /* MIT License
2  */
3 
4 //== FILEDOC =========================================================================
5 
10 //== BEGINNING OF CODE ===============================================================
11 
12 #ifndef __SBNW_TRANSFORM_H_
13 #define __SBNW_TRANSFORM_H_
14 
15 //== INCLUDES ========================================================================
16 
17 #include "SagittariusCore.h"
18 #include "point.h"
19 #include "box.h"
20 
21 //-- C++ code --
22 #ifdef __cplusplus
23 
24 #include <iostream>
25 
26 namespace LibsbmlDraw {
27 
28  struct cutout {
29  cutout(Real a, Real b, Real c, Real d)
30  : u(a), v(b), x(c), y(d) {}
31  Real u, v;
32  Real x, y;
33 
34  Real det() const { return u*y - v*x; }
35  };
36 
37  class _GraphfabExport Affine2d {
38  protected:
39  static Real min(Real x, Real y) {
40  if(x < y)
41  return x;
42  else
43  return y;
44  }
45  public:
46  Affine2d() {
47  _e[0] = 1.; _e[1] = 0.; _e[2] = 0.;
48  _e[3] = 0.; _e[4] = 1.; _e[5] = 0.;
49  _e[6] = 0.; _e[7] = 0.; _e[8] = 1.;
50  }
51 
52  Affine2d(Real a, Real b, Real c,
53  Real u, Real v, Real w,
54  Real x, Real y, Real z) {
55  _e[0] = a; _e[1] = b; _e[2] = c;
56  _e[3] = u; _e[4] = v; _e[5] = w;
57  _e[6] = x; _e[7] = y; _e[8] = z;
58  }
59 
61  Affine2d inv() const;
62 
64  Real det() const;
65 
67  Real rc(int r, int c) const {
68  AT(0 <= r && r < 3, "Row out of range");
69  AT(0 <= c && c < 3, "Column out of range");
70  return _e[c+r*3];
71  }
72 
74  Real& rcref(int r, int c) {
75  AT(0 <= r && r < 3, "Row out of range");
76  AT(0 <= c && c < 3, "Column out of range");
77  return _e[c+r*3];
78  }
79 
80  // Hacky
81  Real scaleFactor() const {
82  return rc(0,0);
83  }
84 
85  void set(int i, int j, Real val) { rcref(i,j) = val; }
86 
87  // Creators:
88 
89  static Affine2d makeXlate(Real x, Real y) {
90  Affine2d a;
91  a.rcref(0,2) = x;
92  a.rcref(1,2) = y;
93  return a;
94  }
95 
96  static Affine2d makeXlate(const Point& p) { return makeXlate(p.x, p.y); }
97 
98  static Affine2d makeScale(Real x, Real y) {
99  Affine2d a;
100  a.rcref(0,0) = x;
101  a.rcref(1,1) = y;
102  return a;
103  }
104 
105  // usual basis transformation - z is xlate
106  // renamed to fromBasis
107  static Affine2d fromPoints(const Point& x, const Point& y, const Point& z) {
108  return Affine2d(
109  x.x, y.x, z.x,
110  x.y, y.y, z.y,
111  0., 0., 1.
112  );
113  }
114 
116  static Affine2d fromBasis(const Point& u, const Point& v, const Point& disp) {
117  return Affine2d(
118  u.x, v.x, disp.x,
119  u.y, v.y, disp.y,
120  0, 0, 1
121  );
122  }
123 
124  static Affine2d fromBox(const Box& b) {
125  return fromPoints(Point(b.getMax().x, 0), Point(0, b.getMax().y), b.getMin());
126  }
127 
128  static Affine2d sendTo(const Box& src, const Box& dst) {
129  return fromBox(dst)*fromBox(src).inv();
130  }
131 
132  static Affine2d FitToWindow(const Box& src, const Box& dst);
133 
134  // Operations:
135 
136  Point operator*(const Point& x) const;
137 
138  Box operator*(const Box& x) const;
139 
140  Affine2d operator*(const Real& k) const;
141 
142  Affine2d operator/(const Real& k) const { return (*this)*(1./k); }
143 
144  // Composition:
145 
146  static Affine2d compose(const Affine2d& u, const Affine2d& v);
147 
148  Affine2d operator*(const Affine2d& z) const { return compose(*this, z); }
149 
151  Point applyLinearOnly(const Point& x) const;
152 
153  Point getScale() const {
154  return Point(applyLinearOnly(Point(1, 0)).mag(),
155  applyLinearOnly(Point(0, 1)).mag());
156  }
157 
158  Point getDisplacement() const {
159  return Point(rc(0, 2), rc(1, 2));
160  }
161 
162  protected:
163 
164  Affine2d cofactors() const;
165 
166  Real cofactor(int i, int j) const;
167 
168  // minor for row, col
169  cutout getCutout(int r, int c) const;
170 
171  Real _e[9];
172  };
173 
174  Point xformPoint(const Point& p, const Affine2d& t);
175  Box xformBox(const Box& b, const Affine2d& t);
176 
178  //Affine2d makeXlate(const Point& p);
179 
180  _GraphfabExport std::ostream& operator<<(std::ostream& o, const Affine2d& t);
181 
182 }
183 
184 #endif
185 
186 #endif
SAGITTARIUS_REAL Real
Make Real visible to C. Consider letting Real lie in top namespace.
Definition: SagittariusCommon.h:112
A box.
First file included.